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.pm.ApplicationInfo
20 import androidx.compose.runtime.Composable
21 import androidx.compose.runtime.remember
22 import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi
23 import androidx.lifecycle.compose.collectAsStateWithLifecycle
24 import com.android.settingslib.applications.AppUtils
25 import com.android.settingslib.spa.widget.button.ActionButton
26 import com.android.settingslib.spa.widget.button.ActionButtons
27
28 @Composable
AppButtonsnull29 fun AppButtons(packageInfoPresenter: PackageInfoPresenter) {
30 if (remember(packageInfoPresenter) { packageInfoPresenter.isMainlineModule() }) return
31 val presenter = remember { AppButtonsPresenter(packageInfoPresenter) }
32 ActionButtons(actionButtons = presenter.getActionButtons())
33 }
34
PackageInfoPresenternull35 private fun PackageInfoPresenter.isMainlineModule(): Boolean =
36 AppUtils.isMainlineModule(userPackageManager, packageName)
37
38 private class AppButtonsPresenter(private val packageInfoPresenter: PackageInfoPresenter) {
39 private val appLaunchButton = AppLaunchButton(packageInfoPresenter)
40 private val appInstallButton = AppInstallButton(packageInfoPresenter)
41 private val appDisableButton = AppDisableButton(packageInfoPresenter)
42 private val appUninstallButton = AppUninstallButton(packageInfoPresenter)
43 private val appClearButton = AppClearButton(packageInfoPresenter)
44 private val appForceStopButton = AppForceStopButton(packageInfoPresenter)
45
46 @OptIn(ExperimentalLifecycleComposeApi::class)
47 @Composable
48 fun getActionButtons() =
49 packageInfoPresenter.flow.collectAsStateWithLifecycle(initialValue = null).value?.let {
50 getActionButtons(it.applicationInfo)
51 } ?: emptyList()
52
53 @Composable
54 private fun getActionButtons(app: ApplicationInfo): List<ActionButton> = listOfNotNull(
55 appLaunchButton.getActionButton(app),
56 appInstallButton.getActionButton(app),
57 appDisableButton.getActionButton(app),
58 appUninstallButton.getActionButton(app),
59 appClearButton.getActionButton(app),
60 appForceStopButton.getActionButton(app),
61 )
62 }
63