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 android.os.Bundle
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.remember
23 import androidx.compose.runtime.rememberCoroutineScope
24 import androidx.compose.ui.platform.LocalContext
25 import androidx.compose.ui.res.stringResource
26 import androidx.lifecycle.compose.collectAsStateWithLifecycle
27 import androidx.navigation.NavType
28 import androidx.navigation.navArgument
29 import com.android.settings.R
30 import com.android.settingslib.spa.framework.common.SettingsPageProvider
31 import com.android.settingslib.spa.widget.scaffold.RegularScaffold
32 import com.android.settingslib.spaprivileged.model.app.toRoute
33 import com.android.settingslib.spaprivileged.template.app.AppInfoProvider
34 
35 private const val PACKAGE_NAME = "packageName"
36 private const val USER_ID = "userId"
37 
38 object CloneAppInfoSettingsProvider : SettingsPageProvider {
39     override val name = "CloneAppInfoSettingsProvider"
40 
41     override val parameter = listOf(
<lambda>null42             navArgument(PACKAGE_NAME) { type = NavType.StringType },
<lambda>null43             navArgument(USER_ID) { type = NavType.IntType },
44     )
45 
46     @Composable
Pagenull47     override fun Page(arguments: Bundle?) {
48         val packageName = arguments!!.getString(PACKAGE_NAME)!!
49         val userId = arguments.getInt(USER_ID)
50         val context = LocalContext.current
51         val coroutineScope = rememberCoroutineScope()
52         val packageInfoPresenter = remember {
53             PackageInfoPresenter(context, packageName, userId, coroutineScope)
54         }
55         CloneAppInfoSettings(packageInfoPresenter)
56         packageInfoPresenter.PackageFullyRemovedEffect()
57     }
58 
59     @Composable
navigatornull60     fun navigator(app: ApplicationInfo) = com.android.settingslib.spa.framework.compose.navigator(route = "$name/${app.toRoute()}")
61 
62     /**
63      * Gets the route to the App Info Settings page.
64      *
65      * Expose route to enable enter from non-SPA pages.
66      */
67     fun getRoute(packageName: String, userId: Int): String = "$name/$packageName/$userId"
68 }
69 
70 @Composable
71 private fun CloneAppInfoSettings(packageInfoPresenter: PackageInfoPresenter) {
72     val packageInfo = packageInfoPresenter.flow.collectAsStateWithLifecycle().value ?: return
73     RegularScaffold(
74             title = stringResource(R.string.application_info_label),
75     ) {
76         val appInfoProvider = remember { AppInfoProvider(packageInfo) }
77 
78         appInfoProvider.AppInfo(isClonedAppPage = true)
79         ClonePageAppButtons(packageInfoPresenter)
80     }
81 }
82