blob: c20c6939d394cc1496b895cd64f6377a90410108 [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 Wren4b171362014-01-13 17:39:02 -050025import android.provider.Settings;
Chris Wren50c8f422014-01-15 16:10:39 -050026import android.util.Log;
Chris Wren92aa4232013-10-04 11:29:36 -040027
Sunny Goyal42de82f2014-09-26 22:09:29 -070028import java.io.IOException;
29
Chris Wren92aa4232013-10-04 11:29:36 -040030public class LauncherBackupAgentHelper extends BackupAgentHelper {
31
Chris Wren45297f82013-10-17 15:16:48 -040032 private static final String TAG = "LauncherBackupAgentHelper";
Chris Wren50c8f422014-01-15 16:10:39 -050033 static final boolean VERBOSE = true;
34 static final boolean DEBUG = false;
Chris Wren45297f82013-10-17 15:16:48 -040035
Chris Wren92aa4232013-10-04 11:29:36 -040036 private static BackupManager sBackupManager;
37
Chris Wren4b171362014-01-13 17:39:02 -050038 protected static final String SETTING_RESTORE_ENABLED = "launcher_restore_enabled";
39
Chris Wren92aa4232013-10-04 11:29:36 -040040 /**
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
Chris Wren45297f82013-10-17 15:16:48 -040054 @Override
55 public void onDestroy() {
56 // There is only one process accessing this preference file, but the restore
57 // modifies the file outside the normal codepaths, so it looks like another
58 // process. This forces a reload of the file, in case this process persists.
59 String spKey = LauncherAppState.getSharedPreferencesKey();
Sunny Goyal42de82f2014-09-26 22:09:29 -070060 getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS);
Chris Wren45297f82013-10-17 15:16:48 -040061 super.onDestroy();
62 }
Chris Wren92aa4232013-10-04 11:29:36 -040063
64 @Override
65 public void onCreate() {
Chris Wren4b171362014-01-13 17:39:02 -050066 boolean restoreEnabled = 0 != Settings.Secure.getInt(
Chris Wren9041a982014-06-26 12:09:34 -040067 getContentResolver(), SETTING_RESTORE_ENABLED, 1);
Chris Wren50c8f422014-01-15 16:10:39 -050068 if (VERBOSE) Log.v(TAG, "restore is " + (restoreEnabled ? "enabled" : "disabled"));
Chris Wren4b171362014-01-13 17:39:02 -050069
Chris Wren45297f82013-10-17 15:16:48 -040070 addHelper(LauncherBackupHelper.LAUNCHER_PREFS_PREFIX,
Chris Wren4b171362014-01-13 17:39:02 -050071 new LauncherPreferencesBackupHelper(this,
72 LauncherAppState.getSharedPreferencesKey(),
73 restoreEnabled));
74 addHelper(LauncherBackupHelper.LAUNCHER_PREFIX,
75 new LauncherBackupHelper(this, restoreEnabled));
Chris Wren92aa4232013-10-04 11:29:36 -040076 }
Sunny Goyal42de82f2014-09-26 22:09:29 -070077
78 @Override
79 public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
80 throws IOException {
81 super.onRestore(data, appVersionCode, newState);
82
83 // If no favorite was migrated, clear the data and start fresh.
84 final Cursor c = getContentResolver().query(
85 LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, null, null, null, null);
86 boolean hasData = c.moveToNext();
87 c.close();
88
89 if (!hasData) {
90 if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
91 LauncherAppState.getLauncherProvider().createEmptyDB();
92 }
93 }
Chris Wren92aa4232013-10-04 11:29:36 -040094}