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; |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 20 | import android.app.backup.BackupDataInput; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 21 | import android.app.backup.BackupManager; |
| 22 | import android.content.Context; |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
| 24 | import android.os.ParcelFileDescriptor; |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 25 | import android.provider.Settings; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 26 | import android.util.Log; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 27 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 28 | import java.io.IOException; |
| 29 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 30 | public class LauncherBackupAgentHelper extends BackupAgentHelper { |
| 31 | |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 32 | private static final String TAG = "LauncherBackupAgentHelper"; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 33 | static final boolean VERBOSE = true; |
| 34 | static final boolean DEBUG = false; |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 35 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 36 | private static BackupManager sBackupManager; |
| 37 | |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 38 | protected static final String SETTING_RESTORE_ENABLED = "launcher_restore_enabled"; |
| 39 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 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 | |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 54 | @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 Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 60 | getSharedPreferences(spKey, Context.MODE_MULTI_PROCESS); |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 61 | super.onDestroy(); |
| 62 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 63 | |
| 64 | @Override |
| 65 | public void onCreate() { |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 66 | boolean restoreEnabled = 0 != Settings.Secure.getInt( |
Chris Wren | 9041a98 | 2014-06-26 12:09:34 -0400 | [diff] [blame] | 67 | getContentResolver(), SETTING_RESTORE_ENABLED, 1); |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 68 | if (VERBOSE) Log.v(TAG, "restore is " + (restoreEnabled ? "enabled" : "disabled")); |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 69 | |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 70 | addHelper(LauncherBackupHelper.LAUNCHER_PREFS_PREFIX, |
Chris Wren | 4b17136 | 2014-01-13 17:39:02 -0500 | [diff] [blame] | 71 | new LauncherPreferencesBackupHelper(this, |
| 72 | LauncherAppState.getSharedPreferencesKey(), |
| 73 | restoreEnabled)); |
| 74 | addHelper(LauncherBackupHelper.LAUNCHER_PREFIX, |
| 75 | new LauncherBackupHelper(this, restoreEnabled)); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 76 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 77 | |
| 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 Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 94 | } |