1 /*
2 * Copyright (C) 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 * 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.settings.spa.app.appinfo
18
19 import android.content.ActivityNotFoundException
20 import android.content.Context
21 import android.content.Intent
22 import android.content.pm.ApplicationInfo
23 import android.util.Log
24 import androidx.compose.runtime.Composable
25 import androidx.compose.runtime.remember
26 import androidx.compose.ui.platform.LocalContext
27 import androidx.lifecycle.compose.collectAsStateWithLifecycle
28 import com.android.settings.R
29 import com.android.settingslib.spa.widget.preference.Preference
30 import com.android.settingslib.spa.widget.preference.PreferenceModel
31 import com.android.settingslib.spaprivileged.framework.compose.placeholder
32 import com.android.settingslib.spaprivileged.model.app.userHandle
33 import kotlinx.coroutines.flow.Flow
34
35 private const val TAG = "AppPermissionPreference"
36 private const val EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"
37
38 @Composable
AppPermissionPreferencenull39 fun AppPermissionPreference(
40 app: ApplicationInfo,
41 summaryFlow: Flow<AppPermissionSummaryState> = rememberAppPermissionSummary(app),
42 ) {
43 val context = LocalContext.current
44 val summaryState = summaryFlow.collectAsStateWithLifecycle(
45 initialValue = AppPermissionSummaryState(summary = placeholder(), enabled = false),
46 )
47 Preference(
48 model = remember {
49 object : PreferenceModel {
50 override val title = context.getString(R.string.permissions_label)
51 override val summary = { summaryState.value.summary }
52 override val enabled = { summaryState.value.enabled }
53 override val onClick = { startManagePermissionsActivity(context, app) }
54 }
55 },
56 singleLineSummary = true,
57 )
58 }
59
60 /** Starts new activity to manage app permissions */
startManagePermissionsActivitynull61 private fun startManagePermissionsActivity(context: Context, app: ApplicationInfo) {
62 val intent = Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS).apply {
63 putExtra(Intent.EXTRA_PACKAGE_NAME, app.packageName)
64 putExtra(EXTRA_HIDE_INFO_BUTTON, true)
65 }
66 try {
67 context.startActivityAsUser(intent, app.userHandle)
68 } catch (e: ActivityNotFoundException) {
69 Log.w(TAG, "No app can handle android.intent.action.MANAGE_APP_PERMISSIONS")
70 }
71 }
72