Search in sources :

Example 1 with WidgetsListBaseEntry

use of com.android.launcher3.widget.model.WidgetsListBaseEntry in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsDiffReporter method process.

/**
 * Notifies the difference between {@code currentEntries} & {@code newEntries} by calling the
 * relevant {@link androidx.recyclerview.widget.RecyclerView.RecyclerViewDataObserver} methods.
 */
public void process(ArrayList<WidgetsListBaseEntry> currentEntries, List<WidgetsListBaseEntry> newEntries, WidgetListBaseRowEntryComparator comparator) {
    if (DEBUG) {
        Log.d(TAG, "process oldEntries#=" + currentEntries.size() + " newEntries#=" + newEntries.size());
    }
    // Early exit if either of the list is empty
    if (currentEntries.isEmpty() || newEntries.isEmpty()) {
        // when the bind actually completes.
        if (currentEntries.size() != newEntries.size()) {
            currentEntries.clear();
            currentEntries.addAll(newEntries);
            mListener.notifyDataSetChanged();
        }
        return;
    }
    ArrayList<WidgetsListBaseEntry> orgEntries = (ArrayList<WidgetsListBaseEntry>) currentEntries.clone();
    Iterator<WidgetsListBaseEntry> orgIter = orgEntries.iterator();
    Iterator<WidgetsListBaseEntry> newIter = newEntries.iterator();
    WidgetsListBaseEntry orgRowEntry = orgIter.next();
    WidgetsListBaseEntry newRowEntry = newIter.next();
    do {
        int diff = compareAppNameAndType(orgRowEntry, newRowEntry, comparator);
        if (DEBUG) {
            Log.d(TAG, String.format("diff=%d orgRowEntry (%s) newRowEntry (%s)", diff, orgRowEntry != null ? orgRowEntry.toString() : null, newRowEntry != null ? newRowEntry.toString() : null));
        }
        int index = -1;
        if (diff < 0) {
            index = currentEntries.indexOf(orgRowEntry);
            mListener.notifyItemRemoved(index);
            if (DEBUG) {
                Log.d(TAG, String.format("notifyItemRemoved called (%d)%s", index, orgRowEntry.mTitleSectionName));
            }
            currentEntries.remove(index);
            orgRowEntry = orgIter.hasNext() ? orgIter.next() : null;
        } else if (diff > 0) {
            index = orgRowEntry != null ? currentEntries.indexOf(orgRowEntry) : currentEntries.size();
            currentEntries.add(index, newRowEntry);
            if (DEBUG) {
                Log.d(TAG, String.format("notifyItemInserted called (%d)%s", index, newRowEntry.mTitleSectionName));
            }
            newRowEntry = newIter.hasNext() ? newIter.next() : null;
            mListener.notifyItemInserted(index);
        } else {
            // or did the widget size and desc, span, etc change?
            if (!isSamePackageItemInfo(orgRowEntry.mPkgItem, newRowEntry.mPkgItem) || hasHeaderUpdated(orgRowEntry, newRowEntry) || hasWidgetsListContentChanged(orgRowEntry, newRowEntry)) {
                index = currentEntries.indexOf(orgRowEntry);
                currentEntries.set(index, newRowEntry);
                mListener.notifyItemChanged(index);
                if (DEBUG) {
                    Log.d(TAG, String.format("notifyItemChanged called (%d)%s", index, newRowEntry.mTitleSectionName));
                }
            }
            orgRowEntry = orgIter.hasNext() ? orgIter.next() : null;
            newRowEntry = newIter.hasNext() ? newIter.next() : null;
        }
    } while (orgRowEntry != null || newRowEntry != null);
}
Also used : WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) ArrayList(java.util.ArrayList)

Example 2 with WidgetsListBaseEntry

use of com.android.launcher3.widget.model.WidgetsListBaseEntry in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsListAdapter method ensureAllPreviewsReady.

/**
 * Checks that all preview images are loaded and starts loading for those that aren't ready.
 *
 * @return true if all previews are ready and the data can be updated, false otherwise.
 */
