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