SystemBackupAgent.java revision c9e584b4228512173224900219cb71b3f7e21151
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.backup; 18 19import android.app.IWallpaperManager; 20import android.app.backup.BackupAgentHelper; 21import android.app.backup.BackupDataInput; 22import android.app.backup.BackupDataOutput; 23import android.app.backup.FullBackup; 24import android.app.backup.FullBackupDataOutput; 25import android.app.backup.WallpaperBackupHelper; 26import android.content.Context; 27import android.os.Environment; 28import android.os.ParcelFileDescriptor; 29import android.os.RemoteException; 30import android.os.ServiceManager; 31import android.os.UserHandle; 32import android.util.Slog; 33 34import java.io.File; 35import java.io.IOException; 36 37/** 38 * Backup agent for various system-managed data, currently just the system wallpaper 39 */ 40public class SystemBackupAgent extends BackupAgentHelper { 41 private static final String TAG = "SystemBackupAgent"; 42 43 // Names of the helper tags within the dataset. Changing one of these names will 44 // break the ability to restore from datasets that predate the change. 45 private static final String WALLPAPER_HELPER = "wallpaper"; 46 private static final String SYNC_SETTINGS_HELPER = "account_sync_settings"; 47 private static final String PREFERRED_HELPER = "preferred_activities"; 48 private static final String NOTIFICATION_HELPER = "notifications"; 49 private static final String PERMISSION_HELPER = "permissions"; 50 private static final String USAGE_STATS_HELPER = "usage_stats"; 51 private static final String SHORTCUT_MANAGER_HELPER = "shortcut_manager"; 52 53 // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME 54 // are also used in the full-backup file format, so must not change unless steps are 55 // taken to support the legacy backed-up datasets. 56 private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper"; 57 private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml"; 58 59 // TODO: Will need to change if backing up non-primary user's wallpaper 60 // TODO: http://b/22388012 61 private static final String WALLPAPER_IMAGE_DIR = 62 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); 63 private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE; 64 65 // TODO: Will need to change if backing up non-primary user's wallpaper 66 // TODO: http://b/22388012 67 private static final String WALLPAPER_INFO_DIR = 68 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); 69 private static final String WALLPAPER_INFO = WallpaperBackupHelper.WALLPAPER_INFO; 70 // Use old keys to keep legacy data compatibility and avoid writing two wallpapers 71 private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY; 72 private static final String WALLPAPER_INFO_KEY = WallpaperBackupHelper.WALLPAPER_INFO_KEY; 73 74 private WallpaperBackupHelper mWallpaperHelper = null; 75 76 @Override 77 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 78 ParcelFileDescriptor newState) throws IOException { 79 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); 80 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); 81 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); 82 addHelper(PERMISSION_HELPER, new PermissionBackupHelper()); 83 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); 84 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); 85 super.onBackup(oldState, data, newState); 86 } 87 88 @Override 89 public void onFullBackup(FullBackupDataOutput data) throws IOException { 90 // At present we don't back up anything 91 } 92 93 @Override 94 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) 95 throws IOException { 96 // Slot in a restore helper for the older wallpaper backup schema to support restore 97 // from devices still generating data in that format. 98 mWallpaperHelper = new WallpaperBackupHelper(this, 99 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }, 100 new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} ); 101 addHelper(WALLPAPER_HELPER, mWallpaperHelper); 102 103 // On restore, we also support a long-ago wallpaper data schema "system_files" 104 addHelper("system_files", new WallpaperBackupHelper(this, 105 new String[] { WALLPAPER_IMAGE }, 106 new String[] { WALLPAPER_IMAGE_KEY} )); 107 108 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); 109 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); 110 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); 111 addHelper(PERMISSION_HELPER, new PermissionBackupHelper()); 112 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); 113 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); 114 115 try { 116 super.onRestore(data, appVersionCode, newState); 117 118 IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService( 119 Context.WALLPAPER_SERVICE); 120 if (wallpaper != null) { 121 try { 122 wallpaper.settingsRestored(); 123 } catch (RemoteException re) { 124 Slog.e(TAG, "Couldn't restore settings\n" + re); 125 } 126 } 127 } catch (IOException ex) { 128 // If there was a failure, delete everything for the wallpaper, this is too aggressive, 129 // but this is hopefully a rare failure. 130 Slog.d(TAG, "restore failed", ex); 131 (new File(WALLPAPER_IMAGE)).delete(); 132 (new File(WALLPAPER_INFO)).delete(); 133 } 134 } 135 136 @Override 137 public void onRestoreFile(ParcelFileDescriptor data, long size, 138 int type, String domain, String path, long mode, long mtime) 139 throws IOException { 140 Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path); 141 142 // Bits to indicate postprocessing we may need to perform 143 boolean restoredWallpaper = false; 144 145 File outFile = null; 146 // Various domain+files we understand a priori 147 if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) { 148 if (path.equals(WALLPAPER_INFO_FILENAME)) { 149 outFile = new File(WALLPAPER_INFO); 150 restoredWallpaper = true; 151 } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) { 152 outFile = new File(WALLPAPER_IMAGE); 153 restoredWallpaper = true; 154 } 155 } 156 157 try { 158 if (outFile == null) { 159 Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]"); 160 } 161 FullBackup.restoreFile(data, size, type, mode, mtime, outFile); 162 163 if (restoredWallpaper) { 164 IWallpaperManager wallpaper = 165 (IWallpaperManager)ServiceManager.getService( 166 Context.WALLPAPER_SERVICE); 167 if (wallpaper != null) { 168 try { 169 wallpaper.settingsRestored(); 170 } catch (RemoteException re) { 171 Slog.e(TAG, "Couldn't restore settings\n" + re); 172 } 173 } 174 } 175 } catch (IOException e) { 176 if (restoredWallpaper) { 177 // Make sure we wind up in a good state 178 (new File(WALLPAPER_IMAGE)).delete(); 179 (new File(WALLPAPER_INFO)).delete(); 180 } 181 } 182 } 183 184 @Override 185 public void onRestoreFinished() { 186 // helper will be null following 'adb restore' or other full-data operation 187 if (mWallpaperHelper != null) { 188 mWallpaperHelper.onRestoreFinished(); 189 } 190 } 191} 192