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