1 /**
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * ```
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  * ```
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.healthconnect.testapps.toolbox.ui
17 
18 import android.health.connect.datatypes.StepsRecord
19 import android.os.Bundle
20 import android.view.View
21 import android.widget.Button
22 import androidx.fragment.app.Fragment
23 import androidx.lifecycle.lifecycleScope
24 import com.android.healthconnect.testapps.toolbox.PKG_TOOLBOX_2
25 import com.android.healthconnect.testapps.toolbox.R
26 import com.android.healthconnect.testapps.toolbox.ToolboxProxyPayload
27 import com.android.healthconnect.testapps.toolbox.callToolbox
28 import com.android.healthconnect.testapps.toolbox.utils.GeneralUtils.Companion.showMessageDialog
29 import kotlinx.coroutines.launch
30 
31 class ReadDataInBackgroundFragment : Fragment(R.layout.fragment_read_data_in_background) {
32 
onViewCreatednull33     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
34         super.onViewCreated(view, savedInstanceState)
35 
36         view.requireViewById<Button>(R.id.read_own_steps_button).setOnClickListener {
37             callToolbox2(
38                 ToolboxProxyPayload.ReadRecords(
39                     type = StepsRecord::class.java,
40                     packageNames = listOf(PKG_TOOLBOX_2),
41                 )
42             )
43         }
44 
45         view.requireViewById<Button>(R.id.read_all_steps_button).setOnClickListener {
46             callToolbox2(ToolboxProxyPayload.ReadRecords(type = StepsRecord::class.java))
47         }
48     }
49 
callToolbox2null50     private fun callToolbox2(payload: ToolboxProxyPayload) {
51         lifecycleScope.launch {
52             val response =
53                 callToolbox(
54                     context = requireContext(),
55                     packageName = PKG_TOOLBOX_2,
56                     payload = payload,
57                 )
58 
59             requireContext().showMessageDialog(response ?: "No response")
60         }
61     }
62 }
63