1 package com.google.chip.chiptool.clusterclient
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.*
21 class OnOffClientFragment : Fragment() {
22 private val deviceController: ChipDeviceController
23 get() = ChipClient.getDeviceController()
25 private var commandType: ChipCommandType? = null
26 private var levelValue: Int? = null
28 override fun onCreateView(
29 inflater: LayoutInflater,
30 container: ViewGroup?,
31 savedInstanceState: Bundle?
33 return inflater.inflate(R.layout.on_off_client_fragment, container, false).apply {
34 deviceController.setCompletionListener(ChipControllerCallback())
36 onBtn.setOnClickListener { sendOnCommandClick() }
37 offBtn.setOnClickListener { sendOffCommandClick() }
38 toggleBtn.setOnClickListener { sendToggleCommandClick() }
40 levelBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
41 override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
45 override fun onStartTrackingTouch(seekBar: SeekBar?) {
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
60 override fun onStart() {
62 deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString())
65 inner class ChipControllerCallback : GenericChipDeviceListener() {
66 override fun onConnectDeviceComplete() {
70 override fun onSendMessageComplete(message: String?) {
71 commandStatusTv.text = requireContext().getString(R.string.echo_status_response, message)
74 override fun onNotifyChipConnectionClosed() {
75 Log.d(TAG, "onNotifyChipConnectionClosed")
78 override fun onCloseBleComplete() {
79 Log.d(TAG, "onCloseBleComplete")
82 override fun onError(error: Throwable?) {
83 Log.d(TAG, "onError: $error")
87 private fun sendOnCommandClick() {
88 commandType = ChipCommandType.ON
93 private fun sendOffCommandClick() {
94 commandType = ChipCommandType.OFF
99 private fun sendToggleCommandClick() {
100 commandType = ChipCommandType.TOGGLE
105 private fun sendCommand() {
106 val chipCommandType = commandType ?: run {
107 Log.e(TAG, "No ChipCommandType specified.")
111 commandStatusTv.text = requireContext()
112 .getString(R.string.send_command_type_label_text, chipCommandType.name, levelValue)
115 // mask levelValue from integer to uint8_t and if null use 0
116 deviceController.sendCommand(
117 DeviceIdUtil.getLastDeviceId(requireContext()),
119 ( 0xff and (levelValue ?: 0))
121 } catch (e: ChipDeviceControllerException) {
122 commandStatusTv.text = e.toString()
127 private const val TAG = "OnOffClientFragment"
128 fun newInstance(): OnOffClientFragment = OnOffClientFragment()