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