Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.app.backup.BackupAgentHelper; |
| 20 | import android.app.backup.BackupManager; |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 21 | import android.app.backup.SharedPreferencesBackupHelper; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 22 | import android.content.Context; |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 23 | import android.content.SharedPreferences; |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 24 | import android.provider.Settings; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 25 | |
| 26 | public class LauncherBackupAgentHelper extends BackupAgentHelper { |
| 27 | |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 28 | private static final String TAG = "LauncherBackupAgentHelper"; |
| 29 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 30 | private static BackupManager sBackupManager; |
| 31 | |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 32 | protected static final String SETTING_RESTORE_ENABLED = "launcher_restore_enabled"; |
| 33 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 34 | /** |
| 35 | * Notify the backup manager that out database is dirty. |
| 36 | * |
| 37 | * <P>This does not force an immediate backup. |
| 38 | * |
| 39 | * @param context application context |
| 40 | */ |
| 41 | public static void dataChanged(Context context) { |
| 42 | if (sBackupManager == null) { |
| 43 | sBackupManager = new BackupManager(context); |
| 44 | } |
| 45 | sBackupManager.dataChanged(); |
| 46 | } |
| 47 | |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 48 | @Override |
| 49 | public void onDestroy() { |
| 50 | // There is only one process accessing this preference file, but the restore |
| 51 | // modifies the file outside the normal codepaths, so it looks like another |
| 52 | // process. This forces a reload of the file, in case this process persists. |
| 53 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
| 54 | SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS); |
| 55 | super.onDestroy(); |
| 56 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 57 | |
| 58 | @Override |
| 59 | public void onCreate() { |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 60 | |
| 61 | boolean restoreEnabled = 0 != Settings.Secure.getInt( |
| 62 | getContentResolver(), SETTING_RESTORE_ENABLED, 0); |
| 63 | |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 64 | addHelper(LauncherBackupHelper.LAUNCHER_PREFS_PREFIX, |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 65 | new LauncherPreferencesBackupHelper(this, |
| 66 | LauncherAppState.getSharedPreferencesKey(), |
| 67 | restoreEnabled)); |
| 68 | addHelper(LauncherBackupHelper.LAUNCHER_PREFIX, |
| 69 | new LauncherBackupHelper(this, restoreEnabled)); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 70 | } |
| 71 | } |