2020-01-27 13:44:06 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2020 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3.model;
|
|
|
|
|
|
2023-05-12 23:07:25 +00:00
|
|
|
import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
|
|
|
|
|
import static com.android.launcher3.LauncherSettings.Favorites.TMP_TABLE;
|
2023-10-04 12:11:20 -07:00
|
|
|
import static com.android.launcher3.config.FeatureFlags.ENABLE_SMARTSPACE_REMOVAL;
|
2023-09-25 11:34:56 -07:00
|
|
|
import static com.android.launcher3.config.FeatureFlags.shouldShowFirstPageWidget;
|
2023-10-04 12:11:20 -07:00
|
|
|
import static com.android.launcher3.model.LoaderTask.SMARTSPACE_ON_HOME_SCREEN;
|
2023-05-12 23:07:25 +00:00
|
|
|
import static com.android.launcher3.provider.LauncherDbUtils.copyTable;
|
2020-02-06 16:37:16 -08:00
|
|
|
import static com.android.launcher3.provider.LauncherDbUtils.dropTable;
|
|
|
|
|
|
|
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.ContentValues;
|
2020-01-27 13:44:06 -08:00
|
|
|
import android.content.Context;
|
2020-02-06 16:37:16 -08:00
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.DatabaseUtils;
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
import android.graphics.Point;
|
2020-06-03 01:07:26 -07:00
|
|
|
import android.util.ArrayMap;
|
2020-02-06 16:37:16 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
2022-08-29 16:40:47 -07:00
|
|
|
import androidx.annotation.NonNull;
|
2020-01-27 13:44:06 -08:00
|
|
|
|
Render user's actual workspace in ThemePicker preview (Part 3)
go/grid-migration-preview
With this change, we can see actual grid migration in wallpaper preview.
The approach here: we use a tmp table (favorites_preview) here specifically for this preview (to write off the migration results), and load from this tmp table workspace items if migration is necessary and successful. Otherwise, we load from the current workspace.
UPDATED: this change should be completely compatible with the new multi-db grid migration algorithm. Here is why
1. In LauncherPreviewRender#renderScreenShot, I added a check to decide which grid migration preview method we should call. Once v2 preview method is implemented, it should be integrated with other parts of this change perfectly (the reason will be mentioned below).
2. While we have multiple DBs, mOpenHelper in LauncherProvider always points to the current db we are using. Queries using CONTENT_URI is routed to whatever DB mOpenHelper points to, so it works perfectly to directly operate on CONTENT_URI even when we use multi-db underneath the hood.
3. With 1 and 2 mentioned, I believe in order for this preview change to support multi-db, we only need to implement the V2 grid migration algorithm. Because most of what we are doing in this change is wrapped in GridSizeMigrationTask, it's perfectly safeguarded.
Bug: 144052839
Change-Id: Ie6d6048d77326f96546c8a180a7cd8f15b47e4c4
2020-01-12 01:07:59 -08:00
|
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
2023-10-04 12:11:20 -07:00
|
|
|
import com.android.launcher3.LauncherPrefs;
|
2020-02-06 16:37:16 -08:00
|
|
|
import com.android.launcher3.LauncherSettings;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
2022-02-10 13:45:02 +00:00
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
2020-02-06 16:37:16 -08:00
|
|
|
import com.android.launcher3.pm.InstallSessionHelper;
|
|
|
|
|
import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
|
|
|
|
|
import com.android.launcher3.util.GridOccupancy;
|
|
|
|
|
import com.android.launcher3.util.IntArray;
|
2021-02-22 14:03:44 +00:00
|
|
|
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
|
2020-02-06 16:37:16 -08:00
|
|
|
import com.android.launcher3.widget.WidgetManagerHelper;
|
|
|
|
|
|
2022-08-29 16:40:47 -07:00
|
|
|
import java.net.URISyntaxException;
|
2020-02-06 16:37:16 -08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
2020-06-09 14:11:01 -07:00
|
|
|
import java.util.HashMap;
|
2020-02-06 16:37:16 -08:00
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
2020-06-03 01:07:26 -07:00
|
|
|
import java.util.Map;
|
2020-02-06 16:37:16 -08:00
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Set;
|
2022-05-17 13:46:39 -07:00
|
|
|
import java.util.stream.Collectors;
|
Render user's actual workspace in ThemePicker preview (Part 3)
go/grid-migration-preview
With this change, we can see actual grid migration in wallpaper preview.
The approach here: we use a tmp table (favorites_preview) here specifically for this preview (to write off the migration results), and load from this tmp table workspace items if migration is necessary and successful. Otherwise, we load from the current workspace.
UPDATED: this change should be completely compatible with the new multi-db grid migration algorithm. Here is why
1. In LauncherPreviewRender#renderScreenShot, I added a check to decide which grid migration preview method we should call. Once v2 preview method is implemented, it should be integrated with other parts of this change perfectly (the reason will be mentioned below).
2. While we have multiple DBs, mOpenHelper in LauncherProvider always points to the current db we are using. Queries using CONTENT_URI is routed to whatever DB mOpenHelper points to, so it works perfectly to directly operate on CONTENT_URI even when we use multi-db underneath the hood.
3. With 1 and 2 mentioned, I believe in order for this preview change to support multi-db, we only need to implement the V2 grid migration algorithm. Because most of what we are doing in this change is wrapped in GridSizeMigrationTask, it's perfectly safeguarded.
Bug: 144052839
Change-Id: Ie6d6048d77326f96546c8a180a7cd8f15b47e4c4
2020-01-12 01:07:59 -08:00
|
|
|
|
2020-01-27 13:44:06 -08:00
|
|
|
/**
|
|
|
|
|
* This class takes care of shrinking the workspace (by maximum of one row and one column), as a
|
|
|
|
|
* result of restoring from a larger device or device density change.
|
|
|
|
|
*/
|
2022-11-21 12:14:16 -08:00
|
|
|
public class GridSizeMigrationUtil {
|
2020-01-27 13:44:06 -08:00
|
|
|
|
2022-11-21 12:14:16 -08:00
|
|
|
private static final String TAG = "GridSizeMigrationUtil";
|
2022-11-21 15:50:30 -08:00
|
|
|
private static final boolean DEBUG = true;
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2022-11-21 12:14:16 -08:00
|
|
|
private GridSizeMigrationUtil() {
|
|
|
|
|
// Util class should not be instantiated
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check given a new IDP, if migration is necessary.
|
|
|
|
|
*/
|
|
|
|
|
public static boolean needsToMigrate(Context context, InvariantDeviceProfile idp) {
|
2022-03-08 21:48:34 +00:00
|
|
|
return needsToMigrate(new DeviceGridState(context), new DeviceGridState(idp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean needsToMigrate(
|
|
|
|
|
DeviceGridState srcDeviceState, DeviceGridState destDeviceState) {
|
|
|
|
|
boolean needsToMigrate = !destDeviceState.isCompatible(srcDeviceState);
|
2021-09-20 12:11:12 +01:00
|
|
|
if (needsToMigrate) {
|
2022-07-04 13:05:38 +01:00
|
|
|
Log.i(TAG, "Migration is needed. destDeviceState: " + destDeviceState
|
2022-03-08 21:48:34 +00:00
|
|
|
+ ", srcDeviceState: " + srcDeviceState);
|
2021-09-20 12:11:12 +01:00
|
|
|
}
|
2021-09-16 00:10:55 +01:00
|
|
|
return needsToMigrate;
|
2020-01-27 13:44:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-05-12 23:07:25 +00:00
|
|
|
* When migrating the grid, we copy the table
|
|
|
|
|
* {@link LauncherSettings.Favorites#TABLE_NAME} from {@code source} into
|
|
|
|
|
* {@link LauncherSettings.Favorites#TMP_TABLE}, run the grid size migration algorithm
|
2020-03-17 18:28:38 -07:00
|
|
|
* to migrate the later to the former, and load the workspace from the default
|
2021-08-02 12:23:44 -07:00
|
|
|
* {@link LauncherSettings.Favorites#TABLE_NAME}.
|
2020-01-27 13:44:06 -08:00
|
|
|
*
|
|
|
|
|
* @return false if the migration failed.
|
|
|
|
|
*/
|
2023-05-12 23:07:25 +00:00
|
|
|
public static boolean migrateGridIfNeeded(
|
|
|
|
|
@NonNull Context context,
|
|
|
|
|
@NonNull InvariantDeviceProfile idp,
|
|
|
|
|
@NonNull DatabaseHelper target,
|
|
|
|
|
@NonNull SQLiteDatabase source) {
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2022-03-08 21:48:34 +00:00
|
|
|
DeviceGridState srcDeviceState = new DeviceGridState(context);
|
|
|
|
|
DeviceGridState destDeviceState = new DeviceGridState(idp);
|
|
|
|
|
if (!needsToMigrate(srcDeviceState, destDeviceState)) {
|
2020-02-06 16:37:16 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
2023-05-12 23:07:25 +00:00
|
|
|
copyTable(source, TABLE_NAME, target.getWritableDatabase(), TMP_TABLE, context);
|
2020-02-06 16:37:16 -08:00
|
|
|
|
|
|
|
|
HashSet<String> validPackages = getValidPackages(context);
|
|
|
|
|
long migrationStartTime = System.currentTimeMillis();
|
2023-05-12 23:07:25 +00:00
|
|
|
try (SQLiteTransaction t = new SQLiteTransaction(target.getWritableDatabase())) {
|
|
|
|
|
DbReader srcReader = new DbReader(t.getDb(), TMP_TABLE, context, validPackages);
|
|
|
|
|
DbReader destReader = new DbReader(t.getDb(), TABLE_NAME, context, validPackages);
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2022-03-08 21:48:34 +00:00
|
|
|
Point targetSize = new Point(destDeviceState.getColumns(), destDeviceState.getRows());
|
2023-05-12 23:07:25 +00:00
|
|
|
migrate(target, srcReader, destReader, destDeviceState.getNumHotseat(),
|
2022-11-21 12:14:16 -08:00
|
|
|
targetSize, srcDeviceState, destDeviceState);
|
2023-05-12 23:07:25 +00:00
|
|
|
dropTable(t.getDb(), TMP_TABLE);
|
2020-02-06 16:37:16 -08:00
|
|
|
t.commit();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error during grid migration", e);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
Log.v(TAG, "Workspace migration completed in "
|
|
|
|
|
+ (System.currentTimeMillis() - migrationStartTime));
|
|
|
|
|
|
2023-05-25 19:20:10 -07:00
|
|
|
// Save current configuration, so that the migration does not run again.
|
|
|
|
|
destDeviceState.writeToPrefs(context);
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 12:14:16 -08:00
|
|
|
public static boolean migrate(
|
2023-05-12 23:07:25 +00:00
|
|
|
@NonNull DatabaseHelper helper,
|
2022-11-21 12:14:16 -08:00
|
|
|
@NonNull final DbReader srcReader, @NonNull final DbReader destReader,
|
|
|
|
|
final int destHotseatSize, @NonNull final Point targetSize,
|
|
|
|
|
@NonNull final DeviceGridState srcDeviceState,
|
|
|
|
|
@NonNull final DeviceGridState destDeviceState) {
|
|
|
|
|
|
2022-11-21 15:50:30 -08:00
|
|
|
final List<DbEntry> srcHotseatItems = srcReader.loadHotseatEntries();
|
|
|
|
|
final List<DbEntry> srcWorkspaceItems = srcReader.loadAllWorkspaceEntries();
|
|
|
|
|
final List<DbEntry> dstHotseatItems = destReader.loadHotseatEntries();
|
|
|
|
|
final List<DbEntry> dstWorkspaceItems = destReader.loadAllWorkspaceEntries();
|
|
|
|
|
final List<DbEntry> hotseatToBeAdded = new ArrayList<>(1);
|
|
|
|
|
final List<DbEntry> workspaceToBeAdded = new ArrayList<>(1);
|
|
|
|
|
final IntArray toBeRemoved = new IntArray();
|
|
|
|
|
|
|
|
|
|
calcDiff(srcHotseatItems, dstHotseatItems, hotseatToBeAdded, toBeRemoved);
|
|
|
|
|
calcDiff(srcWorkspaceItems, dstWorkspaceItems, workspaceToBeAdded, toBeRemoved);
|
2022-11-21 12:14:16 -08:00
|
|
|
|
|
|
|
|
final int trgX = targetSize.x;
|
|
|
|
|
final int trgY = targetSize.y;
|
|
|
|
|
|
2022-11-21 15:50:30 -08:00
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, "Start migration:"
|
|
|
|
|
+ "\n Source Device:"
|
|
|
|
|
+ srcWorkspaceItems.stream().map(DbEntry::toString).collect(
|
|
|
|
|
Collectors.joining(",\n", "[", "]"))
|
|
|
|
|
+ "\n Target Device:"
|
|
|
|
|
+ dstWorkspaceItems.stream().map(DbEntry::toString).collect(
|
|
|
|
|
Collectors.joining(",\n", "[", "]"))
|
|
|
|
|
+ "\n Removing Items:"
|
|
|
|
|
+ dstWorkspaceItems.stream().filter(entry ->
|
|
|
|
|
toBeRemoved.contains(entry.id)).map(DbEntry::toString).collect(
|
|
|
|
|
Collectors.joining(",\n", "[", "]"))
|
|
|
|
|
+ "\n Adding Workspace Items:"
|
|
|
|
|
+ workspaceToBeAdded.stream().map(DbEntry::toString).collect(
|
|
|
|
|
Collectors.joining(",\n", "[", "]"))
|
|
|
|
|
+ "\n Adding Hotseat Items:"
|
|
|
|
|
+ hotseatToBeAdded.stream().map(DbEntry::toString).collect(
|
|
|
|
|
Collectors.joining(",\n", "[", "]"))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (!toBeRemoved.isEmpty()) {
|
|
|
|
|
removeEntryFromDb(destReader.mDb, destReader.mTableName, toBeRemoved);
|
|
|
|
|
}
|
|
|
|
|
if (hotseatToBeAdded.isEmpty() && workspaceToBeAdded.isEmpty()) {
|
2020-02-06 16:37:16 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 15:47:11 +01:00
|
|
|
// Sort the items by the reading order.
|
2022-11-21 15:50:30 -08:00
|
|
|
Collections.sort(hotseatToBeAdded);
|
|
|
|
|
Collections.sort(workspaceToBeAdded);
|
2022-05-06 15:47:11 +01:00
|
|
|
|
2020-02-06 16:37:16 -08:00
|
|
|
// Migrate hotseat
|
2023-05-12 23:07:25 +00:00
|
|
|
solveHotseatPlacement(helper, destHotseatSize,
|
|
|
|
|
srcReader, destReader, dstHotseatItems, hotseatToBeAdded);
|
2020-02-06 16:37:16 -08:00
|
|
|
|
|
|
|
|
// Migrate workspace.
|
2021-06-23 12:18:08 +02:00
|
|
|
// First we create a collection of the screens
|
|
|
|
|
List<Integer> screens = new ArrayList<>();
|
2022-11-21 12:14:16 -08:00
|
|
|
for (int screenId = 0; screenId <= destReader.mLastScreenId; screenId++) {
|
2021-06-23 12:18:08 +02:00
|
|
|
screens.add(screenId);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 13:45:02 +00:00
|
|
|
boolean preservePages = false;
|
|
|
|
|
if (screens.isEmpty() && FeatureFlags.ENABLE_NEW_MIGRATION_LOGIC.get()) {
|
|
|
|
|
preservePages = destDeviceState.compareTo(srcDeviceState) >= 0
|
|
|
|
|
&& destDeviceState.getColumns() - srcDeviceState.getColumns() <= 2;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 12:18:08 +02:00
|
|
|
// Then we place the items on the screens
|
|
|
|
|
for (int screenId : screens) {
|
2020-02-06 16:37:16 -08:00
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, "Migrating " + screenId);
|
|
|
|
|
}
|
2023-05-12 23:07:25 +00:00
|
|
|
solveGridPlacement(helper, srcReader,
|
|
|
|
|
destReader, screenId, trgX, trgY, workspaceToBeAdded, false);
|
2022-11-21 15:50:30 -08:00
|
|
|
if (workspaceToBeAdded.isEmpty()) {
|
2020-02-06 16:37:16 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 12:18:08 +02:00
|
|
|
// In case the new grid is smaller, there might be some leftover items that don't fit on
|
|
|
|
|
// any of the screens, in this case we add them to new screens until all of them are placed.
|
2022-11-21 12:14:16 -08:00
|
|
|
int screenId = destReader.mLastScreenId + 1;
|
2022-11-21 15:50:30 -08:00
|
|
|
while (!workspaceToBeAdded.isEmpty()) {
|
2023-05-12 23:07:25 +00:00
|
|
|
solveGridPlacement(helper, srcReader,
|
|
|
|
|
destReader, screenId, trgX, trgY, workspaceToBeAdded, preservePages);
|
2020-02-06 16:37:16 -08:00
|
|
|
screenId++;
|
|
|
|
|
}
|
2022-02-10 13:45:02 +00:00
|
|
|
|
2020-01-27 13:44:06 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2022-11-21 15:50:30 -08:00
|
|
|
/**
|
|
|
|
|
* Calculate the differences between {@code src} (denoted by A) and {@code dest}
|
|
|
|
|
* (denoted by B).
|
|
|
|
|
* All DbEntry in A - B will be added to {@code toBeAdded}
|
|
|
|
|
* All DbEntry.id in B - A will be added to {@code toBeRemoved}
|
|
|
|
|
*/
|
|
|
|
|
private static void calcDiff(@NonNull final List<DbEntry> src,
|
|
|
|
|
@NonNull final List<DbEntry> dest, @NonNull final List<DbEntry> toBeAdded,
|
|
|
|
|
@NonNull final IntArray toBeRemoved) {
|
|
|
|
|
src.forEach(entry -> {
|
|
|
|
|
if (!dest.contains(entry)) {
|
|
|
|
|
toBeAdded.add(entry);
|
2020-06-10 10:21:39 -07:00
|
|
|
}
|
2022-11-21 15:50:30 -08:00
|
|
|
});
|
|
|
|
|
dest.forEach(entry -> {
|
|
|
|
|
if (!src.contains(entry)) {
|
|
|
|
|
toBeRemoved.add(entry.id);
|
|
|
|
|
if (entry.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
|
|
|
|
|
entry.mFolderItems.values().forEach(ids -> ids.forEach(toBeRemoved::add));
|
2020-06-10 10:21:39 -07:00
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
2022-11-21 15:50:30 -08:00
|
|
|
});
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-12 23:07:25 +00:00
|
|
|
private static void insertEntryInDb(DatabaseHelper helper, DbEntry entry,
|
2020-06-09 14:11:01 -07:00
|
|
|
String srcTableName, String destTableName) {
|
2023-05-12 23:07:25 +00:00
|
|
|
int id = copyEntryAndUpdate(helper, entry, srcTableName, destTableName);
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2023-12-19 22:44:46 -08:00
|
|
|
if (entry.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER
|
|
|
|
|
|| entry.itemType == LauncherSettings.Favorites.ITEM_TYPE_APP_PAIR) {
|
2020-06-22 15:30:22 -07:00
|
|
|
for (Set<Integer> itemIds : entry.mFolderItems.values()) {
|
|
|
|
|
for (int itemId : itemIds) {
|
2023-05-12 23:07:25 +00:00
|
|
|
copyEntryAndUpdate(helper, itemId, id, srcTableName, destTableName);
|
2020-06-22 15:30:22 -07:00
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-09 14:11:01 -07:00
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2023-05-12 23:07:25 +00:00
|
|
|
private static int copyEntryAndUpdate(DatabaseHelper helper,
|
2020-06-09 14:11:01 -07:00
|
|
|
DbEntry entry, String srcTableName, String destTableName) {
|
2023-05-12 23:07:25 +00:00
|
|
|
return copyEntryAndUpdate(helper, entry, -1, -1, srcTableName, destTableName);
|
2020-06-09 14:11:01 -07:00
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2023-05-12 23:07:25 +00:00
|
|
|
private static int copyEntryAndUpdate(DatabaseHelper helper,
|
2020-06-09 14:11:01 -07:00
|
|
|
int id, int folderId, String srcTableName, String destTableName) {
|
2023-05-12 23:07:25 +00:00
|
|
|
return copyEntryAndUpdate(helper, null, id, folderId, srcTableName, destTableName);
|
2020-06-09 14:11:01 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-12 23:07:25 +00:00
|
|
|
private static int copyEntryAndUpdate(DatabaseHelper helper, DbEntry entry,
|
|
|
|
|
int id, int folderId, String srcTableName, String destTableName) {
|
2020-06-09 14:11:01 -07:00
|
|
|
int newId = -1;
|
2023-05-12 23:07:25 +00:00
|
|
|
Cursor c = helper.getWritableDatabase().query(srcTableName, null,
|
2020-06-09 14:11:01 -07:00
|
|
|
LauncherSettings.Favorites._ID + " = '" + (entry != null ? entry.id : id) + "'",
|
|
|
|
|
null, null, null, null);
|
2020-02-06 16:37:16 -08:00
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
DatabaseUtils.cursorRowToContentValues(c, values);
|
2020-06-09 14:11:01 -07:00
|
|
|
if (entry != null) {
|
|
|
|
|
entry.updateContentValues(values);
|
|
|
|
|
} else {
|
|
|
|
|
values.put(LauncherSettings.Favorites.CONTAINER, folderId);
|
|
|
|
|
}
|
2023-05-12 23:07:25 +00:00
|
|
|
newId = helper.generateNewItemId();
|
2020-06-09 14:11:01 -07:00
|
|
|
values.put(LauncherSettings.Favorites._ID, newId);
|
2023-05-12 23:07:25 +00:00
|
|
|
helper.getWritableDatabase().insert(destTableName, null, values);
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
c.close();
|
2020-06-09 14:11:01 -07:00
|
|
|
return newId;
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
2020-06-09 14:11:01 -07:00
|
|
|
private static void removeEntryFromDb(SQLiteDatabase db, String tableName, IntArray entryIds) {
|
2020-03-17 18:28:38 -07:00
|
|
|
db.delete(tableName,
|
2020-06-09 14:11:01 -07:00
|
|
|
Utilities.createDbSelectionQuery(LauncherSettings.Favorites._ID, entryIds), null);
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static HashSet<String> getValidPackages(Context context) {
|
|
|
|
|
// Initialize list of valid packages. This contain all the packages which are already on
|
|
|
|
|
// the device and packages which are being installed. Any item which doesn't belong to
|
|
|
|
|
// this set is removed.
|
|
|
|
|
// Since the loader removes such items anyway, removing these items here doesn't cause
|
|
|
|
|
// any extra data loss and gives us more free space on the grid for better migration.
|
|
|
|
|
HashSet<String> validPackages = new HashSet<>();
|
|
|
|
|
for (PackageInfo info : context.getPackageManager()
|
|
|
|
|
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)) {
|
|
|
|
|
validPackages.add(info.packageName);
|
|
|
|
|
}
|
|
|
|
|
InstallSessionHelper.INSTANCE.get(context)
|
|
|
|
|
.getActiveSessions().keySet()
|
|
|
|
|
.forEach(packageUserKey -> validPackages.add(packageUserKey.mPackageName));
|
|
|
|
|
return validPackages;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-12 23:07:25 +00:00
|
|
|
private static void solveGridPlacement(@NonNull final DatabaseHelper helper,
|
2022-11-21 12:14:16 -08:00
|
|
|
@NonNull final DbReader srcReader, @NonNull final DbReader destReader,
|
2023-05-12 23:07:25 +00:00
|
|
|
final int screenId, final int trgX, final int trgY,
|
2022-11-21 12:14:16 -08:00
|
|
|
@NonNull final List<DbEntry> sortedItemsToPlace, final boolean matchingScreenIdOnly) {
|
|
|
|
|
final GridOccupancy occupied = new GridOccupancy(trgX, trgY);
|
|
|
|
|
final Point trg = new Point(trgX, trgY);
|
2023-09-25 11:34:56 -07:00
|
|
|
final Point next = new Point(0, screenId == 0
|
|
|
|
|
&& (FeatureFlags.QSB_ON_FIRST_SCREEN
|
2023-10-04 12:11:20 -07:00
|
|
|
&& (!ENABLE_SMARTSPACE_REMOVAL.get() || LauncherPrefs.getPrefs(destReader.mContext)
|
|
|
|
|
.getBoolean(SMARTSPACE_ON_HOME_SCREEN, true))
|
2023-09-25 11:34:56 -07:00
|
|
|
&& !shouldShowFirstPageWidget())
|
2022-11-21 12:14:16 -08:00
|
|
|
? 1 /* smartspace */ : 0);
|
|
|
|
|
List<DbEntry> existedEntries = destReader.mWorkspaceEntriesByScreenId.get(screenId);
|
|
|
|
|
if (existedEntries != null) {
|
|
|
|
|
for (DbEntry entry : existedEntries) {
|
|
|
|
|
occupied.markCells(entry, true);
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-21 12:14:16 -08:00
|
|
|
Iterator<DbEntry> iterator = sortedItemsToPlace.iterator();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
final DbEntry entry = iterator.next();
|
|
|
|
|
if (matchingScreenIdOnly && entry.screenId < screenId) continue;
|
|
|
|
|
if (matchingScreenIdOnly && entry.screenId > screenId) break;
|
|
|
|
|
if (entry.minSpanX > trgX || entry.minSpanY > trgY) {
|
|
|
|
|
iterator.remove();
|
|
|
|
|
continue;
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
2022-11-21 12:14:16 -08:00
|
|
|
if (findPlacementForEntry(entry, next, trg, occupied, screenId)) {
|
2023-05-12 23:07:25 +00:00
|
|
|
insertEntryInDb(helper, entry, srcReader.mTableName, destReader.mTableName);
|
2022-11-21 12:14:16 -08:00
|
|
|
iterator.remove();
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 12:14:16 -08:00
|
|
|
/**
|
|
|
|
|
* Search for the next possible placement of an icon. (mNextStartX, mNextStartY) serves as
|
|
|
|
|
* a memoization of last placement, we can start our search for next placement from there
|
|
|
|
|
* to speed up the search.
|
|
|
|
|
*/
|
|
|
|
|
private static boolean findPlacementForEntry(@NonNull final DbEntry entry,
|
|
|
|
|
@NonNull final Point next, @NonNull final Point trg,
|
|
|
|
|
@NonNull final GridOccupancy occupied, final int screenId) {
|
|
|
|
|
for (int y = next.y; y < trg.y; y++) {
|
|
|
|
|
for (int x = next.x; x < trg.x; x++) {
|
|
|
|
|
boolean fits = occupied.isRegionVacant(x, y, entry.spanX, entry.spanY);
|
|
|
|
|
boolean minFits = occupied.isRegionVacant(x, y, entry.minSpanX,
|
|
|
|
|
entry.minSpanY);
|
|
|
|
|
if (minFits) {
|
|
|
|
|
entry.spanX = entry.minSpanX;
|
|
|
|
|
entry.spanY = entry.minSpanY;
|
|
|
|
|
}
|
|
|
|
|
if (fits || minFits) {
|
|
|
|
|
entry.screenId = screenId;
|
|
|
|
|
entry.cellX = x;
|
|
|
|
|
entry.cellY = y;
|
|
|
|
|
occupied.markCells(entry, true);
|
|
|
|
|
next.set(x + entry.spanX, y);
|
|
|
|
|
return true;
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-21 12:14:16 -08:00
|
|
|
next.set(0, next.y);
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
2022-11-21 12:14:16 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2023-05-12 23:07:25 +00:00
|
|
|
private static void solveHotseatPlacement(
|
|
|
|
|
@NonNull final DatabaseHelper helper, final int hotseatSize,
|
2022-11-21 12:14:16 -08:00
|
|
|
@NonNull final DbReader srcReader, @NonNull final DbReader destReader,
|
|
|
|
|
@NonNull final List<DbEntry> placedHotseatItems,
|
|
|
|
|
@NonNull final List<DbEntry> itemsToPlace) {
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2022-11-21 12:14:16 -08:00
|
|
|
final boolean[] occupied = new boolean[hotseatSize];
|
|
|
|
|
for (DbEntry entry : placedHotseatItems) {
|
|
|
|
|
occupied[entry.screenId] = true;
|
|
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
|
2022-11-21 12:14:16 -08:00
|
|
|
for (int i = 0; i < occupied.length; i++) {
|
|
|
|
|
if (!occupied[i] && !itemsToPlace.isEmpty()) {
|
|
|
|
|
DbEntry entry = itemsToPlace.remove(0);
|
|
|
|
|
entry.screenId = i;
|
|
|
|
|
// These values does not affect the item position, but we should set them
|
|
|
|
|
// to something other than -1.
|
|
|
|
|
entry.cellX = i;
|
|
|
|
|
entry.cellY = 0;
|
2023-05-12 23:07:25 +00:00
|
|
|
insertEntryInDb(helper, entry, srcReader.mTableName, destReader.mTableName);
|
2022-11-21 12:14:16 -08:00
|
|
|
occupied[entry.screenId] = true;
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static class DbReader {
|
|
|
|
|
|
|
|
|
|
private final SQLiteDatabase mDb;
|
|
|
|
|
private final String mTableName;
|
|
|
|
|
private final Context mContext;
|
2022-02-10 13:45:02 +00:00
|
|
|
private final Set<String> mValidPackages;
|
2020-02-06 16:37:16 -08:00
|
|
|
private int mLastScreenId = -1;
|
|
|
|
|
|
2020-06-03 01:07:26 -07:00
|
|
|
private final Map<Integer, ArrayList<DbEntry>> mWorkspaceEntriesByScreenId =
|
|
|
|
|
new ArrayMap<>();
|
2020-02-06 16:37:16 -08:00
|
|
|
|
|
|
|
|
DbReader(SQLiteDatabase db, String tableName, Context context,
|
2022-02-10 13:45:02 +00:00
|
|
|
Set<String> validPackages) {
|
2020-02-06 16:37:16 -08:00
|
|
|
mDb = db;
|
|
|
|
|
mTableName = tableName;
|
|
|
|
|
mContext = context;
|
|
|
|
|
mValidPackages = validPackages;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 12:14:16 -08:00
|
|
|
protected List<DbEntry> loadHotseatEntries() {
|
|
|
|
|
final List<DbEntry> hotseatEntries = new ArrayList<>();
|
2020-02-06 16:37:16 -08:00
|
|
|
Cursor c = queryWorkspace(
|
|
|
|
|
new String[]{
|
|
|
|
|
LauncherSettings.Favorites._ID, // 0
|
|
|
|
|
LauncherSettings.Favorites.ITEM_TYPE, // 1
|
|
|
|
|
LauncherSettings.Favorites.INTENT, // 2
|
|
|
|
|
LauncherSettings.Favorites.SCREEN}, // 3
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER + " = "
|
|
|
|
|
+ LauncherSettings.Favorites.CONTAINER_HOTSEAT);
|
|
|
|
|
|
|
|
|
|
final int indexId = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
|
|
|
|
|
final int indexItemType = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
|
|
|
|
|
final int indexIntent = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
|
|
|
|
|
final int indexScreen = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
|
|
|
|
|
|
|
|
|
|
IntArray entriesToRemove = new IntArray();
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
DbEntry entry = new DbEntry();
|
|
|
|
|
entry.id = c.getInt(indexId);
|
|
|
|
|
entry.itemType = c.getInt(indexItemType);
|
|
|
|
|
entry.screenId = c.getInt(indexScreen);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// calculate weight
|
|
|
|
|
switch (entry.itemType) {
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: {
|
|
|
|
|
entry.mIntent = c.getString(indexIntent);
|
|
|
|
|
verifyIntent(c.getString(indexIntent));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
|
|
|
|
|
int total = getFolderItemsCount(entry);
|
|
|
|
|
if (total == 0) {
|
|
|
|
|
throw new Exception("Folder is empty");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-09-21 22:04:53 -07:00
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_APP_PAIR: {
|
|
|
|
|
int total = getFolderItemsCount(entry);
|
|
|
|
|
if (total != 2) {
|
|
|
|
|
throw new Exception("App pair contains fewer or more than 2 items");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
default:
|
|
|
|
|
throw new Exception("Invalid item type");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, "Removing item " + entry.id, e);
|
|
|
|
|
}
|
|
|
|
|
entriesToRemove.add(entry.id);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-21 12:14:16 -08:00
|
|
|
hotseatEntries.add(entry);
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
2020-03-17 18:28:38 -07:00
|
|
|
removeEntryFromDb(mDb, mTableName, entriesToRemove);
|
2020-02-06 16:37:16 -08:00
|
|
|
c.close();
|
2022-11-21 12:14:16 -08:00
|
|
|
return hotseatEntries;
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
2022-11-21 12:14:16 -08:00
|
|
|
protected List<DbEntry> loadAllWorkspaceEntries() {
|
|
|
|
|
final List<DbEntry> workspaceEntries = new ArrayList<>();
|
2020-02-06 16:37:16 -08:00
|
|
|
Cursor c = queryWorkspace(
|
|
|
|
|
new String[]{
|
|
|
|
|
LauncherSettings.Favorites._ID, // 0
|
|
|
|
|
LauncherSettings.Favorites.ITEM_TYPE, // 1
|
|
|
|
|
LauncherSettings.Favorites.SCREEN, // 2
|
|
|
|
|
LauncherSettings.Favorites.CELLX, // 3
|
|
|
|
|
LauncherSettings.Favorites.CELLY, // 4
|
|
|
|
|
LauncherSettings.Favorites.SPANX, // 5
|
|
|
|
|
LauncherSettings.Favorites.SPANY, // 6
|
|
|
|
|
LauncherSettings.Favorites.INTENT, // 7
|
|
|
|
|
LauncherSettings.Favorites.APPWIDGET_PROVIDER, // 8
|
|
|
|
|
LauncherSettings.Favorites.APPWIDGET_ID}, // 9
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER + " = "
|
|
|
|
|
+ LauncherSettings.Favorites.CONTAINER_DESKTOP);
|
|
|
|
|
final int indexId = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
|
|
|
|
|
final int indexItemType = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
|
|
|
|
|
final int indexScreen = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
|
|
|
|
|
final int indexCellX = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
|
|
|
|
|
final int indexCellY = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
|
|
|
|
|
final int indexSpanX = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
|
|
|
|
|
final int indexSpanY = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
|
|
|
|
|
final int indexIntent = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
|
|
|
|
|
final int indexAppWidgetProvider = c.getColumnIndexOrThrow(
|
|
|
|
|
LauncherSettings.Favorites.APPWIDGET_PROVIDER);
|
|
|
|
|
final int indexAppWidgetId = c.getColumnIndexOrThrow(
|
|
|
|
|
LauncherSettings.Favorites.APPWIDGET_ID);
|
|
|
|
|
|
|
|
|
|
IntArray entriesToRemove = new IntArray();
|
|
|
|
|
WidgetManagerHelper widgetManagerHelper = new WidgetManagerHelper(mContext);
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
DbEntry entry = new DbEntry();
|
|
|
|
|
entry.id = c.getInt(indexId);
|
|
|
|
|
entry.itemType = c.getInt(indexItemType);
|
|
|
|
|
entry.screenId = c.getInt(indexScreen);
|
|
|
|
|
mLastScreenId = Math.max(mLastScreenId, entry.screenId);
|
|
|
|
|
entry.cellX = c.getInt(indexCellX);
|
|
|
|
|
entry.cellY = c.getInt(indexCellY);
|
|
|
|
|
entry.spanX = c.getInt(indexSpanX);
|
|
|
|
|
entry.spanY = c.getInt(indexSpanY);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// calculate weight
|
|
|
|
|
switch (entry.itemType) {
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: {
|
|
|
|
|
entry.mIntent = c.getString(indexIntent);
|
|
|
|
|
verifyIntent(entry.mIntent);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: {
|
|
|
|
|
entry.mProvider = c.getString(indexAppWidgetProvider);
|
|
|
|
|
ComponentName cn = ComponentName.unflattenFromString(entry.mProvider);
|
|
|
|
|
verifyPackage(cn.getPackageName());
|
|
|
|
|
|
|
|
|
|
int widgetId = c.getInt(indexAppWidgetId);
|
2023-08-03 17:03:59 -07:00
|
|
|
LauncherAppWidgetProviderInfo pInfo = widgetManagerHelper
|
|
|
|
|
.getLauncherAppWidgetInfo(widgetId, cn);
|
2020-02-06 16:37:16 -08:00
|
|
|
Point spans = null;
|
|
|
|
|
if (pInfo != null) {
|
|
|
|
|
spans = pInfo.getMinSpans();
|
|
|
|
|
}
|
|
|
|
|
if (spans != null) {
|
|
|
|
|
entry.minSpanX = spans.x > 0 ? spans.x : entry.spanX;
|
|
|
|
|
entry.minSpanY = spans.y > 0 ? spans.y : entry.spanY;
|
|
|
|
|
} else {
|
|
|
|
|
// Assume that the widget be resized down to 2x2
|
|
|
|
|
entry.minSpanX = entry.minSpanY = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: {
|
|
|
|
|
int total = getFolderItemsCount(entry);
|
|
|
|
|
if (total == 0) {
|
|
|
|
|
throw new Exception("Folder is empty");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-09-21 22:04:53 -07:00
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_APP_PAIR: {
|
|
|
|
|
int total = getFolderItemsCount(entry);
|
|
|
|
|
if (total != 2) {
|
|
|
|
|
throw new Exception("App pair contains fewer or more than 2 items");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
default:
|
|
|
|
|
throw new Exception("Invalid item type");
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, "Removing item " + entry.id, e);
|
|
|
|
|
}
|
|
|
|
|
entriesToRemove.add(entry.id);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-21 12:14:16 -08:00
|
|
|
workspaceEntries.add(entry);
|
2020-06-03 01:07:26 -07:00
|
|
|
if (!mWorkspaceEntriesByScreenId.containsKey(entry.screenId)) {
|
|
|
|
|
mWorkspaceEntriesByScreenId.put(entry.screenId, new ArrayList<>());
|
|
|
|
|
}
|
|
|
|
|
mWorkspaceEntriesByScreenId.get(entry.screenId).add(entry);
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
2020-03-17 18:28:38 -07:00
|
|
|
removeEntryFromDb(mDb, mTableName, entriesToRemove);
|
2020-02-06 16:37:16 -08:00
|
|
|
c.close();
|
2022-11-21 12:14:16 -08:00
|
|
|
return workspaceEntries;
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int getFolderItemsCount(DbEntry entry) {
|
|
|
|
|
Cursor c = queryWorkspace(
|
|
|
|
|
new String[]{LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT},
|
|
|
|
|
LauncherSettings.Favorites.CONTAINER + " = " + entry.id);
|
|
|
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
try {
|
2020-06-09 14:11:01 -07:00
|
|
|
int id = c.getInt(0);
|
2020-02-06 16:37:16 -08:00
|
|
|
String intent = c.getString(1);
|
|
|
|
|
verifyIntent(intent);
|
|
|
|
|
total++;
|
2020-06-22 15:30:22 -07:00
|
|
|
if (!entry.mFolderItems.containsKey(intent)) {
|
|
|
|
|
entry.mFolderItems.put(intent, new HashSet<>());
|
|
|
|
|
}
|
|
|
|
|
entry.mFolderItems.get(intent).add(id);
|
2020-02-06 16:37:16 -08:00
|
|
|
} catch (Exception e) {
|
2020-03-17 18:28:38 -07:00
|
|
|
removeEntryFromDb(mDb, mTableName, IntArray.wrap(c.getInt(0)));
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Cursor queryWorkspace(String[] columns, String where) {
|
|
|
|
|
return mDb.query(mTableName, columns, where, null, null, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Verifies if the mIntent should be restored. */
|
|
|
|
|
private void verifyIntent(String intentStr)
|
|
|
|
|
throws Exception {
|
|
|
|
|
Intent intent = Intent.parseUri(intentStr, 0);
|
|
|
|
|
if (intent.getComponent() != null) {
|
|
|
|
|
verifyPackage(intent.getComponent().getPackageName());
|
|
|
|
|
} else if (intent.getPackage() != null) {
|
|
|
|
|
// Only verify package if the component was null.
|
|
|
|
|
verifyPackage(intent.getPackage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Verifies if the package should be restored */
|
|
|
|
|
private void verifyPackage(String packageName)
|
|
|
|
|
throws Exception {
|
|
|
|
|
if (!mValidPackages.contains(packageName)) {
|
|
|
|
|
// TODO(b/151468819): Handle promise app icon restoration during grid migration.
|
|
|
|
|
throw new Exception("Package not available");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static class DbEntry extends ItemInfo implements Comparable<DbEntry> {
|
|
|
|
|
|
|
|
|
|
private String mIntent;
|
|
|
|
|
private String mProvider;
|
2020-06-22 15:30:22 -07:00
|
|
|
private Map<String, Set<Integer>> mFolderItems = new HashMap<>();
|
2020-02-06 16:37:16 -08:00
|
|
|
|
|
|
|
|
/** Comparator according to the reading order */
|
|
|
|
|
@Override
|
|
|
|
|
public int compareTo(DbEntry another) {
|
|
|
|
|
if (screenId != another.screenId) {
|
|
|
|
|
return Integer.compare(screenId, another.screenId);
|
|
|
|
|
}
|
|
|
|
|
if (cellY != another.cellY) {
|
2022-03-15 17:11:47 +00:00
|
|
|
return Integer.compare(cellY, another.cellY);
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
return Integer.compare(cellX, another.cellX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
if (this == o) return true;
|
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
DbEntry entry = (DbEntry) o;
|
2022-11-21 15:50:30 -08:00
|
|
|
return Objects.equals(getEntryMigrationId(), entry.getEntryMigrationId());
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
2022-11-21 15:50:30 -08:00
|
|
|
return Objects.hash(getEntryMigrationId());
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateContentValues(ContentValues values) {
|
|
|
|
|
values.put(LauncherSettings.Favorites.SCREEN, screenId);
|
|
|
|
|
values.put(LauncherSettings.Favorites.CELLX, cellX);
|
|
|
|
|
values.put(LauncherSettings.Favorites.CELLY, cellY);
|
|
|
|
|
values.put(LauncherSettings.Favorites.SPANX, spanX);
|
|
|
|
|
values.put(LauncherSettings.Favorites.SPANY, spanY);
|
|
|
|
|
}
|
2022-05-17 13:46:39 -07:00
|
|
|
|
|
|
|
|
/** This id is not used in the DB is only used while doing the migration and it identifies
|
|
|
|
|
* an entry on each workspace. For example two calculator icons would have the same
|
|
|
|
|
* migration id even thought they have different database ids.
|
|
|
|
|
*/
|
|
|
|
|
public String getEntryMigrationId() {
|
|
|
|
|
switch (itemType) {
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
|
2023-09-21 22:04:53 -07:00
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_APP_PAIR:
|
2022-05-17 13:46:39 -07:00
|
|
|
return getFolderMigrationId();
|
|
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
|
|
|
|
|
return mProvider;
|
2022-08-29 16:40:47 -07:00
|
|
|
case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
|
|
|
|
|
final String intentStr = cleanIntentString(mIntent);
|
|
|
|
|
try {
|
|
|
|
|
Intent i = Intent.parseUri(intentStr, 0);
|
|
|
|
|
return Objects.requireNonNull(i.getComponent()).toString();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return intentStr;
|
|
|
|
|
}
|
2022-05-17 13:46:39 -07:00
|
|
|
default:
|
2022-08-29 16:40:47 -07:00
|
|
|
return cleanIntentString(mIntent);
|
2022-05-17 13:46:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-29 16:40:47 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method should return an id that should be the same for two folders containing the
|
|
|
|
|
* same elements.
|
|
|
|
|
*/
|
|
|
|
|
@NonNull
|
|
|
|
|
private String getFolderMigrationId() {
|
|
|
|
|
return mFolderItems.keySet().stream()
|
|
|
|
|
.map(intentString -> mFolderItems.get(intentString).size()
|
|
|
|
|
+ cleanIntentString(intentString))
|
|
|
|
|
.sorted()
|
|
|
|
|
.collect(Collectors.joining(","));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is needed because sourceBounds can change and make the id of two equal items
|
|
|
|
|
* different.
|
|
|
|
|
*/
|
|
|
|
|
@NonNull
|
|
|
|
|
private String cleanIntentString(@NonNull String intentStr) {
|
|
|
|
|
try {
|
|
|
|
|
Intent i = Intent.parseUri(intentStr, 0);
|
|
|
|
|
i.setSourceBounds(null);
|
|
|
|
|
return i.toURI();
|
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
Log.e(TAG, "Unable to parse Intent string", e);
|
|
|
|
|
return intentStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-02-06 16:37:16 -08:00
|
|
|
}
|
2020-01-27 13:44:06 -08:00
|
|
|
}
|