Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c6f766d7 authored by Pratyush More's avatar Pratyush More
Browse files

Add getSystemLocale public API in response to developer feedback.

For more context/information, see the attached bug.

Bug: 223593326
Test: atest CtsLocaleManagerTestCases
Change-Id: I1c55fe4f2767e5a0acfe8b455947e8975d11f381
parent 4e45ade9
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5805,6 +5805,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);
  }
+0 −1
Original line number Diff line number Diff line
@@ -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);
  }

+5 −0
Original line number Diff line number Diff line
@@ -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
+16 −12
Original line number Diff line number Diff line
@@ -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;
@@ -127,31 +126,36 @@ public class LocaleManager {
    }

    /**
     * Sets the current system locales to the provided value.
     * Returns the current system locales, ignoring app-specific overrides.
     *
     * @hide
     * <p><b>Note:</b> 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.
     *
     * <p><b>Note:</b> This API is not user-aware. It returns the system locales for the foreground
     * user.
     */
    @TestApi
    public void setSystemLocales(@NonNull LocaleList locales) {
    @NonNull
    public LocaleList getSystemLocales() {
        try {
            Configuration conf = ActivityManager.getService().getConfiguration();
            conf.setLocales(locales);
            ActivityManager.getService().updatePersistentConfiguration(conf);
            return mService.getSystemLocales();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the current system locales for the device.
     * Sets the current system locales to the provided value.
     *
     * @hide
     */
    @TestApi
    @Nullable
    public LocaleList getSystemLocales() {
    public void setSystemLocales(@NonNull LocaleList locales) {
        try {
            return ActivityManager.getService().getConfiguration().getLocales();
            Configuration conf = ActivityManager.getService().getConfiguration();
            conf.setLocales(locales);
            ActivityManager.getService().updatePersistentConfiguration(conf);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+34 −0
Original line number Diff line number Diff line
@@ -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,