SystemBackupAgent.java revision 7c2bb66db77653c0a690962858cf105b0cb981d7
19bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato/* 29bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * Copyright (C) 2009 The Android Open Source Project 39bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * 49bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License"); 59bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * you may not use this file except in compliance with the License. 69bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * You may obtain a copy of the License at 79bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * 89bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * http://www.apache.org/licenses/LICENSE-2.0 99bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * 109bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * Unless required by applicable law or agreed to in writing, software 119bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS, 129bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * See the License for the specific language governing permissions and 149bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato * limitations under the License. 159bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato */ 169bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato 179bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratopackage com.android.server; 189bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato 199bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.backup.AbsoluteFileBackupHelper; 209bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.backup.BackupDataInput; 219bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.backup.BackupDataInputStream; 229bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.backup.BackupDataOutput; 239bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.backup.BackupHelper; 249bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.backup.BackupHelperAgent; 259bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.content.Context; 269bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.os.ParcelFileDescriptor; 279bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.os.ServiceManager; 289bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.os.SystemService; 299bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport android.util.Log; 309bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato 319bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport java.io.File; 329bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratoimport java.io.IOException; 339bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato 349bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato/** 357c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate * Backup agent for various system-managed data, currently just the system wallpaper 369bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato */ 379bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onoratopublic class SystemBackupAgent extends BackupHelperAgent { 389bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato private static final String TAG = "SystemBackupAgent"; 399bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato 407c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate // These paths must match what the WallpaperManagerService uses 419bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato private static final String WALLPAPER_IMAGE = "/data/data/com.android.settings/files/wallpaper"; 429bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato private static final String WALLPAPER_INFO = "/data/system/wallpaper_info.xml"; 439bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato 449bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato @Override 457c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 467c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate ParcelFileDescriptor newState) throws IOException { 477c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate // We only back up the data under the current "wallpaper" schema with metadata 489bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato addHelper("wallpaper", new AbsoluteFileBackupHelper(SystemBackupAgent.this, 499bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO })); 507c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate super.onBackup(oldState, data, newState); 519bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato } 529bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato 539bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato @Override 549bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) 559bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato throws IOException { 567c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate // On restore, we also support a previous data schema "system_files" 577c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate addHelper("wallpaper", new AbsoluteFileBackupHelper(SystemBackupAgent.this, 587c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate new String[] { WALLPAPER_IMAGE, WALLPAPER_INFO })); 597c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate addHelper("system_files", new AbsoluteFileBackupHelper(SystemBackupAgent.this, 607c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate new String[] { WALLPAPER_IMAGE })); 617c2bb66db77653c0a690962858cf105b0cb981d7Christopher Tate 629bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato boolean success = false; 639bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato try { 649bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato super.onRestore(data, appVersionCode, newState); 659bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato 668cc6a5026aeb5cf9cc36529426fe0cc66714f5fbDianne Hackborn WallpaperManagerService wallpaper = (WallpaperManagerService)ServiceManager.getService( 679bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato Context.WALLPAPER_SERVICE); 689bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato wallpaper.settingsRestored(); 699bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato } catch (IOException ex) { 709bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato // If there was a failure, delete everything for the wallpaper, this is too aggresive, 719bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato // but this is hopefully a rare failure. 729bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato Log.d(TAG, "restore failed", ex); 739bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato (new File(WALLPAPER_IMAGE)).delete(); 749bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato (new File(WALLPAPER_INFO)).delete(); 759bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato } 769bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato } 779bb8fd77c8dc177aab9ac96bed4f55972dcda70aJoe Onorato} 78