1 /*
2  * Copyright (C) 2016 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 
17 package com.android.launcher3.provider;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.database.DatabaseUtils;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.os.Binder;
25 import android.os.Process;
26 import android.util.Log;
27 
28 import com.android.launcher3.LauncherAppState;
29 import com.android.launcher3.LauncherSettings.Favorites;
30 import com.android.launcher3.pm.UserCache;
31 import com.android.launcher3.util.IntArray;
32 
33 import java.util.Locale;
34 
35 /**
36  * A set of utility methods for Launcher DB used for DB updates and migration.
37  */
38 public class LauncherDbUtils {
39 
40     private static final String TAG = "LauncherDbUtils";
41 
42     /**
43      * Makes the first screen as screen 0 (if screen 0 already exists,
44      * renames it to some other number).
45      * If the first row of screen 0 is non empty, runs a 'lossy' GridMigrationTask to clear
46      * the first row. The items in the first screen are moved and resized but the carry-forward
47      * items are simply deleted.
48      */
prepareScreenZeroToHostQsb(Context context, SQLiteDatabase db)49     public static boolean prepareScreenZeroToHostQsb(Context context, SQLiteDatabase db) {
50         try (SQLiteTransaction t = new SQLiteTransaction(db)) {
51             // Get the first screen
52             final int firstScreenId;
53             try (Cursor c = db.rawQuery(String.format(Locale.ENGLISH,
54                     "SELECT MIN(%1$s) from %2$s where %3$s = %4$d",
55                     Favorites.SCREEN, Favorites.TABLE_NAME, Favorites.CONTAINER,
56                     Favorites.CONTAINER_DESKTOP), null)) {
57 
58                 if (!c.moveToNext()) {
59                     // No update needed
60                     t.commit();
61                     return true;
62                 }
63 
64                 firstScreenId = c.getInt(0);
65             }
66 
67             if (firstScreenId != 0) {
68                 // Rename the first screen to 0.
69                 renameScreen(db, firstScreenId, 0);
70             }
71 
72             // Check if the first row is empty
73             if (DatabaseUtils.queryNumEntries(db, Favorites.TABLE_NAME,
74                     "container = -100 and screen = 0 and cellY = 0") == 0) {
75                 // First row is empty, no need to migrate.
76                 t.commit();
77                 return true;
78             }
79 
80             new LossyScreenMigrationTask(context, LauncherAppState.getIDP(context), db)
81                     .migrateScreen0();
82             t.commit();
83             return true;
84         } catch (Exception e) {
85             Log.e(TAG, "Failed to update workspace size", e);
86             return false;
87         }
88     }
89 
renameScreen(SQLiteDatabase db, int oldScreen, int newScreen)90     private static void renameScreen(SQLiteDatabase db, int oldScreen, int newScreen) {
91         String[] whereParams = new String[] { Integer.toString(oldScreen) };
92         ContentValues values = new ContentValues();
93         values.put(Favorites.SCREEN, newScreen);
94         db.update(Favorites.TABLE_NAME, values, "container = -100 and screen = ?", whereParams);
95     }
96 
queryIntArray(SQLiteDatabase db, String tableName, String columnName, String selection, String groupBy, String orderBy)97     public static IntArray queryIntArray(SQLiteDatabase db, String tableName, String columnName,
98             String selection, String groupBy, String orderBy) {
99         IntArray out = new IntArray();
100         try (Cursor c = db.query(tableName, new String[] { columnName }, selection, null,
101                 groupBy, null, orderBy)) {
102             while (c.moveToNext()) {
103                 out.add(c.getInt(0));
104             }
105         }
106         return out;
107     }
108 
tableExists(SQLiteDatabase db, String tableName)109     public static boolean tableExists(SQLiteDatabase db, String tableName) {
110         try (Cursor c = db.query(true, "sqlite_master", new String[] {"tbl_name"},
111                 "tbl_name = ?", new String[] {tableName},
112                 null, null, null, null, null)) {
113             return c.getCount() > 0;
114         }
115     }
116 
dropTable(SQLiteDatabase db, String tableName)117     public static void dropTable(SQLiteDatabase db, String tableName) {
118         db.execSQL("DROP TABLE IF EXISTS " + tableName);
119     }
120 
121     /** Copy fromTable in fromDb to toTable in toDb. */
copyTable(SQLiteDatabase fromDb, String fromTable, SQLiteDatabase toDb, String toTable, Context context)122     public static void copyTable(SQLiteDatabase fromDb, String fromTable, SQLiteDatabase toDb,
123             String toTable, Context context) {
124         long userSerial = UserCache.INSTANCE.get(context).getSerialNumberForUser(
125                 Process.myUserHandle());
126         dropTable(toDb, toTable);
127         Favorites.addTableToDb(toDb, userSerial, false, toTable);
128         if (fromDb != toDb) {
129             toDb.execSQL("ATTACH DATABASE '" + fromDb.getPath() + "' AS from_db");
130             toDb.execSQL(
131                     "INSERT INTO " + toTable + " SELECT * FROM from_db." + fromTable);
132             toDb.execSQL("DETACH DATABASE 'from_db'");
133         } else {
134             toDb.execSQL("INSERT INTO " + toTable + " SELECT * FROM " + fromTable);
135         }
136     }
137 
138     /**
139      * Utility class to simplify managing sqlite transactions
140      */
141     public static class SQLiteTransaction extends Binder implements AutoCloseable {
142         private final SQLiteDatabase mDb;
143 
SQLiteTransaction(SQLiteDatabase db)144         public SQLiteTransaction(SQLiteDatabase db) {
145             mDb = db;
146             db.beginTransaction();
147         }
148 
commit()149         public void commit() {
150             mDb.setTransactionSuccessful();
151         }
152 
153         @Override
close()154         public void close() {
155             mDb.endTransaction();
156         }
157 
getDb()158         public SQLiteDatabase getDb() {
159             return mDb;
160         }
161     }
162 }
163