[Panlingual] Improve performance of displaying app list in Settings.

- Use existed ApplicationInfo to get needed info.
 - Add unittest to AppLocaleUtil.java

Bug: b/238054165
Bug: b/231904717
Test: Maunal test passed
Test: atest passed
Change-Id: I23f354c366b45d4b684184b3366aeab913bcd3c8
This commit is contained in:
tom hsu
2022-10-25 16:26:55 +08:00
parent 862e9b3040
commit 8bd5e2a52f
7 changed files with 97 additions and 82 deletions

View File

@@ -17,48 +17,55 @@
package com.android.settings.applications;
import android.annotation.NonNull;
import android.app.ActivityManager;
import android.app.LocaleConfig;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.FeatureFlagUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import java.util.List;
/** This class provides methods that help dealing with per app locale. */
/**
* This class provides methods that help dealing with per app locale.
*/
public class AppLocaleUtil {
private static final String TAG = AppLocaleUtil.class.getSimpleName();
public static final Intent LAUNCHER_ENTRY_INTENT =
new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
@VisibleForTesting
static LocaleConfig sLocaleConfig;
/**
* Decides the UI display of per app locale.
*/
public static boolean canDisplayLocaleUi(
@NonNull Context context,
@NonNull String packageName,
@NonNull ApplicationInfo app,
@NonNull List<ResolveInfo> infos) {
boolean isDisallowedPackage = isDisallowedPackage(context, packageName);
boolean hasLauncherEntry = hasLauncherEntry(packageName, infos);
boolean isSignedWithPlatformKey = isSignedWithPlatformKey(context, packageName);
boolean isDisallowedPackage = isDisallowedPackage(context, app.packageName);
boolean hasLauncherEntry = hasLauncherEntry(app.packageName, infos);
boolean isSignedWithPlatformKey = app.isSignedWithPlatformKey();
boolean canDisplay = !isDisallowedPackage
&& !isSignedWithPlatformKey
&& hasLauncherEntry
&& isAppLocaleSupported(context, packageName);
&& isAppLocaleSupported(context, app.packageName);
Log.i(TAG, "Can display preference - [" + packageName + "] :"
Log.i(TAG, "Can display preference - [" + app.packageName + "] :"
+ " isDisallowedPackage : " + isDisallowedPackage
+ " / isSignedWithPlatformKey : " + isSignedWithPlatformKey
+ " / hasLauncherEntry : " + hasLauncherEntry
+ " / canDisplay : " + canDisplay);
+ " / canDisplay : " + canDisplay + " / 1.1");
return canDisplay;
}
@@ -66,30 +73,13 @@ public class AppLocaleUtil {
final String[] disallowedPackages = context.getResources().getStringArray(
R.array.config_disallowed_app_localeChange_packages);
for (String disallowedPackage : disallowedPackages) {
if (packageName.equals(disallowedPackage)) {
if (TextUtils.equals(packageName, disallowedPackage)) {
return true;
}
}
return false;
}
private static boolean isSignedWithPlatformKey(Context context, String packageName) {
PackageInfo packageInfo = null;
PackageManager packageManager = context.getPackageManager();
ActivityManager activityManager = context.getSystemService(ActivityManager.class);
try {
packageInfo = packageManager.getPackageInfoAsUser(
packageName, /* flags= */ 0,
activityManager.getCurrentUser());
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, "package not found: " + packageName);
}
if (packageInfo == null) {
return false;
}
return packageInfo.applicationInfo.isSignedWithPlatformKey();
}
private static boolean hasLauncherEntry(String packageName, List<ResolveInfo> infos) {
return infos.stream()
.anyMatch(info -> info.activityInfo.packageName.equals(packageName));
@@ -99,7 +89,13 @@ public class AppLocaleUtil {
* Check the function of per app language is supported by current application.
*/
public static boolean isAppLocaleSupported(Context context, String packageName) {
LocaleList localeList = getPackageLocales(context, packageName);
LocaleList localeList;
if (sLocaleConfig != null) {
localeList = getPackageLocales(sLocaleConfig);
} else {
localeList = getPackageLocales(context, packageName);
}
if (localeList != null) {
return localeList.size() > 0;
}
@@ -118,9 +114,8 @@ public class AppLocaleUtil {
public static String[] getAssetLocales(Context context, String packageName) {
try {
PackageManager packageManager = context.getPackageManager();
String[] locales = packageManager.getResourcesForApplication(
packageManager.getPackageInfo(packageName, PackageManager.MATCH_ALL)
.applicationInfo).getAssets().getNonSystemLocales();
String[] locales = packageManager.getResourcesForApplication(packageName)
.getAssets().getNonSystemLocales();
if (locales == null) {
Log.i(TAG, "[" + packageName + "] locales are null.");
}
@@ -137,6 +132,14 @@ public class AppLocaleUtil {
return new String[0];
}
@VisibleForTesting
static LocaleList getPackageLocales(LocaleConfig localeConfig) {
if (localeConfig.getStatus() == LocaleConfig.STATUS_SUCCESS) {
return localeConfig.getSupportedLocales();
}
return null;
}
/**
* Get locales from LocaleConfig.
*/
@@ -144,9 +147,7 @@ public class AppLocaleUtil {
try {
LocaleConfig localeConfig =
new LocaleConfig(context.createPackageContext(packageName, 0));
if (localeConfig.getStatus() == LocaleConfig.STATUS_SUCCESS) {
return localeConfig.getSupportedLocales();
}
return getPackageLocales(localeConfig);
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Can not found the package name : " + packageName + " / " + e);
}

View File

@@ -55,8 +55,7 @@ public class AppStateLocaleBridge extends AppStateBaseBridge {
AppInfoByProfiles appInfoByProfiles = getAppInfo(UserHandle.getUserId(uid));
app.extraInfo = AppLocaleUtil.canDisplayLocaleUi(appInfoByProfiles.mContextAsUser,
app.info.packageName,
appInfoByProfiles.mListInfos) ? Boolean.TRUE : Boolean.FALSE;
app.info, appInfoByProfiles.mListInfos) ? Boolean.TRUE : Boolean.FALSE;
}
@Override
@@ -67,8 +66,7 @@ public class AppStateLocaleBridge extends AppStateBaseBridge {
AppInfoByProfiles appInfoByProfiles = getAppInfo(UserHandle.getUserId(app.info.uid));
app.extraInfo = AppLocaleUtil.canDisplayLocaleUi(appInfoByProfiles.mContextAsUser,
app.info.packageName,
appInfoByProfiles.mListInfos) ? Boolean.TRUE : Boolean.FALSE;
app.info, appInfoByProfiles.mListInfos) ? Boolean.TRUE : Boolean.FALSE;
}
}

View File

@@ -84,6 +84,6 @@ public class AppLocalePreferenceController extends AppInfoPreferenceControllerBa
@VisibleForTesting
boolean canDisplayLocaleUi() {
return AppLocaleUtil
.canDisplayLocaleUi(mContext, mParent.getAppEntry().info.packageName, mListInfos);
.canDisplayLocaleUi(mContext, mParent.getAppEntry().info, mListInfos);
}
}

View File

@@ -69,7 +69,7 @@ private class AppLocalePresenter(
ResolveInfoFlags.of(PackageManager.GET_META_DATA.toLong()),
app.userId,
)
AppLocaleUtil.canDisplayLocaleUi(context, app.packageName, resolveInfos)
AppLocaleUtil.canDisplayLocaleUi(context, app, resolveInfos)
}
val summaryFlow = flow { emit(getSummary()) }

View File

@@ -58,7 +58,7 @@ class AppLanguagesListModel(private val context: Context) : AppListModel<AppLang
appList.map { app ->
AppLanguagesRecord(app,
AppLocaleUtil.canDisplayLocaleUi(context,
app.packageName, resolveInfos))
app, resolveInfos))
}
}