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 com.android.launcher3.InvariantDeviceProfile;
23 import com.android.launcher3.Launcher;
24 import com.android.launcher3.LauncherSettings;
25 import com.android.launcher3.model.GridBackupTable;
26 import com.android.launcher3.provider.LauncherDbUtils;
27 
28 /**
29  * A helper class to manage migration revert restoration for hybrid hotseat
30  */
31 public class HotseatRestoreHelper {
32     private final Launcher mLauncher;
33     private boolean mBackupRestored = false;
34 
HotseatRestoreHelper(Launcher context)35     HotseatRestoreHelper(Launcher context) {
36         mLauncher = context;
37     }
38 
39     /**
40      * Creates a snapshot backup of Favorite table for future restoration use.
41      */
createBackup()42     public void createBackup() {
43         MODEL_EXECUTOR.execute(() -> {
44             try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction)
45                     LauncherSettings.Settings.call(
46                             mLauncher.getContentResolver(),
47                             LauncherSettings.Settings.METHOD_NEW_TRANSACTION)
48                             .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) {
49                 InvariantDeviceProfile idp = mLauncher.getDeviceProfile().inv;
50                 GridBackupTable backupTable = new GridBackupTable(mLauncher,
51                         transaction.getDb(), idp.numHotseatIcons, idp.numColumns,
52                         idp.numRows);
53                 backupTable.createCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE);
54                 transaction.commit();
55                 LauncherSettings.Settings.call(mLauncher.getContentResolver(),
56                         LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE);
57             }
58         });
59     }
60 
61     /**
62      * Finds and restores a previously saved snapshow of Favorites table
63      */
restoreBackup()64     public void restoreBackup() {
65         if (mBackupRestored) return;
66         MODEL_EXECUTOR.execute(() -> {
67             try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction)
68                     LauncherSettings.Settings.call(
69                             mLauncher.getContentResolver(),
70                             LauncherSettings.Settings.METHOD_NEW_TRANSACTION)
71                             .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) {
72                 if (!tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE)) {
73                     return;
74                 }
75                 InvariantDeviceProfile idp = mLauncher.getDeviceProfile().inv;
76                 GridBackupTable backupTable = new GridBackupTable(mLauncher,
77                         transaction.getDb(), idp.numHotseatIcons, idp.numColumns,
78                         idp.numRows);
79                 backupTable.restoreFromCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE, true);
80                 transaction.commit();
81                 mBackupRestored = true;
82                 mLauncher.getModel().forceReload();
83             }
84         });
85     }
86 }
87