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; |
Sunny Goyal | a6164c5 | 2016-01-06 14:47:13 -0800 | [diff] [blame] | 44 | import com.android.launcher3.backup.nano.BackupProtos; |
| 45 | import com.android.launcher3.backup.nano.BackupProtos.CheckedMessage; |
| 46 | import com.android.launcher3.backup.nano.BackupProtos.DeviceProfieData; |
| 47 | import com.android.launcher3.backup.nano.BackupProtos.Favorite; |
| 48 | import com.android.launcher3.backup.nano.BackupProtos.Journal; |
| 49 | import com.android.launcher3.backup.nano.BackupProtos.Key; |
| 50 | import com.android.launcher3.backup.nano.BackupProtos.Resource; |
| 51 | import com.android.launcher3.backup.nano.BackupProtos.Screen; |
| 52 | import com.android.launcher3.backup.nano.BackupProtos.Widget; |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 53 | import com.android.launcher3.compat.AppWidgetManagerCompat; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 54 | import com.android.launcher3.compat.UserHandleCompat; |
| 55 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 56 | import com.android.launcher3.model.GridSizeMigrationTask; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 57 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 58 | import com.google.protobuf.nano.InvalidProtocolBufferNanoException; |
| 59 | import com.google.protobuf.nano.MessageNano; |
| 60 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 61 | import org.xmlpull.v1.XmlPullParser; |
| 62 | import org.xmlpull.v1.XmlPullParserException; |
| 63 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 64 | import java.io.FileInputStream; |
| 65 | import java.io.FileOutputStream; |
| 66 | import java.io.IOException; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 67 | import java.net.URISyntaxException; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 68 | import java.util.ArrayList; |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 69 | import java.util.Arrays; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 70 | import java.util.HashSet; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 71 | import java.util.zip.CRC32; |
| 72 | |
| 73 | /** |
| 74 | * Persist the launcher home state across calamities. |
| 75 | */ |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 76 | public class LauncherBackupHelper implements BackupHelper { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 77 | private static final String TAG = "LauncherBackupHelper"; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 78 | private static final boolean VERBOSE = LauncherBackupAgentHelper.VERBOSE; |
| 79 | private static final boolean DEBUG = LauncherBackupAgentHelper.DEBUG; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 80 | |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 81 | private static final int BACKUP_VERSION = 4; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 82 | private static final int MAX_JOURNAL_SIZE = 1000000; |
| 83 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 84 | // Journal key is such that it is always smaller than any dynamically generated |
| 85 | // key (any Base64 encoded string). |
| 86 | private static final String JOURNAL_KEY = "#"; |
| 87 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 88 | /** icons are large, dribble them out */ |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 89 | private static final int MAX_ICONS_PER_PASS = 10; |
| 90 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 91 | /** widgets contain previews, which are very large, dribble them out */ |
| 92 | private static final int MAX_WIDGETS_PER_PASS = 5; |
| 93 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 94 | private static final String[] FAVORITE_PROJECTION = { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 95 | Favorites._ID, // 0 |
| 96 | Favorites.MODIFIED, // 1 |
| 97 | Favorites.INTENT, // 2 |
| 98 | Favorites.APPWIDGET_PROVIDER, // 3 |
| 99 | Favorites.APPWIDGET_ID, // 4 |
| 100 | Favorites.CELLX, // 5 |
| 101 | Favorites.CELLY, // 6 |
| 102 | Favorites.CONTAINER, // 7 |
| 103 | Favorites.ICON, // 8 |
| 104 | Favorites.ICON_PACKAGE, // 9 |
| 105 | Favorites.ICON_RESOURCE, // 10 |
Sunny Goyal | eb4b799 | 2016-04-21 14:30:18 -0700 | [diff] [blame] | 106 | Favorites.ITEM_TYPE, // 11 |
| 107 | Favorites.SCREEN, // 12 |
| 108 | Favorites.SPANX, // 13 |
| 109 | Favorites.SPANY, // 14 |
| 110 | Favorites.TITLE, // 15 |
| 111 | Favorites.PROFILE_ID, // 16 |
| 112 | Favorites.RANK, // 17 |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | private static final int ID_INDEX = 0; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 116 | private static final int ID_MODIFIED = 1; |
| 117 | private static final int INTENT_INDEX = 2; |
| 118 | private static final int APPWIDGET_PROVIDER_INDEX = 3; |
| 119 | private static final int APPWIDGET_ID_INDEX = 4; |
| 120 | private static final int CELLX_INDEX = 5; |
| 121 | private static final int CELLY_INDEX = 6; |
| 122 | private static final int CONTAINER_INDEX = 7; |
| 123 | private static final int ICON_INDEX = 8; |
| 124 | private static final int ICON_PACKAGE_INDEX = 9; |
| 125 | private static final int ICON_RESOURCE_INDEX = 10; |
Sunny Goyal | eb4b799 | 2016-04-21 14:30:18 -0700 | [diff] [blame] | 126 | private static final int ITEM_TYPE_INDEX = 11; |
| 127 | private static final int SCREEN_INDEX = 12; |
| 128 | private static final int SPANX_INDEX = 13; |
| 129 | private static final int SPANY_INDEX = 14; |
| 130 | private static final int TITLE_INDEX = 15; |
| 131 | private static final int RANK_INDEX = 17; |
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 | |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 317 | if (GridSizeMigrationTask.ENABLED) { |
| 318 | // One time migrate the workspace when launcher starts. |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 319 | migrationCompatibleProfileData = initDeviceProfileData(mIdp); |
| 320 | migrationCompatibleProfileData.desktopCols = oldProfile.desktopCols; |
| 321 | migrationCompatibleProfileData.desktopRows = oldProfile.desktopRows; |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 322 | migrationCompatibleProfileData.hotseatCount = oldProfile.hotseatCount; |
| 323 | migrationCompatibleProfileData.allappsRank = oldProfile.allappsRank; |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 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; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 442 | return data; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 446 | * Write all modified favorites to the data stream. |
| 447 | * |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 448 | * @param data output stream for key/value pairs |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 449 | * @throws IOException |
| 450 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 451 | private void backupFavorites(BackupDataOutput data) throws IOException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 452 | // persist things that have changed since the last backup |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 453 | ContentResolver cr = mContext.getContentResolver(); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 454 | // Don't backup apps in other profiles for now. |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 455 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 456 | getUserSelectionArg(), null, null); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 457 | try { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 458 | cursor.moveToPosition(-1); |
| 459 | while(cursor.moveToNext()) { |
| 460 | final long id = cursor.getLong(ID_INDEX); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 461 | final long updateTime = cursor.getLong(ID_MODIFIED); |
| 462 | Key key = getKey(Key.FAVORITE, id); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 463 | mKeys.add(key); |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 464 | final String backupKey = keyToBackupKey(key); |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 465 | |
| 466 | // Favorite proto changed in v4. Backup again if the version is old. |
| 467 | if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime |
| 468 | || restoredBackupVersion < 4) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 469 | writeRowToBackup(key, packFavorite(cursor), data); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 470 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 471 | if (DEBUG) Log.d(TAG, "favorite already backup up: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 472 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 473 | } |
| 474 | } finally { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 475 | cursor.close(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 476 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Read a favorite from the stream. |
| 481 | * |
| 482 | * <P>Keys arrive in any order, so screens and containers may not exist yet. |
| 483 | * |
| 484 | * @param key identifier for the row |
| 485 | * @param buffer the serialized proto from the stream, may be larger than dataSize |
| 486 | * @param dataSize the size of the proto from the stream |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 487 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 488 | private void restoreFavorite(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 489 | if (VERBOSE) Log.v(TAG, "unpacking favorite " + key.id); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 490 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 491 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
| 492 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 493 | ContentResolver cr = mContext.getContentResolver(); |
| 494 | ContentValues values = unpackFavorite(buffer, dataSize); |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 495 | cr.insert(Favorites.CONTENT_URI, values); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Write all modified screens to the data stream. |
| 500 | * |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 501 | * @param data output stream for key/value pairs |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 502 | * @throws IOException |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 503 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 504 | private void backupScreens(BackupDataOutput data) throws IOException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 505 | // persist things that have changed since the last backup |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 506 | ContentResolver cr = mContext.getContentResolver(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 507 | Cursor cursor = cr.query(WorkspaceScreens.CONTENT_URI, SCREEN_PROJECTION, |
| 508 | null, null, null); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 509 | try { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 510 | cursor.moveToPosition(-1); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 511 | if (DEBUG) Log.d(TAG, "dumping screens after: " + mLastBackupRestoreTime); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 512 | while(cursor.moveToNext()) { |
| 513 | final long id = cursor.getLong(ID_INDEX); |
| 514 | final long updateTime = cursor.getLong(ID_MODIFIED); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 515 | Key key = getKey(Key.SCREEN, id); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 516 | mKeys.add(key); |
Chris Wren | 5743aa9 | 2014-01-10 18:02:06 -0500 | [diff] [blame] | 517 | final String backupKey = keyToBackupKey(key); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 518 | if (!mExistingKeys.contains(backupKey) || updateTime >= mLastBackupRestoreTime) { |
| 519 | writeRowToBackup(key, packScreen(cursor), data); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 520 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 521 | if (VERBOSE) Log.v(TAG, "screen already backup up " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 522 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 523 | } |
| 524 | } finally { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 525 | cursor.close(); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 526 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Read a screen from the stream. |
| 531 | * |
| 532 | * <P>Keys arrive in any order, so children of this screen may already exist. |
| 533 | * |
| 534 | * @param key identifier for the row |
| 535 | * @param buffer the serialized proto from the stream, may be larger than dataSize |
| 536 | * @param dataSize the size of the proto from the stream |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 537 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 538 | private void restoreScreen(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 539 | if (VERBOSE) Log.v(TAG, "unpacking screen " + key.id); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 540 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 541 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 542 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 543 | ContentResolver cr = mContext.getContentResolver(); |
| 544 | ContentValues values = unpackScreen(buffer, dataSize); |
| 545 | cr.insert(WorkspaceScreens.CONTENT_URI, values); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 548 | /** |
| 549 | * Write all the static icon resources we need to render placeholders |
| 550 | * for a package that is not installed. |
| 551 | * |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 552 | * @param data output stream for key/value pairs |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 553 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 554 | private void backupIcons(BackupDataOutput data) throws IOException { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 555 | // persist icons that haven't been persisted yet |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 556 | final ContentResolver cr = mContext.getContentResolver(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 557 | final int dpi = mContext.getResources().getDisplayMetrics().densityDpi; |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 558 | final UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 559 | int backupUpIconCount = 0; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 560 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 561 | // Don't backup apps in other profiles for now. |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 562 | String where = "(" + Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPLICATION + " OR " + |
Kenny Guy | 43ea7ac | 2014-05-09 16:44:18 +0100 | [diff] [blame] | 563 | Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_SHORTCUT + ") AND " + |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 564 | getUserSelectionArg(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 565 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
| 566 | where, null, null); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 567 | try { |
| 568 | cursor.moveToPosition(-1); |
| 569 | while(cursor.moveToNext()) { |
| 570 | final long id = cursor.getLong(ID_INDEX); |
| 571 | final String intentDescription = cursor.getString(INTENT_INDEX); |
| 572 | try { |
| 573 | Intent intent = Intent.parseUri(intentDescription, 0); |
| 574 | ComponentName cn = intent.getComponent(); |
| 575 | Key key = null; |
| 576 | String backupKey = null; |
| 577 | if (cn != null) { |
| 578 | key = getKey(Key.ICON, cn.flattenToShortString()); |
| 579 | backupKey = keyToBackupKey(key); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 580 | } else { |
| 581 | Log.w(TAG, "empty intent on application favorite: " + id); |
| 582 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 583 | if (mExistingKeys.contains(backupKey)) { |
| 584 | if (DEBUG) Log.d(TAG, "already saved icon " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 585 | |
| 586 | // remember that we already backed this up previously |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 587 | mKeys.add(key); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 588 | } else if (backupKey != null) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 589 | if (DEBUG) Log.d(TAG, "I can count this high: " + backupUpIconCount); |
| 590 | if (backupUpIconCount < MAX_ICONS_PER_PASS) { |
| 591 | if (DEBUG) Log.d(TAG, "saving icon " + backupKey); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 592 | Bitmap icon = mIconCache.getIcon(intent, myUserHandle); |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 593 | if (icon != null && !mIconCache.isDefaultIcon(icon, myUserHandle)) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 594 | writeRowToBackup(key, packIcon(dpi, icon), data); |
| 595 | mKeys.add(key); |
| 596 | backupUpIconCount ++; |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 597 | } |
| 598 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 599 | if (VERBOSE) Log.v(TAG, "deferring icon backup " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 600 | // too many icons for this pass, request another. |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 601 | dataChanged(); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | } catch (URISyntaxException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 605 | Log.e(TAG, "invalid URI on application favorite: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 606 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 607 | Log.e(TAG, "unable to save application icon for favorite: " + id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | } |
| 611 | } finally { |
| 612 | cursor.close(); |
| 613 | } |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Read an icon from the stream. |
| 618 | * |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 619 | * <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] | 620 | * |
| 621 | * @param key identifier for the row |
| 622 | * @param buffer the serialized proto from the stream, may be larger than dataSize |
| 623 | * @param dataSize the size of the proto from the stream |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 624 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 625 | private void restoreIcon(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 626 | if (VERBOSE) Log.v(TAG, "unpacking icon " + key.id); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 627 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 628 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Chris Wren | 6d0dde0 | 2014-02-10 12:16:54 -0500 | [diff] [blame] | 629 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 630 | Resource res = unpackProto(new Resource(), buffer, dataSize); |
| 631 | if (DEBUG) { |
| 632 | Log.d(TAG, "unpacked " + res.dpi + " dpi icon"); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 633 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 634 | Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length); |
| 635 | if (icon == null) { |
| 636 | Log.w(TAG, "failed to unpack icon for " + key.name); |
Sunny Goyal | df6ccf8 | 2015-07-20 14:32:48 -0700 | [diff] [blame] | 637 | } else { |
| 638 | if (VERBOSE) Log.v(TAG, "saving restored icon as: " + key.name); |
| 639 | mIconCache.preloadIcon(ComponentName.unflattenFromString(key.name), icon, res.dpi, |
| 640 | "" /* label */, mUserSerial, mIdp); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 641 | } |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 644 | /** |
| 645 | * Write all the static widget resources we need to render placeholders |
| 646 | * for a package that is not installed. |
| 647 | * |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 648 | * @param data output stream for key/value pairs |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 649 | * @throws IOException |
| 650 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 651 | private void backupWidgets(BackupDataOutput data) throws IOException { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 652 | // persist static widget info that hasn't been persisted yet |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 653 | final ContentResolver cr = mContext.getContentResolver(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 654 | final int dpi = mContext.getResources().getDisplayMetrics().densityDpi; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 655 | int backupWidgetCount = 0; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 656 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 657 | String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET + " AND " |
| 658 | + getUserSelectionArg(); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 659 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, |
| 660 | where, null, null); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 661 | AppWidgetManagerCompat widgetManager = AppWidgetManagerCompat.getInstance(mContext); |
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); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 668 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 669 | Key key = null; |
| 670 | String backupKey = null; |
| 671 | if (provider != null) { |
| 672 | key = getKey(Key.WIDGET, providerName); |
| 673 | backupKey = keyToBackupKey(key); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 674 | } else { |
| 675 | Log.w(TAG, "empty intent on appwidget: " + id); |
| 676 | } |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 677 | |
| 678 | // Widget backup proto changed in v3. So add it again if the original backup is old. |
| 679 | if (mExistingKeys.contains(backupKey) && restoredBackupVersion >= 3) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 680 | if (DEBUG) Log.d(TAG, "already saved widget " + backupKey); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 681 | |
| 682 | // remember that we already backed this up previously |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 683 | mKeys.add(key); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 684 | } else if (backupKey != null) { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 685 | if (DEBUG) Log.d(TAG, "I can count this high: " + backupWidgetCount); |
| 686 | if (backupWidgetCount < MAX_WIDGETS_PER_PASS) { |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 687 | LauncherAppWidgetProviderInfo widgetInfo = widgetManager |
| 688 | .getLauncherAppWidgetInfo(cursor.getInt(APPWIDGET_ID_INDEX)); |
| 689 | if (widgetInfo != null) { |
| 690 | if (DEBUG) Log.d(TAG, "saving widget " + backupKey); |
| 691 | writeRowToBackup(key, packWidget(dpi, widgetInfo), data); |
| 692 | mKeys.add(key); |
| 693 | backupWidgetCount ++; |
| 694 | } |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 695 | } else { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 696 | if (VERBOSE) Log.v(TAG, "deferring widget backup " + backupKey); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 697 | // too many widgets for this pass, request another. |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 698 | dataChanged(); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | } |
| 702 | } finally { |
| 703 | cursor.close(); |
| 704 | } |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Read a widget from the stream. |
| 709 | * |
| 710 | * <P>Keys arrive in any order, so widgets that use this data may already exist. |
| 711 | * |
| 712 | * @param key identifier for the row |
| 713 | * @param buffer the serialized proto from the stream, may be larger than dataSize |
| 714 | * @param dataSize the size of the proto from the stream |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 715 | */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 716 | private void restoreWidget(Key key, byte[] buffer, int dataSize) throws IOException { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 717 | if (VERBOSE) Log.v(TAG, "unpacking widget " + key.id); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 718 | if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " + |
| 719 | Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP)); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 720 | Widget widget = unpackProto(new Widget(), buffer, dataSize); |
| 721 | if (DEBUG) Log.d(TAG, "unpacked " + widget.provider); |
| 722 | if (widget.icon.data != null) { |
| 723 | Bitmap icon = BitmapFactory |
| 724 | .decodeByteArray(widget.icon.data, 0, widget.icon.data.length); |
| 725 | if (icon == null) { |
| 726 | Log.w(TAG, "failed to unpack widget icon for " + key.name); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 727 | } else { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 728 | mIconCache.preloadIcon(ComponentName.unflattenFromString(widget.provider), |
Sunny Goyal | df6ccf8 | 2015-07-20 14:32:48 -0700 | [diff] [blame] | 729 | icon, widget.icon.dpi, widget.label, mUserSerial, mIdp); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 730 | } |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 731 | } |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 732 | |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 733 | // Cache widget min sizes incase migration is required. |
| 734 | widgetSizes.add(widget.provider + "#" + widget.minSpanX + "," + widget.minSpanY); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 735 | } |
| 736 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 737 | /** create a new key, with an integer ID. |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 738 | * |
| 739 | * <P> Keys contain their own checksum instead of using |
| 740 | * the heavy-weight CheckedMessage wrapper. |
| 741 | */ |
| 742 | private Key getKey(int type, long id) { |
| 743 | Key key = new Key(); |
| 744 | key.type = type; |
| 745 | key.id = id; |
| 746 | key.checksum = checkKey(key); |
| 747 | return key; |
| 748 | } |
| 749 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 750 | /** create a new key for a named object. |
| 751 | * |
| 752 | * <P> Keys contain their own checksum instead of using |
| 753 | * the heavy-weight CheckedMessage wrapper. |
| 754 | */ |
| 755 | private Key getKey(int type, String name) { |
| 756 | Key key = new Key(); |
| 757 | key.type = type; |
| 758 | key.name = name; |
| 759 | key.checksum = checkKey(key); |
| 760 | return key; |
| 761 | } |
| 762 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 763 | /** keys need to be strings, serialize and encode. */ |
| 764 | private String keyToBackupKey(Key key) { |
Chris Wren | 978194c | 2013-10-03 17:47:22 -0400 | [diff] [blame] | 765 | return Base64.encodeToString(Key.toByteArray(key), Base64.NO_WRAP); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /** keys need to be strings, decode and parse. */ |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 769 | private Key backupKeyToKey(String backupKey) throws InvalidBackupException { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 770 | try { |
| 771 | Key key = Key.parseFrom(Base64.decode(backupKey, Base64.DEFAULT)); |
| 772 | if (key.checksum != checkKey(key)) { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 773 | throw new InvalidBackupException("invalid key read from stream" + backupKey); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 774 | } |
| 775 | return key; |
Sunny Goyal | b740f59 | 2015-12-17 23:22:42 -0800 | [diff] [blame] | 776 | } catch (InvalidProtocolBufferNanoException | IllegalArgumentException e) { |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 777 | throw new InvalidBackupException(e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
| 781 | /** Compute the checksum over the important bits of a key. */ |
| 782 | private long checkKey(Key key) { |
| 783 | CRC32 checksum = new CRC32(); |
| 784 | checksum.update(key.type); |
| 785 | checksum.update((int) (key.id & 0xffff)); |
| 786 | checksum.update((int) ((key.id >> 32) & 0xffff)); |
| 787 | if (!TextUtils.isEmpty(key.name)) { |
| 788 | checksum.update(key.name.getBytes()); |
| 789 | } |
| 790 | return checksum.getValue(); |
| 791 | } |
| 792 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 793 | /** |
| 794 | * @return true if its an hotseat item, that can be replaced during restore. |
| 795 | * TODO: Extend check for folders in hotseat. |
| 796 | */ |
| 797 | private boolean isReplaceableHotseatItem(Favorite favorite) { |
| 798 | return favorite.container == Favorites.CONTAINER_HOTSEAT |
| 799 | && favorite.intent != null |
| 800 | && (favorite.itemType == Favorites.ITEM_TYPE_APPLICATION |
| 801 | || favorite.itemType == Favorites.ITEM_TYPE_SHORTCUT); |
| 802 | } |
| 803 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 804 | /** Serialize a Favorite for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 805 | private Favorite packFavorite(Cursor c) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 806 | Favorite favorite = new Favorite(); |
| 807 | favorite.id = c.getLong(ID_INDEX); |
| 808 | favorite.screen = c.getInt(SCREEN_INDEX); |
| 809 | favorite.container = c.getInt(CONTAINER_INDEX); |
| 810 | favorite.cellX = c.getInt(CELLX_INDEX); |
| 811 | favorite.cellY = c.getInt(CELLY_INDEX); |
| 812 | favorite.spanX = c.getInt(SPANX_INDEX); |
| 813 | favorite.spanY = c.getInt(SPANY_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) { |
Sunny Goyal | eb4b799 | 2016-04-21 14:30:18 -0700 | [diff] [blame] | 839 | String iconPackage = c.getString(ICON_PACKAGE_INDEX); |
| 840 | String iconResource = c.getString(ICON_RESOURCE_INDEX); |
| 841 | if (!TextUtils.isEmpty(iconPackage) && !TextUtils.isEmpty(iconResource)) { |
| 842 | favorite.iconResource = iconResource; |
| 843 | favorite.iconPackage = iconPackage; |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | byte[] blob = c.getBlob(ICON_INDEX); |
| 847 | if (blob != null && blob.length > 0) { |
| 848 | favorite.icon = blob; |
| 849 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 850 | } |
| 851 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 852 | if (isReplaceableHotseatItem(favorite)) { |
| 853 | if (intent != null && intent.getComponent() != null) { |
| 854 | PackageManager pm = mContext.getPackageManager(); |
| 855 | ActivityInfo activity = null;; |
| 856 | try { |
| 857 | activity = pm.getActivityInfo(intent.getComponent(), 0); |
| 858 | } catch (NameNotFoundException e) { |
| 859 | Log.e(TAG, "Target not found", e); |
| 860 | } |
| 861 | if (activity == null) { |
| 862 | return favorite; |
| 863 | } |
| 864 | for (int i = 0; i < mItemTypeMatchers.length; i++) { |
| 865 | if (mItemTypeMatchers[i] == null) { |
| 866 | mItemTypeMatchers[i] = new ItemTypeMatcher( |
| 867 | CommonAppTypeParser.getResourceForItemType(i)); |
| 868 | } |
| 869 | if (mItemTypeMatchers[i].matches(activity, pm)) { |
| 870 | favorite.targetType = i; |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 877 | return favorite; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /** Deserialize a Favorite from persistence, after verifying checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 881 | private ContentValues unpackFavorite(byte[] buffer, int dataSize) |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 882 | throws IOException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 883 | Favorite favorite = unpackProto(new Favorite(), buffer, dataSize); |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 884 | |
Sunny Goyal | c115e64 | 2015-07-20 14:23:43 -0700 | [diff] [blame] | 885 | // If it is a hotseat item, move it accordingly. |
| 886 | if (favorite.container == Favorites.CONTAINER_HOTSEAT) { |
| 887 | favorite.screen += mHotseatShift; |
| 888 | } |
| 889 | |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 890 | ContentValues values = new ContentValues(); |
| 891 | values.put(Favorites._ID, favorite.id); |
| 892 | values.put(Favorites.SCREEN, favorite.screen); |
| 893 | values.put(Favorites.CONTAINER, favorite.container); |
| 894 | values.put(Favorites.CELLX, favorite.cellX); |
| 895 | values.put(Favorites.CELLY, favorite.cellY); |
| 896 | values.put(Favorites.SPANX, favorite.spanX); |
| 897 | values.put(Favorites.SPANY, favorite.spanY); |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 898 | values.put(Favorites.RANK, favorite.rank); |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 899 | |
| 900 | if (favorite.itemType == Favorites.ITEM_TYPE_SHORTCUT) { |
Sunny Goyal | eb4b799 | 2016-04-21 14:30:18 -0700 | [diff] [blame] | 901 | values.put(Favorites.ICON_PACKAGE, favorite.iconPackage); |
| 902 | values.put(Favorites.ICON_RESOURCE, favorite.iconResource); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 903 | values.put(Favorites.ICON, favorite.icon); |
| 904 | } |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 905 | |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 906 | if (!TextUtils.isEmpty(favorite.title)) { |
| 907 | values.put(Favorites.TITLE, favorite.title); |
| 908 | } else { |
| 909 | values.put(Favorites.TITLE, ""); |
| 910 | } |
| 911 | if (!TextUtils.isEmpty(favorite.intent)) { |
| 912 | values.put(Favorites.INTENT, favorite.intent); |
| 913 | } |
| 914 | values.put(Favorites.ITEM_TYPE, favorite.itemType); |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 915 | |
Kenny Guy | f8b1dfd | 2014-05-13 12:59:34 +0100 | [diff] [blame] | 916 | UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); |
| 917 | long userSerialNumber = |
| 918 | UserManagerCompat.getInstance(mContext).getSerialNumberForUser(myUserHandle); |
| 919 | values.put(LauncherSettings.Favorites.PROFILE_ID, userSerialNumber); |
| 920 | |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 921 | // If we will attempt grid resize, use the original profile to validate grid size, as |
| 922 | // anything which fits in the original grid should fit in the current grid after |
| 923 | // grid migration. |
| 924 | DeviceProfieData currentProfile = migrationCompatibleProfileData == null |
| 925 | ? mDeviceProfileData : migrationCompatibleProfileData; |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 926 | |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 927 | if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) { |
| 928 | if (!TextUtils.isEmpty(favorite.appWidgetProvider)) { |
| 929 | values.put(Favorites.APPWIDGET_PROVIDER, favorite.appWidgetProvider); |
| 930 | } |
| 931 | values.put(Favorites.APPWIDGET_ID, favorite.appWidgetId); |
| 932 | values.put(LauncherSettings.Favorites.RESTORED, |
| 933 | LauncherAppWidgetInfo.FLAG_ID_NOT_VALID | |
| 934 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY | |
| 935 | LauncherAppWidgetInfo.FLAG_UI_NOT_READY); |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 936 | |
| 937 | // Verify placement |
| 938 | if (((favorite.cellX + favorite.spanX) > currentProfile.desktopCols) |
| 939 | || ((favorite.cellY + favorite.spanY) > currentProfile.desktopRows)) { |
| 940 | restoreSuccessful = false; |
| 941 | throw new InvalidBackupException("Widget not in screen bounds, aborting restore"); |
| 942 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 943 | } else { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 944 | // Check if it is an hotseat item, that can be replaced. |
| 945 | if (isReplaceableHotseatItem(favorite) |
| 946 | && favorite.targetType != Favorite.TARGET_NONE |
| 947 | && favorite.targetType < CommonAppTypeParser.SUPPORTED_TYPE_COUNT) { |
| 948 | Log.e(TAG, "Added item type flag"); |
| 949 | values.put(LauncherSettings.Favorites.RESTORED, |
| 950 | 1 | CommonAppTypeParser.encodeItemTypeToFlag(favorite.targetType)); |
| 951 | } else { |
| 952 | // Let LauncherModel know we've been here. |
| 953 | values.put(LauncherSettings.Favorites.RESTORED, 1); |
| 954 | } |
Sunny Goyal | 5fd733d | 2014-10-29 10:51:54 -0700 | [diff] [blame] | 955 | |
| 956 | // Verify placement |
| 957 | if (favorite.container == Favorites.CONTAINER_HOTSEAT) { |
| 958 | if ((favorite.screen >= currentProfile.hotseatCount) |
| 959 | || (favorite.screen == currentProfile.allappsRank)) { |
| 960 | restoreSuccessful = false; |
| 961 | throw new InvalidBackupException("Item not in hotseat bounds, aborting restore"); |
| 962 | } |
| 963 | } else { |
| 964 | if ((favorite.cellX >= currentProfile.desktopCols) |
| 965 | || (favorite.cellY >= currentProfile.desktopRows)) { |
| 966 | restoreSuccessful = false; |
| 967 | throw new InvalidBackupException("Item not in desktop bounds, aborting restore"); |
| 968 | } |
| 969 | } |
Sunny Goyal | ff57227 | 2014-07-23 13:58:07 -0700 | [diff] [blame] | 970 | } |
Chris Wren | f4d0811 | 2014-01-16 18:13:56 -0500 | [diff] [blame] | 971 | |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 972 | return values; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /** Serialize a Screen for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 976 | private Screen packScreen(Cursor c) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 977 | Screen screen = new Screen(); |
| 978 | screen.id = c.getLong(ID_INDEX); |
| 979 | screen.rank = c.getInt(SCREEN_RANK_INDEX); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 980 | return screen; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /** Deserialize a Screen from persistence, after verifying checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 984 | private ContentValues unpackScreen(byte[] buffer, int dataSize) |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 985 | throws InvalidProtocolBufferNanoException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 986 | Screen screen = unpackProto(new Screen(), buffer, dataSize); |
Chris Wren | 5dee7af | 2013-12-20 17:22:11 -0500 | [diff] [blame] | 987 | ContentValues values = new ContentValues(); |
| 988 | values.put(WorkspaceScreens._ID, screen.id); |
| 989 | values.put(WorkspaceScreens.SCREEN_RANK, screen.rank); |
| 990 | return values; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 991 | } |
| 992 | |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 993 | /** Serialize an icon Resource for persistence, including a checksum wrapper. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 994 | private Resource packIcon(int dpi, Bitmap icon) { |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 995 | Resource res = new Resource(); |
| 996 | res.dpi = dpi; |
Sunny Goyal | 73a22a5 | 2015-04-28 16:51:09 -0700 | [diff] [blame] | 997 | res.data = Utilities.flattenBitmap(icon); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 998 | return res; |
| 999 | } |
| 1000 | |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1001 | /** Serialize a widget for persistence, including a checksum wrapper. */ |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1002 | private Widget packWidget(int dpi, LauncherAppWidgetProviderInfo info) { |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1003 | Widget widget = new Widget(); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1004 | widget.provider = info.provider.flattenToShortString(); |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1005 | widget.label = info.label; |
| 1006 | widget.configure = info.configure != null; |
| 1007 | if (info.icon != 0) { |
| 1008 | widget.icon = new Resource(); |
Sunny Goyal | 2e1efb4 | 2016-03-03 16:58:55 -0800 | [diff] [blame] | 1009 | Drawable fullResIcon = mIconCache.getFullResIcon(info.provider.getPackageName(), info.icon); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1010 | Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext); |
Sunny Goyal | 73a22a5 | 2015-04-28 16:51:09 -0700 | [diff] [blame] | 1011 | widget.icon.data = Utilities.flattenBitmap(icon); |
| 1012 | widget.icon.dpi = dpi; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1013 | } |
Sunny Goyal | 3a30cfe | 2015-07-16 17:27:43 -0700 | [diff] [blame] | 1014 | |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 1015 | Point spans = info.getMinSpans(mIdp, mContext); |
| 1016 | widget.minSpanX = spans.x; |
| 1017 | widget.minSpanY = spans.y; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1018 | return widget; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1019 | } |
| 1020 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1021 | /** |
| 1022 | * Deserialize a proto after verifying checksum wrapper. |
| 1023 | */ |
| 1024 | private <T extends MessageNano> T unpackProto(T proto, byte[] buffer, int dataSize) |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1025 | throws InvalidProtocolBufferNanoException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1026 | MessageNano.mergeFrom(proto, readCheckedBytes(buffer, dataSize)); |
| 1027 | if (DEBUG) Log.d(TAG, "unpacked proto " + proto); |
| 1028 | return proto; |
Chris Wren | fd13c71 | 2013-09-27 15:45:19 -0400 | [diff] [blame] | 1029 | } |
| 1030 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1031 | /** |
| 1032 | * Read the old journal from the input file. |
| 1033 | * |
| 1034 | * In the event of any error, just pretend we didn't have a journal, |
| 1035 | * in that case, do a full backup. |
| 1036 | * |
| 1037 | * @param oldState the read-0only file descriptor pointing to the old journal |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1038 | * @return a Journal protocol buffer |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1039 | */ |
| 1040 | private Journal readJournal(ParcelFileDescriptor oldState) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1041 | Journal journal = new Journal(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1042 | if (oldState == null) { |
| 1043 | return journal; |
| 1044 | } |
| 1045 | FileInputStream inStream = new FileInputStream(oldState.getFileDescriptor()); |
| 1046 | try { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1047 | int availableBytes = inStream.available(); |
| 1048 | if (DEBUG) Log.d(TAG, "available " + availableBytes); |
| 1049 | if (availableBytes < MAX_JOURNAL_SIZE) { |
| 1050 | byte[] buffer = new byte[availableBytes]; |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1051 | int bytesRead = 0; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1052 | boolean valid = false; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1053 | InvalidProtocolBufferNanoException lastProtoException = null; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1054 | while (availableBytes > 0) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1055 | try { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1056 | // OMG what are you doing? This is crazy inefficient! |
| 1057 | // If we read a byte that is not ours, we will cause trouble: b/12491813 |
| 1058 | // However, we don't know how many bytes to expect (oops). |
| 1059 | // So we have to step through *slowly*, watching for the end. |
| 1060 | int result = inStream.read(buffer, bytesRead, 1); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1061 | if (result > 0) { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1062 | availableBytes -= result; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1063 | bytesRead += result; |
| 1064 | } else { |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1065 | Log.w(TAG, "unexpected end of file while reading journal."); |
| 1066 | // stop reading and see what there is to parse |
| 1067 | availableBytes = 0; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1068 | } |
| 1069 | } catch (IOException e) { |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1070 | buffer = null; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1071 | availableBytes = 0; |
| 1072 | } |
| 1073 | |
| 1074 | // check the buffer to see if we have a valid journal |
| 1075 | try { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1076 | MessageNano.mergeFrom(journal, readCheckedBytes(buffer, bytesRead)); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1077 | // if we are here, then we have read a valid, checksum-verified journal |
| 1078 | valid = true; |
| 1079 | availableBytes = 0; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1080 | if (VERBOSE) Log.v(TAG, "read " + bytesRead + " bytes of journal"); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1081 | } catch (InvalidProtocolBufferNanoException e) { |
| 1082 | // 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] | 1083 | lastProtoException = e; |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1084 | journal.clear(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1085 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1086 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1087 | if (DEBUG) Log.d(TAG, "journal bytes read: " + bytesRead); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1088 | if (!valid) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1089 | Log.w(TAG, "could not find a valid journal", lastProtoException); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1090 | } |
| 1091 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1092 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1093 | Log.w(TAG, "failed to close the journal", e); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1094 | } finally { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1095 | try { |
| 1096 | inStream.close(); |
| 1097 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1098 | Log.w(TAG, "failed to close the journal", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | return journal; |
| 1102 | } |
| 1103 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1104 | private void writeRowToBackup(Key key, MessageNano proto, BackupDataOutput data) |
| 1105 | throws IOException { |
| 1106 | writeRowToBackup(keyToBackupKey(key), proto, data); |
| 1107 | } |
| 1108 | |
| 1109 | private void writeRowToBackup(String backupKey, MessageNano proto, |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1110 | BackupDataOutput data) throws IOException { |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1111 | byte[] blob = writeCheckedBytes(proto); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1112 | data.writeEntityHeader(backupKey, blob.length); |
| 1113 | data.writeEntityData(blob, blob.length); |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 1114 | mBackupDataWasUpdated = true; |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1115 | if (VERBOSE) Log.v(TAG, "Writing New entry " + backupKey); |
Chris Wren | 22e130d | 2013-09-23 18:25:57 -0400 | [diff] [blame] | 1116 | } |
| 1117 | |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1118 | /** |
| 1119 | * Write the new journal to the output file. |
| 1120 | * |
| 1121 | * In the event of any error, just pretend we didn't have a journal, |
| 1122 | * in that case, do a full backup. |
| 1123 | |
| 1124 | * @param newState the write-only file descriptor pointing to the new journal |
| 1125 | * @param journal a Journal protocol buffer |
| 1126 | */ |
| 1127 | private void writeJournal(ParcelFileDescriptor newState, Journal journal) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1128 | try { |
Sunny Goyal | b740f59 | 2015-12-17 23:22:42 -0800 | [diff] [blame] | 1129 | FileOutputStream outStream = new FileOutputStream(newState.getFileDescriptor()); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1130 | final byte[] journalBytes = writeCheckedBytes(journal); |
Chris Wren | 65b6a60 | 2014-01-10 14:11:25 -0500 | [diff] [blame] | 1131 | outStream.write(journalBytes); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1132 | outStream.close(); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1133 | if (VERBOSE) Log.v(TAG, "wrote " + journalBytes.length + " bytes of journal"); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1134 | } catch (IOException e) { |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 1135 | Log.w(TAG, "failed to write backup journal", e); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /** Wrap a proto in a CheckedMessage and compute the checksum. */ |
| 1140 | private byte[] writeCheckedBytes(MessageNano proto) { |
| 1141 | CheckedMessage wrapper = new CheckedMessage(); |
| 1142 | wrapper.payload = MessageNano.toByteArray(proto); |
| 1143 | CRC32 checksum = new CRC32(); |
| 1144 | checksum.update(wrapper.payload); |
| 1145 | wrapper.checksum = checksum.getValue(); |
| 1146 | return MessageNano.toByteArray(wrapper); |
| 1147 | } |
| 1148 | |
| 1149 | /** Unwrap a proto message from a CheckedMessage, verifying the checksum. */ |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1150 | private static byte[] readCheckedBytes(byte[] buffer, int dataSize) |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1151 | throws InvalidProtocolBufferNanoException { |
| 1152 | CheckedMessage wrapper = new CheckedMessage(); |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1153 | MessageNano.mergeFrom(wrapper, buffer, 0, dataSize); |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1154 | CRC32 checksum = new CRC32(); |
| 1155 | checksum.update(wrapper.payload); |
| 1156 | if (wrapper.checksum != checksum.getValue()) { |
| 1157 | throw new InvalidProtocolBufferNanoException("checksum does not match"); |
| 1158 | } |
| 1159 | return wrapper.payload; |
| 1160 | } |
| 1161 | |
Sunny Goyal | ef728d4 | 2014-10-22 11:28:28 -0700 | [diff] [blame] | 1162 | /** |
| 1163 | * @return true if the launcher is in a state to support backup |
| 1164 | */ |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1165 | private boolean launcherIsReady() { |
| 1166 | ContentResolver cr = mContext.getContentResolver(); |
| 1167 | Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, null, null, null); |
| 1168 | if (cursor == null) { |
| 1169 | // launcher data has been wiped, do nothing |
| 1170 | return false; |
| 1171 | } |
| 1172 | cursor.close(); |
| 1173 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 1174 | if (LauncherAppState.getInstanceNoCreate() == null) { |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1175 | // launcher services are unavailable, try again later |
Chris Wren | 7114426 | 2014-02-27 15:49:39 -0500 | [diff] [blame] | 1176 | return false; |
| 1177 | } |
| 1178 | |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
Sunny Goyal | ffe83f1 | 2014-08-14 17:39:34 -0700 | [diff] [blame] | 1182 | private String getUserSelectionArg() { |
| 1183 | return Favorites.PROFILE_ID + '=' + UserManagerCompat.getInstance(mContext) |
| 1184 | .getSerialNumberForUser(UserHandleCompat.myUserHandle()); |
| 1185 | } |
| 1186 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 1187 | @Thunk class InvalidBackupException extends IOException { |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1188 | |
| 1189 | private static final long serialVersionUID = 8931456637211665082L; |
| 1190 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 1191 | @Thunk InvalidBackupException(Throwable cause) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1192 | super(cause); |
| 1193 | } |
| 1194 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 1195 | @Thunk InvalidBackupException(String reason) { |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1196 | super(reason); |
| 1197 | } |
| 1198 | } |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1199 | |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 1200 | public boolean shouldAttemptWorkspaceMigration() { |
| 1201 | return migrationCompatibleProfileData != null; |
| 1202 | } |
| 1203 | |
Sunny Goyal | bb3b02f | 2015-01-15 12:00:14 -0800 | [diff] [blame] | 1204 | /** |
| 1205 | * A class to check if an activity can handle one of the intents from a list of |
| 1206 | * predefined intents. |
| 1207 | */ |
| 1208 | private class ItemTypeMatcher { |
| 1209 | |
| 1210 | private final ArrayList<Intent> mIntents; |
| 1211 | |
| 1212 | ItemTypeMatcher(int xml_res) { |
| 1213 | mIntents = xml_res == 0 ? new ArrayList<Intent>() : parseIntents(xml_res); |
| 1214 | } |
| 1215 | |
| 1216 | private ArrayList<Intent> parseIntents(int xml_res) { |
| 1217 | ArrayList<Intent> intents = new ArrayList<Intent>(); |
| 1218 | XmlResourceParser parser = mContext.getResources().getXml(xml_res); |
| 1219 | try { |
| 1220 | DefaultLayoutParser.beginDocument(parser, DefaultLayoutParser.TAG_RESOLVE); |
| 1221 | final int depth = parser.getDepth(); |
| 1222 | int type; |
| 1223 | while (((type = parser.next()) != XmlPullParser.END_TAG || |
| 1224 | parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { |
| 1225 | if (type != XmlPullParser.START_TAG) { |
| 1226 | continue; |
| 1227 | } else if (DefaultLayoutParser.TAG_FAVORITE.equals(parser.getName())) { |
| 1228 | final String uri = DefaultLayoutParser.getAttributeValue( |
| 1229 | parser, DefaultLayoutParser.ATTR_URI); |
| 1230 | intents.add(Intent.parseUri(uri, 0)); |
| 1231 | } |
| 1232 | } |
| 1233 | } catch (URISyntaxException | XmlPullParserException | IOException e) { |
| 1234 | Log.e(TAG, "Unable to parse " + xml_res, e); |
| 1235 | } finally { |
| 1236 | parser.close(); |
| 1237 | } |
| 1238 | return intents; |
| 1239 | } |
| 1240 | |
| 1241 | public boolean matches(ActivityInfo activity, PackageManager pm) { |
| 1242 | for (Intent intent : mIntents) { |
| 1243 | intent.setPackage(activity.packageName); |
| 1244 | ResolveInfo info = pm.resolveActivity(intent, 0); |
| 1245 | if (info != null && (info.activityInfo.name.equals(activity.name) |
| 1246 | || info.activityInfo.name.equals(activity.targetActivity))) { |
| 1247 | return true; |
| 1248 | } |
| 1249 | } |
| 1250 | return false; |
| 1251 | } |
| 1252 | } |
Chris Wren | 1ada10d | 2013-09-13 18:01:38 -0400 | [diff] [blame] | 1253 | } |