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.graphics.Point;
     24 
     25 import com.android.launcher3.InvariantDeviceProfile;
     26 import com.android.launcher3.LauncherSettings.Favorites;
     27 import com.android.launcher3.Utilities;
     28 import com.android.launcher3.Workspace;
     29 import com.android.launcher3.model.GridSizeMigrationTask;
     30 import com.android.launcher3.util.LongArrayMap;
     31 
     32 import java.util.ArrayList;
     33 
     34 /**
     35  * An extension of {@link GridSizeMigrationTask} which migrates only one screen and
     36  * deletes all carry-forward items.
     37  */
     38 public class LossyScreenMigrationTask extends GridSizeMigrationTask {
     39 
     40     private final SQLiteDatabase mDb;
     41 
     42     private final LongArrayMap<DbEntry> mOriginalItems;
     43     private final LongArrayMap<DbEntry> mUpdates;
     44 
     45     protected LossyScreenMigrationTask(
     46             Context context, InvariantDeviceProfile idp, SQLiteDatabase db) {
     47         // Decrease the rows count by 1
     48         super(context, idp, getValidPackages(context),
     49                 new Point(idp.numColumns, idp.numRows + 1),
     50                 new Point(idp.numColumns, idp.numRows));
     51 
     52         mDb = db;
     53         mOriginalItems = new LongArrayMap<>();
     54         mUpdates = new LongArrayMap<>();
     55     }
     56 
     57     @Override
     58     protected Cursor queryWorkspace(String[] columns, String where) {
     59         return mDb.query(Favorites.TABLE_NAME, columns, where, null, null, null, null);
     60     }
     61 
     62     @Override
     63     protected void update(DbEntry item) {
     64         mUpdates.put(item.id, item.copy());
     65     }
     66 
     67     @Override
     68     protected ArrayList<DbEntry> loadWorkspaceEntries(long screen) {
     69         ArrayList<DbEntry> result = super.loadWorkspaceEntries(screen);
     70         for (DbEntry entry : result) {
     71             mOriginalItems.put(entry.id, entry.copy());
     72 
     73             // Shift all items by 1 in y direction and mark them for update.
     74             entry.cellY++;
     75             mUpdates.put(entry.id, entry.copy());
     76         }
     77 
     78         return result;
     79     }
     80 
     81     public void migrateScreen0() {
     82         migrateScreen(Workspace.FIRST_SCREEN_ID);
     83 
     84         ContentValues tempValues = new ContentValues();
     85         for (DbEntry update : mUpdates) {
     86             DbEntry org = mOriginalItems.get(update.id);
     87 
     88             if (org.cellX != update.cellX || org.cellY != update.cellY
     89                     || org.spanX != update.spanX || org.spanY != update.spanY) {
     90                 tempValues.clear();
     91                 update.addToContentValues(tempValues);
     92                 mDb.update(Favorites.TABLE_NAME, tempValues, "_id = ?",
     93                         new String[] {Long.toString(update.id)});
     94             }
     95         }
     96 
     97         // Delete any carry over items as we are only migration a single screen.
     98         for (DbEntry entry : mCarryOver) {
     99             mEntryToRemove.add(entry.id);
    100         }
    101 
    102         if (!mEntryToRemove.isEmpty()) {
    103             mDb.delete(Favorites.TABLE_NAME,
    104                     Utilities.createDbSelectionQuery(Favorites._ID, mEntryToRemove), null);
    105         }
    106     }
    107 }
    108