blob: 08c66996128e66f7248dacfbb0492d838dc828a6 [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 Tate3f64f8d2010-12-10 17:12:19 -080024import android.app.backup.WallpaperBackupHelper;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070025import android.content.Context;
26import android.os.ParcelFileDescriptor;
27import android.os.ServiceManager;
Joe Onorato8a9b2202010-02-26 18:56:32 -080028import android.util.Slog;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070029
Christopher Tate3f64f8d2010-12-10 17:12:19 -080030
Joe Onorato9bb8fd72009-07-28 18:24:51 -070031import java.io.File;
32import java.io.IOException;
33
34/**
Christopher Tate7c2bb662009-09-20 19:47:46 -070035 * Backup agent for various system-managed data, currently just the system wallpaper
Joe Onorato9bb8fd72009-07-28 18:24:51 -070036 */
Christopher Tatecc84c692010-03-29 14:54:02 -070037public class SystemBackupAgent extends BackupAgentHelper {
Joe Onorato9bb8fd72009-07-28 18:24:51 -070038 private static final String TAG = "SystemBackupAgent";
39
Christopher Tate75a99702011-05-18 16:28:19 -070040 // These paths must match what the WallpaperManagerService uses. The leaf *_FILENAME
41 // are also used in the full-backup file format, so must not change unless steps are
42 // taken to support the legacy backed-up datasets.
43 private static final String WALLPAPER_IMAGE_FILENAME = "wallpaper";
44 private static final String WALLPAPER_INFO_FILENAME = "wallpaper_info.xml";
45
Christopher Tate4a627c72011-04-01 14:43:32 -070046 private static final String WALLPAPER_IMAGE_DIR = "/data/data/com.android.settings/files";
Christopher Tate75a99702011-05-18 16:28:19 -070047 private static final String WALLPAPER_IMAGE = WALLPAPER_IMAGE_DIR + "/" + WALLPAPER_IMAGE_FILENAME;
48
Christopher Tate4a627c72011-04-01 14:43:32 -070049 private static final String WALLPAPER_INFO_DIR = "/data/system";
Christopher Tate75a99702011-05-18 16:28:19 -070050 private static final String WALLPAPER_INFO = WALLPAPER_INFO_DIR + "/" + WALLPAPER_INFO_FILENAME;
51
Joe Onorato9bb8fd72009-07-28 18:24:51 -070052
53 @Override
Christopher Tate7c2bb662009-09-20 19:47:46 -070054 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
55 ParcelFileDescriptor newState) throws IOException {
Christopher Tate4a627c72011-04-01 14:43:32 -070056 if (oldState == null) {
Christopher Tate75a99702011-05-18 16:28:19 -070057 // Ah, it's a full backup dataset, being restored piecemeal. Just
58 // pop over to the full restore handling and we're done.
Christopher Tate4a627c72011-04-01 14:43:32 -070059 runFullBackup(data);
60 return;
61 }
62
Christopher Tate7c2bb662009-09-20 19:47:46 -070063 // We only back up the data under the current "wallpaper" schema with metadata
Dan Egnor541fa512009-11-11 17:00:06 -080064 WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
65 Context.WALLPAPER_SERVICE);
66 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO };
67 if (wallpaper != null && wallpaper.mName != null && wallpaper.mName.length() > 0) {
68 // When the wallpaper has a name, back up the info by itself.
69 // TODO: Don't rely on the innards of the service object like this!
70 // TODO: Send a delete for any stored wallpaper image in this case?
71 files = new String[] { WALLPAPER_INFO };
72 }
Christopher Tate3f64f8d2010-12-10 17:12:19 -080073 addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, files));
Christopher Tate7c2bb662009-09-20 19:47:46 -070074 super.onBackup(oldState, data, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -070075 }
76
Christopher Tate4a627c72011-04-01 14:43:32 -070077 private void runFullBackup(BackupDataOutput output) {
Christopher Tate75a99702011-05-18 16:28:19 -070078 fullWallpaperBackup(output);
79 }
80
81 private void fullWallpaperBackup(BackupDataOutput output) {
82 // Back up the data files directly. We do them in this specific order --
83 // info file followed by image -- because then we need take no special
84 // steps during restore; the restore will happen properly when the individual
85 // files are restored piecemeal.
86 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
Christopher Tate4a627c72011-04-01 14:43:32 -070087 WALLPAPER_INFO_DIR, WALLPAPER_INFO, output);
Christopher Tate75a99702011-05-18 16:28:19 -070088 FullBackup.backupToTar(getPackageName(), FullBackup.ROOT_TREE_TOKEN, null,
89 WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output);
Christopher Tate4a627c72011-04-01 14:43:32 -070090 }
91
Joe Onorato9bb8fd72009-07-28 18:24:51 -070092 @Override
93 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
94 throws IOException {
Christopher Tate7c2bb662009-09-20 19:47:46 -070095 // On restore, we also support a previous data schema "system_files"
Christopher Tate3f64f8d2010-12-10 17:12:19 -080096 addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this,
Christopher Tate7c2bb662009-09-20 19:47:46 -070097 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }));
Christopher Tate3f64f8d2010-12-10 17:12:19 -080098 addHelper("system_files", new WallpaperBackupHelper(SystemBackupAgent.this,
Christopher Tate7c2bb662009-09-20 19:47:46 -070099 new String[] { WALLPAPER_IMAGE }));
100
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700101 try {
102 super.onRestore(data, appVersionCode, newState);
103
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700104 WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700105 Context.WALLPAPER_SERVICE);
106 wallpaper.settingsRestored();
107 } catch (IOException ex) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800108 // If there was a failure, delete everything for the wallpaper, this is too aggressive,
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700109 // but this is hopefully a rare failure.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800110 Slog.d(TAG, "restore failed", ex);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700111 (new File(WALLPAPER_IMAGE)).delete();
112 (new File(WALLPAPER_INFO)).delete();
113 }
114 }
Christopher Tate75a99702011-05-18 16:28:19 -0700115
116 @Override
117 public void onRestoreFile(ParcelFileDescriptor data, long size,
118 int type, String domain, String path, long mode, long mtime)
119 throws IOException {
120 Slog.i(TAG, "Restoring file domain=" + domain + " path=" + path);
121
122 // Bits to indicate postprocessing we may need to perform
123 boolean restoredWallpaper = false;
124
125 File outFile = null;
126 // Various domain+files we understand a priori
127 if (domain.equals(FullBackup.ROOT_TREE_TOKEN)) {
128 if (path.equals(WALLPAPER_INFO_FILENAME)) {
129 outFile = new File(WALLPAPER_INFO);
130 restoredWallpaper = true;
131 } else if (path.equals(WALLPAPER_IMAGE_FILENAME)) {
132 outFile = new File(WALLPAPER_IMAGE);
133 restoredWallpaper = true;
134 }
135 }
136
137 try {
138 if (outFile == null) {
139 Slog.w(TAG, "Skipping unrecognized system file: [ " + domain + " : " + path + " ]");
140 }
Christopher Tateb0628bf2011-06-02 15:08:13 -0700141 FullBackup.restoreToFile(data, size, type, mode, mtime, outFile, true);
Christopher Tate75a99702011-05-18 16:28:19 -0700142
143 if (restoredWallpaper) {
144 WallpaperManagerService wallpaper =
145 (WallpaperManagerService)ServiceManager.getService(
146 Context.WALLPAPER_SERVICE);
147 wallpaper.settingsRestored();
148 }
149 } catch (IOException e) {
150 if (restoredWallpaper) {
151 // Make sure we wind up in a good state
152 (new File(WALLPAPER_IMAGE)).delete();
153 (new File(WALLPAPER_INFO)).delete();
154 }
155 }
156 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700157}