blob: 54555bb1968946185252e8874d9eaadf106b28f7 [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 Tate7c2bb662009-09-20 19:47:46 -070040 // These paths must match what the WallpaperManagerService uses
Christopher Tate4a627c72011-04-01 14:43:32 -070041 private static final String WALLPAPER_IMAGE_DIR = "/data/data/com.android.settings/files";
42 private static final String WALLPAPER_IMAGE = WALLPAPER_IMAGE_DIR + "/wallpaper";
43 private static final String WALLPAPER_INFO_DIR = "/data/system";
44 private static final String WALLPAPER_INFO = WALLPAPER_INFO_DIR + "/wallpaper_info.xml";
Joe Onorato9bb8fd72009-07-28 18:24:51 -070045
46 @Override
Christopher Tate7c2bb662009-09-20 19:47:46 -070047 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
48 ParcelFileDescriptor newState) throws IOException {
Christopher Tate4a627c72011-04-01 14:43:32 -070049 if (oldState == null) {
50 runFullBackup(data);
51 return;
52 }
53
Christopher Tate7c2bb662009-09-20 19:47:46 -070054 // We only back up the data under the current "wallpaper" schema with metadata
Dan Egnor541fa512009-11-11 17:00:06 -080055 WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
56 Context.WALLPAPER_SERVICE);
57 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO };
58 if (wallpaper != null && wallpaper.mName != null && wallpaper.mName.length() > 0) {
59 // When the wallpaper has a name, back up the info by itself.
60 // TODO: Don't rely on the innards of the service object like this!
61 // TODO: Send a delete for any stored wallpaper image in this case?
62 files = new String[] { WALLPAPER_INFO };
63 }
Christopher Tate3f64f8d2010-12-10 17:12:19 -080064 addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, files));
Christopher Tate7c2bb662009-09-20 19:47:46 -070065 super.onBackup(oldState, data, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -070066 }
67
Christopher Tate4a627c72011-04-01 14:43:32 -070068 private void runFullBackup(BackupDataOutput output) {
69 // Back up the data files directly
70 FullBackup.backupToTar(getPackageName(), null, null,
71 WALLPAPER_IMAGE_DIR, WALLPAPER_IMAGE, output);
72 FullBackup.backupToTar(getPackageName(), null, null,
73 WALLPAPER_INFO_DIR, WALLPAPER_INFO, output);
74 }
75
Joe Onorato9bb8fd72009-07-28 18:24:51 -070076 @Override
77 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
78 throws IOException {
Christopher Tate7c2bb662009-09-20 19:47:46 -070079 // On restore, we also support a previous data schema "system_files"
Christopher Tate3f64f8d2010-12-10 17:12:19 -080080 addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this,
Christopher Tate7c2bb662009-09-20 19:47:46 -070081 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }));
Christopher Tate3f64f8d2010-12-10 17:12:19 -080082 addHelper("system_files", new WallpaperBackupHelper(SystemBackupAgent.this,
Christopher Tate7c2bb662009-09-20 19:47:46 -070083 new String[] { WALLPAPER_IMAGE }));
84
Joe Onorato9bb8fd72009-07-28 18:24:51 -070085 try {
86 super.onRestore(data, appVersionCode, newState);
87
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070088 WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
Joe Onorato9bb8fd72009-07-28 18:24:51 -070089 Context.WALLPAPER_SERVICE);
90 wallpaper.settingsRestored();
91 } catch (IOException ex) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -080092 // If there was a failure, delete everything for the wallpaper, this is too aggressive,
Joe Onorato9bb8fd72009-07-28 18:24:51 -070093 // but this is hopefully a rare failure.
Joe Onorato8a9b2202010-02-26 18:56:32 -080094 Slog.d(TAG, "restore failed", ex);
Joe Onorato9bb8fd72009-07-28 18:24:51 -070095 (new File(WALLPAPER_IMAGE)).delete();
96 (new File(WALLPAPER_INFO)).delete();
97 }
98 }
99}