2013-10-04 11:29:36 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
|
|
|
|
import android.app.backup.BackupAgentHelper;
|
2014-09-26 22:09:29 -07:00
|
|
|
import android.app.backup.BackupDataInput;
|
2013-10-04 11:29:36 -04:00
|
|
|
import android.app.backup.BackupManager;
|
|
|
|
|
import android.content.Context;
|
2014-09-26 22:09:29 -07:00
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.os.ParcelFileDescriptor;
|
2014-01-15 16:10:39 -05:00
|
|
|
import android.util.Log;
|
2013-10-04 11:29:36 -04:00
|
|
|
|
2015-12-14 14:27:38 -08:00
|
|
|
import com.android.launcher3.model.GridSizeMigrationTask;
|
2015-07-27 14:36:07 -07:00
|
|
|
|
2014-09-26 22:09:29 -07:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2013-10-04 11:29:36 -04:00
|
|
|
public class LauncherBackupAgentHelper extends BackupAgentHelper {
|
|
|
|
|
|
2015-12-17 22:23:02 -08:00
|
|
|
private static final String TAG = "LauncherBAHelper";
|
2014-10-16 09:24:19 -07:00
|
|
|
|
|
|
|
|
private static final String LAUNCHER_DATA_PREFIX = "L";
|
|
|
|
|
|
2015-01-16 14:10:32 -08:00
|
|
|
static final boolean VERBOSE = false;
|
2014-01-15 16:10:39 -05:00
|
|
|
static final boolean DEBUG = false;
|
2013-10-17 15:16:48 -04:00
|
|
|
|
2013-10-04 11:29:36 -04:00
|
|
|
private static BackupManager sBackupManager;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notify the backup manager that out database is dirty.
|
|
|
|
|
*
|
|
|
|
|
* <P>This does not force an immediate backup.
|
|
|
|
|
*
|
|
|
|
|
* @param context application context
|
|
|
|
|
*/
|
|
|
|
|
public static void dataChanged(Context context) {
|
|
|
|
|
if (sBackupManager == null) {
|
|
|
|
|
sBackupManager = new BackupManager(context);
|
|
|
|
|
}
|
|
|
|
|
sBackupManager.dataChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-16 09:24:19 -07:00
|
|
|
private LauncherBackupHelper mHelper;
|
2013-10-04 11:29:36 -04:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate() {
|
2014-10-16 09:24:19 -07:00
|
|
|
super.onCreate();
|
|
|
|
|
mHelper = new LauncherBackupHelper(this);
|
|
|
|
|
addHelper(LAUNCHER_DATA_PREFIX, mHelper);
|
2013-10-04 11:29:36 -04:00
|
|
|
}
|
2014-09-26 22:09:29 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
|
|
|
|
|
throws IOException {
|
2015-08-17 12:24:25 -07:00
|
|
|
if (!Utilities.ATLEAST_LOLLIPOP) {
|
2014-10-23 13:57:41 -07:00
|
|
|
// No restore for old devices.
|
|
|
|
|
Log.i(TAG, "You shall not pass!!!");
|
|
|
|
|
Log.d(TAG, "Restore is only supported on devices running Lollipop and above.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-09-26 22:09:29 -07:00
|
|
|
|
2014-11-18 10:38:22 -08:00
|
|
|
// Clear dB before restore
|
2015-09-22 18:24:19 -07:00
|
|
|
LauncherSettings.Settings.call(getContentResolver(),
|
|
|
|
|
LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
|
2014-11-18 10:38:22 -08:00
|
|
|
|
|
|
|
|
boolean hasData;
|
|
|
|
|
try {
|
|
|
|
|
super.onRestore(data, appVersionCode, newState);
|
|
|
|
|
// If no favorite was migrated, clear the data and start fresh.
|
|
|
|
|
final Cursor c = getContentResolver().query(
|
2015-03-30 11:11:46 -07:00
|
|
|
LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
|
2014-11-18 10:38:22 -08:00
|
|
|
hasData = c.moveToNext();
|
|
|
|
|
c.close();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// If the restore fails, we should do a fresh start.
|
|
|
|
|
Log.e(TAG, "Restore failed", e);
|
|
|
|
|
hasData = false;
|
|
|
|
|
}
|
2014-09-26 22:09:29 -07:00
|
|
|
|
2014-10-16 09:24:19 -07:00
|
|
|
if (hasData && mHelper.restoreSuccessful) {
|
2015-09-22 18:24:19 -07:00
|
|
|
LauncherSettings.Settings.call(getContentResolver(),
|
|
|
|
|
LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
|
2015-12-17 22:23:02 -08:00
|
|
|
LauncherClings.markFirstRunClingDismissed(this);
|
2015-01-05 13:41:43 -08:00
|
|
|
|
2015-07-20 12:59:39 -07:00
|
|
|
// Rank was added in v4.
|
2015-07-16 17:27:43 -07:00
|
|
|
if (mHelper.restoredBackupVersion <= 3) {
|
2015-09-22 18:24:19 -07:00
|
|
|
LauncherSettings.Settings.call(getContentResolver(),
|
|
|
|
|
LauncherSettings.Settings.METHOD_UPDATE_FOLDER_ITEMS_RANK);
|
2015-01-05 13:41:43 -08:00
|
|
|
}
|
2015-07-20 12:59:39 -07:00
|
|
|
|
2015-12-14 14:27:38 -08:00
|
|
|
// TODO: Update this logic to handle grid difference of 2. as well as hotseat difference
|
|
|
|
|
if (GridSizeMigrationTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) {
|
|
|
|
|
GridSizeMigrationTask.markForMigration(getApplicationContext(),
|
2015-07-27 14:36:07 -07:00
|
|
|
(int) mHelper.migrationCompatibleProfileData.desktopCols,
|
|
|
|
|
(int) mHelper.migrationCompatibleProfileData.desktopRows,
|
|
|
|
|
mHelper.widgetSizes);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-22 18:24:19 -07:00
|
|
|
LauncherSettings.Settings.call(getContentResolver(),
|
|
|
|
|
LauncherSettings.Settings.METHOD_CONVERT_SHORTCUTS_TO_ACTIVITIES);
|
2014-10-16 09:24:19 -07:00
|
|
|
} else {
|
2014-09-26 22:09:29 -07:00
|
|
|
if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
|
2015-09-22 18:24:19 -07:00
|
|
|
LauncherSettings.Settings.call(getContentResolver(),
|
|
|
|
|
LauncherSettings.Settings.METHOD_CREATE_EMPTY_DB);
|
2014-09-26 22:09:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
2013-10-04 11:29:36 -04:00
|
|
|
}
|