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

Commit b5f11693 authored by James Lemieux's avatar James Lemieux
Browse files

Add backup/restore support

We use the new fullContentBackup feature introduced in M. The
wrinkle that exists in our case is that the preference file
could be either:

com.android.deskclock_preferences.xml
or
com.google.android.deskclock_preferences.xml

depending on whether DeskClockGoogle overlayed DeskClock or
came as part of the device image.

So, a BackupAgent is also configured and used to intercept the
writing of the preference file on the restore device. The
preference filename is then altered to include the package name
of the receiving application, guaranteeing that the preferences
(which store world clocks, timers and stopwatch data) survive
intact.

Bug: 21818176
Change-Id: Ie4652ef8375b8db4ab6e2ae3dc40820dae618ddf
parent 1c6f6b04
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -35,6 +35,10 @@

    <application android:label="@string/app_label"
                 android:name=".DeskClockApplication"
                 android:allowBackup="true"
                 android:backupAgent="DeskClockBackupAgent"
                 android:fullBackupContent="@xml/backup_scheme"
                 android:fullBackupOnly="true"
                 android:icon="@mipmap/ic_launcher_alarmclock"
                 android:requiredForAllUsers="true"
                 android:supportsRtl="true">
@@ -47,8 +51,7 @@
                android:label="@string/app_label"
                android:theme="@style/DeskClock"
                android:icon="@mipmap/ic_launcher_alarmclock"
                android:launchMode="singleTask"
                >
                android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
+20 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 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.
-->

<full-backup-content>
    <include domain="database" path="alarms.db" />
    <include domain="sharedpref" path="com.android.deskclock_preferences.xml" />
</full-backup-content>
 No newline at end of file
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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.deskclock;

import android.app.backup.BackupAgent;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.os.ParcelFileDescriptor;
import android.support.annotation.NonNull;

import com.android.deskclock.alarms.AlarmStateManager;

import java.io.File;
import java.io.IOException;

public class DeskClockBackupAgent extends BackupAgent {

    @Override
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState) throws IOException { }

    @Override
    public void onRestore(BackupDataInput data, int appVersionCode,
            ParcelFileDescriptor newState) throws IOException { }

    @Override
    public void onRestoreFile(@NonNull ParcelFileDescriptor data, long size, File destination,
            int type, long mode, long mtime) throws IOException {
        // The preference file on the backup device may not be the same on the restore device.
        // Massage the file name here before writing it.
        if (destination.getName().endsWith("_preferences.xml")) {
            final String prefFileName = getPackageName() + "_preferences.xml";
            destination = new File(destination.getParentFile(), prefFileName);
        }

        super.onRestoreFile(data, size, destination, type, mode, mtime);
    }

    @Override
    public void onRestoreFinished() {
        // Now that alarms have been restored, schedule them in AlarmManager.
        AlarmStateManager.fixAlarmInstances(this);
    }
}
 No newline at end of file