Home | History | Annotate | Download | only in provider
      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.sqlite.SQLiteDatabase;
     23 import android.util.Log;
     24 
     25 import com.android.launcher3.LauncherAppState;
     26 import com.android.launcher3.LauncherSettings.Favorites;
     27 import com.android.launcher3.LauncherSettings.WorkspaceScreens;
     28 import com.android.launcher3.logging.FileLog;
     29 
     30 import java.util.ArrayList;
     31 
     32 /**
     33  * A set of utility methods for Launcher DB used for DB updates and migration.
     34  */
     35 public class LauncherDbUtils {
     36 
     37     private static final String TAG = "LauncherDbUtils";
     38 
     39     /**
     40      * Makes the first screen as screen 0 (if screen 0 already exists,
     41      * renames it to some other number).
     42      * If the first row of screen 0 is non empty, runs a 'lossy' GridMigrationTask to clear
     43      * the first row. The items in the first screen are moved and resized but the carry-forward
     44      * items are simply deleted.
     45      */
     46     public static boolean prepareScreenZeroToHostQsb(Context context, SQLiteDatabase db) {
     47         db.beginTransaction();
     48         try {
     49             // Get the existing screens
     50             ArrayList<Long> screenIds = getScreenIdsFromCursor(db.query(WorkspaceScreens.TABLE_NAME,
     51                     null, null, null, null, null, WorkspaceScreens.SCREEN_RANK));
     52 
     53             if (screenIds.isEmpty()) {
     54                 // No update needed
     55                 return true;
     56             }
     57             if (screenIds.get(0) != 0) {
     58                 // First screen is not 0, we need to rename screens
     59                 if (screenIds.indexOf(0L) > -1) {
     60                     // There is already a screen 0. First rename it to a different screen.
     61                     long newScreenId = 1;
     62                     while (screenIds.indexOf(newScreenId) > -1) newScreenId++;
     63                     renameScreen(db, 0, newScreenId);
     64                 }
     65 
     66                 // Rename the first screen to 0.
     67                 renameScreen(db, screenIds.get(0), 0);
     68             }
     69 
     70             // Check if the first row is empty
     71             try (Cursor c = db.query(Favorites.TABLE_NAME, null,
     72                     "container = -100 and screen = 0 and cellY = 0", null, null, null, null)) {
     73                 if (c.getCount() == 0) {
     74                     // First row is empty, no need to migrate.
     75                     return true;
     76                 }
     77             }
     78 
     79             new LossyScreenMigrationTask(context, LauncherAppState.getIDP(context), 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 
     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      */
    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