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

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

Clean up enable_metrics_settings_backup_agents flag.


Change-Id: I30c95046cc064a303007e52349758f38e66f1996
Bug: 424010469
Flag: EXEMPT removing com.android.server.backup.enable_metrics_settings_backup_agents
parent a5e92ade
Loading
Loading
Loading
Loading
+96 −174
Original line number Diff line number Diff line
@@ -269,7 +269,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
    private int mDefaultBlinkInterval;

    @Nullable private BackupRestoreEventLogger mBackupRestoreEventLogger;
    @VisibleForTesting boolean areAgentMetricsEnabled = false;
    @VisibleForTesting protected Map<String, Integer> numberOfSettingsPerKey;

    @Override
@@ -289,12 +288,9 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                        com.android.internal.R.array.accessibility_text_cursor_blink_intervals);
        mSettingsHelper = new SettingsHelper(this);
        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (com.android.server.backup.Flags.enableMetricsSettingsBackupAgents()) {
        mBackupRestoreEventLogger = this.getBackupRestoreEventLogger();
        mSettingsHelper.setBackupRestoreEventLogger(mBackupRestoreEventLogger);
        numberOfSettingsPerKey = new HashMap<>();
            areAgentMetricsEnabled = true;
        }
        super.onCreate();
    }

@@ -727,8 +723,7 @@ public class SettingsBackupAgent extends BackupAgentHelper {

    @VisibleForTesting
    void writeDataForKey(String key, byte[] data, BackupDataOutput output) {
        boolean shouldLogMetrics =
            areAgentMetricsEnabled && numberOfSettingsPerKey.containsKey(key);
        boolean shouldLogMetrics = numberOfSettingsPerKey.containsKey(key);
        try {
            if (DEBUG_BACKUP) {
                Log.v(TAG, "Writing entity " + key + " of size " + data.length);
@@ -859,17 +854,13 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            // End marker
            out.writeUTF("");
            out.flush();
            if (areAgentMetricsEnabled) {
            numberOfSettingsPerKey.put(KEY_LOCK_SETTINGS, backedUpSettingsCount);
            }
        } catch (IOException ioe) {
            if (areAgentMetricsEnabled) {
            mBackupRestoreEventLogger.logItemsBackupFailed(
                KEY_LOCK_SETTINGS,
                NUM_LOCK_SETTINGS - backedUpSettingsCount,
                ERROR_IO_EXCEPTION);
        }
        }
        return baos.toByteArray();
    }

@@ -889,10 +880,8 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            data.readEntityData(settings, 0, settings.length);
        } catch (IOException ioe) {
            Log.e(TAG, "Couldn't read entity data");
            if (areAgentMetricsEnabled) {
            mBackupRestoreEventLogger.logItemsRestoreFailed(
                settingsKey, /* count= */ 1, ERROR_COULD_NOT_READ_ENTITY);
            }
            return;
        }
        restoreSettings(
@@ -972,12 +961,10 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                                + " removed from restore by "
                                + (isBlockedBySystem ? "system" : "dynamic")
                                + " block list");
                if (areAgentMetricsEnabled) {
                mBackupRestoreEventLogger.logItemsRestoreFailed(
                    settingsKey,
                    /* count= */ 1,
                    isBlockedBySystem ? ERROR_SKIPPED_BY_SYSTEM : ERROR_SKIPPED_BY_BLOCKLIST);
                }
                continue;
            }

@@ -988,20 +975,16 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            if (isSettingPreserved && !Settings.Secure.NAVIGATION_MODE.equals(key)) {
                Log.i(TAG, "Skipping restore for setting " + key + " as it is marked as "
                        + "preserved");
                if (areAgentMetricsEnabled) {
                mBackupRestoreEventLogger.logItemsRestoreFailed(
                        settingsKey, /* count= */ 1, ERROR_SKIPPED_PRESERVED);
                }
                continue;
            }

            if (LargeScreenSettings.doNotRestoreIfLargeScreenSetting(key, getBaseContext())) {
                Log.i(TAG, "Skipping restore for setting " + key + " as the target device "
                        + "is a large screen (i.e tablet or foldable)");
                if (areAgentMetricsEnabled) {
                        + "is a large screen (i.e tablet or foldable in unfolded state)");
                mBackupRestoreEventLogger.logItemsRestoreFailed(
                        settingsKey, /* count= */ 1, ERROR_SKIPPED_DUE_TO_LARGE_SCREEN);
                }
                continue;
            }

