blob: 2d12fcda87c14002d412354edae5d6d6542c4f2c [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;
Christopher Tate45281862010-03-05 15:46:30 -080022import android.app.backup.BackupDataOutput;
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
34import java.io.File;
35import java.io.IOException;
36
37/**
Christopher Tate7c2bb662009-09-20 19:47:46 -070038 * Backup agent for various system-managed data, currently just the system wallpaper
Joe Onorato9bb8fd72009-07-28 18:24:51 -070039 */
Christopher Tatecc84c692010-03-29 14:54:02 -070040public class SystemBackupAgent extends BackupAgentHelper {
Joe Onorato9bb8fd72009-07-28 18:24:51 -070041 private static final String TAG = "SystemBackupAgent";
42
Christopher Tatee012a232015-04-01 17:18:50 -070043 // 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 Tatee012a232015-04-01 17:18:50 -070046 private static final String SYNC_SETTINGS_HELPER = "account_sync_settings";
47 private static final String PREFERRED_HELPER = "preferred_activities";
Christopher Tatef9767d62015-04-08 14:35:43 -070048 private static final String NOTIFICATION_HELPER = "notifications";
Christopher Tatee9fd1fa2015-09-15 16:43:07 -070049 private static final String PERMISSION_HELPER = "permissions";
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +000050 private static final String USAGE_STATS_HELPER = "usage_stats";
Makoto Onuki2e210c42016-03-30 08:30:36 -070051 private static final String SHORTCUT_MANAGER_HELPER = "shortcut_manager";
Christopher Tatee012a232015-04-01 17:18:50 -070052
Christopher Tate75a99702011-05-18 16:28:19 -070053 // 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 Yamasani37ce3a82012-02-06 12:04:42 -080059 // TODO: Will need to change if backing up non-primary user's wallpaper
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070060 // TODO: http://b/22388012
Amith Yamasani61f57372012-08-31 12:12:28 -070061 private static final String WALLPAPER_IMAGE_DIR =
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070062 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080063 private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE;
Christopher Tate75a99702011-05-18 16:28:19 -070064
Amith Yamasani37ce3a82012-02-06 12:04:42 -080065 // TODO: Will need to change if backing up non-primary user's wallpaper
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070066 // TODO: http://b/22388012
Amith Yamasani61f57372012-08-31 12:12:28 -070067 private static final String WALLPAPER_INFO_DIR =
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070068 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080069 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 Onorato9bb8fd72009-07-28 18:24:51 -070073
Christopher Tate41297ff2016-03-10 16:46:15 -080074 private WallpaperBackupHelper mWallpaperHelper = null;
75
Joe Onorato9bb8fd72009-07-28 18:24:51 -070076 @Override
Christopher Tate7c2bb662009-09-20 19:47:46 -070077 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 Yamasani09e9cdc2013-11-06 14:54:50 -080080 IWallpaperManager wallpaper = (IWallpaperManager)ServiceManager.getService(
Dan Egnor541fa512009-11-11 17:00:06 -080081 Context.WALLPAPER_SERVICE);
82 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO };
Amith Yamasani37ce3a82012-02-06 12:04:42 -080083 String[] keys = new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY };
Amith Yamasani09e9cdc2013-11-06 14:54:50 -080084 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 Egnor541fa512009-11-11 17:00:06 -080097 }
Christopher Tatee012a232015-04-01 17:18:50 -070098 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, files, keys));
Christopher Tatee012a232015-04-01 17:18:50 -070099 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this));
Christopher Tatef7cb8a02015-04-20 16:09:48 -0700100 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper());
Christopher Tatef9767d62015-04-08 14:35:43 -0700101 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this));
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700102 addHelper(PERMISSION_HELPER, new PermissionBackupHelper());
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000103 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
Makoto Onuki2e210c42016-03-30 08:30:36 -0700104 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper());
Christopher Tate7c2bb662009-09-20 19:47:46 -0700105 super.onBackup(oldState, data, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700106 }
107
Christopher Tate2efd2db2011-07-19 16:32:49 -0700108 @Override
109 public void onFullBackup(FullBackupDataOutput data) throws IOException {
110 // At present we back up only the wallpaper
111 fullWallpaperBackup(data);
Christopher Tate75a99702011-05-18 16:28:19 -0700112 }
113
Christopher Tate2efd2db2011-07-19 16:32:49 -0700114 private void fullWallpaperBackup(FullBackupDataOutput output) {
Christopher Tate75a99702011-05-18 16:28:19 -0700115 // 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 Tate11ae7682015-03-24 18:48:10 -0700120 WALLPAPER_INFO_DIR, WALLPAPER_INFO, output);
Christopher Tate75a99702011-05-18 16:28:19 -0700121 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
Christopher Tate11ae7682015-03-24 18:48:10 -0700122 WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output);
Christopher Tate4a627c72011-04-01 14:43:32 -0700123 }
124
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700125 @Override
126 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
127 throws IOException {
Christopher Tate41297ff2016-03-10 16:46:15 -0800128 mWallpaperHelper = new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800129 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO },
Christopher Tate41297ff2016-03-10 16:46:15 -0800130 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 Tatee012a232015-04-01 17:18:50 -0700134 addHelper("system_files", new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800135 new String[] { WALLPAPER_IMAGE },
136 new String[] { WALLPAPER_IMAGE_KEY} ));
Christopher Tate41297ff2016-03-10 16:46:15 -0800137
Christopher Tatee012a232015-04-01 17:18:50 -0700138 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this));
Christopher Tatef7cb8a02015-04-20 16:09:48 -0700139 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper());
140 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this));
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700141 addHelper(PERMISSION_HELPER, new PermissionBackupHelper());
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000142 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
Makoto Onuki2e210c42016-03-30 08:30:36 -0700143 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper());
Christopher Tate7c2bb662009-09-20 19:47:46 -0700144
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700145 try {
146 super.onRestore(data, appVersionCode, newState);
147
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800148 IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700149 Context.WALLPAPER_SERVICE);
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800150 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 Onorato9bb8fd72009-07-28 18:24:51 -0700157 } catch (IOException ex) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800158 // If there was a failure, delete everything for the wallpaper, this is too aggressive,
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700159 // but this is hopefully a rare failure.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800160 Slog.d(TAG, "restore failed", ex);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700161 (new File(WALLPAPER_IMAGE)).delete();
162 (new File(WALLPAPER_INFO)).delete();
163 }
164 }
Christopher Tate75a99702011-05-18 16:28:19 -0700165
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 Tate79ec80d2011-06-24 14:58:49 -0700191 FullBackup.restoreFile(data, size, type, mode, mtime, outFile);
Christopher Tate75a99702011-05-18 16:28:19 -0700192
193 if (restoredWallpaper) {
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800194 IWallpaperManager wallpaper =
195 (IWallpaperManager)ServiceManager.getService(
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000196 Context.WALLPAPER_SERVICE);
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800197 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 Tate75a99702011-05-18 16:28:19 -0700204 }
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 Tate41297ff2016-03-10 16:46:15 -0800213
214 @Override
215 public void onRestoreFinished() {
216 mWallpaperHelper.onRestoreFinished();
217 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700218}