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; |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 22 | import android.app.backup.BackupHelper; |
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 | |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 34 | import com.google.android.collect.Sets; |
| 35 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 36 | import java.io.File; |
| 37 | import java.io.IOException; |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 38 | import java.util.Set; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Christopher Tate | 9f22443 | 2017-08-01 16:32:49 -0700 | [diff] [blame] | 41 | * Backup agent for various system-managed data. Wallpapers are now handled by a |
| 42 | * separate package, but we still process restores from legacy datasets here. |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 43 | */ |
Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 44 | public class SystemBackupAgent extends BackupAgentHelper { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 45 | private static final String TAG = "SystemBackupAgent"; |
| 46 | |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 47 | // Names of the helper tags within the dataset. Changing one of these names will |
| 48 | // break the ability to restore from datasets that predate the change. |
| 49 | private static final String WALLPAPER_HELPER = "wallpaper"; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 50 | private static final String SYNC_SETTINGS_HELPER = "account_sync_settings"; |
| 51 | private static final String PREFERRED_HELPER = "preferred_activities"; |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 52 | private static final String NOTIFICATION_HELPER = "notifications"; |
Christopher Tate | e9fd1fa | 2015-09-15 16:43:07 -0700 | [diff] [blame] | 53 | private static final String PERMISSION_HELPER = "permissions"; |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 54 | private static final String USAGE_STATS_HELPER = "usage_stats"; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 55 | private static final String SHORTCUT_MANAGER_HELPER = "shortcut_manager"; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 56 | private static final String ACCOUNT_MANAGER_HELPER = "account_manager"; |
Jason Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 57 | private static final String SLICES_HELPER = "slices"; |
Trung Lam | d7cf8f2 | 2020-01-09 17:27:19 -0800 | [diff] [blame] | 58 | private static final String PEOPLE_HELPER = "people"; |
Christopher Tate | e012a23 | 2015-04-01 17:18:50 -0700 | [diff] [blame] | 59 | |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 60 | // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME |
| 61 | // are also used in the full-backup file format, so must not change unless steps are |
| 62 | // taken to support the legacy backed-up datasets. |
| 63 | private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper"; |
| 64 | private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml"; |
| 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_IMAGE_DIR = |
Xiaohui Chen | 4cfe29b | 2015-09-04 13:55:23 -0700 | [diff] [blame] | 69 | Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); |
Christopher Tate | 9f22443 | 2017-08-01 16:32:49 -0700 | [diff] [blame] | 70 | public static final String WALLPAPER_IMAGE = |
| 71 | new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), |
| 72 | "wallpaper").getAbsolutePath(); |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 73 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 74 | // 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] | 75 | // TODO: http://b/22388012 |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 76 | private static final String WALLPAPER_INFO_DIR = |
Xiaohui Chen | 4cfe29b | 2015-09-04 13:55:23 -0700 | [diff] [blame] | 77 | Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath(); |
Christopher Tate | 9f22443 | 2017-08-01 16:32:49 -0700 | [diff] [blame] | 78 | public static final String WALLPAPER_INFO = |
| 79 | new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), |
| 80 | "wallpaper_info.xml").getAbsolutePath(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 81 | // Use old keys to keep legacy data compatibility and avoid writing two wallpapers |
| 82 | private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 83 | |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 84 | private static final Set<String> sEligibleForMultiUser = Sets.newArraySet( |
Ruslan Tkhakokhov | 1951303 | 2019-02-05 11:06:21 +0000 | [diff] [blame] | 85 | PERMISSION_HELPER, NOTIFICATION_HELPER, SYNC_SETTINGS_HELPER); |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 86 | |
| 87 | private int mUserId = UserHandle.USER_SYSTEM; |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 88 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 89 | @Override |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 90 | public void onCreate(UserHandle user) { |
| 91 | super.onCreate(user); |
| 92 | |
| 93 | mUserId = user.getIdentifier(); |
| 94 | |
Ruslan Tkhakokhov | 1951303 | 2019-02-05 11:06:21 +0000 | [diff] [blame] | 95 | addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this, mUserId)); |
Christopher Tate | f7cb8a0 | 2015-04-20 16:09:48 -0700 | [diff] [blame] | 96 | addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper()); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 97 | addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(mUserId)); |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 98 | addHelper(PERMISSION_HELPER, new PermissionBackupHelper(mUserId)); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 99 | addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this)); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 100 | addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper()); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 101 | addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper()); |
Jason Monk | b715b04 | 2018-02-01 15:00:05 -0500 | [diff] [blame] | 102 | addHelper(SLICES_HELPER, new SliceBackupHelper(this)); |
Trung Lam | d7cf8f2 | 2020-01-09 17:27:19 -0800 | [diff] [blame] | 103 | addHelper(PEOPLE_HELPER, new PeopleBackupHelper(mUserId)); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Christopher Tate | 2efd2db | 2011-07-19 16:32:49 -0700 | [diff] [blame] | 106 | @Override |
| 107 | public void onFullBackup(FullBackupDataOutput data) throws IOException { |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 108 | // At present we don't back up anything |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 111 | @Override |
| 112 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 113 | throws IOException { |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 114 | // Slot in a restore helper for the older wallpaper backup schema to support restore |
| 115 | // from devices still generating data in that format. |
Andrii Kulian | e57f2dc | 2020-01-26 20:59:07 -0800 | [diff] [blame] | 116 | //TODO(b/147732386): Add multi-display support for wallpaper backup. |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 117 | addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, |
| 118 | new String[] { WALLPAPER_IMAGE_KEY})); |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 119 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 120 | // 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] | 121 | addHelper("system_files", new WallpaperBackupHelper(this, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 122 | new String[] { WALLPAPER_IMAGE_KEY} )); |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 123 | |
Christopher Tate | 9f22443 | 2017-08-01 16:32:49 -0700 | [diff] [blame] | 124 | super.onRestore(data, appVersionCode, newState); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 125 | } |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 126 | |
Ruslan Tkhakokhov | 1583c8b | 2019-01-30 09:19:13 +0000 | [diff] [blame] | 127 | @Override |
| 128 | public void addHelper(String keyPrefix, BackupHelper helper) { |
| 129 | if (mUserId != UserHandle.USER_SYSTEM && !sEligibleForMultiUser.contains(keyPrefix)) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | super.addHelper(keyPrefix, helper); |
| 134 | } |
| 135 | |
Christopher Tate | 9f22443 | 2017-08-01 16:32:49 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Support for 'adb restore' of legacy archives |
| 138 | */ |
Christopher Tate | 75a9970 | 2011-05-18 16:28:19 -0700 | [diff] [blame] | 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 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 186 | } |