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.material.icons.Icons
21 import androidx.compose.material.icons.outlined.Launch
22 import androidx.compose.material.icons.outlined.WarningAmber
23 import androidx.compose.runtime.Composable
24 import androidx.compose.runtime.remember
25 import androidx.lifecycle.compose.collectAsStateWithLifecycle
26 import com.android.settings.R
27 import com.android.settingslib.spa.widget.button.ActionButton
28 import com.android.settingslib.spa.widget.button.ActionButtons
29
30 @Composable
ClonePageAppButtonsnull31 fun ClonePageAppButtons(packageInfoPresenter: PackageInfoPresenter) {
32 val presenter = remember { CloneAppButtonsPresenter(packageInfoPresenter) }
33 ActionButtons(actionButtons = presenter.getActionButtons())
34 }
35
36 private class CloneAppButtonsPresenter(private val packageInfoPresenter: PackageInfoPresenter) {
37 private val appLaunchButton = FakeAppLaunchButton(packageInfoPresenter)
38 private val appCreateButton = AppCreateButton(packageInfoPresenter)
39 private val appForceStopButton = FakeAppForceStopButton(packageInfoPresenter)
40
41 @Composable
getActionButtonsnull42 fun getActionButtons() =
43 packageInfoPresenter.flow.collectAsStateWithLifecycle(initialValue = null).value?.let {
44 getActionButtons(checkNotNull(it.applicationInfo))
45 } ?: emptyList()
46
47 @Composable
getActionButtonsnull48 private fun getActionButtons(app: ApplicationInfo): List<ActionButton> = listOfNotNull(
49 appLaunchButton.getActionButton(),
50 appCreateButton.getActionButton(app),
51 appForceStopButton.getActionButton(),
52 )
53 }
54
55 class FakeAppForceStopButton(packageInfoPresenter: PackageInfoPresenter) {
56 private val context = packageInfoPresenter.context
57
58 fun getActionButton(): ActionButton {
59 return ActionButton(
60 text = context.getString(R.string.force_stop),
61 imageVector = Icons.Outlined.WarningAmber,
62 enabled = false,
63 ) {
64 // Unclickable
65 }
66 }
67 }
68
69 class FakeAppLaunchButton(packageInfoPresenter: PackageInfoPresenter) {
70 private val context = packageInfoPresenter.context
71
72 @Composable
getActionButtonnull73 fun getActionButton(): ActionButton {
74 return ActionButton(
75 text = context.getString(R.string.launch_instant_app),
76 imageVector = Icons.Outlined.Launch,
77 enabled = false
78 ) {
79 // Unclickable
80 }
81 }
82 }
83