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 | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 25 | import android.util.Log; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 26 | |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 27 | import java.io.IOException; |
| 28 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 29 | public class LauncherBackupAgentHelper extends BackupAgentHelper { |
| 30 | |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 31 | private static final String TAG = "LauncherBackupAgentHelper"; |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 32 | |
| 33 | private static final String LAUNCHER_DATA_PREFIX = "L"; |
| 34 | |
Sunny Goyal | c0ee675 | 2015-01-16 14:10:32 -0800 | [diff] [blame] | 35 | static final boolean VERBOSE = false; |
Chris Wren | 50c8f42 | 2014-01-15 16:10:39 -0500 | [diff] [blame] | 36 | static final boolean DEBUG = false; |
Chris Wren | 45297f8 | 2013-10-17 15:16:48 -0400 | [diff] [blame] | 37 | |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 38 | 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 Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 54 | private LauncherBackupHelper mHelper; |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | public void onCreate() { |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 58 | super.onCreate(); |
| 59 | mHelper = new LauncherBackupHelper(this); |
| 60 | addHelper(LAUNCHER_DATA_PREFIX, mHelper); |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 61 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 62 | |
| 63 | @Override |
| 64 | public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) |
| 65 | throws IOException { |
Sunny Goyal | d8cec09 | 2014-10-23 13:57:41 -0700 | [diff] [blame] | 66 | 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 Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 72 | |
Sunny Goyal | cf30da5 | 2014-11-18 10:38:22 -0800 | [diff] [blame] | 73 | // Clear dB before restore |
| 74 | LauncherAppState.getLauncherProvider().createEmptyDB(); |
| 75 | |
| 76 | boolean hasData; |
| 77 | try { |
| 78 | super.onRestore(data, appVersionCode, newState); |
| 79 | // If no favorite was migrated, clear the data and start fresh. |
| 80 | final Cursor c = getContentResolver().query( |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 81 | LauncherSettings.Favorites.CONTENT_URI, null, null, null, null); |
Sunny Goyal | cf30da5 | 2014-11-18 10:38:22 -0800 | [diff] [blame] | 82 | hasData = c.moveToNext(); |
| 83 | c.close(); |
| 84 | } catch (Exception e) { |
| 85 | // If the restore fails, we should do a fresh start. |
| 86 | Log.e(TAG, "Restore failed", e); |
| 87 | hasData = false; |
| 88 | } |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 89 | |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 90 | if (hasData && mHelper.restoreSuccessful) { |
| 91 | LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated(); |
| 92 | LauncherClings.synchonouslyMarkFirstRunClingDismissed(this); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 93 | |
| 94 | // TODO: Update the backup set to include rank. |
| 95 | if (mHelper.restoredBackupVersion <= 2) { |
| 96 | LauncherAppState.getLauncherProvider().updateFolderItemsRank(); |
| 97 | } |
Sunny Goyal | 33d4438 | 2014-10-16 09:24:19 -0700 | [diff] [blame] | 98 | } else { |
Sunny Goyal | 42de82f | 2014-09-26 22:09:29 -0700 | [diff] [blame] | 99 | if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB"); |
| 100 | LauncherAppState.getLauncherProvider().createEmptyDB(); |
| 101 | } |
| 102 | } |
Chris Wren | 92aa423 | 2013-10-04 11:29:36 -0400 | [diff] [blame] | 103 | } |