blob: 30c11efd658ef0347c260f94283f659786f369cf [file] [log] [blame]
Christopher Tate3f64f8d2010-12-10 17:12:19 -08001/*
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;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -070022import android.graphics.Point;
Amith Yamasani61f57372012-08-31 12:12:28 -070023import android.os.Environment;
Christopher Tate3f64f8d2010-12-10 17:12:19 -080024import android.os.ParcelFileDescriptor;
Amith Yamasani61f57372012-08-31 12:12:28 -070025import android.os.UserHandle;
Christopher Tate3f64f8d2010-12-10 17:12:19 -080026import android.util.Slog;
Christopher Tate7f765cf2011-02-28 19:22:13 -080027import android.view.Display;
28import android.view.WindowManager;
Christopher Tate3f64f8d2010-12-10 17:12:19 -080029
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";
Christopher Tatebf6ee4f2011-10-07 12:37:42 -070040 private static final boolean DEBUG = false;
Christopher Tate3f64f8d2010-12-10 17:12:19 -080041
Christopher Tate406abd42014-10-06 18:00:51 -070042 // 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.
Christopher Tate431906b2014-10-08 12:19:48 -070045 private static final boolean REJECT_OUTSIZED_RESTORE = true;
Christopher Tate406abd42014-10-06 18:00:51 -070046
Christopher Tate1133e292014-10-10 10:49:16 -070047 // 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
Christopher Tate3f64f8d2010-12-10 17:12:19 -080057 // This path must match what the WallpaperManagerService uses
Amith Yamasani37ce3a82012-02-06 12:04:42 -080058 // TODO: Will need to change if backing up non-primary user's wallpaper
Xiaohui Chenbd0e03b2015-08-21 09:19:49 -070059 // http://b/22388012
Amith Yamasani61f57372012-08-31 12:12:28 -070060 public static final String WALLPAPER_IMAGE =
Xiaohui Chenbd0e03b2015-08-21 09:19:49 -070061 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
Amith Yamasani61f57372012-08-31 12:12:28 -070062 "wallpaper").getAbsolutePath();
63 public static final String WALLPAPER_INFO =
Xiaohui Chenbd0e03b2015-08-21 09:19:49 -070064 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
Amith Yamasani61f57372012-08-31 12:12:28 -070065 "wallpaper_info.xml").getAbsolutePath();
Amith Yamasani37ce3a82012-02-06 12:04:42 -080066 // 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";
Christopher Tate3f64f8d2010-12-10 17:12:19 -080070
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.
Amith Yamasani37ce3a82012-02-06 12:04:42 -080074 // TODO: Will need to change if backing up non-primary user's wallpaper
Xiaohui Chenbd0e03b2015-08-21 09:19:49 -070075 // http://b/22388012
Amith Yamasani61f57372012-08-31 12:12:28 -070076 private static final String STAGE_FILE =
Xiaohui Chenbd0e03b2015-08-21 09:19:49 -070077 new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
Amith Yamasani61f57372012-08-31 12:12:28 -070078 "wallpaper-tmp").getAbsolutePath();
Christopher Tate3f64f8d2010-12-10 17:12:19 -080079
80 Context mContext;
81 String[] mFiles;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080082 String[] mKeys;
Christopher Tate3f64f8d2010-12-10 17:12:19 -080083 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 */
Amith Yamasani37ce3a82012-02-06 12:04:42 -080093 public WallpaperBackupHelper(Context context, String[] files, String[] keys) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -080094 super(context);
95
96 mContext = context;
97 mFiles = files;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080098 mKeys = keys;
Christopher Tate3f64f8d2010-12-10 17:12:19 -080099
Christopher Tate004c1662014-10-02 13:39:12 -0700100 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);
Christopher Tate31f25692014-12-03 13:07:24 -0800107 mDesiredMinWidth = Math.min(size.x, size.y);
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800108 mDesiredMinHeight = (double) wpm.getDesiredMinimumHeight();
Christopher Tate004c1662014-10-02 13:39:12 -0700109 if (mDesiredMinHeight <= 0) {
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700110 mDesiredMinHeight = size.y;
Christopher Tate7f765cf2011-02-28 19:22:13 -0800111 }
112
Christopher Tate00724ca2011-02-17 12:58:59 -0800113 if (DEBUG) {
114 Slog.d(TAG, "dmW=" + mDesiredMinWidth + " dmH=" + mDesiredMinHeight);
115 }
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800116 }
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 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
124 ParcelFileDescriptor newState) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800125 performBackup_checked(oldState, data, newState, mFiles, mKeys);
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800126 }
127
128 /**
129 * Restore one absolute file entity from the restore stream. If we're restoring the
130 * magic wallpaper file, take specific action to determine whether it is suitable for
131 * the current device.
132 */
133 public void restoreEntity(BackupDataInputStream data) {
134 final String key = data.getKey();
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800135 if (isKeyInList(key, mKeys)) {
136 if (key.equals(WALLPAPER_IMAGE_KEY)) {
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800137 // restore the file to the stage for inspection
138 File f = new File(STAGE_FILE);
139 if (writeFile(f, data)) {
140
141 // Preflight the restored image's dimensions without loading it
142 BitmapFactory.Options options = new BitmapFactory.Options();
143 options.inJustDecodeBounds = true;
144 BitmapFactory.decodeFile(STAGE_FILE, options);
145
Christopher Tate00724ca2011-02-17 12:58:59 -0800146 if (DEBUG) Slog.d(TAG, "Restoring wallpaper image w=" + options.outWidth
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800147 + " h=" + options.outHeight);
148
Christopher Tate406abd42014-10-06 18:00:51 -0700149 if (REJECT_OUTSIZED_RESTORE) {
150 // We accept any wallpaper that is at least as wide as our preference
151 // (i.e. wide enough to fill the screen), and is within a comfortable
152 // factor of the target height, to avoid significant clipping/scaling/
Christopher Tate31f25692014-12-03 13:07:24 -0800153 // letterboxing. At this point we know that mDesiredMinWidth is the
154 // smallest dimension, regardless of current orientation, so we can
155 // safely require that the candidate's width and height both exceed
156 // that hard minimum.
Christopher Tate406abd42014-10-06 18:00:51 -0700157 final double heightRatio = mDesiredMinHeight / options.outHeight;
158 if (options.outWidth < mDesiredMinWidth
Christopher Tate31f25692014-12-03 13:07:24 -0800159 || options.outHeight < mDesiredMinWidth
Christopher Tate1133e292014-10-10 10:49:16 -0700160 || heightRatio >= MAX_HEIGHT_RATIO
161 || heightRatio <= MIN_HEIGHT_RATIO) {
Christopher Tate406abd42014-10-06 18:00:51 -0700162 // Not wide enough for the screen, or too short/tall to be a good fit
163 // for the height of the screen, broken image file, or the system's
164 // desires for wallpaper size are in a bad state. Probably one of the
165 // first two.
166 Slog.i(TAG, "Restored image dimensions (w="
167 + options.outWidth + ", h=" + options.outHeight
168 + ") too far off target (tw="
169 + mDesiredMinWidth + ", th=" + mDesiredMinHeight
170 + "); falling back to default wallpaper.");
171 f.delete();
172 return;
173 }
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800174 }
Christopher Tate406abd42014-10-06 18:00:51 -0700175
176 // We passed the acceptable-dimensions test (if any), so we're going to
177 // use the restored image.
178 // TODO: spin a service to copy the restored image to sd/usb storage,
179 // since it does not exist anywhere other than the private wallpaper
180 // file.
181 Slog.d(TAG, "Applying restored wallpaper image.");
182 f.renameTo(new File(WALLPAPER_IMAGE));
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800183 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800184 } else if (key.equals(WALLPAPER_INFO_KEY)) {
185 // XML file containing wallpaper info
186 File f = new File(WALLPAPER_INFO);
Christopher Tate3f64f8d2010-12-10 17:12:19 -0800187 writeFile(f, data);
188 }
189 }
190 }
191}