2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.app.backup;
19 import android.app.WallpaperManager;
20 import android.content.Context;
21 import android.graphics.BitmapFactory;
22 import android.graphics.Point;
23 import android.os.Environment;
24 import android.os.ParcelFileDescriptor;
25 import android.os.UserHandle;
26 import android.util.Slog;
27 import android.view.Display;
28 import android.view.WindowManager;
33 * Helper for backing up / restoring wallpapers. Basically an AbsoluteFileBackupHelper,
34 * but with logic for deciding what to do with restored wallpaper images.
38 public class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper {
39 private static final String TAG = "WallpaperBackupHelper";
40 private static final boolean DEBUG = false;
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;
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;
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;
57 // This path must match what the WallpaperManagerService uses
58 // TODO: Will need to change if backing up non-primary user's wallpaper
59 public static final String WALLPAPER_IMAGE =
60 new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
61 "wallpaper").getAbsolutePath();
62 public static final String WALLPAPER_INFO =
63 new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
64 "wallpaper_info.xml").getAbsolutePath();
65 // Use old keys to keep legacy data compatibility and avoid writing two wallpapers
66 public static final String WALLPAPER_IMAGE_KEY =
67 "/data/data/com.android.settings/files/wallpaper";
68 public static final String WALLPAPER_INFO_KEY = "/data/system/wallpaper_info.xml";
70 // Stage file - should be adjacent to the WALLPAPER_IMAGE location. The wallpapers
71 // will be saved to this file from the restore stream, then renamed to the proper
72 // location if it's deemed suitable.
73 // TODO: Will need to change if backing up non-primary user's wallpaper
74 private static final String STAGE_FILE =
75 new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
76 "wallpaper-tmp").getAbsolutePath();
81 double mDesiredMinWidth;
82 double mDesiredMinHeight;
85 * Construct a helper for backing up / restoring the files at the given absolute locations
86 * within the file system.
91 public WallpaperBackupHelper(Context context, String[] files, String[] keys) {
98 final WindowManager wm =
99 (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
100 final WallpaperManager wpm =
101 (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
102 final Display d = wm.getDefaultDisplay();
103 final Point size = new Point();
105 mDesiredMinWidth = size.x;
106 mDesiredMinHeight = (double) wpm.getDesiredMinimumHeight();
108 if (mDesiredMinHeight <= 0) {
109 mDesiredMinHeight = size.y;
113 Slog.d(TAG, "dmW=" + mDesiredMinWidth + " dmH=" + mDesiredMinHeight);
118 * Based on oldState, determine which of the files from the application's data directory
119 * need to be backed up, write them to the data stream, and fill in newState with the
120 * state as it exists now.
122 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
123 ParcelFileDescriptor newState) {
124 performBackup_checked(oldState, data, newState, mFiles, mKeys);
128 * Restore one absolute file entity from the restore stream. If we're restoring the
129 * magic wallpaper file, take specific action to determine whether it is suitable for
130 * the current device.
132 public void restoreEntity(BackupDataInputStream data) {
133 final String key = data.getKey();
134 if (isKeyInList(key, mKeys)) {
135 if (key.equals(WALLPAPER_IMAGE_KEY)) {
136 // restore the file to the stage for inspection
137 File f = new File(STAGE_FILE);
138 if (writeFile(f, data)) {
140 // Preflight the restored image's dimensions without loading it
141 BitmapFactory.Options options = new BitmapFactory.Options();
142 options.inJustDecodeBounds = true;
143 BitmapFactory.decodeFile(STAGE_FILE, options);
145 if (DEBUG) Slog.d(TAG, "Restoring wallpaper image w=" + options.outWidth
146 + " h=" + options.outHeight);
148 if (REJECT_OUTSIZED_RESTORE) {
149 // We accept any wallpaper that is at least as wide as our preference
150 // (i.e. wide enough to fill the screen), and is within a comfortable
151 // factor of the target height, to avoid significant clipping/scaling/
153 final double heightRatio = mDesiredMinHeight / options.outHeight;
154 if (options.outWidth < mDesiredMinWidth
155 || heightRatio >= MAX_HEIGHT_RATIO
156 || heightRatio <= MIN_HEIGHT_RATIO) {
157 // Not wide enough for the screen, or too short/tall to be a good fit
158 // for the height of the screen, broken image file, or the system's
159 // desires for wallpaper size are in a bad state. Probably one of the
161 Slog.i(TAG, "Restored image dimensions (w="
162 + options.outWidth + ", h=" + options.outHeight
163 + ") too far off target (tw="
164 + mDesiredMinWidth + ", th=" + mDesiredMinHeight
165 + "); falling back to default wallpaper.");
171 // We passed the acceptable-dimensions test (if any), so we're going to
172 // use the restored image.
173 // TODO: spin a service to copy the restored image to sd/usb storage,
174 // since it does not exist anywhere other than the private wallpaper
176 Slog.d(TAG, "Applying restored wallpaper image.");
177 f.renameTo(new File(WALLPAPER_IMAGE));
179 } else if (key.equals(WALLPAPER_INFO_KEY)) {
180 // XML file containing wallpaper info
181 File f = new File(WALLPAPER_INFO);