1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.devicediagnostics.trusted
18 
19 import android.Manifest
20 import android.os.Bundle
21 import android.util.Log
22 import androidx.fragment.app.commit
23 import androidx.preference.Preference
24 import androidx.preference.PreferenceFragmentCompat
25 import com.android.devicediagnostics.ApplicationInterface
26 import com.android.devicediagnostics.PermissionsHelper
27 import com.android.devicediagnostics.Protos.BluetoothPacket
28 import com.android.devicediagnostics.R
29 import com.android.devicediagnostics.bluetooth.BluetoothServer
30 import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity
31 
32 private const val TAG = "WaitForResultsActivity"
33 
34 class WaitForResultFragment() : PreferenceFragmentCompat() {
onCreatePreferencesnull35     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
36         setPreferencesFromResource(R.xml.preferences_trusted_scan, rootKey)
37     }
38 
onBluetoothErrornull39     fun onBluetoothError() {
40         findPreference<Preference>("continue")!!.also {
41             it.setTitle(R.string.bluetooth_error_title)
42             it.setSummary(R.string.trusted_bluetooth_error_summary)
43         }
44     }
45 }
46 
47 class WaitForResultActivity : CollapsingToolbarBaseActivity(), BluetoothServer.ReadListener {
48     private var mFragment = WaitForResultFragment()
49 
50     private val permissions =
51         PermissionsHelper(
52             this,
53             arrayOf(
54                 Manifest.permission.CAMERA,
55                 Manifest.permission.BLUETOOTH_ADVERTISE,
56                 Manifest.permission.BLUETOOTH_CONNECT,
57                 Manifest.permission.BLUETOOTH_SCAN
58             )
<lambda>null59         ) {
60             onPermissionsGranted()
61         }
62 
onCreatenull63     override fun onCreate(savedInstanceState: Bundle?) {
64         super.onCreate(savedInstanceState)
65         setContentView(R.layout.activity_one_fragment)
66         setTitle(R.string.evaluation_mode_title)
67     }
68 
onPermissionsGrantednull69     private fun onPermissionsGranted() {
70         supportFragmentManager.commit {
71             setReorderingAllowed(true)
72             add(R.id.fragment_container_view, mFragment)
73         }
74 
75         val btServer = ApplicationInterface.app.getBluetoothServer()
76         btServer.startRead(this)
77     }
78 
onResumenull79     override fun onResume() {
80         super.onResume()
81         permissions.requestPermissions()
82     }
83 
onBluetoothReadPacketnull84     override fun onBluetoothReadPacket(packet: BluetoothPacket) {
85         if (mFragment.isDetached) {
86             return
87         }
88 
89         if (!packet.hasDeviceReport()) {
90             onBluetoothReadError(Exception("Packet is type: ${packet.payloadCase}"))
91             return
92         }
93 
94         val state = TrustedState.fromActivity(this)!!
95         state.report = packet.deviceReport
96         launchTrustedActivity(this, DisplayResultActivity::class.java.name, state)
97         finish()
98     }
99 
onBluetoothReadErrornull100     override fun onBluetoothReadError(e: Exception) {
101         Log.e(TAG, "Could not read device report: $e")
102         if (!mFragment.isDetached) {
103             mFragment.onBluetoothError()
104         }
105     }
106 }
107