Merge "Add getSystemLocale public API in response to developer feedback." into tm-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
ef269fabde
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,4 +40,9 @@ import android.os.LocaleList;
|
||||
*/
|
||||
LocaleList getApplicationLocales(String packageName, int userId);
|
||||
|
||||
/**
|
||||
* Returns the current system locales.
|
||||
*/
|
||||
LocaleList getSystemLocales();
|
||||
|
||||
}
|
||||
@@ -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.
|
||||
*
|
||||
* <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.
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user