blob: 5f7173fac104f25185707ca18c5b720d34319659 [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 Goyal42de82f2014-09-26 22:09:29 -070027import java.io.IOException;
28
Chris Wren92aa4232013-10-04 11:29:36 -040029public class LauncherBackupAgentHelper extends BackupAgentHelper {
30
Chris Wren45297f82013-10-17 15:16:48 -040031 private static final String TAG = "LauncherBackupAgentHelper";
Sunny Goyal33d44382014-10-16 09:24:19 -070032
33 private static final String LAUNCHER_DATA_PREFIX = "L";
34
Sunny Goyalc0ee6752015-01-16 14:10:32 -080035 static final boolean VERBOSE = false;
Chris Wren50c8f422014-01-15 16:10:39 -050036 static final boolean DEBUG = false;
Chris Wren45297f82013-10-17 15:16:48 -040037
Chris Wren92aa4232013-10-04 11:29:36 -040038 private static BackupManager sBackupManager;
39
40 /**
41 * Notify the backup manager that out database is dirty.
42 *
43 * <P>This does not force an immediate backup.
44 *
45 * @param context application context
46 */
47 public static void dataChanged(Context context) {
48 if (sBackupManager == null) {
49 sBackupManager = new BackupManager(context);
50 }
51 sBackupManager.dataChanged();
52 }
53
Sunny Goyal33d44382014-10-16 09:24:19 -070054 private LauncherBackupHelper mHelper;
Chris Wren92aa4232013-10-04 11:29:36 -040055
56 @Override
57 public void onCreate() {
Sunny Goyal33d44382014-10-16 09:24:19 -070058 super.onCreate();
59 mHelper = new LauncherBackupHelper(this);
60 addHelper(LAUNCHER_DATA_PREFIX, mHelper);
Chris Wren92aa4232013-10-04 11:29:36 -040061 }
Sunny Goyal42de82f2014-09-26 22:09:29 -070062
63 @Override
64 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
65 throws IOException {
Sunny Goyald8cec092014-10-23 13:57:41 -070066 if (!Utilities.isLmpOrAbove()) {
67 // No restore for old devices.
68 Log.i(TAG, "You shall not pass!!!");
69 Log.d(TAG, "Restore is only supported on devices running Lollipop and above.");
70 return;
71 }
Sunny Goyal42de82f2014-09-26 22:09:29 -070072
Sunny Goyalcf30da52014-11-18 10:38:22 -080073 // Clear dB before restore
74 LauncherAppState.getLauncherProvider().createEmptyDB();
75
76 boolean hasData;
77 try {
78 super.onRestore(data, appVersionCode, newState);
79 // If no favorite was migrated, clear the data and start fresh.
80 final Cursor c = getContentResolver().query(
Sunny Goyal1d4a2df2015-03-30 11:11:46 -070081 LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
Sunny Goyalcf30da52014-11-18 10:38:22 -080082 hasData = c.moveToNext();
83 c.close();
84 } catch (Exception e) {
85 // If the restore fails, we should do a fresh start.
86 Log.e(TAG, "Restore failed", e);
87 hasData = false;
88 }
Sunny Goyal42de82f2014-09-26 22:09:29 -070089
Sunny Goyal33d44382014-10-16 09:24:19 -070090 if (hasData && mHelper.restoreSuccessful) {
91 LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
92 LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
Sunny Goyal08f72612015-01-05 13:41:43 -080093
94 // TODO: Update the backup set to include rank.
95 if (mHelper.restoredBackupVersion <= 2) {
96 LauncherAppState.getLauncherProvider().updateFolderItemsRank();
97 }
Sunny Goyal33d44382014-10-16 09:24:19 -070098 } else {
Sunny Goyal42de82f2014-09-26 22:09:29 -070099 if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
100 LauncherAppState.getLauncherProvider().createEmptyDB();
101 }
102 }
Chris Wren92aa4232013-10-04 11:29:36 -0400103}