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(); |
| 633 | final WidgetPreviewLoader previewLoader = new WidgetPreviewLoader(mContext); |
| 634 | final PagedViewCellLayout widgetSpacingLayout = new PagedViewCellLayout(mContext); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 635 | final int dpi = mContext.getResources().getDisplayMetrics().densityDpi; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 636 | final DeviceProfile profile = appState.getDynamicGrid().getDeviceProfile(); |
| 637 | if (DEBUG) Log.d(TAG, "cellWidthPx: " + profile.cellWidthPx); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 638 | int backupWidgetCount = 0; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 639 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 640 | String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET + " AND " |
| 641 | + getUserSelectionArg(); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 642 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
| 643 | where, null, null); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 644 | try { |
| 645 | cursor.moveToPosition(-1); |
| 646 | while(cursor.moveToNext()) { |
| 647 | final long id = cursor.getLong(ID_INDEX); |
| 648 | final String providerName = cursor.getString(APPWIDGET_PROVIDER_INDEX); |
| 649 | final int spanX = cursor.getInt(SPANX_INDEX); |
| 650 | final int spanY = cursor.getInt(SPANY_INDEX); |
| 651 | final ComponentName provider = ComponentName.unflattenFromString(providerName); |
| 652 | Key key = null; |
| 653 | String backupKey = null; |
| 654 | if (provider != null) { |
| 655 | key = getKey(Key.WIDGET, providerName); |
| 656 | backupKey = keyToBackupKey(key); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 657 | } else { |
| 658 | Log.w(TAG, "empty intent on appwidget: " + id); |
| 659 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 660 | if (mExistingKeys.contains(backupKey)) { |
| 661 | if (DEBUG) Log.d(TAG, "already saved widget " + backupKey); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 662 | |
| 663 | // remember that we already backed this up previously |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 664 | mKeys.add(key); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 665 | } else if (backupKey != null) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 666 | if (DEBUG) Log.d(TAG, "I can count this high: " + backupWidgetCount); |
| 667 | if (backupWidgetCount < MAX_WIDGETS_PER_PASS) { |
| 668 | if (DEBUG) Log.d(TAG, "saving widget " + backupKey); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 669 | previewLoader.setPreviewSize(spanX * profile.cellWidthPx, |
| 670 | spanY * profile.cellHeightPx, widgetSpacingLayout); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 671 | writeRowToBackup(key, packWidget(dpi, previewLoader, mIconCache, provider), data); |
| 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 | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 980 | private Widget packWidget(int dpi, WidgetPreviewLoader previewLoader, IconCache iconCache, |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 981 | ComponentName provider) { |
Adam Cohen | 5940042 | 2014-03-05 18:07:04 -0800 | [diff] [blame] | 982 | final LauncherAppWidgetProviderInfo info = |
| 983 | LauncherModel.getProviderInfo(mContext, provider); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 984 | Widget widget = new Widget(); |
| 985 | widget.provider = provider.flattenToShortString(); |
| 986 | widget.label = info.label; |
| 987 | widget.configure = info.configure != null; |
| 988 | if (info.icon != 0) { |
| 989 | widget.icon = new Resource(); |
| 990 | Drawable fullResIcon = iconCache.getFullResIcon(provider.getPackageName(), info.icon); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 991 | Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 992 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
Chris Wren | b86f076 | 2013-10-04 10:10:21 -0400 | [diff] [blame] | 993 | if (icon.compress(IMAGE_FORMAT, IMAGE_COMPRESSION_QUALITY, os)) { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 994 | widget.icon.data = os.toByteArray(); |
| 995 | widget.icon.dpi = dpi; |
| 996 | } |
| 997 | } |
| 998 | if (info.previewImage != 0) { |
| 999 | widget.preview = new Resource(); |
| 1000 | Bitmap preview = previewLoader.generateWidgetPreview(info, null); |
| 1001 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
Chris Wren | b86f076 | 2013-10-04 10:10:21 -0400 | [diff] [blame] | 1002 | if (preview.compress(IMAGE_FORMAT, IMAGE_COMPRESSION_QUALITY, os)) { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1003 | widget.preview.data = os.toByteArray(); |
| 1004 | widget.preview.dpi = dpi; |
| 1005 | } |
| 1006 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1007 | return widget; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1008 | } |
| 1009 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1010 | /** |
| 1011 | * Deserialize a proto after verifying checksum wrapper. |
| 1012 | */ |
| 1013 | private <T extends MessageNano> T unpackProto(T proto, byte[] buffer, int dataSize) |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1014 | throws InvalidProtocolBufferNanoException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1015 | MessageNano.mergeFrom(proto, readCheckedBytes(buffer, dataSize)); |
| 1016 | if (DEBUG) Log.d(TAG, "unpacked proto " + proto); |
| 1017 | return proto; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1020 | /** |
| 1021 | * Read the old journal from the input file. |
| 1022 | * |
| 1023 | * In the event of any error, just pretend we didn't have a journal, |
| 1024 | * in that case, do a full backup. |
| 1025 | * |
| 1026 | * @param oldState the read-0only file descriptor pointing to the old journal |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1027 | * @return a Journal protocol buffer |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1028 | */ |
| 1029 | private Journal readJournal(ParcelFileDescriptor oldState) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1030 | Journal journal = new Journal(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1031 | if (oldState == null) { |
| 1032 | return journal; |
| 1033 | } |
| 1034 | FileInputStream inStream = new FileInputStream(oldState.getFileDescriptor()); |
| 1035 | try { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1036 | int availableBytes = inStream.available(); |
| 1037 | if (DEBUG) Log.d(TAG, "available " + availableBytes); |
| 1038 | if (availableBytes < MAX_JOURNAL_SIZE) { |
| 1039 | byte[] buffer = new byte[availableBytes]; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1040 | int bytesRead = 0; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1041 | boolean valid = false; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1042 | InvalidProtocolBufferNanoException lastProtoException = null; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1043 | while (availableBytes > 0) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1044 | try { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1045 | // OMG what are you doing? This is crazy inefficient! |
| 1046 | // If we read a byte that is not ours, we will cause trouble: b/12491813 |
| 1047 | // However, we don't know how many bytes to expect (oops). |
| 1048 | // So we have to step through *slowly*, watching for the end. |
| 1049 | int result = inStream.read(buffer, bytesRead, 1); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1050 | if (result > 0) { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1051 | availableBytes -= result; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1052 | bytesRead += result; |
| 1053 | } else { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1054 | Log.w(TAG, "unexpected end of file while reading journal."); |
| 1055 | // stop reading and see what there is to parse |
| 1056 | availableBytes = 0; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1057 | } |
| 1058 | } catch (IOException e) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1059 | buffer = null; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1060 | availableBytes = 0; |
| 1061 | } |
| 1062 | |
| 1063 | // check the buffer to see if we have a valid journal |
| 1064 | try { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1065 | MessageNano.mergeFrom(journal, readCheckedBytes(buffer, bytesRead)); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1066 | // if we are here, then we have read a valid, checksum-verified journal |
| 1067 | valid = true; |
| 1068 | availableBytes = 0; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1069 | if (VERBOSE) Log.v(TAG, "read " + bytesRead + " bytes of journal"); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1070 | } catch (InvalidProtocolBufferNanoException e) { |
| 1071 | // 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] | 1072 | lastProtoException = e; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1073 | journal.clear(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1074 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1075 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1076 | if (DEBUG) Log.d(TAG, "journal bytes read: " + bytesRead); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1077 | if (!valid) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1078 | Log.w(TAG, "could not find a valid journal", lastProtoException); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1079 | } |
| 1080 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1081 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1082 | Log.w(TAG, "failed to close the journal", e); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1083 | } finally { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1084 | try { |
| 1085 | inStream.close(); |
| 1086 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1087 | Log.w(TAG, "failed to close the journal", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | return journal; |
| 1091 | } |
| 1092 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1093 | private void writeRowToBackup(Key key, MessageNano proto, BackupDataOutput data) |
| 1094 | throws IOException { |
| 1095 | writeRowToBackup(keyToBackupKey(key), proto, data); |
| 1096 | } |
| 1097 | |
| 1098 | private void writeRowToBackup(String backupKey, MessageNano proto, |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1099 | BackupDataOutput data) throws IOException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1100 | byte[] blob = writeCheckedBytes(proto); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1101 | data.writeEntityHeader(backupKey, blob.length); |
| 1102 | data.writeEntityData(blob, blob.length); |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 1103 | mBackupDataWasUpdated = true; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1104 | if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1105 | } |
| 1106 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1107 | /** |
| 1108 | * Write the new journal to the output file. |
| 1109 | * |
| 1110 | * In the event of any error, just pretend we didn't have a journal, |
| 1111 | * in that case, do a full backup. |
| 1112 | |
| 1113 | * @param newState the write-only file descriptor pointing to the new journal |
| 1114 | * @param journal a Journal protocol buffer |
| 1115 | */ |
| 1116 | private void writeJournal(ParcelFileDescriptor newState, Journal journal) { |
| 1117 | FileOutputStream outStream = null; |
| 1118 | try { |
| 1119 | outStream = new FileOutputStream(newState.getFileDescriptor()); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1120 | final byte[] journalBytes = writeCheckedBytes(journal); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1121 | outStream.write(journalBytes); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1122 | outStream.close(); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1123 | if (VERBOSE) Log.v(TAG, "wrote " + journalBytes.length + " bytes of journal"); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1124 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1125 | Log.w(TAG, "failed to write backup journal", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | /** Wrap a proto in a CheckedMessage and compute the checksum. */ |
| 1130 | private byte[] writeCheckedBytes(MessageNano proto) { |
| 1131 | CheckedMessage wrapper = new CheckedMessage(); |
| 1132 | wrapper.payload = MessageNano.toByteArray(proto); |
| 1133 | CRC32 checksum = new CRC32(); |
| 1134 | checksum.update(wrapper.payload); |
| 1135 | wrapper.checksum = checksum.getValue(); |
| 1136 | return MessageNano.toByteArray(wrapper); |
| 1137 | } |
| 1138 | |
| 1139 | /** Unwrap a proto message from a CheckedMessage, verifying the checksum. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1140 | private static byte[] readCheckedBytes(byte[] buffer, int dataSize) |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1141 | throws InvalidProtocolBufferNanoException { |
| 1142 | CheckedMessage wrapper = new CheckedMessage(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1143 | MessageNano.mergeFrom(wrapper, buffer, 0, dataSize); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1144 | CRC32 checksum = new CRC32(); |
| 1145 | checksum.update(wrapper.payload); |
| 1146 | if (wrapper.checksum != checksum.getValue()) { |
| 1147 | throw new InvalidProtocolBufferNanoException("checksum does not match"); |
| 1148 | } |
| 1149 | return wrapper.payload; |
| 1150 | } |
| 1151 | |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 1152 | private boolean initializeIconCache() { |
| 1153 | if (mIconCache != null) { |
| 1154 | return true; |
| 1155 | } |
| 1156 | |
| 1157 | final LauncherAppState appState = LauncherAppState.getInstanceNoCreate(); |
| 1158 | if (appState == null) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame^] | 1159 | if (DEBUG) { |
| 1160 | Throwable stackTrace = new Throwable(); |
| 1161 | stackTrace.fillInStackTrace(); |
| 1162 | Log.w(TAG, "Failed to get app state during backup/restore", stackTrace); |
| 1163 | } |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 1164 | return false; |
| 1165 | } |
| 1166 | mIconCache = appState.getIconCache(); |
| 1167 | return mIconCache != null; |
| 1168 | } |
| 1169 | |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1170 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1171 | /** |
| 1172 | * @return true if the launcher is in a state to support backup |
| 1173 | */ |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1174 | private boolean launcherIsReady() { |
| 1175 | ContentResolver cr = mContext.getContentResolver(); |
| 1176 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, null, null, null); |
| 1177 | if (cursor == null) { |
| 1178 | // launcher data has been wiped, do nothing |
| 1179 | return false; |
| 1180 | } |
| 1181 | cursor.close(); |
| 1182 | |
| 1183 | if (!initializeIconCache()) { |
| 1184 | // launcher services are unavailable, try again later |
| 1185 | dataChanged(); |
| 1186 | return false; |
| 1187 | } |
| 1188 | |
| 1189 | return true; |
| 1190 | } |
| 1191 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 1192 | private String getUserSelectionArg() { |
| 1193 | return Favorites.PROFILE_ID + '=' + UserManagerCompat.getInstance(mContext) |
| 1194 | .getSerialNumberForUser(UserHandleCompat.myUserHandle()); |
| 1195 | } |
| 1196 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 1197 | private class InvalidBackupException extends IOException { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1198 | |
| 1199 | private static final long serialVersionUID = 8931456637211665082L; |
| 1200 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 1201 | private InvalidBackupException(Throwable cause) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1202 | super(cause); |
| 1203 | } |
| 1204 | |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 1205 | public InvalidBackupException(String reason) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1206 | super(reason); |
| 1207 | } |
| 1208 | } |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1209 | |
| 1210 | /** |
| 1211 | * A class to check if an activity can handle one of the intents from a list of |
| 1212 | * predefined intents. |
| 1213 | */ |
| 1214 | private class ItemTypeMatcher { |
| 1215 | |
| 1216 | private final ArrayList<Intent> mIntents; |
| 1217 | |
| 1218 | ItemTypeMatcher(int xml_res) { |
| 1219 | mIntents = xml_res == 0 ? new ArrayList<Intent>() : parseIntents(xml_res); |
| 1220 | } |
| 1221 | |
| 1222 | private ArrayList<Intent> parseIntents(int xml_res) { |
| 1223 | ArrayList<Intent> intents = new ArrayList<Intent>(); |
| 1224 | XmlResourceParser parser = mContext.getResources().getXml(xml_res); |
| 1225 | try { |
| 1226 | DefaultLayoutParser.beginDocument(parser, DefaultLayoutParser.TAG_RESOLVE); |
| 1227 | final int depth = parser.getDepth(); |
| 1228 | int type; |
| 1229 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 1230 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 1231 | if (type != XmlPullParser.START_TAG) { |
| 1232 | continue; |
| 1233 | } else if (DefaultLayoutParser.TAG_FAVORITE.equals(parser.getName())) { |
| 1234 | final String uri = DefaultLayoutParser.getAttributeValue( |
| 1235 | parser, DefaultLayoutParser.ATTR_URI); |
| 1236 | intents.add(Intent.parseUri(uri, 0)); |
| 1237 | } |
| 1238 | } |
| 1239 | } catch (URISyntaxException | XmlPullParserException | IOException e) { |
| 1240 | Log.e(TAG, "Unable to parse " + xml_res, e); |
| 1241 | } finally { |
| 1242 | parser.close(); |
| 1243 | } |
| 1244 | return intents; |
| 1245 | } |
| 1246 | |
| 1247 | public boolean matches(ActivityInfo activity, PackageManager pm) { |
| 1248 | for (Intent intent : mIntents) { |
| 1249 | intent.setPackage(activity.packageName); |
| 1250 | ResolveInfo info = pm.resolveActivity(intent, 0); |
| 1251 | if (info != null && (info.activityInfo.name.equals(activity.name) |
| 1252 | || info.activityInfo.name.equals(activity.targetActivity))) { |
| 1253 | return true; |
| 1254 | } |
| 1255 | } |
| 1256 | return false; |
| 1257 | } |
| 1258 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1259 | } |