Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package com.android.wallpaperbackup; |
| 18 | |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 19 | import static android.app.WallpaperManager.FLAG_LOCK; |
| 20 | import static android.app.WallpaperManager.FLAG_SYSTEM; |
| 21 | |
Christopher Tate | 2471a37 | 2016-08-24 16:23:20 -0700 | [diff] [blame] | 22 | import android.app.AppGlobals; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 23 | import android.app.WallpaperManager; |
| 24 | import android.app.backup.BackupAgent; |
| 25 | import android.app.backup.BackupDataInput; |
| 26 | import android.app.backup.BackupDataOutput; |
| 27 | import android.app.backup.FullBackupDataOutput; |
Christopher Tate | 2471a37 | 2016-08-24 16:23:20 -0700 | [diff] [blame] | 28 | import android.content.ComponentName; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 30 | import android.content.SharedPreferences; |
Christopher Tate | 2471a37 | 2016-08-24 16:23:20 -0700 | [diff] [blame] | 31 | import android.content.pm.IPackageManager; |
| 32 | import android.content.pm.PackageInfo; |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 33 | import android.graphics.Rect; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 34 | import android.os.Environment; |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 35 | import android.os.FileUtils; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 36 | import android.os.ParcelFileDescriptor; |
Christopher Tate | 2471a37 | 2016-08-24 16:23:20 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 38 | import android.os.UserHandle; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 39 | import android.util.Slog; |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 40 | import android.util.Xml; |
| 41 | |
Christopher Tate | f47eff7 | 2016-08-09 16:23:14 -0700 | [diff] [blame] | 42 | import libcore.io.IoUtils; |
| 43 | |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 44 | import org.xmlpull.v1.XmlPullParser; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 45 | |
| 46 | import java.io.File; |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 47 | import java.io.FileInputStream; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 48 | import java.io.FileOutputStream; |
| 49 | import java.io.IOException; |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 50 | import java.nio.charset.StandardCharsets; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 51 | |
| 52 | public class WallpaperBackupAgent extends BackupAgent { |
| 53 | private static final String TAG = "WallpaperBackup"; |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 54 | private static final boolean DEBUG = false; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 55 | |
| 56 | // NB: must be kept in sync with WallpaperManagerService but has no |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 57 | // compile-time visibility. |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 58 | |
| 59 | // Target filenames within the system's wallpaper directory |
| 60 | static final String WALLPAPER = "wallpaper_orig"; |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 61 | static final String WALLPAPER_LOCK = "wallpaper_lock_orig"; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 62 | static final String WALLPAPER_INFO = "wallpaper_info.xml"; |
| 63 | |
| 64 | // Names of our local-data stage files/links |
| 65 | static final String IMAGE_STAGE = "wallpaper-stage"; |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 66 | static final String LOCK_IMAGE_STAGE = "wallpaper-lock-stage"; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 67 | static final String INFO_STAGE = "wallpaper-info-stage"; |
| 68 | static final String EMPTY_SENTINEL = "empty"; |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 69 | static final String QUOTA_SENTINEL = "quota"; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 70 | |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 71 | // Not-for-backup bookkeeping |
| 72 | static final String PREFS_NAME = "wbprefs.xml"; |
| 73 | static final String SYSTEM_GENERATION = "system_gen"; |
| 74 | static final String LOCK_GENERATION = "lock_gen"; |
| 75 | |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 76 | private File mWallpaperInfo; // wallpaper metadata file |
| 77 | private File mWallpaperFile; // primary wallpaper image file |
| 78 | private File mLockWallpaperFile; // lock wallpaper image file |
| 79 | |
| 80 | // If this file exists, it means we exceeded our quota last time |
| 81 | private File mQuotaFile; |
| 82 | private boolean mQuotaExceeded; |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 83 | |
| 84 | private WallpaperManager mWm; |
| 85 | |
| 86 | @Override |
| 87 | public void onCreate() { |
| 88 | if (DEBUG) { |
| 89 | Slog.v(TAG, "onCreate()"); |
| 90 | } |
| 91 | |
| 92 | File wallpaperDir = Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM); |
| 93 | mWallpaperInfo = new File(wallpaperDir, WALLPAPER_INFO); |
| 94 | mWallpaperFile = new File(wallpaperDir, WALLPAPER); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 95 | mLockWallpaperFile = new File(wallpaperDir, WALLPAPER_LOCK); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 96 | mWm = (WallpaperManager) getSystemService(Context.WALLPAPER_SERVICE); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 97 | |
| 98 | mQuotaFile = new File(getFilesDir(), QUOTA_SENTINEL); |
| 99 | mQuotaExceeded = mQuotaFile.exists(); |
| 100 | if (DEBUG) { |
| 101 | Slog.v(TAG, "quota file " + mQuotaFile.getPath() + " exists=" + mQuotaExceeded); |
| 102 | } |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onFullBackup(FullBackupDataOutput data) throws IOException { |
| 107 | // To avoid data duplication and disk churn, use links as the stage. |
| 108 | final File filesDir = getFilesDir(); |
| 109 | final File infoStage = new File(filesDir, INFO_STAGE); |
| 110 | final File imageStage = new File (filesDir, IMAGE_STAGE); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 111 | final File lockImageStage = new File (filesDir, LOCK_IMAGE_STAGE); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 112 | final File empty = new File (filesDir, EMPTY_SENTINEL); |
| 113 | |
| 114 | try { |
| 115 | // We always back up this 'empty' file to ensure that the absence of |
| 116 | // storable wallpaper imagery still produces a non-empty backup data |
| 117 | // stream, otherwise it'd simply be ignored in preflight. |
| 118 | FileOutputStream touch = new FileOutputStream(empty); |
| 119 | touch.close(); |
| 120 | fullBackupFile(empty, data); |
| 121 | |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 122 | SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
| 123 | final int lastSysGeneration = prefs.getInt(SYSTEM_GENERATION, -1); |
| 124 | final int lastLockGeneration = prefs.getInt(LOCK_GENERATION, -1); |
Christopher Tate | 5cb5e89 | 2016-06-17 13:24:02 -0700 | [diff] [blame] | 125 | |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 126 | final int sysGeneration = |
| 127 | mWm.getWallpaperIdForUser(FLAG_SYSTEM, UserHandle.USER_SYSTEM); |
| 128 | final int lockGeneration = |
| 129 | mWm.getWallpaperIdForUser(FLAG_LOCK, UserHandle.USER_SYSTEM); |
| 130 | final boolean sysChanged = (sysGeneration != lastSysGeneration); |
| 131 | final boolean lockChanged = (lockGeneration != lastLockGeneration); |
Christopher Tate | 5cb5e89 | 2016-06-17 13:24:02 -0700 | [diff] [blame] | 132 | |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 133 | final boolean sysEligible = mWm.isWallpaperBackupEligible(FLAG_SYSTEM); |
| 134 | final boolean lockEligible = mWm.isWallpaperBackupEligible(FLAG_LOCK); |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 135 | |
Christopher Tate | f47eff7 | 2016-08-09 16:23:14 -0700 | [diff] [blame] | 136 | // There might be a latent lock wallpaper file present but unused: don't |
| 137 | // include it in the backup if that's the case. |
| 138 | ParcelFileDescriptor lockFd = mWm.getWallpaperFile(FLAG_LOCK, UserHandle.USER_SYSTEM); |
| 139 | final boolean hasLockWallpaper = (lockFd != null); |
| 140 | IoUtils.closeQuietly(lockFd); |
| 141 | |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 142 | if (DEBUG) { |
| 143 | Slog.v(TAG, "sysGen=" + sysGeneration + " : sysChanged=" + sysChanged); |
| 144 | Slog.v(TAG, "lockGen=" + lockGeneration + " : lockChanged=" + lockChanged); |
| 145 | Slog.v(TAG, "sysEligble=" + sysEligible); |
| 146 | Slog.v(TAG, "lockEligible=" + lockEligible); |
| 147 | } |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 148 | |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 149 | // only back up the wallpapers if we've been told they're eligible |
| 150 | if ((sysEligible || lockEligible) && mWallpaperInfo.exists()) { |
| 151 | if (sysChanged || lockChanged || !infoStage.exists()) { |
| 152 | if (DEBUG) Slog.v(TAG, "New wallpaper configuration; copying"); |
| 153 | FileUtils.copyFileOrThrow(mWallpaperInfo, infoStage); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 154 | } |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 155 | fullBackupFile(infoStage, data); |
| 156 | } |
| 157 | if (sysEligible && mWallpaperFile.exists()) { |
| 158 | if (sysChanged || !imageStage.exists()) { |
| 159 | if (DEBUG) Slog.v(TAG, "New system wallpaper; copying"); |
| 160 | FileUtils.copyFileOrThrow(mWallpaperFile, imageStage); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 161 | } |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 162 | fullBackupFile(imageStage, data); |
| 163 | prefs.edit().putInt(SYSTEM_GENERATION, sysGeneration).apply(); |
| 164 | } |
| 165 | |
| 166 | // Don't try to store the lock image if we overran our quota last time |
| 167 | if (lockEligible && hasLockWallpaper && mLockWallpaperFile.exists() && !mQuotaExceeded) { |
| 168 | if (lockChanged || !lockImageStage.exists()) { |
| 169 | if (DEBUG) Slog.v(TAG, "New lock wallpaper; copying"); |
| 170 | FileUtils.copyFileOrThrow(mLockWallpaperFile, lockImageStage); |
| 171 | } |
| 172 | fullBackupFile(lockImageStage, data); |
| 173 | prefs.edit().putInt(LOCK_GENERATION, lockGeneration).apply(); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 174 | } |
| 175 | } catch (Exception e) { |
Christopher Tate | 5f829d2 | 2016-07-12 17:22:17 -0700 | [diff] [blame] | 176 | Slog.e(TAG, "Unable to back up wallpaper", e); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 177 | } finally { |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 178 | // Even if this time we had to back off on attempting to store the lock image |
| 179 | // due to exceeding the data quota, try again next time. This will alternate |
| 180 | // between "try both" and "only store the primary image" until either there |
| 181 | // is no lock image to store, or the quota is raised, or both fit under the |
| 182 | // quota. |
| 183 | mQuotaFile.delete(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void onQuotaExceeded(long backupDataBytes, long quotaBytes) { |
| 189 | if (DEBUG) { |
| 190 | Slog.i(TAG, "Quota exceeded (" + backupDataBytes + " vs " + quotaBytes + ')'); |
| 191 | } |
| 192 | try (FileOutputStream f = new FileOutputStream(mQuotaFile)) { |
| 193 | f.write(0); |
| 194 | } catch (Exception e) { |
| 195 | Slog.w(TAG, "Unable to record quota-exceeded: " + e.getMessage()); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | // We use the default onRestoreFile() implementation that will recreate our stage files, |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 200 | // then post-process in onRestoreFinished() to apply the new wallpaper. |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 201 | @Override |
| 202 | public void onRestoreFinished() { |
| 203 | if (DEBUG) { |
| 204 | Slog.v(TAG, "onRestoreFinished()"); |
| 205 | } |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 206 | final File filesDir = getFilesDir(); |
| 207 | final File infoStage = new File(filesDir, INFO_STAGE); |
| 208 | final File imageStage = new File (filesDir, IMAGE_STAGE); |
| 209 | final File lockImageStage = new File (filesDir, LOCK_IMAGE_STAGE); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 210 | |
Christopher Tate | f47eff7 | 2016-08-09 16:23:14 -0700 | [diff] [blame] | 211 | // If we restored separate lock imagery, the system wallpaper should be |
| 212 | // applied as system-only; but if there's no separate lock image, make |
| 213 | // sure to apply the restored system wallpaper as both. |
| 214 | final int sysWhich = FLAG_SYSTEM | (lockImageStage.exists() ? 0 : FLAG_LOCK); |
| 215 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 216 | try { |
Christopher Tate | 5f829d2 | 2016-07-12 17:22:17 -0700 | [diff] [blame] | 217 | // First off, revert to the factory state |
Christopher Tate | f47eff7 | 2016-08-09 16:23:14 -0700 | [diff] [blame] | 218 | mWm.clear(FLAG_SYSTEM | FLAG_LOCK); |
Christopher Tate | 5f829d2 | 2016-07-12 17:22:17 -0700 | [diff] [blame] | 219 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 220 | // It is valid for the imagery to be absent; it means that we were not permitted |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 221 | // to back up the original image on the source device, or there was no user-supplied |
| 222 | // wallpaper image present. |
Christopher Tate | f47eff7 | 2016-08-09 16:23:14 -0700 | [diff] [blame] | 223 | restoreFromStage(imageStage, infoStage, "wp", sysWhich); |
| 224 | restoreFromStage(lockImageStage, infoStage, "kwp", FLAG_LOCK); |
Christopher Tate | 2471a37 | 2016-08-24 16:23:20 -0700 | [diff] [blame] | 225 | |
| 226 | // And reset to the wallpaper service we should be using |
| 227 | ComponentName wpService = parseWallpaperComponent(infoStage, "wp"); |
| 228 | if (servicePackageExists(wpService)) { |
| 229 | if (DEBUG) { |
| 230 | Slog.i(TAG, "Using wallpaper service " + wpService); |
| 231 | } |
| 232 | mWm.setWallpaperComponent(wpService, UserHandle.USER_SYSTEM); |
| 233 | } else { |
| 234 | if (DEBUG) { |
| 235 | Slog.v(TAG, "Can't use wallpaper service " + wpService); |
| 236 | } |
| 237 | } |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 238 | } catch (Exception e) { |
| 239 | Slog.e(TAG, "Unable to restore wallpaper: " + e.getMessage()); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 240 | } finally { |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 241 | if (DEBUG) { |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 242 | Slog.v(TAG, "Restore finished; clearing backup bookkeeping"); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 243 | } |
| 244 | infoStage.delete(); |
| 245 | imageStage.delete(); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 246 | lockImageStage.delete(); |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 247 | |
| 248 | SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
| 249 | prefs.edit() |
| 250 | .putInt(SYSTEM_GENERATION, -1) |
| 251 | .putInt(LOCK_GENERATION, -1) |
| 252 | .commit(); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 256 | private void restoreFromStage(File stage, File info, String hintTag, int which) |
| 257 | throws IOException { |
| 258 | if (stage.exists()) { |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 259 | // Parse the restored info file to find the crop hint. Note that this currently |
| 260 | // relies on a priori knowledge of the wallpaper info file schema. |
| 261 | Rect cropHint = parseCropHint(info, hintTag); |
| 262 | if (cropHint != null) { |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 263 | Slog.i(TAG, "Got restored wallpaper; applying which=" + which); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 264 | if (DEBUG) { |
Christopher Tate | ac482eb | 2016-07-18 17:31:02 -0700 | [diff] [blame] | 265 | Slog.v(TAG, "Restored crop hint " + cropHint); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 266 | } |
| 267 | try (FileInputStream in = new FileInputStream(stage)) { |
Christopher Tate | dd7110d | 2016-08-08 13:09:02 -0700 | [diff] [blame] | 268 | mWm.setStream(in, cropHint.isEmpty() ? null : cropHint, true, which); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 269 | } finally {} // auto-closes 'in' |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | private Rect parseCropHint(File wallpaperInfo, String sectionTag) { |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 275 | Rect cropHint = new Rect(); |
| 276 | try (FileInputStream stream = new FileInputStream(wallpaperInfo)) { |
| 277 | XmlPullParser parser = Xml.newPullParser(); |
| 278 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
| 279 | |
| 280 | int type; |
| 281 | do { |
| 282 | type = parser.next(); |
| 283 | if (type == XmlPullParser.START_TAG) { |
| 284 | String tag = parser.getName(); |
Christopher Tate | bf13ccf | 2016-06-28 16:38:40 -0700 | [diff] [blame] | 285 | if (sectionTag.equals(tag)) { |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 286 | cropHint.left = getAttributeInt(parser, "cropLeft", 0); |
| 287 | cropHint.top = getAttributeInt(parser, "cropTop", 0); |
| 288 | cropHint.right = getAttributeInt(parser, "cropRight", 0); |
| 289 | cropHint.bottom = getAttributeInt(parser, "cropBottom", 0); |
| 290 | } |
| 291 | } |
| 292 | } while (type != XmlPullParser.END_DOCUMENT); |
| 293 | } catch (Exception e) { |
| 294 | // Whoops; can't process the info file at all. Report failure. |
Christopher Tate | 2471a37 | 2016-08-24 16:23:20 -0700 | [diff] [blame] | 295 | Slog.w(TAG, "Failed to parse restored crop: " + e.getMessage()); |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 296 | return null; |
| 297 | } |
| 298 | |
| 299 | return cropHint; |
| 300 | } |
| 301 | |
Christopher Tate | 2471a37 | 2016-08-24 16:23:20 -0700 | [diff] [blame] | 302 | private ComponentName parseWallpaperComponent(File wallpaperInfo, String sectionTag) { |
| 303 | ComponentName name = null; |
| 304 | try (FileInputStream stream = new FileInputStream(wallpaperInfo)) { |
| 305 | final XmlPullParser parser = Xml.newPullParser(); |
| 306 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
| 307 | |
| 308 | int type; |
| 309 | do { |
| 310 | type = parser.next(); |
| 311 | if (type == XmlPullParser.START_TAG) { |
| 312 | String tag = parser.getName(); |
| 313 | if (sectionTag.equals(tag)) { |
| 314 | final String parsedName = parser.getAttributeValue(null, "component"); |
| 315 | name = (parsedName != null) |
| 316 | ? ComponentName.unflattenFromString(parsedName) |
| 317 | : null; |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | } while (type != XmlPullParser.END_DOCUMENT); |
| 322 | } catch (Exception e) { |
| 323 | // Whoops; can't process the info file at all. Report failure. |
| 324 | Slog.w(TAG, "Failed to parse restored component: " + e.getMessage()); |
| 325 | return null; |
| 326 | } |
| 327 | return name; |
| 328 | } |
| 329 | |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 330 | private int getAttributeInt(XmlPullParser parser, String name, int defValue) { |
| 331 | final String value = parser.getAttributeValue(null, name); |
| 332 | return (value == null) ? defValue : Integer.parseInt(value); |
| 333 | } |
| 334 | |
Christopher Tate | 2471a37 | 2016-08-24 16:23:20 -0700 | [diff] [blame] | 335 | private boolean servicePackageExists(ComponentName comp) { |
| 336 | try { |
| 337 | if (comp != null) { |
| 338 | final IPackageManager pm = AppGlobals.getPackageManager(); |
| 339 | final PackageInfo info = pm.getPackageInfo(comp.getPackageName(), |
| 340 | 0, UserHandle.USER_SYSTEM); |
| 341 | return (info != null); |
| 342 | } |
| 343 | } catch (RemoteException e) { |
| 344 | Slog.e(TAG, "Unable to contact package manager"); |
| 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 349 | // |
Christopher Tate | 61f2d63 | 2016-04-27 15:47:13 -0700 | [diff] [blame] | 350 | // Key/value API: abstract, therefore required; but not used |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 351 | // |
| 352 | |
| 353 | @Override |
| 354 | public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 355 | ParcelFileDescriptor newState) throws IOException { |
| 356 | // Intentionally blank |
| 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 361 | throws IOException { |
| 362 | // Intentionally blank |
| 363 | } |
| 364 | } |