Implement LocaleNotification in Settings
Bug: 248514263 Test: atest LocaleListEditorTest Change-Id: I8c5764997d1622f0885d5d32124a49759e585e42
This commit is contained in:
@@ -18,10 +18,13 @@ package com.android.settings.localepicker;
|
||||
|
||||
import android.app.FragmentTransaction;
|
||||
import android.app.LocaleManager;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.LocaleList;
|
||||
import android.os.SystemProperties;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.MenuItem;
|
||||
@@ -29,6 +32,9 @@ import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ListView;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
|
||||
import com.android.internal.app.LocalePickerWithRegion;
|
||||
import com.android.internal.app.LocaleStore;
|
||||
import com.android.settings.R;
|
||||
@@ -36,9 +42,14 @@ import com.android.settings.applications.AppLocaleUtil;
|
||||
import com.android.settings.applications.appinfo.AppLocaleDetails;
|
||||
import com.android.settings.core.SettingsBaseActivity;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class AppLocalePickerActivity extends SettingsBaseActivity
|
||||
implements LocalePickerWithRegion.LocaleSelectedListener, MenuItem.OnActionExpandListener {
|
||||
private static final String TAG = AppLocalePickerActivity.class.getSimpleName();
|
||||
static final String EXTRA_APP_LOCALE = "app_locale";
|
||||
private static final String PROP_SYSTEM_LOCALE_SUGGESTION = "android.system.locale.suggestion";
|
||||
private static final boolean ENABLED = false;
|
||||
|
||||
private String mPackageName;
|
||||
private LocalePickerWithRegion mLocalePickerWithRegion;
|
||||
@@ -98,6 +109,7 @@ public class AppLocalePickerActivity extends SettingsBaseActivity
|
||||
setAppDefaultLocale("");
|
||||
} else {
|
||||
setAppDefaultLocale(localeInfo.getLocale().toLanguageTag());
|
||||
broadcastAppLocaleChange(localeInfo);
|
||||
}
|
||||
finish();
|
||||
}
|
||||
@@ -125,6 +137,58 @@ public class AppLocalePickerActivity extends SettingsBaseActivity
|
||||
localeManager.setApplicationLocales(mPackageName, LocaleList.forLanguageTags(languageTag));
|
||||
}
|
||||
|
||||
private void broadcastAppLocaleChange(LocaleStore.LocaleInfo localeInfo) {
|
||||
if (!SystemProperties.getBoolean(PROP_SYSTEM_LOCALE_SUGGESTION, ENABLED)) {
|
||||
return;
|
||||
}
|
||||
String languageTag = localeInfo.getLocale().toLanguageTag();
|
||||
if (isInSystemLocale(languageTag) || localeInfo.isAppCurrentLocale()) {
|
||||
return;
|
||||
}
|
||||
String intentAction = getString(R.string.config_app_locale_intent_action);
|
||||
if (!TextUtils.isEmpty(intentAction)) {
|
||||
try {
|
||||
PackageManager packageManager = getPackageManager();
|
||||
ApplicationInfo info = packageManager.getApplicationInfo(mPackageName,
|
||||
PackageManager.GET_META_DATA);
|
||||
Intent intent = new Intent(intentAction)
|
||||
.putExtra(Intent.EXTRA_UID, info.uid)
|
||||
.putExtra(EXTRA_APP_LOCALE, languageTag);
|
||||
if (intent.resolveActivity(packageManager) != null) {
|
||||
mStartForResult.launch(intent);
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.e(TAG, "Unable to find info for package: " + mPackageName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Invoke startActivityFroResult so that the calling package can be shared via the intent.
|
||||
private ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
result -> {
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Checks if the localeTag is in the system locale. Since in the current design, the system
|
||||
* language list would not show two locales with the same language and region but different
|
||||
* numbering system. So, during the comparison, the extension has to be stripped.
|
||||
*
|
||||
* @param languageTag A language tag
|
||||
* @return true if the locale is in the system locale. Otherwise, false.
|
||||
*/
|
||||
private static boolean isInSystemLocale(String languageTag) {
|
||||
LocaleList systemLocales = LocaleList.getDefault();
|
||||
Locale locale = Locale.forLanguageTag(languageTag).stripExtensions();
|
||||
for (int i = 0; i < systemLocales.size(); i++) {
|
||||
if (locale.equals(systemLocales.get(i).stripExtensions())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private View launchAppLocaleDetailsPage() {
|
||||
FrameLayout appLocaleDetailsContainer = new FrameLayout(this);
|
||||
appLocaleDetailsContainer.setId(R.id.layout_app_locale_details);
|
||||
@@ -171,4 +235,4 @@ public class AppLocalePickerActivity extends SettingsBaseActivity
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user