Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / android / CHIPTool / app / src / main / java / com / google / chip / chiptool / SelectActionFragment.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
19
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.*
34
35 /** Fragment to select from various options to interact with a CHIP device. */
36 class SelectActionFragment : Fragment() {
37
38   override fun onCreateView(
39       inflater: LayoutInflater,
40       container: ViewGroup?,
41       savedInstanceState: Bundle?
42   ): View {
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() }
48       }
49       provisionThreadCredentialsBtn.apply {
50         isEnabled = hasLocationPermission()
51         setOnClickListener { getCallback()?.onProvisionThreadCredentialsClicked() }
52       }
53       otCommissioningBtn.setOnClickListener { getCallback()?.handleCommissioningClicked() }
54       echoClientBtn.setOnClickListener { getCallback()?.handleEchoClientClicked() }
55       onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
56       attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() }
57     }
58   }
59
60   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
61     super.onViewCreated(view, savedInstanceState)
62
63     if (savedInstanceState != null) return
64     if (hasLocationPermission()) return
65
66     val permissionRequest = registerForActivityResult(
67         ActivityResultContracts.RequestPermission()
68     ) { granted ->
69       if (granted) {
70         provisionWifiCredentialsBtn.isEnabled = true
71         provisionThreadCredentialsBtn.isEnabled = true
72       } else {
73         provisionWifiCredentialsBtn.isEnabled = false
74         provisionThreadCredentialsBtn.isEnabled = false
75
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() }
80             .setCancelable(false)
81             .create()
82             .show()
83       }
84     }
85
86     permissionRequest.launch(Manifest.permission.ACCESS_FINE_LOCATION)
87   }
88
89   private fun hasLocationPermission(): Boolean {
90     val locationPermissionState =
91         ContextCompat.checkSelfPermission(
92             requireContext(),
93             Manifest.permission.ACCESS_FINE_LOCATION
94         )
95
96     return PackageManager.PERMISSION_GRANTED == locationPermissionState
97   }
98
99   private fun getCallback() = FragmentUtil.getHost(this, Callback::class.java)
100
101   /** Interface for notifying the host. */
102   interface Callback {
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()
117   }
118
119   companion object {
120
121     @JvmStatic fun newInstance() = SelectActionFragment()
122   }
123 }