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

Commit dea37e1a authored by Josh Hou's avatar Josh Hou
Browse files

[Panlingual] Dynamic locales

Allows applications to  update their list of supported locales while running, without an update of the app’s software.

Bug: 248446474
Test: atest LocaleManagerTests
      atest LocaleConfigTest
      Use the shell command to set/get the override LocaleConfig
Change-Id: I1bfd5431302d4d31b0f8891dd5a7419641657416
parent dca85cd1
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -5863,10 +5863,15 @@ package android.app {
    method @Deprecated public android.view.Window startActivity(String, android.content.Intent);
  }
  public class LocaleConfig {
  public class LocaleConfig implements android.os.Parcelable {
    ctor public LocaleConfig(@NonNull android.content.Context);
    ctor public LocaleConfig(@NonNull android.os.LocaleList);
    method public int describeContents();
    method @NonNull public static android.app.LocaleConfig fromResources(@NonNull android.content.Context);
    method public int getStatus();
    method @Nullable public android.os.LocaleList getSupportedLocales();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.app.LocaleConfig> CREATOR;
    field public static final int STATUS_NOT_SPECIFIED = 1; // 0x1
    field public static final int STATUS_PARSING_FAILED = 2; // 0x2
    field public static final int STATUS_SUCCESS = 0; // 0x0
@@ -5877,8 +5882,10 @@ 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 @Nullable public android.app.LocaleConfig getOverrideLocaleConfig();
    method @NonNull public android.os.LocaleList getSystemLocales();
    method public void setApplicationLocales(@NonNull android.os.LocaleList);
    method public void setOverrideLocaleConfig(@Nullable android.app.LocaleConfig);
  }
  public class MediaRouteActionProvider extends android.view.ActionProvider {
+4 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package android.app;

import android.app.LocaleConfig;
import android.os.LocaleList;

/**
@@ -29,7 +30,6 @@ import android.os.LocaleList;
 * @hide
 */
 interface ILocaleManager {

     /**
      * Sets a specified app’s app-specific UI locales.
      */
@@ -45,4 +45,7 @@ import android.os.LocaleList;
       */
     LocaleList getSystemLocales();

     void setOverrideLocaleConfig(String packageName, int userId, in LocaleConfig localeConfig);

     LocaleConfig getOverrideLocaleConfig(String packageName, int userId);
 }
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app;

parcelable LocaleConfig;
 No newline at end of file
+127 −8
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.os.LocaleList;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Slog;
import android.util.Xml;
@@ -37,23 +39,27 @@ import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
 * The LocaleConfig of an application.
 * Defined in an XML resource file with an {@code <locale-config>} element and
 * referenced in the manifest via {@code android:localeConfig} on
 * {@code <application>}.
 * There are two sources. One is from an XML resource file with an {@code <locale-config>} element
 * and referenced in the manifest via {@code android:localeConfig} on {@code <application>}. The
 * other is that the application dynamically provides an override version which is persisted in
 * {@link LocaleManager#setOverrideLocaleConfig(LocaleConfig)}.
 *
 * <p>For more information, see
 * <p>For more information about the LocaleConfig from an XML resource file, see
 * <a href="https://developer.android.com/about/versions/13/features/app-languages#use-localeconfig">
 * the section on per-app language preferences</a>.
 *
 * @attr ref android.R.styleable#LocaleConfig_Locale_name
 * @attr ref android.R.styleable#AndroidManifestApplication_localeConfig
 *
 * <p>For more information about the LocaleConfig overridden by the application, see
 * TODO(b/261528306): add link to guide
 */
public class LocaleConfig {

public class LocaleConfig implements Parcelable {
    private static final String TAG = "LocaleConfig";
    public static final String TAG_LOCALE_CONFIG = "locale-config";
    public static final String TAG_LOCALE = "locale";
@@ -83,13 +89,46 @@ public class LocaleConfig {
    public @interface Status{}

    /**
     * Returns the LocaleConfig for the provided application context
     * Returns an override LocaleConfig if it has been set via
     * {@link LocaleManager#setOverrideLocaleConfig(LocaleConfig)}. Otherwise, returns the
     * LocaleConfig from the application resources.
     *
     * @param context the context of the application
     * @param context the context of the application.
     *
     * @see Context#createPackageContext(String, int).
     */
    public LocaleConfig(@NonNull Context context) {
        this(context, true);
    }

    /**
     * Returns a LocaleConfig from the application resources regardless of whether any LocaleConfig
     * is overridden via {@link LocaleManager#setOverrideLocaleConfig(LocaleConfig)}.
     *
     * @param context the context of the application.
     *
     * @see Context#createPackageContext(String, int).
     */
    @NonNull
    public static LocaleConfig fromResources(@NonNull Context context) {
        return new LocaleConfig(context, false);
    }

    private LocaleConfig(@NonNull Context context, boolean allowOverride) {
        if (allowOverride) {
            LocaleManager localeManager = context.getSystemService(LocaleManager.class);
            if (localeManager == null) {
                Slog.w(TAG, "LocaleManager is null, cannot get the override LocaleConfig");
                return;
            }
            LocaleConfig localeConfig = localeManager.getOverrideLocaleConfig();
            if (localeConfig != null) {
                Slog.d(TAG, "Has the override LocaleConfig");
                mStatus = localeConfig.getStatus();
                mLocales = localeConfig.getSupportedLocales();
                return;
            }
        }
        int resId = 0;
        Resources res = context.getResources();
        try {
@@ -108,6 +147,38 @@ public class LocaleConfig {
        }
    }

    /**
     * Return the LocaleConfig with any sequence of locales combined into a {@link LocaleList}.
     *
     * <p><b>Note:</b> Applications seeking to create an override LocaleConfig via
     * {@link LocaleManager#setOverrideLocaleConfig(LocaleConfig)} should use this constructor to
     * first create the LocaleConfig they intend the system to see as the override.
     *
     * <p><b>Note:</b> The creation of this LocaleConfig does not automatically mean it will
     * become the override config for an application. Any LocaleConfig desired to be the override
     * must be passed into the {@link LocaleManager#setOverrideLocaleConfig(LocaleConfig)},
     * otherwise it will not persist or affect the system’s understanding of app-supported
     * resources.
     *
     * @param locales the desired locales for a specified application
     */
    public LocaleConfig(@NonNull LocaleList locales) {
        mStatus = STATUS_SUCCESS;
        mLocales = locales;
    }

    /**
     * Instantiate a new LocaleConfig from the data in a Parcel that was
     * previously written with {@link #writeToParcel(Parcel, int)}.
     *
     * @param in The Parcel containing the previously written LocaleConfig,
     * positioned at the location in the buffer where it was written.
     */
    private LocaleConfig(@NonNull Parcel in) {
        mStatus = in.readInt();
        mLocales = in.readTypedObject(LocaleList.CREATOR);
    }

    /**
     * Parse the XML content and get the locales supported by the application
     */
@@ -165,4 +236,52 @@ public class LocaleConfig {
    public @Status int getStatus() {
        return mStatus;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeInt(mStatus);
        dest.writeTypedObject(mLocales, flags);
    }

    public static final @NonNull Parcelable.Creator<LocaleConfig> CREATOR =
            new Parcelable.Creator<LocaleConfig>() {
                @Override
                public LocaleConfig createFromParcel(Parcel source) {
                    return new LocaleConfig(source);
                }

                @Override
                public LocaleConfig[] newArray(int size) {
                    return new LocaleConfig[size];
                }
            };

    /**
     * Compare whether the locale is existed in the {@code mLocales} of the LocaleConfig.
     *
     * @param locale The {@link Locale} to compare for.
     *
     * @return true if the locale is existed in the {@code mLocales} of the LocaleConfig, false
     * otherwise.
     *
     * @hide
     */
    public boolean containsLocale(Locale locale) {
        if (mLocales == null) {
            return false;
        }

        for (int i = 0; i < mLocales.size(); i++) {
            if (LocaleList.matchesLanguageAndScript(mLocales.get(i), locale)) {
                return true;
            }
        }

        return false;
    }
}
+52 −6
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ 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;
@@ -29,8 +30,9 @@ import android.os.LocaleList;
import android.os.RemoteException;

/**
 * This class gives access to system locale services. These services allow applications to control
 * granular locale settings (such as per-app locales).
 * This class gives access to system locale services. These services allow applications to
 * control granular locale settings (such as per-app locales) or override their list of supported
 * locales while running.
 *
 * <p> Third party applications should treat this as a write-side surface, and continue reading
 * locales via their in-process {@link LocaleList}s.
@@ -106,8 +108,8 @@ public class LocaleManager {
    private void setApplicationLocales(@NonNull String appPackageName, @NonNull LocaleList locales,
            boolean fromDelegate) {
        try {
            mService.setApplicationLocales(appPackageName, mContext.getUser().getIdentifier(),
                    locales, fromDelegate);
            mService.setApplicationLocales(appPackageName, mContext.getUserId(), locales,
                    fromDelegate);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -144,8 +146,7 @@ public class LocaleManager {
    @NonNull
    public LocaleList getApplicationLocales(@NonNull String appPackageName) {
        try {
            return mService.getApplicationLocales(appPackageName, mContext.getUser()
                    .getIdentifier());
            return mService.getApplicationLocales(appPackageName, mContext.getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -187,4 +188,49 @@ public class LocaleManager {
        }
    }

    /**
     * Sets the override LocaleConfig for the calling app.
     *
     * <p><b>Note:</b> Only the app itself with the same user can override its own LocaleConfig.
     *
     * <p><b>Note:</b> This function takes in a {@link LocaleConfig} which is intended to
     * override the original config in the application’s resources. This LocaleConfig will become
     * the override config, and stored in a system file for future access.
     *
     * <p><b>Note:</b> Using this function, applications can update their list of supported
     * locales while running, without an update of the application’s software. For more
     * information, see TODO(b/261528306): add link to guide.
     *
     * <p>Applications can remove the override LocaleConfig with a {@code null} object.
     *
     * @param localeConfig the desired {@link LocaleConfig} for the calling app.
     */
    @UserHandleAware
    public void setOverrideLocaleConfig(@Nullable LocaleConfig localeConfig) {
        try {
            // The permission android.Manifest.permission#SET_APP_SPECIFIC_LOCALECONFIG is
            // required to set an override LocaleConfig of another packages
            mService.setOverrideLocaleConfig(mContext.getPackageName(), mContext.getUserId(),
                    localeConfig);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns the override LocaleConfig for the calling app.
     *
     * @return the override LocaleConfig, or {@code null} if the LocaleConfig isn't overridden.
     */
    @Nullable
    @UserHandleAware
    public LocaleConfig getOverrideLocaleConfig() {
        try {
            return mService.getOverrideLocaleConfig(mContext.getPackageName(),
                    mContext.getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

}
Loading