blob: b40ace3fb4b60535248884756fc1bf0a3dcd1c9a [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 LauncherAppState mLauncherAppState;
150 private IconCache mIconCache;
151 private DeviceProfieData mDeviceProfileData;
152
Sunny Goyal33d44382014-10-16 09:24:19 -0700153 boolean restoreSuccessful;
Sunny Goyal08f72612015-01-05 13:41:43 -0800154 int restoredBackupVersion = 1;
Sunny Goyal33d44382014-10-16 09:24:19 -0700155
156 public LauncherBackupHelper(Context context) {
Chris Wren92aa4232013-10-04 11:29:36 -0400157 mContext = context;
Sunny Goyalef728d42014-10-22 11:28:28 -0700158 mExistingKeys = new HashSet<String>();
Sunny Goyal42de82f2014-09-26 22:09:29 -0700159 mKeys = new ArrayList<Key>();
Sunny Goyal33d44382014-10-16 09:24:19 -0700160 restoreSuccessful = true;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800161 mItemTypeMatchers = new ItemTypeMatcher[CommonAppTypeParser.SUPPORTED_TYPE_COUNT];
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800162
163 UserManagerCompat userManager = UserManagerCompat.getInstance(mContext);
164 mUserSerial = userManager.getSerialNumberForUser(UserHandleCompat.myUserHandle());
Chris Wren92aa4232013-10-04 11:29:36 -0400165 }
166
167 private void dataChanged() {
Sunny Goyalef728d42014-10-22 11:28:28 -0700168 if (mBackupManager == null) {
169 mBackupManager = new BackupManager(mContext);
Chris Wren1ada10d2013-09-13 18:01:38 -0400170 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700171 mBackupManager.dataChanged();
172 }
173
174 private void applyJournal(Journal journal) {
175 mLastBackupRestoreTime = journal.t;
176 mExistingKeys.clear();
177 if (journal.key != null) {
178 for (Key key : journal.key) {
179 mExistingKeys.add(keyToBackupKey(key));
180 }
181 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400182 }
183
184 /**
185 * Back up launcher data so we can restore the user's state on a new device.
186 *
187 * <P>The journal is a timestamp and a list of keys that were saved as of that time.
188 *
189 * <P>Keys may come back in any order, so each key/value is one complete row of the database.
190 *
191 * @param oldState notes from the last backup
192 * @param data incremental key/value pairs to persist off-device
193 * @param newState notes for the next backup
Chris Wren1ada10d2013-09-13 18:01:38 -0400194 */
195 @Override
Chris Wren92aa4232013-10-04 11:29:36 -0400196 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
197 ParcelFileDescriptor newState) {
Chris Wren50c8f422014-01-15 16:10:39 -0500198 if (VERBOSE) Log.v(TAG, "onBackup");
Chris Wren1ada10d2013-09-13 18:01:38 -0400199
200 Journal in = readJournal(oldState);
Sunny Goyalef728d42014-10-22 11:28:28 -0700201 if (!launcherIsReady()) {
Adam Cohen2e6da152015-05-06 11:42:25 -0700202 dataChanged();
Sunny Goyalef728d42014-10-22 11:28:28 -0700203 // Perform backup later.
204 writeJournal(newState, in);
205 return;
206 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700207
208 lazyInitAppState(true /* noCreate */);
209
Sunny Goyalef728d42014-10-22 11:28:28 -0700210 Log.v(TAG, "lastBackupTime = " + in.t);
211 mKeys.clear();
212 applyJournal(in);
Chris Wren1ada10d2013-09-13 18:01:38 -0400213
Sunny Goyalef728d42014-10-22 11:28:28 -0700214 // Record the time before performing backup so that entries edited while the backup
215 // was going on, do not get missed in next backup.
216 long newBackupTime = System.currentTimeMillis();
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800217 mBackupDataWasUpdated = false;
Sunny Goyalef728d42014-10-22 11:28:28 -0700218 try {
219 backupFavorites(data);
220 backupScreens(data);
221 backupIcons(data);
222 backupWidgets(data);
Chris Wren1ada10d2013-09-13 18:01:38 -0400223
Sunny Goyalef728d42014-10-22 11:28:28 -0700224 // Delete any key which still exist in the old backup, but is not valid anymore.
225 HashSet<String> validKeys = new HashSet<String>();
226 for (Key key : mKeys) {
227 validKeys.add(keyToBackupKey(key));
Chris Wren71144262014-02-27 15:49:39 -0500228 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700229 mExistingKeys.removeAll(validKeys);
230
231 // Delete anything left in the existing keys.
232 for (String deleted: mExistingKeys) {
233 if (VERBOSE) Log.v(TAG, "dropping deleted item " + deleted);
234 data.writeEntityHeader(deleted, -1);
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800235 mBackupDataWasUpdated = true;
Sunny Goyalef728d42014-10-22 11:28:28 -0700236 }
237
238 mExistingKeys.clear();
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800239 if (!mBackupDataWasUpdated) {
240 // Check if any metadata has changed
241 mBackupDataWasUpdated = (in.profile == null)
242 || !Arrays.equals(DeviceProfieData.toByteArray(in.profile),
243 DeviceProfieData.toByteArray(getDeviceProfieData()))
244 || (in.backupVersion != BACKUP_VERSION)
245 || (in.appVersion != getAppVersion());
246 }
Sunny Goyal33d44382014-10-16 09:24:19 -0700247
Sunny Goyalc0ee6752015-01-16 14:10:32 -0800248 if (mBackupDataWasUpdated) {
249 mLastBackupRestoreTime = newBackupTime;
250
251 // We store the journal at two places.
252 // 1) Storing it in newState allows us to do partial backups by comparing old state
253 // 2) Storing it in backup data allows us to validate keys during restore
254 Journal state = getCurrentStateJournal();
255 writeRowToBackup(JOURNAL_KEY, state, data);
256 } else {
257 if (DEBUG) Log.d(TAG, "Nothing was written during backup");
258 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700259 } catch (IOException e) {
260 Log.e(TAG, "launcher backup has failed", e);
Chris Wren92aa4232013-10-04 11:29:36 -0400261 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400262
Sunny Goyalef728d42014-10-22 11:28:28 -0700263 writeNewStateDescription(newState);
Chris Wren1ada10d2013-09-13 18:01:38 -0400264 }
265
266 /**
Sunny Goyal33d44382014-10-16 09:24:19 -0700267 * @return true if the backup corresponding to oldstate can be successfully applied
268 * to this device.
269 */
270 private boolean isBackupCompatible(Journal oldState) {
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700271 DeviceProfieData currentProfile = getDeviceProfieData();
272
273 DeviceProfieData oldProfile = oldState.profile;
274
275 if (oldProfile == null || oldProfile.desktopCols == 0) {
276 // Profile info is not valid, ignore the check.
277 return true;
278 }
279
280 boolean isHotsetCompatible = false;
281 if (currentProfile.allappsRank >= oldProfile.hotseatCount) {
282 isHotsetCompatible = true;
283 }
284 if ((currentProfile.hotseatCount >= oldProfile.hotseatCount) &&
285 (currentProfile.allappsRank == oldProfile.allappsRank)) {
286 isHotsetCompatible = true;
287 }
288
289 return isHotsetCompatible && (currentProfile.desktopCols >= oldProfile.desktopCols)
290 && (currentProfile.desktopRows >= oldProfile.desktopRows);
Sunny Goyal33d44382014-10-16 09:24:19 -0700291 }
292
293 /**
Chris Wren92aa4232013-10-04 11:29:36 -0400294 * Restore launcher configuration from the restored data stream.
Sunny Goyal33d44382014-10-16 09:24:19 -0700295 * It assumes that the keys will arrive in lexical order. So if the journal was present in the
296 * backup, it should arrive first.
Chris Wren1ada10d2013-09-13 18:01:38 -0400297 *
Chris Wren92aa4232013-10-04 11:29:36 -0400298 * @param data the key/value pair from the server
Chris Wren1ada10d2013-09-13 18:01:38 -0400299 */
300 @Override
Chris Wren92aa4232013-10-04 11:29:36 -0400301 public void restoreEntity(BackupDataInputStream data) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700302 if (!restoreSuccessful) {
303 return;
304 }
Adam Cohen2e6da152015-05-06 11:42:25 -0700305 lazyInitAppState(false /* noCreate */);
Sunny Goyal33d44382014-10-16 09:24:19 -0700306
Sunny Goyalef728d42014-10-22 11:28:28 -0700307 int dataSize = data.size();
308 if (mBuffer.length < dataSize) {
309 mBuffer = new byte[dataSize];
Chris Wren92aa4232013-10-04 11:29:36 -0400310 }
311 try {
Sunny Goyalef728d42014-10-22 11:28:28 -0700312 int bytesRead = data.read(mBuffer, 0, dataSize);
313 if (DEBUG) Log.d(TAG, "read " + bytesRead + " of " + dataSize + " available");
314 String backupKey = data.getKey();
Sunny Goyal33d44382014-10-16 09:24:19 -0700315
316 if (JOURNAL_KEY.equals(backupKey)) {
317 if (VERBOSE) Log.v(TAG, "Journal entry restored");
318 if (!mKeys.isEmpty()) {
319 // We received the journal key after a restore key.
320 Log.wtf(TAG, keyToBackupKey(mKeys.get(0)) + " received after " + JOURNAL_KEY);
321 restoreSuccessful = false;
322 return;
323 }
324
325 Journal journal = new Journal();
326 MessageNano.mergeFrom(journal, readCheckedBytes(mBuffer, dataSize));
327 applyJournal(journal);
328 restoreSuccessful = isBackupCompatible(journal);
Sunny Goyal08f72612015-01-05 13:41:43 -0800329 restoredBackupVersion = journal.backupVersion;
Sunny Goyal33d44382014-10-16 09:24:19 -0700330 return;
331 }
332
333 if (!mExistingKeys.isEmpty() && !mExistingKeys.contains(backupKey)) {
334 if (DEBUG) Log.e(TAG, "Ignoring key not present in the backup state " + backupKey);
335 return;
336 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700337 Key key = backupKeyToKey(backupKey);
Chris Wren50c8f422014-01-15 16:10:39 -0500338 mKeys.add(key);
Chris Wren92aa4232013-10-04 11:29:36 -0400339 switch (key.type) {
340 case Key.FAVORITE:
Sunny Goyalef728d42014-10-22 11:28:28 -0700341 restoreFavorite(key, mBuffer, dataSize);
Chris Wren92aa4232013-10-04 11:29:36 -0400342 break;
343
344 case Key.SCREEN:
Sunny Goyalef728d42014-10-22 11:28:28 -0700345 restoreScreen(key, mBuffer, dataSize);
Chris Wren92aa4232013-10-04 11:29:36 -0400346 break;
347
348 case Key.ICON:
Sunny Goyalef728d42014-10-22 11:28:28 -0700349 restoreIcon(key, mBuffer, dataSize);
Chris Wren92aa4232013-10-04 11:29:36 -0400350 break;
351
352 case Key.WIDGET:
Sunny Goyalef728d42014-10-22 11:28:28 -0700353 restoreWidget(key, mBuffer, dataSize);
Chris Wren92aa4232013-10-04 11:29:36 -0400354 break;
355
356 default:
357 Log.w(TAG, "unknown restore entity type: " + key.type);
Sunny Goyalef728d42014-10-22 11:28:28 -0700358 mKeys.remove(key);
Chris Wren92aa4232013-10-04 11:29:36 -0400359 break;
Chris Wren1ada10d2013-09-13 18:01:38 -0400360 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700361 } catch (IOException e) {
362 Log.w(TAG, "ignoring unparsable backup entry", e);
Chris Wren1ada10d2013-09-13 18:01:38 -0400363 }
Chris Wren92aa4232013-10-04 11:29:36 -0400364 }
365
366 /**
367 * Record the restore state for the next backup.
368 *
369 * @param newState notes about the backup state after restore.
370 */
371 @Override
372 public void writeNewStateDescription(ParcelFileDescriptor newState) {
Sunny Goyalef728d42014-10-22 11:28:28 -0700373 writeJournal(newState, getCurrentStateJournal());
374 }
375
376 private Journal getCurrentStateJournal() {
377 Journal journal = new Journal();
378 journal.t = mLastBackupRestoreTime;
379 journal.key = mKeys.toArray(new BackupProtos.Key[mKeys.size()]);
380 journal.appVersion = getAppVersion();
Sunny Goyal33d44382014-10-16 09:24:19 -0700381 journal.backupVersion = BACKUP_VERSION;
382 journal.profile = getDeviceProfieData();
Sunny Goyalef728d42014-10-22 11:28:28 -0700383 return journal;
384 }
385
386 private int getAppVersion() {
387 try {
388 return mContext.getPackageManager()
389 .getPackageInfo(mContext.getPackageName(), 0).versionCode;
390 } catch (NameNotFoundException e) {
391 return 0;
392 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400393 }
394
395 /**
Sunny Goyal33d44382014-10-16 09:24:19 -0700396 * @return the current device profile information.
397 */
398 private DeviceProfieData getDeviceProfieData() {
Adam Cohen2e6da152015-05-06 11:42:25 -0700399 return mDeviceProfileData;
400 }
Sunny Goyal33d44382014-10-16 09:24:19 -0700401
Adam Cohen2e6da152015-05-06 11:42:25 -0700402 private void lazyInitAppState(boolean noCreate) {
403 if (mLauncherAppState != null) {
404 return;
405 }
406
407 if (noCreate) {
408 mLauncherAppState = LauncherAppState.getInstanceNoCreate();
409 } else {
410 LauncherAppState.setApplicationContext(mContext);
411 mLauncherAppState = LauncherAppState.getInstance();
412 }
413
414 mIconCache = mLauncherAppState.getIconCache();
415 InvariantDeviceProfile profile = mLauncherAppState.getInvariantDeviceProfile();
416
417 mDeviceProfileData = initDeviceProfileData(profile);
418 }
419
420 private DeviceProfieData initDeviceProfileData(InvariantDeviceProfile profile) {
421 DeviceProfieData data = new DeviceProfieData();
422 data.desktopRows = profile.numRows;
423 data.desktopCols = profile.numColumns;
424 data.hotseatCount = profile.numHotseatIcons;
425 data.allappsRank = profile.hotseatAllAppsRank;
426 return data;
Sunny Goyal33d44382014-10-16 09:24:19 -0700427 }
428
429 /**
Chris Wren1ada10d2013-09-13 18:01:38 -0400430 * Write all modified favorites to the data stream.
431 *
Chris Wren1ada10d2013-09-13 18:01:38 -0400432 * @param data output stream for key/value pairs
Chris Wren1ada10d2013-09-13 18:01:38 -0400433 * @throws IOException
434 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700435 private void backupFavorites(BackupDataOutput data) throws IOException {
Chris Wren1ada10d2013-09-13 18:01:38 -0400436 // persist things that have changed since the last backup
Chris Wren92aa4232013-10-04 11:29:36 -0400437 ContentResolver cr = mContext.getContentResolver();
Sunny Goyalffe83f12014-08-14 17:39:34 -0700438 // Don't backup apps in other profiles for now.
Chris Wren22e130d2013-09-23 18:25:57 -0400439 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
Sunny Goyalffe83f12014-08-14 17:39:34 -0700440 getUserSelectionArg(), null, null);
Chris Wren1ada10d2013-09-13 18:01:38 -0400441 try {
Chris Wren22e130d2013-09-23 18:25:57 -0400442 cursor.moveToPosition(-1);
443 while(cursor.moveToNext()) {
444 final long id = cursor.getLong(ID_INDEX);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700445 final long updateTime = cursor.getLong(ID_MODIFIED);
446 Key key = getKey(Key.FAVORITE, id);
Sunny Goyalef728d42014-10-22 11:28:28 -0700447 mKeys.add(key);
Sunny Goyalffe83f12014-08-14 17:39:34 -0700448 final String backupKey = keyToBackupKey(key);
Sunny Goyalef728d42014-10-22 11:28:28 -0700449 if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) {
450 writeRowToBackup(key, packFavorite(cursor), data);
Chris Wren50c8f422014-01-15 16:10:39 -0500451 } else {
Sunny Goyalef728d42014-10-22 11:28:28 -0700452 if (DEBUG) Log.d(TAG, "favorite already backup up: " + id);
Chris Wren22e130d2013-09-23 18:25:57 -0400453 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400454 }
455 } finally {
Chris Wren22e130d2013-09-23 18:25:57 -0400456 cursor.close();
Chris Wren1ada10d2013-09-13 18:01:38 -0400457 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400458 }
459
460 /**
461 * Read a favorite from the stream.
462 *
463 * <P>Keys arrive in any order, so screens and containers may not exist yet.
464 *
465 * @param key identifier for the row
466 * @param buffer the serialized proto from the stream, may be larger than dataSize
467 * @param dataSize the size of the proto from the stream
Chris Wren1ada10d2013-09-13 18:01:38 -0400468 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700469 private void restoreFavorite(Key key, byte[] buffer, int dataSize) throws IOException {
Chris Wren50c8f422014-01-15 16:10:39 -0500470 if (VERBOSE) Log.v(TAG, "unpacking favorite " + key.id);
Chris Wren1ada10d2013-09-13 18:01:38 -0400471 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
472 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
473
Sunny Goyalef728d42014-10-22 11:28:28 -0700474 ContentResolver cr = mContext.getContentResolver();
475 ContentValues values = unpackFavorite(buffer, dataSize);
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700476 cr.insert(Favorites.CONTENT_URI, values);
Chris Wren1ada10d2013-09-13 18:01:38 -0400477 }
478
479 /**
480 * Write all modified screens to the data stream.
481 *
Chris Wren1ada10d2013-09-13 18:01:38 -0400482 * @param data output stream for key/value pairs
Chris Wren22e130d2013-09-23 18:25:57 -0400483 * @throws IOException
Chris Wren1ada10d2013-09-13 18:01:38 -0400484 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700485 private void backupScreens(BackupDataOutput data) throws IOException {
Chris Wren1ada10d2013-09-13 18:01:38 -0400486 // persist things that have changed since the last backup
Chris Wren92aa4232013-10-04 11:29:36 -0400487 ContentResolver cr = mContext.getContentResolver();
Chris Wren22e130d2013-09-23 18:25:57 -0400488 Cursor cursor = cr.query(WorkspaceScreens.CONTENT_URI, SCREEN_PROJECTION,
489 null, null, null);
Chris Wren1ada10d2013-09-13 18:01:38 -0400490 try {
Chris Wren22e130d2013-09-23 18:25:57 -0400491 cursor.moveToPosition(-1);
Sunny Goyalef728d42014-10-22 11:28:28 -0700492 if (DEBUG) Log.d(TAG, "dumping screens after: " + mLastBackupRestoreTime);
Chris Wren22e130d2013-09-23 18:25:57 -0400493 while(cursor.moveToNext()) {
494 final long id = cursor.getLong(ID_INDEX);
495 final long updateTime = cursor.getLong(ID_MODIFIED);
Chris Wren1ada10d2013-09-13 18:01:38 -0400496 Key key = getKey(Key.SCREEN, id);
Sunny Goyalef728d42014-10-22 11:28:28 -0700497 mKeys.add(key);
Chris Wren5743aa92014-01-10 18:02:06 -0500498 final String backupKey = keyToBackupKey(key);
Sunny Goyalef728d42014-10-22 11:28:28 -0700499 if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) {
500 writeRowToBackup(key, packScreen(cursor), data);
Chris Wren5dee7af2013-12-20 17:22:11 -0500501 } else {
Sunny Goyalef728d42014-10-22 11:28:28 -0700502 if (VERBOSE) Log.v(TAG, "screen already backup up " + id);
Chris Wren22e130d2013-09-23 18:25:57 -0400503 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400504 }
505 } finally {
Chris Wren22e130d2013-09-23 18:25:57 -0400506 cursor.close();
Chris Wren1ada10d2013-09-13 18:01:38 -0400507 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400508 }
509
510 /**
511 * Read a screen from the stream.
512 *
513 * <P>Keys arrive in any order, so children of this screen may already exist.
514 *
515 * @param key identifier for the row
516 * @param buffer the serialized proto from the stream, may be larger than dataSize
517 * @param dataSize the size of the proto from the stream
Chris Wren1ada10d2013-09-13 18:01:38 -0400518 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700519 private void restoreScreen(Key key, byte[] buffer, int dataSize) throws IOException {
Chris Wren50c8f422014-01-15 16:10:39 -0500520 if (VERBOSE) Log.v(TAG, "unpacking screen " + key.id);
Chris Wren1ada10d2013-09-13 18:01:38 -0400521 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
522 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
Chris Wren5dee7af2013-12-20 17:22:11 -0500523
Sunny Goyalef728d42014-10-22 11:28:28 -0700524 ContentResolver cr = mContext.getContentResolver();
525 ContentValues values = unpackScreen(buffer, dataSize);
526 cr.insert(WorkspaceScreens.CONTENT_URI, values);
Chris Wren1ada10d2013-09-13 18:01:38 -0400527 }
528
Chris Wren22e130d2013-09-23 18:25:57 -0400529 /**
530 * Write all the static icon resources we need to render placeholders
531 * for a package that is not installed.
532 *
Chris Wren22e130d2013-09-23 18:25:57 -0400533 * @param data output stream for key/value pairs
Chris Wren22e130d2013-09-23 18:25:57 -0400534 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700535 private void backupIcons(BackupDataOutput data) throws IOException {
Chris Wrenfd13c712013-09-27 15:45:19 -0400536 // persist icons that haven't been persisted yet
Chris Wren92aa4232013-10-04 11:29:36 -0400537 final ContentResolver cr = mContext.getContentResolver();
Chris Wren92aa4232013-10-04 11:29:36 -0400538 final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700539 final UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle();
Sunny Goyalef728d42014-10-22 11:28:28 -0700540 int backupUpIconCount = 0;
Chris Wren22e130d2013-09-23 18:25:57 -0400541
Kenny Guyed131872014-04-30 03:02:21 +0100542 // Don't backup apps in other profiles for now.
Kenny Guyed131872014-04-30 03:02:21 +0100543 String where = "(" + Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPLICATION + " OR " +
Kenny Guy43ea7ac2014-05-09 16:44:18 +0100544 Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_SHORTCUT + ") AND " +
Sunny Goyalffe83f12014-08-14 17:39:34 -0700545 getUserSelectionArg();
Chris Wren22e130d2013-09-23 18:25:57 -0400546 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
547 where, null, null);
Chris Wren22e130d2013-09-23 18:25:57 -0400548 try {
549 cursor.moveToPosition(-1);
550 while(cursor.moveToNext()) {
551 final long id = cursor.getLong(ID_INDEX);
552 final String intentDescription = cursor.getString(INTENT_INDEX);
553 try {
554 Intent intent = Intent.parseUri(intentDescription, 0);
555 ComponentName cn = intent.getComponent();
556 Key key = null;
557 String backupKey = null;
558 if (cn != null) {
559 key = getKey(Key.ICON, cn.flattenToShortString());
560 backupKey = keyToBackupKey(key);
Chris Wren22e130d2013-09-23 18:25:57 -0400561 } else {
562 Log.w(TAG, "empty intent on application favorite: " + id);
563 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700564 if (mExistingKeys.contains(backupKey)) {
565 if (DEBUG) Log.d(TAG, "already saved icon " + backupKey);
Chris Wren22e130d2013-09-23 18:25:57 -0400566
567 // remember that we already backed this up previously
Sunny Goyalef728d42014-10-22 11:28:28 -0700568 mKeys.add(key);
Chris Wren22e130d2013-09-23 18:25:57 -0400569 } else if (backupKey != null) {
Sunny Goyalef728d42014-10-22 11:28:28 -0700570 if (DEBUG) Log.d(TAG, "I can count this high: " + backupUpIconCount);
571 if (backupUpIconCount < MAX_ICONS_PER_PASS) {
572 if (DEBUG) Log.d(TAG, "saving icon " + backupKey);
Kenny Guyed131872014-04-30 03:02:21 +0100573 Bitmap icon = mIconCache.getIcon(intent, myUserHandle);
Kenny Guyed131872014-04-30 03:02:21 +0100574 if (icon != null && !mIconCache.isDefaultIcon(icon, myUserHandle)) {
Sunny Goyalef728d42014-10-22 11:28:28 -0700575 writeRowToBackup(key, packIcon(dpi, icon), data);
576 mKeys.add(key);
577 backupUpIconCount ++;
Chris Wren22e130d2013-09-23 18:25:57 -0400578 }
579 } else {
Sunny Goyalef728d42014-10-22 11:28:28 -0700580 if (VERBOSE) Log.v(TAG, "deferring icon backup " + backupKey);
Chris Wren22e130d2013-09-23 18:25:57 -0400581 // too many icons for this pass, request another.
Chris Wren92aa4232013-10-04 11:29:36 -0400582 dataChanged();
Chris Wren22e130d2013-09-23 18:25:57 -0400583 }
584 }
585 } catch (URISyntaxException e) {
Chris Wren50c8f422014-01-15 16:10:39 -0500586 Log.e(TAG, "invalid URI on application favorite: " + id);
Chris Wren22e130d2013-09-23 18:25:57 -0400587 } catch (IOException e) {
Chris Wren50c8f422014-01-15 16:10:39 -0500588 Log.e(TAG, "unable to save application icon for favorite: " + id);
Chris Wren22e130d2013-09-23 18:25:57 -0400589 }
590
591 }
592 } finally {
593 cursor.close();
594 }
Chris Wren22e130d2013-09-23 18:25:57 -0400595 }
596
597 /**
598 * Read an icon from the stream.
599 *
Chris Wrenfd13c712013-09-27 15:45:19 -0400600 * <P>Keys arrive in any order, so shortcuts that use this icon may already exist.
Chris Wren22e130d2013-09-23 18:25:57 -0400601 *
602 * @param key identifier for the row
603 * @param buffer the serialized proto from the stream, may be larger than dataSize
604 * @param dataSize the size of the proto from the stream
Chris Wren22e130d2013-09-23 18:25:57 -0400605 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700606 private void restoreIcon(Key key, byte[] buffer, int dataSize) throws IOException {
Chris Wren50c8f422014-01-15 16:10:39 -0500607 if (VERBOSE) Log.v(TAG, "unpacking icon " + key.id);
Chris Wren22e130d2013-09-23 18:25:57 -0400608 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
609 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
Chris Wren6d0dde02014-02-10 12:16:54 -0500610
Sunny Goyalef728d42014-10-22 11:28:28 -0700611 Resource res = unpackProto(new Resource(), buffer, dataSize);
612 if (DEBUG) {
613 Log.d(TAG, "unpacked " + res.dpi + " dpi icon");
Chris Wren22e130d2013-09-23 18:25:57 -0400614 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700615 Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length);
616 if (icon == null) {
617 Log.w(TAG, "failed to unpack icon for " + key.name);
618 }
619 if (VERBOSE) Log.v(TAG, "saving restored icon as: " + key.name);
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800620 mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi,
621 "" /* label */, mUserSerial);
Chris Wren22e130d2013-09-23 18:25:57 -0400622 }
623
Chris Wrenfd13c712013-09-27 15:45:19 -0400624 /**
625 * Write all the static widget resources we need to render placeholders
626 * for a package that is not installed.
627 *
Chris Wrenfd13c712013-09-27 15:45:19 -0400628 * @param data output stream for key/value pairs
Chris Wrenfd13c712013-09-27 15:45:19 -0400629 * @throws IOException
630 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700631 private void backupWidgets(BackupDataOutput data) throws IOException {
Chris Wrenfd13c712013-09-27 15:45:19 -0400632 // persist static widget info that hasn't been persisted yet
Chris Wren92aa4232013-10-04 11:29:36 -0400633 final ContentResolver cr = mContext.getContentResolver();
Adam Cohen2e6da152015-05-06 11:42:25 -0700634 final WidgetPreviewLoader previewLoader = mLauncherAppState.getWidgetCache();
Chris Wren92aa4232013-10-04 11:29:36 -0400635 final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
Sunny Goyalef728d42014-10-22 11:28:28 -0700636 int backupWidgetCount = 0;
Chris Wrenfd13c712013-09-27 15:45:19 -0400637
Sunny Goyalffe83f12014-08-14 17:39:34 -0700638 String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET + " AND "
639 + getUserSelectionArg();
Chris Wrenfd13c712013-09-27 15:45:19 -0400640 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
641 where, null, null);
Chris Wrenfd13c712013-09-27 15:45:19 -0400642 try {
643 cursor.moveToPosition(-1);
644 while(cursor.moveToNext()) {
645 final long id = cursor.getLong(ID_INDEX);
646 final String providerName = cursor.getString(APPWIDGET_PROVIDER_INDEX);
647 final int spanX = cursor.getInt(SPANX_INDEX);
Chris Wrenfd13c712013-09-27 15:45:19 -0400648 final ComponentName provider = ComponentName.unflattenFromString(providerName);
649 Key key = null;
650 String backupKey = null;
651 if (provider != null) {
652 key = getKey(Key.WIDGET, providerName);
653 backupKey = keyToBackupKey(key);
Chris Wrenfd13c712013-09-27 15:45:19 -0400654 } else {
655 Log.w(TAG, "empty intent on appwidget: " + id);
656 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700657 if (mExistingKeys.contains(backupKey)) {
658 if (DEBUG) Log.d(TAG, "already saved widget " + backupKey);
Chris Wrenfd13c712013-09-27 15:45:19 -0400659
660 // remember that we already backed this up previously
Sunny Goyalef728d42014-10-22 11:28:28 -0700661 mKeys.add(key);
Chris Wrenfd13c712013-09-27 15:45:19 -0400662 } else if (backupKey != null) {
Sunny Goyalef728d42014-10-22 11:28:28 -0700663 if (DEBUG) Log.d(TAG, "I can count this high: " + backupWidgetCount);
664 if (backupWidgetCount < MAX_WIDGETS_PER_PASS) {
665 if (DEBUG) Log.d(TAG, "saving widget " + backupKey);
Robin Lee26ace122015-03-16 19:41:43 +0000666 UserHandleCompat user = UserHandleCompat.myUserHandle();
667 writeRowToBackup(key,
Adam Cohen2e6da152015-05-06 11:42:25 -0700668 packWidget(dpi, previewLoader, mIconCache, provider, user),
Robin Lee26ace122015-03-16 19:41:43 +0000669 data);
Sunny Goyalef728d42014-10-22 11:28:28 -0700670 mKeys.add(key);
671 backupWidgetCount ++;
Chris Wrenfd13c712013-09-27 15:45:19 -0400672 } else {
Sunny Goyalef728d42014-10-22 11:28:28 -0700673 if (VERBOSE) Log.v(TAG, "deferring widget backup " + backupKey);
Chris Wrenfd13c712013-09-27 15:45:19 -0400674 // too many widgets for this pass, request another.
Chris Wren92aa4232013-10-04 11:29:36 -0400675 dataChanged();
Chris Wrenfd13c712013-09-27 15:45:19 -0400676 }
677 }
678 }
679 } finally {
680 cursor.close();
681 }
Chris Wrenfd13c712013-09-27 15:45:19 -0400682 }
683
684 /**
685 * Read a widget from the stream.
686 *
687 * <P>Keys arrive in any order, so widgets that use this data may already exist.
688 *
689 * @param key identifier for the row
690 * @param buffer the serialized proto from the stream, may be larger than dataSize
691 * @param dataSize the size of the proto from the stream
Chris Wrenfd13c712013-09-27 15:45:19 -0400692 */
Sunny Goyalef728d42014-10-22 11:28:28 -0700693 private void restoreWidget(Key key, byte[] buffer, int dataSize) throws IOException {
Chris Wren50c8f422014-01-15 16:10:39 -0500694 if (VERBOSE) Log.v(TAG, "unpacking widget " + key.id);
Chris Wrenfd13c712013-09-27 15:45:19 -0400695 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
696 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
Sunny Goyalef728d42014-10-22 11:28:28 -0700697 Widget widget = unpackProto(new Widget(), buffer, dataSize);
698 if (DEBUG) Log.d(TAG, "unpacked " + widget.provider);
699 if (widget.icon.data != null) {
700 Bitmap icon = BitmapFactory
701 .decodeByteArray(widget.icon.data, 0, widget.icon.data.length);
702 if (icon == null) {
703 Log.w(TAG, "failed to unpack widget icon for " + key.name);
Chris Wren5dee7af2013-12-20 17:22:11 -0500704 } else {
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800705 mIconCache.preloadIcon(ComponentName.unflattenFromString(widget.provider),
706 icon, widget.icon.dpi, widget.label, mUserSerial);
Chris Wren5dee7af2013-12-20 17:22:11 -0500707 }
Chris Wrenfd13c712013-09-27 15:45:19 -0400708 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700709
710 // future site of widget table mutation
Chris Wrenfd13c712013-09-27 15:45:19 -0400711 }
712
Chris Wren22e130d2013-09-23 18:25:57 -0400713 /** create a new key, with an integer ID.
Chris Wren1ada10d2013-09-13 18:01:38 -0400714 *
715 * <P> Keys contain their own checksum instead of using
716 * the heavy-weight CheckedMessage wrapper.
717 */
718 private Key getKey(int type, long id) {
719 Key key = new Key();
720 key.type = type;
721 key.id = id;
722 key.checksum = checkKey(key);
723 return key;
724 }
725
Chris Wren22e130d2013-09-23 18:25:57 -0400726 /** create a new key for a named object.
727 *
728 * <P> Keys contain their own checksum instead of using
729 * the heavy-weight CheckedMessage wrapper.
730 */
731 private Key getKey(int type, String name) {
732 Key key = new Key();
733 key.type = type;
734 key.name = name;
735 key.checksum = checkKey(key);
736 return key;
737 }
738
Chris Wren1ada10d2013-09-13 18:01:38 -0400739 /** keys need to be strings, serialize and encode. */
740 private String keyToBackupKey(Key key) {
Chris Wren978194c2013-10-03 17:47:22 -0400741 return Base64.encodeToString(Key.toByteArray(key), Base64.NO_WRAP);
Chris Wren1ada10d2013-09-13 18:01:38 -0400742 }
743
744 /** keys need to be strings, decode and parse. */
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700745 private Key backupKeyToKey(String backupKey) throws InvalidBackupException {
Chris Wren1ada10d2013-09-13 18:01:38 -0400746 try {
747 Key key = Key.parseFrom(Base64.decode(backupKey, Base64.DEFAULT));
748 if (key.checksum != checkKey(key)) {
749 key = null;
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700750 throw new InvalidBackupException("invalid key read from stream" + backupKey);
Chris Wren1ada10d2013-09-13 18:01:38 -0400751 }
752 return key;
753 } catch (InvalidProtocolBufferNanoException e) {
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700754 throw new InvalidBackupException(e);
Chris Wren1ada10d2013-09-13 18:01:38 -0400755 } catch (IllegalArgumentException e) {
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700756 throw new InvalidBackupException(e);
Chris Wren1ada10d2013-09-13 18:01:38 -0400757 }
758 }
759
760 /** Compute the checksum over the important bits of a key. */
761 private long checkKey(Key key) {
762 CRC32 checksum = new CRC32();
763 checksum.update(key.type);
764 checksum.update((int) (key.id & 0xffff));
765 checksum.update((int) ((key.id >> 32) & 0xffff));
766 if (!TextUtils.isEmpty(key.name)) {
767 checksum.update(key.name.getBytes());
768 }
769 return checksum.getValue();
770 }
771
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800772 /**
773 * @return true if its an hotseat item, that can be replaced during restore.
774 * TODO: Extend check for folders in hotseat.
775 */
776 private boolean isReplaceableHotseatItem(Favorite favorite) {
777 return favorite.container == Favorites.CONTAINER_HOTSEAT
778 && favorite.intent != null
779 && (favorite.itemType == Favorites.ITEM_TYPE_APPLICATION
780 || favorite.itemType == Favorites.ITEM_TYPE_SHORTCUT);
781 }
782
Chris Wren1ada10d2013-09-13 18:01:38 -0400783 /** Serialize a Favorite for persistence, including a checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700784 private Favorite packFavorite(Cursor c) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400785 Favorite favorite = new Favorite();
786 favorite.id = c.getLong(ID_INDEX);
787 favorite.screen = c.getInt(SCREEN_INDEX);
788 favorite.container = c.getInt(CONTAINER_INDEX);
789 favorite.cellX = c.getInt(CELLX_INDEX);
790 favorite.cellY = c.getInt(CELLY_INDEX);
791 favorite.spanX = c.getInt(SPANX_INDEX);
792 favorite.spanY = c.getInt(SPANY_INDEX);
793 favorite.iconType = c.getInt(ICON_TYPE_INDEX);
794 if (favorite.iconType == Favorites.ICON_TYPE_RESOURCE) {
795 String iconPackage = c.getString(ICON_PACKAGE_INDEX);
796 if (!TextUtils.isEmpty(iconPackage)) {
797 favorite.iconPackage = iconPackage;
798 }
799 String iconResource = c.getString(ICON_RESOURCE_INDEX);
800 if (!TextUtils.isEmpty(iconResource)) {
801 favorite.iconResource = iconResource;
802 }
803 }
804 if (favorite.iconType == Favorites.ICON_TYPE_BITMAP) {
805 byte[] blob = c.getBlob(ICON_INDEX);
806 if (blob != null && blob.length > 0) {
807 favorite.icon = blob;
808 }
809 }
810 String title = c.getString(TITLE_INDEX);
811 if (!TextUtils.isEmpty(title)) {
812 favorite.title = title;
813 }
Kenny Guyf8b1dfd2014-05-13 12:59:34 +0100814 String intentDescription = c.getString(INTENT_INDEX);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800815 Intent intent = null;
Kenny Guyf8b1dfd2014-05-13 12:59:34 +0100816 if (!TextUtils.isEmpty(intentDescription)) {
817 try {
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800818 intent = Intent.parseUri(intentDescription, 0);
Kenny Guyf8b1dfd2014-05-13 12:59:34 +0100819 intent.removeExtra(ItemInfo.EXTRA_PROFILE);
820 favorite.intent = intent.toUri(0);
821 } catch (URISyntaxException e) {
822 Log.e(TAG, "Invalid intent", e);
Sunny Goyalef728d42014-10-22 11:28:28 -0700823 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400824 }
825 favorite.itemType = c.getInt(ITEM_TYPE_INDEX);
826 if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) {
827 favorite.appWidgetId = c.getInt(APPWIDGET_ID_INDEX);
828 String appWidgetProvider = c.getString(APPWIDGET_PROVIDER_INDEX);
829 if (!TextUtils.isEmpty(appWidgetProvider)) {
830 favorite.appWidgetProvider = appWidgetProvider;
831 }
832 }
833
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800834 if (isReplaceableHotseatItem(favorite)) {
835 if (intent != null && intent.getComponent() != null) {
836 PackageManager pm = mContext.getPackageManager();
837 ActivityInfo activity = null;;
838 try {
839 activity = pm.getActivityInfo(intent.getComponent(), 0);
840 } catch (NameNotFoundException e) {
841 Log.e(TAG, "Target not found", e);
842 }
843 if (activity == null) {
844 return favorite;
845 }
846 for (int i = 0; i < mItemTypeMatchers.length; i++) {
847 if (mItemTypeMatchers[i] == null) {
848 mItemTypeMatchers[i] = new ItemTypeMatcher(
849 CommonAppTypeParser.getResourceForItemType(i));
850 }
851 if (mItemTypeMatchers[i].matches(activity, pm)) {
852 favorite.targetType = i;
853 break;
854 }
855 }
856 }
857 }
858
Sunny Goyalef728d42014-10-22 11:28:28 -0700859 return favorite;
Chris Wren1ada10d2013-09-13 18:01:38 -0400860 }
861
862 /** Deserialize a Favorite from persistence, after verifying checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700863 private ContentValues unpackFavorite(byte[] buffer, int dataSize)
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700864 throws IOException {
Sunny Goyalef728d42014-10-22 11:28:28 -0700865 Favorite favorite = unpackProto(new Favorite(), buffer, dataSize);
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800866
Chris Wren5dee7af2013-12-20 17:22:11 -0500867 ContentValues values = new ContentValues();
868 values.put(Favorites._ID, favorite.id);
869 values.put(Favorites.SCREEN, favorite.screen);
870 values.put(Favorites.CONTAINER, favorite.container);
871 values.put(Favorites.CELLX, favorite.cellX);
872 values.put(Favorites.CELLY, favorite.cellY);
873 values.put(Favorites.SPANX, favorite.spanX);
874 values.put(Favorites.SPANY, favorite.spanY);
875 values.put(Favorites.ICON_TYPE, favorite.iconType);
876 if (favorite.iconType == Favorites.ICON_TYPE_RESOURCE) {
877 values.put(Favorites.ICON_PACKAGE, favorite.iconPackage);
878 values.put(Favorites.ICON_RESOURCE, favorite.iconResource);
879 }
880 if (favorite.iconType == Favorites.ICON_TYPE_BITMAP) {
881 values.put(Favorites.ICON, favorite.icon);
882 }
883 if (!TextUtils.isEmpty(favorite.title)) {
884 values.put(Favorites.TITLE, favorite.title);
885 } else {
886 values.put(Favorites.TITLE, "");
887 }
888 if (!TextUtils.isEmpty(favorite.intent)) {
889 values.put(Favorites.INTENT, favorite.intent);
890 }
891 values.put(Favorites.ITEM_TYPE, favorite.itemType);
Chris Wrenf4d08112014-01-16 18:13:56 -0500892
Kenny Guyf8b1dfd2014-05-13 12:59:34 +0100893 UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle();
894 long userSerialNumber =
895 UserManagerCompat.getInstance(mContext).getSerialNumberForUser(myUserHandle);
896 values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber);
897
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700898 DeviceProfieData currentProfile = getDeviceProfieData();
899
Sunny Goyalff572272014-07-23 13:58:07 -0700900 if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) {
901 if (!TextUtils.isEmpty(favorite.appWidgetProvider)) {
902 values.put(Favorites.APPWIDGET_PROVIDER, favorite.appWidgetProvider);
903 }
904 values.put(Favorites.APPWIDGET_ID, favorite.appWidgetId);
905 values.put(LauncherSettings.Favorites.RESTORED,
906 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID |
907 LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY |
908 LauncherAppWidgetInfo.FLAG_UI_NOT_READY);
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700909
910 // Verify placement
911 if (((favorite.cellX + favorite.spanX) > currentProfile.desktopCols)
912 || ((favorite.cellY + favorite.spanY) > currentProfile.desktopRows)) {
913 restoreSuccessful = false;
914 throw new InvalidBackupException("Widget not in screen bounds, aborting restore");
915 }
Sunny Goyalff572272014-07-23 13:58:07 -0700916 } else {
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800917 // Check if it is an hotseat item, that can be replaced.
918 if (isReplaceableHotseatItem(favorite)
919 && favorite.targetType != Favorite.TARGET_NONE
920 && favorite.targetType < CommonAppTypeParser.SUPPORTED_TYPE_COUNT) {
921 Log.e(TAG, "Added item type flag");
922 values.put(LauncherSettings.Favorites.RESTORED,
923 1 | CommonAppTypeParser.encodeItemTypeToFlag(favorite.targetType));
924 } else {
925 // Let LauncherModel know we've been here.
926 values.put(LauncherSettings.Favorites.RESTORED, 1);
927 }
Sunny Goyal5fd733d2014-10-29 10:51:54 -0700928
929 // Verify placement
930 if (favorite.container == Favorites.CONTAINER_HOTSEAT) {
931 if ((favorite.screen >= currentProfile.hotseatCount)
932 || (favorite.screen == currentProfile.allappsRank)) {
933 restoreSuccessful = false;
934 throw new InvalidBackupException("Item not in hotseat bounds, aborting restore");
935 }
936 } else {
937 if ((favorite.cellX >= currentProfile.desktopCols)
938 || (favorite.cellY >= currentProfile.desktopRows)) {
939 restoreSuccessful = false;
940 throw new InvalidBackupException("Item not in desktop bounds, aborting restore");
941 }
942 }
Sunny Goyalff572272014-07-23 13:58:07 -0700943 }
Chris Wrenf4d08112014-01-16 18:13:56 -0500944
Chris Wren5dee7af2013-12-20 17:22:11 -0500945 return values;
Chris Wren1ada10d2013-09-13 18:01:38 -0400946 }
947
948 /** Serialize a Screen for persistence, including a checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700949 private Screen packScreen(Cursor c) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400950 Screen screen = new Screen();
951 screen.id = c.getLong(ID_INDEX);
952 screen.rank = c.getInt(SCREEN_RANK_INDEX);
Sunny Goyalef728d42014-10-22 11:28:28 -0700953 return screen;
Chris Wren1ada10d2013-09-13 18:01:38 -0400954 }
955
956 /** Deserialize a Screen from persistence, after verifying checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700957 private ContentValues unpackScreen(byte[] buffer, int dataSize)
Chris Wren1ada10d2013-09-13 18:01:38 -0400958 throws InvalidProtocolBufferNanoException {
Sunny Goyalef728d42014-10-22 11:28:28 -0700959 Screen screen = unpackProto(new Screen(), buffer, dataSize);
Chris Wren5dee7af2013-12-20 17:22:11 -0500960 ContentValues values = new ContentValues();
961 values.put(WorkspaceScreens._ID, screen.id);
962 values.put(WorkspaceScreens.SCREEN_RANK, screen.rank);
963 return values;
Chris Wren1ada10d2013-09-13 18:01:38 -0400964 }
965
Chris Wren22e130d2013-09-23 18:25:57 -0400966 /** Serialize an icon Resource for persistence, including a checksum wrapper. */
Sunny Goyalef728d42014-10-22 11:28:28 -0700967 private Resource packIcon(int dpi, Bitmap icon) {
Chris Wren22e130d2013-09-23 18:25:57 -0400968 Resource res = new Resource();
969 res.dpi = dpi;
Sunny Goyal73a22a52015-04-28 16:51:09 -0700970 res.data = Utilities.flattenBitmap(icon);
Chris Wren22e130d2013-09-23 18:25:57 -0400971 return res;
972 }
973
Chris Wrenfd13c712013-09-27 15:45:19 -0400974 /** Serialize a widget for persistence, including a checksum wrapper. */
Adam Cohen2e6da152015-05-06 11:42:25 -0700975 private Widget packWidget(int dpi, WidgetPreviewLoader previewLoader, IconCache iconCache,
Robin Lee26ace122015-03-16 19:41:43 +0000976 ComponentName provider, UserHandleCompat user) {
Adam Cohen59400422014-03-05 18:07:04 -0800977 final LauncherAppWidgetProviderInfo info =
Robin Lee26ace122015-03-16 19:41:43 +0000978 LauncherModel.getProviderInfo(mContext, provider, user);
Chris Wrenfd13c712013-09-27 15:45:19 -0400979 Widget widget = new Widget();
980 widget.provider = provider.flattenToShortString();
981 widget.label = info.label;
982 widget.configure = info.configure != null;
983 if (info.icon != 0) {
984 widget.icon = new Resource();
985 Drawable fullResIcon = iconCache.getFullResIcon(provider.getPackageName(), info.icon);
Chris Wren92aa4232013-10-04 11:29:36 -0400986 Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext);
Sunny Goyal73a22a52015-04-28 16:51:09 -0700987 widget.icon.data = Utilities.flattenBitmap(icon);
988 widget.icon.dpi = dpi;
Chris Wrenfd13c712013-09-27 15:45:19 -0400989 }
Sunny Goyalef728d42014-10-22 11:28:28 -0700990 return widget;
Chris Wrenfd13c712013-09-27 15:45:19 -0400991 }
992
Sunny Goyalef728d42014-10-22 11:28:28 -0700993 /**
994 * Deserialize a proto after verifying checksum wrapper.
995 */
996 private <T extends MessageNano> T unpackProto(T proto, byte[] buffer, int dataSize)
Chris Wrenfd13c712013-09-27 15:45:19 -0400997 throws InvalidProtocolBufferNanoException {
Sunny Goyalef728d42014-10-22 11:28:28 -0700998 MessageNano.mergeFrom(proto, readCheckedBytes(buffer, dataSize));
999 if (DEBUG) Log.d(TAG, "unpacked proto " + proto);
1000 return proto;
Chris Wrenfd13c712013-09-27 15:45:19 -04001001 }
1002
Chris Wren1ada10d2013-09-13 18:01:38 -04001003 /**
1004 * Read the old journal from the input file.
1005 *
1006 * In the event of any error, just pretend we didn't have a journal,
1007 * in that case, do a full backup.
1008 *
1009 * @param oldState the read-0only file descriptor pointing to the old journal
Chris Wren65b6a602014-01-10 14:11:25 -05001010 * @return a Journal protocol buffer
Chris Wren1ada10d2013-09-13 18:01:38 -04001011 */
1012 private Journal readJournal(ParcelFileDescriptor oldState) {
Chris Wren1ada10d2013-09-13 18:01:38 -04001013 Journal journal = new Journal();
Chris Wren92aa4232013-10-04 11:29:36 -04001014 if (oldState == null) {
1015 return journal;
1016 }
1017 FileInputStream inStream = new FileInputStream(oldState.getFileDescriptor());
1018 try {
Chris Wren65b6a602014-01-10 14:11:25 -05001019 int availableBytes = inStream.available();
1020 if (DEBUG) Log.d(TAG, "available " + availableBytes);
1021 if (availableBytes < MAX_JOURNAL_SIZE) {
1022 byte[] buffer = new byte[availableBytes];
Chris Wren1ada10d2013-09-13 18:01:38 -04001023 int bytesRead = 0;
Chris Wren65b6a602014-01-10 14:11:25 -05001024 boolean valid = false;
Chris Wren50c8f422014-01-15 16:10:39 -05001025 InvalidProtocolBufferNanoException lastProtoException = null;
Chris Wren65b6a602014-01-10 14:11:25 -05001026 while (availableBytes > 0) {
Chris Wren92aa4232013-10-04 11:29:36 -04001027 try {
Chris Wren65b6a602014-01-10 14:11:25 -05001028 // OMG what are you doing? This is crazy inefficient!
1029 // If we read a byte that is not ours, we will cause trouble: b/12491813
1030 // However, we don't know how many bytes to expect (oops).
1031 // So we have to step through *slowly*, watching for the end.
1032 int result = inStream.read(buffer, bytesRead, 1);
Chris Wren92aa4232013-10-04 11:29:36 -04001033 if (result > 0) {
Chris Wren65b6a602014-01-10 14:11:25 -05001034 availableBytes -= result;
Chris Wren92aa4232013-10-04 11:29:36 -04001035 bytesRead += result;
1036 } else {
Chris Wren65b6a602014-01-10 14:11:25 -05001037 Log.w(TAG, "unexpected end of file while reading journal.");
1038 // stop reading and see what there is to parse
1039 availableBytes = 0;
Chris Wren92aa4232013-10-04 11:29:36 -04001040 }
1041 } catch (IOException e) {
Chris Wren92aa4232013-10-04 11:29:36 -04001042 buffer = null;
Chris Wren65b6a602014-01-10 14:11:25 -05001043 availableBytes = 0;
1044 }
1045
1046 // check the buffer to see if we have a valid journal
1047 try {
Sunny Goyalef728d42014-10-22 11:28:28 -07001048 MessageNano.mergeFrom(journal, readCheckedBytes(buffer, bytesRead));
Chris Wren65b6a602014-01-10 14:11:25 -05001049 // if we are here, then we have read a valid, checksum-verified journal
1050 valid = true;
1051 availableBytes = 0;
Chris Wren50c8f422014-01-15 16:10:39 -05001052 if (VERBOSE) Log.v(TAG, "read " + bytesRead + " bytes of journal");
Chris Wren65b6a602014-01-10 14:11:25 -05001053 } catch (InvalidProtocolBufferNanoException e) {
1054 // if we don't have the whole journal yet, mergeFrom will throw. keep going.
Chris Wren50c8f422014-01-15 16:10:39 -05001055 lastProtoException = e;
Chris Wren65b6a602014-01-10 14:11:25 -05001056 journal.clear();
Chris Wren92aa4232013-10-04 11:29:36 -04001057 }
Chris Wren1ada10d2013-09-13 18:01:38 -04001058 }
Chris Wren92aa4232013-10-04 11:29:36 -04001059 if (DEBUG) Log.d(TAG, "journal bytes read: " + bytesRead);
Chris Wren65b6a602014-01-10 14:11:25 -05001060 if (!valid) {
Chris Wren50c8f422014-01-15 16:10:39 -05001061 Log.w(TAG, "could not find a valid journal", lastProtoException);
Chris Wren1ada10d2013-09-13 18:01:38 -04001062 }
1063 }
Chris Wren92aa4232013-10-04 11:29:36 -04001064 } catch (IOException e) {
Chris Wren50c8f422014-01-15 16:10:39 -05001065 Log.w(TAG, "failed to close the journal", e);
Chris Wren92aa4232013-10-04 11:29:36 -04001066 } finally {
Chris Wren1ada10d2013-09-13 18:01:38 -04001067 try {
1068 inStream.close();
1069 } catch (IOException e) {
Chris Wren50c8f422014-01-15 16:10:39 -05001070 Log.w(TAG, "failed to close the journal", e);
Chris Wren1ada10d2013-09-13 18:01:38 -04001071 }
1072 }
1073 return journal;
1074 }
1075
Sunny Goyalef728d42014-10-22 11:28:28 -07001076 private void writeRowToBackup(Key key, MessageNano proto, BackupDataOutput data)
1077 throws IOException {
1078 writeRowToBackup(keyToBackupKey(key), proto, data);
1079 }
1080
1081 private void writeRowToBackup(String backupKey, MessageNano proto,
Chris Wren22e130d2013-09-23 18:25:57 -04001082 BackupDataOutput data) throws IOException {
Sunny Goyalef728d42014-10-22 11:28:28 -07001083 byte[] blob = writeCheckedBytes(proto);
Chris Wren22e130d2013-09-23 18:25:57 -04001084 data.writeEntityHeader(backupKey, blob.length);
1085 data.writeEntityData(blob, blob.length);
Sunny Goyalc0ee6752015-01-16 14:10:32 -08001086 mBackupDataWasUpdated = true;
Sunny Goyalef728d42014-10-22 11:28:28 -07001087 if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey);
Chris Wren22e130d2013-09-23 18:25:57 -04001088 }
1089
Chris Wren1ada10d2013-09-13 18:01:38 -04001090 /**
1091 * Write the new journal to the output file.
1092 *
1093 * In the event of any error, just pretend we didn't have a journal,
1094 * in that case, do a full backup.
1095
1096 * @param newState the write-only file descriptor pointing to the new journal
1097 * @param journal a Journal protocol buffer
1098 */
1099 private void writeJournal(ParcelFileDescriptor newState, Journal journal) {
1100 FileOutputStream outStream = null;
1101 try {
1102 outStream = new FileOutputStream(newState.getFileDescriptor());
Chris Wren65b6a602014-01-10 14:11:25 -05001103 final byte[] journalBytes = writeCheckedBytes(journal);
Chris Wren65b6a602014-01-10 14:11:25 -05001104 outStream.write(journalBytes);
Chris Wren1ada10d2013-09-13 18:01:38 -04001105 outStream.close();
Chris Wren50c8f422014-01-15 16:10:39 -05001106 if (VERBOSE) Log.v(TAG, "wrote " + journalBytes.length + " bytes of journal");
Chris Wren1ada10d2013-09-13 18:01:38 -04001107 } catch (IOException e) {
Chris Wren50c8f422014-01-15 16:10:39 -05001108 Log.w(TAG, "failed to write backup journal", e);
Chris Wren1ada10d2013-09-13 18:01:38 -04001109 }
1110 }
1111
1112 /** Wrap a proto in a CheckedMessage and compute the checksum. */
1113 private byte[] writeCheckedBytes(MessageNano proto) {
1114 CheckedMessage wrapper = new CheckedMessage();
1115 wrapper.payload = MessageNano.toByteArray(proto);
1116 CRC32 checksum = new CRC32();
1117 checksum.update(wrapper.payload);
1118 wrapper.checksum = checksum.getValue();
1119 return MessageNano.toByteArray(wrapper);
1120 }
1121
1122 /** Unwrap a proto message from a CheckedMessage, verifying the checksum. */
Sunny Goyalef728d42014-10-22 11:28:28 -07001123 private static byte[] readCheckedBytes(byte[] buffer, int dataSize)
Chris Wren1ada10d2013-09-13 18:01:38 -04001124 throws InvalidProtocolBufferNanoException {
1125 CheckedMessage wrapper = new CheckedMessage();
Sunny Goyalef728d42014-10-22 11:28:28 -07001126 MessageNano.mergeFrom(wrapper, buffer, 0, dataSize);
Chris Wren1ada10d2013-09-13 18:01:38 -04001127 CRC32 checksum = new CRC32();
1128 checksum.update(wrapper.payload);
1129 if (wrapper.checksum != checksum.getValue()) {
1130 throw new InvalidProtocolBufferNanoException("checksum does not match");
1131 }
1132 return wrapper.payload;
1133 }
1134
Sunny Goyalef728d42014-10-22 11:28:28 -07001135 /**
1136 * @return true if the launcher is in a state to support backup
1137 */
Chris Wren71144262014-02-27 15:49:39 -05001138 private boolean launcherIsReady() {
1139 ContentResolver cr = mContext.getContentResolver();
1140 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, null, null, null);
1141 if (cursor == null) {
1142 // launcher data has been wiped, do nothing
1143 return false;
1144 }
1145 cursor.close();
1146
Adam Cohen2e6da152015-05-06 11:42:25 -07001147 if (LauncherAppState.getInstanceNoCreate() == null) {
Chris Wren71144262014-02-27 15:49:39 -05001148 // launcher services are unavailable, try again later
Chris Wren71144262014-02-27 15:49:39 -05001149 return false;
1150 }
1151
1152 return true;
1153 }
1154
Sunny Goyalffe83f12014-08-14 17:39:34 -07001155 private String getUserSelectionArg() {
1156 return Favorites.PROFILE_ID + '=' + UserManagerCompat.getInstance(mContext)
1157 .getSerialNumberForUser(UserHandleCompat.myUserHandle());
1158 }
1159
Sunny Goyal316490e2015-06-02 09:38:28 -07001160 @Thunk class InvalidBackupException extends IOException {
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001161
1162 private static final long serialVersionUID = 8931456637211665082L;
1163
Sunny Goyal316490e2015-06-02 09:38:28 -07001164 @Thunk InvalidBackupException(Throwable cause) {
Chris Wren1ada10d2013-09-13 18:01:38 -04001165 super(cause);
1166 }
1167
Sunny Goyal316490e2015-06-02 09:38:28 -07001168 @Thunk InvalidBackupException(String reason) {
Chris Wren1ada10d2013-09-13 18:01:38 -04001169 super(reason);
1170 }
1171 }
Sunny Goyalbb3b02f2015-01-15 12:00:14 -08001172
1173 /**
1174 * A class to check if an activity can handle one of the intents from a list of
1175 * predefined intents.
1176 */
1177 private class ItemTypeMatcher {
1178
1179 private final ArrayList<Intent> mIntents;
1180
1181 ItemTypeMatcher(int xml_res) {
1182 mIntents = xml_res == 0 ? new ArrayList<Intent>() : parseIntents(xml_res);
1183 }
1184
1185 private ArrayList<Intent> parseIntents(int xml_res) {
1186 ArrayList<Intent> intents = new ArrayList<Intent>();
1187 XmlResourceParser parser = mContext.getResources().getXml(xml_res);
1188 try {
1189 DefaultLayoutParser.beginDocument(parser, DefaultLayoutParser.TAG_RESOLVE);
1190 final int depth = parser.getDepth();
1191 int type;
1192 while (((type = parser.next()) != XmlPullParser.END_TAG ||
1193 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1194 if (type != XmlPullParser.START_TAG) {
1195 continue;
1196 } else if (DefaultLayoutParser.TAG_FAVORITE.equals(parser.getName())) {
1197 final String uri = DefaultLayoutParser.getAttributeValue(
1198 parser, DefaultLayoutParser.ATTR_URI);
1199 intents.add(Intent.parseUri(uri, 0));
1200 }
1201 }
1202 } catch (URISyntaxException | XmlPullParserException | IOException e) {
1203 Log.e(TAG, "Unable to parse " + xml_res, e);
1204 } finally {
1205 parser.close();
1206 }
1207 return intents;
1208 }
1209
1210 public boolean matches(ActivityInfo activity, PackageManager pm) {
1211 for (Intent intent : mIntents) {
1212 intent.setPackage(activity.packageName);
1213 ResolveInfo info = pm.resolveActivity(intent, 0);
1214 if (info != null && (info.activityInfo.name.equals(activity.name)
1215 || info.activityInfo.name.equals(activity.targetActivity))) {
1216 return true;
1217 }
1218 }
1219 return false;
1220 }
1221 }
Chris Wren1ada10d2013-09-13 18:01:38 -04001222}