private boolean ensureAllPreviewsReady() {
    boolean allReady = true;
    BaseActivity activity = BaseActivity.fromContext(mContext);
    for (WidgetsListBaseEntry entry : mAllEntries) {
        if (!(entry instanceof WidgetsListContentEntry))
            continue;
        WidgetsListContentEntry contentEntry = (WidgetsListContentEntry) entry;
        if (!matchesKey(entry, mWidgetsContentVisiblePackageUserKey)) {
            // If the entry isn't visible, clear any loaded previews.
            mCachingPreviewLoader.clearPreviews(contentEntry.mWidgets);
            continue;
        }
        for (int i = 0; i < entry.mWidgets.size(); i++) {
            WidgetItem widgetItem = entry.mWidgets.get(i);
            DeviceProfile deviceProfile = activity.getDeviceProfile();
            Size widgetSize = WidgetSizes.getWidgetItemSizePx(mContext, deviceProfile, widgetItem);
            if (widgetItem.isShortcut()) {
                widgetSize = new Size(widgetSize.getWidth() + mShortcutPreviewPadding, widgetSize.getHeight() + mShortcutPreviewPadding);
            }
            if (widgetItem.hasPreviewLayout() || mCachingPreviewLoader.isPreviewLoaded(widgetItem, widgetSize)) {
                // preview bitmap is in the cache.
                continue;
            }
            // If we've reached this point, we should load the preview for the widget.
            allReady = false;
            mCachingPreviewLoader.loadPreview(activity, widgetItem, widgetSize, mPreviewLoadedCallback);
        }
    }
    return allReady;
}
Also used : DeviceProfile(com.android.launcher3.DeviceProfile) Size(android.util.Size) WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) BaseActivity(com.android.launcher3.BaseActivity) WidgetsListContentEntry(com.android.launcher3.widget.model.WidgetsListContentEntry) WidgetItem(com.android.launcher3.model.WidgetItem)

Example 3 with WidgetsListBaseEntry

use of com.android.launcher3.widget.model.WidgetsListBaseEntry in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsListAdapter method updateVisibleEntries.

