13f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate/*
23f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Copyright (C) 2010 The Android Open Source Project
33f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate *
43f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
53f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * you may not use this file except in compliance with the License.
63f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * You may obtain a copy of the License at
73f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate *
83f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
93f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate *
103f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Unless required by applicable law or agreed to in writing, software
113f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
123f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * See the License for the specific language governing permissions and
143f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * limitations under the License.
153f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate */
163f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
173f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tatepackage android.app.backup;
183f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
193f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.app.WallpaperManager;
203f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.content.Context;
213f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.graphics.BitmapFactory;
2244bc17c6b517aef35a390c81b5aa79c4f284f744Dianne Hackbornimport android.graphics.Point;
2361f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasaniimport android.os.Environment;
243f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.os.ParcelFileDescriptor;
2561f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasaniimport android.os.UserHandle;
263f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport android.util.Slog;
277f765cf588e37a70fa2a1d251aaa8e7b847801b6Christopher Tateimport android.view.Display;
287f765cf588e37a70fa2a1d251aaa8e7b847801b6Christopher Tateimport android.view.WindowManager;
293f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
303f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tateimport java.io.File;
313f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
323f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate/**
333f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * Helper for backing up / restoring wallpapers.  Basically an AbsoluteFileBackupHelper,
343f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * but with logic for deciding what to do with restored wallpaper images.
353f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate *
363f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate * @hide
373f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate */
383f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tatepublic class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper {
393f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    private static final String TAG = "WallpaperBackupHelper";
40bf6ee4f509cbe7a44f4cc72f28e6150ca47c066dChristopher Tate    private static final boolean DEBUG = false;
413f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
423f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    // This path must match what the WallpaperManagerService uses
4337ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani    // TODO: Will need to change if backing up non-primary user's wallpaper
4461f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani    public static final String WALLPAPER_IMAGE =
4561f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani            new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
4661f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani                    "wallpaper").getAbsolutePath();
4761f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani    public static final String WALLPAPER_INFO =
4861f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani            new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
4961f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani                    "wallpaper_info.xml").getAbsolutePath();
5037ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani    // Use old keys to keep legacy data compatibility and avoid writing two wallpapers
5137ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani    public static final String WALLPAPER_IMAGE_KEY =
5237ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani            "/data/data/com.android.settings/files/wallpaper";
5337ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani    public static final String WALLPAPER_INFO_KEY = "/data/system/wallpaper_info.xml";
543f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
553f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    // Stage file - should be adjacent to the WALLPAPER_IMAGE location.  The wallpapers
563f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    // will be saved to this file from the restore stream, then renamed to the proper
573f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    // location if it's deemed suitable.
5837ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani    // TODO: Will need to change if backing up non-primary user's wallpaper
5961f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani    private static final String STAGE_FILE =
6061f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani            new File(Environment.getUserSystemDirectory(UserHandle.USER_OWNER),
6161f57379ca2c5b6290c8da7548fa17128f7ab24fAmith Yamasani                    "wallpaper-tmp").getAbsolutePath();
623f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
633f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    Context mContext;
643f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    String[] mFiles;
6537ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani    String[] mKeys;
663f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    double mDesiredMinWidth;
673f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    double mDesiredMinHeight;
683f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
693f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    /**
703f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * Construct a helper for backing up / restoring the files at the given absolute locations
713f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * within the file system.
723f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     *
733f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * @param context
743f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * @param files
753f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     */
7637ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani    public WallpaperBackupHelper(Context context, String[] files, String[] keys) {
773f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        super(context);
783f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
793f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        mContext = context;
803f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        mFiles = files;
8137ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani        mKeys = keys;
823f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
833f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        WallpaperManager wpm;
843f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        wpm = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
853f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        mDesiredMinWidth = (double) wpm.getDesiredMinimumWidth();
863f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        mDesiredMinHeight = (double) wpm.getDesiredMinimumHeight();
8700724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate
887f765cf588e37a70fa2a1d251aaa8e7b847801b6Christopher Tate        if (mDesiredMinWidth <= 0 || mDesiredMinHeight <= 0) {
897f765cf588e37a70fa2a1d251aaa8e7b847801b6Christopher Tate            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
907f765cf588e37a70fa2a1d251aaa8e7b847801b6Christopher Tate            Display d = wm.getDefaultDisplay();
9144bc17c6b517aef35a390c81b5aa79c4f284f744Dianne Hackborn            Point size = new Point();
9244bc17c6b517aef35a390c81b5aa79c4f284f744Dianne Hackborn            d.getSize(size);
9344bc17c6b517aef35a390c81b5aa79c4f284f744Dianne Hackborn            mDesiredMinWidth = size.x;
9444bc17c6b517aef35a390c81b5aa79c4f284f744Dianne Hackborn            mDesiredMinHeight = size.y;
957f765cf588e37a70fa2a1d251aaa8e7b847801b6Christopher Tate        }
967f765cf588e37a70fa2a1d251aaa8e7b847801b6Christopher Tate
9700724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate        if (DEBUG) {
9800724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate            Slog.d(TAG, "dmW=" + mDesiredMinWidth + " dmH=" + mDesiredMinHeight);
9900724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate        }
1003f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    }
1013f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
1023f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    /**
1033f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * Based on oldState, determine which of the files from the application's data directory
1043f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * need to be backed up, write them to the data stream, and fill in newState with the
1053f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * state as it exists now.
1063f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     */
1073f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
1083f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate            ParcelFileDescriptor newState) {
10937ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani        performBackup_checked(oldState, data, newState, mFiles, mKeys);
1103f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    }
1113f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
1123f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    /**
1133f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * Restore one absolute file entity from the restore stream.  If we're restoring the
1143f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * magic wallpaper file, take specific action to determine whether it is suitable for
1153f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     * the current device.
1163f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate     */
1173f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    public void restoreEntity(BackupDataInputStream data) {
1183f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        final String key = data.getKey();
11937ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani        if (isKeyInList(key, mKeys)) {
12037ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani            if (key.equals(WALLPAPER_IMAGE_KEY)) {
1213f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                // restore the file to the stage for inspection
1223f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                File f = new File(STAGE_FILE);
1233f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                if (writeFile(f, data)) {
1243f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
1253f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                    // Preflight the restored image's dimensions without loading it
1263f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                    BitmapFactory.Options options = new BitmapFactory.Options();
1273f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                    options.inJustDecodeBounds = true;
1283f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                    BitmapFactory.decodeFile(STAGE_FILE, options);
1293f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
13000724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate                    if (DEBUG) Slog.d(TAG, "Restoring wallpaper image w=" + options.outWidth
1313f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                            + " h=" + options.outHeight);
1323f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate
1339b95a4fcdce4b1a7a418e36de2ddc4d2c3acfc69Christopher Tate                    // How much does the image differ from our preference?  The threshold
1349b95a4fcdce4b1a7a418e36de2ddc4d2c3acfc69Christopher Tate                    // here is set to accept any image larger than our target, because
1359b95a4fcdce4b1a7a418e36de2ddc4d2c3acfc69Christopher Tate                    // scaling down is acceptable; but to reject images that are deemed
1369b95a4fcdce4b1a7a418e36de2ddc4d2c3acfc69Christopher Tate                    // "too small" to scale up attractively.  The value 1.33 is just barely
1379b95a4fcdce4b1a7a418e36de2ddc4d2c3acfc69Christopher Tate                    // too low to pass Nexus 1 or Droid wallpapers for use on a Xoom, but
1389b95a4fcdce4b1a7a418e36de2ddc4d2c3acfc69Christopher Tate                    // will pass anything relatively larger.
1393f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                    double widthRatio = mDesiredMinWidth / options.outWidth;
1403f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                    double heightRatio = mDesiredMinHeight / options.outHeight;
1419b95a4fcdce4b1a7a418e36de2ddc4d2c3acfc69Christopher Tate                    if (widthRatio > 0 && widthRatio < 1.33
1429b95a4fcdce4b1a7a418e36de2ddc4d2c3acfc69Christopher Tate                            && heightRatio > 0 && heightRatio < 1.33) {
1433f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                        // sufficiently close to our resolution; go ahead and use it
14400724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate                        if (DEBUG) Slog.d(TAG, "wallpaper dimension match; using");
1453f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                        f.renameTo(new File(WALLPAPER_IMAGE));
1463f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                        // TODO: spin a service to copy the restored image to sd/usb storage,
1473f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                        // since it does not exist anywhere other than the private wallpaper
1483f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                        // file.
1493f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                    } else {
15000724cacc8292900c3be8657ea9b3b6284cf4877Christopher Tate                        if (DEBUG) Slog.d(TAG, "dimensions too far off: wr=" + widthRatio
1513f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                                + " hr=" + heightRatio);
1523f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                        f.delete();
1533f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                    }
1543f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                }
15537ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani            } else if (key.equals(WALLPAPER_INFO_KEY)) {
15637ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani                // XML file containing wallpaper info
15737ce3a8af6faab675319d0803b288ab1dddc76beAmith Yamasani                File f = new File(WALLPAPER_INFO);
1583f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate                writeFile(f, data);
1593f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate            }
1603f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate        }
1613f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate    }
1623f64f8d8fc05189777e83b4efd3882cbc661fdebChristopher Tate}
163