1 /*
2  * Copyright (C) 2023 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  *      http://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.permissioncontroller.permission.ui.wear
18 
19 import android.os.Build
20 import android.os.Bundle
21 import android.view.LayoutInflater
22 import android.view.View
23 import android.view.ViewGroup
24 import androidx.annotation.RequiresApi
25 import androidx.compose.ui.platform.ComposeView
26 import androidx.fragment.app.Fragment
27 import androidx.lifecycle.ViewModelProvider
28 import com.android.permissioncontroller.Constants
29 import com.android.permissioncontroller.permission.ui.viewmodel.v31.PermissionUsageViewModel
30 import com.android.permissioncontroller.permission.ui.viewmodel.v31.PermissionUsageViewModelFactory
31 import com.android.permissioncontroller.permission.ui.wear.model.WearPermissionUsageViewModel
32 import com.android.permissioncontroller.permission.ui.wear.model.WearPermissionUsageViewModelFactory
33 
34 /**
35  * This is a condensed version of
36  * [com.android.permissioncontroller.permission.ui.handheld.v31.PermissionUsageFragment], tailored
37  * for Wear.
38  */
39 @RequiresApi(Build.VERSION_CODES.S)
40 class WearPermissionUsageFragment : Fragment() {
41     lateinit var wearViewModel: WearPermissionUsageViewModel
onCreateViewnull42     override fun onCreateView(
43         inflater: LayoutInflater,
44         container: ViewGroup?,
45         savedInstanceState: Bundle?
46     ): View? {
47         val sessionId: Long =
48             arguments?.getLong(Constants.EXTRA_SESSION_ID) ?: Constants.INVALID_SESSION_ID
49         val factory =
50             PermissionUsageViewModelFactory(requireActivity().getApplication(), this, Bundle())
51         val viewModel = ViewModelProvider(this, factory).get(PermissionUsageViewModel::class.java)
52         wearViewModel =
53             ViewModelProvider(this, WearPermissionUsageViewModelFactory(viewModel))
54                 .get(WearPermissionUsageViewModel::class.java)
55 
56         viewModel.permissionUsagesUiLiveData.observe(
57             this,
58             wearViewModel::updatePermissionUsagesUiStateLiveData
59         )
60         return ComposeView(requireContext()).apply {
61             setContent { WearPermissionUsageScreen(sessionId, viewModel, wearViewModel) }
62         }
63     }
64 
65     companion object {
66         @JvmStatic
newInstancenull67         fun newInstance(sessionId: Long): WearPermissionUsageFragment {
68             return WearPermissionUsageFragment().apply {
69                 val arguments = Bundle().apply { putLong(Constants.EXTRA_SESSION_ID, sessionId) }
70                 setArguments(arguments)
71             }
72         }
73     }
74 }
75