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