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

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

Add Backup & Restore Metrics for Network Policies settings.

This is a rollforward with fix of ag/31176152. The previous CL was causing crashes because I had forgotten to guard the new logic behind the flag. 

Bug: 379861078 
Change-Id: I37882ddef459e49087e6dd44c7fef8f91ef27238
Flag: com.android.server.backup.enable_metrics_settings_backup_agents
Tested: Tested manually, as there's no testing infra for these methods and I couldn't find a way to come up with one.
parent fb409567
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -216,6 +216,10 @@ public class SettingsBackupAgent extends BackupAgentHelper {
        "failed_to_restore_wifi_config";
    private static final String ERROR_FAILED_TO_RESTORE_SIM_SPECIFIC_SETTINGS =
        "failed_to_restore_sim_specific_settings";
    private static final String ERROR_FAILED_TO_CONVERT_NETWORK_POLICIES =
        "failed_to_convert_network_policies";
    private static final String ERROR_UNKNOWN_BACKUP_SERIALIZATION_VERSION =
        "unknown_backup_serialization_version";


    // Name of the temporary file we use during full backup/restore.  This is
@@ -1438,6 +1442,7 @@ public class SettingsBackupAgent extends BackupAgentHelper {
            try {
                out.writeInt(NETWORK_POLICIES_BACKUP_VERSION);
                out.writeInt(policies.length);
                int numberOfPoliciesBackedUp = 0;
                for (NetworkPolicy policy : policies) {
                    // We purposefully only backup policies that the user has
                    // defined; any inferred policies might include
@@ -1447,13 +1452,25 @@ 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();
@@ -1504,6 +1521,12 @@ 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");
                }
@@ -1520,10 +1543,20 @@ 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,
                        ERROR_FAILED_TO_CONVERT_NETWORK_POLICIES);
                }
            }
        }
    }