Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 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 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 17 | package com.android.server.backup; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 18 | |
Wale Ogunwale | 18795a2 | 2014-12-03 11:38:33 -0800 | [diff] [blame] | 19 | import android.app.ActivityManagerNative; |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 20 | import android.app.IWallpaperManager; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 21 | import android.app.backup.BackupDataInput; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 22 | import android.app.backup.BackupDataOutput; |
Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 23 | import android.app.backup.BackupAgentHelper; |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 24 | import android.app.backup.FullBackup; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 25 | import android.app.backup.FullBackupDataOutput; |
Christopher Tate | ac6a3a5 | 2014-11-18 11:57:25 -0800 | [diff] [blame] | 26 | import android.app.backup.RecentsBackupHelper; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 27 | import android.app.backup.WallpaperBackupHelper; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 29 | import android.os.Environment; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 30 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 32 | import android.os.ServiceManager; |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 33 | import android.os.UserHandle; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 34 | import android.util.Slog; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 35 | |
| 36 | import java.io.File; |
| 37 | import java.io.IOException; |
| 38 | |
| 39 | /** |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 40 | * Backup agent for various system-managed data, currently just the system wallpaper |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 41 | */ |
Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 42 | public class SystemBackupAgent extends BackupAgentHelper { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 43 | private static final String TAG = "SystemBackupAgent"; |
| 44 | |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 45 | // Names of the helper tags within the dataset. Changing one of these names will |
| 46 | // break the ability to restore from datasets that predate the change. |
| 47 | private static final String WALLPAPER_HELPER = "wallpaper"; |
| 48 | private static final String RECENTS_HELPER = "recents"; |
| 49 | private static final String SYNC_SETTINGS_HELPER = "account_sync_settings"; |
| 50 | private static final String PREFERRED_HELPER = "preferred_activities"; |
| 51 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 52 | // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME |
| 53 | // are also used in the full-backup file format, so must not change unless steps are |
| 54 | // taken to support the legacy backed-up datasets. |
| 55 | private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper"; |
| 56 | private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml"; |
| 57 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 58 | // TODO: Will need to change if backing up non-primary user's wallpaper |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 59 | private static final String WALLPAPER_IMAGE_DIR = |
| 60 | Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 61 | private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE; |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 62 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 63 | // TODO: Will need to change if backing up non-primary user's wallpaper |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 64 | private static final String WALLPAPER_INFO_DIR = |
| 65 | Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 66 | private static final String WALLPAPER_INFO = WallpaperBackupHelper.WALLPAPER_INFO; |
| 67 | // Use old keys to keep legacy data compatibility and avoid writing two wallpapers |
| 68 | private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY; |
| 69 | private static final String WALLPAPER_INFO_KEY = WallpaperBackupHelper.WALLPAPER_INFO_KEY; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 70 | |
| 71 | @Override |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 72 | public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 73 | ParcelFileDescriptor newState) throws IOException { |
| 74 | // We only back up the data under the current "wallpaper" schema with metadata |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 75 | IWallpaperManager wallpaper = (IWallpaperManager)ServiceManager.getService( |
Dan Egnor | 541fa51 | 2009-11-11 17:00:06 -0800 | [diff] [blame] | 76 | Context.WALLPAPER_SERVICE); |
| 77 | String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 78 | String[] keys = new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY }; |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 79 | if (wallpaper != null) { |
| 80 | try { |
| 81 | final String wallpaperName = wallpaper.getName(); |
| 82 | if (wallpaperName != null && wallpaperName.length() > 0) { |
| 83 | // When the wallpaper has a name, back up the info by itself. |
| 84 | // TODO: Don't rely on the innards of the service object like this! |
| 85 | // TODO: Send a delete for any stored wallpaper image in this case? |
| 86 | files = new String[] { WALLPAPER_INFO }; |
| 87 | keys = new String[] { WALLPAPER_INFO_KEY }; |
| 88 | } |
| 89 | } catch (RemoteException re) { |
| 90 | Slog.e(TAG, "Couldn't get wallpaper name\n" + re); |
| 91 | } |
Dan Egnor | 541fa51 | 2009-11-11 17:00:06 -0800 | [diff] [blame] | 92 | } |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 93 | addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, files, keys)); |
| 94 | addHelper(RECENTS_HELPER, new RecentsBackupHelper(this)); |
| 95 | addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); |
| 96 | addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper(this)); |
Christopher Tate | ac6a3a5 | 2014-11-18 11:57:25 -0800 | [diff] [blame] | 97 | |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 98 | super.onBackup(oldState, data, newState); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public void onFullBackup(FullBackupDataOutput data) throws IOException { |
| 103 | // At present we back up only the wallpaper |
| 104 | fullWallpaperBackup(data); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 107 | private void fullWallpaperBackup(FullBackupDataOutput output) { |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 108 | // Back up the data files directly. We do them in this specific order -- |
| 109 | // info file followed by image -- because then we need take no special |
| 110 | // steps during restore; the restore will happen properly when the individual |
| 111 | // files are restored piecemeal. |
| 112 | FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null, |
Christopher Tate | 11ae768 | 2015-03-24 18:48:10 -0700 | [diff] [blame] | 113 | WALLPAPER_INFO_DIR, WALLPAPER_INFO, output); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 114 | FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null, |
Christopher Tate | 11ae768 | 2015-03-24 18:48:10 -0700 | [diff] [blame] | 115 | WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 118 | @Override |
| 119 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 120 | throws IOException { |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 121 | // On restore, we also support a previous data schema "system_files" |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 122 | addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 123 | new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }, |
| 124 | new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} )); |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 125 | addHelper("system_files", new WallpaperBackupHelper(this, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 126 | new String[] { WALLPAPER_IMAGE }, |
| 127 | new String[] { WALLPAPER_IMAGE_KEY} )); |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 128 | addHelper(RECENTS_HELPER, new RecentsBackupHelper(this)); |
| 129 | addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); |
| 130 | addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper(this)); |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 131 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 132 | try { |
| 133 | super.onRestore(data, appVersionCode, newState); |
| 134 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 135 | IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService( |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 136 | Context.WALLPAPER_SERVICE); |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 137 | if (wallpaper != null) { |
| 138 | try { |
| 139 | wallpaper.settingsRestored(); |
| 140 | } catch (RemoteException re) { |
| 141 | Slog.e(TAG, "Couldn't restore settings\n" + re); |
| 142 | } |
| 143 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 144 | } catch (IOException ex) { |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 145 | // If there was a failure, delete everything for the wallpaper, this is too aggressive, |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 146 | // but this is hopefully a rare failure. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 147 | Slog.d(TAG, "restore failed", ex); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 148 | (new File(WALLPAPER_IMAGE)).delete(); |
| 149 | (new File(WALLPAPER_INFO)).delete(); |
| 150 | } |
| 151 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 152 | |
| 153 | @Override |
| 154 | public void onRestoreFile(ParcelFileDescriptor data, long size, |
| 155 | int type, String domain, String path, long mode, long mtime) |
| 156 | throws IOException { |
| 157 | Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path); |
| 158 | |
| 159 | // Bits to indicate postprocessing we may need to perform |
| 160 | boolean restoredWallpaper = false; |
| 161 | |
| 162 | File outFile = null; |
| 163 | // Various domain+files we understand a priori |
| 164 | if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) { |
| 165 | if (path.equals(WALLPAPER_INFO_FILENAME)) { |
| 166 | outFile = new File(WALLPAPER_INFO); |
| 167 | restoredWallpaper = true; |
| 168 | } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) { |
| 169 | outFile = new File(WALLPAPER_IMAGE); |
| 170 | restoredWallpaper = true; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | try { |
| 175 | if (outFile == null) { |
| 176 | Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]"); |
| 177 | } |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 178 | FullBackup.restoreFile(data, size, type, mode, mtime, outFile); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 179 | |
| 180 | if (restoredWallpaper) { |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 181 | IWallpaperManager wallpaper = |
| 182 | (IWallpaperManager)ServiceManager.getService( |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 183 | Context.WALLPAPER_SERVICE); |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 184 | if (wallpaper != null) { |
| 185 | try { |
| 186 | wallpaper.settingsRestored(); |
| 187 | } catch (RemoteException re) { |
| 188 | Slog.e(TAG, "Couldn't restore settings\n" + re); |
| 189 | } |
| 190 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 191 | } |
| 192 | } catch (IOException e) { |
| 193 | if (restoredWallpaper) { |
| 194 | // Make sure we wind up in a good state |
| 195 | (new File(WALLPAPER_IMAGE)).delete(); |
| 196 | (new File(WALLPAPER_INFO)).delete(); |
| 197 | } |
| 198 | } |
| 199 | } |
Wale Ogunwale | 18795a2 | 2014-12-03 11:38:33 -0800 | [diff] [blame] | 200 | |
| 201 | @Override |
| 202 | public void onRestoreFinished() { |
| 203 | try { |
| 204 | ActivityManagerNative.getDefault().systemBackupRestored(); |
| 205 | } catch (RemoteException e) { |
| 206 | // Not possible since this code is running in the system process. |
| 207 | } |
| 208 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 209 | } |