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