blob: 19d9e292f9fb437cf23f7ebc64860be6058b1a64 [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";
51
Christopher Tate75a99702011-05-18 16:28:19 -070052 // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME
53 // are also used in the full-backup file format, so must not change unless steps are
54 // taken to support the legacy backed-up datasets.
55 private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper";
56 private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml";
57
Amith Yamasani37ce3a82012-02-06 12:04:42 -080058 // TODO: Will need to change if backing up non-primary user's wallpaper
Amith Yamasani61f57372012-08-31 12:12:28 -070059 private static final String WALLPAPER_IMAGE_DIR =
60 Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080061 private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE;
Christopher Tate75a99702011-05-18 16:28:19 -070062
Amith Yamasani37ce3a82012-02-06 12:04:42 -080063 // TODO: Will need to change if backing up non-primary user's wallpaper
Amith Yamasani61f57372012-08-31 12:12:28 -070064 private static final String WALLPAPER_INFO_DIR =
65 Environment.getUserSystemDirectory(UserHandle.USER_OWNER).getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080066 private static final String WALLPAPER_INFO = WallpaperBackupHelper.WALLPAPER_INFO;
67 // Use old keys to keep legacy data compatibility and avoid writing two wallpapers
68 private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY;
69 private static final String WALLPAPER_INFO_KEY = WallpaperBackupHelper.WALLPAPER_INFO_KEY;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070070
71 @Override
Christopher Tate7c2bb662009-09-20 19:47:46 -070072 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
73 ParcelFileDescriptor newState) throws IOException {
74 // We only back up the data under the current "wallpaper" schema with metadata
Amith Yamasani09e9cdc2013-11-06 14:54:50 -080075 IWallpaperManager wallpaper = (IWallpaperManager)ServiceManager.getService(
Dan Egnor541fa512009-11-11 17:00:06 -080076 Context.WALLPAPER_SERVICE);
77 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO };
Amith Yamasani37ce3a82012-02-06 12:04:42 -080078 String[] keys = new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY };
Amith Yamasani09e9cdc2013-11-06 14:54:50 -080079 if (wallpaper != null) {
80 try {
81 final String wallpaperName = wallpaper.getName();
82 if (wallpaperName != null && wallpaperName.length() > 0) {
83 // When the wallpaper has a name, back up the info by itself.
84 // TODO: Don't rely on the innards of the service object like this!
85 // TODO: Send a delete for any stored wallpaper image in this case?
86 files = new String[] { WALLPAPER_INFO };
87 keys = new String[] { WALLPAPER_INFO_KEY };
88 }
89 } catch (RemoteException re) {
90 Slog.e(TAG, "Couldn't get wallpaper name\n" + re);
91 }
Dan Egnor541fa512009-11-11 17:00:06 -080092 }
Christopher Tatee012a232015-04-01 17:18:50 -070093 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this, files, keys));
94 addHelper(RECENTS_HELPER, new RecentsBackupHelper(this));
95 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this));
96 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper(this));
Christopher Tateac6a3a52014-11-18 11:57:25 -080097
Christopher Tate7c2bb662009-09-20 19:47:46 -070098 super.onBackup(oldState, data, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -070099 }
100
Christopher Tate2efd2db2011-07-19 16:32:49 -0700101 @Override
102 public void onFullBackup(FullBackupDataOutput data) throws IOException {
103 // At present we back up only the wallpaper
104 fullWallpaperBackup(data);
Christopher Tate75a99702011-05-18 16:28:19 -0700105 }
106
Christopher Tate2efd2db2011-07-19 16:32:49 -0700107 private void fullWallpaperBackup(FullBackupDataOutput output) {
Christopher Tate75a99702011-05-18 16:28:19 -0700108 // Back up the data files directly. We do them in this specific order --
109 // info file followed by image -- because then we need take no special
110 // steps during restore; the restore will happen properly when the individual
111 // files are restored piecemeal.
112 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
Christopher Tate11ae7682015-03-24 18:48:10 -0700113 WALLPAPER_INFO_DIR, WALLPAPER_INFO, output);
Christopher Tate75a99702011-05-18 16:28:19 -0700114 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
Christopher Tate11ae7682015-03-24 18:48:10 -0700115 WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output);
Christopher Tate4a627c72011-04-01 14:43:32 -0700116 }
117
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700118 @Override
119 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
120 throws IOException {
Christopher Tate7c2bb662009-09-20 19:47:46 -0700121 // On restore, we also support a previous data schema "system_files"
Christopher Tatee012a232015-04-01 17:18:50 -0700122 addHelper(WALLPAPER_HELPER, new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800123 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO },
124 new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} ));
Christopher Tatee012a232015-04-01 17:18:50 -0700125 addHelper("system_files", new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800126 new String[] { WALLPAPER_IMAGE },
127 new String[] { WALLPAPER_IMAGE_KEY} ));
Christopher Tatee012a232015-04-01 17:18:50 -0700128 addHelper(RECENTS_HELPER, new RecentsBackupHelper(this));
129 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this));
130 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper(this));
Christopher Tate7c2bb662009-09-20 19:47:46 -0700131
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700132 try {
133 super.onRestore(data, appVersionCode, newState);
134
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800135 IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700136 Context.WALLPAPER_SERVICE);
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800137 if (wallpaper != null) {
138 try {
139 wallpaper.settingsRestored();
140 } catch (RemoteException re) {
141 Slog.e(TAG, "Couldn't restore settings\n" + re);
142 }
143 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700144 } catch (IOException ex) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800145 // If there was a failure, delete everything for the wallpaper, this is too aggressive,
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700146 // but this is hopefully a rare failure.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800147 Slog.d(TAG, "restore failed", ex);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700148 (new File(WALLPAPER_IMAGE)).delete();
149 (new File(WALLPAPER_INFO)).delete();
150 }
151 }
Christopher Tate75a99702011-05-18 16:28:19 -0700152
153 @Override
154 public void onRestoreFile(ParcelFileDescriptor data, long size,
155 int type, String domain, String path, long mode, long mtime)
156 throws IOException {
157 Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path);
158
159 // Bits to indicate postprocessing we may need to perform
160 boolean restoredWallpaper = false;
161
162 File outFile = null;
163 // Various domain+files we understand a priori
164 if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) {
165 if (path.equals(WALLPAPER_INFO_FILENAME)) {
166 outFile = new File(WALLPAPER_INFO);
167 restoredWallpaper = true;
168 } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) {
169 outFile = new File(WALLPAPER_IMAGE);
170 restoredWallpaper = true;
171 }
172 }
173
174 try {
175 if (outFile == null) {
176 Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]");
177 }
Christopher Tate79ec80d2011-06-24 14:58:49 -0700178 FullBackup.restoreFile(data, size, type, mode, mtime, outFile);
Christopher Tate75a99702011-05-18 16:28:19 -0700179
180 if (restoredWallpaper) {
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800181 IWallpaperManager wallpaper =
182 (IWallpaperManager)ServiceManager.getService(
Christopher Tate75a99702011-05-18 16:28:19 -0700183 Context.WALLPAPER_SERVICE);
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800184 if (wallpaper != null) {
185 try {
186 wallpaper.settingsRestored();
187 } catch (RemoteException re) {
188 Slog.e(TAG, "Couldn't restore settings\n" + re);
189 }
190 }
Christopher Tate75a99702011-05-18 16:28:19 -0700191 }
192 } catch (IOException e) {
193 if (restoredWallpaper) {
194 // Make sure we wind up in a good state
195 (new File(WALLPAPER_IMAGE)).delete();
196 (new File(WALLPAPER_INFO)).delete();
197 }
198 }
199 }
Wale Ogunwale18795a22014-12-03 11:38:33 -0800200
201 @Override
202 public void onRestoreFinished() {
203 try {
204 ActivityManagerNative.getDefault().systemBackupRestored();
205 } catch (RemoteException e) {
206 // Not possible since this code is running in the system process.
207 }
208 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700209}