blob: 537565185d9b41ea74b137356c3d8210fdd67e4a [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";
Svet Ganov5d09c992016-09-07 09:57:41 -070052 private static final String ACCOUNT_MANAGER_HELPER = "account_manager";
Christopher Tatee012a232015-04-01 17:18:50 -070053
Christopher Tate75a99702011-05-18 16:28:19 -070054 // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME
55 // are also used in the full-backup file format, so must not change unless steps are
56 // taken to support the legacy backed-up datasets.
57 private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper";
58 private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml";
59
Amith Yamasani37ce3a82012-02-06 12:04:42 -080060 // TODO: Will need to change if backing up non-primary user's wallpaper
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070061 // TODO: http://b/22388012
Amith Yamasani61f57372012-08-31 12:12:28 -070062 private static final String WALLPAPER_IMAGE_DIR =
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070063 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080064 private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE;
Christopher Tate75a99702011-05-18 16:28:19 -070065
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_INFO_DIR =
Xiaohui Chen4cfe29b2015-09-04 13:55:23 -070069 Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM).getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080070 private static final String WALLPAPER_INFO = WallpaperBackupHelper.WALLPAPER_INFO;
71 // Use old keys to keep legacy data compatibility and avoid writing two wallpapers
72 private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY;
73 private static final String WALLPAPER_INFO_KEY = WallpaperBackupHelper.WALLPAPER_INFO_KEY;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070074
Christopher Tate41297ff2016-03-10 16:46:15 -080075 private WallpaperBackupHelper mWallpaperHelper = null;
76
Joe Onorato9bb8fd72009-07-28 18:24:51 -070077 @Override
Christopher Tate7c2bb662009-09-20 19:47:46 -070078 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
79 ParcelFileDescriptor newState) throws IOException {
Christopher Tatee012a232015-04-01 17:18:50 -070080 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this));
Christopher Tatef7cb8a02015-04-20 16:09:48 -070081 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper());
Christopher Tatef9767d62015-04-08 14:35:43 -070082 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this));
Christopher Tatee9fd1fa2015-09-15 16:43:07 -070083 addHelper(PERMISSION_HELPER, new PermissionBackupHelper());
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +000084 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
Makoto Onuki2e210c42016-03-30 08:30:36 -070085 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper());
Svet Ganov5d09c992016-09-07 09:57:41 -070086 addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper());
Christopher Tate7c2bb662009-09-20 19:47:46 -070087 super.onBackup(oldState, data, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -070088 }
89
Christopher Tate2efd2db2011-07-19 16:32:49 -070090 @Override
91 public void onFullBackup(FullBackupDataOutput data) throws IOException {
Christopher Tated7faf532016-02-25 12:43:38 -080092 // At present we don't back up anything
Christopher Tate4a627c72011-04-01 14:43:32 -070093 }
94
Joe Onorato9bb8fd72009-07-28 18:24:51 -070095 @Override
96 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
97 throws IOException {
Christopher Tated7faf532016-02-25 12:43:38 -080098 // Slot in a restore helper for the older wallpaper backup schema to support restore
99 // from devices still generating data in that format.
Christopher Tate41297ff2016-03-10 16:46:15 -0800100 mWallpaperHelper = new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800101 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO },
Christopher Tate41297ff2016-03-10 16:46:15 -0800102 new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} );
103 addHelper(WALLPAPER_HELPER, mWallpaperHelper);
104
Christopher Tated7faf532016-02-25 12:43:38 -0800105 // On restore, we also support a long-ago wallpaper data schema "system_files"
Christopher Tatee012a232015-04-01 17:18:50 -0700106 addHelper("system_files", new WallpaperBackupHelper(this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800107 new String[] { WALLPAPER_IMAGE },
108 new String[] { WALLPAPER_IMAGE_KEY} ));
Christopher Tate41297ff2016-03-10 16:46:15 -0800109
Christopher Tatee012a232015-04-01 17:18:50 -0700110 addHelper(SYNC_SETTINGS_HELPER, new AccountSyncSettingsBackupHelper(this));
Christopher Tatef7cb8a02015-04-20 16:09:48 -0700111 addHelper(PREFERRED_HELPER, new PreferredActivityBackupHelper());
112 addHelper(NOTIFICATION_HELPER, new NotificationBackupHelper(this));
Christopher Tatee9fd1fa2015-09-15 16:43:07 -0700113 addHelper(PERMISSION_HELPER, new PermissionBackupHelper());
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000114 addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
Makoto Onuki2e210c42016-03-30 08:30:36 -0700115 addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper());
Svet Ganov5d09c992016-09-07 09:57:41 -0700116 addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper());
Christopher Tate7c2bb662009-09-20 19:47:46 -0700117
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700118 try {
119 super.onRestore(data, appVersionCode, newState);
120
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800121 IWallpaperManager wallpaper = (IWallpaperManager) ServiceManager.getService(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700122 Context.WALLPAPER_SERVICE);
Amith Yamasani09e9cdc2013-11-06 14:54:50 -0800123 if (wallpaper != null) {
124 try {
125 wallpaper.settingsRestored();
126 } catch (RemoteException re) {
127 Slog.e(TAG, "Couldn't restore settings\n" + re);
128 }
129 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700130 } catch (IOException ex) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800131 // If there was a failure, delete everything for the wallpaper, this is too aggressive,
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700132 // but this is hopefully a rare failure.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800133 Slog.d(TAG, "restore failed", ex);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700134 (new File(WALLPAPER_IMAGE)).delete();
135 (new File(WALLPAPER_INFO)).delete();
136 }
137 }
Christopher Tate75a99702011-05-18 16:28:19 -0700138
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 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 }
Christopher Tate41297ff2016-03-10 16:46:15 -0800186
187 @Override
188 public void onRestoreFinished() {
Christopher Tatec9e584b2016-04-12 12:36:47 -0700189 // helper will be null following 'adb restore' or other full-data operation
190 if (mWallpaperHelper != null) {
191 mWallpaperHelper.onRestoreFinished();
192 }
Christopher Tate41297ff2016-03-10 16:46:15 -0800193 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700194}