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

Commit cee48131 authored by Daniel Perez's avatar Daniel Perez
Browse files

Add metrics logging for B&R of locale data in settings.

This is part of an effort to add metrics support to SettingsBackupAgent. For more details, see go/settings-backup-agent-metrics-design

Bug: 379861078
Change-Id: I234549f11a1a2e6296e8611aa2bc973019be3b92
Flag: com.android.server.backup.enable_metrics_settings_backup_agents
Tested: SettingsHelperTest and manually by verifying that the metrics are logged during backup (https://screenshot.googleplex.com/9gkq9bUtXXyaVXp) and restore (https://screenshot.googleplex.com/BqnQFn2CBguLfU6). Note that I tried adding tests for more methods in SettingsHelperTest, but I couldn't due to an issue with permissions when calling `am.updatePersistentConfigurationWithAttribution`.
parent 2faf819d
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.net.Uri;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.LocaleList;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.provider.Settings;
@@ -269,7 +270,7 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        byte[] secureSettingsData = getSecureSettings();
        byte[] globalSettingsData = getGlobalSettings();
        byte[] lockSettingsData   = getLockSettings(UserHandle.myUserId());
        byte[] locale = mSettingsHelper.getLocaleData();
        byte[] locale = getLocaleSettings();
        byte[] softApConfigData = getSoftAPConfiguration();
        byte[] netPoliciesData = getNetworkPolicies();
        byte[] wifiFullConfigData = getNewWifiConfigData();
@@ -408,7 +409,12 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                case KEY_LOCALE :
                    byte[] localeData = new byte[size];
                    data.readEntityData(localeData, 0, size);
                    mSettingsHelper.setLocaleData(localeData, size);
                    mSettingsHelper
                        .setLocaleData(
                            localeData,
                            size,
                            mBackupRestoreEventLogger,
                            KEY_LOCALE);
                    break;

                case KEY_WIFI_CONFIG :
@@ -545,7 +551,9 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            if (DEBUG_BACKUP) Log.d(TAG, nBytes + " bytes of locale data");
            if (nBytes > buffer.length) buffer = new byte[nBytes];
            in.readFully(buffer, 0, nBytes);
            mSettingsHelper.setLocaleData(buffer, nBytes);
            mSettingsHelper
                .setLocaleData(
                    buffer, nBytes, mBackupRestoreEventLogger, KEY_LOCALE);

            // Restore older backups performing the necessary migrations.
            if (version < FULL_BACKUP_ADDED_WIFI_NEW) {
@@ -1410,6 +1418,15 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        return mWifiManager.retrieveBackupData();
    }

    private byte[] getLocaleSettings() {
        if (!areAgentMetricsEnabled) {
            return mSettingsHelper.getLocaleData();
        }
        LocaleList localeList = mSettingsHelper.getLocaleList();
        numberOfSettingsPerKey.put(KEY_LOCALE, localeList.size());
        return localeList.toLanguageTags().getBytes();
    }

    private void restoreNewWifiConfigData(byte[] bytes) {
        if (DEBUG_BACKUP) {
            Log.v(TAG, "Applying restored wifi data");
+27 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.providers.settings;
import android.annotation.NonNull;
import android.app.ActivityManager;
import android.app.IActivityManager;
import android.app.backup.BackupRestoreEventLogger;
import android.app.backup.IBackupManager;
import android.content.ContentResolver;
import android.content.ContentValues;
@@ -43,6 +44,7 @@ import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.LocalePicker;
import com.android.server.backup.Flags;
import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager;

import java.io.FileNotFoundException;
@@ -68,6 +70,10 @@ public class SettingsHelper {
    /** See frameworks/base/core/res/res/values/config.xml#config_keyChordPowerVolumeUp **/
    private static final int KEY_CHORD_POWER_VOLUME_UP_GLOBAL_ACTIONS = 2;

    // Error messages for logging metrics.
    private static final String ERROR_REMOTE_EXCEPTION_SETTING_LOCALE_DATA =
        "remote_exception_setting_locale_data";

    private Context mContext;
    private AudioManager mAudioManager;
    private TelephonyManager mTelephonyManager;
@@ -528,6 +534,11 @@ public class SettingsHelper {
        return conf.getLocales().toLanguageTags().getBytes();
    }

    LocaleList getLocaleList() {
        Configuration conf = mContext.getResources().getConfiguration();
        return conf.getLocales();
    }

    private static Locale toFullLocale(@NonNull Locale locale) {
        if (locale.getScript().isEmpty() || locale.getCountry().isEmpty()) {
            return ULocale.addLikelySubtags(ULocale.forLocale(locale)).toLocale();
@@ -653,8 +664,12 @@ public class SettingsHelper {
     * code and {@code CC} is a two letter country code.
     *
     * @param data the comma separated BCP-47 language tags in bytes.
     * @param size the size of the data in bytes.
     * @param backupRestoreEventLogger the logger to log the restore event.
     * @param dataType the data type of the setting for logging purposes.
     */
    /* package */ void setLocaleData(byte[] data, int size) {
    /* package */ void setLocaleData(
        byte[] data, int size, BackupRestoreEventLogger backupRestoreEventLogger, String dataType) {
        final Configuration conf = mContext.getResources().getConfiguration();

        // Replace "_" with "-" to deal with older backups.
@@ -681,8 +696,18 @@ public class SettingsHelper {

            am.updatePersistentConfigurationWithAttribution(config, mContext.getOpPackageName(),
                    mContext.getAttributionTag());
            if (Flags.enableMetricsSettingsBackupAgents()) {
                backupRestoreEventLogger
                    .logItemsRestored(dataType, localeList.size());
            }
        } catch (RemoteException e) {
            // Intentionally left blank
            if (Flags.enableMetricsSettingsBackupAgents()) {
                backupRestoreEventLogger
                    .logItemsRestoreFailed(
                        dataType,
                        localeList.size(),
                        ERROR_REMOTE_EXCEPTION_SETTING_LOCALE_DATA);
            }
        }
    }

+13 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.res.AssetFileDescriptor;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.MatrixCursor;
@@ -83,6 +84,9 @@ public class SettingsHelperTest {
            "content://media/internal/audio/media/30?title=DefaultAlarm&canonical=1";
    private static final String VIBRATION_FILE_NAME = "haptics.xml";

    private static final LocaleList LOCALE_LIST =
        LocaleList.forLanguageTags("en-US,en-UK");

    private SettingsHelper mSettingsHelper;

    @Rule
@@ -778,6 +782,15 @@ public class SettingsHelperTest {
        assertThat(getAutoRotationSettingValue()).isEqualTo(previousValue);
    }

    @Test
    public void getLocaleList_returnsLocaleList() {
        Configuration config = new Configuration();
        config.setLocales(LOCALE_LIST);
        when(mResources.getConfiguration()).thenReturn(config);

        assertThat(mSettingsHelper.getLocaleList()).isEqualTo(LOCALE_LIST);
    }

    private int getAutoRotationSettingValue() {
        return Settings.System.getInt(mContentResolver,
                Settings.System.ACCELEROMETER_ROTATION,