blob: 3debef60e03a4cb8ce5cda30441a544b91a58c21 [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 Goyald8cec092014-10-23 13:57:41 -070068 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 Goyal42de82f2014-09-26 22:09:29 -070074
Sunny Goyalcf30da52014-11-18 10:38:22 -080075 // 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 Goyal1d4a2df2015-03-30 11:11:46 -070083 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Sunny Goyalcf30da52014-11-18 10:38:22 -080084 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 Goyal42de82f2014-09-26 22:09:29 -070091
Sunny Goyal33d44382014-10-16 09:24:19 -070092 if (hasData && mHelper.restoreSuccessful) {
93 LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
94 LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
Sunny Goyal08f72612015-01-05 13:41:43 -080095
Sunny Goyal107ea632015-07-20 12:59:39 -070096 // Rank was added in v4.
Sunny Goyal3a30cfe2015-07-16 17:27:43 -070097 if (mHelper.restoredBackupVersion <= 3) {
Sunny Goyal08f72612015-01-05 13:41:43 -080098 LauncherAppState.getLauncherProvider().updateFolderItemsRank();
99 }
Sunny Goyal107ea632015-07-20 12:59:39 -0700100
Sunny Goyal6579e1e2015-08-11 12:10:34 -0700101 if (MigrateFromRestoreTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) {
Sunny Goyale5bb7052015-07-27 14:36:07 -0700102 MigrateFromRestoreTask.markForMigration(getApplicationContext(),
103 (int) mHelper.migrationCompatibleProfileData.desktopCols,
104 (int) mHelper.migrationCompatibleProfileData.desktopRows,
105 mHelper.widgetSizes);
106 }
107
Sunny Goyal107ea632015-07-20 12:59:39 -0700108 LauncherAppState.getLauncherProvider().convertShortcutsToLauncherActivities();
Sunny Goyal33d44382014-10-16 09:24:19 -0700109 } else {
Sunny Goyal42de82f2014-09-26 22:09:29 -0700110 if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
111 LauncherAppState.getLauncherProvider().createEmptyDB();
112 }
113 }
Chris Wren92aa4232013-10-04 11:29:36 -0400114}