File: WallpaperBackupHelper.java

package info (click to toggle)
android-platform-frameworks-base 1%3A10.0.0%2Br36-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 321,788 kB
  • sloc: java: 962,234; cpp: 274,314; xml: 242,770; python: 5,060; sh: 1,432; ansic: 494; makefile: 47; sed: 19
file content (113 lines) | stat: -rw-r--r-- 4,153 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.backup;

import android.app.WallpaperManager;
import android.content.Context;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.util.Slog;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * We no longer back up wallpapers with this helper, but we do need to process restores
 * of legacy backup payloads.  We just take the restored image as-is and apply it as the
 * system wallpaper using the public "set the wallpaper" API.
 *
 * @hide
 */
public class WallpaperBackupHelper extends FileBackupHelperBase implements BackupHelper {
    private static final String TAG = "WallpaperBackupHelper";
    private static final boolean DEBUG = false;

    // Key that legacy wallpaper imagery was stored under
    public static final String WALLPAPER_IMAGE_KEY =
            "/data/data/com.android.settings/files/wallpaper";
    public static final String WALLPAPER_INFO_KEY = "/data/system/wallpaper_info.xml";

    // Stage file that the restored imagery is stored to prior to being applied
    // as the system wallpaper.
    private static final String STAGE_FILE =
            new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
                    "wallpaper-tmp").getAbsolutePath();

    private final String[] mKeys;
    private final WallpaperManager mWpm;

    /**
     * Legacy wallpaper restores, from back when the imagery was stored under the
     * "android" system package as file key/value entities.
     *
     * @param context
     * @param files
     */
    public WallpaperBackupHelper(Context context, String[] keys) {
        super(context);

        mContext = context;
        mKeys = keys;

        mWpm = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
    }

    /**
     * Based on oldState, determine which of the files from the application's data directory
     * need to be backed up, write them to the data stream, and fill in newState with the
     * state as it exists now.
     */
    @Override
    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState) {
        // Intentionally no-op; we don't back up the wallpaper this way any more.
    }

    /**
     * Restore one absolute file entity from the restore stream.  If we're restoring the
     * magic wallpaper file, apply it as the system wallpaper.
     */
    @Override
    public void restoreEntity(BackupDataInputStream data) {
        if (mWpm == null) {
            Slog.w(TAG, "restoreEntity(): no wallpaper service");
            return;
        }
        final String key = data.getKey();
        if (isKeyInList(key, mKeys)) {
            if (key.equals(WALLPAPER_IMAGE_KEY)) {
                // restore the file to the stage for inspection
                File stage = new File(STAGE_FILE);
                try {
                    if (writeFile(stage, data)) {
                        try (FileInputStream in = new FileInputStream(stage)) {
                            mWpm.setStream(in);
                        } catch (IOException e) {
                            Slog.e(TAG, "Unable to set restored wallpaper: " + e.getMessage());
                        }
                    } else {
                        Slog.e(TAG, "Unable to save restored wallpaper");
                    }
                } finally {
                    stage.delete();
                }
            }
        }
    }
}