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

Commit 45703502 authored by Guojing Yuan's avatar Guojing Yuan Committed by Android (Google) Code Review
Browse files

Merge "[CDM B/R] Add CompanionBackupHelper in SystemBackupAgent" into main

parents 309528d9 41160143
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -119,4 +119,8 @@ interface ICompanionDeviceManager {
    void setAssociationTag(int associationId, String tag);

    void clearAssociationTag(int associationId);

    byte[] getBackupPayload(int userId);

    void applyRestoredPayload(in byte[] payload, int userId);
}
+76 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.backup;

import android.annotation.UserIdInt;
import android.app.backup.BlobBackupHelper;
import android.companion.ICompanionDeviceManager;
import android.content.Context;
import android.os.ServiceManager;
import android.util.Slog;

/**
 * CDM backup and restore helper.
 */
public class CompanionBackupHelper extends BlobBackupHelper {

    private static final String TAG = "CompanionBackupHelper";

    // current schema of the backup state blob
    private static final int BLOB_VERSION = 1;

    // key under which the CDM data blob is committed to back up
    private static final String KEY_COMPANION = "companion";

    @UserIdInt
    private final int mUserId;

    public CompanionBackupHelper(int userId) {
        super(BLOB_VERSION, KEY_COMPANION);

        mUserId = userId;
    }

    @Override
    protected byte[] getBackupPayload(String key) {
        byte[] payload = null;
        if (KEY_COMPANION.equals(key)) {
            try {
                ICompanionDeviceManager cdm = ICompanionDeviceManager.Stub.asInterface(
                        ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE));
                payload = cdm.getBackupPayload(mUserId);
            } catch (Exception e) {
                Slog.e(TAG, "Error getting backup from CompanionDeviceManager.", e);
            }
        }
        return payload;
    }

    @Override
    protected void applyRestoredPayload(String key, byte[] payload) {
        Slog.i(TAG, "Got companion backup data.");
        if (KEY_COMPANION.equals(key)) {
            try {
                ICompanionDeviceManager cdm = ICompanionDeviceManager.Stub.asInterface(
                        ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE));
                cdm.applyRestoredPayload(payload, mUserId);
            } catch (Exception e) {
                Slog.e(TAG, "Error applying restored payload to CompanionDeviceManager.", e);
            }
        }
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -1025,6 +1025,17 @@ public class CompanionDeviceManagerService extends SystemService {
            setAssociationTag(associationId, null);
        }

        @Override
        public byte[] getBackupPayload(int userId) {
            // TODO(b/286124853): back up CDM data
            return new byte[0];
        }

        @Override
        public void applyRestoredPayload(byte[] payload, int userId) {
            // TODO(b/286124853): restore CDM data
        }

        @Override
        public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
                String[] args, ShellCallback callback, ResultReceiver resultReceiver)
+4 −1
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ public class SystemBackupAgent extends BackupAgentHelper {
    private static final String PEOPLE_HELPER = "people";
    private static final String APP_LOCALES_HELPER = "app_locales";
    private static final String APP_GENDER_HELPER = "app_gender";
    private static final String COMPANION_HELPER = "companion";

    // These paths must match what the WallpaperManagerService uses.  The leaf *_FILENAME
    // are also used in the full-backup file format, so must not change unless steps are
@@ -95,7 +96,8 @@ public class SystemBackupAgent extends BackupAgentHelper {
                    PERMISSION_HELPER,
                    NOTIFICATION_HELPER,
                    SYNC_SETTINGS_HELPER,
                    APP_LOCALES_HELPER);
                    APP_LOCALES_HELPER,
                    COMPANION_HELPER);

    /** Helpers that are enabled for full, non-system users. */
    private static final Set<String> sEligibleHelpersForNonSystemUser =
@@ -132,6 +134,7 @@ public class SystemBackupAgent extends BackupAgentHelper {
        addHelperIfEligibleForUser(APP_LOCALES_HELPER, new AppSpecificLocalesBackupHelper(mUserId));
        addHelperIfEligibleForUser(APP_GENDER_HELPER,
                new AppGrammaticalGenderBackupHelper(mUserId));
        addHelperIfEligibleForUser(COMPANION_HELPER, new CompanionBackupHelper(mUserId));
    }

    @Override
+8 −4
Original line number Diff line number Diff line
@@ -83,7 +83,8 @@ public class SystemBackupAgentTest {
                        "slices",
                        "people",
                        "app_locales",
                        "app_gender");
                        "app_gender",
                        "companion");
    }

    @Test
@@ -106,7 +107,8 @@ public class SystemBackupAgentTest {
                        "account_manager",
                        "people",
                        "app_locales",
                        "app_gender");
                        "app_gender",
                        "companion");
    }

    @Test
@@ -121,7 +123,8 @@ public class SystemBackupAgentTest {
                        "account_sync_settings",
                        "notifications",
                        "permissions",
                        "app_locales");
                        "app_locales",
                        "companion");
    }

    @Test
@@ -140,7 +143,8 @@ public class SystemBackupAgentTest {
                        "app_locales",
                        "account_manager",
                        "usage_stats",
                        "shortcut_manager");
                        "shortcut_manager",
                        "companion");
    }

    private class TestableSystemBackupAgent extends SystemBackupAgent {