blob: 2a5f1974e3ce12d42991cb00f9d0c3fe9c83171b [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 Goyal42de82f2014-09-26 22:09:29 -070023import android.database.Cursor;
24import android.os.ParcelFileDescriptor;
Chris Wren50c8f422014-01-15 16:10:39 -050025import android.util.Log;
Chris Wren92aa4232013-10-04 11:29:36 -040026
Sunny Goyale5bb7052015-07-27 14:36:07 -070027import com.android.launcher3.model.MigrateFromRestoreTask;
28
Sunny Goyal42de82f2014-09-26 22:09:29 -070029import java.io.IOException;
30
Chris Wren92aa4232013-10-04 11:29:36 -040031public class LauncherBackupAgentHelper extends BackupAgentHelper {
32
Chris Wren45297f82013-10-17 15:16:48 -040033 private static final String TAG = "LauncherBackupAgentHelper";
Sunny Goyal33d44382014-10-16 09:24:19 -070034
35 private static final String LAUNCHER_DATA_PREFIX = "L";
36
Sunny Goyalc0ee6752015-01-16 14:10:32 -080037 static final boolean VERBOSE = false;
Chris Wren50c8f422014-01-15 16:10:39 -050038 static final boolean DEBUG = false;
Chris Wren45297f82013-10-17 15:16:48 -040039
Chris Wren92aa4232013-10-04 11:29:36 -040040 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 Goyal33d44382014-10-16 09:24:19 -070056 private LauncherBackupHelper mHelper;
Chris Wren92aa4232013-10-04 11:29:36 -040057
58 @Override
59 public void onCreate() {
Sunny Goyal33d44382014-10-16 09:24:19 -070060 super.onCreate();
61 mHelper = new LauncherBackupHelper(this);
62 addHelper(LAUNCHER_DATA_PREFIX, mHelper);
Chris Wren92aa4232013-10-04 11:29:36 -040063 }
Sunny Goyal42de82f2014-09-26 22:09:29 -070064
65 @Override
66 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
67 throws IOException {
Sunny Goyal9fc953b2015-08-17 12:24:25 -070068 if (!Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald8cec092014-10-23 13:57:41 -070069 // 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 Goyal42de82f2014-09-26 22:09:29 -070074
Sunny Goyalcf30da52014-11-18 10:38:22 -080075 // Clear dB before restore
Sunny Goyald2497482015-09-22 18:24:19 -070076 LauncherSettings.Settings.call(getContentResolver(),
77 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Sunny Goyalcf30da52014-11-18 10:38:22 -080078
79 boolean hasData;
80 try {
81 super.onRestore(data, appVersionCode, newState);
82 // If no favorite was migrated, clear the data and start fresh.
83 final Cursor c = getContentResolver().query(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -070084 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Sunny Goyalcf30da52014-11-18 10:38:22 -080085 hasData = c.moveToNext();
86 c.close();
87 } catch (Exception e) {
88 // If the restore fails, we should do a fresh start.
89 Log.e(TAG, "Restore failed", e);
90 hasData = false;
91 }
Sunny Goyal42de82f2014-09-26 22:09:29 -070092
Sunny Goyal33d44382014-10-16 09:24:19 -070093 if (hasData && mHelper.restoreSuccessful) {
Sunny Goyald2497482015-09-22 18:24:19 -070094 LauncherSettings.Settings.call(getContentResolver(),
95 LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
Sunny Goyal33d44382014-10-16 09:24:19 -070096 LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
Sunny Goyal08f72612015-01-05 13:41:43 -080097
Sunny Goyal107ea632015-07-20 12:59:39 -070098 // Rank was added in v4.
Sunny Goyal3a30cfe2015-07-16 17:27:43 -070099 if (mHelper.restoredBackupVersion <= 3) {
Sunny Goyald2497482015-09-22 18:24:19 -0700100 LauncherSettings.Settings.call(getContentResolver(),
101 LauncherSettings.Settings.METHOD_UPDATE_FOLDER_ITEMS_RANK);
Sunny Goyal08f72612015-01-05 13:41:43 -0800102 }
Sunny Goyal107ea632015-07-20 12:59:39 -0700103
Sunny Goyal6579e1e2015-08-11 12:10:34 -0700104 if (MigrateFromRestoreTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) {
Sunny Goyale5bb7052015-07-27 14:36:07 -0700105 MigrateFromRestoreTask.markForMigration(getApplicationContext(),
106 (int) mHelper.migrationCompatibleProfileData.desktopCols,
107 (int) mHelper.migrationCompatibleProfileData.desktopRows,
108 mHelper.widgetSizes);
109 }
110
Sunny Goyald2497482015-09-22 18:24:19 -0700111 LauncherSettings.Settings.call(getContentResolver(),
112 LauncherSettings.Settings.METHOD_CONVERT_SHORTCUTS_TO_ACTIVITIES);
Sunny Goyal33d44382014-10-16 09:24:19 -0700113 } else {
Sunny Goyal42de82f2014-09-26 22:09:29 -0700114 if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
Sunny Goyald2497482015-09-22 18:24:19 -0700115 LauncherSettings.Settings.call(getContentResolver(),
116 LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
Sunny Goyal42de82f2014-09-26 22:09:29 -0700117 }
118 }
Chris Wren92aa4232013-10-04 11:29:36 -0400119}