blob: 2177f527e2d9d5ef336c723ee4d158c509a3fe0a [file] [log] [blame]
Chris Wren92aa4232013-10-04 11:29:36 -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 android.app.backup.BackupAgentHelper;
Sunny Goyal42de82f2014-09-26 22:09:29 -070020import android.app.backup.BackupDataInput;
Chris Wren92aa4232013-10-04 11:29:36 -040021import android.app.backup.BackupManager;
22import android.content.Context;
Sunny Goyalb171e562016-01-21 16:41:54 -080023import android.content.SharedPreferences;
Sunny Goyal42de82f2014-09-26 22:09:29 -070024import android.database.Cursor;
25import android.os.ParcelFileDescriptor;
Chris Wren50c8f422014-01-15 16:10:39 -050026import android.util.Log;
Chris Wren92aa4232013-10-04 11:29:36 -040027
Sunny Goyale5bb7052015-07-27 14:36:07 -070028import com.android.launcher3.model.MigrateFromRestoreTask;
29
Sunny Goyal42de82f2014-09-26 22:09:29 -070030import java.io.IOException;
31
Chris Wren92aa4232013-10-04 11:29:36 -040032public class LauncherBackupAgentHelper extends BackupAgentHelper {
33
Sunny Goyald8043982015-12-17 22:23:02 -080034 private static final String TAG = "LauncherBAHelper";
Sunny Goyal33d44382014-10-16 09:24:19 -070035
Sunny Goyalb171e562016-01-21 16:41:54 -080036 private static final String KEY_LAST_NOTIFIED_TIME = "backup_manager_last_notified";
37
Sunny Goyal33d44382014-10-16 09:24:19 -070038 private static final String LAUNCHER_DATA_PREFIX = "L";
39
Sunny Goyalc0ee6752015-01-16 14:10:32 -080040 static final boolean VERBOSE = false;
Chris Wren50c8f422014-01-15 16:10:39 -050041 static final boolean DEBUG = false;
Chris Wren45297f82013-10-17 15:16:48 -040042
Chris Wren92aa4232013-10-04 11:29:36 -040043 /**
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 Goyalb171e562016-01-21 16:41:54 -080051 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 Wren92aa4232013-10-04 11:29:36 -040073 }
Chris Wren92aa4232013-10-04 11:29:36 -040074 }
75
Sunny Goyal33d44382014-10-16 09:24:19 -070076 private LauncherBackupHelper mHelper;
Chris Wren92aa4232013-10-04 11:29:36 -040077
78 @Override
79 public void onCreate() {
Sunny Goyal33d44382014-10-16 09:24:19 -070080 super.onCreate();
81 mHelper = new LauncherBackupHelper(this);
82 addHelper(LAUNCHER_DATA_PREFIX, mHelper);
Chris Wren92aa4232013-10-04 11:29:36 -040083 }
Sunny Goyal42de82f2014-09-26 22:09:29 -070084
85 @Override
86 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
87 throws IOException {
Sunny Goyal9fc953b2015-08-17 12:24:25 -070088 if (!Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald8cec092014-10-23 13:57:41 -070089 // 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 Goyal42de82f2014-09-26 22:09:29 -070094
Sunny Goyalcf30da52014-11-18 10:38:22 -080095 // Clear dB before restore
96 LauncherAppState.getLauncherProvider().createEmptyDB();
97
98 boolean hasData;
99 try {
100 super.onRestore(data, appVersionCode, newState);
101 // If no favorite was migrated, clear the data and start fresh.
102 final Cursor c = getContentResolver().query(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700103 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Sunny Goyalcf30da52014-11-18 10:38:22 -0800104 hasData = c.moveToNext();
105 c.close();
106 } catch (Exception e) {
107 // If the restore fails, we should do a fresh start.
108 Log.e(TAG, "Restore failed", e);
109 hasData = false;
110 }
Sunny Goyal42de82f2014-09-26 22:09:29 -0700111
Sunny Goyal33d44382014-10-16 09:24:19 -0700112 if (hasData && mHelper.restoreSuccessful) {
113 LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
Sunny Goyald8043982015-12-17 22:23:02 -0800114 LauncherClings.markFirstRunClingDismissed(this);
Sunny Goyal08f72612015-01-05 13:41:43 -0800115
Sunny Goyal107ea632015-07-20 12:59:39 -0700116 // Rank was added in v4.
Sunny Goyal3a30cfe2015-07-16 17:27:43 -0700117 if (mHelper.restoredBackupVersion <= 3) {
Sunny Goyal08f72612015-01-05 13:41:43 -0800118 LauncherAppState.getLauncherProvider().updateFolderItemsRank();
119 }
Sunny Goyal107ea632015-07-20 12:59:39 -0700120
Sunny Goyal6579e1e2015-08-11 12:10:34 -0700121 if (MigrateFromRestoreTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) {
Sunny Goyale5bb7052015-07-27 14:36:07 -0700122 MigrateFromRestoreTask.markForMigration(getApplicationContext(),
123 (int) mHelper.migrationCompatibleProfileData.desktopCols,
124 (int) mHelper.migrationCompatibleProfileData.desktopRows,
125 mHelper.widgetSizes);
126 }
127
Sunny Goyal107ea632015-07-20 12:59:39 -0700128 LauncherAppState.getLauncherProvider().convertShortcutsToLauncherActivities();
Sunny Goyal33d44382014-10-16 09:24:19 -0700129 } else {
Sunny Goyal42de82f2014-09-26 22:09:29 -0700130 if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
131 LauncherAppState.getLauncherProvider().createEmptyDB();
132 }
133 }
Chris Wren92aa4232013-10-04 11:29:36 -0400134}