blob: 0a30b763b8f4025ef8fdbf6b4a9eae4b06accadd [file] [log] [blame]
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001/*
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 Yamasani09e9cdc2013-11-06 14:54:50 -080017package com.android.server.backup;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070018
Amith Yamasani09e9cdc2013-11-06 14:54:50 -080019import android.app.IWallpaperManager;
Makoto Onuki2e210c42016-03-30 08:30:36 -070020import android.app.backup.BackupAgentHelper;
Christopher Tate45281862010-03-05 15:46:30 -080021import android.app.backup.BackupDataInput;
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +000022import android.app.backup.BackupHelper;
Christopher Tate4a627c72011-04-01 14:43:32 -070023import android.app.backup.FullBackup;
Christopher Tate2efd2db2011-07-19 16:32:49 -070024import android.app.backup.FullBackupDataOutput;
Christopher Tate3f64f8d2010-12-10 17:12:19 -080025import android.app.backup.WallpaperBackupHelper;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070026import android.content.Context;
Amith Yamasani61f57372012-08-31 12:12:28 -070027import android.os.Environment;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070028import android.os.ParcelFileDescriptor;
Amith Yamasani09e9cdc2013-11-06 14:54:50 -080029import android.os.RemoteException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070030import android.os.ServiceManager;
Amith Yamasani61f57372012-08-31 12:12:28 -070031import android.os.UserHandle;
Joe Onorato8a9b2202010-02-26 18:56:32 -080032import android.util.Slog;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070033
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +000034import com.google.android.collect.Sets;
35
Joe Onorato9bb8fd72009-07-28 18:24:51 -070036import java.io.File;
37import java.io.IOException;
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +000038import java.util.Set;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070039
40/**
Christopher Tate9f224432017-08-01 16:32:49 -070041 * 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 Onorato9bb8fd72009-07-28 18:24:51 -070043 */
Christopher Tatecc84c692010-03-29 14:54:02 -070044public class SystemBackupAgent extends BackupAgentHelper {
Joe Onorato9bb8fd72009-07-28 18:24:51 -070045 private static final String TAG = "SystemBackupAgent";
46
Christopher Tatee012a232015-04-01 17:18:50 -070047 // 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 Tatee012a232015-04-01 17:18:50 -070050 private static final String SYNC_SETTINGS_HELPER = "account_sync_settings";
51 private static final String PREFERRED_HELPER = "preferred_activities";
Christopher Tatef9767d62015-04-08 14:35:43 -070052 private static final String NOTIFICATION_HELPER = "notifications";
Christopher Tatee9fd1fa2015-09-15 16:43:07 -070053 private static final String PERMISSION_HELPER = "permissions";
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +000054 private static final String USAGE_STATS_HELPER = "usage_stats";
Makoto Onuki2e210c42016-03-30 08:30:36 -070055 private static final String SHORTCUT_MANAGER_HELPER = "shortcut_manager";
Svet Ganov5d09c992016-09-07 09:57:41 -070056 private static final String ACCOUNT_MANAGER_HELPER = "account_manager";
Jason Monkb715b042018-02-01 15:00:05 -050057 private static final String SLICES_HELPER = "slices";
Trung Lamd7cf8f22020-01-09 17:27:19 -080058 private static final String PEOPLE_HELPER = "people";
Christopher Tatee012a232015-04-01 17:18:50 -070059
Christopher Tate75a99702011-05-18 16:28:19 -070060 // 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 Yamasani37ce3a82012-02-06 12:04:42 -080066 // TODO: Will need to change if backing up non-primary user's wallpaper
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070067 // TODO: http://b/22388012
Amith Yamasani61f57372012-08-31 12:12:28 -070068 private static final String WALLPAPER_IMAGE_DIR =
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070069 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath();
Christopher Tate9f224432017-08-01 16:32:49 -070070 public static final String WALLPAPER_IMAGE =
71 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
72 "wallpaper").getAbsolutePath();
Christopher Tate75a99702011-05-18 16:28:19 -070073
Amith Yamasani37ce3a82012-02-06 12:04:42 -080074 // TODO: Will need to change if backing up non-primary user's wallpaper
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070075 // TODO: http://b/22388012
Amith Yamasani61f57372012-08-31 12:12:28 -070076 private static final String WALLPAPER_INFO_DIR =
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070077 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath();
Christopher Tate9f224432017-08-01 16:32:49 -070078 public static final String WALLPAPER_INFO =
79 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
80 "wallpaper_info.xml").getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080081 // 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 Onorato9bb8fd72009-07-28 18:24:51 -070083
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +000084 private static final Set<String> sEligibleForMultiUser = Sets.newArraySet(
Ruslan Tkhakokhov19513032019-02-05 11:06:21 +000085 PERMISSION_HELPER, NOTIFICATION_HELPER, SYNC_SETTINGS_HELPER);
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +000086
87 private int mUserId = UserHandle.USER_SYSTEM;
Christopher Tate41297ff2016-03-10 16:46:15 -080088
Joe Onorato9bb8fd72009-07-28 18:24:51 -070089 @Override
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +000090 public void onCreate(UserHandle user) {
91 super.onCreate(user);
92
93 mUserId = user.getIdentifier();
94
Ruslan Tkhakokhov19513032019-02-05 11:06:21 +000095 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this, mUserId));
Christopher Tatef7cb8a02015-04-20 16:09:48 -070096 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper());
Annie Meng8b646fd2019-02-01 18:46:42 +000097 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(mUserId));
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +000098 addHelper(PERMISSION_HELPER, new PermissionBackupHelper(mUserId));
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +000099 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
Makoto Onuki2e210c42016-03-30 08:30:36 -0700100 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper());
Svet Ganov5d09c992016-09-07 09:57:41 -0700101 addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper());
Jason Monkb715b042018-02-01 15:00:05 -0500102 addHelper(SLICES_HELPER, new SliceBackupHelper(this));
Trung Lamd7cf8f22020-01-09 17:27:19 -0800103 addHelper(PEOPLE_HELPER, new PeopleBackupHelper(mUserId));
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700104 }
105
Christopher Tate2efd2db2011-07-19 16:32:49 -0700106 @Override
107 public void onFullBackup(FullBackupDataOutput data) throws IOException {
Christopher Tated7faf532016-02-25 12:43:38 -0800108 // At present we don't back up anything
Christopher Tate4a627c72011-04-01 14:43:32 -0700109 }
110
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700111 @Override
112 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
113 throws IOException {
Christopher Tated7faf532016-02-25 12:43:38 -0800114 // Slot in a restore helper for the older wallpaper backup schema to support restore
115 // from devices still generating data in that format.
Andrii Kuliane57f2dc2020-01-26 20:59:07 -0800116 //TODO(b/147732386): Add multi-display support for wallpaper backup.
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +0000117 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this,
118 new String[] { WALLPAPER_IMAGE_KEY}));
Christopher Tate41297ff2016-03-10 16:46:15 -0800119
Christopher Tated7faf532016-02-25 12:43:38 -0800120 // On restore, we also support a long-ago wallpaper data schema "system_files"
Christopher Tatee012a232015-04-01 17:18:50 -0700121 addHelper("system_files", new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800122 new String[] { WALLPAPER_IMAGE_KEY} ));
Christopher Tate41297ff2016-03-10 16:46:15 -0800123
Christopher Tate9f224432017-08-01 16:32:49 -0700124 super.onRestore(data, appVersionCode, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700125 }
Christopher Tate75a99702011-05-18 16:28:19 -0700126
Ruslan Tkhakokhov1583c8b2019-01-30 09:19:13 +0000127 @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 Tate9f224432017-08-01 16:32:49 -0700136 /**
137 * Support for 'adb restore' of legacy archives
138 */
Christopher Tate75a99702011-05-18 16:28:19 -0700139 @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 Tate79ec80d2011-06-24 14:58:49 -0700164 FullBackup.restoreFile(data, size, type, mode, mtime, outFile);
Christopher Tate75a99702011-05-18 16:28:19 -0700165
166 if (restoredWallpaper) {
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800167 IWallpaperManager wallpaper =
168 (IWallpaperManager)ServiceManager.getService(
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000169 Context.WALLPAPER_SERVICE);
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800170 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 Tate75a99702011-05-18 16:28:19 -0700177 }
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 Onorato9bb8fd72009-07-28 18:24:51 -0700186}