2022-06-13 14:38:43 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 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.celllayout;
|
|
|
|
|
|
2023-05-17 12:44:03 -07:00
|
|
|
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
|
|
|
|
|
|
2023-05-25 19:20:10 -07:00
|
|
|
import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
|
2022-06-13 14:38:43 -07:00
|
|
|
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
|
|
|
|
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
|
2023-05-25 19:20:10 -07:00
|
|
|
import static com.android.launcher3.util.TestUtil.runOnExecutorSync;
|
2022-06-13 14:38:43 -07:00
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
2023-05-17 12:44:03 -07:00
|
|
|
import androidx.test.uiautomator.UiDevice;
|
|
|
|
|
|
2022-06-13 14:38:43 -07:00
|
|
|
import com.android.launcher3.LauncherAppState;
|
2023-05-25 19:20:10 -07:00
|
|
|
import com.android.launcher3.LauncherModel;
|
2022-06-13 14:38:43 -07:00
|
|
|
import com.android.launcher3.LauncherSettings;
|
2023-05-25 19:20:10 -07:00
|
|
|
import com.android.launcher3.model.BgDataModel.Callbacks;
|
|
|
|
|
import com.android.launcher3.model.ModelDbController;
|
2023-06-15 02:57:38 +01:00
|
|
|
import com.android.launcher3.model.data.FolderInfo;
|
2022-06-13 14:38:43 -07:00
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
2023-05-17 12:44:03 -07:00
|
|
|
import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
|
|
|
|
|
import com.android.launcher3.tapl.LauncherInstrumentation;
|
2022-06-13 14:38:43 -07:00
|
|
|
import com.android.launcher3.util.ContentWriter;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2023-05-25 19:20:10 -07:00
|
|
|
import java.util.function.Supplier;
|
2022-06-13 14:38:43 -07:00
|
|
|
|
|
|
|
|
public class FavoriteItemsTransaction {
|
2023-05-25 19:20:10 -07:00
|
|
|
private ArrayList<Supplier<ItemInfo>> mItemsToSubmit;
|
2022-06-13 14:38:43 -07:00
|
|
|
private Context mContext;
|
|
|
|
|
|
2023-05-25 19:20:10 -07:00
|
|
|
public FavoriteItemsTransaction(Context context) {
|
2022-06-13 14:38:43 -07:00
|
|
|
mItemsToSubmit = new ArrayList<>();
|
|
|
|
|
mContext = context;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 19:20:10 -07:00
|
|
|
public FavoriteItemsTransaction addItem(Supplier<ItemInfo> itemInfo) {
|
2022-06-13 14:38:43 -07:00
|
|
|
this.mItemsToSubmit.add(itemInfo);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Commits all the ItemInfo into the database of Favorites
|
|
|
|
|
**/
|
2023-05-25 19:20:10 -07:00
|
|
|
public void commit() {
|
|
|
|
|
LauncherModel model = LauncherAppState.getInstance(mContext).getModel();
|
|
|
|
|
// Load the model once so that there is no pending migration:
|
|
|
|
|
loadModelSync(model);
|
|
|
|
|
|
|
|
|
|
runOnExecutorSync(MODEL_EXECUTOR, () -> {
|
|
|
|
|
ModelDbController controller = model.getModelDbController();
|
|
|
|
|
// Migrate any previous data so that the DB state is correct
|
|
|
|
|
controller.tryMigrateDB();
|
|
|
|
|
|
|
|
|
|
// Create DB again to load fresh data
|
|
|
|
|
controller.createEmptyDB();
|
|
|
|
|
controller.clearEmptyDbFlag();
|
|
|
|
|
|
|
|
|
|
// Add new data
|
2023-05-17 12:44:03 -07:00
|
|
|
try (SQLiteTransaction transaction = controller.newTransaction()) {
|
|
|
|
|
int count = mItemsToSubmit.size();
|
2023-06-15 02:57:38 +01:00
|
|
|
ArrayList<ItemInfo> containerItems = new ArrayList<>();
|
2023-05-17 12:44:03 -07:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
ContentWriter writer = new ContentWriter(mContext);
|
2023-06-15 02:57:38 +01:00
|
|
|
ItemInfo item = mItemsToSubmit.get(i).get();
|
|
|
|
|
|
|
|
|
|
if (item.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) {
|
|
|
|
|
FolderInfo folderInfo = (FolderInfo) item;
|
|
|
|
|
for (ItemInfo itemInfo : folderInfo.contents) {
|
|
|
|
|
itemInfo.container = i;
|
|
|
|
|
containerItems.add(itemInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.onAddToDatabase(writer);
|
2023-05-17 12:44:03 -07:00
|
|
|
writer.put(LauncherSettings.Favorites._ID, i);
|
|
|
|
|
controller.insert(TABLE_NAME, writer.getValues(mContext));
|
|
|
|
|
}
|
2023-06-15 02:57:38 +01:00
|
|
|
|
|
|
|
|
for (int i = 0; i < containerItems.size(); i++) {
|
|
|
|
|
ContentWriter writer = new ContentWriter(mContext);
|
|
|
|
|
ItemInfo item = containerItems.get(i);
|
|
|
|
|
item.onAddToDatabase(writer);
|
|
|
|
|
writer.put(LauncherSettings.Favorites._ID, count + i);
|
|
|
|
|
controller.insert(TABLE_NAME, writer.getValues(mContext));
|
|
|
|
|
}
|
2023-05-17 12:44:03 -07:00
|
|
|
transaction.commit();
|
2023-05-25 19:20:10 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Reload model
|
|
|
|
|
runOnExecutorSync(MAIN_EXECUTOR, model::forceReload);
|
|
|
|
|
loadModelSync(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadModelSync(LauncherModel model) {
|
|
|
|
|
Callbacks mockCb = new Callbacks() { };
|
|
|
|
|
runOnExecutorSync(MAIN_EXECUTOR, () -> model.addCallbacksAndLoad(mockCb));
|
|
|
|
|
runOnExecutorSync(MODEL_EXECUTOR, () -> { });
|
|
|
|
|
|
|
|
|
|
runOnExecutorSync(MAIN_EXECUTOR, () -> { });
|
|
|
|
|
runOnExecutorSync(MAIN_EXECUTOR, () -> model.removeCallbacks(mockCb));
|
2022-06-13 14:38:43 -07:00
|
|
|
}
|
2023-05-17 12:44:03 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Commits the transaction and waits for home load
|
|
|
|
|
*/
|
|
|
|
|
public void commitAndLoadHome(LauncherInstrumentation inst) {
|
|
|
|
|
commit();
|
|
|
|
|
|
|
|
|
|
// Launch the home activity
|
|
|
|
|
UiDevice.getInstance(getInstrumentation()).pressHome();
|
|
|
|
|
inst.waitForLauncherInitialized();
|
|
|
|
|
}
|
2022-06-13 14:38:43 -07:00
|
|
|
}
|