Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / android / CHIPTool / app / src / main / java / com / google / chip / chiptool / clusterclient / OnOffClientFragment.kt
1 package com.google.chip.chiptool.clusterclient
2
3 import android.os.Bundle
4 import android.util.Log
5 import android.view.LayoutInflater
6 import android.view.View
7 import android.view.ViewGroup
8 import android.widget.SeekBar
9 import android.widget.Toast
10 import androidx.fragment.app.Fragment
11 import chip.devicecontroller.ChipCommandType
12 import chip.devicecontroller.ChipDeviceController
13 import chip.devicecontroller.ChipDeviceControllerException
14 import com.google.chip.chiptool.ChipClient
15 import com.google.chip.chiptool.GenericChipDeviceListener
16 import com.google.chip.chiptool.R
17 import com.google.chip.chiptool.util.DeviceIdUtil
18 import kotlinx.android.synthetic.main.on_off_client_fragment.*
19 import kotlinx.android.synthetic.main.on_off_client_fragment.view.*
20
21 class OnOffClientFragment : Fragment() {
22   private val deviceController: ChipDeviceController
23     get() = ChipClient.getDeviceController()
24
25   private var commandType: ChipCommandType? = null
26   private var levelValue: Int? = null
27
28   override fun onCreateView(
29       inflater: LayoutInflater,
30       container: ViewGroup?,
31       savedInstanceState: Bundle?
32   ): View {
33     return inflater.inflate(R.layout.on_off_client_fragment, container, false).apply {
34       deviceController.setCompletionListener(ChipControllerCallback())
35
36       onBtn.setOnClickListener { sendOnCommandClick() }
37       offBtn.setOnClickListener { sendOffCommandClick() }
38       toggleBtn.setOnClickListener { sendToggleCommandClick() }
39
40       levelBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
41         override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
42
43         }
44
45         override fun onStartTrackingTouch(seekBar: SeekBar?) {
46         }
47
48         override fun onStopTrackingTouch(seekBar: SeekBar?) {
49           Toast.makeText(requireContext(),
50                   "Level is: " + levelBar.progress,
51                   Toast.LENGTH_SHORT).show()
52           commandType = ChipCommandType.LEVEL
53           levelValue = levelBar.progress
54           sendCommand()
55         }
56       })
57     }
58   }
59
60   override fun onStart() {
61     super.onStart()
62     deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString())
63   }
64
65   inner class ChipControllerCallback : GenericChipDeviceListener() {
66     override fun onConnectDeviceComplete() {
67       sendCommand()
68     }
69
70     override fun onSendMessageComplete(message: String?) {
71       commandStatusTv.text = requireContext().getString(R.string.echo_status_response, message)
72     }
73
74     override fun onNotifyChipConnectionClosed() {
75       Log.d(TAG, "onNotifyChipConnectionClosed")
76     }
77
78     override fun onCloseBleComplete() {
79       Log.d(TAG, "onCloseBleComplete")
80     }
81
82     override fun onError(error: Throwable?) {
83       Log.d(TAG, "onError: $error")
84     }
85   }
86
87   private fun sendOnCommandClick() {
88     commandType = ChipCommandType.ON
89     levelValue = 0
90     sendCommand()
91   }
92
93   private fun sendOffCommandClick() {
94     commandType = ChipCommandType.OFF
95     levelValue = 0
96     sendCommand()
97   }
98
99   private fun sendToggleCommandClick() {
100     commandType = ChipCommandType.TOGGLE
101     levelValue = 0
102     sendCommand()
103   }
104
105   private fun sendCommand() {
106     val chipCommandType = commandType ?: run {
107       Log.e(TAG, "No ChipCommandType specified.")
108       return
109     }
110
111     commandStatusTv.text = requireContext()
112       .getString(R.string.send_command_type_label_text, chipCommandType.name, levelValue)
113
114     try {
115       // mask levelValue from integer to uint8_t and if null use 0
116       deviceController.sendCommand(
117         DeviceIdUtil.getLastDeviceId(requireContext()),
118         commandType,
119         ( 0xff and (levelValue ?: 0))
120       )
121     } catch (e: ChipDeviceControllerException) {
122       commandStatusTv.text = e.toString()
123     }
124   }
125
126   companion object {
127     private const val TAG = "OnOffClientFragment"
128     fun newInstance(): OnOffClientFragment = OnOffClientFragment()
129   }
130 }