Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.app.backup.BackupAgentHelper; |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 20 | import android.app.backup.BackupDataInput; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 21 | import android.app.backup.BackupManager; |
| 22 | import android.content.Context; |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
| 24 | import android.os.ParcelFileDescriptor; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 25 | import android.util.Log; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 26 | |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 27 | import com.android.launcher3.model.MigrateFromRestoreTask; |
| 28 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 29 | import java.io.IOException; |
| 30 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 31 | public class LauncherBackupAgentHelper extends BackupAgentHelper { |
| 32 | |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 33 | private static final String TAG = "LauncherBackupAgentHelper"; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 34 | |
| 35 | private static final String LAUNCHER_DATA_PREFIX = "L"; |
| 36 | |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 37 | static final boolean VERBOSE = false; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 38 | static final boolean DEBUG = false; |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 39 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 40 | private static BackupManager sBackupManager; |
| 41 | |
| 42 | /** |
| 43 | * Notify the backup manager that out database is dirty. |
| 44 | * |
| 45 | * <P>This does not force an immediate backup. |
| 46 | * |
| 47 | * @param context application context |
| 48 | */ |
| 49 | public static void dataChanged(Context context) { |
| 50 | if (sBackupManager == null) { |
| 51 | sBackupManager = new BackupManager(context); |
| 52 | } |
| 53 | sBackupManager.dataChanged(); |
| 54 | } |
| 55 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 56 | private LauncherBackupHelper mHelper; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 57 | |
| 58 | @Override |
| 59 | public void onCreate() { |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 60 | super.onCreate(); |
| 61 | mHelper = new LauncherBackupHelper(this); |
| 62 | addHelper(LAUNCHER_DATA_PREFIX, mHelper); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 63 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 64 | |
| 65 | @Override |
| 66 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 67 | throws IOException { |
Sunny Goyal | d8cec09 | 2014-10-23 13:57:41 -0700 | [diff] [blame] | 68 | if (!Utilities.isLmpOrAbove()) { |
| 69 | // No restore for old devices. |
| 70 | Log.i(TAG, "You shall not pass!!!"); |
| 71 | Log.d(TAG, "Restore is only supported on devices running Lollipop and above."); |
| 72 | return; |
| 73 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 74 | |
Sunny Goyal | cf30da5 | 2014-11-18 10:38:22 -0800 | [diff] [blame] | 75 | // Clear dB before restore |
| 76 | LauncherAppState.getLauncherProvider().createEmptyDB(); |
| 77 | |
| 78 | boolean hasData; |
| 79 | try { |
| 80 | super.onRestore(data, appVersionCode, newState); |
| 81 | // If no favorite was migrated, clear the data and start fresh. |
| 82 | final Cursor c = getContentResolver().query( |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 83 | LauncherSettings.Favorites.CONTENT_URI, null, null, null, null); |
Sunny Goyal | cf30da5 | 2014-11-18 10:38:22 -0800 | [diff] [blame] | 84 | hasData = c.moveToNext(); |
| 85 | c.close(); |
| 86 | } catch (Exception e) { |
| 87 | // If the restore fails, we should do a fresh start. |
| 88 | Log.e(TAG, "Restore failed", e); |
| 89 | hasData = false; |
| 90 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 91 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 92 | if (hasData && mHelper.restoreSuccessful) { |
| 93 | LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated(); |
| 94 | LauncherClings.synchonouslyMarkFirstRunClingDismissed(this); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 95 | |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 96 | // Rank was added in v4. |
Sunny Goyal | 3a30cfe | 2015-07-16 17:27:43 -0700 | [diff] [blame] | 97 | if (mHelper.restoredBackupVersion <= 3) { |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 98 | LauncherAppState.getLauncherProvider().updateFolderItemsRank(); |
| 99 | } |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 100 | |
Sunny Goyal | 6579e1e | 2015-08-11 12:10:34 -0700 | [diff] [blame] | 101 | if (MigrateFromRestoreTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) { |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 102 | MigrateFromRestoreTask.markForMigration(getApplicationContext(), |
| 103 | (int) mHelper.migrationCompatibleProfileData.desktopCols, |
| 104 | (int) mHelper.migrationCompatibleProfileData.desktopRows, |
| 105 | mHelper.widgetSizes); |
| 106 | } |
| 107 | |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 108 | LauncherAppState.getLauncherProvider().convertShortcutsToLauncherActivities(); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 109 | } else { |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 110 | if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB"); |
| 111 | LauncherAppState.getLauncherProvider().createEmptyDB(); |
| 112 | } |
| 113 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 114 | } |