blob: 8023fcd36a3833b1053a519269ba4324a9606bf6 [file] [log] [blame]
Chris Wren1ada10d2013-09-13 18:01:38 -04001/*
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
17package com.android.launcher3;
18
19import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
20import com.google.protobuf.nano.MessageNano;
21
Chris Wren1ada10d2013-09-13 18:01:38 -040022import com.android.launcher3.LauncherSettings.Favorites;
23import com.android.launcher3.LauncherSettings.WorkspaceScreens;
24import com.android.launcher3.backup.BackupProtos;
25import com.android.launcher3.backup.BackupProtos.CheckedMessage;
26import com.android.launcher3.backup.BackupProtos.Favorite;
27import com.android.launcher3.backup.BackupProtos.Journal;
28import com.android.launcher3.backup.BackupProtos.Key;
Chris Wren22e130d2013-09-23 18:25:57 -040029import com.android.launcher3.backup.BackupProtos.Resource;
Chris Wren1ada10d2013-09-13 18:01:38 -040030import com.android.launcher3.backup.BackupProtos.Screen;
Chris Wrenfd13c712013-09-27 15:45:19 -040031import com.android.launcher3.backup.BackupProtos.Widget;
Chris Wren1ada10d2013-09-13 18:01:38 -040032
Chris Wren92aa4232013-10-04 11:29:36 -040033import android.app.backup.BackupDataInputStream;
Chris Wren1ada10d2013-09-13 18:01:38 -040034import android.app.backup.BackupDataOutput;
Chris Wren4d89e2a2013-10-09 17:03:50 -040035import android.app.backup.BackupHelper;
Chris Wren1ada10d2013-09-13 18:01:38 -040036import android.app.backup.BackupManager;
Chris Wren22e130d2013-09-23 18:25:57 -040037import android.appwidget.AppWidgetManager;
38import android.appwidget.AppWidgetProviderInfo;
39import android.content.ComponentName;
Chris Wren1ada10d2013-09-13 18:01:38 -040040import android.content.ContentResolver;
41import android.content.Context;
Chris Wren22e130d2013-09-23 18:25:57 -040042import android.content.Intent;
Chris Wren1ada10d2013-09-13 18:01:38 -040043import android.database.Cursor;
Chris Wren22e130d2013-09-23 18:25:57 -040044import android.graphics.Bitmap;
45import android.graphics.BitmapFactory;
Chris Wrenfd13c712013-09-27 15:45:19 -040046import android.graphics.drawable.Drawable;
Chris Wren1ada10d2013-09-13 18:01:38 -040047import android.os.ParcelFileDescriptor;
Chris Wren1ada10d2013-09-13 18:01:38 -040048import android.text.TextUtils;
49import android.util.Base64;
50import android.util.Log;
51
Chris Wren22e130d2013-09-23 18:25:57 -040052import java.io.ByteArrayOutputStream;
Chris Wren1ada10d2013-09-13 18:01:38 -040053import java.io.FileInputStream;
54import java.io.FileOutputStream;
55import java.io.IOException;
Chris Wren22e130d2013-09-23 18:25:57 -040056import java.net.URISyntaxException;
Chris Wren1ada10d2013-09-13 18:01:38 -040057import java.util.ArrayList;
Chris Wren22e130d2013-09-23 18:25:57 -040058import java.util.HashMap;
Chris Wren1ada10d2013-09-13 18:01:38 -040059import java.util.HashSet;
Chris Wren22e130d2013-09-23 18:25:57 -040060import java.util.List;
Chris Wren1ada10d2013-09-13 18:01:38 -040061import java.util.Set;
62import java.util.zip.CRC32;
63
64/**
65 * Persist the launcher home state across calamities.
66 */
Chris Wren92aa4232013-10-04 11:29:36 -040067public class LauncherBackupHelper implements BackupHelper {
Chris Wren1ada10d2013-09-13 18:01:38 -040068
Chris Wren92aa4232013-10-04 11:29:36 -040069 private static final String TAG = "LauncherBackupHelper";
Chris Wrenfd13c712013-09-27 15:45:19 -040070 private static final boolean DEBUG = false;
Chris Wren92aa4232013-10-04 11:29:36 -040071 private static final boolean DEBUG_PAYLOAD = false;
Chris Wren1ada10d2013-09-13 18:01:38 -040072
73 private static final int MAX_JOURNAL_SIZE = 1000000;
74
Chris Wrenfd13c712013-09-27 15:45:19 -040075 /** icons are large, dribble them out */
Chris Wren22e130d2013-09-23 18:25:57 -040076 private static final int MAX_ICONS_PER_PASS = 10;
77
Chris Wrenfd13c712013-09-27 15:45:19 -040078 /** widgets contain previews, which are very large, dribble them out */
79 private static final int MAX_WIDGETS_PER_PASS = 5;
80
81 public static final int IMAGE_COMPRESSION_QUALITY = 75;
82
Chris Wren92aa4232013-10-04 11:29:36 -040083 public static final String LAUNCHER_PREFIX = "L";
84
Chris Wrenb86f0762013-10-04 10:10:21 -040085 private static final Bitmap.CompressFormat IMAGE_FORMAT =
86 android.graphics.Bitmap.CompressFormat.PNG;
87
Chris Wren1ada10d2013-09-13 18:01:38 -040088 private static BackupManager sBackupManager;
89
90 private static final String[] FAVORITE_PROJECTION = {
91 Favorites._ID, // 0
Chris Wren22e130d2013-09-23 18:25:57 -040092 Favorites.MODIFIED, // 1
93 Favorites.INTENT, // 2
94 Favorites.APPWIDGET_PROVIDER, // 3
95 Favorites.APPWIDGET_ID, // 4
96 Favorites.CELLX, // 5
97 Favorites.CELLY, // 6
98 Favorites.CONTAINER, // 7
99 Favorites.ICON, // 8
100 Favorites.ICON_PACKAGE, // 9
101 Favorites.ICON_RESOURCE, // 10
102 Favorites.ICON_TYPE, // 11
103 Favorites.ITEM_TYPE, // 12
104 Favorites.SCREEN, // 13
105 Favorites.SPANX, // 14
106 Favorites.SPANY, // 15
107 Favorites.TITLE, // 16
Chris Wren1ada10d2013-09-13 18:01:38 -0400108 };
109
110 private static final int ID_INDEX = 0;
Chris Wren22e130d2013-09-23 18:25:57 -0400111 private static final int ID_MODIFIED = 1;
112 private static final int INTENT_INDEX = 2;
113 private static final int APPWIDGET_PROVIDER_INDEX = 3;
114 private static final int APPWIDGET_ID_INDEX = 4;
115 private static final int CELLX_INDEX = 5;
116 private static final int CELLY_INDEX = 6;
117 private static final int CONTAINER_INDEX = 7;
118 private static final int ICON_INDEX = 8;
119 private static final int ICON_PACKAGE_INDEX = 9;
120 private static final int ICON_RESOURCE_INDEX = 10;
121 private static final int ICON_TYPE_INDEX = 11;
122 private static final int ITEM_TYPE_INDEX = 12;
123 private static final int SCREEN_INDEX = 13;
124 private static final int SPANX_INDEX = 14;
125 private static final int SPANY_INDEX = 15;
126 private static final int TITLE_INDEX = 16;
Chris Wren1ada10d2013-09-13 18:01:38 -0400127
128 private static final String[] SCREEN_PROJECTION = {
129 WorkspaceScreens._ID, // 0
Chris Wren22e130d2013-09-23 18:25:57 -0400130 WorkspaceScreens.MODIFIED, // 1
131 WorkspaceScreens.SCREEN_RANK // 2
Chris Wren1ada10d2013-09-13 18:01:38 -0400132 };
133
Chris Wren22e130d2013-09-23 18:25:57 -0400134 private static final int SCREEN_RANK_INDEX = 2;
Chris Wren1ada10d2013-09-13 18:01:38 -0400135
Chris Wren92aa4232013-10-04 11:29:36 -0400136 private final Context mContext;
Chris Wren1ada10d2013-09-13 18:01:38 -0400137
Chris Wren22e130d2013-09-23 18:25:57 -0400138 private HashMap<ComponentName, AppWidgetProviderInfo> mWidgetMap;
139
Chris Wren92aa4232013-10-04 11:29:36 -0400140 private ArrayList<Key> mKeys;
Chris Wren1ada10d2013-09-13 18:01:38 -0400141
Chris Wren92aa4232013-10-04 11:29:36 -0400142 public LauncherBackupHelper(Context context) {
143 mContext = context;
144 }
145
146 private void dataChanged() {
Chris Wren1ada10d2013-09-13 18:01:38 -0400147 if (sBackupManager == null) {
Chris Wren92aa4232013-10-04 11:29:36 -0400148 sBackupManager = new BackupManager(mContext);
Chris Wren1ada10d2013-09-13 18:01:38 -0400149 }
150 sBackupManager.dataChanged();
151 }
152
153 /**
154 * Back up launcher data so we can restore the user's state on a new device.
155 *
156 * <P>The journal is a timestamp and a list of keys that were saved as of that time.
157 *
158 * <P>Keys may come back in any order, so each key/value is one complete row of the database.
159 *
160 * @param oldState notes from the last backup
161 * @param data incremental key/value pairs to persist off-device
162 * @param newState notes for the next backup
163 * @throws IOException
164 */
165 @Override
Chris Wren92aa4232013-10-04 11:29:36 -0400166 public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
167 ParcelFileDescriptor newState) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400168 Log.v(TAG, "onBackup");
169
170 Journal in = readJournal(oldState);
171 Journal out = new Journal();
172
173 long lastBackupTime = in.t;
174 out.t = System.currentTimeMillis();
175 out.rows = 0;
176 out.bytes = 0;
177
178 Log.v(TAG, "lastBackupTime=" + lastBackupTime);
179
180 ArrayList<Key> keys = new ArrayList<Key>();
Chris Wren92aa4232013-10-04 11:29:36 -0400181 try {
182 backupFavorites(in, data, out, keys);
183 backupScreens(in, data, out, keys);
184 backupIcons(in, data, out, keys);
185 backupWidgets(in, data, out, keys);
186 } catch (IOException e) {
187 Log.e(TAG, "launcher backup has failed", e);
188 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400189
190 out.key = keys.toArray(BackupProtos.Key.EMPTY_ARRAY);
191 writeJournal(newState, out);
192 Log.v(TAG, "onBackup: wrote " + out.bytes + "b in " + out.rows + " rows.");
Chris Wren1ada10d2013-09-13 18:01:38 -0400193 }
194
195 /**
Chris Wren92aa4232013-10-04 11:29:36 -0400196 * Restore launcher configuration from the restored data stream.
Chris Wren1ada10d2013-09-13 18:01:38 -0400197 *
198 * <P>Keys may arrive in any order.
199 *
Chris Wren92aa4232013-10-04 11:29:36 -0400200 * @param data the key/value pair from the server
Chris Wren1ada10d2013-09-13 18:01:38 -0400201 */
202 @Override
Chris Wren92aa4232013-10-04 11:29:36 -0400203 public void restoreEntity(BackupDataInputStream data) {
204 Log.v(TAG, "restoreEntity");
205 if (mKeys == null) {
206 mKeys = new ArrayList<Key>();
207 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400208 byte[] buffer = new byte[512];
Chris Wren1ada10d2013-09-13 18:01:38 -0400209 String backupKey = data.getKey();
Chris Wren92aa4232013-10-04 11:29:36 -0400210 int dataSize = data.size();
Chris Wren1ada10d2013-09-13 18:01:38 -0400211 if (buffer.length < dataSize) {
212 buffer = new byte[dataSize];
213 }
214 Key key = null;
Chris Wren92aa4232013-10-04 11:29:36 -0400215 int bytesRead = 0;
216 try {
217 bytesRead = data.read(buffer, 0, dataSize);
218 if (DEBUG) Log.d(TAG, "read " + bytesRead + " of " + dataSize + " available");
219 } catch (IOException e) {
220 Log.d(TAG, "failed to read entity from restore data", e);
221 }
222 try {
223 key = backupKeyToKey(backupKey);
224 switch (key.type) {
225 case Key.FAVORITE:
226 restoreFavorite(key, buffer, dataSize, mKeys);
227 break;
228
229 case Key.SCREEN:
230 restoreScreen(key, buffer, dataSize, mKeys);
231 break;
232
233 case Key.ICON:
234 restoreIcon(key, buffer, dataSize, mKeys);
235 break;
236
237 case Key.WIDGET:
238 restoreWidget(key, buffer, dataSize, mKeys);
239 break;
240
241 default:
242 Log.w(TAG, "unknown restore entity type: " + key.type);
243 break;
Chris Wren1ada10d2013-09-13 18:01:38 -0400244 }
Chris Wren92aa4232013-10-04 11:29:36 -0400245 } catch (KeyParsingException e) {
246 Log.w(TAG, "ignoring unparsable backup key: " + backupKey);
Chris Wren1ada10d2013-09-13 18:01:38 -0400247 }
248
Chris Wren92aa4232013-10-04 11:29:36 -0400249 }
250
251 /**
252 * Record the restore state for the next backup.
253 *
254 * @param newState notes about the backup state after restore.
255 */
256 @Override
257 public void writeNewStateDescription(ParcelFileDescriptor newState) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400258 // clear the output journal time, to force a full backup to
259 // will catch any changes the restore process might have made
Chris Wren92aa4232013-10-04 11:29:36 -0400260 Journal out = new Journal();
Chris Wren1ada10d2013-09-13 18:01:38 -0400261 out.t = 0;
Chris Wren92aa4232013-10-04 11:29:36 -0400262 out.key = mKeys.toArray(BackupProtos.Key.EMPTY_ARRAY);
Chris Wren1ada10d2013-09-13 18:01:38 -0400263 writeJournal(newState, out);
Chris Wren92aa4232013-10-04 11:29:36 -0400264 Log.v(TAG, "onRestore: read " + mKeys.size() + " rows");
265 mKeys.clear();
Chris Wren1ada10d2013-09-13 18:01:38 -0400266 }
267
268 /**
269 * Write all modified favorites to the data stream.
270 *
271 *
272 * @param in notes from last backup
273 * @param data output stream for key/value pairs
274 * @param out notes about this backup
275 * @param keys keys to mark as clean in the notes for next backup
276 * @throws IOException
277 */
278 private void backupFavorites(Journal in, BackupDataOutput data, Journal out,
279 ArrayList<Key> keys)
280 throws IOException {
281 // read the old ID set
Chris Wren22e130d2013-09-23 18:25:57 -0400282 Set<String> savedIds = getSavedIdsByType(Key.FAVORITE, in);
Chris Wren1ada10d2013-09-13 18:01:38 -0400283 if (DEBUG) Log.d(TAG, "favorite savedIds.size()=" + savedIds.size());
284
285 // persist things that have changed since the last backup
Chris Wren92aa4232013-10-04 11:29:36 -0400286 ContentResolver cr = mContext.getContentResolver();
Chris Wren22e130d2013-09-23 18:25:57 -0400287 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
288 null, null, null);
289 Set<String> currentIds = new HashSet<String>(cursor.getCount());
Chris Wren1ada10d2013-09-13 18:01:38 -0400290 try {
Chris Wren22e130d2013-09-23 18:25:57 -0400291 cursor.moveToPosition(-1);
292 while(cursor.moveToNext()) {
293 final long id = cursor.getLong(ID_INDEX);
294 final long updateTime = cursor.getLong(ID_MODIFIED);
Chris Wren1ada10d2013-09-13 18:01:38 -0400295 Key key = getKey(Key.FAVORITE, id);
Chris Wren1ada10d2013-09-13 18:01:38 -0400296 keys.add(key);
Chris Wren22e130d2013-09-23 18:25:57 -0400297 currentIds.add(keyToBackupKey(key));
298 if (updateTime > in.t) {
299 byte[] blob = packFavorite(cursor);
300 writeRowToBackup(key, blob, out, data);
301 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400302 }
303 } finally {
Chris Wren22e130d2013-09-23 18:25:57 -0400304 cursor.close();
Chris Wren1ada10d2013-09-13 18:01:38 -0400305 }
306 if (DEBUG) Log.d(TAG, "favorite currentIds.size()=" + currentIds.size());
307
308 // these IDs must have been deleted
309 savedIds.removeAll(currentIds);
Chris Wren22e130d2013-09-23 18:25:57 -0400310 out.rows += removeDeletedKeysFromBackup(savedIds, data);
Chris Wren1ada10d2013-09-13 18:01:38 -0400311 }
312
313 /**
314 * Read a favorite from the stream.
315 *
316 * <P>Keys arrive in any order, so screens and containers may not exist yet.
317 *
318 * @param key identifier for the row
319 * @param buffer the serialized proto from the stream, may be larger than dataSize
320 * @param dataSize the size of the proto from the stream
321 * @param keys keys to mark as clean in the notes for next backup
322 */
323 private void restoreFavorite(Key key, byte[] buffer, int dataSize, ArrayList<Key> keys) {
324 Log.v(TAG, "unpacking favorite " + key.id + " (" + dataSize + " bytes)");
325 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
326 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
327
328 try {
329 Favorite favorite = unpackFavorite(buffer, 0, dataSize);
330 if (DEBUG) Log.d(TAG, "unpacked " + favorite.itemType);
331 } catch (InvalidProtocolBufferNanoException e) {
332 Log.w(TAG, "failed to decode proto", e);
333 }
334 }
335
336 /**
337 * Write all modified screens to the data stream.
338 *
339 *
340 * @param in notes from last backup
341 * @param data output stream for key/value pairs
342 * @param out notes about this backup
Chris Wren22e130d2013-09-23 18:25:57 -0400343 * @param keys keys to mark as clean in the notes for next backup
344 * @throws IOException
Chris Wren1ada10d2013-09-13 18:01:38 -0400345 */
346 private void backupScreens(Journal in, BackupDataOutput data, Journal out,
347 ArrayList<Key> keys)
348 throws IOException {
349 // read the old ID set
Chris Wren22e130d2013-09-23 18:25:57 -0400350 Set<String> savedIds = getSavedIdsByType(Key.SCREEN, in);
351 if (DEBUG) Log.d(TAG, "screen savedIds.size()=" + savedIds.size());
Chris Wren1ada10d2013-09-13 18:01:38 -0400352
353 // persist things that have changed since the last backup
Chris Wren92aa4232013-10-04 11:29:36 -0400354 ContentResolver cr = mContext.getContentResolver();
Chris Wren22e130d2013-09-23 18:25:57 -0400355 Cursor cursor = cr.query(WorkspaceScreens.CONTENT_URI, SCREEN_PROJECTION,
356 null, null, null);
357 Set<String> currentIds = new HashSet<String>(cursor.getCount());
Chris Wren1ada10d2013-09-13 18:01:38 -0400358 try {
Chris Wren22e130d2013-09-23 18:25:57 -0400359 cursor.moveToPosition(-1);
360 while(cursor.moveToNext()) {
361 final long id = cursor.getLong(ID_INDEX);
362 final long updateTime = cursor.getLong(ID_MODIFIED);
Chris Wren1ada10d2013-09-13 18:01:38 -0400363 Key key = getKey(Key.SCREEN, id);
Chris Wren1ada10d2013-09-13 18:01:38 -0400364 keys.add(key);
Chris Wren22e130d2013-09-23 18:25:57 -0400365 currentIds.add(keyToBackupKey(key));
366 if (updateTime > in.t) {
367 byte[] blob = packScreen(cursor);
368 writeRowToBackup(key, blob, out, data);
369 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400370 }
371 } finally {
Chris Wren22e130d2013-09-23 18:25:57 -0400372 cursor.close();
Chris Wren1ada10d2013-09-13 18:01:38 -0400373 }
374 if (DEBUG) Log.d(TAG, "screen currentIds.size()=" + currentIds.size());
375
376 // these IDs must have been deleted
377 savedIds.removeAll(currentIds);
Chris Wren22e130d2013-09-23 18:25:57 -0400378 out.rows += removeDeletedKeysFromBackup(savedIds, data);
Chris Wren1ada10d2013-09-13 18:01:38 -0400379 }
380
381 /**
382 * Read a screen from the stream.
383 *
384 * <P>Keys arrive in any order, so children of this screen may already exist.
385 *
386 * @param key identifier for the row
387 * @param buffer the serialized proto from the stream, may be larger than dataSize
388 * @param dataSize the size of the proto from the stream
389 * @param keys keys to mark as clean in the notes for next backup
390 */
391 private void restoreScreen(Key key, byte[] buffer, int dataSize, ArrayList<Key> keys) {
392 Log.v(TAG, "unpacking screen " + key.id);
393 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
394 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
395 try {
396 Screen screen = unpackScreen(buffer, 0, dataSize);
397 if (DEBUG) Log.d(TAG, "unpacked " + screen.rank);
398 } catch (InvalidProtocolBufferNanoException e) {
399 Log.w(TAG, "failed to decode proto", e);
400 }
401 }
402
Chris Wren22e130d2013-09-23 18:25:57 -0400403 /**
404 * Write all the static icon resources we need to render placeholders
405 * for a package that is not installed.
406 *
407 * @param in notes from last backup
408 * @param data output stream for key/value pairs
409 * @param out notes about this backup
410 * @param keys keys to mark as clean in the notes for next backup
411 * @throws IOException
412 */
413 private void backupIcons(Journal in, BackupDataOutput data, Journal out,
414 ArrayList<Key> keys) throws IOException {
Chris Wrenfd13c712013-09-27 15:45:19 -0400415 // persist icons that haven't been persisted yet
Chris Wren92aa4232013-10-04 11:29:36 -0400416 final LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
417 if (appState == null) {
418 dataChanged(); // try again later
Chris Wrend8fe6de2013-10-04 10:42:14 -0400419 if (DEBUG) Log.d(TAG, "Launcher is not initialized, delaying icon backup");
420 return;
421 }
Chris Wren92aa4232013-10-04 11:29:36 -0400422 final ContentResolver cr = mContext.getContentResolver();
423 final IconCache iconCache = appState.getIconCache();
424 final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
Chris Wren22e130d2013-09-23 18:25:57 -0400425
426 // read the old ID set
427 Set<String> savedIds = getSavedIdsByType(Key.ICON, in);
428 if (DEBUG) Log.d(TAG, "icon savedIds.size()=" + savedIds.size());
429
430 int startRows = out.rows;
431 if (DEBUG) Log.d(TAG, "starting here: " + startRows);
432 String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPLICATION;
433 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
434 where, null, null);
435 Set<String> currentIds = new HashSet<String>(cursor.getCount());
436 try {
437 cursor.moveToPosition(-1);
438 while(cursor.moveToNext()) {
439 final long id = cursor.getLong(ID_INDEX);
440 final String intentDescription = cursor.getString(INTENT_INDEX);
441 try {
442 Intent intent = Intent.parseUri(intentDescription, 0);
443 ComponentName cn = intent.getComponent();
444 Key key = null;
445 String backupKey = null;
446 if (cn != null) {
447 key = getKey(Key.ICON, cn.flattenToShortString());
448 backupKey = keyToBackupKey(key);
449 currentIds.add(backupKey);
450 } else {
451 Log.w(TAG, "empty intent on application favorite: " + id);
452 }
453 if (savedIds.contains(backupKey)) {
454 if (DEBUG) Log.d(TAG, "already saved icon " + backupKey);
455
456 // remember that we already backed this up previously
457 keys.add(key);
458 } else if (backupKey != null) {
459 if (DEBUG) Log.d(TAG, "I can count this high: " + out.rows);
460 if ((out.rows - startRows) < MAX_ICONS_PER_PASS) {
461 if (DEBUG) Log.d(TAG, "saving icon " + backupKey);
462 Bitmap icon = iconCache.getIcon(intent);
463 keys.add(key);
464 if (icon != null && !iconCache.isDefaultIcon(icon)) {
465 byte[] blob = packIcon(dpi, icon);
466 writeRowToBackup(key, blob, out, data);
467 }
468 } else {
Chris Wrenfd13c712013-09-27 15:45:19 -0400469 if (DEBUG) Log.d(TAG, "scheduling another run for icon " + backupKey);
Chris Wren22e130d2013-09-23 18:25:57 -0400470 // too many icons for this pass, request another.
Chris Wren92aa4232013-10-04 11:29:36 -0400471 dataChanged();
Chris Wren22e130d2013-09-23 18:25:57 -0400472 }
473 }
474 } catch (URISyntaxException e) {
475 Log.w(TAG, "invalid URI on application favorite: " + id);
476 } catch (IOException e) {
477 Log.w(TAG, "unable to save application icon for favorite: " + id);
478 }
479
480 }
481 } finally {
482 cursor.close();
483 }
484 if (DEBUG) Log.d(TAG, "icon currentIds.size()=" + currentIds.size());
485
486 // these IDs must have been deleted
487 savedIds.removeAll(currentIds);
488 out.rows += removeDeletedKeysFromBackup(savedIds, data);
489 }
490
491 /**
492 * Read an icon from the stream.
493 *
Chris Wrenfd13c712013-09-27 15:45:19 -0400494 * <P>Keys arrive in any order, so shortcuts that use this icon may already exist.
Chris Wren22e130d2013-09-23 18:25:57 -0400495 *
496 * @param key identifier for the row
497 * @param buffer the serialized proto from the stream, may be larger than dataSize
498 * @param dataSize the size of the proto from the stream
499 * @param keys keys to mark as clean in the notes for next backup
500 */
501 private void restoreIcon(Key key, byte[] buffer, int dataSize, ArrayList<Key> keys) {
502 Log.v(TAG, "unpacking icon " + key.id);
503 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
504 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
505 try {
506 Resource res = unpackIcon(buffer, 0, dataSize);
507 if (DEBUG) Log.d(TAG, "unpacked " + res.dpi);
508 if (DEBUG) Log.d(TAG, "read " +
509 Base64.encodeToString(res.data, 0, res.data.length,
510 Base64.NO_WRAP));
511 Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length);
512 if (icon == null) {
513 Log.w(TAG, "failed to unpack icon for " + key.name);
514 }
515 } catch (InvalidProtocolBufferNanoException e) {
516 Log.w(TAG, "failed to decode proto", e);
517 }
518 }
519
Chris Wrenfd13c712013-09-27 15:45:19 -0400520 /**
521 * Write all the static widget resources we need to render placeholders
522 * for a package that is not installed.
523 *
524 * @param in notes from last backup
525 * @param data output stream for key/value pairs
526 * @param out notes about this backup
527 * @param keys keys to mark as clean in the notes for next backup
528 * @throws IOException
529 */
530 private void backupWidgets(Journal in, BackupDataOutput data, Journal out,
531 ArrayList<Key> keys) throws IOException {
532 // persist static widget info that hasn't been persisted yet
Chris Wrend8fe6de2013-10-04 10:42:14 -0400533 final LauncherAppState appState = LauncherAppState.getInstanceNoCreate();
534 if (appState == null) {
Chris Wren92aa4232013-10-04 11:29:36 -0400535 dataChanged(); // try again later
Chris Wrend8fe6de2013-10-04 10:42:14 -0400536 if (DEBUG) Log.d(TAG, "Launcher is not initialized, delaying widget backup");
537 return;
538 }
Chris Wren92aa4232013-10-04 11:29:36 -0400539 final ContentResolver cr = mContext.getContentResolver();
540 final WidgetPreviewLoader previewLoader = new WidgetPreviewLoader(mContext);
541 final PagedViewCellLayout widgetSpacingLayout = new PagedViewCellLayout(mContext);
Chris Wrenfd13c712013-09-27 15:45:19 -0400542 final IconCache iconCache = appState.getIconCache();
Chris Wren92aa4232013-10-04 11:29:36 -0400543 final int dpi = mContext.getResources().getDisplayMetrics().densityDpi;
Chris Wrenfd13c712013-09-27 15:45:19 -0400544 final DeviceProfile profile = appState.getDynamicGrid().getDeviceProfile();
545 if (DEBUG) Log.d(TAG, "cellWidthPx: " + profile.cellWidthPx);
546
547 // read the old ID set
548 Set<String> savedIds = getSavedIdsByType(Key.WIDGET, in);
549 if (DEBUG) Log.d(TAG, "widgets savedIds.size()=" + savedIds.size());
550
551 int startRows = out.rows;
552 if (DEBUG) Log.d(TAG, "starting here: " + startRows);
553 String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET;
554 Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION,
555 where, null, null);
556 Set<String> currentIds = new HashSet<String>(cursor.getCount());
557 try {
558 cursor.moveToPosition(-1);
559 while(cursor.moveToNext()) {
560 final long id = cursor.getLong(ID_INDEX);
561 final String providerName = cursor.getString(APPWIDGET_PROVIDER_INDEX);
562 final int spanX = cursor.getInt(SPANX_INDEX);
563 final int spanY = cursor.getInt(SPANY_INDEX);
564 final ComponentName provider = ComponentName.unflattenFromString(providerName);
565 Key key = null;
566 String backupKey = null;
567 if (provider != null) {
568 key = getKey(Key.WIDGET, providerName);
569 backupKey = keyToBackupKey(key);
570 currentIds.add(backupKey);
571 } else {
572 Log.w(TAG, "empty intent on appwidget: " + id);
573 }
574 if (savedIds.contains(backupKey)) {
575 if (DEBUG) Log.d(TAG, "already saved widget " + backupKey);
576
577 // remember that we already backed this up previously
578 keys.add(key);
579 } else if (backupKey != null) {
580 if (DEBUG) Log.d(TAG, "I can count this high: " + out.rows);
581 if ((out.rows - startRows) < MAX_WIDGETS_PER_PASS) {
582 if (DEBUG) Log.d(TAG, "saving widget " + backupKey);
583 previewLoader.setPreviewSize(spanX * profile.cellWidthPx,
584 spanY * profile.cellHeightPx, widgetSpacingLayout);
585 byte[] blob = packWidget(dpi, previewLoader, iconCache, provider);
Chris Wrenb1fd63b2013-10-03 15:43:58 -0400586 keys.add(key);
Chris Wrenfd13c712013-09-27 15:45:19 -0400587 writeRowToBackup(key, blob, out, data);
588
589 } else {
590 if (DEBUG) Log.d(TAG, "scheduling another run for widget " + backupKey);
591 // too many widgets for this pass, request another.
Chris Wren92aa4232013-10-04 11:29:36 -0400592 dataChanged();
Chris Wrenfd13c712013-09-27 15:45:19 -0400593 }
594 }
595 }
596 } finally {
597 cursor.close();
598 }
599 if (DEBUG) Log.d(TAG, "widget currentIds.size()=" + currentIds.size());
600
601 // these IDs must have been deleted
602 savedIds.removeAll(currentIds);
603 out.rows += removeDeletedKeysFromBackup(savedIds, data);
604 }
605
606 /**
607 * Read a widget from the stream.
608 *
609 * <P>Keys arrive in any order, so widgets that use this data may already exist.
610 *
611 * @param key identifier for the row
612 * @param buffer the serialized proto from the stream, may be larger than dataSize
613 * @param dataSize the size of the proto from the stream
614 * @param keys keys to mark as clean in the notes for next backup
615 */
616 private void restoreWidget(Key key, byte[] buffer, int dataSize, ArrayList<Key> keys) {
617 Log.v(TAG, "unpacking widget " + key.id);
618 if (DEBUG) Log.d(TAG, "read (" + buffer.length + "): " +
619 Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
620 try {
621 Widget widget = unpackWidget(buffer, 0, dataSize);
622 if (DEBUG) Log.d(TAG, "unpacked " + widget.provider);
623 if (widget.icon.data != null) {
624 Bitmap icon = BitmapFactory
625 .decodeByteArray(widget.icon.data, 0, widget.icon.data.length);
626 if (icon == null) {
627 Log.w(TAG, "failed to unpack widget icon for " + key.name);
628 }
629 }
630 } catch (InvalidProtocolBufferNanoException e) {
631 Log.w(TAG, "failed to decode proto", e);
632 }
633 }
634
Chris Wren22e130d2013-09-23 18:25:57 -0400635 /** create a new key, with an integer ID.
Chris Wren1ada10d2013-09-13 18:01:38 -0400636 *
637 * <P> Keys contain their own checksum instead of using
638 * the heavy-weight CheckedMessage wrapper.
639 */
640 private Key getKey(int type, long id) {
641 Key key = new Key();
642 key.type = type;
643 key.id = id;
644 key.checksum = checkKey(key);
645 return key;
646 }
647
Chris Wren22e130d2013-09-23 18:25:57 -0400648 /** create a new key for a named object.
649 *
650 * <P> Keys contain their own checksum instead of using
651 * the heavy-weight CheckedMessage wrapper.
652 */
653 private Key getKey(int type, String name) {
654 Key key = new Key();
655 key.type = type;
656 key.name = name;
657 key.checksum = checkKey(key);
658 return key;
659 }
660
Chris Wren1ada10d2013-09-13 18:01:38 -0400661 /** keys need to be strings, serialize and encode. */
662 private String keyToBackupKey(Key key) {
Chris Wren978194c2013-10-03 17:47:22 -0400663 return Base64.encodeToString(Key.toByteArray(key), Base64.NO_WRAP);
Chris Wren1ada10d2013-09-13 18:01:38 -0400664 }
665
666 /** keys need to be strings, decode and parse. */
667 private Key backupKeyToKey(String backupKey) throws KeyParsingException {
668 try {
669 Key key = Key.parseFrom(Base64.decode(backupKey, Base64.DEFAULT));
670 if (key.checksum != checkKey(key)) {
671 key = null;
672 throw new KeyParsingException("invalid key read from stream" + backupKey);
673 }
674 return key;
675 } catch (InvalidProtocolBufferNanoException e) {
676 throw new KeyParsingException(e);
677 } catch (IllegalArgumentException e) {
678 throw new KeyParsingException(e);
679 }
680 }
681
Chris Wren22e130d2013-09-23 18:25:57 -0400682 private String getKeyName(Key key) {
683 if (TextUtils.isEmpty(key.name)) {
684 return Long.toString(key.id);
685 } else {
686 return key.name;
687 }
688
689 }
690
691 private String geKeyType(Key key) {
692 switch (key.type) {
693 case Key.FAVORITE:
694 return "favorite";
695 case Key.SCREEN:
696 return "screen";
697 case Key.ICON:
698 return "icon";
Chris Wrenfd13c712013-09-27 15:45:19 -0400699 case Key.WIDGET:
700 return "widget";
Chris Wren22e130d2013-09-23 18:25:57 -0400701 default:
702 return "anonymous";
703 }
704 }
705
Chris Wren1ada10d2013-09-13 18:01:38 -0400706 /** Compute the checksum over the important bits of a key. */
707 private long checkKey(Key key) {
708 CRC32 checksum = new CRC32();
709 checksum.update(key.type);
710 checksum.update((int) (key.id & 0xffff));
711 checksum.update((int) ((key.id >> 32) & 0xffff));
712 if (!TextUtils.isEmpty(key.name)) {
713 checksum.update(key.name.getBytes());
714 }
715 return checksum.getValue();
716 }
717
718 /** Serialize a Favorite for persistence, including a checksum wrapper. */
719 private byte[] packFavorite(Cursor c) {
720 Favorite favorite = new Favorite();
721 favorite.id = c.getLong(ID_INDEX);
722 favorite.screen = c.getInt(SCREEN_INDEX);
723 favorite.container = c.getInt(CONTAINER_INDEX);
724 favorite.cellX = c.getInt(CELLX_INDEX);
725 favorite.cellY = c.getInt(CELLY_INDEX);
726 favorite.spanX = c.getInt(SPANX_INDEX);
727 favorite.spanY = c.getInt(SPANY_INDEX);
728 favorite.iconType = c.getInt(ICON_TYPE_INDEX);
729 if (favorite.iconType == Favorites.ICON_TYPE_RESOURCE) {
730 String iconPackage = c.getString(ICON_PACKAGE_INDEX);
731 if (!TextUtils.isEmpty(iconPackage)) {
732 favorite.iconPackage = iconPackage;
733 }
734 String iconResource = c.getString(ICON_RESOURCE_INDEX);
735 if (!TextUtils.isEmpty(iconResource)) {
736 favorite.iconResource = iconResource;
737 }
738 }
739 if (favorite.iconType == Favorites.ICON_TYPE_BITMAP) {
740 byte[] blob = c.getBlob(ICON_INDEX);
741 if (blob != null && blob.length > 0) {
742 favorite.icon = blob;
743 }
744 }
745 String title = c.getString(TITLE_INDEX);
746 if (!TextUtils.isEmpty(title)) {
747 favorite.title = title;
748 }
749 String intent = c.getString(INTENT_INDEX);
750 if (!TextUtils.isEmpty(intent)) {
751 favorite.intent = intent;
752 }
753 favorite.itemType = c.getInt(ITEM_TYPE_INDEX);
754 if (favorite.itemType == Favorites.ITEM_TYPE_APPWIDGET) {
755 favorite.appWidgetId = c.getInt(APPWIDGET_ID_INDEX);
756 String appWidgetProvider = c.getString(APPWIDGET_PROVIDER_INDEX);
757 if (!TextUtils.isEmpty(appWidgetProvider)) {
758 favorite.appWidgetProvider = appWidgetProvider;
759 }
760 }
761
762 return writeCheckedBytes(favorite);
763 }
764
765 /** Deserialize a Favorite from persistence, after verifying checksum wrapper. */
766 private Favorite unpackFavorite(byte[] buffer, int offset, int dataSize)
767 throws InvalidProtocolBufferNanoException {
768 Favorite favorite = new Favorite();
769 MessageNano.mergeFrom(favorite, readCheckedBytes(buffer, offset, dataSize));
770 return favorite;
771 }
772
773 /** Serialize a Screen for persistence, including a checksum wrapper. */
774 private byte[] packScreen(Cursor c) {
775 Screen screen = new Screen();
776 screen.id = c.getLong(ID_INDEX);
777 screen.rank = c.getInt(SCREEN_RANK_INDEX);
778
779 return writeCheckedBytes(screen);
780 }
781
782 /** Deserialize a Screen from persistence, after verifying checksum wrapper. */
783 private Screen unpackScreen(byte[] buffer, int offset, int dataSize)
784 throws InvalidProtocolBufferNanoException {
785 Screen screen = new Screen();
786 MessageNano.mergeFrom(screen, readCheckedBytes(buffer, offset, dataSize));
787 return screen;
788 }
789
Chris Wren22e130d2013-09-23 18:25:57 -0400790 /** Serialize an icon Resource for persistence, including a checksum wrapper. */
791 private byte[] packIcon(int dpi, Bitmap icon) {
792 Resource res = new Resource();
793 res.dpi = dpi;
794 ByteArrayOutputStream os = new ByteArrayOutputStream();
Chris Wrenb86f0762013-10-04 10:10:21 -0400795 if (icon.compress(IMAGE_FORMAT, IMAGE_COMPRESSION_QUALITY, os)) {
Chris Wren22e130d2013-09-23 18:25:57 -0400796 res.data = os.toByteArray();
797 }
798 return writeCheckedBytes(res);
799 }
800
801 /** Deserialize an icon resource from persistence, after verifying checksum wrapper. */
802 private Resource unpackIcon(byte[] buffer, int offset, int dataSize)
803 throws InvalidProtocolBufferNanoException {
804 Resource res = new Resource();
805 MessageNano.mergeFrom(res, readCheckedBytes(buffer, offset, dataSize));
806 return res;
807 }
808
Chris Wrenfd13c712013-09-27 15:45:19 -0400809 /** Serialize a widget for persistence, including a checksum wrapper. */
810 private byte[] packWidget(int dpi, WidgetPreviewLoader previewLoader, IconCache iconCache,
811 ComponentName provider) {
812 final AppWidgetProviderInfo info = findAppWidgetProviderInfo(provider);
813 Widget widget = new Widget();
814 widget.provider = provider.flattenToShortString();
815 widget.label = info.label;
816 widget.configure = info.configure != null;
817 if (info.icon != 0) {
818 widget.icon = new Resource();
819 Drawable fullResIcon = iconCache.getFullResIcon(provider.getPackageName(), info.icon);
Chris Wren92aa4232013-10-04 11:29:36 -0400820 Bitmap icon = Utilities.createIconBitmap(fullResIcon, mContext);
Chris Wrenfd13c712013-09-27 15:45:19 -0400821 ByteArrayOutputStream os = new ByteArrayOutputStream();
Chris Wrenb86f0762013-10-04 10:10:21 -0400822 if (icon.compress(IMAGE_FORMAT, IMAGE_COMPRESSION_QUALITY, os)) {
Chris Wrenfd13c712013-09-27 15:45:19 -0400823 widget.icon.data = os.toByteArray();
824 widget.icon.dpi = dpi;
825 }
826 }
827 if (info.previewImage != 0) {
828 widget.preview = new Resource();
829 Bitmap preview = previewLoader.generateWidgetPreview(info, null);
830 ByteArrayOutputStream os = new ByteArrayOutputStream();
Chris Wrenb86f0762013-10-04 10:10:21 -0400831 if (preview.compress(IMAGE_FORMAT, IMAGE_COMPRESSION_QUALITY, os)) {
Chris Wrenfd13c712013-09-27 15:45:19 -0400832 widget.preview.data = os.toByteArray();
833 widget.preview.dpi = dpi;
834 }
835 }
836 return writeCheckedBytes(widget);
837 }
838
839 /** Deserialize a widget from persistence, after verifying checksum wrapper. */
840 private Widget unpackWidget(byte[] buffer, int offset, int dataSize)
841 throws InvalidProtocolBufferNanoException {
842 Widget widget = new Widget();
843 MessageNano.mergeFrom(widget, readCheckedBytes(buffer, offset, dataSize));
844 return widget;
845 }
846
Chris Wren1ada10d2013-09-13 18:01:38 -0400847 /**
848 * Read the old journal from the input file.
849 *
850 * In the event of any error, just pretend we didn't have a journal,
851 * in that case, do a full backup.
852 *
853 * @param oldState the read-0only file descriptor pointing to the old journal
854 * @return a Journal protocol bugffer
855 */
856 private Journal readJournal(ParcelFileDescriptor oldState) {
Chris Wren1ada10d2013-09-13 18:01:38 -0400857 Journal journal = new Journal();
Chris Wren92aa4232013-10-04 11:29:36 -0400858 if (oldState == null) {
859 return journal;
860 }
861 FileInputStream inStream = new FileInputStream(oldState.getFileDescriptor());
862 try {
863 int remaining = inStream.available();
864 if (DEBUG) Log.d(TAG, "available " + remaining);
865 if (remaining < MAX_JOURNAL_SIZE) {
866 byte[] buffer = new byte[remaining];
Chris Wren1ada10d2013-09-13 18:01:38 -0400867 int bytesRead = 0;
Chris Wren92aa4232013-10-04 11:29:36 -0400868 while (remaining > 0) {
869 try {
870 int result = inStream.read(buffer, bytesRead, remaining);
871 if (result > 0) {
872 if (DEBUG) Log.d(TAG, "read some bytes: " + result);
873 remaining -= result;
874 bytesRead += result;
875 } else {
876 // stop reading ands see what there is to parse
877 Log.w(TAG, "read error: " + result);
878 remaining = 0;
879 }
880 } catch (IOException e) {
881 Log.w(TAG, "failed to read the journal", e);
882 buffer = null;
883 remaining = 0;
884 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400885 }
Chris Wren92aa4232013-10-04 11:29:36 -0400886 if (DEBUG) Log.d(TAG, "journal bytes read: " + bytesRead);
887
888 if (buffer != null) {
889 try {
890 MessageNano.mergeFrom(journal, readCheckedBytes(buffer, 0, bytesRead));
891 } catch (InvalidProtocolBufferNanoException e) {
892 Log.d(TAG, "failed to read the journal", e);
893 journal.clear();
894 }
Chris Wren1ada10d2013-09-13 18:01:38 -0400895 }
896 }
Chris Wren92aa4232013-10-04 11:29:36 -0400897 } catch (IOException e) {
898 Log.d(TAG, "failed to close the journal", e);
899 } finally {
Chris Wren1ada10d2013-09-13 18:01:38 -0400900 try {
901 inStream.close();
902 } catch (IOException e) {
903 Log.d(TAG, "failed to close the journal", e);
904 }
905 }
906 return journal;
907 }
908
Chris Wren22e130d2013-09-23 18:25:57 -0400909 private void writeRowToBackup(Key key, byte[] blob, Journal out,
910 BackupDataOutput data) throws IOException {
911 String backupKey = keyToBackupKey(key);
912 data.writeEntityHeader(backupKey, blob.length);
913 data.writeEntityData(blob, blob.length);
914 out.rows++;
915 out.bytes += blob.length;
916 Log.v(TAG, "saving " + geKeyType(key) + " " + backupKey + ": " +
917 getKeyName(key) + "/" + blob.length);
Chris Wren92aa4232013-10-04 11:29:36 -0400918 if(DEBUG_PAYLOAD) {
Chris Wren2b6c21d2013-10-02 14:16:04 -0400919 String encoded = Base64.encodeToString(blob, 0, blob.length, Base64.NO_WRAP);
920 final int chunkSize = 1024;
921 for (int offset = 0; offset < encoded.length(); offset += chunkSize) {
922 int end = offset + chunkSize;
923 end = Math.min(end, encoded.length());
924 Log.d(TAG, "wrote " + encoded.substring(offset, end));
925 }
926 }
Chris Wren22e130d2013-09-23 18:25:57 -0400927 }
928
929 private Set<String> getSavedIdsByType(int type, Journal in) {
930 Set<String> savedIds = new HashSet<String>();
931 for(int i = 0; i < in.key.length; i++) {
932 Key key = in.key[i];
933 if (key.type == type) {
934 savedIds.add(keyToBackupKey(key));
935 }
936 }
937 return savedIds;
938 }
939
940 private int removeDeletedKeysFromBackup(Set<String> deletedIds, BackupDataOutput data)
941 throws IOException {
942 int rows = 0;
943 for(String deleted: deletedIds) {
944 Log.v(TAG, "dropping icon " + deleted);
945 data.writeEntityHeader(deleted, -1);
946 rows++;
947 }
948 return rows;
949 }
950
Chris Wren1ada10d2013-09-13 18:01:38 -0400951 /**
952 * Write the new journal to the output file.
953 *
954 * In the event of any error, just pretend we didn't have a journal,
955 * in that case, do a full backup.
956
957 * @param newState the write-only file descriptor pointing to the new journal
958 * @param journal a Journal protocol buffer
959 */
960 private void writeJournal(ParcelFileDescriptor newState, Journal journal) {
961 FileOutputStream outStream = null;
962 try {
963 outStream = new FileOutputStream(newState.getFileDescriptor());
964 outStream.write(writeCheckedBytes(journal));
965 outStream.close();
966 } catch (IOException e) {
967 Log.d(TAG, "failed to write backup journal", e);
968 }
969 }
970
971 /** Wrap a proto in a CheckedMessage and compute the checksum. */
972 private byte[] writeCheckedBytes(MessageNano proto) {
973 CheckedMessage wrapper = new CheckedMessage();
974 wrapper.payload = MessageNano.toByteArray(proto);
975 CRC32 checksum = new CRC32();
976 checksum.update(wrapper.payload);
977 wrapper.checksum = checksum.getValue();
978 return MessageNano.toByteArray(wrapper);
979 }
980
981 /** Unwrap a proto message from a CheckedMessage, verifying the checksum. */
982 private byte[] readCheckedBytes(byte[] buffer, int offset, int dataSize)
983 throws InvalidProtocolBufferNanoException {
984 CheckedMessage wrapper = new CheckedMessage();
985 MessageNano.mergeFrom(wrapper, buffer, offset, dataSize);
986 CRC32 checksum = new CRC32();
987 checksum.update(wrapper.payload);
988 if (wrapper.checksum != checksum.getValue()) {
989 throw new InvalidProtocolBufferNanoException("checksum does not match");
990 }
991 return wrapper.payload;
992 }
993
Chris Wrenfd13c712013-09-27 15:45:19 -0400994 private AppWidgetProviderInfo findAppWidgetProviderInfo(ComponentName component) {
995 if (mWidgetMap == null) {
996 List<AppWidgetProviderInfo> widgets =
Chris Wren92aa4232013-10-04 11:29:36 -0400997 AppWidgetManager.getInstance(mContext).getInstalledProviders();
Chris Wrenfd13c712013-09-27 15:45:19 -0400998 mWidgetMap = new HashMap<ComponentName, AppWidgetProviderInfo>(widgets.size());
999 for (AppWidgetProviderInfo info : widgets) {
1000 mWidgetMap.put(info.provider, info);
1001 }
1002 }
1003 return mWidgetMap.get(component);
1004 }
1005
Chris Wren1ada10d2013-09-13 18:01:38 -04001006 private class KeyParsingException extends Throwable {
1007 private KeyParsingException(Throwable cause) {
1008 super(cause);
1009 }
1010
1011 public KeyParsingException(String reason) {
1012 super(reason);
1013 }
1014 }
1015}