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;
|
|
|
|
|
|
2019-11-29 21:47:29 -08:00
|
|
|
import static com.android.launcher3.model.ModelUtils.filterCurrentWorkspaceItems;
|
2020-05-04 11:14:52 -07:00
|
|
|
import static com.android.launcher3.model.ModelUtils.getMissingHotseatRanks;
|
2019-11-29 21:47:29 -08:00
|
|
|
import static com.android.launcher3.model.ModelUtils.sortWorkspaceItemsSpatially;
|
2019-08-15 14:53:41 -07:00
|
|
|
|
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;
|
2017-06-01 14:26:38 -07:00
|
|
|
import com.android.launcher3.PagedView;
|
2019-08-15 14:53:41 -07:00
|
|
|
import com.android.launcher3.model.BgDataModel.Callbacks;
|
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;
|
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;
|
2017-06-01 14:26:38 -07:00
|
|
|
import com.android.launcher3.util.ViewOnDrawExecutor;
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
public void bindWorkspace() {
|
|
|
|
|
// 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();
|
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());
|
2018-03-02 15:16:12 -08:00
|
|
|
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,
|
|
|
|
|
workspaceItems, appWidgets, 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();
|
|
|
|
|
executeCallbacksTask(c -> c.bindAllApplications(apps), 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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WorkspaceBinder(Callbacks callbacks,
|
|
|
|
|
Executor uiExecutor,
|
|
|
|
|
LauncherAppState app,
|
|
|
|
|
BgDataModel bgDataModel,
|
|
|
|
|
int myBindingId,
|
|
|
|
|
ArrayList<ItemInfo> workspaceItems,
|
|
|
|
|
ArrayList<LauncherAppWidgetInfo> appWidgets,
|
|
|
|
|
IntArray orderedScreenIds) {
|
|
|
|
|
mCallbacks = callbacks;
|
|
|
|
|
mUiExecutor = uiExecutor;
|
|
|
|
|
mApp = app;
|
|
|
|
|
mBgDataModel = bgDataModel;
|
|
|
|
|
mMyBindingId = myBindingId;
|
|
|
|
|
mWorkspaceItems = workspaceItems;
|
|
|
|
|
mAppWidgets = appWidgets;
|
|
|
|
|
mOrderedScreenIds = orderedScreenIds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void bind() {
|
|
|
|
|
final int currentScreen;
|
|
|
|
|
{
|
|
|
|
|
// Create an anonymous scope to calculate currentScreen as it has to be a
|
|
|
|
|
// final variable.
|
|
|
|
|
int currScreen = mCallbacks.getPageToBindSynchronously();
|
|
|
|
|
if (currScreen >= mOrderedScreenIds.size()) {
|
|
|
|
|
// There may be no workspace screens (just hotseat items and an empty page).
|
|
|
|
|
currScreen = PagedView.INVALID_PAGE;
|
|
|
|
|
}
|
|
|
|
|
currentScreen = currScreen;
|
|
|
|
|
}
|
|
|
|
|
final boolean validFirstPage = currentScreen >= 0;
|
|
|
|
|
final int currentScreenId =
|
|
|
|
|
validFirstPage ? mOrderedScreenIds.get(currentScreen) : INVALID_SCREEN_ID;
|
|
|
|
|
|
|
|
|
|
// 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<>();
|
|
|
|
|
|
|
|
|
|
filterCurrentWorkspaceItems(currentScreenId, mWorkspaceItems, currentWorkspaceItems,
|
|
|
|
|
otherWorkspaceItems);
|
|
|
|
|
filterCurrentWorkspaceItems(currentScreenId, mAppWidgets, currentAppWidgets,
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
Executor mainExecutor = mUiExecutor;
|
|
|
|
|
// Load items on the current page.
|
|
|
|
|
bindWorkspaceItems(currentWorkspaceItems, mainExecutor);
|
|
|
|
|
bindAppWidgets(currentAppWidgets, mainExecutor);
|
2020-05-04 11:14:52 -07:00
|
|
|
|
|
|
|
|
// Locate available spots for prediction using currentWorkspaceItems
|
|
|
|
|
IntArray gaps = getMissingHotseatRanks(currentWorkspaceItems, idp.numHotseatIcons);
|
|
|
|
|
bindPredictedItems(gaps, mainExecutor);
|
2020-01-05 15:35:29 +05:30
|
|
|
// In case of validFirstPage, only bind the first screen, and defer binding the
|
|
|
|
|
// remaining screens after first onDraw (and an optional the fade animation whichever
|
|
|
|
|
// happens later).
|
|
|
|
|
// This ensures that the first screen is immediately visible (eg. during rotation)
|
|
|
|
|
// In case of !validFirstPage, bind all pages one after other.
|
|
|
|
|
final Executor deferredExecutor =
|
|
|
|
|
validFirstPage ? new ViewOnDrawExecutor() : mainExecutor;
|
|
|
|
|
|
|
|
|
|
executeCallbacksTask(c -> c.finishFirstPageBind(
|
|
|
|
|
validFirstPage ? (ViewOnDrawExecutor) deferredExecutor : null), mainExecutor);
|
|
|
|
|
|
|
|
|
|
bindWorkspaceItems(otherWorkspaceItems, deferredExecutor);
|
|
|
|
|
bindAppWidgets(otherAppWidgets, deferredExecutor);
|
|
|
|
|
// Tell the workspace that we're done binding items
|
|
|
|
|
executeCallbacksTask(c -> c.finishBindingItems(currentScreen), deferredExecutor);
|
|
|
|
|
|
|
|
|
|
if (validFirstPage) {
|
|
|
|
|
executeCallbacksTask(c -> {
|
|
|
|
|
// We are loading synchronously, which means, some of the pages will be
|
|
|
|
|
// bound after first draw. Inform the mCallbacks that page binding is
|
|
|
|
|
// not complete, and schedule the remaining pages.
|
|
|
|
|
c.onPageBoundSynchronously(currentScreen);
|
|
|
|
|
c.executeOnNextDraw((ViewOnDrawExecutor) deferredExecutor);
|
|
|
|
|
|
|
|
|
|
}, mUiExecutor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 11:14:52 -07:00
|
|
|
private void bindPredictedItems(IntArray ranks, final Executor executor) {
|
2020-05-11 19:48:59 -07:00
|
|
|
ArrayList<AppInfo> items = new ArrayList<>(mBgDataModel.cachedPredictedItems);
|
|
|
|
|
executeCallbacksTask(c -> c.bindPredictedItems(items, ranks), executor);
|
2020-05-04 11:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
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
|
|
|
}
|