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

Commit c7a4bee4 authored by Josh Hou's avatar Josh Hou Committed by Android (Google) Code Review
Browse files

Merge "Remove unsupported per-app locales after the LocaleConfig override" into udc-dev

parents 0dedd086 42a97933
Loading
Loading
Loading
Loading
+0 −180
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 com.android.server.locales;

import android.app.LocaleConfig;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.LocaleList;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.ArraySet;
import android.util.FeatureFlagUtils;
import android.util.Log;
import android.util.Slog;

import com.android.internal.annotations.VisibleForTesting;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
 * Track when a app is being updated.
 */
public class AppUpdateTracker {
    private static final String TAG = "AppUpdateTracker";

    private final Context mContext;
    private final LocaleManagerService mLocaleManagerService;
    private final LocaleManagerBackupHelper mBackupHelper;

    AppUpdateTracker(Context context, LocaleManagerService localeManagerService,
            LocaleManagerBackupHelper backupHelper) {
        mContext = context;
        mLocaleManagerService = localeManagerService;
        mBackupHelper = backupHelper;
    }

    /**
     * <p><b>Note:</b> This is invoked by service's common monitor
     * {@link LocaleManagerServicePackageMonitor#onPackageUpdateFinished} when a package is upgraded
     * on device.
     */
    public void onPackageUpdateFinished(String packageName, int uid) {
        Log.d(TAG, "onPackageUpdateFinished " + packageName);
        int userId = UserHandle.getUserId(uid);
        cleanApplicationLocalesIfNeeded(packageName, userId);
    }

    /**
     * When the user has set per-app locales for a specific application from a delegate selector,
     * and then the LocaleConfig of that application is removed in the upgraded version, the per-app
     * locales needs to be reset to system default locales to avoid the user being unable to change
     * system locales setting.
     */
    private void cleanApplicationLocalesIfNeeded(String packageName, int userId) {
        Set<String> packageNames = new ArraySet<>();
        SharedPreferences delegateAppLocalePackages = mBackupHelper.getPersistedInfo();
        if (delegateAppLocalePackages != null) {
            packageNames = delegateAppLocalePackages.getStringSet(Integer.toString(userId),
                    new ArraySet<>());
        }

        try {
            LocaleList appLocales = mLocaleManagerService.getApplicationLocales(packageName,
                    userId);
            if (appLocales.isEmpty() || isLocalesExistedInLocaleConfig(appLocales, packageName,
                    userId) || !packageNames.contains(packageName)) {
                return;
            }
        } catch (RemoteException | IllegalArgumentException e) {
            Slog.e(TAG, "Exception when getting locales for " + packageName, e);
            return;
        }

        Slog.d(TAG, "Clear app locales for " + packageName);
        try {
            mLocaleManagerService.setApplicationLocales(packageName, userId,
                    LocaleList.forLanguageTags(""), false);
        } catch (RemoteException | IllegalArgumentException e) {
            Slog.e(TAG, "Could not clear locales for " + packageName, e);
        }
    }

    /**
     * Check whether the LocaleConfig is existed and the per-app locales is presented in the
     * LocaleConfig file after the application is upgraded.
     */
    private boolean isLocalesExistedInLocaleConfig(LocaleList appLocales, String packageName,
            int userId) {
        LocaleList packageLocalesList = getPackageLocales(packageName, userId);
        HashSet<Locale> packageLocales = new HashSet<>();

        if (isSettingsAppLocalesOptIn()) {
            if (packageLocalesList == null || packageLocalesList.isEmpty()) {
                // The app locale feature is not enabled by the app
                Slog.d(TAG, "opt-in: the app locale feature is not enabled");
                return false;
            }
        } else {
            if (packageLocalesList != null && packageLocalesList.isEmpty()) {
                // The app locale feature is not enabled by the app
                Slog.d(TAG, "opt-out: the app locale feature is not enabled");
                return false;
            }
        }

        if (packageLocalesList != null && !packageLocalesList.isEmpty()) {
            // The app has added the supported locales into the LocaleConfig
            for (int i = 0; i < packageLocalesList.size(); i++) {
                packageLocales.add(packageLocalesList.get(i));
            }
            if (!matchesLocale(packageLocales, appLocales)) {
                // The set app locales do not match with the list of app supported locales
                Slog.d(TAG, "App locales: " + appLocales.toLanguageTags()
                        + " are not existed in the supported locale list");
                return false;
            }
        }

        return true;
    }

