[Panlingual] Use feature flag to switch opt-in on/off

- Currently per app language use opt-out by default. This change is to
   add a new idea to have a way to change opt-out to opt-in, and let
   user be able to use LocaleConfig.xml to control the feature more
   precise.
 - If app does not support locale picker or there is no locale provided, remove entries' UI.

Bug: b/231396734
Bug: b/230688538
Test: local
Change-Id: I2661fffab804a2816744711130b26aa2ec47f820
This commit is contained in:
tom hsu
2022-05-05 16:36:15 +08:00
parent afe6d302e4
commit 8976293162
3 changed files with 75 additions and 40 deletions

View File

@@ -18,11 +18,14 @@ 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.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.LocaleList;
import android.util.FeatureFlagUtils;
import android.util.Log;
import com.android.settings.R;
@@ -46,12 +49,17 @@ public class AppLocaleUtil {
boolean isDisallowedPackage = isDisallowedPackage(context, packageName);
boolean hasLauncherEntry = hasLauncherEntry(packageName, infos);
boolean isSignedWithPlatformKey = isSignedWithPlatformKey(context, packageName);
boolean isAppLocaleSupported = isAppLocaleSupported(context, packageName);
Log.i(TAG, "Can display preference - [" + packageName + "] :"
+ " isDisallowedPackage : " + isDisallowedPackage
+ " / isSignedWithPlatformKey : " + isSignedWithPlatformKey
+ " / hasLauncherEntry : " + hasLauncherEntry);
+ " / hasLauncherEntry : " + hasLauncherEntry
+ " / isAppLocaleSupported : " + isAppLocaleSupported);
return !isDisallowedPackage && !isSignedWithPlatformKey && hasLauncherEntry;
return !isDisallowedPackage
&& !isSignedWithPlatformKey
&& hasLauncherEntry
&& isAppLocaleSupported;
}
private static boolean isDisallowedPackage(Context context, String packageName) {
@@ -86,4 +94,61 @@ public class AppLocaleUtil {
return infos.stream()
.anyMatch(info -> info.activityInfo.packageName.equals(packageName));
}
/**
* Check the function of per app language is supported by current application.
*/
public static boolean isAppLocaleSupported(Context context, String packageName) {
if (getPackageLocales(context, packageName) != null) {
return true;
}
if (FeatureFlagUtils.isEnabled(
context, FeatureFlagUtils.SETTINGS_APP_LOCALE_OPT_IN_ENABLED)) {
return false;
}
return getAssetLocales(context, packageName).length > 0;
}
/**
* Get locales fron AssetManager.
*/
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();
if (locales == null) {
Log.i(TAG, "[" + packageName + "] locales are null.");
}
if (locales.length <= 0) {
Log.i(TAG, "[" + packageName + "] locales length is 0.");
return new String[0];
}
String locale = locales[0];
Log.i(TAG, "First asset locale - [" + packageName + "] " + locale);
return locales;
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Can not found the package name : " + packageName + " / " + e);
}
return new String[0];
}
/**
* Get locales from LocaleConfig.
*/
public static LocaleList getPackageLocales(Context context, String packageName) {
try {
LocaleConfig localeConfig =
new LocaleConfig(context.createPackageContext(packageName, 0));
if (localeConfig.getStatus() == LocaleConfig.STATUS_SUCCESS) {
return localeConfig.getSupportedLocales();
}
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Can not found the package name : " + packageName + " / " + e);
}
return null;
}
}