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