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