@@ -1041,10 +1024,8 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            if (!isValidSettingValue(key, value, allowlist.mSettingsValidators)) {
                Log.w(TAG, "Attempted restore of " + key + " setting, but its value didn't pass"
                        + " validation, value: " + value);
                if (areAgentMetricsEnabled) {
                mBackupRestoreEventLogger.logItemsRestoreFailed(
                        settingsKey, /* count= */ 1, ERROR_DID_NOT_PASS_VALIDATION);
                }
                continue;
            }

@@ -1053,19 +1034,13 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            String finalSettingsKey = settingsKey;
            if (movedToGlobal != null && movedToGlobal.contains(key)) {
                destination = Settings.Global.CONTENT_URI;
                if (areAgentMetricsEnabled) {
                finalSettingsKey = KEY_GLOBAL;
                }
            } else if (movedToSecure != null && movedToSecure.contains(key)) {
                destination = Settings.Secure.CONTENT_URI;
                if (areAgentMetricsEnabled) {
                finalSettingsKey = KEY_SECURE;
                }
            } else if (movedToSystem != null && movedToSystem.contains(key)) {
                destination = Settings.System.CONTENT_URI;
                if (areAgentMetricsEnabled) {
                finalSettingsKey = KEY_SYSTEM;
                }
            } else {
                destination = contentUri;
            }
@@ -1083,10 +1058,8 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                if (isSettingPreserved) {
                    Log.i(TAG, "Skipping restore for setting navigation_mode "
                        + "as it is marked as preserved");
                    if (areAgentMetricsEnabled) {
                    mBackupRestoreEventLogger.logItemsRestoreFailed(
                            finalSettingsKey, /* count= */ 1, ERROR_SKIPPED_PRESERVED);
                    }
                    continue;
                }
            } else if (Settings.Secure.SELECTED_SPELL_CHECKER.equals(key)) {
@@ -1336,22 +1309,18 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                        lockPatternUtils.setPinEnhancedPrivacyEnabled("1".equals(value), userId);
                        break;
                }
                if (areAgentMetricsEnabled) {
                mBackupRestoreEventLogger.logItemsRestored(KEY_LOCK_SETTINGS, /* count= */ 1);
                restoredLockSettingsCount++;
                }

            }
            in.close();
        } catch (IOException ioe) {
            if (areAgentMetricsEnabled) {
            mBackupRestoreEventLogger.logItemsRestoreFailed(
                    KEY_LOCK_SETTINGS,
                    NUM_LOCK_SETTINGS - restoredLockSettingsCount,
                    ERROR_IO_EXCEPTION);
        }
    }
    }

    private void restoreLockSettings(@UserIdInt int userId, BackupDataInput data) {
        final byte[] settings = new byte[data.getDataSize()];
@@ -1377,13 +1346,11 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        Cursor cursor, String[] settings, String settingsKey) {
        if (!cursor.moveToFirst()) {
            Log.e(TAG, "Couldn't read from the cursor");
            if (areAgentMetricsEnabled) {
            mBackupRestoreEventLogger
                .logItemsBackupFailed(
                    settingsKey,
                    settings.length,
                    ERROR_COULD_NOT_READ_FROM_CURSOR);
            }
            return new byte[0];
        }

@@ -1442,9 +1409,7 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            }
        }

        if (areAgentMetricsEnabled) {
        numberOfSettingsPerKey.put(settingsKey, backedUpSettingIndex);
        }

        // Aggregate the result.
        byte[] result = new byte[totalSize];
