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

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

Add Backup & Restore Metrics for SoftAP settings.

Bug: 379861078
Change-Id: Iedf391e20c755f8dfc62c40b421e3b08777b4d56
Flag: com.android.server.backup.enable_metrics_settings_backup_agents
Tested: atest SettingsBackupAgentTest
parent c6146094
Loading
Loading
Loading
Loading
+25 −4
Original line number Diff line number Diff line
@@ -210,6 +210,8 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        "skipped_due_to_large_screen";
    private static final String ERROR_DID_NOT_PASS_VALIDATION = "did_not_pass_validation";
    private static final String ERROR_IO_EXCEPTION = "io_exception";
    private static final String ERROR_FAILED_TO_RESTORE_SOFTAP_CONFIG =
        "failed_to_restore_softap_config";


    // Name of the temporary file we use during full backup/restore.  This is
@@ -1338,12 +1340,31 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        mWifiManager.restoreSupplicantBackupData(supplicant_bytes, ipconfig_bytes);
    }

    private byte[] getSoftAPConfiguration() {
        return mWifiManager.retrieveSoftApBackupData();
    @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;
    }

    private void restoreSoftApConfiguration(byte[] data) {
        SoftApConfiguration configInCloud = mWifiManager.restoreSoftApBackupData(data);
    @VisibleForTesting
    void restoreSoftApConfiguration(byte[] data) {
        SoftApConfiguration configInCloud;
        if (areAgentMetricsEnabled) {
            try {
                configInCloud = mWifiManager.restoreSoftApBackupData(data);
                mBackupRestoreEventLogger.logItemsRestored(KEY_SOFTAP_CONFIG, /* count= */ 1);
            } catch (Exception e) {
                configInCloud = null;
                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
+95 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.providers.settings;

import static com.android.providers.settings.SettingsBackupRestoreKeys.KEY_SOFTAP_CONFIG;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
@@ -28,6 +30,7 @@ import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import android.annotation.Nullable;
import android.app.backup.BackupAnnotations.BackupDestination;
import android.app.backup.BackupAnnotations.OperationType;
import android.app.backup.BackupDataInput;
@@ -42,6 +45,8 @@ import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.UserHandle;
@@ -126,6 +131,7 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {

    @Mock private BackupDataInput mBackupDataInput;
    @Mock private BackupDataOutput mBackupDataOutput;
    @Mock private static WifiManager mWifiManager;

    private TestFriendlySettingsBackupAgent mAgentUnderTest;
    private Context mContext;
@@ -754,6 +760,80 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
        assertNull(getLoggingResultForDatatype(TEST_KEY, mAgentUnderTest));
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void getSoftAPConfiguration_flagIsEnabled_numberOfSettingsInKeyAreRecorded() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.BACKUP);
        when(mWifiManager.retrieveSoftApBackupData()).thenReturn(null);

        mAgentUnderTest.getSoftAPConfiguration();

        assertEquals(mAgentUnderTest.getNumberOfSettingsPerKey(KEY_SOFTAP_CONFIG), 1);
    }

    @Test
    @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void getSoftAPConfiguration_flagIsNotEnabled_numberOfSettingsInKeyAreNotRecorded() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.BACKUP);
        when(mWifiManager.retrieveSoftApBackupData()).thenReturn(null);

        mAgentUnderTest.getSoftAPConfiguration();

        assertEquals(mAgentUnderTest.getNumberOfSettingsPerKey(KEY_SOFTAP_CONFIG), 0);
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void
        restoreSoftApConfiguration_flagIsEnabled_restoreIsSuccessful_successMetricsAreLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SoftApConfiguration config = new SoftApConfiguration.Builder().setSsid("test").build();
        byte[] data = config.toString().getBytes();
        when(mWifiManager.restoreSoftApBackupData(any())).thenReturn(null);

        mAgentUnderTest.restoreSoftApConfiguration(data);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(KEY_SOFTAP_CONFIG, mAgentUnderTest);
        assertNotNull(loggingResult);
        assertEquals(loggingResult.getSuccessCount(), 1);
    }

    @Test
    @EnableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void
        restoreSoftApConfiguration_flagIsEnabled_restoreIsNotSuccessful_failureMetricsAreLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SoftApConfiguration config = new SoftApConfiguration.Builder().setSsid("test").build();
        byte[] data = config.toString().getBytes();
        when(mWifiManager.restoreSoftApBackupData(any())).thenThrow(new RuntimeException());

        mAgentUnderTest.restoreSoftApConfiguration(data);

        DataTypeResult loggingResult =
            getLoggingResultForDatatype(KEY_SOFTAP_CONFIG, mAgentUnderTest);
        assertNotNull(loggingResult);
        assertEquals(loggingResult.getFailCount(), 1);
    }

    @Test
    @DisableFlags(com.android.server.backup.Flags.FLAG_ENABLE_METRICS_SETTINGS_BACKUP_AGENTS)
    public void restoreSoftApConfiguration_flagIsNotEnabled_metricsAreNotLogged() {
        mAgentUnderTest.onCreate(
            UserHandle.SYSTEM, BackupDestination.CLOUD, OperationType.RESTORE);
        SoftApConfiguration config = new SoftApConfiguration.Builder().setSsid("test").build();
        byte[] data = config.toString().getBytes();
        when(mWifiManager.restoreSoftApBackupData(any())).thenReturn(null);

        mAgentUnderTest.restoreSoftApConfiguration(data);

        assertNull(getLoggingResultForDatatype(KEY_SOFTAP_CONFIG, mAgentUnderTest));
    }

    private byte[] generateBackupData(Map<String, String> keyValueData) {
        int totalBytes = 0;
        for (String key : keyValueData.keySet()) {
@@ -890,6 +970,13 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
                this.numberOfSettingsPerKey.put(key, numberOfSettings);
            }
        }

        int getNumberOfSettingsPerKey(String key) {
            if (numberOfSettingsPerKey == null || !numberOfSettingsPerKey.containsKey(key)) {
                return 0;
            }
            return numberOfSettingsPerKey.get(key);
        }
    }

    /** The TestSettingsHelper tracks which values have been backed up and/or restored. */
@@ -944,6 +1031,14 @@ public class SettingsBackupAgentTest extends BaseSettingsProviderTest {
        public ContentResolver getContentResolver() {
            return mContentResolver;
        }

        @Override
        public Object getSystemService(String name) {
            if (name.equals(Context.WIFI_SERVICE)) {
                return mWifiManager;
            }
            return super.getSystemService(name);
        }
    }

    /** ContentProvider which returns a set of known test values. */