| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 19 | |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 20 | import android.app.backup.BackupDataInput; |
| Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 21 | import android.app.backup.BackupDataOutput; |
| Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 22 | import android.app.backup.BackupAgentHelper; |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 23 | import android.app.backup.FullBackup; |
| Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 24 | import android.app.backup.WallpaperBackupHelper; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.os.ParcelFileDescriptor; |
| 27 | import android.os.ServiceManager; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 28 | import android.util.Slog; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 29 | |
| Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 30 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 31 | import java.io.File; |
| 32 | import java.io.IOException; |
| 33 | |
| 34 | /** |
| Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 35 | * Backup agent for various system-managed data, currently just the system wallpaper |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 36 | */ |
| Christopher Tate | cc84c69 | 2010-03-29 14:54:02 -0700 | [diff] [blame] | 37 | public class SystemBackupAgent extends BackupAgentHelper { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 38 | private static final String TAG = "SystemBackupAgent"; |
| 39 | |
| Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 40 | // These paths must match what the WallpaperManagerService uses |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 41 | 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 Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 45 | |
| 46 | @Override |
| Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 47 | public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 48 | ParcelFileDescriptor newState) throws IOException { |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 49 | if (oldState == null) { |
| 50 | runFullBackup(data); |
| 51 | return; |
| 52 | } |
| 53 | |
| Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 54 | // We only back up the data under the current "wallpaper" schema with metadata |
| Dan Egnor | 541fa51 | 2009-11-11 17:00:06 -0800 | [diff] [blame] | 55 | 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 Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 64 | addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, files)); |
| Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 65 | super.onBackup(oldState, data, newState); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 68 | 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 Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 76 | @Override |
| 77 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 78 | throws IOException { |
| Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 79 | // On restore, we also support a previous data schema "system_files" |
| Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 80 | addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, |
| Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 81 | new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO })); |
| Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 82 | addHelper("system_files", new WallpaperBackupHelper(SystemBackupAgent.this, |
| Christopher Tate | 7c2bb66 | 2009-09-20 19:47:46 -0700 | [diff] [blame] | 83 | new String[] { WALLPAPER_IMAGE })); |
| 84 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 85 | try { |
| 86 | super.onRestore(data, appVersionCode, newState); |
| 87 | |
| Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 88 | WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService( |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 89 | Context.WALLPAPER_SERVICE); |
| 90 | wallpaper.settingsRestored(); |
| 91 | } catch (IOException ex) { |
| Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 92 | // If there was a failure, delete everything for the wallpaper, this is too aggressive, |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 93 | // but this is hopefully a rare failure. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 94 | Slog.d(TAG, "restore failed", ex); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 95 | (new File(WALLPAPER_IMAGE)).delete(); |
| 96 | (new File(WALLPAPER_INFO)).delete(); |
| 97 | } |
| 98 | } |
| 99 | } |