blob: 67abe554f61b479ff8bc564b784f27a0c8151bd3 [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
19import android.backup.AbsoluteFileBackupHelper;
20import android.backup.BackupDataInput;
21import android.backup.BackupDataInputStream;
22import android.backup.BackupDataOutput;
23import android.backup.BackupHelper;
24import android.backup.BackupHelperAgent;
25import android.content.Context;
26import android.os.ParcelFileDescriptor;
27import android.os.ServiceManager;
28import android.os.SystemService;
29import android.util.Log;
30
31import 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 */
37public class SystemBackupAgent extends BackupHelperAgent {
38 private static final String TAG = "SystemBackupAgent";
39
Christopher Tate7c2bb662009-09-20 19:47:46 -070040 // These paths must match what the WallpaperManagerService uses
Joe Onorato9bb8fd72009-07-28 18:24:51 -070041 private static final String WALLPAPER_IMAGE = "/data/data/com.android.settings/files/wallpaper";
42 private static final String WALLPAPER_INFO = "/data/system/wallpaper_info.xml";
43
44 @Override
Christopher Tate7c2bb662009-09-20 19:47:46 -070045 public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
46 ParcelFileDescriptor newState) throws IOException {
47 // We only back up the data under the current "wallpaper" schema with metadata
Dan Egnor541fa512009-11-11 17:00:06 -080048 WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
49 Context.WALLPAPER_SERVICE);
50 String[] files = new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO };
51 if (wallpaper != null && wallpaper.mName != null && wallpaper.mName.length() > 0) {
52 // When the wallpaper has a name, back up the info by itself.
53 // TODO: Don't rely on the innards of the service object like this!
54 // TODO: Send a delete for any stored wallpaper image in this case?
55 files = new String[] { WALLPAPER_INFO };
56 }
57 addHelper("wallpaper", new AbsoluteFileBackupHelper(SystemBackupAgent.this, files));
Christopher Tate7c2bb662009-09-20 19:47:46 -070058 super.onBackup(oldState, data, newState);
Joe Onorato9bb8fd72009-07-28 18:24:51 -070059 }
60
61 @Override
62 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
63 throws IOException {
Christopher Tate7c2bb662009-09-20 19:47:46 -070064 // On restore, we also support a previous data schema "system_files"
65 addHelper("wallpaper", new AbsoluteFileBackupHelper(SystemBackupAgent.this,
66 new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO }));
67 addHelper("system_files", new AbsoluteFileBackupHelper(SystemBackupAgent.this,
68 new String[] { WALLPAPER_IMAGE }));
69
Joe Onorato9bb8fd72009-07-28 18:24:51 -070070 boolean success = false;
71 try {
72 super.onRestore(data, appVersionCode, newState);
73
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070074 WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService(
Joe Onorato9bb8fd72009-07-28 18:24:51 -070075 Context.WALLPAPER_SERVICE);
76 wallpaper.settingsRestored();
77 } catch (IOException ex) {
78 // If there was a failure, delete everything for the wallpaper, this is too aggresive,
79 // but this is hopefully a rare failure.
80 Log.d(TAG, "restore failed", ex);
81 (new File(WALLPAPER_IMAGE)).delete();
82 (new File(WALLPAPER_INFO)).delete();
83 }
84 }
85}