    /**
     * Get locales from LocaleConfig.
     */
    @VisibleForTesting
    public LocaleList getPackageLocales(String packageName, int userId) {
        try {
            LocaleConfig localeConfig = new LocaleConfig(
                    mContext.createPackageContextAsUser(packageName, 0, UserHandle.of(userId)));
            if (localeConfig.getStatus() == LocaleConfig.STATUS_SUCCESS) {
                return localeConfig.getSupportedLocales();
            }
        } catch (PackageManager.NameNotFoundException e) {
            Slog.e(TAG, "Can not found the package name : " + packageName + " / " + e);
        }
        return null;
    }

    /**
     * Check whether the feature to show per-app locales list in Settings is enabled.
     */
    @VisibleForTesting
    public boolean isSettingsAppLocalesOptIn() {
        return FeatureFlagUtils.isEnabled(mContext,
                FeatureFlagUtils.SETTINGS_APP_LOCALE_OPT_IN_ENABLED);
    }

    private boolean matchesLocale(HashSet<Locale> supported, LocaleList appLocales) {
        if (supported.size() <= 0 || appLocales.size() <= 0) {
            return true;
        }

        for (int i = 0; i < appLocales.size(); i++) {
            final Locale appLocale = appLocales.get(i);
            if (supported.stream().anyMatch(
                    locale -> LocaleList.matchesLanguageAndScript(locale, appLocale))) {
                return true;
            }
        }

        return false;
    }
}
+59 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static com.android.server.locales.LocaleManagerService.DEBUG;

import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.app.LocaleConfig;
import android.app.backup.BackupManager;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -296,6 +297,16 @@ class LocaleManagerBackupHelper {
        }
    }

    /**
     * <p><b>Note:</b> This is invoked by service's common monitor
     * {@link LocaleManagerServicePackageMonitor#onPackageUpdateFinished} when a package is upgraded
     * on device.
     */
    void onPackageUpdateFinished(String packageName, int uid) {
        int userId = UserHandle.getUserId(uid);
        cleanApplicationLocalesIfNeeded(packageName, userId);
    }

    /**
     * <p><b>Note:</b> This is invoked by service's common monitor
     * {@link LocaleManagerServicePackageMonitor#onPackageDataCleared} when a package's data
@@ -608,4 +619,52 @@ class LocaleManagerBackupHelper {
            Slog.e(TAG, "failed to commit locale setter info");
        }
    }

    boolean areLocalesSetFromDelegate(@UserIdInt int userId, String packageName) {
        if (mDelegateAppLocalePackages == null) {
            Slog.w(TAG, "Failed to persist data into the shared preference!");
            return false;
        }

        String user = Integer.toString(userId);
        Set<String> packageNames = new ArraySet<>(
                mDelegateAppLocalePackages.getStringSet(user, new ArraySet<>()));

        return packageNames.contains(packageName);
    }

    /**
     * When the user has set per-app locales for a specific application from a delegate selector,
     * and then the LocaleConfig of that application is removed in the upgraded version, the per-app
     * locales need to be removed or reset to system default locales to avoid the user being unable
     * to change system locales setting.
     */
    private void cleanApplicationLocalesIfNeeded(String packageName, int userId) {
        if (mDelegateAppLocalePackages == null) {
            Slog.w(TAG, "Failed to persist data into the shared preference!");
            return;
        }

        String user = Integer.toString(userId);
        Set<String> packageNames = new ArraySet<>(
                mDelegateAppLocalePackages.getStringSet(user, new ArraySet<>()));
        try {
            LocaleList appLocales = mLocaleManagerService.getApplicationLocales(packageName,
                    userId);
            if (appLocales.isEmpty() || !packageNames.contains(packageName)) {
                return;
            }
        } catch (RemoteException | IllegalArgumentException e) {
            Slog.e(TAG, "Exception when getting locales for " + packageName, e);
            return;
        }

        try {
            LocaleConfig localeConfig = new LocaleConfig(
                    mContext.createPackageContextAsUser(packageName, 0, UserHandle.of(userId)));
            mLocaleManagerService.removeUnsupportedAppLocales(packageName, userId, localeConfig);
        } catch (PackageManager.NameNotFoundException e) {
            Slog.e(TAG, "Can not found the package name : " + packageName + " / " + e);
        }
    }
}
+38 −31
Original line number Diff line number Diff line
@@ -129,11 +129,8 @@ public class LocaleManagerService extends SystemService {

        mBackupHelper = new LocaleManagerBackupHelper(this,
                mPackageManager, broadcastHandlerThread);
        AppUpdateTracker appUpdateTracker =
                new AppUpdateTracker(mContext, this, mBackupHelper);

        mPackageMonitor = new LocaleManagerServicePackageMonitor(mBackupHelper,
                systemAppUpdateTracker, appUpdateTracker, this);
                systemAppUpdateTracker, this);
        mPackageMonitor.register(context, broadcastHandlerThread.getLooper(),
                UserHandle.ALL,
                true);
