Add AppInstallerInfoPreference for Spa
This is used in new App Info page. To try: 1. adb shell am start -n com.android.settings/.spa.SpaActivity 2. Go to Apps -> All apps -> [One App] -> App details Bug: 236346018 Test: Unit test Change-Id: Ibd1ae27c60a096b7f12ca6640a58b099dcfb0b6b
This commit is contained in:
@@ -21,7 +21,6 @@ import static android.content.Intent.EXTRA_USER_ID;
|
||||
import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
|
||||
import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import android.app.ActionBar;
|
||||
import android.app.Activity;
|
||||
import android.app.ActivityManager;
|
||||
@@ -96,6 +95,7 @@ import android.widget.TabWidget;
|
||||
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
|
||||
@@ -799,7 +799,9 @@ public final class Utils extends com.android.settingslib.Utils {
|
||||
}
|
||||
}
|
||||
|
||||
public static CharSequence getApplicationLabel(Context context, String packageName) {
|
||||
/** Gets the application label of the given package name. */
|
||||
@Nullable
|
||||
public static CharSequence getApplicationLabel(Context context, @NonNull String packageName) {
|
||||
try {
|
||||
final ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(
|
||||
packageName,
|
||||
|
@@ -24,7 +24,9 @@ import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.util.Log;
|
||||
|
||||
// This class provides methods that help dealing with app stores.
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
/** This class provides methods that help dealing with app stores. */
|
||||
public class AppStoreUtil {
|
||||
private static final String LOG_TAG = "AppStoreUtil";
|
||||
|
||||
@@ -34,8 +36,11 @@ public class AppStoreUtil {
|
||||
.setClassName(result.activityInfo.packageName, result.activityInfo.name) : null;
|
||||
}
|
||||
|
||||
// Returns the package name of the app that we consider to be the user-visible 'installer'
|
||||
// of given packageName, if one is available.
|
||||
/**
|
||||
* Returns the package name of the app that we consider to be the user-visible 'installer'
|
||||
* of given packageName, if one is available.
|
||||
*/
|
||||
@Nullable
|
||||
public static String getInstallerPackageName(Context context, String packageName) {
|
||||
String installerPackageName;
|
||||
try {
|
||||
@@ -62,7 +67,8 @@ public class AppStoreUtil {
|
||||
return installerPackageName;
|
||||
}
|
||||
|
||||
// Returns a link to the installer app store for a given package name.
|
||||
/** Returns a link to the installer app store for a given package name. */
|
||||
@Nullable
|
||||
public static Intent getAppStoreLink(Context context, String installerPackageName,
|
||||
String packageName) {
|
||||
Intent intent = new Intent(Intent.ACTION_SHOW_APP_INFO)
|
||||
@@ -75,7 +81,7 @@ public class AppStoreUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Convenience method that looks up the installerPackageName for you.
|
||||
/** Convenience method that looks up the installerPackageName for you. */
|
||||
public static Intent getAppStoreLink(Context context, String packageName) {
|
||||
String installerPackageName = getInstallerPackageName(context, packageName);
|
||||
return getAppStoreLink(context, installerPackageName, packageName);
|
||||
|
@@ -113,7 +113,9 @@ private fun AppInfoSettings(packageInfoPresenter: PackageInfoPresenter) {
|
||||
AlarmsAndRemindersAppListProvider.InfoPageEntryItem(app)
|
||||
}
|
||||
|
||||
// TODO: app_installer
|
||||
Category(title = stringResource(R.string.app_install_details_group_title)) {
|
||||
AppInstallerInfoPreference(app)
|
||||
}
|
||||
appInfoProvider.FooterAppVersion()
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright (C) 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.spa.app.appinfo
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.ApplicationInfo
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.android.settings.R
|
||||
import com.android.settings.Utils
|
||||
import com.android.settings.applications.AppStoreUtil
|
||||
import com.android.settingslib.applications.AppUtils
|
||||
import com.android.settingslib.spa.framework.compose.collectAsStateWithLifecycle
|
||||
import com.android.settingslib.spa.widget.preference.Preference
|
||||
import com.android.settingslib.spa.widget.preference.PreferenceModel
|
||||
import com.android.settingslib.spaprivileged.framework.common.asUser
|
||||
import com.android.settingslib.spaprivileged.framework.common.userManager
|
||||
import com.android.settingslib.spaprivileged.model.app.userHandle
|
||||
import com.android.settingslib.spaprivileged.model.app.userId
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.shareIn
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
fun AppInstallerInfoPreference(app: ApplicationInfo) {
|
||||
val context = LocalContext.current
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val presenter = remember { AppInstallerInfoPresenter(context, app, coroutineScope) }
|
||||
if (!presenter.isAvailableFlow.collectAsStateWithLifecycle(initialValue = false).value) return
|
||||
|
||||
Preference(object : PreferenceModel {
|
||||
override val title = stringResource(R.string.app_install_details_title)
|
||||
override val summary = presenter.summaryFlow.collectAsStateWithLifecycle(
|
||||
initialValue = stringResource(R.string.summary_placeholder),
|
||||
)
|
||||
override val enabled =
|
||||
presenter.enabledFlow.collectAsStateWithLifecycle(initialValue = false)
|
||||
override val onClick = presenter::startActivity
|
||||
})
|
||||
}
|
||||
|
||||
private class AppInstallerInfoPresenter(
|
||||
private val context: Context,
|
||||
private val app: ApplicationInfo,
|
||||
private val coroutineScope: CoroutineScope,
|
||||
) {
|
||||
private val userContext = context.asUser(app.userHandle)
|
||||
private val packageManager = userContext.packageManager
|
||||
private val userManager = context.userManager
|
||||
|
||||
private val installerPackageFlow = flow {
|
||||
emit(withContext(Dispatchers.IO) {
|
||||
AppStoreUtil.getInstallerPackageName(userContext, app.packageName)
|
||||
})
|
||||
}.sharedFlow()
|
||||
|
||||
private val installerLabelFlow = installerPackageFlow.map { installerPackage ->
|
||||
installerPackage ?: return@map null
|
||||
withContext(Dispatchers.IO) {
|
||||
Utils.getApplicationLabel(context, installerPackage)
|
||||
}
|
||||
}.sharedFlow()
|
||||
|
||||
val isAvailableFlow = installerLabelFlow.map { installerLabel ->
|
||||
withContext(Dispatchers.IO) {
|
||||
!userManager.isManagedProfile(app.userId) &&
|
||||
!AppUtils.isMainlineModule(packageManager, app.packageName) &&
|
||||
installerLabel != null
|
||||
}
|
||||
}
|
||||
|
||||
val summaryFlow = installerLabelFlow.map { installerLabel ->
|
||||
val detailsStringId = when {
|
||||
app.isInstantApp -> R.string.instant_app_details_summary
|
||||
else -> R.string.app_install_details_summary
|
||||
}
|
||||
context.getString(detailsStringId, installerLabel)
|
||||
}
|
||||
|
||||
private val intentFlow = installerPackageFlow.map { installerPackage ->
|
||||
withContext(Dispatchers.IO) {
|
||||
AppStoreUtil.getAppStoreLink(context, installerPackage, app.packageName)
|
||||
}
|
||||
}.sharedFlow()
|
||||
|
||||
val enabledFlow = intentFlow.map { it != null }
|
||||
|
||||
fun startActivity() {
|
||||
coroutineScope.launch {
|
||||
intentFlow.collect { intent ->
|
||||
if (intent != null) {
|
||||
context.startActivityAsUser(intent, app.userHandle)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T> Flow<T>.sharedFlow() =
|
||||
shareIn(coroutineScope, SharingStarted.WhileSubscribed(), 1)
|
||||
}
|
Reference in New Issue
Block a user