private void updateVisibleEntries() {
    // loads.
    if (!ensureAllPreviewsReady())
        return;
    // Get the current top of the header with the matching key before adjusting the visible
    // entries.
    OptionalInt previousPositionForPackageUserKey = getPositionForPackageUserKey(mPendingClickHeader);
    OptionalInt topForPackageUserKey = getOffsetForPosition(previousPositionForPackageUserKey);
    List<WidgetsListBaseEntry> newVisibleEntries = mAllEntries.stream().filter(entry -> (mFilter == null || mFilter.test(entry)) && mHeaderAndSelectedContentFilter.test(entry)).map(entry -> {
        if (entry instanceof WidgetsListBaseEntry.Header<?> && matchesKey(entry, mWidgetsContentVisiblePackageUserKey)) {
            // Adjust the original entries to expand headers for the selected content.
            return ((WidgetsListBaseEntry.Header<?>) entry).withWidgetListShown();
        } else if (entry instanceof WidgetsListContentEntry) {
            // maxSpanSize.
            return ((WidgetsListContentEntry) entry).withMaxSpanSize(mMaxSpanSize);
        }
        return entry;
    }).collect(Collectors.toList());
    mDiffReporter.process(mVisibleEntries, newVisibleEntries, mRowComparator);
    if (mPendingClickHeader != null) {
        // Get the position for the clicked header after adjusting the visible entries. The
        // position may have changed if another header had previously been expanded.
        OptionalInt positionForPackageUserKey = getPositionForPackageUserKey(mPendingClickHeader);
        scrollToPositionAndMaintainOffset(positionForPackageUserKey, topForPackageUserKey);
        mPendingClickHeader = null;
    }
}
Also used : IntStream(java.util.stream.IntStream) Context(android.content.Context) Rect(android.graphics.Rect) Arrays(java.util.Arrays) NonNull(androidx.annotation.NonNull) LabelComparator(com.android.launcher3.util.LabelComparator) IconCache(com.android.launcher3.icons.IconCache) OptionalInt(java.util.OptionalInt) Process(android.os.Process) PackageItemInfo(com.android.launcher3.model.data.PackageItemInfo) ArrayList(java.util.ArrayList) WidgetPreviewLoadedCallback(com.android.launcher3.widget.WidgetPreviewLoader.WidgetPreviewLoadedCallback) CachingWidgetPreviewLoader(com.android.launcher3.widget.CachingWidgetPreviewLoader) BaseActivity(com.android.launcher3.BaseActivity) WidgetItem(com.android.launcher3.model.WidgetItem) Map(java.util.Map) View(android.view.View) ViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder) RecyclerView(androidx.recyclerview.widget.RecyclerView) Log(android.util.Log) Launcher(com.android.launcher3.Launcher) WidgetsListHeaderEntry(com.android.launcher3.widget.model.WidgetsListHeaderEntry) LayoutInflater(android.view.LayoutInflater) Adapter(androidx.recyclerview.widget.RecyclerView.Adapter) WidgetsListContentEntry(com.android.launcher3.widget.model.WidgetsListContentEntry) Predicate(java.util.function.Predicate) WidgetCell(com.android.launcher3.widget.WidgetCell) LAUNCHER_WIDGETSTRAY_APP_EXPANDED(com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_WIDGETSTRAY_APP_EXPANDED) PackageUserKey(com.android.launcher3.util.PackageUserKey) DatabaseWidgetPreviewLoader(com.android.launcher3.widget.DatabaseWidgetPreviewLoader) WidgetsListSearchHeaderEntry(com.android.launcher3.widget.model.WidgetsListSearchHeaderEntry) Collectors(java.util.stream.Collectors) ViewGroup(android.view.ViewGroup) ViewHolderBinder(com.android.launcher3.recyclerview.ViewHolderBinder) DeviceProfile(com.android.launcher3.DeviceProfile) SparseArray(android.util.SparseArray) WidgetSizes(com.android.launcher3.widget.util.WidgetSizes) List(java.util.List) Nullable(androidx.annotation.Nullable) WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) R(com.android.launcher3.R) TableRow(android.widget.TableRow) LayoutParams(androidx.recyclerview.widget.RecyclerView.LayoutParams) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Comparator(java.util.Comparator) OnLongClickListener(android.view.View.OnLongClickListener) Size(android.util.Size) OnClickListener(android.view.View.OnClickListener) WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) WidgetsListContentEntry(com.android.launcher3.widget.model.WidgetsListContentEntry) OptionalInt(java.util.OptionalInt)

Example 4 with WidgetsListBaseEntry

use of com.android.launcher3.widget.model.WidgetsListBaseEntry in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsListAdapter method shouldClearVisibleEntries.

/**
 * Returns {@code true} if there is a change in {@link #mAllEntries} that results in an
 * invalidation of {@link #mVisibleEntries}. e.g. there is change in the device language.
 */