@@ -598,7 +595,7 @@ public class LocaleManagerService extends SystemService {
    }

    private void setOverrideLocaleConfigUnchecked(@NonNull String appPackageName,
            @UserIdInt int userId, @Nullable LocaleConfig overridelocaleConfig,
            @UserIdInt int userId, @Nullable LocaleConfig overrideLocaleConfig,
            @NonNull AppSupportedLocalesChangedAtomRecord atomRecord) {
        synchronized (mWriteLock) {
            if (DEBUG) {
@@ -606,26 +603,35 @@ public class LocaleManagerService extends SystemService {
                        "set the override LocaleConfig for package " + appPackageName + " and user "
                                + userId);
            }
            LocaleConfig resLocaleConfig = null;
            try {
                resLocaleConfig = LocaleConfig.fromContextIgnoringOverride(
                        mContext.createPackageContext(appPackageName, 0));
            } catch (PackageManager.NameNotFoundException e) {
                Slog.e(TAG, "Unknown package name " + appPackageName);
                return;
            }
            final File file = getXmlFileNameForUser(appPackageName, userId);

            if (overridelocaleConfig == null) {
            if (overrideLocaleConfig == null) {
                if (file.exists()) {
                    Slog.d(TAG, "remove the override LocaleConfig");
                    file.delete();
                }
                removeUnsupportedAppLocales(appPackageName, userId, resLocaleConfig);
                atomRecord.setOverrideRemoved(true);
                atomRecord.setStatus(FrameworkStatsLog
                        .APP_SUPPORTED_LOCALES_CHANGED__STATUS__SUCCESS);
                return;
            } else {
                if (overridelocaleConfig.isSameLocaleConfig(
                if (overrideLocaleConfig.isSameLocaleConfig(
                        getOverrideLocaleConfig(appPackageName, userId))) {
                    Slog.d(TAG, "the same override, ignore it");
                    atomRecord.setSameAsPrevConfig(true);
                    return;
                }

                LocaleList localeList = overridelocaleConfig.getSupportedLocales();
                LocaleList localeList = overrideLocaleConfig.getSupportedLocales();
                // Normally the LocaleList object should not be null. However we reassign it as the
                // empty list in case it happens.
                if (localeList == null) {
@@ -654,17 +660,11 @@ public class LocaleManagerService extends SystemService {
                }
                atomicFile.finishWrite(stream);
                // Clear per-app locales if they are not in the override LocaleConfig.
                removeUnsupportedAppLocales(appPackageName, userId, overridelocaleConfig);
                try {
                    Context appContext = mContext.createPackageContext(appPackageName, 0);
                    if (overridelocaleConfig.isSameLocaleConfig(
                            LocaleConfig.fromContextIgnoringOverride(appContext))) {
                removeUnsupportedAppLocales(appPackageName, userId, overrideLocaleConfig);
                if (overrideLocaleConfig.isSameLocaleConfig(resLocaleConfig)) {
                    Slog.d(TAG, "setOverrideLocaleConfig, same as the app's LocaleConfig");
                    atomRecord.setSameAsResConfig(true);
                }
                } catch (PackageManager.NameNotFoundException e) {
                    Slog.e(TAG, "Unknown package name " + appPackageName);
                }
                atomRecord.setStatus(FrameworkStatsLog
                        .APP_SUPPORTED_LOCALES_CHANGED__STATUS__SUCCESS);
                if (DEBUG) {
@@ -675,31 +675,38 @@ public class LocaleManagerService extends SystemService {
    }

    /**
     * Checks if the per-app locales are in the new override LocaleConfig. Per-app locales
     * missing from the new LocaleConfig will be removed.
     * Checks if the per-app locales are in the LocaleConfig. Per-app locales missing from the
     * LocaleConfig will be removed.
     */
    private void removeUnsupportedAppLocales(String appPackageName, int userId,
    void removeUnsupportedAppLocales(String appPackageName, int userId,
            LocaleConfig localeConfig) {
        LocaleList appLocales = getApplicationLocalesUnchecked(appPackageName, userId);
        // Remove the app locale from the locale list if it doesn't exist in the override
        // LocaleConfig.
        // Remove the per-app locales from the locale list if they don't exist in the LocaleConfig.
        boolean resetAppLocales = false;
        List<Locale> newAppLocales = new ArrayList<Locale>();

        if (localeConfig == null) {
            //Reset the app locales to the system default
            Slog.i(TAG, "There is no LocaleConfig, reset app locales");
            resetAppLocales = true;
        } else {
            for (int i = 0; i < appLocales.size(); i++) {
                if (!localeConfig.containsLocale(appLocales.get(i))) {
                Slog.i(TAG, "reset the app locales");
                    Slog.i(TAG, "Missing from the LocaleConfig, reset app locales");
                    resetAppLocales = true;
                    continue;
                }
                newAppLocales.add(appLocales.get(i));
            }
        }

        if (resetAppLocales) {
            // Reset the app locales
            Locale[] locales = new Locale[newAppLocales.size()];
            try {
                setApplicationLocales(appPackageName, userId,
                        new LocaleList(newAppLocales.toArray(locales)), false);
                        new LocaleList(newAppLocales.toArray(locales)),
                        mBackupHelper.areLocalesSetFromDelegate(userId, appPackageName));
            } catch (RemoteException | IllegalArgumentException e) {
                Slog.e(TAG, "Could not set locales for " + appPackageName, e);
            }
@@ -829,7 +836,7 @@ public class LocaleManagerService extends SystemService {
    @NonNull
    private File getXmlFileNameForUser(@NonNull String appPackageName, @UserIdInt int userId) {
        // TODO(b/262752965): use per-package data directory
        final File dir = new File(Environment.getDataSystemDeDirectory(userId), LOCALE_CONFIGS);
        final File dir = new File(Environment.getDataSystemCeDirectory(userId), LOCALE_CONFIGS);
        return new File(dir, appPackageName + SUFFIX_FILE_NAME);
    }

+1 −4
Original line number Diff line number Diff line
@@ -37,16 +37,13 @@ import com.android.internal.content.PackageMonitor;
final class LocaleManagerServicePackageMonitor extends PackageMonitor {
    private LocaleManagerBackupHelper mBackupHelper;
    private SystemAppUpdateTracker mSystemAppUpdateTracker;
    private AppUpdateTracker mAppUpdateTracker;
    private LocaleManagerService mLocaleManagerService;

    LocaleManagerServicePackageMonitor(@NonNull LocaleManagerBackupHelper localeManagerBackupHelper,
            @NonNull SystemAppUpdateTracker systemAppUpdateTracker,
            @NonNull AppUpdateTracker appUpdateTracker,
            @NonNull LocaleManagerService localeManagerService) {
        mBackupHelper = localeManagerBackupHelper;
        mSystemAppUpdateTracker = systemAppUpdateTracker;
        mAppUpdateTracker = appUpdateTracker;
        mLocaleManagerService = localeManagerService;
    }

@@ -68,7 +65,7 @@ final class LocaleManagerServicePackageMonitor extends PackageMonitor {

    @Override
    public void onPackageUpdateFinished(String packageName, int uid) {
        mAppUpdateTracker.onPackageUpdateFinished(packageName, uid);
        mBackupHelper.onPackageUpdateFinished(packageName, uid);
        mSystemAppUpdateTracker.onPackageUpdateFinished(packageName, uid);
    }
}
+0 −190

File deleted.

Preview size limit exceeded, changes collapsed.

Loading