Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 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; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 22 | import android.graphics.Point; |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 23 | import android.os.Environment; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 24 | import android.os.ParcelFileDescriptor; |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 25 | import android.os.UserHandle; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 26 | import android.util.Slog; |
Christopher Tate | 7f765cf | 2011-02-28 19:22:13 -0800 | [diff] [blame] | 27 | import android.view.Display; |
| 28 | import android.view.WindowManager; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 29 | |
| 30 | import 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 | */ |
| 38 | public class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper { |
| 39 | private static final String TAG = "WallpaperBackupHelper"; |
Christopher Tate | bf6ee4f | 2011-10-07 12:37:42 -0700 | [diff] [blame] | 40 | private static final boolean DEBUG = false; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 41 | |
Christopher Tate | 406abd4 | 2014-10-06 18:00:51 -0700 | [diff] [blame] | 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. |
Christopher Tate | 431906b | 2014-10-08 12:19:48 -0700 | [diff] [blame] | 45 | private static final boolean REJECT_OUTSIZED_RESTORE = true; |
Christopher Tate | 406abd4 | 2014-10-06 18:00:51 -0700 | [diff] [blame] | 46 | |
Christopher Tate | 1133e29 | 2014-10-10 10:49:16 -0700 | [diff] [blame] | 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 | |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 57 | // This path must match what the WallpaperManagerService uses |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 58 | // TODO: Will need to change if backing up non-primary user's wallpaper |
Xiaohui Chen | bd0e03b | 2015-08-21 09:19:49 -0700 | [diff] [blame] | 59 | // http://b/22388012 |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 60 | public static final String WALLPAPER_IMAGE = |
Xiaohui Chen | bd0e03b | 2015-08-21 09:19:49 -0700 | [diff] [blame] | 61 | new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 62 | "wallpaper").getAbsolutePath(); |
| 63 | public static final String WALLPAPER_INFO = |
Xiaohui Chen | bd0e03b | 2015-08-21 09:19:49 -0700 | [diff] [blame] | 64 | new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 65 | "wallpaper_info.xml").getAbsolutePath(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 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"; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 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. |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 74 | // TODO: Will need to change if backing up non-primary user's wallpaper |
Xiaohui Chen | bd0e03b | 2015-08-21 09:19:49 -0700 | [diff] [blame] | 75 | // http://b/22388012 |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 76 | private static final String STAGE_FILE = |
Xiaohui Chen | bd0e03b | 2015-08-21 09:19:49 -0700 | [diff] [blame] | 77 | new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM), |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 78 | "wallpaper-tmp").getAbsolutePath(); |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 79 | |
| 80 | Context mContext; |
| 81 | String[] mFiles; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 82 | String[] mKeys; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 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 | */ |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 93 | public WallpaperBackupHelper(Context context, String[] files, String[] keys) { |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 94 | super(context); |
| 95 | |
| 96 | mContext = context; |
| 97 | mFiles = files; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 98 | mKeys = keys; |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 99 | |
Christopher Tate | 004c166 | 2014-10-02 13:39:12 -0700 | [diff] [blame] | 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); |
Christopher Tate | 31f2569 | 2014-12-03 13:07:24 -0800 | [diff] [blame] | 107 | mDesiredMinWidth = Math.min(size.x, size.y); |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 108 | mDesiredMinHeight = (double) wpm.getDesiredMinimumHeight(); |
Christopher Tate | 004c166 | 2014-10-02 13:39:12 -0700 | [diff] [blame] | 109 | if (mDesiredMinHeight <= 0) { |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 110 | mDesiredMinHeight = size.y; |
Christopher Tate | 7f765cf | 2011-02-28 19:22:13 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Christopher Tate | 00724ca | 2011-02-17 12:58:59 -0800 | [diff] [blame] | 113 | if (DEBUG) { |
| 114 | Slog.d(TAG, "dmW=" + mDesiredMinWidth + " dmH=" + mDesiredMinHeight); |
| 115 | } |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 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 | public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 124 | ParcelFileDescriptor newState) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 125 | performBackup_checked(oldState, data, newState, mFiles, mKeys); |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 126 | } |
| 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 Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 135 | if (isKeyInList(key, mKeys)) { |
| 136 | if (key.equals(WALLPAPER_IMAGE_KEY)) { |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 137 | // 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 Tate | 00724ca | 2011-02-17 12:58:59 -0800 | [diff] [blame] | 146 | if (DEBUG) Slog.d(TAG, "Restoring wallpaper image w=" + options.outWidth |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 147 | + " h=" + options.outHeight); |
| 148 | |
Christopher Tate | 406abd4 | 2014-10-06 18:00:51 -0700 | [diff] [blame] | 149 | 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 Tate | 31f2569 | 2014-12-03 13:07:24 -0800 | [diff] [blame] | 153 | // 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 Tate | 406abd4 | 2014-10-06 18:00:51 -0700 | [diff] [blame] | 157 | final double heightRatio = mDesiredMinHeight / options.outHeight; |
| 158 | if (options.outWidth < mDesiredMinWidth |
Christopher Tate | 31f2569 | 2014-12-03 13:07:24 -0800 | [diff] [blame] | 159 | || options.outHeight < mDesiredMinWidth |
Christopher Tate | 1133e29 | 2014-10-10 10:49:16 -0700 | [diff] [blame] | 160 | || heightRatio >= MAX_HEIGHT_RATIO |
| 161 | || heightRatio <= MIN_HEIGHT_RATIO) { |
Christopher Tate | 406abd4 | 2014-10-06 18:00:51 -0700 | [diff] [blame] | 162 | // 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 Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 174 | } |
Christopher Tate | 406abd4 | 2014-10-06 18:00:51 -0700 | [diff] [blame] | 175 | |
| 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 Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 183 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 184 | } else if (key.equals(WALLPAPER_INFO_KEY)) { |
| 185 | // XML file containing wallpaper info |
| 186 | File f = new File(WALLPAPER_INFO); |
Christopher Tate | 3f64f8d | 2010-12-10 17:12:19 -0800 | [diff] [blame] | 187 | writeFile(f, data); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |