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.InvariantDeviceProfile; 25 import com.android.launcher3.LauncherAppState; 26 import com.android.launcher3.LauncherSettings; 27 import com.android.launcher3.model.GridBackupTable; 28 import com.android.launcher3.provider.LauncherDbUtils; 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 try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) 41 LauncherSettings.Settings.call( 42 context.getContentResolver(), 43 LauncherSettings.Settings.METHOD_NEW_TRANSACTION) 44 .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) { 45 InvariantDeviceProfile idp = LauncherAppState.getIDP(context); 46 GridBackupTable backupTable = new GridBackupTable(context, 47 transaction.getDb(), idp.numDatabaseHotseatIcons, idp.numColumns, 48 idp.numRows); 49 backupTable.createCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE); 50 transaction.commit(); 51 LauncherSettings.Settings.call(context.getContentResolver(), 52 LauncherSettings.Settings.METHOD_REFRESH_HOTSEAT_RESTORE_TABLE); 53 } 54 }); 55 } 56 57 /** 58 * Finds and restores a previously saved snapshow of Favorites table 59 */ restoreBackup(Context context)60 public static void restoreBackup(Context context) { 61 MODEL_EXECUTOR.execute(() -> { 62 try (LauncherDbUtils.SQLiteTransaction transaction = (LauncherDbUtils.SQLiteTransaction) 63 LauncherSettings.Settings.call( 64 context.getContentResolver(), 65 LauncherSettings.Settings.METHOD_NEW_TRANSACTION) 66 .getBinder(LauncherSettings.Settings.EXTRA_VALUE)) { 67 if (!tableExists(transaction.getDb(), HYBRID_HOTSEAT_BACKUP_TABLE)) { 68 return; 69 } 70 InvariantDeviceProfile idp = LauncherAppState.getIDP(context); 71 GridBackupTable backupTable = new GridBackupTable(context, 72 transaction.getDb(), idp.numDatabaseHotseatIcons, idp.numColumns, 73 idp.numRows); 74 backupTable.restoreFromCustomBackupTable(HYBRID_HOTSEAT_BACKUP_TABLE, true); 75 transaction.commit(); 76 LauncherAppState.getInstance(context).getModel().forceReload(); 77 } 78 }); 79 } 80 } 81