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