blob: a80abce5c4850ae78b8fd13f2a5d6b951f0878b0 [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
Wale Ogunwale18795a22014-12-03 11:38:33 -080019import android.app.ActivityManagerNative;
Amith Yamasani09e9cdc2013-11-06 14:54:50 -080020import android.app.IWallpaperManager;
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 Tatecc84c692010-03-29 14:54:02 -070023import android.app.backup.BackupAgentHelper;
Christopher Tate4a627c72011-04-01 14:43:32 -070024import android.app.backup.FullBackup;
Christopher Tate2efd2db2011-07-19 16:32:49 -070025import android.app.backup.FullBackupDataOutput;
Christopher Tateac6a3a52014-11-18 11:57:25 -080026import android.app.backup.RecentsBackupHelper;
Christopher Tate3f64f8d2010-12-10 17:12:19 -080027import android.app.backup.WallpaperBackupHelper;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070028import android.content.Context;
Amith Yamasani61f57372012-08-31 12:12:28 -070029import android.os.Environment;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070030import android.os.ParcelFileDescriptor;
Amith Yamasani09e9cdc2013-11-06 14:54:50 -080031import android.os.RemoteException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070032import android.os.ServiceManager;
Amith Yamasani61f57372012-08-31 12:12:28 -070033import android.os.UserHandle;
Joe Onorato8a9b2202010-02-26 18:56:32 -080034import android.util.Slog;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070035
36import java.io.File;
37import java.io.IOException;
38
39/**
Christopher Tate7c2bb662009-09-20 19:47:46 -070040 * Backup agent for various system-managed data, currently just the system wallpaper
Joe Onorato9bb8fd72009-07-28 18:24:51 -070041 */
Christopher Tatecc84c692010-03-29 14:54:02 -070042public class SystemBackupAgent extends BackupAgentHelper {
Joe Onorato9bb8fd72009-07-28 18:24:51 -070043 private static final String TAG = "SystemBackupAgent";
44
Christopher Tatee012a232015-04-01 17:18:50 -070045 // 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 Tatef9767d62015-04-08 14:35:43 -070051 private static final String NOTIFICATION_HELPER = "notifications";
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
Amith Yamasani61f57372012-08-31 12:12:28 -070060 private static final String WALLPAPER_IMAGE_DIR =
61 Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080062 private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE;
Christopher Tate75a99702011-05-18 16:28:19 -070063
Amith Yamasani37ce3a82012-02-06 12:04:42 -080064 // TODO: Will need to change if backing up non-primary user's wallpaper
Amith Yamasani61f57372012-08-31 12:12:28 -070065 private static final String WALLPAPER_INFO_DIR =
66 Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080067 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 Onorato9bb8fd72009-07-28 18:24:51 -070071
72 @Override
Christopher Tate7c2bb662009-09-20 19:47:46 -070073 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 Yamasani09e9cdc2013-11-06 14:54:50 -080076 IWallpaperManager wallpaper = (IWallpaperManager)ServiceManager.getService(
Dan Egnor541fa512009-11-11 17:00:06 -080077 Context.WALLPAPER_SERVICE);
78 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO };
Amith Yamasani37ce3a82012-02-06 12:04:42 -080079 String[] keys = new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY };
Amith Yamasani09e9cdc2013-11-06 14:54:50 -080080 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 Egnor541fa512009-11-11 17:00:06 -080093 }
Christopher Tatee012a232015-04-01 17:18:50 -070094 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, files, keys));
95 addHelper(RECENTS_HELPER, new RecentsBackupHelper(this));
96 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this));
Christopher Tatef7cb8a02015-04-20 16:09:48 -070097 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper());
Christopher Tatef9767d62015-04-08 14:35:43 -070098 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this));
Christopher Tateac6a3a52014-11-18 11:57:25 -080099
Christopher Tate7c2bb662009-09-20 19:47:46 -0700100 super.onBackup(oldState, data, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700101 }
102
Christopher Tate2efd2db2011-07-19 16:32:49 -0700103 @Override
104 public void onFullBackup(FullBackupDataOutput data) throws IOException {
105 // At present we back up only the wallpaper
106 fullWallpaperBackup(data);
Christopher Tate75a99702011-05-18 16:28:19 -0700107 }
108
Christopher Tate2efd2db2011-07-19 16:32:49 -0700109 private void fullWallpaperBackup(FullBackupDataOutput output) {
Christopher Tate75a99702011-05-18 16:28:19 -0700110 // 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 Tate11ae7682015-03-24 18:48:10 -0700115 WALLPAPER_INFO_DIR, WALLPAPER_INFO, output);
Christopher Tate75a99702011-05-18 16:28:19 -0700116 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
Christopher Tate11ae7682015-03-24 18:48:10 -0700117 WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output);
Christopher Tate4a627c72011-04-01 14:43:32 -0700118 }
119
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700120 @Override
121 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
122 throws IOException {
Christopher Tate7c2bb662009-09-20 19:47:46 -0700123 // On restore, we also support a previous data schema "system_files"
Christopher Tatee012a232015-04-01 17:18:50 -0700124 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800125 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO },
126 new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} ));
Christopher Tatee012a232015-04-01 17:18:50 -0700127 addHelper("system_files", new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800128 new String[] { WALLPAPER_IMAGE },
129 new String[] { WALLPAPER_IMAGE_KEY} ));
Christopher Tatee012a232015-04-01 17:18:50 -0700130 addHelper(RECENTS_HELPER, new RecentsBackupHelper(this));
131 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this));
Christopher Tatef7cb8a02015-04-20 16:09:48 -0700132 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper());
133 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this));
Christopher Tate7c2bb662009-09-20 19:47:46 -0700134
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700135 try {
136 super.onRestore(data, appVersionCode, newState);
137
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800138 IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700139 Context.WALLPAPER_SERVICE);
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800140 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 Onorato9bb8fd72009-07-28 18:24:51 -0700147 } catch (IOException ex) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800148 // If there was a failure, delete everything for the wallpaper, this is too aggressive,
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700149 // but this is hopefully a rare failure.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800150 Slog.d(TAG, "restore failed", ex);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700151 (new File(WALLPAPER_IMAGE)).delete();
152 (new File(WALLPAPER_INFO)).delete();
153 }
154 }
Christopher Tate75a99702011-05-18 16:28:19 -0700155
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 Tate79ec80d2011-06-24 14:58:49 -0700181 FullBackup.restoreFile(data, size, type, mode, mtime, outFile);
Christopher Tate75a99702011-05-18 16:28:19 -0700182
183 if (restoredWallpaper) {
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800184 IWallpaperManager wallpaper =
185 (IWallpaperManager)ServiceManager.getService(
Christopher Tate75a99702011-05-18 16:28:19 -0700186 Context.WALLPAPER_SERVICE);
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800187 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 Tate75a99702011-05-18 16:28:19 -0700194 }
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 Ogunwale18795a22014-12-03 11:38:33 -0800203
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 Onorato9bb8fd72009-07-28 18:24:51 -0700212}