@@ -1472,17 +1437,14 @@ public class SettingsBackupAgent extends BackupAgentHelper {
    @VisibleForTesting
    byte[] getSoftAPConfiguration() {
        byte[] data = mWifiManager.retrieveSoftApBackupData();
        if (areAgentMetricsEnabled) {
        // We're unable to determine how many settings this includes, so we'll just log 1.
        numberOfSettingsPerKey.put(KEY_SOFTAP_CONFIG, 1);
        }
        return data;
    }

    @VisibleForTesting
    void restoreSoftApConfiguration(byte[] data) {
        SoftApConfiguration configInCloud;
        if (areAgentMetricsEnabled) {
        try {
            configInCloud = mWifiManager.restoreSoftApBackupData(data);
            mBackupRestoreEventLogger.logItemsRestored(KEY_SOFTAP_CONFIG, /* count= */ 1);
@@ -1491,9 +1453,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            mBackupRestoreEventLogger.logItemsRestoreFailed(
                KEY_SOFTAP_CONFIG, /* count= */ 1, ERROR_FAILED_TO_RESTORE_SOFTAP_CONFIG);
        }
        } else {
            configInCloud = mWifiManager.restoreSoftApBackupData(data);
        }
        if (configInCloud != null) {
            if (DEBUG) Log.d(TAG, "Successfully unMarshaled SoftApConfiguration ");
            // Depending on device hardware, we may need to notify the user of a setting change
@@ -1573,44 +1532,33 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                        out.writeByte(BackupUtils.NOT_NULL);
                        out.writeInt(marshaledPolicy.length);
                        out.write(marshaledPolicy);
                        if (areAgentMetricsEnabled) {
                        numberOfPoliciesBackedUp++;
                        }
                    } else {
                        out.writeByte(BackupUtils.NULL);
                    }
                }
                if (areAgentMetricsEnabled) {
                numberOfSettingsPerKey.put(KEY_NETWORK_POLICIES, numberOfPoliciesBackedUp);
                }
            } catch (IOException ioe) {
                Log.e(TAG, "Failed to convert NetworkPolicies to byte array " + ioe.getMessage());
                baos.reset();
                if (areAgentMetricsEnabled) {
                mBackupRestoreEventLogger.logItemsBackupFailed(
                    KEY_NETWORK_POLICIES,
                    policies.length,
                    ERROR_FAILED_TO_CONVERT_NETWORK_POLICIES);
            }
        }
        }
        return baos.toByteArray();
    }

    @VisibleForTesting
    byte[] getNewWifiConfigData() {
        byte[] data = mWifiManager.retrieveBackupData();
        if (areAgentMetricsEnabled) {
        // We're unable to determine how many settings this includes, so we'll just log 1.
        numberOfSettingsPerKey.put(KEY_WIFI_NEW_CONFIG, 1);
        }
        return data;
    }

    private byte[] getLocaleSettings() {
        if (!areAgentMetricsEnabled) {
            return mSettingsHelper.getLocaleData();
        }
        LocaleList localeList = mSettingsHelper.getLocaleList();
        numberOfSettingsPerKey.put(KEY_LOCALE, localeList.size());
        return localeList.toLanguageTags().getBytes();
@@ -1621,7 +1569,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        if (DEBUG_BACKUP) {
            Log.v(TAG, "Applying restored wifi data");
        }
        if (areAgentMetricsEnabled) {
        try {
            mWifiManager.restoreBackupData(bytes);
            mBackupRestoreEventLogger.logItemsRestored(KEY_WIFI_NEW_CONFIG, /* count= */ 1);
@@ -1629,9 +1576,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            mBackupRestoreEventLogger.logItemsRestoreFailed(
                KEY_WIFI_NEW_CONFIG, /* count= */ 1, ERROR_FAILED_TO_RESTORE_WIFI_CONFIG);
        }
        } else {
            mWifiManager.restoreBackupData(bytes);
        }
    }

    private void restoreNetworkPolicies(byte[] data) {
@@ -1642,12 +1586,10 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            try {
                int version = in.readInt();
                if (version < 1 || version > NETWORK_POLICIES_BACKUP_VERSION) {
                    if (areAgentMetricsEnabled) {
                    mBackupRestoreEventLogger.logItemsRestoreFailed(
                        KEY_NETWORK_POLICIES,
                        /* count= */ 1,
                        ERROR_UNKNOWN_BACKUP_SERIALIZATION_VERSION);
                    }
                    throw new BackupUtils.BadVersionException(
                            "Unknown Backup Serialization Version");
                }
@@ -1664,15 +1606,12 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                }
                // Only set the policies if there was no error in the restore operation
                networkPolicyManager.setNetworkPolicies(policies);
                if (areAgentMetricsEnabled) {
                mBackupRestoreEventLogger
                    .logItemsRestored(KEY_NETWORK_POLICIES, policies.length);
                }
            } catch (NullPointerException | IOException | BackupUtils.BadVersionException
                    | DateTimeException e) {
                // NPE can be thrown when trying to instantiate a NetworkPolicy
                Log.e(TAG, "Failed to convert byte array to NetworkPolicies " + e.getMessage());
                if (areAgentMetricsEnabled) {
                mBackupRestoreEventLogger.logItemsRestoreFailed(
                    KEY_NETWORK_POLICIES,
                    /* count= */ 1,
@@ -1680,7 +1619,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            }
        }
    }
    }

    @VisibleForTesting
    byte[] getDeviceSpecificConfiguration() throws IOException {
@@ -1757,11 +1695,9 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            simSpecificData = subManager.getAllSimSpecificSettingsForBackup();
            Log.i(TAG, "sim specific data of length + " + simSpecificData.length
                + " successfully retrieved");
            if (areAgentMetricsEnabled) {
            // We're unable to determine how many settings this includes, so we'll just log 1.
            numberOfSettingsPerKey.put(KEY_SIM_SPECIFIC_SETTINGS_2, 1);
        }
        }

        return simSpecificData;
    }
