• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settingslib.spaprivileged.template.app
18 
19 import android.content.pm.ApplicationInfo
20 import android.content.pm.PackageInfo
21 import android.text.BidiFormatter
22 import androidx.compose.foundation.Image
23 import androidx.compose.foundation.layout.Box
24 import androidx.compose.foundation.layout.Column
25 import androidx.compose.foundation.layout.Spacer
26 import androidx.compose.foundation.layout.fillMaxWidth
27 import androidx.compose.foundation.layout.height
28 import androidx.compose.foundation.layout.padding
29 import androidx.compose.foundation.layout.size
30 import androidx.compose.material3.Divider
31 import androidx.compose.runtime.Composable
32 import androidx.compose.ui.Alignment
33 import androidx.compose.ui.Modifier
34 import androidx.compose.ui.res.stringResource
35 import androidx.compose.ui.semantics.semantics
36 import androidx.compose.ui.unit.Dp
37 import androidx.compose.ui.unit.dp
38 import com.android.settingslib.spa.framework.compose.rememberDrawablePainter
39 import com.android.settingslib.spa.framework.theme.SettingsDimension
40 import com.android.settingslib.spa.widget.ui.SettingsBody
41 import com.android.settingslib.spa.widget.ui.SettingsTitle
42 import com.android.settingslib.spaprivileged.R
43 import com.android.settingslib.spaprivileged.model.app.rememberAppRepository
44 
45 class AppInfoProvider(private val packageInfo: PackageInfo) {
46     @Composable
AppInfonull47     fun AppInfo(displayVersion: Boolean = false, isClonedAppPage: Boolean = false) {
48         Column(
49             modifier = Modifier
50                 .fillMaxWidth()
51                 .padding(
52                     horizontal = SettingsDimension.itemPaddingStart,
53                     vertical = SettingsDimension.itemPaddingVertical,
54                 )
55                 .semantics(mergeDescendants = true) {},
56             horizontalAlignment = Alignment.CenterHorizontally,
57         ) {
58             val app = packageInfo.applicationInfo
59             Box(modifier = Modifier.padding(SettingsDimension.itemPaddingAround)) {
60                 AppIcon(app = app, size = SettingsDimension.appIconInfoSize)
61             }
62             AppLabel(app, isClonedAppPage)
63             InstallType(app)
64             if (displayVersion) AppVersion()
65         }
66     }
67 
68     @Composable
InstallTypenull69     private fun InstallType(app: ApplicationInfo) {
70         if (!app.isInstantApp) return
71         Spacer(modifier = Modifier.height(4.dp))
72         SettingsBody(stringResource(R.string.install_type_instant))
73     }
74 
75     @Composable
AppVersionnull76     private fun AppVersion() {
77         if (packageInfo.versionName == null) return
78         Spacer(modifier = Modifier.height(4.dp))
79         SettingsBody(packageInfo.versionNameBidiWrapped)
80     }
81 
82     @Composable
FooterAppVersionnull83     fun FooterAppVersion() {
84         if (packageInfo.versionName == null) return
85         Divider()
86         Box(modifier = Modifier.padding(SettingsDimension.itemPadding)) {
87             SettingsBody(stringResource(R.string.version_text, packageInfo.versionNameBidiWrapped))
88         }
89     }
90 
91     private companion object {
92         /** Wrapped the version name, so its directionality still keep same when RTL. */
93         val PackageInfo.versionNameBidiWrapped: String
94             get() = BidiFormatter.getInstance().unicodeWrap(versionName)
95     }
96 }
97 
98 @Composable
AppIconnull99 internal fun AppIcon(app: ApplicationInfo, size: Dp) {
100     val appRepository = rememberAppRepository()
101     Image(
102         painter = rememberDrawablePainter(appRepository.produceIcon(app).value),
103         contentDescription = appRepository.produceIconContentDescription(app).value,
104         modifier = Modifier.size(size),
105     )
106 }
107 
108 @Composable
AppLabelnull109 internal fun AppLabel(app: ApplicationInfo, isClonedAppPage: Boolean = false) {
110     val appRepository = rememberAppRepository()
111     SettingsTitle(title = appRepository.produceLabel(app, isClonedAppPage), useMediumWeight = true)
112 }
113