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