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