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
17package com.android.launcher3;
18
19import android.app.backup.BackupAgentHelper;
20import android.app.backup.BackupDataInput;
21import android.app.backup.BackupManager;
22import android.content.Context;
23import android.database.Cursor;
24import android.os.ParcelFileDescriptor;
25import android.util.Log;
26
27import com.android.launcher3.model.MigrateFromRestoreTask;
28
29import java.io.IOException;
30
31public class LauncherBackupAgentHelper extends BackupAgentHelper {
32
33    private static final String TAG = "LauncherBackupAgentHelper";
34
35    private static final String LAUNCHER_DATA_PREFIX = "L";
36
37    static final boolean VERBOSE = false;
38    static final boolean DEBUG = false;
39
40    private static BackupManager sBackupManager;
41
42    /**
43     * Notify the backup manager that out database is dirty.
44     *
45     * <P>This does not force an immediate backup.
46     *
47     * @param context application context
48     */
49    public static void dataChanged(Context context) {
50        if (sBackupManager == null) {
51            sBackupManager = new BackupManager(context);
52        }
53        sBackupManager.dataChanged();
54    }
55
56    private LauncherBackupHelper mHelper;
57
58    @Override
59    public void onCreate() {
60        super.onCreate();
61        mHelper = new LauncherBackupHelper(this);
62        addHelper(LAUNCHER_DATA_PREFIX, mHelper);
63    }
64
65    @Override
66    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
67            throws IOException {
68        if (!Utilities.ATLEAST_LOLLIPOP) {
69            // No restore for old devices.
70            Log.i(TAG, "You shall not pass!!!");
71            Log.d(TAG, "Restore is only supported on devices running Lollipop and above.");
72            return;
73        }
74
75        // Clear dB before restore
76        LauncherAppState.getLauncherProvider().createEmptyDB();
77
78        boolean hasData;
79        try {
80            super.onRestore(data, appVersionCode, newState);
81            // If no favorite was migrated, clear the data and start fresh.
82            final Cursor c = getContentResolver().query(
83                    LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
84            hasData = c.moveToNext();
85            c.close();
86        } catch (Exception e) {
87            // If the restore fails, we should do a fresh start.
88            Log.e(TAG, "Restore failed", e);
89            hasData = false;
90        }
91
92        if (hasData && mHelper.restoreSuccessful) {
93            LauncherAppState.getLauncherProvider().clearFlagEmptyDbCreated();
94            LauncherClings.synchonouslyMarkFirstRunClingDismissed(this);
95
96            // Rank was added in v4.
97            if (mHelper.restoredBackupVersion <= 3) {
98                LauncherAppState.getLauncherProvider().updateFolderItemsRank();
99            }
100
101            if (MigrateFromRestoreTask.ENABLED && mHelper.shouldAttemptWorkspaceMigration()) {
102                MigrateFromRestoreTask.markForMigration(getApplicationContext(),
103                        (int) mHelper.migrationCompatibleProfileData.desktopCols,
104                        (int) mHelper.migrationCompatibleProfileData.desktopRows,
105                        mHelper.widgetSizes);
106            }
107
108            LauncherAppState.getLauncherProvider().convertShortcutsToLauncherActivities();
109        } else {
110            if (VERBOSE) Log.v(TAG, "Nothing was restored, clearing DB");
111            LauncherAppState.getLauncherProvider().createEmptyDB();
112        }
113    }
114}
115