Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / android / CHIPTool / app / src / main / java / com / google / chip / chiptool / echoclient / EchoClientFragment.kt
1 /*
2  *   Copyright (c) 2020 Project CHIP Authors
3  *   All rights reserved.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  *
17  */
18 package com.google.chip.chiptool.echoclient
19
20 import android.os.Bundle
21 import android.util.Log
22 import android.view.LayoutInflater
23 import android.view.View
24 import android.view.ViewGroup
25 import androidx.fragment.app.Fragment
26 import chip.devicecontroller.ChipDeviceController
27 import com.google.chip.chiptool.ChipClient
28 import com.google.chip.chiptool.GenericChipDeviceListener
29 import com.google.chip.chiptool.R
30 import com.google.chip.chiptool.util.DeviceIdUtil
31 import kotlinx.android.synthetic.main.echo_client_fragment.*
32 import kotlinx.android.synthetic.main.echo_client_fragment.view.*
33 import kotlinx.android.synthetic.main.on_off_client_fragment.deviceIdEd
34
35 /** Sends echo messages to the CHIP device. */
36 class EchoClientFragment : Fragment() {
37
38   private val deviceController: ChipDeviceController
39     get() = ChipClient.getDeviceController()
40
41   override fun onCreateView(
42       inflater: LayoutInflater,
43       container: ViewGroup?,
44       savedInstanceState: Bundle?
45   ): View {
46     return inflater.inflate(R.layout.echo_client_fragment, container, false).apply {
47       deviceController.setCompletionListener(ChipControllerCallback())
48
49       inputTextEd.hint = requireContext().getString(R.string.echo_input_hint_text)
50
51       sendCommandBtn.setOnClickListener { sendEcho() }
52     }
53   }
54
55   override fun onStart() {
56     super.onStart()
57     deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString())
58   }
59
60   inner class ChipControllerCallback : GenericChipDeviceListener() {
61     override fun onConnectDeviceComplete() {
62       sendEcho()
63     }
64
65     override fun onSendMessageComplete(message: String?) {
66       commandStatusTv.text = requireContext().getString(R.string.echo_status_response, message)
67     }
68
69     override fun onNotifyChipConnectionClosed() {
70       Log.d(TAG, "onNotifyChipConnectionClosed")
71     }
72
73     override fun onCloseBleComplete() {
74       Log.d(TAG, "onCloseBleComplete")
75     }
76
77     override fun onError(error: Throwable?) {
78       Log.d(TAG, "onError: $error")
79     }
80   }
81
82   private fun sendEcho() {
83     commandStatusTv.text = requireContext().getString(R.string.echo_status_sending_message)
84
85     val echoEditTextContents = inputTextEd.text.toString()
86     val echoText = if (echoEditTextContents.isEmpty()) DEFAULT_ECHO_MSG else echoEditTextContents
87     deviceController.sendMessage(deviceIdEd.text.toString().toLong(), echoText)
88   }
89
90   companion object {
91     private const val TAG = "EchoClientFragment"
92     private const val DEFAULT_ECHO_MSG = "Hello! from the Android CHIP demo app"
93
94     fun newInstance(): EchoClientFragment = EchoClientFragment()
95   }
96 }