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