1 /*
2  * Copyright (C) 2018 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.model;
17 
18 import static com.android.launcher3.provider.LauncherDbUtils.dropTable;
19 import static com.android.launcher3.provider.LauncherDbUtils.tableExists;
20 
21 import android.content.Context;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.os.Process;
24 
25 import com.android.launcher3.LauncherSettings.Favorites;
26 import com.android.launcher3.pm.UserCache;
27 
28 /**
29  * Helper class to backup and restore Favorites table into a separate table
30  * within the same data base.
31  */
32 public class GridBackupTable {
33 
34     private final Context mContext;
35     private final SQLiteDatabase mDb;
36 
GridBackupTable(Context context, SQLiteDatabase db)37     public GridBackupTable(Context context, SQLiteDatabase db) {
38         mContext = context;
39         mDb = db;
40     }
41 
42     /**
43      * Creates a new table and populates with copy of Favorites.TABLE_NAME
44      */
createCustomBackupTable(String tableName)45     public void createCustomBackupTable(String tableName) {
46         long profileId = UserCache.INSTANCE.get(mContext).getSerialNumberForUser(
47                 Process.myUserHandle());
48         copyTable(mDb, Favorites.TABLE_NAME, tableName, profileId);
49     }
50 
51     /**
52      *
53      * Restores the contents of a custom table to Favorites.TABLE_NAME
54      */
55 
restoreFromCustomBackupTable(String tableName, boolean dropAfterUse)56     public void restoreFromCustomBackupTable(String tableName, boolean dropAfterUse) {
57         if (!tableExists(mDb, tableName)) {
58             return;
59         }
60         long userSerial = UserCache.INSTANCE.get(mContext).getSerialNumberForUser(
61                 Process.myUserHandle());
62         copyTable(mDb, tableName, Favorites.TABLE_NAME, userSerial);
63         if (dropAfterUse) {
64             dropTable(mDb, tableName);
65         }
66     }
67     /**
68      * Copy valid grid entries from one table to another.
69      */
copyTable(SQLiteDatabase db, String from, String to, long userSerial)70     private static void copyTable(SQLiteDatabase db, String from, String to, long userSerial) {
71         dropTable(db, to);
72         Favorites.addTableToDb(db, userSerial, false, to);
73         db.execSQL("INSERT INTO " + to + " SELECT * FROM " + from);
74     }
75 }
76