Merge "Add the different summary for the app store details" into main
This commit is contained in:
@@ -22,10 +22,15 @@ import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.InstallSourceInfo;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** This class provides methods that help dealing with app stores. */
|
||||
public class AppStoreUtil {
|
||||
private static final String LOG_TAG = "AppStoreUtil";
|
||||
@@ -37,15 +42,20 @@ public class AppStoreUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the package name of the app that we consider to be the user-visible 'installer'
|
||||
* of given packageName, if one is available.
|
||||
* Returns a {@link Pair pair result}. The first item is the package name of the app that we
|
||||
* consider to be the user-visible 'installer' of given packageName, if one is available. The
|
||||
* second item is the {@link InstallSourceInfo install source info} of the given package.
|
||||
*/
|
||||
@Nullable
|
||||
public static String getInstallerPackageName(Context context, String packageName) {
|
||||
@NonNull
|
||||
public static Pair<String, InstallSourceInfo> getInstallerPackageNameAndInstallSourceInfo(
|
||||
@NonNull Context context, @NonNull String packageName) {
|
||||
Objects.requireNonNull(context);
|
||||
Objects.requireNonNull(packageName);
|
||||
|
||||
String installerPackageName;
|
||||
InstallSourceInfo source = null;
|
||||
try {
|
||||
InstallSourceInfo source =
|
||||
context.getPackageManager().getInstallSourceInfo(packageName);
|
||||
source = context.getPackageManager().getInstallSourceInfo(packageName);
|
||||
// By default, use the installing package name.
|
||||
installerPackageName = source.getInstallingPackageName();
|
||||
// Use the recorded originating package name only if the initiating package is a system
|
||||
@@ -65,7 +75,17 @@ public class AppStoreUtil {
|
||||
Log.e(LOG_TAG, "Exception while retrieving the package installer of " + packageName, e);
|
||||
installerPackageName = null;
|
||||
}
|
||||
return installerPackageName;
|
||||
return new Pair<>(installerPackageName, source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(@NonNull Context context,
|
||||
@NonNull String packageName) {
|
||||
return getInstallerPackageNameAndInstallSourceInfo(context, packageName).first;
|
||||
}
|
||||
|
||||
/** Returns a link to the installer app store for a given package name. */
|
||||
@@ -88,4 +108,18 @@ public class AppStoreUtil {
|
||||
String installerPackageName = getInstallerPackageName(context, packageName);
|
||||
return getAppStoreLink(context, installerPackageName, packageName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} when the initiating package is different from installing package
|
||||
* for the given {@link InstallSourceInfo install source}. Otherwise, returns {@code false}.
|
||||
* If the {@code source} is null, also return {@code false}.
|
||||
*/
|
||||
public static boolean isInitiatedFromDifferentPackage(@Nullable InstallSourceInfo source) {
|
||||
if (source == null) {
|
||||
return false;
|
||||
}
|
||||
final String initiatingPackageName = source.getInitiatingPackageName();
|
||||
return initiatingPackageName != null
|
||||
&& !TextUtils.equals(source.getInstallingPackageName(), initiatingPackageName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package com.android.settings.spa.app.appinfo
|
||||
|
||||
import android.content.Context
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.InstallSourceInfo
|
||||
import android.util.Pair
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -51,8 +53,9 @@ fun AppInstallerInfoPreference(app: ApplicationInfo) {
|
||||
if (!presenter.isAvailableFlow.collectAsStateWithLifecycle(initialValue = false).value) return
|
||||
|
||||
val summary by presenter.summaryFlow.collectAsStateWithLifecycle(
|
||||
initialValue = stringResource(R.string.summary_placeholder),
|
||||
initialValue = stringResource(R.string.summary_placeholder)
|
||||
)
|
||||
|
||||
val enabled by presenter.enabledFlow.collectAsStateWithLifecycle(initialValue = false)
|
||||
Preference(object : PreferenceModel {
|
||||
override val title = stringResource(R.string.app_install_details_title)
|
||||
@@ -63,16 +66,21 @@ fun AppInstallerInfoPreference(app: ApplicationInfo) {
|
||||
}
|
||||
|
||||
private class AppInstallerInfoPresenter(
|
||||
private val context: Context,
|
||||
private val app: ApplicationInfo,
|
||||
private val coroutineScope: CoroutineScope,
|
||||
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 var installSourceInfo : InstallSourceInfo? = null
|
||||
|
||||
private val installerPackageFlow = flow {
|
||||
emit(withContext(Dispatchers.IO) {
|
||||
AppStoreUtil.getInstallerPackageName(userContext, app.packageName)
|
||||
val result : Pair<String, InstallSourceInfo> =
|
||||
AppStoreUtil.getInstallerPackageNameAndInstallSourceInfo(
|
||||
userContext, app.packageName)
|
||||
installSourceInfo = result.second
|
||||
result.first
|
||||
})
|
||||
}.sharedFlow()
|
||||
|
||||
@@ -91,11 +99,23 @@ private class AppInstallerInfoPresenter(
|
||||
}
|
||||
|
||||
val summaryFlow = installerLabelFlow.map { installerLabel ->
|
||||
val detailsStringId = when {
|
||||
app.isInstantApp -> R.string.instant_app_details_summary
|
||||
else -> R.string.app_install_details_summary
|
||||
if (app.isInstantApp) {
|
||||
context.getString(R.string.instant_app_details_summary, installerLabel)
|
||||
} else if (AppStoreUtil.isInitiatedFromDifferentPackage(installSourceInfo)) {
|
||||
val initiatingLabel : CharSequence? = Utils.getApplicationLabel(
|
||||
context, installSourceInfo!!.initiatingPackageName!!)
|
||||
if (initiatingLabel != null) {
|
||||
context.getString(
|
||||
R.string.app_install_details_different_initiating_package_summary,
|
||||
installerLabel,
|
||||
initiatingLabel
|
||||
)
|
||||
} else {
|
||||
context.getString(R.string.app_install_details_summary, installerLabel)
|
||||
}
|
||||
} else {
|
||||
context.getString(R.string.app_install_details_summary, installerLabel)
|
||||
}
|
||||
context.getString(detailsStringId, installerLabel)
|
||||
}
|
||||
|
||||
private val intentFlow = installerPackageFlow.map { installerPackage ->
|
||||
@@ -117,5 +137,5 @@ private class AppInstallerInfoPresenter(
|
||||
}
|
||||
|
||||
private fun <T> Flow<T>.sharedFlow() =
|
||||
shareIn(coroutineScope, SharingStarted.WhileSubscribed(), 1)
|
||||
shareIn(coroutineScope, SharingStarted.WhileSubscribed(), 1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user