Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 308faf83 authored by Hyunyoung Song's avatar Hyunyoung Song
Browse files

Move foldername and appinfo fetching to model thread

Bug: 149255695
Change-Id: I8377d4a06d4ae67b601f3e0ad9e722bd7b103a7a
parent 0580b4a5
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -66,7 +66,6 @@ import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
@@ -126,16 +125,6 @@ public class LauncherModel extends LauncherApps.Callback implements InstallSessi
        mBgAllAppsList = new AllAppsList(iconCache, appFilter);
    }

    /**
     * Returns AppInfo with corresponding package name.
     * TODO: move to enqueueModelTask
     */
    public Optional<AppInfo> getAppInfoByPackageName(String pkg) {
        return mBgAllAppsList.data.stream()
                .filter(info -> info.componentName.getPackageName().equals(pkg))
                .findAny();
    }

    /**
     * Adds the provided items to the workspace.
     */
+34 −3
Original line number Diff line number Diff line
@@ -21,9 +21,14 @@ import android.text.TextUtils;
import android.util.Log;

import com.android.launcher3.AppInfo;
import com.android.launcher3.FolderInfo;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.R;
import com.android.launcher3.WorkspaceItemInfo;
import com.android.launcher3.model.AllAppsList;
import com.android.launcher3.model.BaseModelUpdateTask;
import com.android.launcher3.model.BgDataModel;
import com.android.launcher3.util.IntSparseArrayMap;
import com.android.launcher3.util.ResourceBasedOverride;

import java.util.ArrayList;
@@ -50,6 +55,8 @@ public class FolderNameProvider implements ResourceBasedOverride {
     * name edit box can also be used to provide suggestion.
     */
    public static final int SUGGEST_MAX = 4;
    protected IntSparseArrayMap<FolderInfo> mFolderInfos;
    protected List<AppInfo> mAppInfos;

    /**
     * Retrieve instance of this object that can be overridden in runtime based on the build
@@ -58,9 +65,16 @@ public class FolderNameProvider implements ResourceBasedOverride {
    public static FolderNameProvider newInstance(Context context) {
        FolderNameProvider fnp = Overrides.getObject(FolderNameProvider.class,
                context.getApplicationContext(), R.string.folder_name_provider_class);
        fnp.load(context);

        return fnp;
    }

    private void load(Context context) {
        LauncherAppState.getInstance(context).getModel().enqueueModelUpdateTask(
                new FolderNameWorker());
    }

    /**
     * Generate and rank the suggested Folder names.
     */
@@ -92,9 +106,8 @@ public class FolderNameProvider implements ResourceBasedOverride {
                .collect(Collectors.toList());

        if (distinctItemInfos.size() == 1) {
            Optional<AppInfo> info = LauncherAppState.getInstance(context).getModel()
                    .getAppInfoByPackageName(distinctItemInfos.get(0).getTargetComponent()
                            .getPackageName());
            Optional<AppInfo> info = getAppInfoByPackageName(
                    distinctItemInfos.get(0).getTargetComponent().getPackageName());
            // Place it as first viable suggestion and shift everything else
            info.ifPresent(i -> setAsFirstSuggestion(nameInfos, i.title.toString()));
        }
@@ -103,6 +116,15 @@ public class FolderNameProvider implements ResourceBasedOverride {
        }
    }

    private Optional<AppInfo> getAppInfoByPackageName(String packageName) {
        if (mAppInfos == null || mAppInfos.isEmpty()) {
            return Optional.empty();
        }
        return mAppInfos.stream()
                .filter(info -> info.componentName.getPackageName().equals(packageName))
                .findAny();
    }

    private void setAsFirstSuggestion(FolderNameInfo[] nameInfos, CharSequence label) {
        if (nameInfos.length == 0 || contains(nameInfos, label)) {
            return;
@@ -143,4 +165,13 @@ public class FolderNameProvider implements ResourceBasedOverride {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    private class FolderNameWorker extends BaseModelUpdateTask {
        @Override
        public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
            mFolderInfos = dataModel.folders.clone();
            mAppInfos = Arrays.asList(apps.copyData());
        }
    }

}