Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1 | /* |
| 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 16 | package com.android.launcher3; |
| 17 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 18 | import android.app.backup.BackupDataInputStream; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 19 | import android.app.backup.BackupDataOutput; |
Chris Wren | 4d89e2a | 2013-10-09 17:03:50 -0400 | [diff] [blame] | 20 | import android.app.backup.BackupHelper; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 21 | import android.app.backup.BackupManager; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 22 | import android.content.ComponentName; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 24 | import android.content.ContentValues; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 25 | import android.content.Context; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 26 | import android.content.Intent; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 27 | import android.content.pm.ActivityInfo; |
| 28 | import android.content.pm.PackageManager; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager.NameNotFoundException; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 30 | import android.content.pm.ResolveInfo; |
| 31 | import android.content.res.XmlResourceParser; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 32 | import android.database.Cursor; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
| 34 | import android.graphics.BitmapFactory; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 35 | import android.graphics.drawable.Drawable; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 36 | import android.os.ParcelFileDescriptor; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 37 | import android.text.TextUtils; |
| 38 | import android.util.Base64; |
| 39 | import android.util.Log; |
| 40 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 41 | import com.android.launcher3.LauncherSettings.Favorites; |
| 42 | import com.android.launcher3.LauncherSettings.WorkspaceScreens; |
| 43 | import com.android.launcher3.backup.BackupProtos; |
| 44 | import com.android.launcher3.backup.BackupProtos.CheckedMessage; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 45 | import com.android.launcher3.backup.BackupProtos.DeviceProfieData; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 46 | import com.android.launcher3.backup.BackupProtos.Favorite; |
| 47 | import com.android.launcher3.backup.BackupProtos.Journal; |
| 48 | import com.android.launcher3.backup.BackupProtos.Key; |
| 49 | import com.android.launcher3.backup.BackupProtos.Resource; |
| 50 | import com.android.launcher3.backup.BackupProtos.Screen; |
| 51 | import com.android.launcher3.backup.BackupProtos.Widget; |
| 52 | import com.android.launcher3.compat.UserHandleCompat; |
| 53 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame^] | 54 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 55 | import com.google.protobuf.nano.InvalidProtocolBufferNanoException; |
| 56 | import com.google.protobuf.nano.MessageNano; |
| 57 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 58 | import org.xmlpull.v1.XmlPullParser; |
| 59 | import org.xmlpull.v1.XmlPullParserException; |
| 60 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 61 | import java.io.FileInputStream; |
| 62 | import java.io.FileOutputStream; |
| 63 | import java.io.IOException; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 64 | import java.net.URISyntaxException; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 65 | import java.util.ArrayList; |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 66 | import java.util.Arrays; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 67 | import java.util.HashSet; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 68 | import java.util.zip.CRC32; |
| 69 | |
| 70 | /** |
| 71 | * Persist the launcher home state across calamities. |
| 72 | */ |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 73 | public class LauncherBackupHelper implements BackupHelper { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 74 | private static final String TAG = "LauncherBackupHelper"; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 75 | private static final boolean VERBOSE = LauncherBackupAgentHelper.VERBOSE; |
| 76 | private static final boolean DEBUG = LauncherBackupAgentHelper.DEBUG; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 77 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 78 | private static final int BACKUP_VERSION = 2; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 79 | private static final int MAX_JOURNAL_SIZE = 1000000; |
| 80 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 81 | // 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 Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 85 | /** icons are large, dribble them out */ |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 86 | private static final int MAX_ICONS_PER_PASS = 10; |
| 87 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 88 | /** widgets contain previews, which are very large, dribble them out */ |
| 89 | private static final int MAX_WIDGETS_PER_PASS = 5; |
| 90 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 91 | private static final String[] FAVORITE_PROJECTION = { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 92 | 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | private static final int ID_INDEX = 0; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 113 | 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 129 | |
| 130 | private static final String[] SCREEN_PROJECTION = { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 131 | WorkspaceScreens._ID, // 0 |
| 132 | WorkspaceScreens.MODIFIED, // 1 |
| 133 | WorkspaceScreens.SCREEN_RANK // 2 |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 134 | }; |
| 135 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 136 | private static final int SCREEN_RANK_INDEX = 2; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 137 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame^] | 138 | @Thunk final Context mContext; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 139 | private final HashSet<String> mExistingKeys; |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 140 | private final ArrayList<Key> mKeys; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 141 | private final ItemTypeMatcher[] mItemTypeMatchers; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 142 | private final long mUserSerial; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 143 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 144 | private BackupManager mBackupManager; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 145 | private byte[] mBuffer = new byte[512]; |
| 146 | private long mLastBackupRestoreTime; |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 147 | private boolean mBackupDataWasUpdated; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 148 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 149 | private LauncherAppState mLauncherAppState; |
| 150 | private IconCache mIconCache; |
| 151 | private DeviceProfieData mDeviceProfileData; |
| 152 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 153 | boolean restoreSuccessful; |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 154 | int restoredBackupVersion = 1; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 155 | |
| 156 | public LauncherBackupHelper(Context context) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 157 | mContext = context; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 158 | mExistingKeys = new HashSet<String>(); |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 159 | mKeys = new ArrayList<Key>(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 160 | restoreSuccessful = true; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 161 | mItemTypeMatchers = new ItemTypeMatcher[CommonAppTypeParser.SUPPORTED_TYPE_COUNT]; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 162 | |
| 163 | UserManagerCompat userManager = UserManagerCompat.getInstance(mContext); |
| 164 | mUserSerial = userManager.getSerialNumberForUser(UserHandleCompat.myUserHandle()); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | private void dataChanged() { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 168 | if (mBackupManager == null) { |
| 169 | mBackupManager = new BackupManager(mContext); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 170 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 171 | 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 182 | } |
| 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 194 | */ |
| 195 | @Override |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 196 | public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 197 | ParcelFileDescriptor newState) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 198 | if (VERBOSE) Log.v(TAG, "onBackup"); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 199 | |
| 200 | Journal in = readJournal(oldState); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 201 | if (!launcherIsReady()) { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 202 | dataChanged(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 203 | // Perform backup later. |
| 204 | writeJournal(newState, in); |
| 205 | return; |
| 206 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 207 | |
| 208 | lazyInitAppState(true /* noCreate */); |
| 209 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 210 | Log.v(TAG, "lastBackupTime = " + in.t); |
| 211 | mKeys.clear(); |
| 212 | applyJournal(in); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 213 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 214 | // 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 Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 217 | mBackupDataWasUpdated = false; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 218 | try { |
| 219 | backupFavorites(data); |
| 220 | backupScreens(data); |
| 221 | backupIcons(data); |
| 222 | backupWidgets(data); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 223 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 224 | // 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 Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 228 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 229 | 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 Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 235 | mBackupDataWasUpdated = true; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | mExistingKeys.clear(); |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 239 | 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 Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 247 | |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 248 | 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 Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 259 | } catch (IOException e) { |
| 260 | Log.e(TAG, "launcher backup has failed", e); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 261 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 262 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 263 | writeNewStateDescription(newState); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 267 | * @return true if the backup corresponding to oldstate can be successfully applied |
| 268 | * to this device. |
| 269 | */ |
| 270 | private boolean isBackupCompatible(Journal oldState) { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 271 | 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 Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 294 | * Restore launcher configuration from the restored data stream. |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 295 | * 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 297 | * |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 298 | * @param data the key/value pair from the server |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 299 | */ |
| 300 | @Override |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 301 | public void restoreEntity(BackupDataInputStream data) { |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 302 | if (!restoreSuccessful) { |
| 303 | return; |
| 304 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 305 | lazyInitAppState(false /* noCreate */); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 306 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 307 | int dataSize = data.size(); |
| 308 | if (mBuffer.length < dataSize) { |
| 309 | mBuffer = new byte[dataSize]; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 310 | } |
| 311 | try { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 312 | int bytesRead = data.read(mBuffer, 0, dataSize); |
| 313 | if (DEBUG) Log.d(TAG, "read " + bytesRead + " of " + dataSize + " available"); |
| 314 | String backupKey = data.getKey(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 315 | |
| 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 Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 329 | restoredBackupVersion = journal.backupVersion; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 330 | 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 Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 337 | Key key = backupKeyToKey(backupKey); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 338 | mKeys.add(key); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 339 | switch (key.type) { |
| 340 | case Key.FAVORITE: |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 341 | restoreFavorite(key, mBuffer, dataSize); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 342 | break; |
| 343 | |
| 344 | case Key.SCREEN: |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 345 | restoreScreen(key, mBuffer, dataSize); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 346 | break; |
| 347 | |
| 348 | case Key.ICON: |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 349 | restoreIcon(key, mBuffer, dataSize); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 350 | break; |
| 351 | |
| 352 | case Key.WIDGET: |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 353 | restoreWidget(key, mBuffer, dataSize); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 354 | break; |
| 355 | |
| 356 | default: |
| 357 | Log.w(TAG, "unknown restore entity type: " + key.type); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 358 | mKeys.remove(key); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 359 | break; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 360 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 361 | } catch (IOException e) { |
| 362 | Log.w(TAG, "ignoring unparsable backup entry", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 363 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 364 | } |
| 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 Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 373 | 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 Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 381 | journal.backupVersion = BACKUP_VERSION; |
| 382 | journal.profile = getDeviceProfieData(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 383 | 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /** |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 396 | * @return the current device profile information. |
| 397 | */ |
| 398 | private DeviceProfieData getDeviceProfieData() { |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 399 | return mDeviceProfileData; |
| 400 | } |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 401 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 402 | 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 Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /** |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 430 | * Write all modified favorites to the data stream. |
| 431 | * |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 432 | * @param data output stream for key/value pairs |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 433 | * @throws IOException |
| 434 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 435 | private void backupFavorites(BackupDataOutput data) throws IOException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 436 | // persist things that have changed since the last backup |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 437 | ContentResolver cr = mContext.getContentResolver(); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 438 | // Don't backup apps in other profiles for now. |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 439 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 440 | getUserSelectionArg(), null, null); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 441 | try { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 442 | cursor.moveToPosition(-1); |
| 443 | while(cursor.moveToNext()) { |
| 444 | final long id = cursor.getLong(ID_INDEX); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 445 | final long updateTime = cursor.getLong(ID_MODIFIED); |
| 446 | Key key = getKey(Key.FAVORITE, id); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 447 | mKeys.add(key); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 448 | final String backupKey = keyToBackupKey(key); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 449 | if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) { |
| 450 | writeRowToBackup(key, packFavorite(cursor), data); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 451 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 452 | if (DEBUG) Log.d(TAG, "favorite already backup up: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 453 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 454 | } |
| 455 | } finally { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 456 | cursor.close(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 457 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 458 | } |
| 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 468 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 469 | private void restoreFavorite(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 470 | if (VERBOSE) Log.v(TAG, "unpacking favorite " + key.id); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 471 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 472 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
| 473 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 474 | ContentResolver cr = mContext.getContentResolver(); |
| 475 | ContentValues values = unpackFavorite(buffer, dataSize); |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 476 | cr.insert(Favorites.CONTENT_URI, values); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Write all modified screens to the data stream. |
| 481 | * |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 482 | * @param data output stream for key/value pairs |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 483 | * @throws IOException |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 484 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 485 | private void backupScreens(BackupDataOutput data) throws IOException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 486 | // persist things that have changed since the last backup |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 487 | ContentResolver cr = mContext.getContentResolver(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 488 | Cursor cursor = cr.query(WorkspaceScreens.CONTENT_URI, SCREEN_PROJECTION, |
| 489 | null, null, null); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 490 | try { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 491 | cursor.moveToPosition(-1); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 492 | if (DEBUG) Log.d(TAG, "dumping screens after: " + mLastBackupRestoreTime); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 493 | while(cursor.moveToNext()) { |
| 494 | final long id = cursor.getLong(ID_INDEX); |
| 495 | final long updateTime = cursor.getLong(ID_MODIFIED); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 496 | Key key = getKey(Key.SCREEN, id); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 497 | mKeys.add(key); |
Chris Wren | 5743aa9 | 2014-01-10 18:02:06 -0500 | [diff] [blame] | 498 | final String backupKey = keyToBackupKey(key); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 499 | if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) { |
| 500 | writeRowToBackup(key, packScreen(cursor), data); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 501 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 502 | if (VERBOSE) Log.v(TAG, "screen already backup up " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 503 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 504 | } |
| 505 | } finally { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 506 | cursor.close(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 507 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 508 | } |
| 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 518 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 519 | private void restoreScreen(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 520 | if (VERBOSE) Log.v(TAG, "unpacking screen " + key.id); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 521 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 522 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 523 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 524 | ContentResolver cr = mContext.getContentResolver(); |
| 525 | ContentValues values = unpackScreen(buffer, dataSize); |
| 526 | cr.insert(WorkspaceScreens.CONTENT_URI, values); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 529 | /** |
| 530 | * Write all the static icon resources we need to render placeholders |
| 531 | * for a package that is not installed. |
| 532 | * |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 533 | * @param data output stream for key/value pairs |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 534 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 535 | private void backupIcons(BackupDataOutput data) throws IOException { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 536 | // persist icons that haven't been persisted yet |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 537 | final ContentResolver cr = mContext.getContentResolver(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 538 | final int dpi = mContext.getResources().getDisplayMetrics().densityDpi; |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 539 | final UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 540 | int backupUpIconCount = 0; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 541 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 542 | // Don't backup apps in other profiles for now. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 543 | String where = "(" + Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPLICATION + " OR " + |
Kenny Guy | 43ea7ac | 2014-05-09 16:44:18 +0100 | [diff] [blame] | 544 | Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_SHORTCUT + ") AND " + |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 545 | getUserSelectionArg(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 546 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
| 547 | where, null, null); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 548 | 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 Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 561 | } else { |
| 562 | Log.w(TAG, "empty intent on application favorite: " + id); |
| 563 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 564 | if (mExistingKeys.contains(backupKey)) { |
| 565 | if (DEBUG) Log.d(TAG, "already saved icon " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 566 | |
| 567 | // remember that we already backed this up previously |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 568 | mKeys.add(key); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 569 | } else if (backupKey != null) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 570 | 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 Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 573 | Bitmap icon = mIconCache.getIcon(intent, myUserHandle); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 574 | if (icon != null && !mIconCache.isDefaultIcon(icon, myUserHandle)) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 575 | writeRowToBackup(key, packIcon(dpi, icon), data); |
| 576 | mKeys.add(key); |
| 577 | backupUpIconCount ++; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 578 | } |
| 579 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 580 | if (VERBOSE) Log.v(TAG, "deferring icon backup " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 581 | // too many icons for this pass, request another. |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 582 | dataChanged(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } catch (URISyntaxException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 586 | Log.e(TAG, "invalid URI on application favorite: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 587 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 588 | Log.e(TAG, "unable to save application icon for favorite: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | } |
| 592 | } finally { |
| 593 | cursor.close(); |
| 594 | } |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Read an icon from the stream. |
| 599 | * |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 600 | * <P>Keys arrive in any order, so shortcuts that use this icon may already exist. |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 601 | * |
| 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 Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 605 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 606 | private void restoreIcon(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 607 | if (VERBOSE) Log.v(TAG, "unpacking icon " + key.id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 608 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 609 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 610 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 611 | Resource res = unpackProto(new Resource(), buffer, dataSize); |
| 612 | if (DEBUG) { |
| 613 | Log.d(TAG, "unpacked " + res.dpi + " dpi icon"); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 614 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 615 | 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 Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 620 | mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi, |
| 621 | "" /* label */, mUserSerial); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 624 | /** |
| 625 | * Write all the static widget resources we need to render placeholders |
| 626 | * for a package that is not installed. |
| 627 | * |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 628 | * @param data output stream for key/value pairs |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 629 | * @throws IOException |
| 630 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 631 | private void backupWidgets(BackupDataOutput data) throws IOException { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 632 | // persist static widget info that hasn't been persisted yet |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 633 | final ContentResolver cr = mContext.getContentResolver(); |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 634 | final WidgetPreviewLoader previewLoader = mLauncherAppState.getWidgetCache(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 635 | final int dpi = mContext.getResources().getDisplayMetrics().densityDpi; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 636 | int backupWidgetCount = 0; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 637 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 638 | String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET + " AND " |
| 639 | + getUserSelectionArg(); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 640 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
| 641 | where, null, null); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 642 | 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 Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 648 | 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 Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 654 | } else { |
| 655 | Log.w(TAG, "empty intent on appwidget: " + id); |
| 656 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 657 | if (mExistingKeys.contains(backupKey)) { |
| 658 | if (DEBUG) Log.d(TAG, "already saved widget " + backupKey); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 659 | |
| 660 | // remember that we already backed this up previously |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 661 | mKeys.add(key); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 662 | } else if (backupKey != null) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 663 | 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 Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 666 | UserHandleCompat user = UserHandleCompat.myUserHandle(); |
| 667 | writeRowToBackup(key, |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 668 | packWidget(dpi, previewLoader, mIconCache, provider, user), |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 669 | data); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 670 | mKeys.add(key); |
| 671 | backupWidgetCount ++; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 672 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 673 | if (VERBOSE) Log.v(TAG, "deferring widget backup " + backupKey); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 674 | // too many widgets for this pass, request another. |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 675 | dataChanged(); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
| 679 | } finally { |
| 680 | cursor.close(); |
| 681 | } |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 682 | } |
| 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 Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 692 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 693 | private void restoreWidget(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 694 | if (VERBOSE) Log.v(TAG, "unpacking widget " + key.id); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 695 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 696 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 697 | 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 Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 704 | } else { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 705 | mIconCache.preloadIcon(ComponentName.unflattenFromString(widget.provider), |
| 706 | icon, widget.icon.dpi, widget.label, mUserSerial); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 707 | } |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 708 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 709 | |
| 710 | // future site of widget table mutation |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 711 | } |
| 712 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 713 | /** create a new key, with an integer ID. |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 714 | * |
| 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 Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 726 | /** 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 739 | /** keys need to be strings, serialize and encode. */ |
| 740 | private String keyToBackupKey(Key key) { |
Chris Wren | 978194c | 2013-10-03 17:47:22 -0400 | [diff] [blame] | 741 | return Base64.encodeToString(Key.toByteArray(key), Base64.NO_WRAP); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /** keys need to be strings, decode and parse. */ |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 745 | private Key backupKeyToKey(String backupKey) throws InvalidBackupException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 746 | try { |
| 747 | Key key = Key.parseFrom(Base64.decode(backupKey, Base64.DEFAULT)); |
| 748 | if (key.checksum != checkKey(key)) { |
| 749 | key = null; |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 750 | throw new InvalidBackupException("invalid key read from stream" + backupKey); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 751 | } |
| 752 | return key; |
| 753 | } catch (InvalidProtocolBufferNanoException e) { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 754 | throw new InvalidBackupException(e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 755 | } catch (IllegalArgumentException e) { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 756 | throw new InvalidBackupException(e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 757 | } |
| 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 Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 772 | /** |
| 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 783 | /** Serialize a Favorite for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 784 | private Favorite packFavorite(Cursor c) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 785 | 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 Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 814 | String intentDescription = c.getString(INTENT_INDEX); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 815 | Intent intent = null; |
Kenny Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 816 | if (!TextUtils.isEmpty(intentDescription)) { |
| 817 | try { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 818 | intent = Intent.parseUri(intentDescription, 0); |
Kenny Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 819 | intent.removeExtra(ItemInfo.EXTRA_PROFILE); |
| 820 | favorite.intent = intent.toUri(0); |
| 821 | } catch (URISyntaxException e) { |
| 822 | Log.e(TAG, "Invalid intent", e); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 823 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 824 | } |
| 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 Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 834 | 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 Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 859 | return favorite; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /** Deserialize a Favorite from persistence, after verifying checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 863 | private ContentValues unpackFavorite(byte[] buffer, int dataSize) |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 864 | throws IOException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 865 | Favorite favorite = unpackProto(new Favorite(), buffer, dataSize); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 866 | |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 867 | 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 Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 892 | |
Kenny Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 893 | UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); |
| 894 | long userSerialNumber = |
| 895 | UserManagerCompat.getInstance(mContext).getSerialNumberForUser(myUserHandle); |
| 896 | values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber); |
| 897 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 898 | DeviceProfieData currentProfile = getDeviceProfieData(); |
| 899 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 900 | 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 Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 909 | |
| 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 Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 916 | } else { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 917 | // 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 Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 928 | |
| 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 Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 943 | } |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 944 | |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 945 | return values; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /** Serialize a Screen for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 949 | private Screen packScreen(Cursor c) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 950 | Screen screen = new Screen(); |
| 951 | screen.id = c.getLong(ID_INDEX); |
| 952 | screen.rank = c.getInt(SCREEN_RANK_INDEX); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 953 | return screen; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /** Deserialize a Screen from persistence, after verifying checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 957 | private ContentValues unpackScreen(byte[] buffer, int dataSize) |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 958 | throws InvalidProtocolBufferNanoException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 959 | Screen screen = unpackProto(new Screen(), buffer, dataSize); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 960 | ContentValues values = new ContentValues(); |
| 961 | values.put(WorkspaceScreens._ID, screen.id); |
| 962 | values.put(WorkspaceScreens.SCREEN_RANK, screen.rank); |
| 963 | return values; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 964 | } |
| 965 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 966 | /** Serialize an icon Resource for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 967 | private Resource packIcon(int dpi, Bitmap icon) { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 968 | Resource res = new Resource(); |
| 969 | res.dpi = dpi; |
Sunny Goyal | 73a22a5 | 2015-04-28 16:51:09 -0700 | [diff] [blame] | 970 | res.data = Utilities.flattenBitmap(icon); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 971 | return res; |
| 972 | } |
| 973 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 974 | /** Serialize a widget for persistence, including a checksum wrapper. */ |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 975 | private Widget packWidget(int dpi, WidgetPreviewLoader previewLoader, IconCache iconCache, |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 976 | ComponentName provider, UserHandleCompat user) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 977 | final LauncherAppWidgetProviderInfo info = |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 978 | LauncherModel.getProviderInfo(mContext, provider, user); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 979 | 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 Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 986 | Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext); |
Sunny Goyal | 73a22a5 | 2015-04-28 16:51:09 -0700 | [diff] [blame] | 987 | widget.icon.data = Utilities.flattenBitmap(icon); |
| 988 | widget.icon.dpi = dpi; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 989 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 990 | return widget; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 991 | } |
| 992 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 993 | /** |
| 994 | * Deserialize a proto after verifying checksum wrapper. |
| 995 | */ |
| 996 | private <T extends MessageNano> T unpackProto(T proto, byte[] buffer, int dataSize) |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 997 | throws InvalidProtocolBufferNanoException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 998 | MessageNano.mergeFrom(proto, readCheckedBytes(buffer, dataSize)); |
| 999 | if (DEBUG) Log.d(TAG, "unpacked proto " + proto); |
| 1000 | return proto; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1003 | /** |
| 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 Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1010 | * @return a Journal protocol buffer |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1011 | */ |
| 1012 | private Journal readJournal(ParcelFileDescriptor oldState) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1013 | Journal journal = new Journal(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1014 | if (oldState == null) { |
| 1015 | return journal; |
| 1016 | } |
| 1017 | FileInputStream inStream = new FileInputStream(oldState.getFileDescriptor()); |
| 1018 | try { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1019 | 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1023 | int bytesRead = 0; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1024 | boolean valid = false; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1025 | InvalidProtocolBufferNanoException lastProtoException = null; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1026 | while (availableBytes > 0) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1027 | try { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1028 | // 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 Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1033 | if (result > 0) { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1034 | availableBytes -= result; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1035 | bytesRead += result; |
| 1036 | } else { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1037 | Log.w(TAG, "unexpected end of file while reading journal."); |
| 1038 | // stop reading and see what there is to parse |
| 1039 | availableBytes = 0; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1040 | } |
| 1041 | } catch (IOException e) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1042 | buffer = null; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1043 | availableBytes = 0; |
| 1044 | } |
| 1045 | |
| 1046 | // check the buffer to see if we have a valid journal |
| 1047 | try { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1048 | MessageNano.mergeFrom(journal, readCheckedBytes(buffer, bytesRead)); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1049 | // if we are here, then we have read a valid, checksum-verified journal |
| 1050 | valid = true; |
| 1051 | availableBytes = 0; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1052 | if (VERBOSE) Log.v(TAG, "read " + bytesRead + " bytes of journal"); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1053 | } catch (InvalidProtocolBufferNanoException e) { |
| 1054 | // if we don't have the whole journal yet, mergeFrom will throw. keep going. |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1055 | lastProtoException = e; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1056 | journal.clear(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1057 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1058 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1059 | if (DEBUG) Log.d(TAG, "journal bytes read: " + bytesRead); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1060 | if (!valid) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1061 | Log.w(TAG, "could not find a valid journal", lastProtoException); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1062 | } |
| 1063 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1064 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1065 | Log.w(TAG, "failed to close the journal", e); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1066 | } finally { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1067 | try { |
| 1068 | inStream.close(); |
| 1069 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1070 | Log.w(TAG, "failed to close the journal", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | return journal; |
| 1074 | } |
| 1075 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1076 | 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 Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1082 | BackupDataOutput data) throws IOException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1083 | byte[] blob = writeCheckedBytes(proto); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1084 | data.writeEntityHeader(backupKey, blob.length); |
| 1085 | data.writeEntityData(blob, blob.length); |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 1086 | mBackupDataWasUpdated = true; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1087 | if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1088 | } |
| 1089 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1090 | /** |
| 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 Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1103 | final byte[] journalBytes = writeCheckedBytes(journal); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1104 | outStream.write(journalBytes); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1105 | outStream.close(); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1106 | if (VERBOSE) Log.v(TAG, "wrote " + journalBytes.length + " bytes of journal"); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1107 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1108 | Log.w(TAG, "failed to write backup journal", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1109 | } |
| 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 Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1123 | private static byte[] readCheckedBytes(byte[] buffer, int dataSize) |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1124 | throws InvalidProtocolBufferNanoException { |
| 1125 | CheckedMessage wrapper = new CheckedMessage(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1126 | MessageNano.mergeFrom(wrapper, buffer, 0, dataSize); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1127 | 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 Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1135 | /** |
| 1136 | * @return true if the launcher is in a state to support backup |
| 1137 | */ |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1138 | 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 Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1147 | if (LauncherAppState.getInstanceNoCreate() == null) { |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1148 | // launcher services are unavailable, try again later |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | return true; |
| 1153 | } |
| 1154 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 1155 | private String getUserSelectionArg() { |
| 1156 | return Favorites.PROFILE_ID + '=' + UserManagerCompat.getInstance(mContext) |
| 1157 | .getSerialNumberForUser(UserHandleCompat.myUserHandle()); |
| 1158 | } |
| 1159 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame^] | 1160 | @Thunk class InvalidBackupException extends IOException { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1161 | |
| 1162 | private static final long serialVersionUID = 8931456637211665082L; |
| 1163 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame^] | 1164 | @Thunk InvalidBackupException(Throwable cause) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1165 | super(cause); |
| 1166 | } |
| 1167 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame^] | 1168 | @Thunk InvalidBackupException(String reason) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1169 | super(reason); |
| 1170 | } |
| 1171 | } |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1172 | |
| 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 Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1222 | } |