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