2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
import static com.android.launcher3.LauncherAppState.ACTION_FORCE_ROLOAD;
|
2020-02-14 14:15:13 -08:00
|
|
|
import static com.android.launcher3.config.FeatureFlags.IS_STUDIO_BUILD;
|
2020-01-05 15:35:29 +05:30
|
|
|
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
|
|
|
|
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
|
|
|
|
|
|
2019-10-24 15:59:49 -07:00
|
|
|
import android.content.Context;
|
2014-07-08 13:01:29 -07:00
|
|
|
import android.content.Intent;
|
2019-10-02 16:13:34 -07:00
|
|
|
import android.content.pm.LauncherApps;
|
2019-09-20 12:51:37 -07:00
|
|
|
import android.content.pm.PackageInstaller;
|
2019-04-17 18:38:52 -07:00
|
|
|
import android.content.pm.ShortcutInfo;
|
2016-12-15 15:53:17 -08:00
|
|
|
import android.os.UserHandle;
|
2013-11-15 13:05:06 -08:00
|
|
|
import android.text.TextUtils;
|
2010-06-11 17:34:16 -07:00
|
|
|
import android.util.Log;
|
2017-05-04 16:47:11 -07:00
|
|
|
import android.util.Pair;
|
2013-12-13 16:07:45 +01:00
|
|
|
|
2019-08-15 14:53:41 -07:00
|
|
|
import androidx.annotation.Nullable;
|
2019-10-24 15:59:49 -07:00
|
|
|
import androidx.annotation.WorkerThread;
|
2019-08-15 14:53:41 -07:00
|
|
|
|
2019-09-20 12:51:37 -07:00
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
2018-09-21 14:41:05 -07:00
|
|
|
import com.android.launcher3.icons.IconCache;
|
2019-09-06 16:19:11 -07:00
|
|
|
import com.android.launcher3.logging.FileLog;
|
2016-09-09 15:47:55 -07:00
|
|
|
import com.android.launcher3.model.AddWorkspaceItemsTask;
|
2019-07-17 20:35:56 -07:00
|
|
|
import com.android.launcher3.model.AllAppsList;
|
2018-01-31 11:59:00 -08:00
|
|
|
import com.android.launcher3.model.BaseModelUpdateTask;
|
2016-09-01 17:24:47 -07:00
|
|
|
import com.android.launcher3.model.BgDataModel;
|
2019-07-17 20:35:56 -07:00
|
|
|
import com.android.launcher3.model.BgDataModel.Callbacks;
|
2016-09-09 15:47:55 -07:00
|
|
|
import com.android.launcher3.model.CacheDataUpdatedTask;
|
2020-08-12 13:59:27 -07:00
|
|
|
import com.android.launcher3.model.ItemInstallQueue;
|
2017-06-01 14:26:38 -07:00
|
|
|
import com.android.launcher3.model.LoaderResults;
|
2017-06-06 10:46:59 -07:00
|
|
|
import com.android.launcher3.model.LoaderTask;
|
2020-07-27 17:50:33 -07:00
|
|
|
import com.android.launcher3.model.ModelDelegate;
|
2017-02-02 13:52:53 -08:00
|
|
|
import com.android.launcher3.model.ModelWriter;
|
2021-01-12 19:30:02 +00:00
|
|
|
import com.android.launcher3.model.PackageIncrementalDownloadUpdatedTask;
|
2016-09-09 15:47:55 -07:00
|
|
|
import com.android.launcher3.model.PackageInstallStateChangedTask;
|
|
|
|
|
import com.android.launcher3.model.PackageUpdatedTask;
|
|
|
|
|
import com.android.launcher3.model.ShortcutsChangedTask;
|
|
|
|
|
import com.android.launcher3.model.UserLockStateChangedTask;
|
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.WorkspaceItemInfo;
|
2019-09-20 12:51:37 -07:00
|
|
|
import com.android.launcher3.pm.InstallSessionTracker;
|
|
|
|
|
import com.android.launcher3.pm.PackageInstallInfo;
|
2019-12-10 12:19:13 -08:00
|
|
|
import com.android.launcher3.pm.UserCache;
|
2019-12-11 10:00:47 -08:00
|
|
|
import com.android.launcher3.shortcuts.ShortcutRequest;
|
2020-07-09 19:31:40 -07:00
|
|
|
import com.android.launcher3.util.IntSet;
|
2019-09-17 11:51:49 -07:00
|
|
|
import com.android.launcher3.util.ItemInfoMatcher;
|
2017-03-29 15:30:43 -07:00
|
|
|
import com.android.launcher3.util.PackageUserKey;
|
2016-05-17 13:38:54 -07:00
|
|
|
import com.android.launcher3.util.Preconditions;
|
2010-03-04 13:03:17 -08:00
|
|
|
|
2017-01-30 15:11:27 -08:00
|
|
|
import java.io.FileDescriptor;
|
|
|
|
|
import java.io.PrintWriter;
|
2011-07-12 14:19:46 -07:00
|
|
|
import java.util.ArrayList;
|
2012-07-12 17:55:31 -07:00
|
|
|
import java.util.HashSet;
|
2011-07-12 14:19:46 -07:00
|
|
|
import java.util.List;
|
2017-02-17 11:22:34 -08:00
|
|
|
import java.util.concurrent.CancellationException;
|
2015-08-28 15:19:36 -07:00
|
|
|
import java.util.concurrent.Executor;
|
2021-08-06 11:52:10 -07:00
|
|
|
import java.util.function.Consumer;
|
2019-01-25 15:10:18 -08:00
|
|
|
import java.util.function.Supplier;
|
2011-07-12 14:19:46 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* Maintains in-memory state of the Launcher. It is expected that there should be only one
|
|
|
|
|
* LauncherModel object held in a static. Also provide APIs for updating the database state
|
2009-03-09 11:52:14 -07:00
|
|
|
* for the Launcher.
|
2009-03-03 19:32:27 -08:00
|
|
|
*/
|
2019-10-02 16:13:34 -07:00
|
|
|
public class LauncherModel extends LauncherApps.Callback implements InstallSessionTracker.Callback {
|
2014-09-09 10:09:02 -04:00
|
|
|
private static final boolean DEBUG_RECEIVER = false;
|
2014-04-16 13:37:00 -04:00
|
|
|
|
2009-08-17 11:03:03 -04:00
|
|
|
static final String TAG = "Launcher.Model";
|
|
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
private final LauncherAppState mApp;
|
|
|
|
|
private final Object mLock = new Object();
|
|
|
|
|
|
|
|
|
|
private LoaderTask mLoaderTask;
|
|
|
|
|
private boolean mIsLoaderTaskRunning;
|
2009-08-17 11:03:03 -04:00
|
|
|
|
2017-02-17 11:22:34 -08:00
|
|
|
// Indicates whether the current model data is valid or not.
|
|
|
|
|
// We start off with everything not loaded. After that, we assume that
|
2010-06-08 10:54:30 -07:00
|
|
|
// our monitoring of the package manager provides all updates and we never
|
2017-02-17 11:22:34 -08:00
|
|
|
// need to do a requery. This is only ever touched from the loader thread.
|
|
|
|
|
private boolean mModelLoaded;
|
2021-08-27 21:22:17 +00:00
|
|
|
private boolean mModelDestroyed = false;
|
2017-02-06 10:46:24 -08:00
|
|
|
public boolean isModelLoaded() {
|
|
|
|
|
synchronized (mLock) {
|
2021-08-27 21:22:17 +00:00
|
|
|
return mModelLoaded && mLoaderTask == null && !mModelDestroyed;
|
2017-02-06 10:46:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
2010-06-08 10:54:30 -07:00
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
private final ArrayList<Callbacks> mCallbacksList = new ArrayList<>(1);
|
2009-08-17 11:03:03 -04:00
|
|
|
|
2011-04-28 14:59:33 -07:00
|
|
|
// < only access in worker thread >
|
2016-03-03 16:58:55 -08:00
|
|
|
private final AllAppsList mBgAllAppsList;
|
2011-04-28 14:59:33 -07:00
|
|
|
|
2017-06-02 13:46:55 -07:00
|
|
|
/**
|
|
|
|
|
* All the static data should be accessed on the background thread, A lock should be acquired
|
|
|
|
|
* on this object when accessing any data from this model.
|
|
|
|
|
*/
|
2019-12-18 19:29:00 +05:30
|
|
|
private final BgDataModel mBgDataModel = new BgDataModel();
|
2017-06-02 13:46:55 -07:00
|
|
|
|
2020-07-27 17:50:33 -07:00
|
|
|
private final ModelDelegate mModelDelegate;
|
|
|
|
|
|
2016-08-10 16:09:29 -07:00
|
|
|
// Runnable to check if the shortcuts permission has changed.
|
2020-07-27 17:50:33 -07:00
|
|
|
private final Runnable mDataValidationCheck = new Runnable() {
|
2016-08-10 16:09:29 -07:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2020-07-27 17:50:33 -07:00
|
|
|
if (mModelLoaded) {
|
|
|
|
|
mModelDelegate.validateData();
|
2016-08-10 16:09:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-27 17:50:33 -07:00
|
|
|
LauncherModel(Context context, LauncherAppState app, IconCache iconCache, AppFilter appFilter) {
|
2013-06-25 15:13:26 -04:00
|
|
|
mApp = app;
|
2013-10-03 22:31:03 +01:00
|
|
|
mBgAllAppsList = new AllAppsList(iconCache, appFilter);
|
2020-07-27 17:50:33 -07:00
|
|
|
mModelDelegate = ModelDelegate.newInstance(context, app, mBgAllAppsList, mBgDataModel);
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 14:17:41 -07:00
|
|
|
public ModelDelegate getModelDelegate() {
|
|
|
|
|
return mModelDelegate;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-08 16:59:04 -08:00
|
|
|
/**
|
|
|
|
|
* Adds the provided items to the workspace.
|
|
|
|
|
*/
|
2017-10-05 15:57:40 -07:00
|
|
|
public void addAndBindAddedWorkspaceItems(List<Pair<ItemInfo, Object>> itemList) {
|
2020-01-05 15:35:29 +05:30
|
|
|
for (Callbacks cb : getCallbacks()) {
|
|
|
|
|
cb.preAddApps();
|
2018-08-21 11:40:23 -07:00
|
|
|
}
|
2017-10-05 15:57:40 -07:00
|
|
|
enqueueModelUpdateTask(new AddWorkspaceItemsTask(itemList));
|
2013-07-08 17:17:08 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-23 12:36:18 -07:00
|
|
|
public ModelWriter getWriter(boolean hasVerticalHotseat, boolean verifyChanges,
|
|
|
|
|
@Nullable Callbacks owner) {
|
2019-12-18 19:29:00 +05:30
|
|
|
return new ModelWriter(mApp.getContext(), this, mBgDataModel,
|
2021-06-23 12:36:18 -07:00
|
|
|
hasVerticalHotseat, verifyChanges, owner);
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2014-04-30 03:02:21 +01:00
|
|
|
@Override
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackageChanged(String packageName, UserHandle user) {
|
2014-04-30 03:02:21 +01:00
|
|
|
int op = PackageUpdatedTask.OP_UPDATE;
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
|
2014-04-30 03:02:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackageRemoved(String packageName, UserHandle user) {
|
2016-09-01 15:50:36 -07:00
|
|
|
onPackagesRemoved(user, packageName);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackagesRemoved(UserHandle user, String... packages) {
|
2014-04-30 03:02:21 +01:00
|
|
|
int op = PackageUpdatedTask.OP_REMOVE;
|
2020-02-25 15:27:06 -08:00
|
|
|
FileLog.d(TAG, "package removed received " + TextUtils.join(",", packages));
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packages));
|
2014-04-30 03:02:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackageAdded(String packageName, UserHandle user) {
|
2014-04-30 03:02:21 +01:00
|
|
|
int op = PackageUpdatedTask.OP_ADD;
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new PackageUpdatedTask(op, user, packageName));
|
2014-04-30 03:02:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackagesAvailable(String[] packageNames, UserHandle user,
|
2014-04-30 03:02:21 +01:00
|
|
|
boolean replacing) {
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(
|
|
|
|
|
new PackageUpdatedTask(PackageUpdatedTask.OP_UPDATE, user, packageNames));
|
2014-04-30 03:02:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackagesUnavailable(String[] packageNames, UserHandle user,
|
2014-04-30 03:02:21 +01:00
|
|
|
boolean replacing) {
|
|
|
|
|
if (!replacing) {
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new PackageUpdatedTask(
|
|
|
|
|
PackageUpdatedTask.OP_UNAVAILABLE, user, packageNames));
|
2014-04-30 03:02:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 19:50:02 +00:00
|
|
|
@Override
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackagesSuspended(String[] packageNames, UserHandle user) {
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new PackageUpdatedTask(
|
|
|
|
|
PackageUpdatedTask.OP_SUSPEND, user, packageNames));
|
2016-01-21 19:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new PackageUpdatedTask(
|
|
|
|
|
PackageUpdatedTask.OP_UNSUSPEND, user, packageNames));
|
2016-01-21 19:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-12 19:30:02 +00:00
|
|
|
@Override
|
|
|
|
|
public void onPackageLoadingProgressChanged(
|
|
|
|
|
String packageName, UserHandle user, float progress) {
|
|
|
|
|
if (Utilities.ATLEAST_S) {
|
|
|
|
|
enqueueModelUpdateTask(new PackageIncrementalDownloadUpdatedTask(
|
|
|
|
|
packageName, user, progress));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 11:19:39 -07:00
|
|
|
@Override
|
2019-04-17 18:38:52 -07:00
|
|
|
public void onShortcutsChanged(String packageName, List<ShortcutInfo> shortcuts,
|
2016-12-15 15:53:17 -08:00
|
|
|
UserHandle user) {
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, shortcuts, user, true));
|
2016-08-04 12:03:52 -07:00
|
|
|
}
|
|
|
|
|
|
2019-10-24 15:59:49 -07:00
|
|
|
/**
|
|
|
|
|
* Called when the icon for an app changes, outside of package event
|
|
|
|
|
*/
|
|
|
|
|
@WorkerThread
|
|
|
|
|
public void onAppIconChanged(String packageName, UserHandle user) {
|
|
|
|
|
// Update the icon for the calendar package
|
|
|
|
|
Context context = mApp.getContext();
|
|
|
|
|
onPackageChanged(packageName, user);
|
|
|
|
|
|
2019-12-11 10:00:47 -08:00
|
|
|
List<ShortcutInfo> pinnedShortcuts = new ShortcutRequest(context, user)
|
|
|
|
|
.forPackage(packageName).query(ShortcutRequest.PINNED);
|
2019-10-24 15:59:49 -07:00
|
|
|
if (!pinnedShortcuts.isEmpty()) {
|
|
|
|
|
enqueueModelUpdateTask(new ShortcutsChangedTask(packageName, pinnedShortcuts, user,
|
|
|
|
|
false));
|
|
|
|
|
}
|
2016-05-19 11:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-11 18:49:28 -07:00
|
|
|
/**
|
|
|
|
|
* Called when the workspace items have drastically changed
|
|
|
|
|
*/
|
|
|
|
|
public void onWorkspaceUiChanged() {
|
|
|
|
|
MODEL_EXECUTOR.execute(mModelDelegate::workspaceLoadComplete);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 17:50:33 -07:00
|
|
|
/**
|
|
|
|
|
* Called when the model is destroyed
|
|
|
|
|
*/
|
|
|
|
|
public void destroy() {
|
2021-08-27 21:22:17 +00:00
|
|
|
mModelDestroyed = true;
|
2020-07-27 17:50:33 -07:00
|
|
|
MODEL_EXECUTOR.execute(mModelDelegate::destroy);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 16:13:34 -07:00
|
|
|
public void onBroadcastIntent(Intent intent) {
|
2014-04-16 13:37:00 -04:00
|
|
|
if (DEBUG_RECEIVER) Log.d(TAG, "onReceive intent=" + intent);
|
2010-06-17 13:28:48 -04:00
|
|
|
final String action = intent.getAction();
|
2014-04-30 03:02:21 +01:00
|
|
|
if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
|
2011-09-23 17:20:28 -07:00
|
|
|
// If we have changed locale we need to clear out the labels in all apps/workspace.
|
|
|
|
|
forceReload();
|
2016-07-28 12:11:54 -07:00
|
|
|
} else if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
|
|
|
|
|
Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
|
|
|
|
|
Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
|
2016-12-15 15:53:17 -08:00
|
|
|
UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
|
2016-03-18 18:29:24 -07:00
|
|
|
if (user != null) {
|
2016-07-28 12:11:54 -07:00
|
|
|
if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action) ||
|
|
|
|
|
Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new PackageUpdatedTask(
|
|
|
|
|
PackageUpdatedTask.OP_USER_AVAILABILITY_CHANGE, user));
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ACTION_MANAGED_PROFILE_UNAVAILABLE sends the profile back to locked mode, so
|
|
|
|
|
// we need to run the state change task again.
|
|
|
|
|
if (Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action) ||
|
|
|
|
|
Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)) {
|
2020-04-06 18:11:29 -07:00
|
|
|
enqueueModelUpdateTask(new UserLockStateChangedTask(
|
|
|
|
|
user, Intent.ACTION_MANAGED_PROFILE_UNLOCKED.equals(action)));
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|
2016-03-18 18:29:24 -07:00
|
|
|
}
|
2020-02-14 14:15:13 -08:00
|
|
|
} else if (IS_STUDIO_BUILD && ACTION_FORCE_ROLOAD.equals(action)) {
|
2020-01-05 15:35:29 +05:30
|
|
|
for (Callbacks cb : getCallbacks()) {
|
|
|
|
|
if (cb instanceof Launcher) {
|
|
|
|
|
((Launcher) cb).recreate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-29 17:35:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 11:22:34 -08:00
|
|
|
/**
|
|
|
|
|
* Reloads the workspace items from the DB and re-binds the workspace. This should generally
|
|
|
|
|
* not be called as DB updates are automatically followed by UI update
|
|
|
|
|
*/
|
2020-01-05 15:35:29 +05:30
|
|
|
public void forceReload() {
|
2017-02-17 11:22:34 -08:00
|
|
|
synchronized (mLock) {
|
|
|
|
|
// Stop any existing loaders first, so they don't set mModelLoaded to true later
|
2017-06-06 14:33:18 -07:00
|
|
|
stopLoader();
|
2017-02-17 11:22:34 -08:00
|
|
|
mModelLoaded = false;
|
|
|
|
|
}
|
2012-03-21 16:10:31 -07:00
|
|
|
|
2017-12-18 13:49:44 -08:00
|
|
|
// Start the loader if launcher is already running, otherwise the loader will run,
|
|
|
|
|
// the next time launcher starts
|
2020-01-05 15:35:29 +05:30
|
|
|
if (hasCallbacks()) {
|
|
|
|
|
startLoader();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rebinds all existing callbacks with already loaded model
|
|
|
|
|
*/
|
|
|
|
|
public void rebindCallbacks() {
|
|
|
|
|
if (hasCallbacks()) {
|
|
|
|
|
startLoader();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes an existing callback
|
|
|
|
|
*/
|
|
|
|
|
public void removeCallbacks(Callbacks callbacks) {
|
|
|
|
|
synchronized (mCallbacksList) {
|
|
|
|
|
Preconditions.assertUIThread();
|
|
|
|
|
if (mCallbacksList.remove(callbacks)) {
|
|
|
|
|
if (stopLoader()) {
|
|
|
|
|
// Rebind existing callbacks
|
|
|
|
|
startLoader();
|
|
|
|
|
}
|
2018-12-21 11:58:04 -08:00
|
|
|
}
|
2010-10-29 17:35:36 -07:00
|
|
|
}
|
2010-06-17 13:28:48 -04:00
|
|
|
}
|
2009-03-18 17:39:48 -07:00
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
/**
|
|
|
|
|
* Adds a callbacks to receive model updates
|
|
|
|
|
* @return true if workspace load was performed synchronously
|
|
|
|
|
*/
|
|
|
|
|
public boolean addCallbacksAndLoad(Callbacks callbacks) {
|
|
|
|
|
synchronized (mLock) {
|
|
|
|
|
addCallbacks(callbacks);
|
2021-06-23 12:36:18 -07:00
|
|
|
return startLoader(new Callbacks[] { callbacks });
|
2020-01-05 15:35:29 +05:30
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a callbacks to receive model updates
|
|
|
|
|
*/
|
|
|
|
|
public void addCallbacks(Callbacks callbacks) {
|
|
|
|
|
Preconditions.assertUIThread();
|
|
|
|
|
synchronized (mCallbacksList) {
|
|
|
|
|
mCallbacksList.add(callbacks);
|
|
|
|
|
}
|
2014-09-30 09:48:49 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-02 11:23:39 -07:00
|
|
|
/**
|
|
|
|
|
* Starts the loader. Tries to bind {@params synchronousBindPage} synchronously if possible.
|
|
|
|
|
* @return true if the page could be bound synchronously.
|
|
|
|
|
*/
|
2020-01-05 15:35:29 +05:30
|
|
|
public boolean startLoader() {
|
2021-06-23 12:36:18 -07:00
|
|
|
return startLoader(new Callbacks[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean startLoader(Callbacks[] newCallbacks) {
|
2015-04-16 15:20:51 -07:00
|
|
|
// Enable queue before starting loader. It will get disabled in Launcher#finishBindingItems
|
2020-08-12 13:59:27 -07:00
|
|
|
ItemInstallQueue.INSTANCE.get(mApp.getContext())
|
|
|
|
|
.pauseModelPush(ItemInstallQueue.FLAG_LOADER_RUNNING);
|
2010-06-17 13:28:48 -04:00
|
|
|
synchronized (mLock) {
|
2021-06-23 12:36:18 -07:00
|
|
|
// If there is already one running, tell it to stop.
|
|
|
|
|
boolean wasRunning = stopLoader();
|
|
|
|
|
boolean bindDirectly = mModelLoaded && !mIsLoaderTaskRunning;
|
|
|
|
|
boolean bindAllCallbacks = wasRunning || !bindDirectly || newCallbacks.length == 0;
|
|
|
|
|
final Callbacks[] callbacksList = bindAllCallbacks ? getCallbacks() : newCallbacks;
|
|
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
if (callbacksList.length > 0) {
|
2015-08-28 15:19:36 -07:00
|
|
|
// Clear any pending bind-runnables from the synchronized load process.
|
2020-01-05 15:35:29 +05:30
|
|
|
for (Callbacks cb : callbacksList) {
|
2020-08-10 10:50:36 -07:00
|
|
|
MAIN_EXECUTOR.execute(cb::clearPendingBinds);
|
2020-01-05 15:35:29 +05:30
|
|
|
}
|
2015-08-28 15:19:36 -07:00
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
LoaderResults loaderResults = new LoaderResults(
|
2020-08-10 10:50:36 -07:00
|
|
|
mApp, mBgDataModel, mBgAllAppsList, callbacksList);
|
2021-06-23 12:36:18 -07:00
|
|
|
if (bindDirectly) {
|
2017-06-01 14:26:38 -07:00
|
|
|
// Divide the set of loaded items into those that we are binding synchronously,
|
|
|
|
|
// and everything else that is to be bound normally (asynchronously).
|
2021-06-23 12:36:18 -07:00
|
|
|
loaderResults.bindWorkspace(bindAllCallbacks);
|
2017-06-01 14:26:38 -07:00
|
|
|
// For now, continue posting the binding of AllApps as there are other
|
|
|
|
|
// issues that arise from that.
|
|
|
|
|
loaderResults.bindAllApps();
|
|
|
|
|
loaderResults.bindDeepShortcuts();
|
2017-06-02 13:46:55 -07:00
|
|
|
loaderResults.bindWidgets();
|
2016-04-02 11:23:39 -07:00
|
|
|
return true;
|
2012-07-12 17:55:31 -07:00
|
|
|
} else {
|
2021-08-06 11:52:10 -07:00
|
|
|
stopLoader();
|
|
|
|
|
mLoaderTask = new LoaderTask(
|
|
|
|
|
mApp, mBgAllAppsList, mBgDataModel, mModelDelegate, loaderResults);
|
|
|
|
|
|
|
|
|
|
// Always post the loader task, instead of running directly
|
|
|
|
|
// (even on same thread) so that we exit any nested synchronized blocks
|
|
|
|
|
MODEL_EXECUTOR.post(mLoaderTask);
|
2012-07-12 17:55:31 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-02 11:23:39 -07:00
|
|
|
return false;
|
2012-07-12 17:55:31 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-06 14:33:18 -07:00
|
|
|
/**
|
|
|
|
|
* If there is already a loader task running, tell it to stop.
|
2020-01-05 15:35:29 +05:30
|
|
|
* @return true if an existing loader was stopped.
|
2017-06-06 14:33:18 -07:00
|
|
|
*/
|
2021-06-23 12:36:18 -07:00
|
|
|
private boolean stopLoader() {
|
2010-06-17 13:28:48 -04:00
|
|
|
synchronized (mLock) {
|
2017-06-06 14:33:18 -07:00
|
|
|
LoaderTask oldTask = mLoaderTask;
|
|
|
|
|
mLoaderTask = null;
|
|
|
|
|
if (oldTask != null) {
|
|
|
|
|
oldTask.stopLocked();
|
2020-01-05 15:35:29 +05:30
|
|
|
return true;
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
2020-01-05 15:35:29 +05:30
|
|
|
return false;
|
2009-03-18 17:39:48 -07:00
|
|
|
}
|
2010-06-17 13:28:48 -04:00
|
|
|
}
|
2009-06-24 13:45:46 -07:00
|
|
|
|
2021-08-06 11:52:10 -07:00
|
|
|
/**
|
|
|
|
|
* Loads the model if not loaded
|
|
|
|
|
* @param callback called with the data model upon successful load or null on model thread.
|
|
|
|
|
*/
|
|
|
|
|
public void loadAsync(Consumer<BgDataModel> callback) {
|
2018-02-16 03:23:51 +00:00
|
|
|
synchronized (mLock) {
|
2021-08-06 11:52:10 -07:00
|
|
|
if (!mModelLoaded && !mIsLoaderTaskRunning) {
|
|
|
|
|
startLoader();
|
2018-02-16 03:23:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-08-06 11:52:10 -07:00
|
|
|
MODEL_EXECUTOR.post(() -> callback.accept(isModelLoaded() ? mBgDataModel : null));
|
2018-02-16 03:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-20 12:51:37 -07:00
|
|
|
@Override
|
2017-03-15 11:56:47 -07:00
|
|
|
public void onInstallSessionCreated(final PackageInstallInfo sessionInfo) {
|
2019-09-20 12:51:37 -07:00
|
|
|
if (FeatureFlags.PROMISE_APPS_IN_ALL_APPS.get()) {
|
|
|
|
|
enqueueModelUpdateTask(new BaseModelUpdateTask() {
|
|
|
|
|
@Override
|
|
|
|
|
public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
|
|
|
|
|
apps.addPromiseApp(app.getContext(), sessionInfo);
|
|
|
|
|
bindApplicationsIfNeeded();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onSessionFailure(String packageName, UserHandle user) {
|
|
|
|
|
if (!FeatureFlags.PROMISE_APPS_NEW_INSTALLS.get()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-06 14:33:18 -07:00
|
|
|
enqueueModelUpdateTask(new BaseModelUpdateTask() {
|
2017-03-15 11:56:47 -07:00
|
|
|
@Override
|
|
|
|
|
public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
|
2020-07-09 19:31:40 -07:00
|
|
|
final IntSet removedIds = new IntSet();
|
2019-09-20 12:51:37 -07:00
|
|
|
synchronized (dataModel) {
|
|
|
|
|
for (ItemInfo info : dataModel.itemsIdMap) {
|
|
|
|
|
if (info instanceof WorkspaceItemInfo
|
|
|
|
|
&& ((WorkspaceItemInfo) info).hasPromiseIconUi()
|
|
|
|
|
&& user.equals(info.user)
|
|
|
|
|
&& info.getIntent() != null
|
|
|
|
|
&& TextUtils.equals(packageName, info.getIntent().getPackage())) {
|
2020-07-09 19:31:40 -07:00
|
|
|
removedIds.add(info.id);
|
2019-09-20 12:51:37 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!removedIds.isEmpty()) {
|
2020-07-09 19:31:40 -07:00
|
|
|
deleteAndBindComponentsRemoved(ItemInfoMatcher.ofItemIds(removedIds));
|
2019-09-20 12:51:37 -07:00
|
|
|
}
|
2017-03-15 11:56:47 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-20 12:51:37 -07:00
|
|
|
@Override
|
|
|
|
|
public void onPackageStateChanged(PackageInstallInfo installInfo) {
|
|
|
|
|
enqueueModelUpdateTask(new PackageInstallStateChangedTask(installInfo));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the icons and label of all pending icons for the provided package name.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onUpdateSessionDisplay(PackageUserKey key, PackageInstaller.SessionInfo info) {
|
|
|
|
|
mApp.getIconCache().updateSessionCache(key, info);
|
|
|
|
|
|
|
|
|
|
HashSet<String> packages = new HashSet<>();
|
|
|
|
|
packages.add(key.mPackageName);
|
|
|
|
|
enqueueModelUpdateTask(new CacheDataUpdatedTask(
|
|
|
|
|
CacheDataUpdatedTask.OP_SESSION_UPDATE, key.mUser, packages));
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 13:46:55 -07:00
|
|
|
public class LoaderTransaction implements AutoCloseable {
|
|
|
|
|
|
|
|
|
|
private final LoaderTask mTask;
|
|
|
|
|
|
|
|
|
|
private LoaderTransaction(LoaderTask task) throws CancellationException {
|
|
|
|
|
synchronized (mLock) {
|
|
|
|
|
if (mLoaderTask != task) {
|
|
|
|
|
throw new CancellationException("Loader already stopped");
|
|
|
|
|
}
|
|
|
|
|
mTask = task;
|
|
|
|
|
mIsLoaderTaskRunning = true;
|
|
|
|
|
mModelLoaded = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void commit() {
|
|
|
|
|
synchronized (mLock) {
|
|
|
|
|
// Everything loaded bind the data.
|
|
|
|
|
mModelLoaded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void close() {
|
|
|
|
|
synchronized (mLock) {
|
|
|
|
|
// If we are still the last one to be scheduled, remove ourselves.
|
|
|
|
|
if (mLoaderTask == mTask) {
|
|
|
|
|
mLoaderTask = null;
|
|
|
|
|
}
|
|
|
|
|
mIsLoaderTaskRunning = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LoaderTransaction beginLoader(LoaderTask task) throws CancellationException {
|
|
|
|
|
return new LoaderTransaction(task);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-10 16:09:29 -07:00
|
|
|
/**
|
|
|
|
|
* Refreshes the cached shortcuts if the shortcut permission has changed.
|
|
|
|
|
* Current implementation simply reloads the workspace, but it can be optimized to
|
2019-12-10 12:19:13 -08:00
|
|
|
* use partial updates similar to {@link UserCache}
|
2016-08-10 16:09:29 -07:00
|
|
|
*/
|
2020-07-27 17:50:33 -07:00
|
|
|
public void validateModelDataOnResume() {
|
|
|
|
|
MODEL_EXECUTOR.getHandler().removeCallbacks(mDataValidationCheck);
|
|
|
|
|
MODEL_EXECUTOR.post(mDataValidationCheck);
|
2016-08-10 16:09:29 -07:00
|
|
|
}
|
|
|
|
|
|
2015-05-20 21:57:06 -07:00
|
|
|
/**
|
|
|
|
|
* Called when the icons for packages have been updated in the icon cache.
|
|
|
|
|
*/
|
2016-12-15 15:53:17 -08:00
|
|
|
public void onPackageIconsUpdated(HashSet<String> updatedPackages, UserHandle user) {
|
2015-05-20 21:57:06 -07:00
|
|
|
// If any package icon has changed (app was updated while launcher was dead),
|
|
|
|
|
// update the corresponding shortcuts.
|
2016-09-09 15:47:55 -07:00
|
|
|
enqueueModelUpdateTask(new CacheDataUpdatedTask(
|
|
|
|
|
CacheDataUpdatedTask.OP_CACHE_UPDATE, user, updatedPackages));
|
2016-05-19 11:19:39 -07:00
|
|
|
}
|
2015-05-06 16:53:21 -07:00
|
|
|
|
2018-09-26 12:00:30 -07:00
|
|
|
/**
|
|
|
|
|
* Called when the labels for the widgets has updated in the icon cache.
|
|
|
|
|
*/
|
|
|
|
|
public void onWidgetLabelsUpdated(HashSet<String> updatedPackages, UserHandle user) {
|
|
|
|
|
enqueueModelUpdateTask(new BaseModelUpdateTask() {
|
|
|
|
|
@Override
|
|
|
|
|
public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
|
|
|
|
|
dataModel.widgetsModel.onPackageIconsUpdated(updatedPackages, user, app);
|
|
|
|
|
bindUpdatedWidgets(dataModel);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 14:33:18 -07:00
|
|
|
public void enqueueModelUpdateTask(ModelUpdateTask task) {
|
2021-08-27 21:22:17 +00:00
|
|
|
if (mModelDestroyed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-10 10:50:36 -07:00
|
|
|
task.init(mApp, this, mBgDataModel, mBgAllAppsList, MAIN_EXECUTOR);
|
2019-08-15 14:53:41 -07:00
|
|
|
MODEL_EXECUTOR.execute(task);
|
2016-07-28 12:11:54 -07:00
|
|
|
}
|
|
|
|
|
|
2016-09-09 15:47:55 -07:00
|
|
|
/**
|
|
|
|
|
* A task to be executed on the current callbacks on the UI thread.
|
|
|
|
|
* If there is no current callbacks, the task is ignored.
|
|
|
|
|
*/
|
|
|
|
|
public interface CallbackTask {
|
2015-05-20 21:57:06 -07:00
|
|
|
|
2016-09-09 15:47:55 -07:00
|
|
|
void execute(Callbacks callbacks);
|
2010-06-17 13:28:48 -04:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2016-09-09 15:47:55 -07:00
|
|
|
/**
|
|
|
|
|
* A runnable which changes/updates the data model of the launcher based on certain events.
|
|
|
|
|
*/
|
2017-06-06 14:33:18 -07:00
|
|
|
public interface ModelUpdateTask extends Runnable {
|
2016-07-28 12:11:54 -07:00
|
|
|
|
2016-09-09 15:47:55 -07:00
|
|
|
/**
|
2017-06-06 14:33:18 -07:00
|
|
|
* Called before the task is posted to initialize the internal state.
|
2016-09-09 15:47:55 -07:00
|
|
|
*/
|
2017-06-06 14:33:18 -07:00
|
|
|
void init(LauncherAppState app, LauncherModel model,
|
|
|
|
|
BgDataModel dataModel, AllAppsList allAppsList, Executor uiExecutor);
|
2014-10-06 16:23:56 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 18:38:52 -07:00
|
|
|
public void updateAndBindWorkspaceItem(WorkspaceItemInfo si, ShortcutInfo info) {
|
2019-03-27 16:03:06 -07:00
|
|
|
updateAndBindWorkspaceItem(() -> {
|
2018-07-09 16:47:01 -07:00
|
|
|
si.updateFromDeepShortcutInfo(info, mApp.getContext());
|
2020-02-06 11:28:01 -08:00
|
|
|
mApp.getIconCache().getShortcutIcon(si, info);
|
2018-07-09 16:47:01 -07:00
|
|
|
return si;
|
2017-01-18 11:30:23 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 15:42:44 -07:00
|
|
|
/**
|
2017-01-06 16:32:57 -08:00
|
|
|
* Utility method to update a shortcut on the background thread.
|
2016-07-20 15:42:44 -07:00
|
|
|
*/
|
2019-03-27 16:03:06 -07:00
|
|
|
public void updateAndBindWorkspaceItem(final Supplier<WorkspaceItemInfo> itemProvider) {
|
2017-06-06 14:33:18 -07:00
|
|
|
enqueueModelUpdateTask(new BaseModelUpdateTask() {
|
2016-07-20 15:42:44 -07:00
|
|
|
@Override
|
2016-09-09 15:47:55 -07:00
|
|
|
public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
|
2019-03-27 16:03:06 -07:00
|
|
|
WorkspaceItemInfo info = itemProvider.get();
|
2018-06-21 09:59:08 -07:00
|
|
|
getModelWriter().updateItemInDatabase(info);
|
2019-03-27 16:03:06 -07:00
|
|
|
ArrayList<WorkspaceItemInfo> update = new ArrayList<>();
|
2016-07-20 15:42:44 -07:00
|
|
|
update.add(info);
|
2019-03-27 16:03:06 -07:00
|
|
|
bindUpdatedWorkspaceItems(update);
|
2016-07-20 15:42:44 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 13:46:55 -07:00
|
|
|
public void refreshAndBindWidgetsAndShortcuts(@Nullable final PackageUserKey packageUser) {
|
2017-06-06 14:33:18 -07:00
|
|
|
enqueueModelUpdateTask(new BaseModelUpdateTask() {
|
2015-04-20 20:40:03 -07:00
|
|
|
@Override
|
2017-06-02 13:46:55 -07:00
|
|
|
public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
|
|
|
|
|
dataModel.widgetsModel.update(app, packageUser);
|
|
|
|
|
bindUpdatedWidgets(dataModel);
|
2015-04-20 20:40:03 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-30 15:11:27 -08:00
|
|
|
public void dumpState(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
|
|
|
|
|
if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
|
|
|
|
|
writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size());
|
|
|
|
|
for (AppInfo info : mBgAllAppsList.data) {
|
2019-10-25 13:41:28 -07:00
|
|
|
writer.println(prefix + " title=\"" + info.title
|
|
|
|
|
+ "\" bitmapIcon=" + info.bitmap.icon
|
2017-01-30 15:11:27 -08:00
|
|
|
+ " componentName=" + info.componentName.getPackageName());
|
|
|
|
|
}
|
2021-02-26 12:56:25 -08:00
|
|
|
writer.println();
|
2010-06-17 13:28:48 -04:00
|
|
|
}
|
2021-02-26 12:56:25 -08:00
|
|
|
mModelDelegate.dump(prefix, fd, writer, args);
|
2019-12-18 19:29:00 +05:30
|
|
|
mBgDataModel.dump(prefix, fd, writer, args);
|
2009-11-17 17:32:16 -08:00
|
|
|
}
|
2014-11-10 18:05:31 -08:00
|
|
|
|
2020-01-05 15:35:29 +05:30
|
|
|
/**
|
|
|
|
|
* Returns true if there are any callbacks attached to the model
|
|
|
|
|
*/
|
|
|
|
|
public boolean hasCallbacks() {
|
|
|
|
|
synchronized (mCallbacksList) {
|
|
|
|
|
return !mCallbacksList.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array of currently attached callbacks
|
|
|
|
|
*/
|
|
|
|
|
public Callbacks[] getCallbacks() {
|
|
|
|
|
synchronized (mCallbacksList) {
|
|
|
|
|
return mCallbacksList.toArray(new Callbacks[mCallbacksList.size()]);
|
|
|
|
|
}
|
2014-11-10 18:05:31 -08:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|