blob: a7a583c918c7d08923afebf06452487d4fa969d8 [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
17package com.android.server;
18
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080019
Christopher Tate45281862010-03-05 15:46:30 -080020import android.app.backup.BackupDataInput;
Christopher Tate45281862010-03-05 15:46:30 -080021import android.app.backup.BackupDataOutput;
Christopher Tatecc84c692010-03-29 14:54:02 -070022import android.app.backup.BackupAgentHelper;
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;
27import android.os.ParcelFileDescriptor;
28import android.os.ServiceManager;
Joe Onorato8a9b2202010-02-26 18:56:32 -080029import android.util.Slog;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070030
Christopher Tate3f64f8d2010-12-10 17:12:19 -080031
Joe Onorato9bb8fd72009-07-28 18:24:51 -070032import java.io.File;
33import java.io.IOException;
34
35/**
Christopher Tate7c2bb662009-09-20 19:47:46 -070036 * Backup agent for various system-managed data, currently just the system wallpaper
Joe Onorato9bb8fd72009-07-28 18:24:51 -070037 */
Christopher Tatecc84c692010-03-29 14:54:02 -070038public class SystemBackupAgent extends BackupAgentHelper {
Joe Onorato9bb8fd72009-07-28 18:24:51 -070039 private static final String TAG = "SystemBackupAgent";
40
Christopher Tate75a99702011-05-18 16:28:19 -070041 // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME
42 // are also used in the full-backup file format, so must not change unless steps are
43 // taken to support the legacy backed-up datasets.
44 private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper";
45 private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml";
46
Amith Yamasani37ce3a82012-02-06 12:04:42 -080047 // TODO: Will need to change if backing up non-primary user's wallpaper
48 private static final String WALLPAPER_IMAGE_DIR = "/data/system/users/0";
49 private static final String WALLPAPER_IMAGE = WallpaperBackupHelper.WALLPAPER_IMAGE;
Christopher Tate75a99702011-05-18 16:28:19 -070050
Amith Yamasani37ce3a82012-02-06 12:04:42 -080051 // TODO: Will need to change if backing up non-primary user's wallpaper
52 private static final String WALLPAPER_INFO_DIR = "/data/system/users/0";
53 private static final String WALLPAPER_INFO = WallpaperBackupHelper.WALLPAPER_INFO;
54 // Use old keys to keep legacy data compatibility and avoid writing two wallpapers
55 private static final String WALLPAPER_IMAGE_KEY = WallpaperBackupHelper.WALLPAPER_IMAGE_KEY;
56 private static final String WALLPAPER_INFO_KEY = WallpaperBackupHelper.WALLPAPER_INFO_KEY;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070057
58 @Override
Christopher Tate7c2bb662009-09-20 19:47:46 -070059 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
60 ParcelFileDescriptor newState) throws IOException {
61 // We only back up the data under the current "wallpaper" schema with metadata
Dan Egnor541fa512009-11-11 17:00:06 -080062 WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
63 Context.WALLPAPER_SERVICE);
64 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO };
Amith Yamasani37ce3a82012-02-06 12:04:42 -080065 String[] keys = new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY };
66 if (wallpaper != null && wallpaper.getName() != null && wallpaper.getName().length() > 0) {
Dan Egnor541fa512009-11-11 17:00:06 -080067 // When the wallpaper has a name, back up the info by itself.
68 // TODO: Don't rely on the innards of the service object like this!
69 // TODO: Send a delete for any stored wallpaper image in this case?
70 files = new String[] { WALLPAPER_INFO };
Amith Yamasani37ce3a82012-02-06 12:04:42 -080071 keys = new String[] { WALLPAPER_INFO_KEY };
Dan Egnor541fa512009-11-11 17:00:06 -080072 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -080073 addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, files, keys));
Christopher Tate7c2bb662009-09-20 19:47:46 -070074 super.onBackup(oldState, data, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -070075 }
76
Christopher Tate2efd2db2011-07-19 16:32:49 -070077 @Override
78 public void onFullBackup(FullBackupDataOutput data) throws IOException {
79 // At present we back up only the wallpaper
80 fullWallpaperBackup(data);
Christopher Tate75a99702011-05-18 16:28:19 -070081 }
82
Christopher Tate2efd2db2011-07-19 16:32:49 -070083 private void fullWallpaperBackup(FullBackupDataOutput output) {
Christopher Tate75a99702011-05-18 16:28:19 -070084 // Back up the data files directly. We do them in this specific order --
85 // info file followed by image -- because then we need take no special
86 // steps during restore; the restore will happen properly when the individual
87 // files are restored piecemeal.
88 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
Christopher Tate2efd2db2011-07-19 16:32:49 -070089 WALLPAPER_INFO_DIR, WALLPAPER_INFO, output.getData());
Christopher Tate75a99702011-05-18 16:28:19 -070090 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
Christopher Tate2efd2db2011-07-19 16:32:49 -070091 WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output.getData());
Christopher Tate4a627c72011-04-01 14:43:32 -070092 }
93
Joe Onorato9bb8fd72009-07-28 18:24:51 -070094 @Override
95 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
96 throws IOException {
Christopher Tate7c2bb662009-09-20 19:47:46 -070097 // On restore, we also support a previous data schema "system_files"
Christopher Tate3f64f8d2010-12-10 17:12:19 -080098 addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -080099 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO },
100 new String[] { WALLPAPER_IMAGE_KEY, WALLPAPER_INFO_KEY} ));
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800101 addHelper("system_files", new WallpaperBackupHelper(SystemBackupAgent.this,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800102 new String[] { WALLPAPER_IMAGE },
103 new String[] { WALLPAPER_IMAGE_KEY} ));
Christopher Tate7c2bb662009-09-20 19:47:46 -0700104
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700105 try {
106 super.onRestore(data, appVersionCode, newState);
107
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700108 WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700109 Context.WALLPAPER_SERVICE);
110 wallpaper.settingsRestored();
111 } catch (IOException ex) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800112 // If there was a failure, delete everything for the wallpaper, this is too aggressive,
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700113 // but this is hopefully a rare failure.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800114 Slog.d(TAG, "restore failed", ex);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700115 (new File(WALLPAPER_IMAGE)).delete();
116 (new File(WALLPAPER_INFO)).delete();
117 }
118 }
Christopher Tate75a99702011-05-18 16:28:19 -0700119
120 @Override
121 public void onRestoreFile(ParcelFileDescriptor data, long size,
122 int type, String domain, String path, long mode, long mtime)
123 throws IOException {
124 Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path);
125
126 // Bits to indicate postprocessing we may need to perform
127 boolean restoredWallpaper = false;
128
129 File outFile = null;
130 // Various domain+files we understand a priori
131 if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) {
132 if (path.equals(WALLPAPER_INFO_FILENAME)) {
133 outFile = new File(WALLPAPER_INFO);
134 restoredWallpaper = true;
135 } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) {
136 outFile = new File(WALLPAPER_IMAGE);
137 restoredWallpaper = true;
138 }
139 }
140
141 try {
142 if (outFile == null) {
143 Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]");
144 }
Christopher Tate79ec80d2011-06-24 14:58:49 -0700145 FullBackup.restoreFile(data, size, type, mode, mtime, outFile);
Christopher Tate75a99702011-05-18 16:28:19 -0700146
147 if (restoredWallpaper) {
148 WallpaperManagerService wallpaper =
149 (WallpaperManagerService)ServiceManager.getService(
150 Context.WALLPAPER_SERVICE);
151 wallpaper.settingsRestored();
152 }
153 } catch (IOException e) {
154 if (restoredWallpaper) {
155 // Make sure we wind up in a good state
156 (new File(WALLPAPER_IMAGE)).delete();
157 (new File(WALLPAPER_INFO)).delete();
158 }
159 }
160 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700161}