WallpaperBackupHelper.java revision 41297ff82c94d28fe19a6dd8f6214aee7c0af8e2
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.graphics.Point; 23import android.os.Environment; 24import android.os.ParcelFileDescriptor; 25import android.os.UserHandle; 26import android.util.Slog; 27import android.view.Display; 28import android.view.WindowManager; 29 30import java.io.File; 31 32/** 33 * Helper for backing up / restoring wallpapers. Basically an AbsoluteFileBackupHelper, 34 * but with logic for deciding what to do with restored wallpaper images. 35 * 36 * @hide 37 */ 38public class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper { 39 private static final String TAG = "WallpaperBackupHelper"; 40 private static final boolean DEBUG = false; 41 42 // If 'true', then apply an acceptable-size heuristic at restore time, dropping back 43 // to the factory default wallpaper if the restored one differs "too much" from the 44 // device's preferred wallpaper image dimensions. 45 private static final boolean REJECT_OUTSIZED_RESTORE = true; 46 47 // When outsized restore rejection is enabled, this is the maximum ratio between the 48 // source and target image heights that will be permitted. The ratio is checked both 49 // ways (i.e. >= MAX, or <= 1/MAX) to validate restores from both largeer-than-target 50 // and smaller-than-target sources. 51 private static final double MAX_HEIGHT_RATIO = 1.35; 52 53 // The height ratio check when applying larger images on smaller screens is separate; 54 // in current policy we accept any such restore regardless of the relative dimensions. 55 private static final double MIN_HEIGHT_RATIO = 0; 56 57 // This path must match what the WallpaperManagerService uses 58 // TODO: Will need to change if backing up non-primary user's wallpaper 59 // http://b/22388012 60 public static final String WALLPAPER_IMAGE = 61 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), 62 "wallpaper").getAbsolutePath(); 63 public static final String WALLPAPER_INFO = 64 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), 65 "wallpaper_info.xml").getAbsolutePath(); 66 // Use old keys to keep legacy data compatibility and avoid writing two wallpapers 67 public static final String WALLPAPER_IMAGE_KEY = 68 "/data/data/com.android.settings/files/wallpaper"; 69 public static final String WALLPAPER_INFO_KEY = "/data/system/wallpaper_info.xml"; 70 71 // Stage file - should be adjacent to the WALLPAPER_IMAGE location. The wallpapers 72 // will be saved to this file from the restore stream, then renamed to the proper 73 // location if it's deemed suitable. 74 // TODO: Will need to change if backing up non-primary user's wallpaper 75 // http://b/22388012 76 private static final String STAGE_FILE = 77 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), 78 "wallpaper-tmp").getAbsolutePath(); 79 80 Context mContext; 81 String[] mFiles; 82 String[] mKeys; 83 double mDesiredMinWidth; 84 double mDesiredMinHeight; 85 86 /** 87 * Construct a helper for backing up / restoring the files at the given absolute locations 88 * within the file system. 89 * 90 * @param context 91 * @param files 92 */ 93 public WallpaperBackupHelper(Context context, String[] files, String[] keys) { 94 super(context); 95 96 mContext = context; 97 mFiles = files; 98 mKeys = keys; 99 100 final WindowManager wm = 101 (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 102 final WallpaperManager wpm = 103 (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE); 104 final Display d = wm.getDefaultDisplay(); 105 final Point size = new Point(); 106 d.getSize(size); 107 mDesiredMinWidth = Math.min(size.x, size.y); 108 mDesiredMinHeight = (double) wpm.getDesiredMinimumHeight(); 109 if (mDesiredMinHeight <= 0) { 110 mDesiredMinHeight = size.y; 111 } 112 113 if (DEBUG) { 114 Slog.d(TAG, "dmW=" + mDesiredMinWidth + " dmH=" + mDesiredMinHeight); 115 } 116 } 117 118 /** 119 * Based on oldState, determine which of the files from the application's data directory 120 * need to be backed up, write them to the data stream, and fill in newState with the 121 * state as it exists now. 122 */ 123 @Override 124 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, 125 ParcelFileDescriptor newState) { 126 performBackup_checked(oldState, data, newState, mFiles, mKeys); 127 } 128 129 /** 130 * Restore one absolute file entity from the restore stream. If we're restoring the 131 * magic wallpaper file, take specific action to determine whether it is suitable for 132 * the current device. 133 */ 134 @Override 135 public void restoreEntity(BackupDataInputStream data) { 136 final String key = data.getKey(); 137 if (isKeyInList(key, mKeys)) { 138 if (key.equals(WALLPAPER_IMAGE_KEY)) { 139 // restore the file to the stage for inspection 140 File f = new File(STAGE_FILE); 141 if (writeFile(f, data)) { 142 143 // Preflight the restored image's dimensions without loading it 144 BitmapFactory.Options options = new BitmapFactory.Options(); 145 options.inJustDecodeBounds = true; 146 BitmapFactory.decodeFile(STAGE_FILE, options); 147 148 if (DEBUG) Slog.d(TAG, "Restoring wallpaper image w=" + options.outWidth 149 + " h=" + options.outHeight); 150 151 if (REJECT_OUTSIZED_RESTORE) { 152 // We accept any wallpaper that is at least as wide as our preference 153 // (i.e. wide enough to fill the screen), and is within a comfortable 154 // factor of the target height, to avoid significant clipping/scaling/ 155 // letterboxing. At this point we know that mDesiredMinWidth is the 156 // smallest dimension, regardless of current orientation, so we can 157 // safely require that the candidate's width and height both exceed 158 // that hard minimum. 159 final double heightRatio = mDesiredMinHeight / options.outHeight; 160 if (options.outWidth < mDesiredMinWidth 161 || options.outHeight < mDesiredMinWidth 162 || heightRatio >= MAX_HEIGHT_RATIO 163 || heightRatio <= MIN_HEIGHT_RATIO) { 164 // Not wide enough for the screen, or too short/tall to be a good fit 165 // for the height of the screen, broken image file, or the system's 166 // desires for wallpaper size are in a bad state. Probably one of the 167 // first two. 168 Slog.i(TAG, "Restored image dimensions (w=" 169 + options.outWidth + ", h=" + options.outHeight 170 + ") too far off target (tw=" 171 + mDesiredMinWidth + ", th=" + mDesiredMinHeight 172 + "); falling back to default wallpaper."); 173 f.delete(); 174 return; 175 } 176 } 177 178 // We passed the acceptable-dimensions test (if any), so we're going to 179 // use the restored image. That comes last, when we are done restoring 180 // both the pixels and the metadata. 181 } 182 } else if (key.equals(WALLPAPER_INFO_KEY)) { 183 // XML file containing wallpaper info 184 File f = new File(WALLPAPER_INFO); 185 writeFile(f, data); 186 } 187 } 188 } 189 190 /** 191 * Hook for the agent to call this helper upon completion of the restore. We do this 192 * upon completion so that we know both the imagery and the wallpaper info have 193 * been emplaced without requiring either or relying on ordering. 194 */ 195 public void onRestoreFinished() { 196 final File f = new File(STAGE_FILE); 197 if (f.exists()) { 198 // TODO: spin a service to copy the restored image to sd/usb storage, 199 // since it does not exist anywhere other than the private wallpaper 200 // file. 201 Slog.d(TAG, "Applying restored wallpaper image."); 202 f.renameTo(new File(WALLPAPER_IMAGE)); 203 } 204 } 205} 206