diff --git a/core/api/current.txt b/core/api/current.txt index b9201f2690217..3f12b6ca7d64e 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -5815,6 +5815,7 @@ package android.app { public class LocaleManager { method @NonNull public android.os.LocaleList getApplicationLocales(); method @NonNull @RequiresPermission(value="android.permission.READ_APP_SPECIFIC_LOCALES", conditional=true) public android.os.LocaleList getApplicationLocales(@NonNull String); + method @NonNull public android.os.LocaleList getSystemLocales(); method public void setApplicationLocales(@NonNull android.os.LocaleList); } diff --git a/core/api/test-current.txt b/core/api/test-current.txt index 2e8bed32665cb..d984abe457c9d 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -300,7 +300,6 @@ package android.app { } public class LocaleManager { - method @Nullable public android.os.LocaleList getSystemLocales(); method public void setSystemLocales(@NonNull android.os.LocaleList); } diff --git a/core/java/android/app/ILocaleManager.aidl b/core/java/android/app/ILocaleManager.aidl index 348cb2d30739a..3002c8bb9c3e2 100644 --- a/core/java/android/app/ILocaleManager.aidl +++ b/core/java/android/app/ILocaleManager.aidl @@ -40,4 +40,9 @@ import android.os.LocaleList; */ LocaleList getApplicationLocales(String packageName, int userId); + /** + * Returns the current system locales. + */ + LocaleList getSystemLocales(); + } \ No newline at end of file diff --git a/core/java/android/app/LocaleManager.java b/core/java/android/app/LocaleManager.java index 522dc845f57c7..efe9e35d4c64a 100644 --- a/core/java/android/app/LocaleManager.java +++ b/core/java/android/app/LocaleManager.java @@ -18,7 +18,6 @@ package android.app; import android.Manifest; import android.annotation.NonNull; -import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.SystemService; @@ -126,6 +125,26 @@ public class LocaleManager { } } + /** + * Returns the current system locales, ignoring app-specific overrides. + * + *
Note: Apps should generally access the user's locale preferences as indicated in + * their in-process {@link LocaleList}s. However, in case an app-specific locale is set, this + * method helps cater to rare use-cases which might require specifically knowing the system + * locale. + * + *
Note: This API is not user-aware. It returns the system locales for the foreground + * user. + */ + @NonNull + public LocaleList getSystemLocales() { + try { + return mService.getSystemLocales(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + /** * Sets the current system locales to the provided value. * @@ -142,19 +161,4 @@ public class LocaleManager { } } - /** - * Returns the current system locales for the device. - * - * @hide - */ - @TestApi - @Nullable - public LocaleList getSystemLocales() { - try { - return ActivityManager.getService().getConfiguration().getLocales(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - } - } diff --git a/services/core/java/com/android/server/locales/LocaleManagerService.java b/services/core/java/com/android/server/locales/LocaleManagerService.java index 176c08c8da293..924db6a49eeb7 100644 --- a/services/core/java/com/android/server/locales/LocaleManagerService.java +++ b/services/core/java/com/android/server/locales/LocaleManagerService.java @@ -22,12 +22,14 @@ import android.Manifest; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.UserIdInt; +import android.app.ActivityManager; import android.app.ActivityManagerInternal; import android.app.ILocaleManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.PackageManagerInternal; +import android.content.res.Configuration; import android.os.Binder; import android.os.HandlerThread; import android.os.LocaleList; @@ -153,6 +155,12 @@ public class LocaleManagerService extends SystemService { return LocaleManagerService.this.getApplicationLocales(appPackageName, userId); } + @Override + @NonNull + public LocaleList getSystemLocales() throws RemoteException { + return LocaleManagerService.this.getSystemLocales(); + } + @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, String[] args, ShellCallback callback, @@ -426,6 +434,32 @@ public class LocaleManagerService extends SystemService { return null; } + /** + * Returns the current system locales. + */ + @NonNull + public LocaleList getSystemLocales() throws RemoteException { + final long token = Binder.clearCallingIdentity(); + try { + return getSystemLocalesUnchecked(); + } finally { + Binder.restoreCallingIdentity(token); + } + } + + @NonNull + private LocaleList getSystemLocalesUnchecked() throws RemoteException { + LocaleList systemLocales = null; + Configuration conf = ActivityManager.getService().getConfiguration(); + if (conf != null) { + systemLocales = conf.getLocales(); + } + if (systemLocales == null) { + systemLocales = LocaleList.getEmptyLocaleList(); + } + return systemLocales; + } + private void logMetric(@NonNull AppLocaleChangedAtomRecord atomRecordForMetrics) { FrameworkStatsLog.write(FrameworkStatsLog.APPLICATION_LOCALES_CHANGED, atomRecordForMetrics.mCallingUid,