blob: f2097366afcd06daa2d3e12fd202e49289a56bed [file] [log] [blame]
Chris Wren1ada10d2013-09-13 18:01:38 -04001/*
2 * Copyright (C) 2013 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 */
Chris Wren1ada10d2013-09-13 18:01:38 -040016package com.android.launcher3;
17
Chris Wren92aa4232013-10-04 11:29:36 -040018import android.app.backup.BackupDataInputStream;
Chris Wren1ada10d2013-09-13 18:01:38 -040019import android.app.backup.BackupDataOutput;
Chris Wren4d89e2a2013-10-09 17:03:50 -040020import android.app.backup.BackupHelper;
Chris Wren1ada10d2013-09-13 18:01:38 -040021import android.app.backup.BackupManager;
Chris Wren22e130d2013-09-23 18:25:57 -040022import android.content.ComponentName;
Chris Wren1ada10d2013-09-13 18:01:38 -040023import android.content.ContentResolver;
Chris Wren5dee7af2013-12-20 17:22:11 -050024import android.content.ContentValues;
Chris Wren1ada10d2013-09-13 18:01:38 -040025import android.content.Context;
Chris Wren22e130d2013-09-23 18:25:57 -040026import android.content.Intent;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -080027import android.content.pm.ActivityInfo;
28import android.content.pm.PackageManager;
Sunny Goyalef728d42014-10-22 11:28:28 -070029import android.content.pm.PackageManager.NameNotFoundException;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -080030import android.content.pm.ResolveInfo;
31import android.content.res.XmlResourceParser;
Chris Wren1ada10d2013-09-13 18:01:38 -040032import android.database.Cursor;
Chris Wren22e130d2013-09-23 18:25:57 -040033import android.graphics.Bitmap;
34import android.graphics.BitmapFactory;
Chris Wrenfd13c712013-09-27 15:45:19 -040035import android.graphics.drawable.Drawable;
Chris Wren1ada10d2013-09-13 18:01:38 -040036import android.os.ParcelFileDescriptor;
Chris Wren1ada10d2013-09-13 18:01:38 -040037import android.text.TextUtils;
38import android.util.Base64;
39import android.util.Log;
40
Sunny Goyalef728d42014-10-22 11:28:28 -070041import com.android.launcher3.LauncherSettings.Favorites;
42import com.android.launcher3.LauncherSettings.WorkspaceScreens;
43import com.android.launcher3.backup.BackupProtos;
44import com.android.launcher3.backup.BackupProtos.CheckedMessage;
Sunny Goyal33d44382014-10-16 09:24:19 -070045import com.android.launcher3.backup.BackupProtos.DeviceProfieData;
Sunny Goyalef728d42014-10-22 11:28:28 -070046import com.android.launcher3.backup.BackupProtos.Favorite;
47import com.android.launcher3.backup.BackupProtos.Journal;
48import com.android.launcher3.backup.BackupProtos.Key;
49import com.android.launcher3.backup.BackupProtos.Resource;
50import com.android.launcher3.backup.BackupProtos.Screen;
51import com.android.launcher3.backup.BackupProtos.Widget;
52import com.android.launcher3.compat.UserHandleCompat;
53import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal316490e2015-06-02 09:38:28 -070054import com.android.launcher3.util.Thunk;
Sunny Goyalef728d42014-10-22 11:28:28 -070055import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
56import com.google.protobuf.nano.MessageNano;
57
Sunny Goyalbb3b02f2015-01-15 12:00:14 -080058import org.xmlpull.v1.XmlPullParser;
59import org.xmlpull.v1.XmlPullParserException;
60
Chris Wren1ada10d2013-09-13 18:01:38 -040061import java.io.FileInputStream;
62import java.io.FileOutputStream;
63import java.io.IOException;
Chris Wren22e130d2013-09-23 18:25:57 -040064import java.net.URISyntaxException;
Chris Wren1ada10d2013-09-13 18:01:38 -040065import java.util.ArrayList;
Sunny Goyalc0ee6752015-01-16 14:10:32 -080066import java.util.Arrays;
Chris Wren1ada10d2013-09-13 18:01:38 -040067import java.util.HashSet;
Chris Wren1ada10d2013-09-13 18:01:38 -040068import java.util.zip.CRC32;
69
70/**
71 * Persist the launcher home state across calamities.
72 */
Chris Wren92aa4232013-10-04 11:29:36 -040073public class LauncherBackupHelper implements BackupHelper {
Chris Wren92aa4232013-10-04 11:29:36 -040074 private static final String TAG = "LauncherBackupHelper";
Chris Wren50c8f422014-01-15 16:10:39 -050075 private static final boolean VERBOSE = LauncherBackupAgentHelper.VERBOSE;
76 private static final boolean DEBUG = LauncherBackupAgentHelper.DEBUG;
Chris Wren1ada10d2013-09-13 18:01:38 -040077
Sunny Goyal33d44382014-10-16 09:24:19 -070078 private static final int BACKUP_VERSION = 2;
Chris Wren1ada10d2013-09-13 18:01:38 -040079 private static final int MAX_JOURNAL_SIZE = 1000000;
80
Sunny Goyal33d44382014-10-16 09:24:19 -070081 // Journal key is such that it is always smaller than any dynamically generated
82 // key (any Base64 encoded string).
83 private static final String JOURNAL_KEY = "#";
84
Chris Wrenfd13c712013-09-27 15:45:19 -040085 /** icons are large, dribble them out */
Chris Wren22e130d2013-09-23 18:25:57 -040086 private static final int MAX_ICONS_PER_PASS = 10;
87
Chris Wrenfd13c712013-09-27 15:45:19 -040088 /** widgets contain previews, which are very large, dribble them out */
89 private static final int MAX_WIDGETS_PER_PASS = 5;
90
Chris Wren1ada10d2013-09-13 18:01:38 -040091 private static final String[] FAVORITE_PROJECTION = {
Sunny Goyalef728d42014-10-22 11:28:28 -070092 Favorites._ID, // 0
93 Favorites.MODIFIED, // 1
94 Favorites.INTENT, // 2
95 Favorites.APPWIDGET_PROVIDER, // 3
96 Favorites.APPWIDGET_ID, // 4
97 Favorites.CELLX, // 5
98 Favorites.CELLY, // 6
99 Favorites.CONTAINER, // 7
100 Favorites.ICON, // 8
101 Favorites.ICON_PACKAGE, // 9
102 Favorites.ICON_RESOURCE, // 10
103 Favorites.ICON_TYPE, // 11
104 Favorites.ITEM_TYPE, // 12
105 Favorites.SCREEN, // 13
106 Favorites.SPANX, // 14
107 Favorites.SPANY, // 15
108 Favorites.TITLE, // 16
109 Favorites.PROFILE_ID, // 17
Chris Wren1ada10d2013-09-13 18:01:38 -0400110 };
111
112 private static final int ID_INDEX = 0;
Chris Wren22e130d2013-09-23 18:25:57 -0400113 private static final int ID_MODIFIED = 1;
114 private static final int INTENT_INDEX = 2;
115 private static final int APPWIDGET_PROVIDER_INDEX = 3;
116 private static final int APPWIDGET_ID_INDEX = 4;
117 private static final int CELLX_INDEX = 5;
118 private static final int CELLY_INDEX = 6;
119 private static final int CONTAINER_INDEX = 7;
120 private static final int ICON_INDEX = 8;
121 private static final int ICON_PACKAGE_INDEX = 9;
122 private static final int ICON_RESOURCE_INDEX = 10;
123 private static final int ICON_TYPE_INDEX = 11;
124 private static final int ITEM_TYPE_INDEX = 12;
125 private static final int SCREEN_INDEX = 13;
126 private static final int SPANX_INDEX = 14;
127 private static final int SPANY_INDEX = 15;
128 private static final int TITLE_INDEX = 16;
Chris Wren1ada10d2013-09-13 18:01:38 -0400129
130 private static final String[] SCREEN_PROJECTION = {
Sunny Goyalef728d42014-10-22 11:28:28 -0700131 WorkspaceScreens._ID, // 0
132 WorkspaceScreens.MODIFIED, // 1
133 WorkspaceScreens.SCREEN_RANK // 2
Chris Wren1ada10d2013-09-13 18:01:38 -0400134 };
135
Chris Wren22e130d2013-09-23 18:25:57 -0400136 private static final int SCREEN_RANK_INDEX = 2;
Chris Wren1ada10d2013-09-13 18:01:38 -0400137
Sunny Goyal316490e2015-06-02 09:38:28 -0700138 @Thunk final Context mContext;
Sunny Goyalef728d42014-10-22 11:28:28 -0700139 private final HashSet<String> mExistingKeys;
Sunny Goyal42de82f2014-09-26 22:09:29 -0700140 private final ArrayList<Key> mKeys;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800141 private final ItemTypeMatcher[] mItemTypeMatchers;
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800142 private final long mUserSerial;
Chris Wren1ada10d2013-09-13 18:01:38 -0400143
Sunny Goyalef728d42014-10-22 11:28:28 -0700144 private BackupManager mBackupManager;
Sunny Goyalef728d42014-10-22 11:28:28 -0700145 private byte[] mBuffer = new byte[512];
146 private long mLastBackupRestoreTime;
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800147 private boolean mBackupDataWasUpdated;
Sunny Goyalef728d42014-10-22 11:28:28 -0700148
Adam Cohen2e6da152015-05-06 11:42:25 -0700149 private IconCache mIconCache;
150 private DeviceProfieData mDeviceProfileData;
151
Sunny Goyal33d44382014-10-16 09:24:19 -0700152 boolean restoreSuccessful;
Sunny Goyal08f72612015-01-05 13:41:43 -0800153 int restoredBackupVersion = 1;
Sunny Goyal33d44382014-10-16 09:24:19 -0700154
155 public LauncherBackupHelper(Context context) {
Chris Wren92aa4232013-10-04 11:29:36 -0400156 mContext = context;
Sunny Goyalef728d42014-10-22 11:28:28 -0700157 mExistingKeys = new HashSet<String>();
Sunny Goyal42de82f2014-09-26 22:09:29 -0700158 mKeys = new ArrayList<Key>();
Sunny Goyal33d44382014-10-16 09:24:19 -0700159 restoreSuccessful = true;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800160 mItemTypeMatchers = new ItemTypeMatcher[CommonAppTypeParser.SUPPORTED_TYPE_COUNT];
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800161
162 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
163 mUserSerial = userManager.getSerialNumberForUser(UserHandleCompat.myUserHandle());
Chris Wren92aa4232013-10-04 11:29:36 -0400164 }
165
166 private void dataChanged() {
Sunny Goyalef728d42014-10-22 11:28:28 -0700167 if (mBackupManager == null) {
168 mBackupManager = new BackupManager(mContext);
Chris Wren1ada10d2013-09-13 18:01:38 -0400169 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700170 mBackupManager.dataChanged();
171 }
172
173 private void applyJournal(Journal journal) {
174 mLastBackupRestoreTime = journal.t;
175 mExistingKeys.clear();
176 if (journal.key != null) {
177 for (Key key : journal.key) {
178 mExistingKeys.add(keyToBackupKey(key));
179 }
180 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400181 }
182
183 /**
184 * Back up launcher data so we can restore the user's state on a new device.
185 *
186 * <P>The journal is a timestamp and a list of keys that were saved as of that time.
187 *
188 * <P>Keys may come back in any order, so each key/value is one complete row of the database.
189 *
190 * @param oldState notes from the last backup
191 * @param data incremental key/value pairs to persist off-device
192 * @param newState notes for the next backup
Chris Wren1ada10d2013-09-13 18:01:38 -0400193 */
194 @Override
Chris Wren92aa4232013-10-04 11:29:36 -0400195 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
196 ParcelFileDescriptor newState) {
Chris Wren50c8f422014-01-15 16:10:39 -0500197 if (VERBOSE) Log.v(TAG, "onBackup");
Chris Wren1ada10d2013-09-13 18:01:38 -0400198
199 Journal in = readJournal(oldState);
Sunny Goyalef728d42014-10-22 11:28:28 -0700200 if (!launcherIsReady()) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700201 dataChanged();
Sunny Goyalef728d42014-10-22 11:28:28 -0700202 // Perform backup later.
203 writeJournal(newState, in);
204 return;
205 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700206
Sunny Goyal96373642015-06-05 11:14:01 -0700207 if (mDeviceProfileData == null) {
208 LauncherAppState app = LauncherAppState.getInstance();
209 mDeviceProfileData = initDeviceProfileData(app.getInvariantDeviceProfile());
210 mIconCache = app.getIconCache();
211 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700212
Sunny Goyalef728d42014-10-22 11:28:28 -0700213 Log.v(TAG, "lastBackupTime = " + in.t);
214 mKeys.clear();
215 applyJournal(in);
Chris Wren1ada10d2013-09-13 18:01:38 -0400216
Sunny Goyalef728d42014-10-22 11:28:28 -0700217 // Record the time before performing backup so that entries edited while the backup
218 // was going on, do not get missed in next backup.
219 long newBackupTime = System.currentTimeMillis();
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800220 mBackupDataWasUpdated = false;
Sunny Goyalef728d42014-10-22 11:28:28 -0700221 try {
222 backupFavorites(data);
223 backupScreens(data);
224 backupIcons(data);
225 backupWidgets(data);
Chris Wren1ada10d2013-09-13 18:01:38 -0400226
Sunny Goyalef728d42014-10-22 11:28:28 -0700227 // Delete any key which still exist in the old backup, but is not valid anymore.
228 HashSet<String> validKeys = new HashSet<String>();
229 for (Key key : mKeys) {
230 validKeys.add(keyToBackupKey(key));
Chris Wren71144262014-02-27 15:49:39 -0500231 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700232 mExistingKeys.removeAll(validKeys);
233
234 // Delete anything left in the existing keys.
235 for (String deleted: mExistingKeys) {
236 if (VERBOSE) Log.v(TAG, "dropping deleted item " + deleted);
237 data.writeEntityHeader(deleted, -1);
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800238 mBackupDataWasUpdated = true;
Sunny Goyalef728d42014-10-22 11:28:28 -0700239 }
240
241 mExistingKeys.clear();
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800242 if (!mBackupDataWasUpdated) {
243 // Check if any metadata has changed
244 mBackupDataWasUpdated = (in.profile == null)
245 || !Arrays.equals(DeviceProfieData.toByteArray(in.profile),
Sunny Goyal96373642015-06-05 11:14:01 -0700246 DeviceProfieData.toByteArray(mDeviceProfileData))
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800247 || (in.backupVersion != BACKUP_VERSION)
248 || (in.appVersion != getAppVersion());
249 }
Sunny Goyal33d44382014-10-16 09:24:19 -0700250
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800251 if (mBackupDataWasUpdated) {
252 mLastBackupRestoreTime = newBackupTime;
253
254 // We store the journal at two places.
255 // 1) Storing it in newState allows us to do partial backups by comparing old state
256 // 2) Storing it in backup data allows us to validate keys during restore
257 Journal state = getCurrentStateJournal();
258 writeRowToBackup(JOURNAL_KEY, state, data);
259 } else {
260 if (DEBUG) Log.d(TAG, "Nothing was written during backup");
261 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700262 } catch (IOException e) {
263 Log.e(TAG, "launcher backup has failed", e);
Chris Wren92aa4232013-10-04 11:29:36 -0400264 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400265
Sunny Goyalef728d42014-10-22 11:28:28 -0700266 writeNewStateDescription(newState);
Chris Wren1ada10d2013-09-13 18:01:38 -0400267 }
268
269 /**
Sunny Goyal33d44382014-10-16 09:24:19 -0700270 * @return true if the backup corresponding to oldstate can be successfully applied
271 * to this device.
272 */
273 private boolean isBackupCompatible(Journal oldState) {
Sunny Goyal96373642015-06-05 11:14:01 -0700274 DeviceProfieData currentProfile = mDeviceProfileData;
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700275 DeviceProfieData oldProfile = oldState.profile;
276
277 if (oldProfile == null || oldProfile.desktopCols == 0) {
278 // Profile info is not valid, ignore the check.
279 return true;
280 }
281
282 boolean isHotsetCompatible = false;
283 if (currentProfile.allappsRank >= oldProfile.hotseatCount) {
284 isHotsetCompatible = true;
285 }
286 if ((currentProfile.hotseatCount >= oldProfile.hotseatCount) &&
287 (currentProfile.allappsRank == oldProfile.allappsRank)) {
288 isHotsetCompatible = true;
289 }
290
291 return isHotsetCompatible && (currentProfile.desktopCols >= oldProfile.desktopCols)
292 && (currentProfile.desktopRows >= oldProfile.desktopRows);
Sunny Goyal33d44382014-10-16 09:24:19 -0700293 }
294
295 /**
Chris Wren92aa4232013-10-04 11:29:36 -0400296 * Restore launcher configuration from the restored data stream.
Sunny Goyal33d44382014-10-16 09:24:19 -0700297 * It assumes that the keys will arrive in lexical order. So if the journal was present in the
298 * backup, it should arrive first.
Chris Wren1ada10d2013-09-13 18:01:38 -0400299 *
Chris Wren92aa4232013-10-04 11:29:36 -0400300 * @param data the key/value pair from the server
Chris Wren1ada10d2013-09-13 18:01:38 -0400301 */
302 @Override
Chris Wren92aa4232013-10-04 11:29:36 -0400303 public void restoreEntity(BackupDataInputStream data) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700304 if (!restoreSuccessful) {
305 return;
306 }
Sunny Goyal96373642015-06-05 11:14:01 -0700307
308 if (mDeviceProfileData == null) {
309 // This call does not happen on a looper thread. So LauncherAppState
310 // can't be created . Instead initialize required dependencies directly.
311 InvariantDeviceProfile profile = new InvariantDeviceProfile(mContext);
312 mDeviceProfileData = initDeviceProfileData(profile);
313 mIconCache = new IconCache(mContext, profile);
314 }
Sunny Goyal33d44382014-10-16 09:24:19 -0700315
Sunny Goyalef728d42014-10-22 11:28:28 -0700316 int dataSize = data.size();
317 if (mBuffer.length < dataSize) {
318 mBuffer = new byte[dataSize];
Chris Wren92aa4232013-10-04 11:29:36 -0400319 }
320 try {
Sunny Goyalef728d42014-10-22 11:28:28 -0700321 int bytesRead = data.read(mBuffer, 0, dataSize);
322 if (DEBUG) Log.d(TAG, "read " + bytesRead + " of " + dataSize + " available");
323 String backupKey = data.getKey();
Sunny Goyal33d44382014-10-16 09:24:19 -0700324
325 if (JOURNAL_KEY.equals(backupKey)) {
326 if (VERBOSE) Log.v(TAG, "Journal entry restored");
327 if (!mKeys.isEmpty()) {
328 // We received the journal key after a restore key.
329 Log.wtf(TAG, keyToBackupKey(mKeys.get(0)) + " received after " + JOURNAL_KEY);
330 restoreSuccessful = false;
331 return;
332 }
333
334 Journal journal = new Journal();
335 MessageNano.mergeFrom(journal, readCheckedBytes(mBuffer, dataSize));
336 applyJournal(journal);
337 restoreSuccessful = isBackupCompatible(journal);
Sunny Goyal08f72612015-01-05 13:41:43 -0800338 restoredBackupVersion = journal.backupVersion;
Sunny Goyal33d44382014-10-16 09:24:19 -0700339 return;
340 }
341
342 if (!mExistingKeys.isEmpty() && !mExistingKeys.contains(backupKey)) {
343 if (DEBUG) Log.e(TAG, "Ignoring key not present in the backup state " + backupKey);
344 return;
345 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700346 Key key = backupKeyToKey(backupKey);
Chris Wren50c8f422014-01-15 16:10:39 -0500347 mKeys.add(key);
Chris Wren92aa4232013-10-04 11:29:36 -0400348 switch (key.type) {
349 case Key.FAVORITE:
Sunny Goyalef728d42014-10-22 11:28:28 -0700350 restoreFavorite(key, mBuffer, dataSize);
Chris Wren92aa4232013-10-04 11:29:36 -0400351 break;
352
353 case Key.SCREEN:
Sunny Goyalef728d42014-10-22 11:28:28 -0700354 restoreScreen(key, mBuffer, dataSize);
Chris Wren92aa4232013-10-04 11:29:36 -0400355 break;
356
357 case Key.ICON:
Sunny Goyalef728d42014-10-22 11:28:28 -0700358 restoreIcon(key, mBuffer, dataSize);
Chris Wren92aa4232013-10-04 11:29:36 -0400359 break;
360
361 case Key.WIDGET:
Sunny Goyalef728d42014-10-22 11:28:28 -0700362 restoreWidget(key, mBuffer, dataSize);
Chris Wren92aa4232013-10-04 11:29:36 -0400363 break;
364
365 default:
366 Log.w(TAG, "unknown restore entity type: " + key.type);
Sunny Goyalef728d42014-10-22 11:28:28 -0700367 mKeys.remove(key);
Chris Wren92aa4232013-10-04 11:29:36 -0400368 break;
Chris Wren1ada10d2013-09-13 18:01:38 -0400369 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700370 } catch (IOException e) {
371 Log.w(TAG, "ignoring unparsable backup entry", e);
Chris Wren1ada10d2013-09-13 18:01:38 -0400372 }
Chris Wren92aa4232013-10-04 11:29:36 -0400373 }
374
375 /**
376 * Record the restore state for the next backup.
377 *
378 * @param newState notes about the backup state after restore.
379 */
380 @Override
381 public void writeNewStateDescription(ParcelFileDescriptor newState) {
Sunny Goyalef728d42014-10-22 11:28:28 -0700382 writeJournal(newState, getCurrentStateJournal());
383 }
384
385 private Journal getCurrentStateJournal() {
386 Journal journal = new Journal();
387 journal.t = mLastBackupRestoreTime;
388 journal.key = mKeys.toArray(new BackupProtos.Key[mKeys.size()]);
389 journal.appVersion = getAppVersion();
Sunny Goyal33d44382014-10-16 09:24:19 -0700390 journal.backupVersion = BACKUP_VERSION;
Sunny Goyal96373642015-06-05 11:14:01 -0700391 journal.profile = mDeviceProfileData;
Sunny Goyalef728d42014-10-22 11:28:28 -0700392 return journal;
393 }
394
395 private int getAppVersion() {
396 try {
397 return mContext.getPackageManager()
398 .getPackageInfo(mContext.getPackageName(), 0).versionCode;
399 } catch (NameNotFoundException e) {
400 return 0;
401 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400402 }
403
Adam Cohen2e6da152015-05-06 11:42:25 -0700404 private DeviceProfieData initDeviceProfileData(InvariantDeviceProfile profile) {
405 DeviceProfieData data = new DeviceProfieData();
406 data.desktopRows = profile.numRows;
407 data.desktopCols = profile.numColumns;
408 data.hotseatCount = profile.numHotseatIcons;
409 data.allappsRank = profile.hotseatAllAppsRank;
410 return data;
Sunny Goyal33d44382014-10-16 09:24:19 -0700411 }
412
413 /**
Chris Wren1ada10d2013-09-13 18:01:38 -0400414 * Write all modified favorites to the data stream.
415 *
Chris Wren1ada10d2013-09-13 18:01:38 -0400416 * @param data output stream for key/value pairs
Chris Wren1ada10d2013-09-13 18:01:38 -0400417 * @throws IOException
418 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700419 private void backupFavorites(BackupDataOutput data) throws IOException {
Chris Wren1ada10d2013-09-13 18:01:38 -0400420 // persist things that have changed since the last backup
Chris Wren92aa4232013-10-04 11:29:36 -0400421 ContentResolver cr = mContext.getContentResolver();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700422 // Don't backup apps in other profiles for now.
Chris Wren22e130d2013-09-23 18:25:57 -0400423 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
Sunny Goyalffe83f12014-08-14 17:39:34 -0700424 getUserSelectionArg(), null, null);
Chris Wren1ada10d2013-09-13 18:01:38 -0400425 try {
Chris Wren22e130d2013-09-23 18:25:57 -0400426 cursor.moveToPosition(-1);
427 while(cursor.moveToNext()) {
428 final long id = cursor.getLong(ID_INDEX);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700429 final long updateTime = cursor.getLong(ID_MODIFIED);
430 Key key = getKey(Key.FAVORITE, id);
Sunny Goyalef728d42014-10-22 11:28:28 -0700431 mKeys.add(key);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700432 final String backupKey = keyToBackupKey(key);
Sunny Goyalef728d42014-10-22 11:28:28 -0700433 if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) {
434 writeRowToBackup(key, packFavorite(cursor), data);
Chris Wren50c8f422014-01-15 16:10:39 -0500435 } else {
Sunny Goyalef728d42014-10-22 11:28:28 -0700436 if (DEBUG) Log.d(TAG, "favorite already backup up: " + id);
Chris Wren22e130d2013-09-23 18:25:57 -0400437 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400438 }
439 } finally {
Chris Wren22e130d2013-09-23 18:25:57 -0400440 cursor.close();
Chris Wren1ada10d2013-09-13 18:01:38 -0400441 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400442 }
443
444 /**
445 * Read a favorite from the stream.
446 *
447 * <P>Keys arrive in any order, so screens and containers may not exist yet.
448 *
449 * @param key identifier for the row
450 * @param buffer the serialized proto from the stream, may be larger than dataSize
451 * @param dataSize the size of the proto from the stream
Chris Wren1ada10d2013-09-13 18:01:38 -0400452 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700453 private void restoreFavorite(Key key, byte[] buffer, int dataSize) throws IOException {
Chris Wren50c8f422014-01-15 16:10:39 -0500454 if (VERBOSE) Log.v(TAG, "unpacking favorite " + key.id);
Chris Wren1ada10d2013-09-13 18:01:38 -0400455 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
456 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
457
Sunny Goyalef728d42014-10-22 11:28:28 -0700458 ContentResolver cr = mContext.getContentResolver();
459 ContentValues values = unpackFavorite(buffer, dataSize);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700460 cr.insert(Favorites.CONTENT_URI, values);
Chris Wren1ada10d2013-09-13 18:01:38 -0400461 }
462
463 /**
464 * Write all modified screens to the data stream.
465 *
Chris Wren1ada10d2013-09-13 18:01:38 -0400466 * @param data output stream for key/value pairs
Chris Wren22e130d2013-09-23 18:25:57 -0400467 * @throws IOException
Chris Wren1ada10d2013-09-13 18:01:38 -0400468 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700469 private void backupScreens(BackupDataOutput data) throws IOException {
Chris Wren1ada10d2013-09-13 18:01:38 -0400470 // persist things that have changed since the last backup
Chris Wren92aa4232013-10-04 11:29:36 -0400471 ContentResolver cr = mContext.getContentResolver();
Chris Wren22e130d2013-09-23 18:25:57 -0400472 Cursor cursor = cr.query(WorkspaceScreens.CONTENT_URI, SCREEN_PROJECTION,
473 null, null, null);
Chris Wren1ada10d2013-09-13 18:01:38 -0400474 try {
Chris Wren22e130d2013-09-23 18:25:57 -0400475 cursor.moveToPosition(-1);
Sunny Goyalef728d42014-10-22 11:28:28 -0700476 if (DEBUG) Log.d(TAG, "dumping screens after: " + mLastBackupRestoreTime);
Chris Wren22e130d2013-09-23 18:25:57 -0400477 while(cursor.moveToNext()) {
478 final long id = cursor.getLong(ID_INDEX);
479 final long updateTime = cursor.getLong(ID_MODIFIED);
Chris Wren1ada10d2013-09-13 18:01:38 -0400480 Key key = getKey(Key.SCREEN, id);
Sunny Goyalef728d42014-10-22 11:28:28 -0700481 mKeys.add(key);
Chris Wren5743aa92014-01-10 18:02:06 -0500482 final String backupKey = keyToBackupKey(key);
Sunny Goyalef728d42014-10-22 11:28:28 -0700483 if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) {
484 writeRowToBackup(key, packScreen(cursor), data);
Chris Wren5dee7af2013-12-20 17:22:11 -0500485 } else {
Sunny Goyalef728d42014-10-22 11:28:28 -0700486 if (VERBOSE) Log.v(TAG, "screen already backup up " + id);
Chris Wren22e130d2013-09-23 18:25:57 -0400487 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400488 }
489 } finally {
Chris Wren22e130d2013-09-23 18:25:57 -0400490 cursor.close();
Chris Wren1ada10d2013-09-13 18:01:38 -0400491 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400492 }
493
494 /**
495 * Read a screen from the stream.
496 *
497 * <P>Keys arrive in any order, so children of this screen may already exist.
498 *
499 * @param key identifier for the row
500 * @param buffer the serialized proto from the stream, may be larger than dataSize
501 * @param dataSize the size of the proto from the stream
Chris Wren1ada10d2013-09-13 18:01:38 -0400502 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700503 private void restoreScreen(Key key, byte[] buffer, int dataSize) throws IOException {
Chris Wren50c8f422014-01-15 16:10:39 -0500504 if (VERBOSE) Log.v(TAG, "unpacking screen " + key.id);
Chris Wren1ada10d2013-09-13 18:01:38 -0400505 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
506 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
Chris Wren5dee7af2013-12-20 17:22:11 -0500507
Sunny Goyalef728d42014-10-22 11:28:28 -0700508 ContentResolver cr = mContext.getContentResolver();
509 ContentValues values = unpackScreen(buffer, dataSize);
510 cr.insert(WorkspaceScreens.CONTENT_URI, values);
Chris Wren1ada10d2013-09-13 18:01:38 -0400511 }
512
Chris Wren22e130d2013-09-23 18:25:57 -0400513 /**
514 * Write all the static icon resources we need to render placeholders
515 * for a package that is not installed.
516 *
Chris Wren22e130d2013-09-23 18:25:57 -0400517 * @param data output stream for key/value pairs
Chris Wren22e130d2013-09-23 18:25:57 -0400518 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700519 private void backupIcons(BackupDataOutput data) throws IOException {
Chris Wrenfd13c712013-09-27 15:45:19 -0400520 // persist icons that haven't been persisted yet
Chris Wren92aa4232013-10-04 11:29:36 -0400521 final ContentResolver cr = mContext.getContentResolver();
Chris Wren92aa4232013-10-04 11:29:36 -0400522 final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700523 final UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle();
Sunny Goyalef728d42014-10-22 11:28:28 -0700524 int backupUpIconCount = 0;
Chris Wren22e130d2013-09-23 18:25:57 -0400525
Kenny Guyed131872014-04-30 03:02:21 +0100526 // Don't backup apps in other profiles for now.
Kenny Guyed131872014-04-30 03:02:21 +0100527 String where = "(" + Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPLICATION + " OR " +
Kenny Guy43ea7ac2014-05-09 16:44:18 +0100528 Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_SHORTCUT + ") AND " +
Sunny Goyalffe83f12014-08-14 17:39:34 -0700529 getUserSelectionArg();
Chris Wren22e130d2013-09-23 18:25:57 -0400530 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
531 where, null, null);
Chris Wren22e130d2013-09-23 18:25:57 -0400532 try {
533 cursor.moveToPosition(-1);
534 while(cursor.moveToNext()) {
535 final long id = cursor.getLong(ID_INDEX);
536 final String intentDescription = cursor.getString(INTENT_INDEX);
537 try {
538 Intent intent = Intent.parseUri(intentDescription, 0);
539 ComponentName cn = intent.getComponent();
540 Key key = null;
541 String backupKey = null;
542 if (cn != null) {
543 key = getKey(Key.ICON, cn.flattenToShortString());
544 backupKey = keyToBackupKey(key);
Chris Wren22e130d2013-09-23 18:25:57 -0400545 } else {
546 Log.w(TAG, "empty intent on application favorite: " + id);
547 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700548 if (mExistingKeys.contains(backupKey)) {
549 if (DEBUG) Log.d(TAG, "already saved icon " + backupKey);
Chris Wren22e130d2013-09-23 18:25:57 -0400550
551 // remember that we already backed this up previously
Sunny Goyalef728d42014-10-22 11:28:28 -0700552 mKeys.add(key);
Chris Wren22e130d2013-09-23 18:25:57 -0400553 } else if (backupKey != null) {
Sunny Goyalef728d42014-10-22 11:28:28 -0700554 if (DEBUG) Log.d(TAG, "I can count this high: " + backupUpIconCount);
555 if (backupUpIconCount < MAX_ICONS_PER_PASS) {
556 if (DEBUG) Log.d(TAG, "saving icon " + backupKey);
Kenny Guyed131872014-04-30 03:02:21 +0100557 Bitmap icon = mIconCache.getIcon(intent, myUserHandle);
Kenny Guyed131872014-04-30 03:02:21 +0100558 if (icon != null && !mIconCache.isDefaultIcon(icon, myUserHandle)) {
Sunny Goyalef728d42014-10-22 11:28:28 -0700559 writeRowToBackup(key, packIcon(dpi, icon), data);
560 mKeys.add(key);
561 backupUpIconCount ++;
Chris Wren22e130d2013-09-23 18:25:57 -0400562 }
563 } else {
Sunny Goyalef728d42014-10-22 11:28:28 -0700564 if (VERBOSE) Log.v(TAG, "deferring icon backup " + backupKey);
Chris Wren22e130d2013-09-23 18:25:57 -0400565 // too many icons for this pass, request another.
Chris Wren92aa4232013-10-04 11:29:36 -0400566 dataChanged();
Chris Wren22e130d2013-09-23 18:25:57 -0400567 }
568 }
569 } catch (URISyntaxException e) {
Chris Wren50c8f422014-01-15 16:10:39 -0500570 Log.e(TAG, "invalid URI on application favorite: " + id);
Chris Wren22e130d2013-09-23 18:25:57 -0400571 } catch (IOException e) {
Chris Wren50c8f422014-01-15 16:10:39 -0500572 Log.e(TAG, "unable to save application icon for favorite: " + id);
Chris Wren22e130d2013-09-23 18:25:57 -0400573 }
574
575 }
576 } finally {
577 cursor.close();
578 }
Chris Wren22e130d2013-09-23 18:25:57 -0400579 }
580
581 /**
582 * Read an icon from the stream.
583 *
Chris Wrenfd13c712013-09-27 15:45:19 -0400584 * <P>Keys arrive in any order, so shortcuts that use this icon may already exist.
Chris Wren22e130d2013-09-23 18:25:57 -0400585 *
586 * @param key identifier for the row
587 * @param buffer the serialized proto from the stream, may be larger than dataSize
588 * @param dataSize the size of the proto from the stream
Chris Wren22e130d2013-09-23 18:25:57 -0400589 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700590 private void restoreIcon(Key key, byte[] buffer, int dataSize) throws IOException {
Chris Wren50c8f422014-01-15 16:10:39 -0500591 if (VERBOSE) Log.v(TAG, "unpacking icon " + key.id);
Chris Wren22e130d2013-09-23 18:25:57 -0400592 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
593 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
Chris Wren6d0dde02014-02-10 12:16:54 -0500594
Sunny Goyalef728d42014-10-22 11:28:28 -0700595 Resource res = unpackProto(new Resource(), buffer, dataSize);
596 if (DEBUG) {
597 Log.d(TAG, "unpacked " + res.dpi + " dpi icon");
Chris Wren22e130d2013-09-23 18:25:57 -0400598 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700599 Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length);
600 if (icon == null) {
601 Log.w(TAG, "failed to unpack icon for " + key.name);
602 }
603 if (VERBOSE) Log.v(TAG, "saving restored icon as: " + key.name);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800604 mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi,
605 "" /* label */, mUserSerial);
Chris Wren22e130d2013-09-23 18:25:57 -0400606 }
607
Chris Wrenfd13c712013-09-27 15:45:19 -0400608 /**
609 * Write all the static widget resources we need to render placeholders
610 * for a package that is not installed.
611 *
Chris Wrenfd13c712013-09-27 15:45:19 -0400612 * @param data output stream for key/value pairs
Chris Wrenfd13c712013-09-27 15:45:19 -0400613 * @throws IOException
614 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700615 private void backupWidgets(BackupDataOutput data) throws IOException {
Chris Wrenfd13c712013-09-27 15:45:19 -0400616 // persist static widget info that hasn't been persisted yet
Chris Wren92aa4232013-10-04 11:29:36 -0400617 final ContentResolver cr = mContext.getContentResolver();
Chris Wren92aa4232013-10-04 11:29:36 -0400618 final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
Sunny Goyalef728d42014-10-22 11:28:28 -0700619 int backupWidgetCount = 0;
Chris Wrenfd13c712013-09-27 15:45:19 -0400620
Sunny Goyalffe83f12014-08-14 17:39:34 -0700621 String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET + " AND "
622 + getUserSelectionArg();
Chris Wrenfd13c712013-09-27 15:45:19 -0400623 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
624 where, null, null);
Chris Wrenfd13c712013-09-27 15:45:19 -0400625 try {
626 cursor.moveToPosition(-1);
627 while(cursor.moveToNext()) {
628 final long id = cursor.getLong(ID_INDEX);
629 final String providerName = cursor.getString(APPWIDGET_PROVIDER_INDEX);
Chris Wrenfd13c712013-09-27 15:45:19 -0400630 final ComponentName provider = ComponentName.unflattenFromString(providerName);
631 Key key = null;
632 String backupKey = null;
633 if (provider != null) {
634 key = getKey(Key.WIDGET, providerName);
635 backupKey = keyToBackupKey(key);
Chris Wrenfd13c712013-09-27 15:45:19 -0400636 } else {
637 Log.w(TAG, "empty intent on appwidget: " + id);
638 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700639 if (mExistingKeys.contains(backupKey)) {
640 if (DEBUG) Log.d(TAG, "already saved widget " + backupKey);
Chris Wrenfd13c712013-09-27 15:45:19 -0400641
642 // remember that we already backed this up previously
Sunny Goyalef728d42014-10-22 11:28:28 -0700643 mKeys.add(key);
Chris Wrenfd13c712013-09-27 15:45:19 -0400644 } else if (backupKey != null) {
Sunny Goyalef728d42014-10-22 11:28:28 -0700645 if (DEBUG) Log.d(TAG, "I can count this high: " + backupWidgetCount);
646 if (backupWidgetCount < MAX_WIDGETS_PER_PASS) {
647 if (DEBUG) Log.d(TAG, "saving widget " + backupKey);
Robin Lee26ace122015-03-16 19:41:43 +0000648 UserHandleCompat user = UserHandleCompat.myUserHandle();
Sunny Goyal96373642015-06-05 11:14:01 -0700649 writeRowToBackup(key, packWidget(dpi, provider, user), data);
Sunny Goyalef728d42014-10-22 11:28:28 -0700650 mKeys.add(key);
651 backupWidgetCount ++;
Chris Wrenfd13c712013-09-27 15:45:19 -0400652 } else {
Sunny Goyalef728d42014-10-22 11:28:28 -0700653 if (VERBOSE) Log.v(TAG, "deferring widget backup " + backupKey);
Chris Wrenfd13c712013-09-27 15:45:19 -0400654 // too many widgets for this pass, request another.
Chris Wren92aa4232013-10-04 11:29:36 -0400655 dataChanged();
Chris Wrenfd13c712013-09-27 15:45:19 -0400656 }
657 }
658 }
659 } finally {
660 cursor.close();
661 }
Chris Wrenfd13c712013-09-27 15:45:19 -0400662 }
663
664 /**
665 * Read a widget from the stream.
666 *
667 * <P>Keys arrive in any order, so widgets that use this data may already exist.
668 *
669 * @param key identifier for the row
670 * @param buffer the serialized proto from the stream, may be larger than dataSize
671 * @param dataSize the size of the proto from the stream
Chris Wrenfd13c712013-09-27 15:45:19 -0400672 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700673 private void restoreWidget(Key key, byte[] buffer, int dataSize) throws IOException {
Chris Wren50c8f422014-01-15 16:10:39 -0500674 if (VERBOSE) Log.v(TAG, "unpacking widget " + key.id);
Chris Wrenfd13c712013-09-27 15:45:19 -0400675 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
676 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
Sunny Goyalef728d42014-10-22 11:28:28 -0700677 Widget widget = unpackProto(new Widget(), buffer, dataSize);
678 if (DEBUG) Log.d(TAG, "unpacked " + widget.provider);
679 if (widget.icon.data != null) {
680 Bitmap icon = BitmapFactory
681 .decodeByteArray(widget.icon.data, 0, widget.icon.data.length);
682 if (icon == null) {
683 Log.w(TAG, "failed to unpack widget icon for " + key.name);
Chris Wren5dee7af2013-12-20 17:22:11 -0500684 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800685 mIconCache.preloadIcon(ComponentName.unflattenFromString(widget.provider),
686 icon, widget.icon.dpi, widget.label, mUserSerial);
Chris Wren5dee7af2013-12-20 17:22:11 -0500687 }
Chris Wrenfd13c712013-09-27 15:45:19 -0400688 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700689
690 // future site of widget table mutation
Chris Wrenfd13c712013-09-27 15:45:19 -0400691 }
692
Chris Wren22e130d2013-09-23 18:25:57 -0400693 /** create a new key, with an integer ID.
Chris Wren1ada10d2013-09-13 18:01:38 -0400694 *
695 * <P> Keys contain their own checksum instead of using
696 * the heavy-weight CheckedMessage wrapper.
697 */
698 private Key getKey(int type, long id) {
699 Key key = new Key();
700 key.type = type;
701 key.id = id;
702 key.checksum = checkKey(key);
703 return key;
704 }
705
Chris Wren22e130d2013-09-23 18:25:57 -0400706 /** create a new key for a named object.
707 *
708 * <P> Keys contain their own checksum instead of using
709 * the heavy-weight CheckedMessage wrapper.
710 */
711 private Key getKey(int type, String name) {
712 Key key = new Key();
713 key.type = type;
714 key.name = name;
715 key.checksum = checkKey(key);
716 return key;
717 }
718
Chris Wren1ada10d2013-09-13 18:01:38 -0400719 /** keys need to be strings, serialize and encode. */
720 private String keyToBackupKey(Key key) {
Chris Wren978194c2013-10-03 17:47:22 -0400721 return Base64.encodeToString(Key.toByteArray(key), Base64.NO_WRAP);
Chris Wren1ada10d2013-09-13 18:01:38 -0400722 }
723
724 /** keys need to be strings, decode and parse. */
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700725 private Key backupKeyToKey(String backupKey) throws InvalidBackupException {
Chris Wren1ada10d2013-09-13 18:01:38 -0400726 try {
727 Key key = Key.parseFrom(Base64.decode(backupKey, Base64.DEFAULT));
728 if (key.checksum != checkKey(key)) {
729 key = null;
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700730 throw new InvalidBackupException("invalid key read from stream" + backupKey);
Chris Wren1ada10d2013-09-13 18:01:38 -0400731 }
732 return key;
733 } catch (InvalidProtocolBufferNanoException e) {
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700734 throw new InvalidBackupException(e);
Chris Wren1ada10d2013-09-13 18:01:38 -0400735 } catch (IllegalArgumentException e) {
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700736 throw new InvalidBackupException(e);
Chris Wren1ada10d2013-09-13 18:01:38 -0400737 }
738 }
739
740 /** Compute the checksum over the important bits of a key. */
741 private long checkKey(Key key) {
742 CRC32 checksum = new CRC32();
743 checksum.update(key.type);
744 checksum.update((int) (key.id & 0xffff));
745 checksum.update((int) ((key.id >> 32) & 0xffff));
746 if (!TextUtils.isEmpty(key.name)) {
747 checksum.update(key.name.getBytes());
748 }
749 return checksum.getValue();
750 }
751
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800752 /**
753 * @return true if its an hotseat item, that can be replaced during restore.
754 * TODO: Extend check for folders in hotseat.
755 */
756 private boolean isReplaceableHotseatItem(Favorite favorite) {
757 return favorite.container == Favorites.CONTAINER_HOTSEAT
758 && favorite.intent != null
759 && (favorite.itemType == Favorites.ITEM_TYPE_APPLICATION
760 || favorite.itemType == Favorites.ITEM_TYPE_SHORTCUT);
761 }
762
Chris Wren1ada10d2013-09-13 18:01:38 -0400763 /** Serialize a Favorite for persistence, including a checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700764 private Favorite packFavorite(Cursor c) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400765 Favorite favorite = new Favorite();
766 favorite.id = c.getLong(ID_INDEX);
767 favorite.screen = c.getInt(SCREEN_INDEX);
768 favorite.container = c.getInt(CONTAINER_INDEX);
769 favorite.cellX = c.getInt(CELLX_INDEX);
770 favorite.cellY = c.getInt(CELLY_INDEX);
771 favorite.spanX = c.getInt(SPANX_INDEX);
772 favorite.spanY = c.getInt(SPANY_INDEX);
773 favorite.iconType = c.getInt(ICON_TYPE_INDEX);
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700774
Chris Wren1ada10d2013-09-13 18:01:38 -0400775 String title = c.getString(TITLE_INDEX);
776 if (!TextUtils.isEmpty(title)) {
777 favorite.title = title;
778 }
Kenny Guyf8b1dfd2014-05-13 12:59:34 +0100779 String intentDescription = c.getString(INTENT_INDEX);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800780 Intent intent = null;
Kenny Guyf8b1dfd2014-05-13 12:59:34 +0100781 if (!TextUtils.isEmpty(intentDescription)) {
782 try {
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800783 intent = Intent.parseUri(intentDescription, 0);
Kenny Guyf8b1dfd2014-05-13 12:59:34 +0100784 intent.removeExtra(ItemInfo.EXTRA_PROFILE);
785 favorite.intent = intent.toUri(0);
786 } catch (URISyntaxException e) {
787 Log.e(TAG, "Invalid intent", e);
Sunny Goyalef728d42014-10-22 11:28:28 -0700788 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400789 }
790 favorite.itemType = c.getInt(ITEM_TYPE_INDEX);
791 if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) {
792 favorite.appWidgetId = c.getInt(APPWIDGET_ID_INDEX);
793 String appWidgetProvider = c.getString(APPWIDGET_PROVIDER_INDEX);
794 if (!TextUtils.isEmpty(appWidgetProvider)) {
795 favorite.appWidgetProvider = appWidgetProvider;
796 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700797 } else if (favorite.itemType == Favorites.ITEM_TYPE_SHORTCUT) {
798 if (favorite.iconType == Favorites.ICON_TYPE_RESOURCE) {
799 String iconPackage = c.getString(ICON_PACKAGE_INDEX);
800 if (!TextUtils.isEmpty(iconPackage)) {
801 favorite.iconPackage = iconPackage;
802 }
803 String iconResource = c.getString(ICON_RESOURCE_INDEX);
804 if (!TextUtils.isEmpty(iconResource)) {
805 favorite.iconResource = iconResource;
806 }
807 }
808
809 byte[] blob = c.getBlob(ICON_INDEX);
810 if (blob != null && blob.length > 0) {
811 favorite.icon = blob;
812 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400813 }
814
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800815 if (isReplaceableHotseatItem(favorite)) {
816 if (intent != null && intent.getComponent() != null) {
817 PackageManager pm = mContext.getPackageManager();
818 ActivityInfo activity = null;;
819 try {
820 activity = pm.getActivityInfo(intent.getComponent(), 0);
821 } catch (NameNotFoundException e) {
822 Log.e(TAG, "Target not found", e);
823 }
824 if (activity == null) {
825 return favorite;
826 }
827 for (int i = 0; i < mItemTypeMatchers.length; i++) {
828 if (mItemTypeMatchers[i] == null) {
829 mItemTypeMatchers[i] = new ItemTypeMatcher(
830 CommonAppTypeParser.getResourceForItemType(i));
831 }
832 if (mItemTypeMatchers[i].matches(activity, pm)) {
833 favorite.targetType = i;
834 break;
835 }
836 }
837 }
838 }
839
Sunny Goyalef728d42014-10-22 11:28:28 -0700840 return favorite;
Chris Wren1ada10d2013-09-13 18:01:38 -0400841 }
842
843 /** Deserialize a Favorite from persistence, after verifying checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700844 private ContentValues unpackFavorite(byte[] buffer, int dataSize)
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700845 throws IOException {
Sunny Goyalef728d42014-10-22 11:28:28 -0700846 Favorite favorite = unpackProto(new Favorite(), buffer, dataSize);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800847
Chris Wren5dee7af2013-12-20 17:22:11 -0500848 ContentValues values = new ContentValues();
849 values.put(Favorites._ID, favorite.id);
850 values.put(Favorites.SCREEN, favorite.screen);
851 values.put(Favorites.CONTAINER, favorite.container);
852 values.put(Favorites.CELLX, favorite.cellX);
853 values.put(Favorites.CELLY, favorite.cellY);
854 values.put(Favorites.SPANX, favorite.spanX);
855 values.put(Favorites.SPANY, favorite.spanY);
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700856
857 if (favorite.itemType == Favorites.ITEM_TYPE_SHORTCUT) {
858 values.put(Favorites.ICON_TYPE, favorite.iconType);
859 if (favorite.iconType == Favorites.ICON_TYPE_RESOURCE) {
860 values.put(Favorites.ICON_PACKAGE, favorite.iconPackage);
861 values.put(Favorites.ICON_RESOURCE, favorite.iconResource);
862 }
Chris Wren5dee7af2013-12-20 17:22:11 -0500863 values.put(Favorites.ICON, favorite.icon);
864 }
Sunny Goyal4e5cc642015-06-25 16:37:44 -0700865
Chris Wren5dee7af2013-12-20 17:22:11 -0500866 if (!TextUtils.isEmpty(favorite.title)) {
867 values.put(Favorites.TITLE, favorite.title);
868 } else {
869 values.put(Favorites.TITLE, "");
870 }
871 if (!TextUtils.isEmpty(favorite.intent)) {
872 values.put(Favorites.INTENT, favorite.intent);
873 }
874 values.put(Favorites.ITEM_TYPE, favorite.itemType);
Chris Wrenf4d08112014-01-16 18:13:56 -0500875
Kenny Guyf8b1dfd2014-05-13 12:59:34 +0100876 UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle();
877 long userSerialNumber =
878 UserManagerCompat.getInstance(mContext).getSerialNumberForUser(myUserHandle);
879 values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
880
Sunny Goyal96373642015-06-05 11:14:01 -0700881 DeviceProfieData currentProfile = mDeviceProfileData;
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700882
Sunny Goyalff572272014-07-23 13:58:07 -0700883 if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) {
884 if (!TextUtils.isEmpty(favorite.appWidgetProvider)) {
885 values.put(Favorites.APPWIDGET_PROVIDER, favorite.appWidgetProvider);
886 }
887 values.put(Favorites.APPWIDGET_ID, favorite.appWidgetId);
888 values.put(LauncherSettings.Favorites.RESTORED,
889 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
890 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
891 LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700892
893 // Verify placement
894 if (((favorite.cellX + favorite.spanX) > currentProfile.desktopCols)
895 || ((favorite.cellY + favorite.spanY) > currentProfile.desktopRows)) {
896 restoreSuccessful = false;
897 throw new InvalidBackupException("Widget not in screen bounds, aborting restore");
898 }
Sunny Goyalff572272014-07-23 13:58:07 -0700899 } else {
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800900 // Check if it is an hotseat item, that can be replaced.
901 if (isReplaceableHotseatItem(favorite)
902 && favorite.targetType != Favorite.TARGET_NONE
903 && favorite.targetType < CommonAppTypeParser.SUPPORTED_TYPE_COUNT) {
904 Log.e(TAG, "Added item type flag");
905 values.put(LauncherSettings.Favorites.RESTORED,
906 1 | CommonAppTypeParser.encodeItemTypeToFlag(favorite.targetType));
907 } else {
908 // Let LauncherModel know we've been here.
909 values.put(LauncherSettings.Favorites.RESTORED, 1);
910 }
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700911
912 // Verify placement
913 if (favorite.container == Favorites.CONTAINER_HOTSEAT) {
914 if ((favorite.screen >= currentProfile.hotseatCount)
915 || (favorite.screen == currentProfile.allappsRank)) {
916 restoreSuccessful = false;
917 throw new InvalidBackupException("Item not in hotseat bounds, aborting restore");
918 }
919 } else {
920 if ((favorite.cellX >= currentProfile.desktopCols)
921 || (favorite.cellY >= currentProfile.desktopRows)) {
922 restoreSuccessful = false;
923 throw new InvalidBackupException("Item not in desktop bounds, aborting restore");
924 }
925 }
Sunny Goyalff572272014-07-23 13:58:07 -0700926 }
Chris Wrenf4d08112014-01-16 18:13:56 -0500927
Chris Wren5dee7af2013-12-20 17:22:11 -0500928 return values;
Chris Wren1ada10d2013-09-13 18:01:38 -0400929 }
930
931 /** Serialize a Screen for persistence, including a checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700932 private Screen packScreen(Cursor c) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400933 Screen screen = new Screen();
934 screen.id = c.getLong(ID_INDEX);
935 screen.rank = c.getInt(SCREEN_RANK_INDEX);
Sunny Goyalef728d42014-10-22 11:28:28 -0700936 return screen;
Chris Wren1ada10d2013-09-13 18:01:38 -0400937 }
938
939 /** Deserialize a Screen from persistence, after verifying checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700940 private ContentValues unpackScreen(byte[] buffer, int dataSize)
Chris Wren1ada10d2013-09-13 18:01:38 -0400941 throws InvalidProtocolBufferNanoException {
Sunny Goyalef728d42014-10-22 11:28:28 -0700942 Screen screen = unpackProto(new Screen(), buffer, dataSize);
Chris Wren5dee7af2013-12-20 17:22:11 -0500943 ContentValues values = new ContentValues();
944 values.put(WorkspaceScreens._ID, screen.id);
945 values.put(WorkspaceScreens.SCREEN_RANK, screen.rank);
946 return values;
Chris Wren1ada10d2013-09-13 18:01:38 -0400947 }
948
Chris Wren22e130d2013-09-23 18:25:57 -0400949 /** Serialize an icon Resource for persistence, including a checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700950 private Resource packIcon(int dpi, Bitmap icon) {
Chris Wren22e130d2013-09-23 18:25:57 -0400951 Resource res = new Resource();
952 res.dpi = dpi;
Sunny Goyal73a22a52015-04-28 16:51:09 -0700953 res.data = Utilities.flattenBitmap(icon);
Chris Wren22e130d2013-09-23 18:25:57 -0400954 return res;
955 }
956
Chris Wrenfd13c712013-09-27 15:45:19 -0400957 /** Serialize a widget for persistence, including a checksum wrapper. */
Sunny Goyal96373642015-06-05 11:14:01 -0700958 private Widget packWidget(int dpi, ComponentName provider, UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -0800959 final LauncherAppWidgetProviderInfo info =
Robin Lee26ace122015-03-16 19:41:43 +0000960 LauncherModel.getProviderInfo(mContext, provider, user);
Chris Wrenfd13c712013-09-27 15:45:19 -0400961 Widget widget = new Widget();
962 widget.provider = provider.flattenToShortString();
963 widget.label = info.label;
964 widget.configure = info.configure != null;
965 if (info.icon != 0) {
966 widget.icon = new Resource();
Sunny Goyal96373642015-06-05 11:14:01 -0700967 Drawable fullResIcon = mIconCache.getFullResIcon(provider.getPackageName(), info.icon);
Chris Wren92aa4232013-10-04 11:29:36 -0400968 Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext);
Sunny Goyal73a22a52015-04-28 16:51:09 -0700969 widget.icon.data = Utilities.flattenBitmap(icon);
970 widget.icon.dpi = dpi;
Chris Wrenfd13c712013-09-27 15:45:19 -0400971 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700972 return widget;
Chris Wrenfd13c712013-09-27 15:45:19 -0400973 }
974
Sunny Goyalef728d42014-10-22 11:28:28 -0700975 /**
976 * Deserialize a proto after verifying checksum wrapper.
977 */
978 private <T extends MessageNano> T unpackProto(T proto, byte[] buffer, int dataSize)
Chris Wrenfd13c712013-09-27 15:45:19 -0400979 throws InvalidProtocolBufferNanoException {
Sunny Goyalef728d42014-10-22 11:28:28 -0700980 MessageNano.mergeFrom(proto, readCheckedBytes(buffer, dataSize));
981 if (DEBUG) Log.d(TAG, "unpacked proto " + proto);
982 return proto;
Chris Wrenfd13c712013-09-27 15:45:19 -0400983 }
984
Chris Wren1ada10d2013-09-13 18:01:38 -0400985 /**
986 * Read the old journal from the input file.
987 *
988 * In the event of any error, just pretend we didn't have a journal,
989 * in that case, do a full backup.
990 *
991 * @param oldState the read-0only file descriptor pointing to the old journal
Chris Wren65b6a602014-01-10 14:11:25 -0500992 * @return a Journal protocol buffer
Chris Wren1ada10d2013-09-13 18:01:38 -0400993 */
994 private Journal readJournal(ParcelFileDescriptor oldState) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400995 Journal journal = new Journal();
Chris Wren92aa4232013-10-04 11:29:36 -0400996 if (oldState == null) {
997 return journal;
998 }
999 FileInputStream inStream = new FileInputStream(oldState.getFileDescriptor());
1000 try {
Chris Wren65b6a602014-01-10 14:11:25 -05001001 int availableBytes = inStream.available();
1002 if (DEBUG) Log.d(TAG, "available " + availableBytes);
1003 if (availableBytes < MAX_JOURNAL_SIZE) {
1004 byte[] buffer = new byte[availableBytes];
Chris Wren1ada10d2013-09-13 18:01:38 -04001005 int bytesRead = 0;
Chris Wren65b6a602014-01-10 14:11:25 -05001006 boolean valid = false;
Chris Wren50c8f422014-01-15 16:10:39 -05001007 InvalidProtocolBufferNanoException lastProtoException = null;
Chris Wren65b6a602014-01-10 14:11:25 -05001008 while (availableBytes > 0) {
Chris Wren92aa4232013-10-04 11:29:36 -04001009 try {
Chris Wren65b6a602014-01-10 14:11:25 -05001010 // OMG what are you doing? This is crazy inefficient!
1011 // If we read a byte that is not ours, we will cause trouble: b/12491813
1012 // However, we don't know how many bytes to expect (oops).
1013 // So we have to step through *slowly*, watching for the end.
1014 int result = inStream.read(buffer, bytesRead, 1);
Chris Wren92aa4232013-10-04 11:29:36 -04001015 if (result > 0) {
Chris Wren65b6a602014-01-10 14:11:25 -05001016 availableBytes -= result;
Chris Wren92aa4232013-10-04 11:29:36 -04001017 bytesRead += result;
1018 } else {
Chris Wren65b6a602014-01-10 14:11:25 -05001019 Log.w(TAG, "unexpected end of file while reading journal.");
1020 // stop reading and see what there is to parse
1021 availableBytes = 0;
Chris Wren92aa4232013-10-04 11:29:36 -04001022 }
1023 } catch (IOException e) {
Chris Wren92aa4232013-10-04 11:29:36 -04001024 buffer = null;
Chris Wren65b6a602014-01-10 14:11:25 -05001025 availableBytes = 0;
1026 }
1027
1028 // check the buffer to see if we have a valid journal
1029 try {
Sunny Goyalef728d42014-10-22 11:28:28 -07001030 MessageNano.mergeFrom(journal, readCheckedBytes(buffer, bytesRead));
Chris Wren65b6a602014-01-10 14:11:25 -05001031 // if we are here, then we have read a valid, checksum-verified journal
1032 valid = true;
1033 availableBytes = 0;
Chris Wren50c8f422014-01-15 16:10:39 -05001034 if (VERBOSE) Log.v(TAG, "read " + bytesRead + " bytes of journal");
Chris Wren65b6a602014-01-10 14:11:25 -05001035 } catch (InvalidProtocolBufferNanoException e) {
1036 // if we don't have the whole journal yet, mergeFrom will throw. keep going.
Chris Wren50c8f422014-01-15 16:10:39 -05001037 lastProtoException = e;
Chris Wren65b6a602014-01-10 14:11:25 -05001038 journal.clear();
Chris Wren92aa4232013-10-04 11:29:36 -04001039 }
Chris Wren1ada10d2013-09-13 18:01:38 -04001040 }
Chris Wren92aa4232013-10-04 11:29:36 -04001041 if (DEBUG) Log.d(TAG, "journal bytes read: " + bytesRead);
Chris Wren65b6a602014-01-10 14:11:25 -05001042 if (!valid) {
Chris Wren50c8f422014-01-15 16:10:39 -05001043 Log.w(TAG, "could not find a valid journal", lastProtoException);
Chris Wren1ada10d2013-09-13 18:01:38 -04001044 }
1045 }
Chris Wren92aa4232013-10-04 11:29:36 -04001046 } catch (IOException e) {
Chris Wren50c8f422014-01-15 16:10:39 -05001047 Log.w(TAG, "failed to close the journal", e);
Chris Wren92aa4232013-10-04 11:29:36 -04001048 } finally {
Chris Wren1ada10d2013-09-13 18:01:38 -04001049 try {
1050 inStream.close();
1051 } catch (IOException e) {
Chris Wren50c8f422014-01-15 16:10:39 -05001052 Log.w(TAG, "failed to close the journal", e);
Chris Wren1ada10d2013-09-13 18:01:38 -04001053 }
1054 }
1055 return journal;
1056 }
1057
Sunny Goyalef728d42014-10-22 11:28:28 -07001058 private void writeRowToBackup(Key key, MessageNano proto, BackupDataOutput data)
1059 throws IOException {
1060 writeRowToBackup(keyToBackupKey(key), proto, data);
1061 }
1062
1063 private void writeRowToBackup(String backupKey, MessageNano proto,
Chris Wren22e130d2013-09-23 18:25:57 -04001064 BackupDataOutput data) throws IOException {
Sunny Goyalef728d42014-10-22 11:28:28 -07001065 byte[] blob = writeCheckedBytes(proto);
Chris Wren22e130d2013-09-23 18:25:57 -04001066 data.writeEntityHeader(backupKey, blob.length);
1067 data.writeEntityData(blob, blob.length);
Sunny Goyalc0ee6752015-01-16 14:10:32 -08001068 mBackupDataWasUpdated = true;
Sunny Goyalef728d42014-10-22 11:28:28 -07001069 if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey);
Chris Wren22e130d2013-09-23 18:25:57 -04001070 }
1071
Chris Wren1ada10d2013-09-13 18:01:38 -04001072 /**
1073 * Write the new journal to the output file.
1074 *
1075 * In the event of any error, just pretend we didn't have a journal,
1076 * in that case, do a full backup.
1077
1078 * @param newState the write-only file descriptor pointing to the new journal
1079 * @param journal a Journal protocol buffer
1080 */
1081 private void writeJournal(ParcelFileDescriptor newState, Journal journal) {
1082 FileOutputStream outStream = null;
1083 try {
1084 outStream = new FileOutputStream(newState.getFileDescriptor());
Chris Wren65b6a602014-01-10 14:11:25 -05001085 final byte[] journalBytes = writeCheckedBytes(journal);
Chris Wren65b6a602014-01-10 14:11:25 -05001086 outStream.write(journalBytes);
Chris Wren1ada10d2013-09-13 18:01:38 -04001087 outStream.close();
Chris Wren50c8f422014-01-15 16:10:39 -05001088 if (VERBOSE) Log.v(TAG, "wrote " + journalBytes.length + " bytes of journal");
Chris Wren1ada10d2013-09-13 18:01:38 -04001089 } catch (IOException e) {
Chris Wren50c8f422014-01-15 16:10:39 -05001090 Log.w(TAG, "failed to write backup journal", e);
Chris Wren1ada10d2013-09-13 18:01:38 -04001091 }
1092 }
1093
1094 /** Wrap a proto in a CheckedMessage and compute the checksum. */
1095 private byte[] writeCheckedBytes(MessageNano proto) {
1096 CheckedMessage wrapper = new CheckedMessage();
1097 wrapper.payload = MessageNano.toByteArray(proto);
1098 CRC32 checksum = new CRC32();
1099 checksum.update(wrapper.payload);
1100 wrapper.checksum = checksum.getValue();
1101 return MessageNano.toByteArray(wrapper);
1102 }
1103
1104 /** Unwrap a proto message from a CheckedMessage, verifying the checksum. */
Sunny Goyalef728d42014-10-22 11:28:28 -07001105 private static byte[] readCheckedBytes(byte[] buffer, int dataSize)
Chris Wren1ada10d2013-09-13 18:01:38 -04001106 throws InvalidProtocolBufferNanoException {
1107 CheckedMessage wrapper = new CheckedMessage();
Sunny Goyalef728d42014-10-22 11:28:28 -07001108 MessageNano.mergeFrom(wrapper, buffer, 0, dataSize);
Chris Wren1ada10d2013-09-13 18:01:38 -04001109 CRC32 checksum = new CRC32();
1110 checksum.update(wrapper.payload);
1111 if (wrapper.checksum != checksum.getValue()) {
1112 throw new InvalidProtocolBufferNanoException("checksum does not match");
1113 }
1114 return wrapper.payload;
1115 }
1116
Sunny Goyalef728d42014-10-22 11:28:28 -07001117 /**
1118 * @return true if the launcher is in a state to support backup
1119 */
Chris Wren71144262014-02-27 15:49:39 -05001120 private boolean launcherIsReady() {
1121 ContentResolver cr = mContext.getContentResolver();
1122 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, null, null, null);
1123 if (cursor == null) {
1124 // launcher data has been wiped, do nothing
1125 return false;
1126 }
1127 cursor.close();
1128
Adam Cohen2e6da152015-05-06 11:42:25 -07001129 if (LauncherAppState.getInstanceNoCreate() == null) {
Chris Wren71144262014-02-27 15:49:39 -05001130 // launcher services are unavailable, try again later
Chris Wren71144262014-02-27 15:49:39 -05001131 return false;
1132 }
1133
1134 return true;
1135 }
1136
Sunny Goyalffe83f12014-08-14 17:39:34 -07001137 private String getUserSelectionArg() {
1138 return Favorites.PROFILE_ID + '=' + UserManagerCompat.getInstance(mContext)
1139 .getSerialNumberForUser(UserHandleCompat.myUserHandle());
1140 }
1141
Sunny Goyal316490e2015-06-02 09:38:28 -07001142 @Thunk class InvalidBackupException extends IOException {
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001143
1144 private static final long serialVersionUID = 8931456637211665082L;
1145
Sunny Goyal316490e2015-06-02 09:38:28 -07001146 @Thunk InvalidBackupException(Throwable cause) {
Chris Wren1ada10d2013-09-13 18:01:38 -04001147 super(cause);
1148 }
1149
Sunny Goyal316490e2015-06-02 09:38:28 -07001150 @Thunk InvalidBackupException(String reason) {
Chris Wren1ada10d2013-09-13 18:01:38 -04001151 super(reason);
1152 }
1153 }
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001154
1155 /**
1156 * A class to check if an activity can handle one of the intents from a list of
1157 * predefined intents.
1158 */
1159 private class ItemTypeMatcher {
1160
1161 private final ArrayList<Intent> mIntents;
1162
1163 ItemTypeMatcher(int xml_res) {
1164 mIntents = xml_res == 0 ? new ArrayList<Intent>() : parseIntents(xml_res);
1165 }
1166
1167 private ArrayList<Intent> parseIntents(int xml_res) {
1168 ArrayList<Intent> intents = new ArrayList<Intent>();
1169 XmlResourceParser parser = mContext.getResources().getXml(xml_res);
1170 try {
1171 DefaultLayoutParser.beginDocument(parser, DefaultLayoutParser.TAG_RESOLVE);
1172 final int depth = parser.getDepth();
1173 int type;
1174 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1175 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1176 if (type != XmlPullParser.START_TAG) {
1177 continue;
1178 } else if (DefaultLayoutParser.TAG_FAVORITE.equals(parser.getName())) {
1179 final String uri = DefaultLayoutParser.getAttributeValue(
1180 parser, DefaultLayoutParser.ATTR_URI);
1181 intents.add(Intent.parseUri(uri, 0));
1182 }
1183 }
1184 } catch (URISyntaxException | XmlPullParserException | IOException e) {
1185 Log.e(TAG, "Unable to parse " + xml_res, e);
1186 } finally {
1187 parser.close();
1188 }
1189 return intents;
1190 }
1191
1192 public boolean matches(ActivityInfo activity, PackageManager pm) {
1193 for (Intent intent : mIntents) {
1194 intent.setPackage(activity.packageName);
1195 ResolveInfo info = pm.resolveActivity(intent, 0);
1196 if (info != null && (info.activityInfo.name.equals(activity.name)
1197 || info.activityInfo.name.equals(activity.targetActivity))) {
1198 return true;
1199 }
1200 }
1201 return false;
1202 }
1203 }
Chris Wren1ada10d2013-09-13 18:01:38 -04001204}