blob: c2ab20a6292db1f5f5d8d3c8d554253eaec1aae2 [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 Goyalf862a262015-12-14 14:27:38 -080028import com.android.launcher3.model.GridSizeMigrationTask;
Sunny Goyale5bb7052015-07-27 14:36:07 -070029
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
Sunny Goyald2497482015-09-22 18:24:19 -070096 LauncherSettings.Settings.call(getContentResolver(),
97 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Sunny Goyalcf30da52014-11-18 10:38:22 -080098
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 Goyal1d4a2df2015-03-30 11:11:46 -0700104 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Sunny Goyalcf30da52014-11-18 10:38:22 -0800105 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 Goyal42de82f2014-09-26 22:09:29 -0700112
Sunny Goyal33d44382014-10-16 09:24:19 -0700113 if (hasData && mHelper.restoreSuccessful) {
Sunny Goyald2497482015-09-22 18:24:19 -0700114 LauncherSettings.Settings.call(getContentResolver(),
115 LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
Sunny Goyald8043982015-12-17 22:23:02 -0800116 LauncherClings.markFirstRunClingDismissed(this);
Sunny Goyal08f72612015-01-05 13:41:43 -0800117
Sunny Goyal107ea632015-07-20 12:59:39 -0700118 // Rank was added in v4.
Sunny Goyal3a30cfe2015-07-16 17:27:43 -0700119 if (mHelper.restoredBackupVersion <= 3) {
Sunny Goyald2497482015-09-22 18:24:19 -0700120 LauncherSettings.Settings.call(getContentResolver(),
121 LauncherSettings.Settings.METHOD_UPDATE_FOLDER_ITEMS_RANK);
Sunny Goyal08f72612015-01-05 13:41:43 -0800122 }
Sunny Goyal107ea632015-07-20 12:59:39 -0700123
Sunny Goyalf862a262015-12-14 14:27:38 -0800124 if (GridSizeMigrationTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) {
125 GridSizeMigrationTask.markForMigration(getApplicationContext(),
Sunny Goyalf076eae2016-01-11 12:25:10 -0800126 mHelper.widgetSizes, mHelper.migrationCompatibleProfileData);
Sunny Goyale5bb7052015-07-27 14:36:07 -0700127 }
128
Sunny Goyald2497482015-09-22 18:24:19 -0700129 LauncherSettings.Settings.call(getContentResolver(),
130 LauncherSettings.Settings.METHOD_CONVERT_SHORTCUTS_TO_ACTIVITIES);
Sunny Goyal33d44382014-10-16 09:24:19 -0700131 } else {
Sunny Goyal42de82f2014-09-26 22:09:29 -0700132 if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
Sunny Goyald2497482015-09-22 18:24:19 -0700133 LauncherSettings.Settings.call(getContentResolver(),
134 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Sunny Goyal42de82f2014-09-26 22:09:29 -0700135 }
136 }
Chris Wren92aa4232013-10-04 11:29:36 -0400137}