WallpaperBackupHelper.java revision 3f64f8d8fc05189777e83b4efd3882cbc661fdeb
1/* 2 * Copyright (C) 2010 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 android.app.backup; 18 19import android.app.WallpaperManager; 20import android.content.Context; 21import android.graphics.BitmapFactory; 22import android.os.ParcelFileDescriptor; 23import android.util.Slog; 24 25import java.io.File; 26 27/** 28 * Helper for backing up / restoring wallpapers. Basically an AbsoluteFileBackupHelper, 29 * but with logic for deciding what to do with restored wallpaper images. 30 * 31 * @hide 32 */ 33public class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper { 34 private static final String TAG = "WallpaperBackupHelper"; 35 private static final boolean DEBUG = false; 36 37 // This path must match what the WallpaperManagerService uses 38 private static final String WALLPAPER_IMAGE = "/data/data/com.android.settings/files/wallpaper"; 39 40 // Stage file - should be adjacent to the WALLPAPER_IMAGE location. The wallpapers 41 // will be saved to this file from the restore stream, then renamed to the proper 42 // location if it's deemed suitable. 43 private static final String STAGE_FILE = "/data/data/com.android.settings/files/wallpaper-tmp"; 44 45 Context mContext; 46 String[] mFiles; 47 double mDesiredMinWidth; 48 double mDesiredMinHeight; 49 50 /** 51 * Construct a helper for backing up / restoring the files at the given absolute locations 52 * within the file system. 53 * 54 * @param context 55 * @param files 56 */ 57 public WallpaperBackupHelper(Context context, String... files) { 58 super(context); 59 60 mContext = context; 61 mFiles = files; 62 63 WallpaperManager wpm; 64 wpm = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE); 65 mDesiredMinWidth = (double) wpm.getDesiredMinimumWidth(); 66 mDesiredMinHeight = (double) wpm.getDesiredMinimumHeight(); 67 } 68 69 /** 70 * Based on oldState, determine which of the files from the application's data directory 71 * need to be backed up, write them to the data stream, and fill in newState with the 72 * state as it exists now. 73 */ 74 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 75 ParcelFileDescriptor newState) { 76 performBackup_checked(oldState, data, newState, mFiles, mFiles); 77 } 78 79 /** 80 * Restore one absolute file entity from the restore stream. If we're restoring the 81 * magic wallpaper file, take specific action to determine whether it is suitable for 82 * the current device. 83 */ 84 public void restoreEntity(BackupDataInputStream data) { 85 final String key = data.getKey(); 86 if (isKeyInList(key, mFiles)) { 87 if (key.equals(WALLPAPER_IMAGE)) { 88 // restore the file to the stage for inspection 89 File f = new File(STAGE_FILE); 90 if (writeFile(f, data)) { 91 92 // Preflight the restored image's dimensions without loading it 93 BitmapFactory.Options options = new BitmapFactory.Options(); 94 options.inJustDecodeBounds = true; 95 BitmapFactory.decodeFile(STAGE_FILE, options); 96 97 if (DEBUG) Slog.v(TAG, "Restoring wallpaper image w=" + options.outWidth 98 + " h=" + options.outHeight); 99 100 // how much does the image differ from our preference? 101 double widthRatio = mDesiredMinWidth / options.outWidth; 102 double heightRatio = mDesiredMinHeight / options.outHeight; 103 if (widthRatio > 0.8 && widthRatio < 1.25 104 && heightRatio > 0.8 && heightRatio < 1.25) { 105 // sufficiently close to our resolution; go ahead and use it 106 if (DEBUG) Slog.v(TAG, "wallpaper dimension match; using"); 107 f.renameTo(new File(WALLPAPER_IMAGE)); 108 // TODO: spin a service to copy the restored image to sd/usb storage, 109 // since it does not exist anywhere other than the private wallpaper 110 // file. 111 } else { 112 if (DEBUG) Slog.v(TAG, "dimensions too far off: wr=" + widthRatio 113 + " hr=" + heightRatio); 114 f.delete(); 115 } 116 } 117 } else { 118 // Some other normal file; just decode it to its destination 119 File f = new File(key); 120 writeFile(f, data); 121 } 122 } 123 } 124} 125