@@ -1772,7 +1708,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        boolean hasTelephony = packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
        if (hasTelephony) {
            SubscriptionManager subManager = SubscriptionManager.from(getBaseContext());
            if (areAgentMetricsEnabled) {
            try {
                subManager.restoreAllSimSpecificSettingsFromBackup(data);
                mBackupRestoreEventLogger
@@ -1784,9 +1719,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                        /* count= */ 1,
                        ERROR_FAILED_TO_RESTORE_SIM_SPECIFIC_SETTINGS);
            }
            } else {
                subManager.restoreAllSimSpecificSettingsFromBackup(data);
            }
        }
    }

@@ -1813,27 +1745,21 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                    });
            // cts requires B&R with 10 seconds
            if (latch.await(10, TimeUnit.SECONDS) && backupWifiData.value != null) {
                if (areAgentMetricsEnabled) {
                numberOfSettingsPerKey.put(KEY_WIFI_SETTINGS_BACKUP_DATA, 1);
                }
                return backupWifiData.value;
            }
        } catch (InterruptedException ie) {
            Log.e(TAG, "fail to retrieveWifiBackupData, " + ie);
            if (areAgentMetricsEnabled) {
            mBackupRestoreEventLogger.logItemsBackupFailed(
                KEY_WIFI_SETTINGS_BACKUP_DATA,
                /* count= */ 1,
                INTERRUPTED_EXCEPTION);
        }
        }
        Log.e(TAG, "fail to retrieveWifiBackupData");
        if (areAgentMetricsEnabled) {
        mBackupRestoreEventLogger.logItemsBackupFailed(
            KEY_WIFI_SETTINGS_BACKUP_DATA,
            /* count= */ 1,
            ERROR_FAILED_TO_RETRIEVE_WIFI_SETTINGS_BACKUP_DATA);
        }
        return new byte[0];
    }

