blob: 7dd8cdeddedd5e843dcf266f071c88b67968647c [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;
20import android.app.backup.BackupManager;
Chris Wren45297f82013-10-17 15:16:48 -040021import android.app.backup.SharedPreferencesBackupHelper;
Chris Wren92aa4232013-10-04 11:29:36 -040022import android.content.Context;
Chris Wren45297f82013-10-17 15:16:48 -040023import android.content.SharedPreferences;
Chris Wren4b171362014-01-13 17:39:02 -050024import android.provider.Settings;
Chris Wren50c8f422014-01-15 16:10:39 -050025import android.util.Log;
Chris Wren92aa4232013-10-04 11:29:36 -040026
27public class LauncherBackupAgentHelper extends BackupAgentHelper {
28
Chris Wren45297f82013-10-17 15:16:48 -040029 private static final String TAG = "LauncherBackupAgentHelper";
Chris Wren50c8f422014-01-15 16:10:39 -050030 static final boolean VERBOSE = true;
31 static final boolean DEBUG = false;
Chris Wren45297f82013-10-17 15:16:48 -040032
Chris Wren92aa4232013-10-04 11:29:36 -040033 private static BackupManager sBackupManager;
34
Chris Wren4b171362014-01-13 17:39:02 -050035 protected static final String SETTING_RESTORE_ENABLED = "launcher_restore_enabled";
36
Chris Wren92aa4232013-10-04 11:29:36 -040037 /**
38 * Notify the backup manager that out database is dirty.
39 *
40 * <P>This does not force an immediate backup.
41 *
42 * @param context application context
43 */
44 public static void dataChanged(Context context) {
45 if (sBackupManager == null) {
46 sBackupManager = new BackupManager(context);
47 }
48 sBackupManager.dataChanged();
49 }
50
Chris Wren45297f82013-10-17 15:16:48 -040051 @Override
52 public void onDestroy() {
53 // There is only one process accessing this preference file, but the restore
54 // modifies the file outside the normal codepaths, so it looks like another
55 // process. This forces a reload of the file, in case this process persists.
56 String spKey = LauncherAppState.getSharedPreferencesKey();
57 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS);
58 super.onDestroy();
59 }
Chris Wren92aa4232013-10-04 11:29:36 -040060
61 @Override
62 public void onCreate() {
Chris Wren4b171362014-01-13 17:39:02 -050063 boolean restoreEnabled = 0 != Settings.Secure.getInt(
Chris Wren9041a982014-06-26 12:09:34 -040064 getContentResolver(), SETTING_RESTORE_ENABLED, 1);
Chris Wren50c8f422014-01-15 16:10:39 -050065 if (VERBOSE) Log.v(TAG, "restore is " + (restoreEnabled ? "enabled" : "disabled"));
Chris Wren4b171362014-01-13 17:39:02 -050066
Chris Wren45297f82013-10-17 15:16:48 -040067 addHelper(LauncherBackupHelper.LAUNCHER_PREFS_PREFIX,
Chris Wren4b171362014-01-13 17:39:02 -050068 new LauncherPreferencesBackupHelper(this,
69 LauncherAppState.getSharedPreferencesKey(),
70 restoreEnabled));
71 addHelper(LauncherBackupHelper.LAUNCHER_PREFIX,
72 new LauncherBackupHelper(this, restoreEnabled));
Chris Wren92aa4232013-10-04 11:29:36 -040073 }
74}