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