WallpaperBackupHelper.java revision 7f765cf588e37a70fa2a1d251aaa8e7b847801b6
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.os.ParcelFileDescriptor;
23import android.util.Slog;
24import android.view.Display;
25import android.view.WindowManager;
26
27import java.io.File;
28
29/**
30 * Helper for backing up / restoring wallpapers.  Basically an AbsoluteFileBackupHelper,
31 * but with logic for deciding what to do with restored wallpaper images.
32 *
33 * @hide
34 */
35public class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper {
36    private static final String TAG = "WallpaperBackupHelper";
37    private static final boolean DEBUG = true;
38
39    // This path must match what the WallpaperManagerService uses
40    private static final String WALLPAPER_IMAGE = "/data/data/com.android.settings/files/wallpaper";
41
42    // Stage file - should be adjacent to the WALLPAPER_IMAGE location.  The wallpapers
43    // will be saved to this file from the restore stream, then renamed to the proper
44    // location if it's deemed suitable.
45    private static final String STAGE_FILE = "/data/data/com.android.settings/files/wallpaper-tmp";
46
47    Context mContext;
48    String[] mFiles;
49    double mDesiredMinWidth;
50    double mDesiredMinHeight;
51
52    /**
53     * Construct a helper for backing up / restoring the files at the given absolute locations
54     * within the file system.
55     *
56     * @param context
57     * @param files
58     */
59    public WallpaperBackupHelper(Context context, String... files) {
60        super(context);
61
62        mContext = context;
63        mFiles = files;
64
65        WallpaperManager wpm;
66        wpm = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
67        mDesiredMinWidth = (double) wpm.getDesiredMinimumWidth();
68        mDesiredMinHeight = (double) wpm.getDesiredMinimumHeight();
69
70        if (mDesiredMinWidth <= 0 || mDesiredMinHeight <= 0) {
71            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
72            Display d = wm.getDefaultDisplay();
73            mDesiredMinWidth = d.getWidth();
74            mDesiredMinHeight = d.getHeight();
75        }
76
77        if (DEBUG) {
78            Slog.d(TAG, "dmW=" + mDesiredMinWidth + " dmH=" + mDesiredMinHeight);
79        }
80    }
81
82    /**
83     * Based on oldState, determine which of the files from the application's data directory
84     * need to be backed up, write them to the data stream, and fill in newState with the
85     * state as it exists now.
86     */
87    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
88            ParcelFileDescriptor newState) {
89        performBackup_checked(oldState, data, newState, mFiles, mFiles);
90    }
91
92    /**
93     * Restore one absolute file entity from the restore stream.  If we're restoring the
94     * magic wallpaper file, take specific action to determine whether it is suitable for
95     * the current device.
96     */
97    public void restoreEntity(BackupDataInputStream data) {
98        final String key = data.getKey();
99        if (isKeyInList(key, mFiles)) {
100            if (key.equals(WALLPAPER_IMAGE)) {
101                // restore the file to the stage for inspection
102                File f = new File(STAGE_FILE);
103                if (writeFile(f, data)) {
104
105                    // Preflight the restored image's dimensions without loading it
106                    BitmapFactory.Options options = new BitmapFactory.Options();
107                    options.inJustDecodeBounds = true;
108                    BitmapFactory.decodeFile(STAGE_FILE, options);
109
110                    if (DEBUG) Slog.d(TAG, "Restoring wallpaper image w=" + options.outWidth
111                            + " h=" + options.outHeight);
112
113                    // how much does the image differ from our preference?
114                    double widthRatio = mDesiredMinWidth / options.outWidth;
115                    double heightRatio = mDesiredMinHeight / options.outHeight;
116                    if (widthRatio > 0.8 && widthRatio < 1.25
117                            && heightRatio > 0.8 && heightRatio < 1.25) {
118                        // sufficiently close to our resolution; go ahead and use it
119                        if (DEBUG) Slog.d(TAG, "wallpaper dimension match; using");
120                        f.renameTo(new File(WALLPAPER_IMAGE));
121                        // TODO: spin a service to copy the restored image to sd/usb storage,
122                        // since it does not exist anywhere other than the private wallpaper
123                        // file.
124                    } else {
125                        if (DEBUG) Slog.d(TAG, "dimensions too far off: wr=" + widthRatio
126                                + " hr=" + heightRatio);
127                        f.delete();
128                    }
129                }
130            } else {
131                // Some other normal file; just decode it to its destination
132                File f = new File(key);
133                writeFile(f, data);
134            }
135        }
136    }
137}
138