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