2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3;
19 import android.app.backup.BackupAgentHelper;
20 import android.app.backup.BackupDataInput;
21 import android.app.backup.BackupManager;
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.os.ParcelFileDescriptor;
25 import android.util.Log;
27 import com.android.launcher3.model.MigrateFromRestoreTask;
29 import java.io.IOException;
31 public class LauncherBackupAgentHelper extends BackupAgentHelper {
33 private static final String TAG = "LauncherBackupAgentHelper";
35 private static final String LAUNCHER_DATA_PREFIX = "L";
37 static final boolean VERBOSE = false;
38 static final boolean DEBUG = false;
40 private static BackupManager sBackupManager;
43 * Notify the backup manager that out database is dirty.
45 * <P>This does not force an immediate backup.
47 * @param context application context
49 public static void dataChanged(Context context) {
50 if (sBackupManager == null) {
51 sBackupManager = new BackupManager(context);
53 sBackupManager.dataChanged();
56 private LauncherBackupHelper mHelper;
59 public void onCreate() {
61 mHelper = new LauncherBackupHelper(this);
62 addHelper(LAUNCHER_DATA_PREFIX, mHelper);
66 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
68 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.");
75 // Clear dB before restore
76 LauncherAppState.getLauncherProvider().createEmptyDB();
80 super.onRestore(data, appVersionCode, newState);
81 // If no favorite was migrated, clear the data and start fresh.
82 final Cursor c = getContentResolver().query(
83 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
84 hasData = c.moveToNext();
86 } catch (Exception e) {
87 // If the restore fails, we should do a fresh start.
88 Log.e(TAG, "Restore failed", e);
92 if (hasData && mHelper.restoreSuccessful) {
93 LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
94 LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
96 // Rank was added in v4.
97 if (mHelper.restoredBackupVersion <= 3) {
98 LauncherAppState.getLauncherProvider().updateFolderItemsRank();
101 if (mHelper.shouldAttemptWorkspaceMigration()) {
102 MigrateFromRestoreTask.markForMigration(getApplicationContext(),
103 (int) mHelper.migrationCompatibleProfileData.desktopCols,
104 (int) mHelper.migrationCompatibleProfileData.desktopRows,
105 mHelper.widgetSizes);
108 LauncherAppState.getLauncherProvider().convertShortcutsToLauncherActivities();
110 if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
111 LauncherAppState.getLauncherProvider().createEmptyDB();