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

Commit 111bd4ac authored by Christopher Tate's avatar Christopher Tate
Browse files

Start backing up wallpaper

This CL does the following:
   + adds an AbsoluteFileBackupHelper class for managing backup of files
     known by absolute path, not based off of the app's getFilesDir() root
   + bumps up the collection interval from its testing-only default of 1 second
     to 3 minutes
   + adds a SystemBackupAgent class to the main system package and names it as
     the android:backupAgent for the main OS package.  Right now this agent
     only backs up & restores the wallpaper file.
   + amend the Wallpaper Service to inform the Backup Manager when the wallpaper
     changes.

On the subject of the 3-minute collection interval before the backup actually
occurs:  this can be short-circuited from an adb shell.  Running the command
'bmgr run' will cause the Backup Manager to kick off any pending backup
operations immediately.
parent 69e1f472
Loading
Loading
Loading
Loading
+66 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 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 android.backup;

import android.content.Context;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.File;
import java.io.FileDescriptor;

/**
 * Like FileBackupHelper, but takes absolute paths for the files instead of
 * subpaths of getFilesDir()
 *
 * @hide
 */
public class AbsoluteFileBackupHelper extends FileBackupHelperBase implements BackupHelper {
    private static final String TAG = "AbsoluteFileBackupHelper";

    Context mContext;
    String[] mFiles;

    public AbsoluteFileBackupHelper(Context context, String... files) {
        super(context);

        mContext = context;
        mFiles = files;
    }

    /**
     * Based on oldState, determine which of the files from the application's data directory
     * need to be backed up, write them to the data stream, and fill in newState with the
     * state as it exists now.
     */
    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState) {
        // use the file paths as the keys, too
        performBackup_checked(oldState, data, newState, mFiles, mFiles);
    }

    public void restoreEntity(BackupDataInputStream data) {
        // TODO: turn this off before ship
        Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size());
        String key = data.getKey();
        if (isKeyInList(key, mFiles)) {
            File f = new File(key);
            writeFile(f, data);
        }
    }
}
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 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.internal.backup;

import android.backup.AbsoluteFileBackupHelper;
import android.backup.BackupHelperAgent;

/**
 * Backup agent for various system-managed data
 */
public class SystemBackupAgent extends BackupHelperAgent {
    // the set of files that we back up whole, as absolute paths
    String[] mFiles = {
            /* WallpaperService.WALLPAPER_FILE */
            "/data/data/com.android.settings/files/wallpaper",
            };

    public void onCreate() {
        addHelper("system_files", new AbsoluteFileBackupHelper(this, mFiles));
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -1000,6 +1000,7 @@
                 android:hasCode="false"
                 android:label="@string/android_system_label"
                 android:allowClearUserData="false"
                 android:backupAgent="com.android.internal.backup.SystemBackupAgent"
                 android:icon="@drawable/ic_launcher_android">
        <activity android:name="com.android.internal.app.ChooserActivity"
                android:theme="@style/Theme.Dialog.Alert"
+3 −3
Original line number Diff line number Diff line
@@ -76,8 +76,7 @@ class BackupManagerService extends IBackupManager.Stub {
    private static final boolean DEBUG = true;

    // Default time to wait after data changes before we back up the data
    private static final long COLLECTION_INTERVAL = 1000;
    //private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;
    private static final long COLLECTION_INTERVAL = 3 * 60 * 1000;

    private static final int MSG_RUN_BACKUP = 1;
    private static final int MSG_RUN_FULL_BACKUP = 2;
@@ -360,7 +359,8 @@ class BackupManagerService extends IBackupManager.Stub {
        if (DEBUG) {
            Log.v(TAG, "Adding " + targetPkgs.size() + " backup participants:");
            for (PackageInfo p : targetPkgs) {
                Log.v(TAG, "    " + p + " agent=" + p.applicationInfo.backupAgentName);
                Log.v(TAG, "    " + p + " agent=" + p.applicationInfo.backupAgentName
                        + " uid=" + p.applicationInfo.uid);
            }
        }

+12 −1
Original line number Diff line number Diff line
@@ -18,8 +18,10 @@ package com.android.server;

import static android.os.FileObserver.*;
import static android.os.ParcelFileDescriptor.*;

import android.app.IWallpaperService;
import android.app.IWallpaperServiceCallback;
import android.backup.BackupManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -154,7 +156,16 @@ class WallpaperService extends IWallpaperService.Stub {
    public ParcelFileDescriptor setWallpaper() {
        checkPermission(android.Manifest.permission.SET_WALLPAPER);
        try {
            return ParcelFileDescriptor.open(WALLPAPER_FILE, MODE_CREATE|MODE_READ_WRITE);
            ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
                    MODE_CREATE|MODE_READ_WRITE);

            // changing the wallpaper means we'll need to back up the new one
            long origId = Binder.clearCallingIdentity();
            BackupManager bm = new BackupManager(mContext);
            bm.dataChanged();
            Binder.restoreCallingIdentity(origId);

            return fd;
        } catch (FileNotFoundException e) {
            if (Config.LOGD) Log.d(TAG, "Error setting wallpaper", e);
        }