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; |
| 54 | import com.google.protobuf.nano.InvalidProtocolBufferNanoException; |
| 55 | import com.google.protobuf.nano.MessageNano; |
| 56 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 57 | import org.xmlpull.v1.XmlPullParser; |
| 58 | import org.xmlpull.v1.XmlPullParserException; |
| 59 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 60 | import java.io.ByteArrayOutputStream; |
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 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 138 | private 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 IconCache mIconCache; |
| 145 | private BackupManager mBackupManager; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 146 | private byte[] mBuffer = new byte[512]; |
| 147 | private long mLastBackupRestoreTime; |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 148 | private boolean mBackupDataWasUpdated; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 149 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 150 | private DeviceProfieData mCurrentProfile; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 151 | boolean restoreSuccessful; |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 152 | int restoredBackupVersion = 1; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 153 | |
| 154 | public LauncherBackupHelper(Context context) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 155 | mContext = context; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 156 | mExistingKeys = new HashSet<String>(); |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 157 | mKeys = new ArrayList<Key>(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 158 | restoreSuccessful = true; |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 159 | mItemTypeMatchers = new ItemTypeMatcher[CommonAppTypeParser.SUPPORTED_TYPE_COUNT]; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 160 | |
| 161 | UserManagerCompat userManager = UserManagerCompat.getInstance(mContext); |
| 162 | mUserSerial = userManager.getSerialNumberForUser(UserHandleCompat.myUserHandle()); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | private void dataChanged() { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 166 | if (mBackupManager == null) { |
| 167 | mBackupManager = new BackupManager(mContext); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 168 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 169 | mBackupManager.dataChanged(); |
| 170 | } |
| 171 | |
| 172 | private void applyJournal(Journal journal) { |
| 173 | mLastBackupRestoreTime = journal.t; |
| 174 | mExistingKeys.clear(); |
| 175 | if (journal.key != null) { |
| 176 | for (Key key : journal.key) { |
| 177 | mExistingKeys.add(keyToBackupKey(key)); |
| 178 | } |
| 179 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Back up launcher data so we can restore the user's state on a new device. |
| 184 | * |
| 185 | * <P>The journal is a timestamp and a list of keys that were saved as of that time. |
| 186 | * |
| 187 | * <P>Keys may come back in any order, so each key/value is one complete row of the database. |
| 188 | * |
| 189 | * @param oldState notes from the last backup |
| 190 | * @param data incremental key/value pairs to persist off-device |
| 191 | * @param newState notes for the next backup |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 192 | */ |
| 193 | @Override |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 194 | public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, |
| 195 | ParcelFileDescriptor newState) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 196 | if (VERBOSE) Log.v(TAG, "onBackup"); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 197 | |
| 198 | Journal in = readJournal(oldState); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 199 | if (!launcherIsReady()) { |
| 200 | // Perform backup later. |
| 201 | writeJournal(newState, in); |
| 202 | return; |
| 203 | } |
| 204 | Log.v(TAG, "lastBackupTime = " + in.t); |
| 205 | mKeys.clear(); |
| 206 | applyJournal(in); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 207 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 208 | // Record the time before performing backup so that entries edited while the backup |
| 209 | // was going on, do not get missed in next backup. |
| 210 | long newBackupTime = System.currentTimeMillis(); |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 211 | mBackupDataWasUpdated = false; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 212 | try { |
| 213 | backupFavorites(data); |
| 214 | backupScreens(data); |
| 215 | backupIcons(data); |
| 216 | backupWidgets(data); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 217 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 218 | // Delete any key which still exist in the old backup, but is not valid anymore. |
| 219 | HashSet<String> validKeys = new HashSet<String>(); |
| 220 | for (Key key : mKeys) { |
| 221 | validKeys.add(keyToBackupKey(key)); |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 222 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 223 | mExistingKeys.removeAll(validKeys); |
| 224 | |
| 225 | // Delete anything left in the existing keys. |
| 226 | for (String deleted: mExistingKeys) { |
| 227 | if (VERBOSE) Log.v(TAG, "dropping deleted item " + deleted); |
| 228 | data.writeEntityHeader(deleted, -1); |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 229 | mBackupDataWasUpdated = true; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | mExistingKeys.clear(); |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 233 | if (!mBackupDataWasUpdated) { |
| 234 | // Check if any metadata has changed |
| 235 | mBackupDataWasUpdated = (in.profile == null) |
| 236 | || !Arrays.equals(DeviceProfieData.toByteArray(in.profile), |
| 237 | DeviceProfieData.toByteArray(getDeviceProfieData())) |
| 238 | || (in.backupVersion != BACKUP_VERSION) |
| 239 | || (in.appVersion != getAppVersion()); |
| 240 | } |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 241 | |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 242 | if (mBackupDataWasUpdated) { |
| 243 | mLastBackupRestoreTime = newBackupTime; |
| 244 | |
| 245 | // We store the journal at two places. |
| 246 | // 1) Storing it in newState allows us to do partial backups by comparing old state |
| 247 | // 2) Storing it in backup data allows us to validate keys during restore |
| 248 | Journal state = getCurrentStateJournal(); |
| 249 | writeRowToBackup(JOURNAL_KEY, state, data); |
| 250 | } else { |
| 251 | if (DEBUG) Log.d(TAG, "Nothing was written during backup"); |
| 252 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 253 | } catch (IOException e) { |
| 254 | Log.e(TAG, "launcher backup has failed", e); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 255 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 256 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 257 | writeNewStateDescription(newState); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 261 | * @return true if the backup corresponding to oldstate can be successfully applied |
| 262 | * to this device. |
| 263 | */ |
| 264 | private boolean isBackupCompatible(Journal oldState) { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 265 | DeviceProfieData currentProfile = getDeviceProfieData(); |
| 266 | |
| 267 | DeviceProfieData oldProfile = oldState.profile; |
| 268 | |
| 269 | if (oldProfile == null || oldProfile.desktopCols == 0) { |
| 270 | // Profile info is not valid, ignore the check. |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | boolean isHotsetCompatible = false; |
| 275 | if (currentProfile.allappsRank >= oldProfile.hotseatCount) { |
| 276 | isHotsetCompatible = true; |
| 277 | } |
| 278 | if ((currentProfile.hotseatCount >= oldProfile.hotseatCount) && |
| 279 | (currentProfile.allappsRank == oldProfile.allappsRank)) { |
| 280 | isHotsetCompatible = true; |
| 281 | } |
| 282 | |
| 283 | return isHotsetCompatible && (currentProfile.desktopCols >= oldProfile.desktopCols) |
| 284 | && (currentProfile.desktopRows >= oldProfile.desktopRows); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 288 | * Restore launcher configuration from the restored data stream. |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 289 | * It assumes that the keys will arrive in lexical order. So if the journal was present in the |
| 290 | * backup, it should arrive first. |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 291 | * |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 292 | * @param data the key/value pair from the server |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 293 | */ |
| 294 | @Override |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 295 | public void restoreEntity(BackupDataInputStream data) { |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 296 | if (!restoreSuccessful) { |
| 297 | return; |
| 298 | } |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 299 | if (!initializeIconCache()) { |
| 300 | // During restore we do not need an initialized instance of IconCache. We can create |
| 301 | // a temporary icon cache here, as the process will be rebooted after restore |
| 302 | // is complete. |
| 303 | mIconCache = new IconCache(mContext); |
| 304 | } |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 305 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 306 | int dataSize = data.size(); |
| 307 | if (mBuffer.length < dataSize) { |
| 308 | mBuffer = new byte[dataSize]; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 309 | } |
| 310 | try { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 311 | int bytesRead = data.read(mBuffer, 0, dataSize); |
| 312 | if (DEBUG) Log.d(TAG, "read " + bytesRead + " of " + dataSize + " available"); |
| 313 | String backupKey = data.getKey(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 314 | |
| 315 | if (JOURNAL_KEY.equals(backupKey)) { |
| 316 | if (VERBOSE) Log.v(TAG, "Journal entry restored"); |
| 317 | if (!mKeys.isEmpty()) { |
| 318 | // We received the journal key after a restore key. |
| 319 | Log.wtf(TAG, keyToBackupKey(mKeys.get(0)) + " received after " + JOURNAL_KEY); |
| 320 | restoreSuccessful = false; |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | Journal journal = new Journal(); |
| 325 | MessageNano.mergeFrom(journal, readCheckedBytes(mBuffer, dataSize)); |
| 326 | applyJournal(journal); |
| 327 | restoreSuccessful = isBackupCompatible(journal); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 328 | restoredBackupVersion = journal.backupVersion; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | if (!mExistingKeys.isEmpty() && !mExistingKeys.contains(backupKey)) { |
| 333 | if (DEBUG) Log.e(TAG, "Ignoring key not present in the backup state " + backupKey); |
| 334 | return; |
| 335 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 336 | Key key = backupKeyToKey(backupKey); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 337 | mKeys.add(key); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 338 | switch (key.type) { |
| 339 | case Key.FAVORITE: |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 340 | restoreFavorite(key, mBuffer, dataSize); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 341 | break; |
| 342 | |
| 343 | case Key.SCREEN: |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 344 | restoreScreen(key, mBuffer, dataSize); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 345 | break; |
| 346 | |
| 347 | case Key.ICON: |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 348 | restoreIcon(key, mBuffer, dataSize); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | case Key.WIDGET: |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 352 | restoreWidget(key, mBuffer, dataSize); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 353 | break; |
| 354 | |
| 355 | default: |
| 356 | Log.w(TAG, "unknown restore entity type: " + key.type); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 357 | mKeys.remove(key); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 358 | break; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 359 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 360 | } catch (IOException e) { |
| 361 | Log.w(TAG, "ignoring unparsable backup entry", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 362 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Record the restore state for the next backup. |
| 367 | * |
| 368 | * @param newState notes about the backup state after restore. |
| 369 | */ |
| 370 | @Override |
| 371 | public void writeNewStateDescription(ParcelFileDescriptor newState) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 372 | writeJournal(newState, getCurrentStateJournal()); |
| 373 | } |
| 374 | |
| 375 | private Journal getCurrentStateJournal() { |
| 376 | Journal journal = new Journal(); |
| 377 | journal.t = mLastBackupRestoreTime; |
| 378 | journal.key = mKeys.toArray(new BackupProtos.Key[mKeys.size()]); |
| 379 | journal.appVersion = getAppVersion(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 380 | journal.backupVersion = BACKUP_VERSION; |
| 381 | journal.profile = getDeviceProfieData(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 382 | return journal; |
| 383 | } |
| 384 | |
| 385 | private int getAppVersion() { |
| 386 | try { |
| 387 | return mContext.getPackageManager() |
| 388 | .getPackageInfo(mContext.getPackageName(), 0).versionCode; |
| 389 | } catch (NameNotFoundException e) { |
| 390 | return 0; |
| 391 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 395 | * @return the current device profile information. |
| 396 | */ |
| 397 | private DeviceProfieData getDeviceProfieData() { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 398 | if (mCurrentProfile != null) { |
| 399 | return mCurrentProfile; |
| 400 | } |
Chris Wren | b02e611 | 2014-11-24 16:57:54 -0500 | [diff] [blame] | 401 | final Context applicationContext = mContext.getApplicationContext(); |
| 402 | DeviceProfile profile = LauncherAppState.createDynamicGrid(applicationContext, null) |
| 403 | .getDeviceProfile(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 404 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 405 | mCurrentProfile = new DeviceProfieData(); |
| 406 | mCurrentProfile.desktopRows = profile.numRows; |
| 407 | mCurrentProfile.desktopCols = profile.numColumns; |
| 408 | mCurrentProfile.hotseatCount = profile.numHotseatIcons; |
| 409 | mCurrentProfile.allappsRank = profile.hotseatAllAppsRank; |
| 410 | return mCurrentProfile; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 414 | * Write all modified favorites to the data stream. |
| 415 | * |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 416 | * @param data output stream for key/value pairs |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 417 | * @throws IOException |
| 418 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 419 | private void backupFavorites(BackupDataOutput data) throws IOException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 420 | // persist things that have changed since the last backup |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 421 | ContentResolver cr = mContext.getContentResolver(); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 422 | // Don't backup apps in other profiles for now. |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 423 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 424 | getUserSelectionArg(), null, null); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 425 | try { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 426 | cursor.moveToPosition(-1); |
| 427 | while(cursor.moveToNext()) { |
| 428 | final long id = cursor.getLong(ID_INDEX); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 429 | final long updateTime = cursor.getLong(ID_MODIFIED); |
| 430 | Key key = getKey(Key.FAVORITE, id); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 431 | mKeys.add(key); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 432 | final String backupKey = keyToBackupKey(key); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 433 | if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) { |
| 434 | writeRowToBackup(key, packFavorite(cursor), data); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 435 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 436 | if (DEBUG) Log.d(TAG, "favorite already backup up: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 437 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 438 | } |
| 439 | } finally { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 440 | cursor.close(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 441 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Read a favorite from the stream. |
| 446 | * |
| 447 | * <P>Keys arrive in any order, so screens and containers may not exist yet. |
| 448 | * |
| 449 | * @param key identifier for the row |
| 450 | * @param buffer the serialized proto from the stream, may be larger than dataSize |
| 451 | * @param dataSize the size of the proto from the stream |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 452 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 453 | private void restoreFavorite(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 454 | if (VERBOSE) Log.v(TAG, "unpacking favorite " + key.id); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 455 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 456 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
| 457 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 458 | ContentResolver cr = mContext.getContentResolver(); |
| 459 | ContentValues values = unpackFavorite(buffer, dataSize); |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 460 | cr.insert(Favorites.CONTENT_URI, values); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Write all modified screens to the data stream. |
| 465 | * |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 466 | * @param data output stream for key/value pairs |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 467 | * @throws IOException |
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 backupScreens(BackupDataOutput data) throws IOException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 470 | // persist things that have changed since the last backup |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 471 | ContentResolver cr = mContext.getContentResolver(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 472 | Cursor cursor = cr.query(WorkspaceScreens.CONTENT_URI, SCREEN_PROJECTION, |
| 473 | null, null, null); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 474 | try { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 475 | cursor.moveToPosition(-1); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 476 | if (DEBUG) Log.d(TAG, "dumping screens after: " + mLastBackupRestoreTime); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 477 | while(cursor.moveToNext()) { |
| 478 | final long id = cursor.getLong(ID_INDEX); |
| 479 | final long updateTime = cursor.getLong(ID_MODIFIED); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 480 | Key key = getKey(Key.SCREEN, id); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 481 | mKeys.add(key); |
Chris Wren | 5743aa9 | 2014-01-10 18:02:06 -0500 | [diff] [blame] | 482 | final String backupKey = keyToBackupKey(key); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 483 | if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) { |
| 484 | writeRowToBackup(key, packScreen(cursor), data); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 485 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 486 | if (VERBOSE) Log.v(TAG, "screen already backup up " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 487 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 488 | } |
| 489 | } finally { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 490 | cursor.close(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 491 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Read a screen from the stream. |
| 496 | * |
| 497 | * <P>Keys arrive in any order, so children of this screen may already exist. |
| 498 | * |
| 499 | * @param key identifier for the row |
| 500 | * @param buffer the serialized proto from the stream, may be larger than dataSize |
| 501 | * @param dataSize the size of the proto from the stream |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 502 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 503 | private void restoreScreen(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 504 | if (VERBOSE) Log.v(TAG, "unpacking screen " + key.id); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 505 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 506 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 507 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 508 | ContentResolver cr = mContext.getContentResolver(); |
| 509 | ContentValues values = unpackScreen(buffer, dataSize); |
| 510 | cr.insert(WorkspaceScreens.CONTENT_URI, values); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 511 | } |
| 512 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 513 | /** |
| 514 | * Write all the static icon resources we need to render placeholders |
| 515 | * for a package that is not installed. |
| 516 | * |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 517 | * @param data output stream for key/value pairs |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 518 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 519 | private void backupIcons(BackupDataOutput data) throws IOException { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 520 | // persist icons that haven't been persisted yet |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 521 | if (!initializeIconCache()) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 522 | dataChanged(); // try again later |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 523 | if (DEBUG) Log.d(TAG, "Launcher is not initialized, delaying icon backup"); |
| 524 | return; |
| 525 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 526 | final ContentResolver cr = mContext.getContentResolver(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 527 | final int dpi = mContext.getResources().getDisplayMetrics().densityDpi; |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 528 | final UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 529 | int backupUpIconCount = 0; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 530 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 531 | // Don't backup apps in other profiles for now. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 532 | String where = "(" + Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPLICATION + " OR " + |
Kenny Guy | 43ea7ac | 2014-05-09 16:44:18 +0100 | [diff] [blame] | 533 | Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_SHORTCUT + ") AND " + |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 534 | getUserSelectionArg(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 535 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
| 536 | where, null, null); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 537 | try { |
| 538 | cursor.moveToPosition(-1); |
| 539 | while(cursor.moveToNext()) { |
| 540 | final long id = cursor.getLong(ID_INDEX); |
| 541 | final String intentDescription = cursor.getString(INTENT_INDEX); |
| 542 | try { |
| 543 | Intent intent = Intent.parseUri(intentDescription, 0); |
| 544 | ComponentName cn = intent.getComponent(); |
| 545 | Key key = null; |
| 546 | String backupKey = null; |
| 547 | if (cn != null) { |
| 548 | key = getKey(Key.ICON, cn.flattenToShortString()); |
| 549 | backupKey = keyToBackupKey(key); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 550 | } else { |
| 551 | Log.w(TAG, "empty intent on application favorite: " + id); |
| 552 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 553 | if (mExistingKeys.contains(backupKey)) { |
| 554 | if (DEBUG) Log.d(TAG, "already saved icon " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 555 | |
| 556 | // remember that we already backed this up previously |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 557 | mKeys.add(key); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 558 | } else if (backupKey != null) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 559 | if (DEBUG) Log.d(TAG, "I can count this high: " + backupUpIconCount); |
| 560 | if (backupUpIconCount < MAX_ICONS_PER_PASS) { |
| 561 | if (DEBUG) Log.d(TAG, "saving icon " + backupKey); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 562 | Bitmap icon = mIconCache.getIcon(intent, myUserHandle); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 563 | if (icon != null && !mIconCache.isDefaultIcon(icon, myUserHandle)) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 564 | writeRowToBackup(key, packIcon(dpi, icon), data); |
| 565 | mKeys.add(key); |
| 566 | backupUpIconCount ++; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 567 | } |
| 568 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 569 | if (VERBOSE) Log.v(TAG, "deferring icon backup " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 570 | // too many icons for this pass, request another. |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 571 | dataChanged(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | } catch (URISyntaxException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 575 | Log.e(TAG, "invalid URI on application favorite: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 576 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 577 | Log.e(TAG, "unable to save application icon for favorite: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | } |
| 581 | } finally { |
| 582 | cursor.close(); |
| 583 | } |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Read an icon from the stream. |
| 588 | * |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 589 | * <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] | 590 | * |
| 591 | * @param key identifier for the row |
| 592 | * @param buffer the serialized proto from the stream, may be larger than dataSize |
| 593 | * @param dataSize the size of the proto from the stream |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 594 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 595 | private void restoreIcon(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 596 | if (VERBOSE) Log.v(TAG, "unpacking icon " + key.id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 597 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 598 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 599 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 600 | Resource res = unpackProto(new Resource(), buffer, dataSize); |
| 601 | if (DEBUG) { |
| 602 | Log.d(TAG, "unpacked " + res.dpi + " dpi icon"); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 603 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 604 | Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length); |
| 605 | if (icon == null) { |
| 606 | Log.w(TAG, "failed to unpack icon for " + key.name); |
| 607 | } |
| 608 | if (VERBOSE) Log.v(TAG, "saving restored icon as: " + key.name); |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 609 | mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi, |
| 610 | "" /* label */, mUserSerial); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 611 | } |
| 612 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 613 | /** |
| 614 | * Write all the static widget resources we need to render placeholders |
| 615 | * for a package that is not installed. |
| 616 | * |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 617 | * @param data output stream for key/value pairs |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 618 | * @throws IOException |
| 619 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 620 | private void backupWidgets(BackupDataOutput data) throws IOException { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 621 | // persist static widget info that hasn't been persisted yet |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 622 | final LauncherAppState appState = LauncherAppState.getInstanceNoCreate(); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 623 | if (appState == null || !initializeIconCache()) { |
| 624 | Log.w(TAG, "Failed to get icon cache during restore"); |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 625 | return; |
| 626 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 627 | final ContentResolver cr = mContext.getContentResolver(); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 628 | final WidgetPreviewLoader previewLoader = appState.getWidgetCache(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 629 | final int dpi = mContext.getResources().getDisplayMetrics().densityDpi; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 630 | final DeviceProfile profile = appState.getDynamicGrid().getDeviceProfile(); |
| 631 | if (DEBUG) Log.d(TAG, "cellWidthPx: " + profile.cellWidthPx); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 632 | int backupWidgetCount = 0; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 633 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 634 | String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET + " AND " |
| 635 | + getUserSelectionArg(); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 636 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
| 637 | where, null, null); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 638 | try { |
| 639 | cursor.moveToPosition(-1); |
| 640 | while(cursor.moveToNext()) { |
| 641 | final long id = cursor.getLong(ID_INDEX); |
| 642 | final String providerName = cursor.getString(APPWIDGET_PROVIDER_INDEX); |
| 643 | final int spanX = cursor.getInt(SPANX_INDEX); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 644 | final ComponentName provider = ComponentName.unflattenFromString(providerName); |
| 645 | Key key = null; |
| 646 | String backupKey = null; |
| 647 | if (provider != null) { |
| 648 | key = getKey(Key.WIDGET, providerName); |
| 649 | backupKey = keyToBackupKey(key); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 650 | } else { |
| 651 | Log.w(TAG, "empty intent on appwidget: " + id); |
| 652 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 653 | if (mExistingKeys.contains(backupKey)) { |
| 654 | if (DEBUG) Log.d(TAG, "already saved widget " + backupKey); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 655 | |
| 656 | // remember that we already backed this up previously |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 657 | mKeys.add(key); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 658 | } else if (backupKey != null) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 659 | if (DEBUG) Log.d(TAG, "I can count this high: " + backupWidgetCount); |
| 660 | if (backupWidgetCount < MAX_WIDGETS_PER_PASS) { |
| 661 | if (DEBUG) Log.d(TAG, "saving widget " + backupKey); |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 662 | UserHandleCompat user = UserHandleCompat.myUserHandle(); |
| 663 | writeRowToBackup(key, |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 664 | packWidget(dpi, previewLoader,spanX * profile.cellWidthPx, |
| 665 | mIconCache, provider, user), |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 666 | data); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 667 | mKeys.add(key); |
| 668 | backupWidgetCount ++; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 669 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 670 | if (VERBOSE) Log.v(TAG, "deferring widget backup " + backupKey); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 671 | // too many widgets for this pass, request another. |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 672 | dataChanged(); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
| 676 | } finally { |
| 677 | cursor.close(); |
| 678 | } |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Read a widget from the stream. |
| 683 | * |
| 684 | * <P>Keys arrive in any order, so widgets that use this data may already exist. |
| 685 | * |
| 686 | * @param key identifier for the row |
| 687 | * @param buffer the serialized proto from the stream, may be larger than dataSize |
| 688 | * @param dataSize the size of the proto from the stream |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 689 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 690 | private void restoreWidget(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 691 | if (VERBOSE) Log.v(TAG, "unpacking widget " + key.id); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 692 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 693 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 694 | Widget widget = unpackProto(new Widget(), buffer, dataSize); |
| 695 | if (DEBUG) Log.d(TAG, "unpacked " + widget.provider); |
| 696 | if (widget.icon.data != null) { |
| 697 | Bitmap icon = BitmapFactory |
| 698 | .decodeByteArray(widget.icon.data, 0, widget.icon.data.length); |
| 699 | if (icon == null) { |
| 700 | Log.w(TAG, "failed to unpack widget icon for " + key.name); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 701 | } else { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 702 | mIconCache.preloadIcon(ComponentName.unflattenFromString(widget.provider), |
| 703 | icon, widget.icon.dpi, widget.label, mUserSerial); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 704 | } |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 705 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 706 | |
| 707 | // future site of widget table mutation |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 710 | /** create a new key, with an integer ID. |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 711 | * |
| 712 | * <P> Keys contain their own checksum instead of using |
| 713 | * the heavy-weight CheckedMessage wrapper. |
| 714 | */ |
| 715 | private Key getKey(int type, long id) { |
| 716 | Key key = new Key(); |
| 717 | key.type = type; |
| 718 | key.id = id; |
| 719 | key.checksum = checkKey(key); |
| 720 | return key; |
| 721 | } |
| 722 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 723 | /** create a new key for a named object. |
| 724 | * |
| 725 | * <P> Keys contain their own checksum instead of using |
| 726 | * the heavy-weight CheckedMessage wrapper. |
| 727 | */ |
| 728 | private Key getKey(int type, String name) { |
| 729 | Key key = new Key(); |
| 730 | key.type = type; |
| 731 | key.name = name; |
| 732 | key.checksum = checkKey(key); |
| 733 | return key; |
| 734 | } |
| 735 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 736 | /** keys need to be strings, serialize and encode. */ |
| 737 | private String keyToBackupKey(Key key) { |
Chris Wren | 978194c | 2013-10-03 17:47:22 -0400 | [diff] [blame] | 738 | return Base64.encodeToString(Key.toByteArray(key), Base64.NO_WRAP); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /** keys need to be strings, decode and parse. */ |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 742 | private Key backupKeyToKey(String backupKey) throws InvalidBackupException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 743 | try { |
| 744 | Key key = Key.parseFrom(Base64.decode(backupKey, Base64.DEFAULT)); |
| 745 | if (key.checksum != checkKey(key)) { |
| 746 | key = null; |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 747 | throw new InvalidBackupException("invalid key read from stream" + backupKey); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 748 | } |
| 749 | return key; |
| 750 | } catch (InvalidProtocolBufferNanoException e) { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 751 | throw new InvalidBackupException(e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 752 | } catch (IllegalArgumentException e) { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 753 | throw new InvalidBackupException(e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | /** Compute the checksum over the important bits of a key. */ |
| 758 | private long checkKey(Key key) { |
| 759 | CRC32 checksum = new CRC32(); |
| 760 | checksum.update(key.type); |
| 761 | checksum.update((int) (key.id & 0xffff)); |
| 762 | checksum.update((int) ((key.id >> 32) & 0xffff)); |
| 763 | if (!TextUtils.isEmpty(key.name)) { |
| 764 | checksum.update(key.name.getBytes()); |
| 765 | } |
| 766 | return checksum.getValue(); |
| 767 | } |
| 768 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 769 | /** |
| 770 | * @return true if its an hotseat item, that can be replaced during restore. |
| 771 | * TODO: Extend check for folders in hotseat. |
| 772 | */ |
| 773 | private boolean isReplaceableHotseatItem(Favorite favorite) { |
| 774 | return favorite.container == Favorites.CONTAINER_HOTSEAT |
| 775 | && favorite.intent != null |
| 776 | && (favorite.itemType == Favorites.ITEM_TYPE_APPLICATION |
| 777 | || favorite.itemType == Favorites.ITEM_TYPE_SHORTCUT); |
| 778 | } |
| 779 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 780 | /** Serialize a Favorite for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 781 | private Favorite packFavorite(Cursor c) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 782 | Favorite favorite = new Favorite(); |
| 783 | favorite.id = c.getLong(ID_INDEX); |
| 784 | favorite.screen = c.getInt(SCREEN_INDEX); |
| 785 | favorite.container = c.getInt(CONTAINER_INDEX); |
| 786 | favorite.cellX = c.getInt(CELLX_INDEX); |
| 787 | favorite.cellY = c.getInt(CELLY_INDEX); |
| 788 | favorite.spanX = c.getInt(SPANX_INDEX); |
| 789 | favorite.spanY = c.getInt(SPANY_INDEX); |
| 790 | favorite.iconType = c.getInt(ICON_TYPE_INDEX); |
| 791 | if (favorite.iconType == Favorites.ICON_TYPE_RESOURCE) { |
| 792 | String iconPackage = c.getString(ICON_PACKAGE_INDEX); |
| 793 | if (!TextUtils.isEmpty(iconPackage)) { |
| 794 | favorite.iconPackage = iconPackage; |
| 795 | } |
| 796 | String iconResource = c.getString(ICON_RESOURCE_INDEX); |
| 797 | if (!TextUtils.isEmpty(iconResource)) { |
| 798 | favorite.iconResource = iconResource; |
| 799 | } |
| 800 | } |
| 801 | if (favorite.iconType == Favorites.ICON_TYPE_BITMAP) { |
| 802 | byte[] blob = c.getBlob(ICON_INDEX); |
| 803 | if (blob != null && blob.length > 0) { |
| 804 | favorite.icon = blob; |
| 805 | } |
| 806 | } |
| 807 | String title = c.getString(TITLE_INDEX); |
| 808 | if (!TextUtils.isEmpty(title)) { |
| 809 | favorite.title = title; |
| 810 | } |
Kenny Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 811 | String intentDescription = c.getString(INTENT_INDEX); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 812 | Intent intent = null; |
Kenny Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 813 | if (!TextUtils.isEmpty(intentDescription)) { |
| 814 | try { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 815 | intent = Intent.parseUri(intentDescription, 0); |
Kenny Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 816 | intent.removeExtra(ItemInfo.EXTRA_PROFILE); |
| 817 | favorite.intent = intent.toUri(0); |
| 818 | } catch (URISyntaxException e) { |
| 819 | Log.e(TAG, "Invalid intent", e); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 820 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 821 | } |
| 822 | favorite.itemType = c.getInt(ITEM_TYPE_INDEX); |
| 823 | if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) { |
| 824 | favorite.appWidgetId = c.getInt(APPWIDGET_ID_INDEX); |
| 825 | String appWidgetProvider = c.getString(APPWIDGET_PROVIDER_INDEX); |
| 826 | if (!TextUtils.isEmpty(appWidgetProvider)) { |
| 827 | favorite.appWidgetProvider = appWidgetProvider; |
| 828 | } |
| 829 | } |
| 830 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 831 | if (isReplaceableHotseatItem(favorite)) { |
| 832 | if (intent != null && intent.getComponent() != null) { |
| 833 | PackageManager pm = mContext.getPackageManager(); |
| 834 | ActivityInfo activity = null;; |
| 835 | try { |
| 836 | activity = pm.getActivityInfo(intent.getComponent(), 0); |
| 837 | } catch (NameNotFoundException e) { |
| 838 | Log.e(TAG, "Target not found", e); |
| 839 | } |
| 840 | if (activity == null) { |
| 841 | return favorite; |
| 842 | } |
| 843 | for (int i = 0; i < mItemTypeMatchers.length; i++) { |
| 844 | if (mItemTypeMatchers[i] == null) { |
| 845 | mItemTypeMatchers[i] = new ItemTypeMatcher( |
| 846 | CommonAppTypeParser.getResourceForItemType(i)); |
| 847 | } |
| 848 | if (mItemTypeMatchers[i].matches(activity, pm)) { |
| 849 | favorite.targetType = i; |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 856 | return favorite; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /** Deserialize a Favorite from persistence, after verifying checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 860 | private ContentValues unpackFavorite(byte[] buffer, int dataSize) |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 861 | throws IOException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 862 | Favorite favorite = unpackProto(new Favorite(), buffer, dataSize); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 863 | |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 864 | ContentValues values = new ContentValues(); |
| 865 | values.put(Favorites._ID, favorite.id); |
| 866 | values.put(Favorites.SCREEN, favorite.screen); |
| 867 | values.put(Favorites.CONTAINER, favorite.container); |
| 868 | values.put(Favorites.CELLX, favorite.cellX); |
| 869 | values.put(Favorites.CELLY, favorite.cellY); |
| 870 | values.put(Favorites.SPANX, favorite.spanX); |
| 871 | values.put(Favorites.SPANY, favorite.spanY); |
| 872 | values.put(Favorites.ICON_TYPE, favorite.iconType); |
| 873 | if (favorite.iconType == Favorites.ICON_TYPE_RESOURCE) { |
| 874 | values.put(Favorites.ICON_PACKAGE, favorite.iconPackage); |
| 875 | values.put(Favorites.ICON_RESOURCE, favorite.iconResource); |
| 876 | } |
| 877 | if (favorite.iconType == Favorites.ICON_TYPE_BITMAP) { |
| 878 | values.put(Favorites.ICON, favorite.icon); |
| 879 | } |
| 880 | if (!TextUtils.isEmpty(favorite.title)) { |
| 881 | values.put(Favorites.TITLE, favorite.title); |
| 882 | } else { |
| 883 | values.put(Favorites.TITLE, ""); |
| 884 | } |
| 885 | if (!TextUtils.isEmpty(favorite.intent)) { |
| 886 | values.put(Favorites.INTENT, favorite.intent); |
| 887 | } |
| 888 | values.put(Favorites.ITEM_TYPE, favorite.itemType); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 889 | |
Kenny Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 890 | UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); |
| 891 | long userSerialNumber = |
| 892 | UserManagerCompat.getInstance(mContext).getSerialNumberForUser(myUserHandle); |
| 893 | values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber); |
| 894 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 895 | DeviceProfieData currentProfile = getDeviceProfieData(); |
| 896 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 897 | if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) { |
| 898 | if (!TextUtils.isEmpty(favorite.appWidgetProvider)) { |
| 899 | values.put(Favorites.APPWIDGET_PROVIDER, favorite.appWidgetProvider); |
| 900 | } |
| 901 | values.put(Favorites.APPWIDGET_ID, favorite.appWidgetId); |
| 902 | values.put(LauncherSettings.Favorites.RESTORED, |
| 903 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID | |
| 904 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY | |
| 905 | LauncherAppWidgetInfo.FLAG_UI_NOT_READY); |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 906 | |
| 907 | // Verify placement |
| 908 | if (((favorite.cellX + favorite.spanX) > currentProfile.desktopCols) |
| 909 | || ((favorite.cellY + favorite.spanY) > currentProfile.desktopRows)) { |
| 910 | restoreSuccessful = false; |
| 911 | throw new InvalidBackupException("Widget not in screen bounds, aborting restore"); |
| 912 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 913 | } else { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 914 | // Check if it is an hotseat item, that can be replaced. |
| 915 | if (isReplaceableHotseatItem(favorite) |
| 916 | && favorite.targetType != Favorite.TARGET_NONE |
| 917 | && favorite.targetType < CommonAppTypeParser.SUPPORTED_TYPE_COUNT) { |
| 918 | Log.e(TAG, "Added item type flag"); |
| 919 | values.put(LauncherSettings.Favorites.RESTORED, |
| 920 | 1 | CommonAppTypeParser.encodeItemTypeToFlag(favorite.targetType)); |
| 921 | } else { |
| 922 | // Let LauncherModel know we've been here. |
| 923 | values.put(LauncherSettings.Favorites.RESTORED, 1); |
| 924 | } |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 925 | |
| 926 | // Verify placement |
| 927 | if (favorite.container == Favorites.CONTAINER_HOTSEAT) { |
| 928 | if ((favorite.screen >= currentProfile.hotseatCount) |
| 929 | || (favorite.screen == currentProfile.allappsRank)) { |
| 930 | restoreSuccessful = false; |
| 931 | throw new InvalidBackupException("Item not in hotseat bounds, aborting restore"); |
| 932 | } |
| 933 | } else { |
| 934 | if ((favorite.cellX >= currentProfile.desktopCols) |
| 935 | || (favorite.cellY >= currentProfile.desktopRows)) { |
| 936 | restoreSuccessful = false; |
| 937 | throw new InvalidBackupException("Item not in desktop bounds, aborting restore"); |
| 938 | } |
| 939 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 940 | } |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 941 | |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 942 | return values; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /** Serialize a Screen for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 946 | private Screen packScreen(Cursor c) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 947 | Screen screen = new Screen(); |
| 948 | screen.id = c.getLong(ID_INDEX); |
| 949 | screen.rank = c.getInt(SCREEN_RANK_INDEX); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 950 | return screen; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /** Deserialize a Screen from persistence, after verifying checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 954 | private ContentValues unpackScreen(byte[] buffer, int dataSize) |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 955 | throws InvalidProtocolBufferNanoException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 956 | Screen screen = unpackProto(new Screen(), buffer, dataSize); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 957 | ContentValues values = new ContentValues(); |
| 958 | values.put(WorkspaceScreens._ID, screen.id); |
| 959 | values.put(WorkspaceScreens.SCREEN_RANK, screen.rank); |
| 960 | return values; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 961 | } |
| 962 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 963 | /** Serialize an icon Resource for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 964 | private Resource packIcon(int dpi, Bitmap icon) { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 965 | Resource res = new Resource(); |
| 966 | res.dpi = dpi; |
Sunny Goyal | 73a22a5 | 2015-04-28 16:51:09 -0700 | [diff] [blame] | 967 | res.data = Utilities.flattenBitmap(icon); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 968 | return res; |
| 969 | } |
| 970 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 971 | /** Serialize a widget for persistence, including a checksum wrapper. */ |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 972 | private Widget packWidget(int dpi, WidgetPreviewLoader previewLoader, |
| 973 | int previewWidth, IconCache iconCache, |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 974 | ComponentName provider, UserHandleCompat user) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 975 | final LauncherAppWidgetProviderInfo info = |
Robin Lee | 26ace12 | 2015-03-16 19:41:43 +0000 | [diff] [blame] | 976 | LauncherModel.getProviderInfo(mContext, provider, user); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 977 | Widget widget = new Widget(); |
| 978 | widget.provider = provider.flattenToShortString(); |
| 979 | widget.label = info.label; |
| 980 | widget.configure = info.configure != null; |
| 981 | if (info.icon != 0) { |
| 982 | widget.icon = new Resource(); |
| 983 | Drawable fullResIcon = iconCache.getFullResIcon(provider.getPackageName(), info.icon); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 984 | Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext); |
Sunny Goyal | 73a22a5 | 2015-04-28 16:51:09 -0700 | [diff] [blame] | 985 | widget.icon.data = Utilities.flattenBitmap(icon); |
| 986 | widget.icon.dpi = dpi; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 987 | } |
| 988 | if (info.previewImage != 0) { |
| 989 | widget.preview = new Resource(); |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 990 | Bitmap preview = previewLoader.generateWidgetPreview(info, previewWidth, null); |
Sunny Goyal | 73a22a5 | 2015-04-28 16:51:09 -0700 | [diff] [blame] | 991 | widget.preview.data = Utilities.flattenBitmap(preview); |
| 992 | widget.preview.dpi = dpi; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 993 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 994 | return widget; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 995 | } |
| 996 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 997 | /** |
| 998 | * Deserialize a proto after verifying checksum wrapper. |
| 999 | */ |
| 1000 | private <T extends MessageNano> T unpackProto(T proto, byte[] buffer, int dataSize) |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1001 | throws InvalidProtocolBufferNanoException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1002 | MessageNano.mergeFrom(proto, readCheckedBytes(buffer, dataSize)); |
| 1003 | if (DEBUG) Log.d(TAG, "unpacked proto " + proto); |
| 1004 | return proto; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1007 | /** |
| 1008 | * Read the old journal from the input file. |
| 1009 | * |
| 1010 | * In the event of any error, just pretend we didn't have a journal, |
| 1011 | * in that case, do a full backup. |
| 1012 | * |
| 1013 | * @param oldState the read-0only file descriptor pointing to the old journal |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1014 | * @return a Journal protocol buffer |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1015 | */ |
| 1016 | private Journal readJournal(ParcelFileDescriptor oldState) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1017 | Journal journal = new Journal(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1018 | if (oldState == null) { |
| 1019 | return journal; |
| 1020 | } |
| 1021 | FileInputStream inStream = new FileInputStream(oldState.getFileDescriptor()); |
| 1022 | try { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1023 | int availableBytes = inStream.available(); |
| 1024 | if (DEBUG) Log.d(TAG, "available " + availableBytes); |
| 1025 | if (availableBytes < MAX_JOURNAL_SIZE) { |
| 1026 | byte[] buffer = new byte[availableBytes]; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1027 | int bytesRead = 0; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1028 | boolean valid = false; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1029 | InvalidProtocolBufferNanoException lastProtoException = null; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1030 | while (availableBytes > 0) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1031 | try { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1032 | // OMG what are you doing? This is crazy inefficient! |
| 1033 | // If we read a byte that is not ours, we will cause trouble: b/12491813 |
| 1034 | // However, we don't know how many bytes to expect (oops). |
| 1035 | // So we have to step through *slowly*, watching for the end. |
| 1036 | int result = inStream.read(buffer, bytesRead, 1); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1037 | if (result > 0) { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1038 | availableBytes -= result; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1039 | bytesRead += result; |
| 1040 | } else { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1041 | Log.w(TAG, "unexpected end of file while reading journal."); |
| 1042 | // stop reading and see what there is to parse |
| 1043 | availableBytes = 0; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1044 | } |
| 1045 | } catch (IOException e) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1046 | buffer = null; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1047 | availableBytes = 0; |
| 1048 | } |
| 1049 | |
| 1050 | // check the buffer to see if we have a valid journal |
| 1051 | try { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1052 | MessageNano.mergeFrom(journal, readCheckedBytes(buffer, bytesRead)); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1053 | // if we are here, then we have read a valid, checksum-verified journal |
| 1054 | valid = true; |
| 1055 | availableBytes = 0; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1056 | if (VERBOSE) Log.v(TAG, "read " + bytesRead + " bytes of journal"); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1057 | } catch (InvalidProtocolBufferNanoException e) { |
| 1058 | // 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] | 1059 | lastProtoException = e; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1060 | journal.clear(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1061 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1062 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1063 | if (DEBUG) Log.d(TAG, "journal bytes read: " + bytesRead); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1064 | if (!valid) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1065 | Log.w(TAG, "could not find a valid journal", lastProtoException); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1066 | } |
| 1067 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1068 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1069 | Log.w(TAG, "failed to close the journal", e); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1070 | } finally { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1071 | try { |
| 1072 | inStream.close(); |
| 1073 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1074 | Log.w(TAG, "failed to close the journal", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | return journal; |
| 1078 | } |
| 1079 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1080 | private void writeRowToBackup(Key key, MessageNano proto, BackupDataOutput data) |
| 1081 | throws IOException { |
| 1082 | writeRowToBackup(keyToBackupKey(key), proto, data); |
| 1083 | } |
| 1084 | |
| 1085 | private void writeRowToBackup(String backupKey, MessageNano proto, |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1086 | BackupDataOutput data) throws IOException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1087 | byte[] blob = writeCheckedBytes(proto); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1088 | data.writeEntityHeader(backupKey, blob.length); |
| 1089 | data.writeEntityData(blob, blob.length); |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 1090 | mBackupDataWasUpdated = true; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1091 | if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1094 | /** |
| 1095 | * Write the new journal to the output file. |
| 1096 | * |
| 1097 | * In the event of any error, just pretend we didn't have a journal, |
| 1098 | * in that case, do a full backup. |
| 1099 | |
| 1100 | * @param newState the write-only file descriptor pointing to the new journal |
| 1101 | * @param journal a Journal protocol buffer |
| 1102 | */ |
| 1103 | private void writeJournal(ParcelFileDescriptor newState, Journal journal) { |
| 1104 | FileOutputStream outStream = null; |
| 1105 | try { |
| 1106 | outStream = new FileOutputStream(newState.getFileDescriptor()); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1107 | final byte[] journalBytes = writeCheckedBytes(journal); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1108 | outStream.write(journalBytes); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1109 | outStream.close(); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1110 | if (VERBOSE) Log.v(TAG, "wrote " + journalBytes.length + " bytes of journal"); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1111 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1112 | Log.w(TAG, "failed to write backup journal", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | /** Wrap a proto in a CheckedMessage and compute the checksum. */ |
| 1117 | private byte[] writeCheckedBytes(MessageNano proto) { |
| 1118 | CheckedMessage wrapper = new CheckedMessage(); |
| 1119 | wrapper.payload = MessageNano.toByteArray(proto); |
| 1120 | CRC32 checksum = new CRC32(); |
| 1121 | checksum.update(wrapper.payload); |
| 1122 | wrapper.checksum = checksum.getValue(); |
| 1123 | return MessageNano.toByteArray(wrapper); |
| 1124 | } |
| 1125 | |
| 1126 | /** Unwrap a proto message from a CheckedMessage, verifying the checksum. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1127 | private static byte[] readCheckedBytes(byte[] buffer, int dataSize) |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1128 | throws InvalidProtocolBufferNanoException { |
| 1129 | CheckedMessage wrapper = new CheckedMessage(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1130 | MessageNano.mergeFrom(wrapper, buffer, 0, dataSize); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1131 | CRC32 checksum = new CRC32(); |
| 1132 | checksum.update(wrapper.payload); |
| 1133 | if (wrapper.checksum != checksum.getValue()) { |
| 1134 | throw new InvalidProtocolBufferNanoException("checksum does not match"); |
| 1135 | } |
| 1136 | return wrapper.payload; |
| 1137 | } |
| 1138 | |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 1139 | private boolean initializeIconCache() { |
| 1140 | if (mIconCache != null) { |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | final LauncherAppState appState = LauncherAppState.getInstanceNoCreate(); |
| 1145 | if (appState == null) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 1146 | if (DEBUG) { |
| 1147 | Throwable stackTrace = new Throwable(); |
| 1148 | stackTrace.fillInStackTrace(); |
| 1149 | Log.w(TAG, "Failed to get app state during backup/restore", stackTrace); |
| 1150 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 1151 | return false; |
| 1152 | } |
| 1153 | mIconCache = appState.getIconCache(); |
| 1154 | return mIconCache != null; |
| 1155 | } |
| 1156 | |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1157 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1158 | /** |
| 1159 | * @return true if the launcher is in a state to support backup |
| 1160 | */ |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1161 | private boolean launcherIsReady() { |
| 1162 | ContentResolver cr = mContext.getContentResolver(); |
| 1163 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, null, null, null); |
| 1164 | if (cursor == null) { |
| 1165 | // launcher data has been wiped, do nothing |
| 1166 | return false; |
| 1167 | } |
| 1168 | cursor.close(); |
| 1169 | |
| 1170 | if (!initializeIconCache()) { |
| 1171 | // launcher services are unavailable, try again later |
| 1172 | dataChanged(); |
| 1173 | return false; |
| 1174 | } |
| 1175 | |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 1179 | private String getUserSelectionArg() { |
| 1180 | return Favorites.PROFILE_ID + '=' + UserManagerCompat.getInstance(mContext) |
| 1181 | .getSerialNumberForUser(UserHandleCompat.myUserHandle()); |
| 1182 | } |
| 1183 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 1184 | private class InvalidBackupException extends IOException { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1185 | |
| 1186 | private static final long serialVersionUID = 8931456637211665082L; |
| 1187 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 1188 | private InvalidBackupException(Throwable cause) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1189 | super(cause); |
| 1190 | } |
| 1191 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 1192 | public InvalidBackupException(String reason) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1193 | super(reason); |
| 1194 | } |
| 1195 | } |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1196 | |
| 1197 | /** |
| 1198 | * A class to check if an activity can handle one of the intents from a list of |
| 1199 | * predefined intents. |
| 1200 | */ |
| 1201 | private class ItemTypeMatcher { |
| 1202 | |
| 1203 | private final ArrayList<Intent> mIntents; |
| 1204 | |
| 1205 | ItemTypeMatcher(int xml_res) { |
| 1206 | mIntents = xml_res == 0 ? new ArrayList<Intent>() : parseIntents(xml_res); |
| 1207 | } |
| 1208 | |
| 1209 | private ArrayList<Intent> parseIntents(int xml_res) { |
| 1210 | ArrayList<Intent> intents = new ArrayList<Intent>(); |
| 1211 | XmlResourceParser parser = mContext.getResources().getXml(xml_res); |
| 1212 | try { |
| 1213 | DefaultLayoutParser.beginDocument(parser, DefaultLayoutParser.TAG_RESOLVE); |
| 1214 | final int depth = parser.getDepth(); |
| 1215 | int type; |
| 1216 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 1217 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 1218 | if (type != XmlPullParser.START_TAG) { |
| 1219 | continue; |
| 1220 | } else if (DefaultLayoutParser.TAG_FAVORITE.equals(parser.getName())) { |
| 1221 | final String uri = DefaultLayoutParser.getAttributeValue( |
| 1222 | parser, DefaultLayoutParser.ATTR_URI); |
| 1223 | intents.add(Intent.parseUri(uri, 0)); |
| 1224 | } |
| 1225 | } |
| 1226 | } catch (URISyntaxException | XmlPullParserException | IOException e) { |
| 1227 | Log.e(TAG, "Unable to parse " + xml_res, e); |
| 1228 | } finally { |
| 1229 | parser.close(); |
| 1230 | } |
| 1231 | return intents; |
| 1232 | } |
| 1233 | |
| 1234 | public boolean matches(ActivityInfo activity, PackageManager pm) { |
| 1235 | for (Intent intent : mIntents) { |
| 1236 | intent.setPackage(activity.packageName); |
| 1237 | ResolveInfo info = pm.resolveActivity(intent, 0); |
| 1238 | if (info != null && (info.activityInfo.name.equals(activity.name) |
| 1239 | || info.activityInfo.name.equals(activity.targetActivity))) { |
| 1240 | return true; |
| 1241 | } |
| 1242 | } |
| 1243 | return false; |
| 1244 | } |
| 1245 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1246 | } |