• 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.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.util.Log;
23 
24 import com.android.launcher3.LauncherAppState;
25 import com.android.launcher3.LauncherSettings.Favorites;
26 import com.android.launcher3.LauncherSettings.WorkspaceScreens;
27 import com.android.launcher3.logging.FileLog;
28 
29 import java.util.ArrayList;
30 
31 /**
32  * A set of utility methods for Launcher DB used for DB updates and migration.
33  */
34 public class LauncherDbUtils {
35 
36     private static final String TAG = "LauncherDbUtils";
37 
38     /**
39      * Makes the first screen as screen 0 (if screen 0 already exists,
40      * renames it to some other number).
41      * If the first row of screen 0 is non empty, runs a 'lossy' GridMigrationTask to clear
42      * the first row. The items in the first screen are moved and resized but the carry-forward
43      * items are simply deleted.
44      */
prepareScreenZeroToHostQsb(SQLiteDatabase db)45     public static boolean prepareScreenZeroToHostQsb(SQLiteDatabase db) {
46         db.beginTransaction();
47         try {
48             // Get the existing screens
49             ArrayList<Long> screenIds = getScreenIdsFromCursor(db.query(WorkspaceScreens.TABLE_NAME,
50                     null, null, null, null, null, WorkspaceScreens.SCREEN_RANK));
51 
52             if (screenIds.isEmpty()) {
53                 // No update needed
54                 return true;
55             }
56             if (screenIds.get(0) != 0) {
57                 // First screen is not 0, we need to rename screens
58                 if (screenIds.indexOf(0L) > -1) {
59                     // There is already a screen 0. First rename it to a differen screen.
60                     long newScreenId = 1;
61                     while (screenIds.indexOf(newScreenId) > -1) newScreenId++;
62                     renameScreen(db, 0, newScreenId);
63                 }
64 
65                 // Rename the first screen to 0.
66                 renameScreen(db, screenIds.get(0), 0);
67             }
68 
69             // Check if the first row is empty
70             try (Cursor c = db.query(Favorites.TABLE_NAME, null,
71                     "container = -100 and screen = 0 and cellY = 0", null, null, null, null)) {
72                 if (c.getCount() == 0) {
73                     // First row is empty, no need to migrate.
74                     return true;
75                 }
76             }
77 
78             LauncherAppState app = LauncherAppState.getInstance();
79             new LossyScreenMigrationTask(app.getContext(), app.getInvariantDeviceProfile(), db)
80                     .migrateScreen0();
81             db.setTransactionSuccessful();
82             return true;
83         } catch (Exception e) {
84             Log.e(TAG, "Failed to update workspace size", e);
85             return false;
86         } finally {
87             db.endTransaction();
88         }
89     }
90 
renameScreen(SQLiteDatabase db, long oldScreen, long newScreen)91     private static void renameScreen(SQLiteDatabase db, long oldScreen, long newScreen) {
92         String[] whereParams = new String[] { Long.toString(oldScreen) };
93 
94         ContentValues values = new ContentValues();
95         values.put(WorkspaceScreens._ID, newScreen);
96         db.update(WorkspaceScreens.TABLE_NAME, values, "_id = ?", whereParams);
97 
98         values.clear();
99         values.put(Favorites.SCREEN, newScreen);
100         db.update(Favorites.TABLE_NAME, values, "container = -100 and screen = ?", whereParams);
101     }
102 
103     /**
104      * Parses the cursor containing workspace screens table and returns the list of screen IDs
105      */
getScreenIdsFromCursor(Cursor sc)106     public static ArrayList<Long> getScreenIdsFromCursor(Cursor sc) {
107         ArrayList<Long> screenIds = new ArrayList<Long>();
108         try {
109             final int idIndex = sc.getColumnIndexOrThrow(WorkspaceScreens._ID);
110             while (sc.moveToNext()) {
111                 try {
112                     screenIds.add(sc.getLong(idIndex));
113                 } catch (Exception e) {
114                     FileLog.d(TAG, "Invalid screen id", e);
115                 }
116             }
117         } finally {
118             sc.close();
119         }
120         return screenIds;
121     }
122 }
123