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