blob: b03b13cccb7681e9b2ac63d55860c53cd1477fa2 [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
Chris Wren50c8f422014-01-15 16:10:39 -050035 static final boolean VERBOSE = true;
36 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 super.onRestore(data, appVersionCode, newState);
73
74 // If no favorite was migrated, clear the data and start fresh.
75 final Cursor c = getContentResolver().query(
76 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, null, null, null, null);
77 boolean hasData = c.moveToNext();
78 c.close();
79
Sunny Goyal33d44382014-10-16 09:24:19 -070080 if (hasData && mHelper.restoreSuccessful) {
81 LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
82 LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
83 } else {
Sunny Goyal42de82f2014-09-26 22:09:29 -070084 if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
85 LauncherAppState.getLauncherProvider().createEmptyDB();
86 }
87 }
Chris Wren92aa4232013-10-04 11:29:36 -040088}