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.Context
20 import android.content.pm.ApplicationInfo
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.remember
24 import androidx.compose.ui.platform.LocalContext
25 import androidx.compose.ui.res.stringResource
26 import androidx.lifecycle.compose.collectAsStateWithLifecycle
27 import com.android.settings.R
28 import com.android.settings.applications.appinfo.AppInfoDashboardFragment
29 import com.android.settings.applications.intentpicker.AppLaunchSettings
30 import com.android.settings.applications.intentpicker.IntentPickerUtils
31 import com.android.settingslib.applications.AppUtils
32 import com.android.settingslib.spa.widget.preference.Preference
33 import com.android.settingslib.spa.widget.preference.PreferenceModel
34 import com.android.settingslib.spaprivileged.framework.common.asUser
35 import com.android.settingslib.spaprivileged.framework.common.domainVerificationManager
36 import com.android.settingslib.spaprivileged.model.app.hasFlag
37 import com.android.settingslib.spaprivileged.model.app.userHandle
38 import com.android.settingslib.spaprivileged.model.app.userId
39 import kotlinx.coroutines.Dispatchers
40 import kotlinx.coroutines.flow.flow
41 import kotlinx.coroutines.flow.flowOn
42
43 @Composable
AppOpenByDefaultPreferencenull44 fun AppOpenByDefaultPreference(app: ApplicationInfo) {
45 val context = LocalContext.current
46 val presenter = remember(app) { AppOpenByDefaultPresenter(context, app) }
47 if (remember(presenter) { !presenter.isAvailable() }) return
48
49 val summary by presenter.summaryFlow.collectAsStateWithLifecycle(
50 initialValue = stringResource(R.string.summary_placeholder),
51 )
52 Preference(object : PreferenceModel {
53 override val title = stringResource(R.string.launch_by_default)
54 override val summary = { summary }
55 override val enabled = { presenter.isEnabled() }
56 override val onClick = presenter::startActivity
57 })
58 }
59
60 private class AppOpenByDefaultPresenter(
61 private val context: Context,
62 private val app: ApplicationInfo,
63 ) {
64 private val domainVerificationManager = context.asUser(app.userHandle).domainVerificationManager
65
isAvailablenull66 fun isAvailable() =
67 !app.isInstantApp && !AppUtils.isBrowserApp(context, app.packageName, app.userId)
68
69 fun isEnabled() = app.hasFlag(ApplicationInfo.FLAG_INSTALLED) && app.enabled
70
71 val summaryFlow = flow { emit(getSummary()) }.flowOn(Dispatchers.IO)
72
getSummarynull73 private fun getSummary() = context.getString(
74 when {
75 isLinkHandlingAllowed() -> R.string.app_link_open_always
76 else -> R.string.app_link_open_never
77 }
78 )
79
isLinkHandlingAllowednull80 private fun isLinkHandlingAllowed(): Boolean {
81 val userState = IntentPickerUtils.getDomainVerificationUserState(
82 domainVerificationManager, app.packageName
83 )
84 return userState?.isLinkHandlingAllowed ?: false
85 }
86
startActivitynull87 fun startActivity() {
88 AppInfoDashboardFragment.startAppInfoFragment(
89 AppLaunchSettings::class.java,
90 app,
91 context,
92 AppInfoSettingsProvider.METRICS_CATEGORY,
93 )
94 }
95 }
96