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