1 /*
2 * Copyright (C) 2022 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.derivedStateOf
26 import androidx.compose.runtime.livedata.observeAsState
27 import androidx.compose.runtime.remember
28 import androidx.compose.ui.platform.LocalContext
29 import androidx.compose.ui.res.stringResource
30 import androidx.lifecycle.LiveData
31 import com.android.settings.R
32 import com.android.settingslib.spa.widget.preference.Preference
33 import com.android.settingslib.spa.widget.preference.PreferenceModel
34 import com.android.settingslib.spaprivileged.model.app.userHandle
35
36 private const val TAG = "AppPermissionPreference"
37 private const val EXTRA_HIDE_INFO_BUTTON = "hideInfoButton"
38
39 @Composable
AppPermissionPreferencenull40 fun AppPermissionPreference(
41 app: ApplicationInfo,
42 summaryLiveData: LiveData<AppPermissionSummaryState> = rememberAppPermissionSummary(app),
43 ) {
44 val context = LocalContext.current
45 val summaryState = summaryLiveData.observeAsState(
46 initial = AppPermissionSummaryState(
47 summary = stringResource(R.string.summary_placeholder),
48 enabled = false,
49 )
50 )
51 Preference(
52 model = remember {
53 object : PreferenceModel {
54 override val title = context.getString(R.string.permissions_label)
55 override val summary = derivedStateOf { summaryState.value.summary }
56 override val enabled = derivedStateOf { summaryState.value.enabled }
57 override val onClick = { startManagePermissionsActivity(context, app) }
58 }
59 },
60 singleLineSummary = true,
61 )
62 }
63
64 /** Starts new activity to manage app permissions */
startManagePermissionsActivitynull65 private fun startManagePermissionsActivity(context: Context, app: ApplicationInfo) {
66 val intent = Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS).apply {
67 putExtra(Intent.EXTRA_PACKAGE_NAME, app.packageName)
68 putExtra(EXTRA_HIDE_INFO_BUTTON, true)
69 }
70 try {
71 context.startActivityAsUser(intent, app.userHandle)
72 } catch (e: ActivityNotFoundException) {
73 Log.w(TAG, "No app can handle android.intent.action.MANAGE_APP_PERMISSIONS")
74 }
75 }
76