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 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 19 | import android.app.IWallpaperManager; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 20 | import android.app.backup.BackupAgentHelper; |
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 | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 23 | import android.app.backup.FullBackup; |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 24 | import android.app.backup.FullBackupDataOutput; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 25 | import android.app.backup.WallpaperBackupHelper; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 27 | import android.os.Environment; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 28 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 29 | import android.os.RemoteException; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 30 | import android.os.ServiceManager; |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 31 | import android.os.UserHandle; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 32 | import android.util.Slog; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 33 | |
| 34 | import java.io.File; |
| 35 | import java.io.IOException; |
| 36 | |
| 37 | /** |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 38 | * Backup agent for various system-managed data, currently just the system wallpaper |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 39 | */ |
Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 40 | public class SystemBackupAgent extends BackupAgentHelper { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 41 | private static final String TAG = "SystemBackupAgent"; |
| 42 | |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 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"; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 46 | private static final String SYNC_SETTINGS_HELPER = "account_sync_settings"; |
| 47 | private static final String PREFERRED_HELPER = "preferred_activities"; |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 48 | private static final String NOTIFICATION_HELPER = "notifications"; |
Christopher Tate | e9fd1fa | 2015-09-15 16:43:07 -0700 | [diff] [blame] | 49 | private static final String PERMISSION_HELPER = "permissions"; |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 50 | private static final String USAGE_STATS_HELPER = "usage_stats"; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 51 | private static final String SHORTCUT_MANAGER_HELPER = "shortcut_manager"; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 52 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 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 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 59 | // TODO: Will need to change if backing up non-primary user's wallpaper |
Xiaohui Chen | 4cfe29b | 2015-09-04 13:55:23 -0700 | [diff] [blame] | 60 | // TODO: http://b/22388012 |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 61 | private static final String WALLPAPER_IMAGE_DIR = |
Xiaohui Chen | 4cfe29b | 2015-09-04 13:55:23 -0700 | [diff] [blame] | 62 | Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 63 | private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE; |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 64 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 65 | // TODO: Will need to change if backing up non-primary user's wallpaper |
Xiaohui Chen | 4cfe29b | 2015-09-04 13:55:23 -0700 | [diff] [blame] | 66 | // TODO: http://b/22388012 |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 67 | private static final String WALLPAPER_INFO_DIR = |
Xiaohui Chen | 4cfe29b | 2015-09-04 13:55:23 -0700 | [diff] [blame] | 68 | Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 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; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 73 | |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 74 | private WallpaperBackupHelper mWallpaperHelper = null; |
| 75 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 76 | @Override |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 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 |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 80 | IWallpaperManager wallpaper = (IWallpaperManager)ServiceManager.getService( |
Dan Egnor | 541fa51 | 2009-11-11 17:00:06 -0800 | [diff] [blame] | 81 | Context.WALLPAPER_SERVICE); |
| 82 | String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 83 | String[] keys = new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY }; |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 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 | } |
Dan Egnor | 541fa51 | 2009-11-11 17:00:06 -0800 | [diff] [blame] | 97 | } |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 98 | addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, files, keys)); |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 99 | addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 100 | addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 101 | addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); |
Christopher Tate | e9fd1fa | 2015-09-15 16:43:07 -0700 | [diff] [blame] | 102 | addHelper(PERMISSION_HELPER, new PermissionBackupHelper()); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 103 | addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 104 | addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 105 | super.onBackup(oldState, data, newState); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 108 | @Override |
| 109 | public void onFullBackup(FullBackupDataOutput data) throws IOException { |
| 110 | // At present we back up only the wallpaper |
| 111 | fullWallpaperBackup(data); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 114 | private void fullWallpaperBackup(FullBackupDataOutput output) { |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 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, |
Christopher Tate | 11ae768 | 2015-03-24 18:48:10 -0700 | [diff] [blame] | 120 | WALLPAPER_INFO_DIR, WALLPAPER_INFO, output); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 121 | FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null, |
Christopher Tate | 11ae768 | 2015-03-24 18:48:10 -0700 | [diff] [blame] | 122 | WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 125 | @Override |
| 126 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 127 | throws IOException { |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 128 | mWallpaperHelper = new WallpaperBackupHelper(this, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 129 | new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }, |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 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" |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 134 | addHelper("system_files", new WallpaperBackupHelper(this, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 135 | new String[] { WALLPAPER_IMAGE }, |
| 136 | new String[] { WALLPAPER_IMAGE_KEY} )); |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 137 | |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 138 | addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 139 | addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); |
| 140 | addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); |
Christopher Tate | e9fd1fa | 2015-09-15 16:43:07 -0700 | [diff] [blame] | 141 | addHelper(PERMISSION_HELPER, new PermissionBackupHelper()); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 142 | addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 143 | addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 144 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 145 | try { |
| 146 | super.onRestore(data, appVersionCode, newState); |
| 147 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 148 | IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService( |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 149 | Context.WALLPAPER_SERVICE); |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 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 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 157 | } catch (IOException ex) { |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 158 | // 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] | 159 | // but this is hopefully a rare failure. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 160 | Slog.d(TAG, "restore failed", ex); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 161 | (new File(WALLPAPER_IMAGE)).delete(); |
| 162 | (new File(WALLPAPER_INFO)).delete(); |
| 163 | } |
| 164 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 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 | } |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 191 | FullBackup.restoreFile(data, size, type, mode, mtime, outFile); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 192 | |
| 193 | if (restoredWallpaper) { |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 194 | IWallpaperManager wallpaper = |
| 195 | (IWallpaperManager)ServiceManager.getService( |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 196 | Context.WALLPAPER_SERVICE); |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 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 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 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 | } |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 213 | |
| 214 | @Override |
| 215 | public void onRestoreFinished() { |
| 216 | mWallpaperHelper.onRestoreFinished(); |
| 217 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 218 | } |