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