SystemBackupAgent.java revision 2e210c4d0f766e52ea4c087a1d54213c36a4e0ea
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 // We only back up the data under the current "wallpaper" schema with metadata 80 IWallpaperManager wallpaper = (IWallpaperManager)ServiceManager.getService( 81 Context.WALLPAPER_SERVICE); 82 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }; 83 String[] keys = new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY }; 84 if (wallpaper != null) { 85 try { 86 final String wallpaperName = wallpaper.getName(); 87 if (wallpaperName != null && wallpaperName.length() > 0) { 88 // When the wallpaper has a name, back up the info by itself. 89 // TODO: Don't rely on the innards of the service object like this! 90 // TODO: Send a delete for any stored wallpaper image in this case? 91 files = new String[] { WALLPAPER_INFO }; 92 keys = new String[] { WALLPAPER_INFO_KEY }; 93 } 94 } catch (RemoteException re) { 95 Slog.e(TAG, "Couldn't get wallpaper name\n" + re); 96 } 97 } 98 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, files, keys)); 99 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); 100 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); 101 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); 102 addHelper(PERMISSION_HELPER, new PermissionBackupHelper()); 103 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); 104 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); 105 super.onBackup(oldState, data, newState); 106 } 107 108 @Override 109 public void onFullBackup(FullBackupDataOutput data) throws IOException { 110 // At present we back up only the wallpaper 111 fullWallpaperBackup(data); 112 } 113 114 private void fullWallpaperBackup(FullBackupDataOutput output) { 115 // Back up the data files directly. We do them in this specific order -- 116 // info file followed by image -- because then we need take no special 117 // steps during restore; the restore will happen properly when the individual 118 // files are restored piecemeal. 119 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null, 120 WALLPAPER_INFO_DIR, WALLPAPER_INFO, output); 121 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null, 122 WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output); 123 } 124 125 @Override 126 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) 127 throws IOException { 128 mWallpaperHelper = new WallpaperBackupHelper(this, 129 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }, 130 new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} ); 131 addHelper(WALLPAPER_HELPER, mWallpaperHelper); 132 133 // On restore, we also support a previous data schema "system_files" 134 addHelper("system_files", new WallpaperBackupHelper(this, 135 new String[] { WALLPAPER_IMAGE }, 136 new String[] { WALLPAPER_IMAGE_KEY} )); 137 138 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); 139 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); 140 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); 141 addHelper(PERMISSION_HELPER, new PermissionBackupHelper()); 142 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); 143 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); 144 145 try { 146 super.onRestore(data, appVersionCode, newState); 147 148 IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService( 149 Context.WALLPAPER_SERVICE); 150 if (wallpaper != null) { 151 try { 152 wallpaper.settingsRestored(); 153 } catch (RemoteException re) { 154 Slog.e(TAG, "Couldn't restore settings\n" + re); 155 } 156 } 157 } catch (IOException ex) { 158 // If there was a failure, delete everything for the wallpaper, this is too aggressive, 159 // but this is hopefully a rare failure. 160 Slog.d(TAG, "restore failed", ex); 161 (new File(WALLPAPER_IMAGE)).delete(); 162 (new File(WALLPAPER_INFO)).delete(); 163 } 164 } 165 166 @Override 167 public void onRestoreFile(ParcelFileDescriptor data, long size, 168 int type, String domain, String path, long mode, long mtime) 169 throws IOException { 170 Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path); 171 172 // Bits to indicate postprocessing we may need to perform 173 boolean restoredWallpaper = false; 174 175 File outFile = null; 176 // Various domain+files we understand a priori 177 if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) { 178 if (path.equals(WALLPAPER_INFO_FILENAME)) { 179 outFile = new File(WALLPAPER_INFO); 180 restoredWallpaper = true; 181 } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) { 182 outFile = new File(WALLPAPER_IMAGE); 183 restoredWallpaper = true; 184 } 185 } 186 187 try { 188 if (outFile == null) { 189 Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]"); 190 } 191 FullBackup.restoreFile(data, size, type, mode, mtime, outFile); 192 193 if (restoredWallpaper) { 194 IWallpaperManager wallpaper = 195 (IWallpaperManager)ServiceManager.getService( 196 Context.WALLPAPER_SERVICE); 197 if (wallpaper != null) { 198 try { 199 wallpaper.settingsRestored(); 200 } catch (RemoteException re) { 201 Slog.e(TAG, "Couldn't restore settings\n" + re); 202 } 203 } 204 } 205 } catch (IOException e) { 206 if (restoredWallpaper) { 207 // Make sure we wind up in a good state 208 (new File(WALLPAPER_IMAGE)).delete(); 209 (new File(WALLPAPER_INFO)).delete(); 210 } 211 } 212 } 213 214 @Override 215 public void onRestoreFinished() { 216 mWallpaperHelper.onRestoreFinished(); 217 } 218} 219