1 /*
2  * Copyright (C) 2020 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 package com.android.launcher3.hybridhotseat;
17 
18 import static com.android.launcher3.LauncherSettings.Favorites.HYBRID_HOTSEAT_BACKUP_TABLE;
19 import static com.android.launcher3.provider.LauncherDbUtils.tableExists;
20 import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
21 
22 import android.content.Context;
23 
24 import com.android.launcher3.LauncherAppState;
25 import com.android.launcher3.LauncherModel;
26 import com.android.launcher3.model.GridBackupTable;
27 import com.android.launcher3.model.ModelDbController;
28 import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
29 
30 /**
31  * A helper class to manage migration revert restoration for hybrid hotseat
32  */
33 public class HotseatRestoreHelper {
34 
35     /**
36      * Creates a snapshot backup of Favorite table for future restoration use.
37      */
createBackup(Context context)38     public static void createBackup(Context context) {
39         MODEL_EXECUTOR.execute(() -> {
40             ModelDbController dbController = LauncherAppState.getInstance(context)
41                     .getModel().getModelDbController();
42             try (SQLiteTransaction transaction = dbController.newTransaction()) {
43                 GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb());
44                 backupTable.createCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE);
45                 transaction.commit();
46                 dbController.refreshHotseatRestoreTable();
47             }
48         });
49     }
50 
51     /**
52      * Finds and restores a previously saved snapshow of Favorites table
53      */
restoreBackup(Context context)54     public static void restoreBackup(Context context) {
55         MODEL_EXECUTOR.execute(() -> {
56             LauncherModel model = LauncherAppState.getInstance(context).getModel();
57             try (SQLiteTransaction transaction = model.getModelDbController().newTransaction()) {
58                 if (!tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE)) {
59                     return;
60                 }
61                 GridBackupTable backupTable = new GridBackupTable(context, transaction.getDb());
62                 backupTable.restoreFromCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE, true);
63                 transaction.commit();
64                 model.forceReload();
65             }
66         });
67     }
68 }
69