2017-06-01 14:26:38 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017 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;
|
|
|
|
|
|
2021-06-23 12:36:18 -07:00
|
|
|
import static com.android.launcher3.model.ItemInstallQueue.FLAG_LOADER_RUNNING;
|
2019-11-29 21:47:29 -08:00
|
|
|
import static com.android.launcher3.model.ModelUtils.filterCurrentWorkspaceItems;
|
|
|
|
|
import static com.android.launcher3.model.ModelUtils.sortWorkspaceItemsSpatially;
|
2021-06-17 15:15:46 -07:00
|
|
|
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
|
2019-08-15 14:53:41 -07:00
|
|
|
|
2021-06-17 15:15:46 -07:00
|
|
|
import android.os.Process;
|
2017-06-01 14:26:38 -07:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
|
|
|
|
import com.android.launcher3.LauncherAppState;
|
2018-12-27 13:28:08 -08:00
|
|
|
import com.android.launcher3.LauncherModel.CallbackTask;
|
2019-08-15 14:53:41 -07:00
|
|
|
import com.android.launcher3.model.BgDataModel.Callbacks;
|
2020-07-29 15:03:46 -07:00
|
|
|
import com.android.launcher3.model.BgDataModel.FixedContainerItems;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.AppInfo;
|
|
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
|
|
|
|
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
|
2018-10-04 15:11:00 -07:00
|
|
|
import com.android.launcher3.util.IntArray;
|
2021-05-14 12:21:30 +02:00
|
|
|
import com.android.launcher3.util.IntSet;
|
2020-01-05 15:35:29 +05:30
|
|
|
import com.android.launcher3.util.LooperExecutor;
|
2017-06-06 14:33:18 -07:00
|
|
|
import com.android.launcher3.util.LooperIdleLock;
|
2021-06-17 15:15:46 -07:00
|
|
|
import com.android.launcher3.util.RunnableList;
|
2017-06-01 14:26:38 -07:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
2020-01-05 15:35:29 +05:30
|
|
|
import java.util.List;
|
2017-06-01 14:26:38 -07:00
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-05 14:26:26 -08:00
|
|
|
* Base Helper class to handle results of {@link com.android.launcher3.model.LoaderTask}.
|
2017-06-01 14:26:38 -07:00
|
|
|
*/
|
2018-11-05 14:26:26 -08:00
|
|
|
public abstract class BaseLoaderResults {
|
2017-06-01 14:26:38 -07:00
|
|
|
|
2018-11-05 14:26:26 -08:00
|
|
|
protected static final String TAG = "LoaderResults";
|
|
|
|
|
protected static final int INVALID_SCREEN_ID = -1;
|
2017-06-01 14:26:38 -07:00
|
|
|
private static final int ITEMS_CHUNK = 6; // batch size for the workspace icons
|
|
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
protected final LooperExecutor mUiExecutor;
|
2017-06-01 14:26:38 -07:00
|
|
|
|
2018-11-05 14:26:26 -08:00
|
|
|
protected final LauncherAppState mApp;
|
|
|
|
|
protected final BgDataModel mBgDataModel;
|
2017-06-01 14:26:38 -07:00
|
|
|
private final AllAppsList mBgAllAppsList;
|
2017-06-02 13:46:55 -07:00
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
private final Callbacks[] mCallbacksList;
|
2017-06-01 14:26:38 -07:00
|
|
|
|
2019-03-06 15:38:32 -08:00
|
|
|
private int mMyBindingId;
|
|
|
|
|
|
2018-11-05 14:26:26 -08:00
|
|
|
public BaseLoaderResults(LauncherAppState app, BgDataModel dataModel,
|
2020-01-05 15:35:29 +05:30
|
|
|
AllAppsList allAppsList, Callbacks[] callbacksList, LooperExecutor uiExecutor) {
|
|
|
|
|
mUiExecutor = uiExecutor;
|
2017-06-01 14:26:38 -07:00
|
|
|
mApp = app;
|
|
|
|
|
mBgDataModel = dataModel;
|
|
|
|
|
mBgAllAppsList = allAppsList;
|
2020-01-05 15:35:29 +05:30
|
|
|
mCallbacksList = callbacksList;
|
2017-06-01 14:26:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Binds all loaded data to actual views on the main thread.
|
|
|
|
|
*/
|
2021-06-23 12:36:18 -07:00
|
|
|
public void bindWorkspace(boolean incrementBindId) {
|
2017-06-01 14:26:38 -07:00
|
|
|
// Save a copy of all the bg-thread collections
|
|
|
|
|
ArrayList<ItemInfo> workspaceItems = new ArrayList<>();
|
|
|
|
|
ArrayList<LauncherAppWidgetInfo> appWidgets = new ArrayList<>();
|
2018-10-04 15:11:00 -07:00
|
|
|
final IntArray orderedScreenIds = new IntArray();
|
2020-07-29 15:03:46 -07:00
|
|
|
ArrayList<FixedContainerItems> extraItems = new ArrayList<>();
|
2017-06-01 14:26:38 -07:00
|
|
|
|
|
|
|
|
synchronized (mBgDataModel) {
|
|
|
|
|
workspaceItems.addAll(mBgDataModel.workspaceItems);
|
|
|
|
|
appWidgets.addAll(mBgDataModel.appWidgets);
|
2018-12-27 13:58:25 -08:00
|
|
|
orderedScreenIds.addAll(mBgDataModel.collectWorkspaceScreens());
|
2020-07-29 15:03:46 -07:00
|
|
|
mBgDataModel.extraItems.forEach(extraItems::add);
|
2021-06-23 12:36:18 -07:00
|
|
|
if (incrementBindId) {
|
|
|
|
|
mBgDataModel.lastBindId++;
|
|
|
|
|
}
|
2019-03-06 15:38:32 -08:00
|
|
|
mMyBindingId = mBgDataModel.lastBindId;
|
2017-06-01 14:26:38 -07:00
|
|
|
}
|
|
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
for (Callbacks cb : mCallbacksList) {
|
|
|
|
|
new WorkspaceBinder(cb, mUiExecutor, mApp, mBgDataModel, mMyBindingId,
|
2020-07-29 15:03:46 -07:00
|
|
|
workspaceItems, appWidgets, extraItems, orderedScreenIds).bind();
|
2017-06-01 14:26:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 14:26:26 -08:00
|
|
|
public abstract void bindDeepShortcuts();
|
2017-06-01 14:26:38 -07:00
|
|
|
|
|
|
|
|
public void bindAllApps() {
|
|
|
|
|
// shallow copy
|
2019-07-17 20:35:56 -07:00
|
|
|
AppInfo[] apps = mBgAllAppsList.copyData();
|
2020-06-08 17:06:09 -07:00
|
|
|
int flags = mBgAllAppsList.getFlags();
|
|
|
|
|
executeCallbacksTask(c -> c.bindAllApplications(apps, flags), mUiExecutor);
|
2017-06-01 14:26:38 -07:00
|
|
|
}
|
2017-06-02 13:46:55 -07:00
|
|
|
|
2018-11-05 14:26:26 -08:00
|
|
|
public abstract void bindWidgets();
|
2017-06-06 14:33:18 -07:00
|
|
|
|
2018-12-27 13:28:08 -08:00
|
|
|
protected void executeCallbacksTask(CallbackTask task, Executor executor) {
|
|
|
|
|
executor.execute(() -> {
|
2019-03-06 15:38:32 -08:00
|
|
|
if (mMyBindingId != mBgDataModel.lastBindId) {
|
|
|
|
|
Log.d(TAG, "Too many consecutive reloads, skipping obsolete data-bind");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-05 15:35:29 +05:30
|
|
|
for (Callbacks cb : mCallbacksList) {
|
|
|
|
|
task.execute(cb);
|
2018-12-27 13:28:08 -08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 14:33:18 -07:00
|
|
|
public LooperIdleLock newIdleLock(Object lock) {
|
2020-01-05 15:35:29 +05:30
|
|
|
LooperIdleLock idleLock = new LooperIdleLock(lock, mUiExecutor.getLooper());
|
2019-10-17 22:24:45 -07:00
|
|
|
// If we are not binding or if the main looper is already idle, there is no reason to wait
|
2020-01-05 15:35:29 +05:30
|
|
|
if (mUiExecutor.getLooper().getQueue().isIdle()) {
|
2017-06-06 14:33:18 -07:00
|
|
|
idleLock.queueIdle();
|
|
|
|
|
}
|
|
|
|
|
return idleLock;
|
|
|
|
|
}
|
2020-01-05 15:35:29 +05:30
|
|
|
|
|
|
|
|
private static class WorkspaceBinder {
|
|
|
|
|
|
|
|
|
|
private final Executor mUiExecutor;
|
|
|
|
|
private final Callbacks mCallbacks;
|
|
|
|
|
|
|
|
|
|
private final LauncherAppState mApp;
|
|
|
|
|
private final BgDataModel mBgDataModel;
|
|
|
|
|
|
|
|
|
|
private final int mMyBindingId;
|
|
|
|
|
private final ArrayList<ItemInfo> mWorkspaceItems;
|
|
|
|
|
private final ArrayList<LauncherAppWidgetInfo> mAppWidgets;
|
|
|
|
|
private final IntArray mOrderedScreenIds;
|
2020-07-29 15:03:46 -07:00
|
|
|
private final ArrayList<FixedContainerItems> mExtraItems;
|
2020-01-05 15:35:29 +05:30
|
|
|
|
|
|
|
|
WorkspaceBinder(Callbacks callbacks,
|
|
|
|
|
Executor uiExecutor,
|
|
|
|
|
LauncherAppState app,
|
|
|
|
|
BgDataModel bgDataModel,
|
|
|
|
|
int myBindingId,
|
|
|
|
|
ArrayList<ItemInfo> workspaceItems,
|
|
|
|
|
ArrayList<LauncherAppWidgetInfo> appWidgets,
|
2020-07-29 15:03:46 -07:00
|
|
|
ArrayList<FixedContainerItems> extraItems,
|
2020-01-05 15:35:29 +05:30
|
|
|
IntArray orderedScreenIds) {
|
|
|
|
|
mCallbacks = callbacks;
|
|
|
|
|
mUiExecutor = uiExecutor;
|
|
|
|
|
mApp = app;
|
|
|
|
|
mBgDataModel = bgDataModel;
|
|
|
|
|
mMyBindingId = myBindingId;
|
|
|
|
|
mWorkspaceItems = workspaceItems;
|
|
|
|
|
mAppWidgets = appWidgets;
|
2020-07-29 15:03:46 -07:00
|
|
|
mExtraItems = extraItems;
|
2020-01-05 15:35:29 +05:30
|
|
|
mOrderedScreenIds = orderedScreenIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void bind() {
|
2021-07-15 14:31:58 -07:00
|
|
|
IntSet currentScreenIds = mCallbacks.getPagesToBindSynchronously(mOrderedScreenIds);
|
2020-01-05 15:35:29 +05:30
|
|
|
|
|
|
|
|
// Separate the items that are on the current screen, and all the other remaining items
|
|
|
|
|
ArrayList<ItemInfo> currentWorkspaceItems = new ArrayList<>();
|
|
|
|
|
ArrayList<ItemInfo> otherWorkspaceItems = new ArrayList<>();
|
|
|
|
|
ArrayList<LauncherAppWidgetInfo> currentAppWidgets = new ArrayList<>();
|
|
|
|
|
ArrayList<LauncherAppWidgetInfo> otherAppWidgets = new ArrayList<>();
|
|
|
|
|
|
2021-05-14 12:21:30 +02:00
|
|
|
filterCurrentWorkspaceItems(currentScreenIds, mWorkspaceItems, currentWorkspaceItems,
|
2020-01-05 15:35:29 +05:30
|
|
|
otherWorkspaceItems);
|
2021-05-14 12:21:30 +02:00
|
|
|
filterCurrentWorkspaceItems(currentScreenIds, mAppWidgets, currentAppWidgets,
|
2020-01-05 15:35:29 +05:30
|
|
|
otherAppWidgets);
|
|
|
|
|
final InvariantDeviceProfile idp = mApp.getInvariantDeviceProfile();
|
|
|
|
|
sortWorkspaceItemsSpatially(idp, currentWorkspaceItems);
|
|
|
|
|
sortWorkspaceItemsSpatially(idp, otherWorkspaceItems);
|
|
|
|
|
|
|
|
|
|
// Tell the workspace that we're about to start binding items
|
|
|
|
|
executeCallbacksTask(c -> {
|
|
|
|
|
c.clearPendingBinds();
|
|
|
|
|
c.startBinding();
|
|
|
|
|
}, mUiExecutor);
|
|
|
|
|
|
|
|
|
|
// Bind workspace screens
|
|
|
|
|
executeCallbacksTask(c -> c.bindScreens(mOrderedScreenIds), mUiExecutor);
|
|
|
|
|
|
|
|
|
|
// Load items on the current page.
|
2021-06-17 15:15:46 -07:00
|
|
|
bindWorkspaceItems(currentWorkspaceItems, mUiExecutor);
|
|
|
|
|
bindAppWidgets(currentAppWidgets, mUiExecutor);
|
2020-07-29 15:03:46 -07:00
|
|
|
mExtraItems.forEach(item ->
|
2021-06-17 15:15:46 -07:00
|
|
|
executeCallbacksTask(c -> c.bindExtraContainerItems(item), mUiExecutor));
|
|
|
|
|
|
|
|
|
|
RunnableList pendingTasks = new RunnableList();
|
|
|
|
|
Executor pendingExecutor = pendingTasks::add;
|
|
|
|
|
bindWorkspaceItems(otherWorkspaceItems, pendingExecutor);
|
|
|
|
|
bindAppWidgets(otherAppWidgets, pendingExecutor);
|
2021-07-15 14:31:58 -07:00
|
|
|
executeCallbacksTask(c -> c.finishBindingItems(currentScreenIds), pendingExecutor);
|
2021-06-17 15:15:46 -07:00
|
|
|
pendingExecutor.execute(
|
2021-06-23 12:36:18 -07:00
|
|
|
() -> {
|
|
|
|
|
MODEL_EXECUTOR.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
|
|
|
|
|
ItemInstallQueue.INSTANCE.get(mApp.getContext())
|
|
|
|
|
.resumeModelPush(FLAG_LOADER_RUNNING);
|
|
|
|
|
});
|
2021-06-17 15:15:46 -07:00
|
|
|
|
|
|
|
|
executeCallbacksTask(
|
|
|
|
|
c -> {
|
|
|
|
|
MODEL_EXECUTOR.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
2021-07-15 14:31:58 -07:00
|
|
|
c.onInitialBindComplete(currentScreenIds, pendingTasks);
|
2021-06-17 15:15:46 -07:00
|
|
|
}, mUiExecutor);
|
2020-01-05 15:35:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void bindWorkspaceItems(
|
|
|
|
|
final ArrayList<ItemInfo> workspaceItems, final Executor executor) {
|
|
|
|
|
// Bind the workspace items
|
|
|
|
|
int count = workspaceItems.size();
|
|
|
|
|
for (int i = 0; i < count; i += ITEMS_CHUNK) {
|
|
|
|
|
final int start = i;
|
|
|
|
|
final int chunkSize = (i + ITEMS_CHUNK <= count) ? ITEMS_CHUNK : (count - i);
|
|
|
|
|
executeCallbacksTask(
|
|
|
|
|
c -> c.bindItems(workspaceItems.subList(start, start + chunkSize), false),
|
|
|
|
|
executor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void bindAppWidgets(List<LauncherAppWidgetInfo> appWidgets, Executor executor) {
|
|
|
|
|
// Bind the widgets, one at a time
|
|
|
|
|
int count = appWidgets.size();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
final ItemInfo widget = appWidgets.get(i);
|
|
|
|
|
executeCallbacksTask(
|
|
|
|
|
c -> c.bindItems(Collections.singletonList(widget), false), executor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void executeCallbacksTask(CallbackTask task, Executor executor) {
|
|
|
|
|
executor.execute(() -> {
|
|
|
|
|
if (mMyBindingId != mBgDataModel.lastBindId) {
|
|
|
|
|
Log.d(TAG, "Too many consecutive reloads, skipping obsolete data-bind");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
task.execute(mCallbacks);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-01 14:26:38 -07:00
|
|
|
}
|