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.BackupDataInput; 21import android.app.backup.BackupDataOutput; 22import android.app.backup.BackupAgentHelper; 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 50 // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME 51 // are also used in the full-backup file format, so must not change unless steps are 52 // taken to support the legacy backed-up datasets. 53 private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper"; 54 private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml"; 55 56 // TODO: Will need to change if backing up non-primary user's wallpaper 57 private static final String WALLPAPER_IMAGE_DIR = 58 Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath(); 59 private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE; 60 61 // TODO: Will need to change if backing up non-primary user's wallpaper 62 private static final String WALLPAPER_INFO_DIR = 63 Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath(); 64 private static final String WALLPAPER_INFO = WallpaperBackupHelper.WALLPAPER_INFO; 65 // Use old keys to keep legacy data compatibility and avoid writing two wallpapers 66 private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY; 67 private static final String WALLPAPER_INFO_KEY = WallpaperBackupHelper.WALLPAPER_INFO_KEY; 68 69 @Override 70 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 71 ParcelFileDescriptor newState) throws IOException { 72 // We only back up the data under the current "wallpaper" schema with metadata 73 IWallpaperManager wallpaper = (IWallpaperManager)ServiceManager.getService( 74 Context.WALLPAPER_SERVICE); 75 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }; 76 String[] keys = new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY }; 77 if (wallpaper != null) { 78 try { 79 final String wallpaperName = wallpaper.getName(); 80 if (wallpaperName != null && wallpaperName.length() > 0) { 81 // When the wallpaper has a name, back up the info by itself. 82 // TODO: Don't rely on the innards of the service object like this! 83 // TODO: Send a delete for any stored wallpaper image in this case? 84 files = new String[] { WALLPAPER_INFO }; 85 keys = new String[] { WALLPAPER_INFO_KEY }; 86 } 87 } catch (RemoteException re) { 88 Slog.e(TAG, "Couldn't get wallpaper name\n" + re); 89 } 90 } 91 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, files, keys)); 92 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); 93 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); 94 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); 95 96 super.onBackup(oldState, data, newState); 97 } 98 99 @Override 100 public void onFullBackup(FullBackupDataOutput data) throws IOException { 101 // At present we back up only the wallpaper 102 fullWallpaperBackup(data); 103 } 104 105 private void fullWallpaperBackup(FullBackupDataOutput output) { 106 // Back up the data files directly. We do them in this specific order -- 107 // info file followed by image -- because then we need take no special 108 // steps during restore; the restore will happen properly when the individual 109 // files are restored piecemeal. 110 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null, 111 WALLPAPER_INFO_DIR, WALLPAPER_INFO, output); 112 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null, 113 WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output); 114 } 115 116 @Override 117 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) 118 throws IOException { 119 // On restore, we also support a previous data schema "system_files" 120 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, 121 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }, 122 new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} )); 123 addHelper("system_files", new WallpaperBackupHelper(this, 124 new String[] { WALLPAPER_IMAGE }, 125 new String[] { WALLPAPER_IMAGE_KEY} )); 126 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); 127 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); 128 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); 129 130 try { 131 super.onRestore(data, appVersionCode, newState); 132 133 IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService( 134 Context.WALLPAPER_SERVICE); 135 if (wallpaper != null) { 136 try { 137 wallpaper.settingsRestored(); 138 } catch (RemoteException re) { 139 Slog.e(TAG, "Couldn't restore settings\n" + re); 140 } 141 } 142 } catch (IOException ex) { 143 // If there was a failure, delete everything for the wallpaper, this is too aggressive, 144 // but this is hopefully a rare failure. 145 Slog.d(TAG, "restore failed", ex); 146 (new File(WALLPAPER_IMAGE)).delete(); 147 (new File(WALLPAPER_INFO)).delete(); 148 } 149 } 150 151 @Override 152 public void onRestoreFile(ParcelFileDescriptor data, long size, 153 int type, String domain, String path, long mode, long mtime) 154 throws IOException { 155 Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path); 156 157 // Bits to indicate postprocessing we may need to perform 158 boolean restoredWallpaper = false; 159 160 File outFile = null; 161 // Various domain+files we understand a priori 162 if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) { 163 if (path.equals(WALLPAPER_INFO_FILENAME)) { 164 outFile = new File(WALLPAPER_INFO); 165 restoredWallpaper = true; 166 } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) { 167 outFile = new File(WALLPAPER_IMAGE); 168 restoredWallpaper = true; 169 } 170 } 171 172 try { 173 if (outFile == null) { 174 Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]"); 175 } 176 FullBackup.restoreFile(data, size, type, mode, mtime, outFile); 177 178 if (restoredWallpaper) { 179 IWallpaperManager wallpaper = 180 (IWallpaperManager)ServiceManager.getService( 181 Context.WALLPAPER_SERVICE); 182 if (wallpaper != null) { 183 try { 184 wallpaper.settingsRestored(); 185 } catch (RemoteException re) { 186 Slog.e(TAG, "Couldn't restore settings\n" + re); 187 } 188 } 189 } 190 } catch (IOException e) { 191 if (restoredWallpaper) { 192 // Make sure we wind up in a good state 193 (new File(WALLPAPER_IMAGE)).delete(); 194 (new File(WALLPAPER_INFO)).delete(); 195 } 196 } 197 } 198} 199