WallpaperBackupHelper.java revision 00724cacc8292900c3be8657ea9b3b6284cf4877
13f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate/* 23f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Copyright (C) 2010 The Android Open Source Project 33f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * 43f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Licensed under the Apache License, Version 2.0 (the "License"); 53f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * you may not use this file except in compliance with the License. 63f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * You may obtain a copy of the License at 73f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * 83f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * http://www.apache.org/licenses/LICENSE-2.0 93f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * 103f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Unless required by applicable law or agreed to in writing, software 113f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * distributed under the License is distributed on an "AS IS" BASIS, 123f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * See the License for the specific language governing permissions and 143f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * limitations under the License. 153f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate */ 163f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 173f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tatepackage android.app.backup; 183f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 193f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.app.WallpaperManager; 203f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.content.Context; 213f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.graphics.BitmapFactory; 223f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.os.ParcelFileDescriptor; 233f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.util.Slog; 243f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 253f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport java.io.File; 263f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 273f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate/** 283f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Helper for backing up / restoring wallpapers. Basically an AbsoluteFileBackupHelper, 293f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * but with logic for deciding what to do with restored wallpaper images. 303f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * 313f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * @hide 323f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate */ 333f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tatepublic class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper { 343f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate private static final String TAG = "WallpaperBackupHelper"; 3500724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate private static final boolean DEBUG = true; 363f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 373f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // This path must match what the WallpaperManagerService uses 383f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate private static final String WALLPAPER_IMAGE = "/data/data/com.android.settings/files/wallpaper"; 393f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 403f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // Stage file - should be adjacent to the WALLPAPER_IMAGE location. The wallpapers 413f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // will be saved to this file from the restore stream, then renamed to the proper 423f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // location if it's deemed suitable. 433f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate private static final String STAGE_FILE = "/data/data/com.android.settings/files/wallpaper-tmp"; 443f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 453f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate Context mContext; 463f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate String[] mFiles; 473f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate double mDesiredMinWidth; 483f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate double mDesiredMinHeight; 493f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 503f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate /** 513f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Construct a helper for backing up / restoring the files at the given absolute locations 523f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * within the file system. 533f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * 543f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * @param context 553f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * @param files 563f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate */ 573f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate public WallpaperBackupHelper(Context context, String... files) { 583f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate super(context); 593f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 603f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate mContext = context; 613f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate mFiles = files; 623f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 633f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate WallpaperManager wpm; 643f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate wpm = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE); 653f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate mDesiredMinWidth = (double) wpm.getDesiredMinimumWidth(); 663f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate mDesiredMinHeight = (double) wpm.getDesiredMinimumHeight(); 6700724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate 6800724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate if (DEBUG) { 6900724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate Slog.d(TAG, "dmW=" + mDesiredMinWidth + " dmH=" + mDesiredMinHeight); 7000724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate } 713f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } 723f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 733f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate /** 743f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Based on oldState, determine which of the files from the application's data directory 753f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * need to be backed up, write them to the data stream, and fill in newState with the 763f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * state as it exists now. 773f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate */ 783f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 793f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate ParcelFileDescriptor newState) { 803f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate performBackup_checked(oldState, data, newState, mFiles, mFiles); 813f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } 823f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 833f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate /** 843f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Restore one absolute file entity from the restore stream. If we're restoring the 853f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * magic wallpaper file, take specific action to determine whether it is suitable for 863f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * the current device. 873f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate */ 883f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate public void restoreEntity(BackupDataInputStream data) { 893f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate final String key = data.getKey(); 903f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate if (isKeyInList(key, mFiles)) { 913f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate if (key.equals(WALLPAPER_IMAGE)) { 923f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // restore the file to the stage for inspection 933f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate File f = new File(STAGE_FILE); 943f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate if (writeFile(f, data)) { 953f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 963f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // Preflight the restored image's dimensions without loading it 973f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate BitmapFactory.Options options = new BitmapFactory.Options(); 983f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate options.inJustDecodeBounds = true; 993f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate BitmapFactory.decodeFile(STAGE_FILE, options); 1003f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 10100724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate if (DEBUG) Slog.d(TAG, "Restoring wallpaper image w=" + options.outWidth 1023f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate + " h=" + options.outHeight); 1033f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate 1043f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // how much does the image differ from our preference? 1053f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate double widthRatio = mDesiredMinWidth / options.outWidth; 1063f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate double heightRatio = mDesiredMinHeight / options.outHeight; 1073f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate if (widthRatio > 0.8 && widthRatio < 1.25 1083f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate && heightRatio > 0.8 && heightRatio < 1.25) { 1093f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // sufficiently close to our resolution; go ahead and use it 11000724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate if (DEBUG) Slog.d(TAG, "wallpaper dimension match; using"); 1113f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate f.renameTo(new File(WALLPAPER_IMAGE)); 1123f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // TODO: spin a service to copy the restored image to sd/usb storage, 1133f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // since it does not exist anywhere other than the private wallpaper 1143f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // file. 1153f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } else { 11600724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate if (DEBUG) Slog.d(TAG, "dimensions too far off: wr=" + widthRatio 1173f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate + " hr=" + heightRatio); 1183f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate f.delete(); 1193f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } 1203f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } 1213f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } else { 1223f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate // Some other normal file; just decode it to its destination 1233f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate File f = new File(key); 1243f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate writeFile(f, data); 1253f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } 1263f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } 1273f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate } 1283f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate} 129