2 * Copyright (c) 2020 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package com.google.chip.chiptool
20 import android.Manifest
21 import android.content.pm.PackageManager
22 import android.os.Bundle
23 import android.view.LayoutInflater
24 import android.view.View
25 import android.view.ViewGroup
26 import androidx.activity.result.contract.ActivityResultContracts
27 import androidx.appcompat.app.AlertDialog
28 import androidx.core.content.ContextCompat
29 import androidx.fragment.app.Fragment
30 import com.google.chip.chiptool.util.FragmentUtil
31 import kotlinx.android.synthetic.main.select_action_fragment.provisionThreadCredentialsBtn
32 import kotlinx.android.synthetic.main.select_action_fragment.provisionWifiCredentialsBtn
33 import kotlinx.android.synthetic.main.select_action_fragment.view.*
35 /** Fragment to select from various options to interact with a CHIP device. */
36 class SelectActionFragment : Fragment() {
38 override fun onCreateView(
39 inflater: LayoutInflater,
40 container: ViewGroup?,
41 savedInstanceState: Bundle?
43 return inflater.inflate(R.layout.select_action_fragment, container, false).apply {
44 scanQrBtn.setOnClickListener { getCallback()?.handleScanQrCodeClicked() }
45 provisionWifiCredentialsBtn.apply {
46 isEnabled = hasLocationPermission()
47 setOnClickListener { getCallback()?.onProvisionWifiCredentialsClicked() }
49 provisionThreadCredentialsBtn.apply {
50 isEnabled = hasLocationPermission()
51 setOnClickListener { getCallback()?.onProvisionThreadCredentialsClicked() }
53 otCommissioningBtn.setOnClickListener { getCallback()?.handleCommissioningClicked() }
54 echoClientBtn.setOnClickListener { getCallback()?.handleEchoClientClicked() }
55 onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
56 attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() }
60 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
61 super.onViewCreated(view, savedInstanceState)
63 if (savedInstanceState != null) return
64 if (hasLocationPermission()) return
66 val permissionRequest = registerForActivityResult(
67 ActivityResultContracts.RequestPermission()
70 provisionWifiCredentialsBtn.isEnabled = true
71 provisionThreadCredentialsBtn.isEnabled = true
73 provisionWifiCredentialsBtn.isEnabled = false
74 provisionThreadCredentialsBtn.isEnabled = false
76 AlertDialog.Builder(requireContext())
77 .setTitle(R.string.location_permission_denied_title)
78 .setMessage(R.string.location_permission_denied_message)
79 .setPositiveButton(R.string.text_ok) { dialog, _ -> dialog.dismiss() }
86 permissionRequest.launch(Manifest.permission.ACCESS_FINE_LOCATION)
89 private fun hasLocationPermission(): Boolean {
90 val locationPermissionState =
91 ContextCompat.checkSelfPermission(
93 Manifest.permission.ACCESS_FINE_LOCATION
96 return PackageManager.PERMISSION_GRANTED == locationPermissionState
99 private fun getCallback() = FragmentUtil.getHost(this, Callback::class.java)
101 /** Interface for notifying the host. */
103 /** Notifies listener of Scan QR code button click. */
104 fun handleScanQrCodeClicked()
105 /** Notifies listener of provision-Wifi-credentials button click. */
106 fun onProvisionWifiCredentialsClicked()
107 /** Notifies listener of provision-Thread-credentials button click. */
108 fun onProvisionThreadCredentialsClicked()
109 /** Notifies listener of Commissioning button click. */
110 fun handleCommissioningClicked()
111 /** Notifies listener of Echo client button click. */
112 fun handleEchoClientClicked()
113 /** Notifies listener of send command button click. */
114 fun handleOnOffClicked()
115 /** Notifies listener of attestation command button clicked. */
116 fun handleAttestationTestClicked()
121 @JvmStatic fun newInstance() = SelectActionFragment()