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

Commit 4af29434 authored by Jacky Wang's avatar Jacky Wang
Browse files

[DataStore] Migrate BatteryBackupHelper to BackupRestoreStorage

Bug: 325144964
Test: Manual tests
Change-Id: I689b6bfa22ebea92ed1f4a8f7e0aaaab6221917f
parent a3a23cb7
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settingslib.datastore

import android.app.Application
import android.app.backup.BackupAgentHelper
import android.app.backup.BackupManager
import android.content.Context
import android.util.Log
@@ -42,6 +43,35 @@ class BackupRestoreStorageManager private constructor(private val application: A
        BackupManager.dataChanged(application.packageName)
    }

    /**
     * Adds all the registered [BackupRestoreStorage] as the helpers of given [BackupAgentHelper].
     *
     * @see BackupAgentHelper.addHelper
     */
    fun addBackupAgentHelpers(backupAgentHelper: BackupAgentHelper) {
        for ((keyPrefix, storage) in storages) {
            backupAgentHelper.addHelper(keyPrefix, storage)
        }
    }

    /**
     * Callback when restore finished.
     *
     * The observers of the storages will be notified.
     */
    fun onRestoreFinished() {
        for (storage in storages.values) {
            storage.notifyRestoreFinished()
        }
    }

    private fun BackupRestoreStorage.notifyRestoreFinished() {
        when (this) {
            is KeyedObservable<*> -> notifyChange(ChangeReason.RESTORE)
            is Observable -> notifyChange(ChangeReason.RESTORE)
        }
    }

    /**
     * Adds a list of storages.
     *
@@ -78,6 +108,11 @@ class BackupRestoreStorageManager private constructor(private val application: A
        }
    }

    /** Removes all the storages. */
    fun removeAll() {
        for ((name, _) in storages) remove(name)
    }

    /** Removes storage with given name. */
    fun remove(name: String): BackupRestoreStorage? {
        val storage = storages.remove(name)
+25 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.settingslib.datastore

/**
 * A [BackupRestoreStorage] that implements [Observable].
 *
 * This class provides the [Observable] implementations on top of [DataObservable] by delegation.
 */
abstract class ObservableBackupRestoreStorage :
    BackupRestoreStorage(), Observable by DataObservable()