@@ -1842,7 +1768,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        if (DEBUG_BACKUP) {
            Log.v(TAG, "Applying restored all wifi data");
        }
        if (areAgentMetricsEnabled) {
        try {
            mWifiManager.restoreWifiBackupData(data);
            mBackupRestoreEventLogger.logItemsRestored(
@@ -1853,9 +1778,6 @@ public class SettingsBackupAgent extends BackupAgentHelper {
                /* count= */ 1,
                ERROR_FAILED_TO_RESTORE_WIFI_SETTINGS_BACKUP_DATA);
        }
        } else {
            mWifiManager.restoreWifiBackupData(data);
        }
    }

    private void updateWindowManagerIfNeeded(Integer previousDensity) {
+5 −12
Original line number Diff line number Diff line
@@ -212,10 +212,7 @@ public class SettingsHelper {
        }

        // Get datatype for B&R metrics logging.
        String datatype = "";
        if (areAgentMetricsEnabled()) {
            datatype = SettingsBackupRestoreKeys.getKeyFromUri(destination);
        }
        String datatype = SettingsBackupRestoreKeys.getKeyFromUri(destination);

        sendBroadcast = sBroadcastOnRestore.contains(name);
        sendBroadcastSystemUI = sBroadcastOnRestoreSystemUI.contains(name);
@@ -299,7 +296,7 @@ public class SettingsHelper {
            contentValues.put(Settings.NameValueTable.NAME, name);
            contentValues.put(Settings.NameValueTable.VALUE, value);
            cr.insert(destination, contentValues);
            if (areAgentMetricsEnabled()) {
            if (mBackupRestoreEventLogger != null) {
                mBackupRestoreEventLogger.logItemsRestored(datatype, /* count= */ 1);
            }
        } catch (Exception e) {
@@ -308,7 +305,7 @@ public class SettingsHelper {
            sendBroadcastSystemUI = false;
            sendBroadcastAccessibility = false;
            Log.e(TAG, "Failed to restore setting name: " + name + " + value: " + value, e);
            if (areAgentMetricsEnabled()) {
            if (mBackupRestoreEventLogger != null) {
                mBackupRestoreEventLogger.logItemsRestoreFailed(
                    datatype, /* count= */ 1, ERROR_FAILED_TO_RESTORE_SETTING);
            }
@@ -792,12 +789,12 @@ public class SettingsHelper {

            am.updatePersistentConfigurationWithAttribution(config, mContext.getOpPackageName(),
                    mContext.getAttributionTag());
            if (areAgentMetricsEnabled()) {
            if (mBackupRestoreEventLogger != null) {
                mBackupRestoreEventLogger
                    .logItemsRestored(SettingsBackupRestoreKeys.KEY_LOCALE, localeList.size());
            }
        } catch (RemoteException e) {
            if (areAgentMetricsEnabled()) {
            if (mBackupRestoreEventLogger != null) {
                mBackupRestoreEventLogger
                    .logItemsRestoreFailed(
                        SettingsBackupRestoreKeys.KEY_LOCALE,
@@ -824,8 +821,4 @@ public class SettingsHelper {
    void setBackupRestoreEventLogger(BackupRestoreEventLogger backupRestoreEventLogger) {
        mBackupRestoreEventLogger = backupRestoreEventLogger;
    }

    private boolean areAgentMetricsEnabled() {
        return Flags.enableMetricsSettingsBackupAgents() && mBackupRestoreEventLogger != null;
    }
}
+32 −308

File changed.

Preview size limit exceeded, changes collapsed.

+4 −23
Original line number Diff line number Diff line
@@ -843,9 +843,8 @@ public class SettingsHelperTest {
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void
    restoreValue_metricsFlagIsEnabled_restoresSetting_secureUri_logsSuccessWithSecureDatatype()
    restoreValue_restoresSetting_secureUri_logsSuccessWithSecureDatatype()
    {
        mSettingsHelper.restoreValue(
                mContext,
@@ -863,9 +862,8 @@ public class SettingsHelperTest {
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void
    restoreValue_metricsFlagIsEnabled_restoresSetting_systemUri_logsSuccessWithSystemDatatype()
    restoreValue_restoresSetting_systemUri_logsSuccessWithSystemDatatype()
    {
        mSettingsHelper.restoreValue(
                mContext,
@@ -883,9 +881,8 @@ public class SettingsHelperTest {
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void
    restoreValue_metricsFlagIsEnabled_restoresSetting_globalUri_logsSuccessWithGlobalDatatype()
    restoreValue_restoresSetting_globalUri_logsSuccessWithGlobalDatatype()
    {
        mSettingsHelper.restoreValue(
                mContext,
@@ -903,8 +900,7 @@ public class SettingsHelperTest {
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreValue_metricsFlagIsEnabled_doesNotRestoreSetting_logsFailure() {
    public void restoreValue_doesNotRestoreSetting_logsFailure() {
        mSettingsHelper.restoreValue(
                mContext,
                mContentResolver,
@@ -920,21 +916,6 @@ public class SettingsHelperTest {
        assertThat(loggingResult.getFailCount()).isEqualTo(1);
    }

    @Test
    @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreValue_metricsFlagIsDisabled_doesNotLogMetrics() {
        mSettingsHelper.restoreValue(
                mContext,
                mContentResolver,
                new ContentValues(),
                Uri.EMPTY,
                SETTING_KEY,
                SETTING_VALUE,
                /* restoredFromSdkInt */ 0);

        assertThat(getLoggingResultForDatatype(SettingsBackupRestoreKeys.KEY_UNKNOWN)).isNull();
    }

    private int getAutoRotationSettingValue() {
        return Settings.System.getInt(mContentResolver,
                Settings.System.ACCELEROMETER_ROTATION,
+0 −8
Original line number Diff line number Diff line
@@ -79,14 +79,6 @@ flag {
    bug: "376598575"
}

flag {
    name: "enable_metrics_settings_backup_agents"
    namespace: "onboarding"
    description: "Enable SettingsBackupAgent to collect B&R agent metrics."
    bug: "379861078"
    is_fixed_read_only: true
}

flag {
    name: "enable_cross_platform_transfer"
    namespace: "onboarding"