private boolean shouldClearVisibleEntries() {
    Map<PackageUserKey, PackageItemInfo> packagesInfo = mAllEntries.stream().filter(entry -> entry instanceof WidgetsListHeaderEntry).map(entry -> entry.mPkgItem).collect(Collectors.toMap(entry -> new PackageUserKey(entry.packageName, entry.user), entry -> entry));
    for (WidgetsListBaseEntry visibleEntry : mVisibleEntries) {
        PackageUserKey key = new PackageUserKey(visibleEntry.mPkgItem.packageName, visibleEntry.mPkgItem.user);
        PackageItemInfo packageItemInfo = packagesInfo.get(key);
        if (packageItemInfo != null && !visibleEntry.mPkgItem.title.equals(packageItemInfo.title)) {
            return true;
        }
    }
    return false;
}
Also used : IntStream(java.util.stream.IntStream) Context(android.content.Context) Rect(android.graphics.Rect) Arrays(java.util.Arrays) NonNull(androidx.annotation.NonNull) LabelComparator(com.android.launcher3.util.LabelComparator) IconCache(com.android.launcher3.icons.IconCache) OptionalInt(java.util.OptionalInt) Process(android.os.Process) PackageItemInfo(com.android.launcher3.model.data.PackageItemInfo) ArrayList(java.util.ArrayList) WidgetPreviewLoadedCallback(com.android.launcher3.widget.WidgetPreviewLoader.WidgetPreviewLoadedCallback) CachingWidgetPreviewLoader(com.android.launcher3.widget.CachingWidgetPreviewLoader) BaseActivity(com.android.launcher3.BaseActivity) WidgetItem(com.android.launcher3.model.WidgetItem) Map(java.util.Map) View(android.view.View) ViewHolder(androidx.recyclerview.widget.RecyclerView.ViewHolder) RecyclerView(androidx.recyclerview.widget.RecyclerView) Log(android.util.Log) Launcher(com.android.launcher3.Launcher) WidgetsListHeaderEntry(com.android.launcher3.widget.model.WidgetsListHeaderEntry) LayoutInflater(android.view.LayoutInflater) Adapter(androidx.recyclerview.widget.RecyclerView.Adapter) WidgetsListContentEntry(com.android.launcher3.widget.model.WidgetsListContentEntry) Predicate(java.util.function.Predicate) WidgetCell(com.android.launcher3.widget.WidgetCell) LAUNCHER_WIDGETSTRAY_APP_EXPANDED(com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_WIDGETSTRAY_APP_EXPANDED) PackageUserKey(com.android.launcher3.util.PackageUserKey) DatabaseWidgetPreviewLoader(com.android.launcher3.widget.DatabaseWidgetPreviewLoader) WidgetsListSearchHeaderEntry(com.android.launcher3.widget.model.WidgetsListSearchHeaderEntry) Collectors(java.util.stream.Collectors) ViewGroup(android.view.ViewGroup) ViewHolderBinder(com.android.launcher3.recyclerview.ViewHolderBinder) DeviceProfile(com.android.launcher3.DeviceProfile) SparseArray(android.util.SparseArray) WidgetSizes(com.android.launcher3.widget.util.WidgetSizes) List(java.util.List) Nullable(androidx.annotation.Nullable) WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) R(com.android.launcher3.R) TableRow(android.widget.TableRow) LayoutParams(androidx.recyclerview.widget.RecyclerView.LayoutParams) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Comparator(java.util.Comparator) OnLongClickListener(android.view.View.OnLongClickListener) Size(android.util.Size) OnClickListener(android.view.View.OnClickListener) WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) PackageItemInfo(com.android.launcher3.model.data.PackageItemInfo) PackageUserKey(com.android.launcher3.util.PackageUserKey) WidgetsListHeaderEntry(com.android.launcher3.widget.model.WidgetsListHeaderEntry)

Example 5 with WidgetsListBaseEntry

use of com.android.launcher3.widget.model.WidgetsListBaseEntry in project android_packages_apps_Launcher3 by crdroidandroid.

the class WidgetsListAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(ViewHolder holder, int pos) {
    ViewHolderBinder viewHolderBinder = mViewHolderBinders.get(getItemViewType(pos));
    WidgetsListBaseEntry entry = mVisibleEntries.get(pos);
    viewHolderBinder.bindViewHolder(holder, mVisibleEntries.get(pos), pos);
    holder.itemView.setTag(R.id.tag_widget_entry, entry);
}
Also used : WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) ViewHolderBinder(com.android.launcher3.recyclerview.ViewHolderBinder)

Aggregations

WidgetsListBaseEntry (com.android.launcher3.widget.model.WidgetsListBaseEntry)12 WidgetsListContentEntry (com.android.launcher3.widget.model.WidgetsListContentEntry)8 ArrayList (java.util.ArrayList)8 WidgetItem (com.android.launcher3.model.WidgetItem)6 WidgetsListHeaderEntry (com.android.launcher3.widget.model.WidgetsListHeaderEntry)6 PackageItemInfo (com.android.launcher3.model.data.PackageItemInfo)4 PackageUserKey (com.android.launcher3.util.PackageUserKey)4 List (java.util.List)4 Size (android.util.Size)3 BaseActivity (com.android.launcher3.BaseActivity)3 DeviceProfile (com.android.launcher3.DeviceProfile)3 ViewHolderBinder (com.android.launcher3.recyclerview.ViewHolderBinder)3 Map (java.util.Map)3 Test (org.junit.Test)3 Context (android.content.Context)2 Rect (android.graphics.Rect)2 Process (android.os.Process)2 Log (android.util.Log)2 SparseArray (android.util.SparseArray)2 LayoutInflater (android.view.LayoutInflater)2