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 | b171e56 | 2016-01-21 16:41:54 -0800 | [diff] [blame] | 23 | import android.content.SharedPreferences; |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 24 | import android.database.Cursor; |
| 25 | import android.os.ParcelFileDescriptor; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 26 | import android.util.Log; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 27 | |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 28 | import com.android.launcher3.model.GridSizeMigrationTask; |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 29 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 32 | public class LauncherBackupAgentHelper extends BackupAgentHelper { |
| 33 | |
Sunny Goyal | d804398 | 2015-12-17 22:23:02 -0800 | [diff] [blame] | 34 | private static final String TAG = "LauncherBAHelper"; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 35 | |
Sunny Goyal | b171e56 | 2016-01-21 16:41:54 -0800 | [diff] [blame] | 36 | private static final String KEY_LAST_NOTIFIED_TIME = "backup_manager_last_notified"; |
| 37 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 38 | private static final String LAUNCHER_DATA_PREFIX = "L"; |
| 39 | |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 40 | static final boolean VERBOSE = false; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 41 | static final boolean DEBUG = false; |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 42 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 43 | /** |
| 44 | * Notify the backup manager that out database is dirty. |
| 45 | * |
| 46 | * <P>This does not force an immediate backup. |
| 47 | * |
| 48 | * @param context application context |
| 49 | */ |
| 50 | public static void dataChanged(Context context) { |
Sunny Goyal | b171e56 | 2016-01-21 16:41:54 -0800 | [diff] [blame] | 51 | dataChanged(context, 0); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Notify the backup manager that out database is dirty. |
| 56 | * |
| 57 | * <P>This does not force an immediate backup. |
| 58 | * |
| 59 | * @param context application context |
| 60 | * @param throttleMs duration in ms for which two consecutive calls to backup manager should |
| 61 | * not be made. |
| 62 | */ |
| 63 | public static void dataChanged(Context context, long throttleMs) { |
| 64 | SharedPreferences prefs = Utilities.getPrefs(context); |
| 65 | long now = System.currentTimeMillis(); |
| 66 | long lastTime = prefs.getLong(KEY_LAST_NOTIFIED_TIME, 0); |
| 67 | |
| 68 | // User can manually change the system time, which could lead to now < lastTime. |
| 69 | // Re-backup in that case, as the backup will have a wrong lastModifiedTime. |
| 70 | if (now < lastTime || now >= (lastTime + throttleMs)) { |
| 71 | BackupManager.dataChanged(context.getPackageName()); |
| 72 | prefs.edit().putLong(KEY_LAST_NOTIFIED_TIME, now).apply(); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 73 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 76 | private LauncherBackupHelper mHelper; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public void onCreate() { |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 80 | super.onCreate(); |
| 81 | mHelper = new LauncherBackupHelper(this); |
| 82 | addHelper(LAUNCHER_DATA_PREFIX, mHelper); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 83 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 84 | |
| 85 | @Override |
| 86 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 87 | throws IOException { |
Sunny Goyal | 9fc953b | 2015-08-17 12:24:25 -0700 | [diff] [blame] | 88 | if (!Utilities.ATLEAST_LOLLIPOP) { |
Sunny Goyal | d8cec09 | 2014-10-23 13:57:41 -0700 | [diff] [blame] | 89 | // No restore for old devices. |
| 90 | Log.i(TAG, "You shall not pass!!!"); |
| 91 | Log.d(TAG, "Restore is only supported on devices running Lollipop and above."); |
| 92 | return; |
| 93 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 94 | |
Sunny Goyal | cf30da5 | 2014-11-18 10:38:22 -0800 | [diff] [blame] | 95 | // Clear dB before restore |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 96 | LauncherSettings.Settings.call(getContentResolver(), |
| 97 | LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB); |
Sunny Goyal | cf30da5 | 2014-11-18 10:38:22 -0800 | [diff] [blame] | 98 | |
| 99 | boolean hasData; |
| 100 | try { |
| 101 | super.onRestore(data, appVersionCode, newState); |
| 102 | // If no favorite was migrated, clear the data and start fresh. |
| 103 | final Cursor c = getContentResolver().query( |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 104 | LauncherSettings.Favorites.CONTENT_URI, null, null, null, null); |
Sunny Goyal | cf30da5 | 2014-11-18 10:38:22 -0800 | [diff] [blame] | 105 | hasData = c.moveToNext(); |
| 106 | c.close(); |
| 107 | } catch (Exception e) { |
| 108 | // If the restore fails, we should do a fresh start. |
| 109 | Log.e(TAG, "Restore failed", e); |
| 110 | hasData = false; |
| 111 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 112 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 113 | if (hasData && mHelper.restoreSuccessful) { |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 114 | LauncherSettings.Settings.call(getContentResolver(), |
| 115 | LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG); |
Sunny Goyal | d804398 | 2015-12-17 22:23:02 -0800 | [diff] [blame] | 116 | LauncherClings.markFirstRunClingDismissed(this); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 117 | |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 118 | // Rank was added in v4. |
Sunny Goyal | 3a30cfe | 2015-07-16 17:27:43 -0700 | [diff] [blame] | 119 | if (mHelper.restoredBackupVersion <= 3) { |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 120 | LauncherSettings.Settings.call(getContentResolver(), |
| 121 | LauncherSettings.Settings.METHOD_UPDATE_FOLDER_ITEMS_RANK); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 122 | } |
Sunny Goyal | 107ea63 | 2015-07-20 12:59:39 -0700 | [diff] [blame] | 123 | |
Sunny Goyal | f862a26 | 2015-12-14 14:27:38 -0800 | [diff] [blame] | 124 | if (GridSizeMigrationTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) { |
| 125 | GridSizeMigrationTask.markForMigration(getApplicationContext(), |
Sunny Goyal | f076eae | 2016-01-11 12:25:10 -0800 | [diff] [blame] | 126 | mHelper.widgetSizes, mHelper.migrationCompatibleProfileData); |
Sunny Goyal | e5bb705 | 2015-07-27 14:36:07 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 129 | LauncherSettings.Settings.call(getContentResolver(), |
| 130 | LauncherSettings.Settings.METHOD_CONVERT_SHORTCUTS_TO_ACTIVITIES); |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 131 | } else { |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 132 | if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB"); |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 133 | LauncherSettings.Settings.call(getContentResolver(), |
| 134 | LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB); |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 137 | } |