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"; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 52 | private static final String ACCOUNT_MANAGER_HELPER = "account_manager"; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 53 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 54 | // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME |
| 55 | // are also used in the full-backup file format, so must not change unless steps are |
| 56 | // taken to support the legacy backed-up datasets. |
| 57 | private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper"; |
| 58 | private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml"; |
| 59 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 60 | // 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] | 61 | // TODO: http://b/22388012 |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 62 | private static final String WALLPAPER_IMAGE_DIR = |
Xiaohui Chen | 4cfe29b | 2015-09-04 13:55:23 -0700 | [diff] [blame] | 63 | Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 64 | private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE; |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 65 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 66 | // 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] | 67 | // TODO: http://b/22388012 |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 68 | private static final String WALLPAPER_INFO_DIR = |
Xiaohui Chen | 4cfe29b | 2015-09-04 13:55:23 -0700 | [diff] [blame] | 69 | Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 70 | private static final String WALLPAPER_INFO = WallpaperBackupHelper.WALLPAPER_INFO; |
| 71 | // Use old keys to keep legacy data compatibility and avoid writing two wallpapers |
| 72 | private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY; |
| 73 | private static final String WALLPAPER_INFO_KEY = WallpaperBackupHelper.WALLPAPER_INFO_KEY; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 74 | |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 75 | private WallpaperBackupHelper mWallpaperHelper = null; |
| 76 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 77 | @Override |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 78 | public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 79 | ParcelFileDescriptor newState) throws IOException { |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 80 | addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 81 | addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 82 | addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); |
Christopher Tate | e9fd1fa | 2015-09-15 16:43:07 -0700 | [diff] [blame] | 83 | addHelper(PERMISSION_HELPER, new PermissionBackupHelper()); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 84 | addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 85 | addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 86 | addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper()); |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 87 | super.onBackup(oldState, data, newState); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public void onFullBackup(FullBackupDataOutput data) throws IOException { |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 92 | // At present we don't back up anything |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 97 | throws IOException { |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 98 | // Slot in a restore helper for the older wallpaper backup schema to support restore |
| 99 | // from devices still generating data in that format. |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 100 | mWallpaperHelper = new WallpaperBackupHelper(this, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 101 | new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }, |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 102 | new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} ); |
| 103 | addHelper(WALLPAPER_HELPER, mWallpaperHelper); |
| 104 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 105 | // On restore, we also support a long-ago wallpaper data schema "system_files" |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 106 | addHelper("system_files", new WallpaperBackupHelper(this, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 107 | new String[] { WALLPAPER_IMAGE }, |
| 108 | new String[] { WALLPAPER_IMAGE_KEY} )); |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 109 | |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 110 | addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this)); |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 111 | addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); |
| 112 | addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this)); |
Christopher Tate | e9fd1fa | 2015-09-15 16:43:07 -0700 | [diff] [blame] | 113 | addHelper(PERMISSION_HELPER, new PermissionBackupHelper()); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 114 | addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 115 | addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 116 | addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper()); |
Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 117 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 118 | try { |
| 119 | super.onRestore(data, appVersionCode, newState); |
| 120 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 121 | IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService( |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 122 | Context.WALLPAPER_SERVICE); |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 123 | if (wallpaper != null) { |
| 124 | try { |
| 125 | wallpaper.settingsRestored(); |
| 126 | } catch (RemoteException re) { |
| 127 | Slog.e(TAG, "Couldn't restore settings\n" + re); |
| 128 | } |
| 129 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 130 | } catch (IOException ex) { |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 131 | // 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] | 132 | // but this is hopefully a rare failure. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 133 | Slog.d(TAG, "restore failed", ex); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 134 | (new File(WALLPAPER_IMAGE)).delete(); |
| 135 | (new File(WALLPAPER_INFO)).delete(); |
| 136 | } |
| 137 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 138 | |
| 139 | @Override |
| 140 | public void onRestoreFile(ParcelFileDescriptor data, long size, |
| 141 | int type, String domain, String path, long mode, long mtime) |
| 142 | throws IOException { |
| 143 | Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path); |
| 144 | |
| 145 | // Bits to indicate postprocessing we may need to perform |
| 146 | boolean restoredWallpaper = false; |
| 147 | |
| 148 | File outFile = null; |
| 149 | // Various domain+files we understand a priori |
| 150 | if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) { |
| 151 | if (path.equals(WALLPAPER_INFO_FILENAME)) { |
| 152 | outFile = new File(WALLPAPER_INFO); |
| 153 | restoredWallpaper = true; |
| 154 | } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) { |
| 155 | outFile = new File(WALLPAPER_IMAGE); |
| 156 | restoredWallpaper = true; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | try { |
| 161 | if (outFile == null) { |
| 162 | Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]"); |
| 163 | } |
Christopher Tate | 79ec80d | 2011-06-24 14:58:49 -0700 | [diff] [blame] | 164 | FullBackup.restoreFile(data, size, type, mode, mtime, outFile); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 165 | |
| 166 | if (restoredWallpaper) { |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 167 | IWallpaperManager wallpaper = |
| 168 | (IWallpaperManager)ServiceManager.getService( |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 169 | Context.WALLPAPER_SERVICE); |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 170 | if (wallpaper != null) { |
| 171 | try { |
| 172 | wallpaper.settingsRestored(); |
| 173 | } catch (RemoteException re) { |
| 174 | Slog.e(TAG, "Couldn't restore settings\n" + re); |
| 175 | } |
| 176 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 177 | } |
| 178 | } catch (IOException e) { |
| 179 | if (restoredWallpaper) { |
| 180 | // Make sure we wind up in a good state |
| 181 | (new File(WALLPAPER_IMAGE)).delete(); |
| 182 | (new File(WALLPAPER_INFO)).delete(); |
| 183 | } |
| 184 | } |
| 185 | } |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 186 | |
| 187 | @Override |
| 188 | public void onRestoreFinished() { |
Christopher Tate | c9e584b | 2016-04-12 12:36:47 -0700 | [diff] [blame] | 189 | // helper will be null following 'adb restore' or other full-data operation |
| 190 | if (mWallpaperHelper != null) { |
| 191 | mWallpaperHelper.onRestoreFinished(); |
| 192 | } |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 193 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 194 | } |