Merge branch 'aosp-13' into 13-wip
This commit is contained in:
commit
19bf26dd5a
1558 files changed
+103731
-36693
No files matched your search
@@ -17,87 +17,91 @@
|
||||
*/
|
||||
package com.android.launcher3.allapps;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.launcher3.BaseDraggingActivity;
|
||||
import com.android.launcher3.allapps.AllAppsGridAdapter.AdapterItem;
|
||||
import com.android.launcher3.config.FeatureFlags;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
|
||||
import com.android.launcher3.allapps.BaseAllAppsAdapter.AdapterItem;
|
||||
import com.android.launcher3.model.data.AppInfo;
|
||||
import com.android.launcher3.util.ItemInfoMatcher;
|
||||
import com.android.launcher3.model.data.ItemInfo;
|
||||
import com.android.launcher3.util.LabelComparator;
|
||||
import com.android.launcher3.views.ActivityContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* The alphabetically sorted list of applications.
|
||||
*
|
||||
* @param <T> Type of context inflating this view.
|
||||
*/
|
||||
public class AlphabeticalAppsList implements AllAppsStore.OnUpdateListener {
|
||||
public class AlphabeticalAppsList<T extends Context & ActivityContext> implements
|
||||
AllAppsStore.OnUpdateListener {
|
||||
|
||||
public static final String TAG = "AlphabeticalAppsList";
|
||||
|
||||
private static final int FAST_SCROLL_FRACTION_DISTRIBUTE_BY_ROWS_FRACTION = 0;
|
||||
private static final int FAST_SCROLL_FRACTION_DISTRIBUTE_BY_NUM_SECTIONS = 1;
|
||||
|
||||
private final int mFastScrollDistributionMode = FAST_SCROLL_FRACTION_DISTRIBUTE_BY_NUM_SECTIONS;
|
||||
private final WorkAdapterProvider mWorkAdapterProvider;
|
||||
private final WorkProfileManager mWorkProviderManager;
|
||||
|
||||
/**
|
||||
* Info about a fast scroller section, depending if sections are merged, the fast scroller
|
||||
* Info about a fast scroller section, depending if sections are merged, the
|
||||
* fast scroller
|
||||
* sections will not be the same set as the section headers.
|
||||
*/
|
||||
public static class FastScrollSectionInfo {
|
||||
// The section name
|
||||
public String sectionName;
|
||||
// The AdapterItem to scroll to for this section
|
||||
public AdapterItem fastScrollToItem;
|
||||
// The touch fraction that should map to this fast scroll section info
|
||||
public float touchFraction;
|
||||
public final String sectionName;
|
||||
// The item position
|
||||
public final int position;
|
||||
|
||||
public FastScrollSectionInfo(String sectionName) {
|
||||
public FastScrollSectionInfo(String sectionName, int position) {
|
||||
this.sectionName = sectionName;
|
||||
this.position = position;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final BaseDraggingActivity mLauncher;
|
||||
private final T mActivityContext;
|
||||
|
||||
// The set of apps from the system
|
||||
private final List<AppInfo> mApps = new ArrayList<>();
|
||||
@Nullable
|
||||
private final AllAppsStore mAllAppsStore;
|
||||
|
||||
// The number of results in current adapter
|
||||
private int mAccessibilityResultsCount = 0;
|
||||
// The current set of adapter items
|
||||
private final ArrayList<AdapterItem> mAdapterItems = new ArrayList<>();
|
||||
// The set of sections that we allow fast-scrolling to (includes non-merged sections)
|
||||
// The set of sections that we allow fast-scrolling to (includes non-merged
|
||||
// sections)
|
||||
private final List<FastScrollSectionInfo> mFastScrollerSections = new ArrayList<>();
|
||||
|
||||
// The of ordered component names as a result of a search query
|
||||
private ArrayList<AdapterItem> mSearchResults;
|
||||
private AllAppsGridAdapter mAdapter;
|
||||
private final ArrayList<AdapterItem> mSearchResults = new ArrayList<>();
|
||||
private BaseAllAppsAdapter<T> mAdapter;
|
||||
private AppInfoComparator mAppNameComparator;
|
||||
private final int mNumAppsPerRow;
|
||||
private final int mNumAppsPerRowAllApps;
|
||||
private int mNumAppRowsInAdapter;
|
||||
private ItemInfoMatcher mItemFilter;
|
||||
public Predicate<ItemInfo> mItemFilter;
|
||||
|
||||
public AlphabeticalAppsList(Context context, AllAppsStore appsStore,
|
||||
WorkAdapterProvider adapterProvider) {
|
||||
public AlphabeticalAppsList(Context context, @Nullable AllAppsStore appsStore,
|
||||
WorkProfileManager workProfileManager) {
|
||||
mAllAppsStore = appsStore;
|
||||
mLauncher = BaseDraggingActivity.fromContext(context);
|
||||
mActivityContext = ActivityContext.lookupContext(context);
|
||||
mAppNameComparator = new AppInfoComparator(context);
|
||||
mWorkAdapterProvider = adapterProvider;
|
||||
mNumAppsPerRow = mLauncher.getDeviceProfile().inv.numColumns;
|
||||
mAllAppsStore.addUpdateListener(this);
|
||||
mWorkProviderManager = workProfileManager;
|
||||
mNumAppsPerRowAllApps = mActivityContext.getDeviceProfile().inv.numAllAppsColumns;
|
||||
if (mAllAppsStore != null) {
|
||||
mAllAppsStore.addUpdateListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateItemFilter(ItemInfoMatcher itemFilter) {
|
||||
public void updateItemFilter(Predicate<ItemInfo> itemFilter) {
|
||||
this.mItemFilter = itemFilter;
|
||||
onAppsUpdated();
|
||||
}
|
||||
@@ -105,17 +109,10 @@ public class AlphabeticalAppsList implements AllAppsStore.OnUpdateListener {
|
||||
/**
|
||||
* Sets the adapter to notify when this dataset changes.
|
||||
*/
|
||||
public void setAdapter(AllAppsGridAdapter adapter) {
|
||||
public void setAdapter(BaseAllAppsAdapter<T> adapter) {
|
||||
mAdapter = adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the apps.
|
||||
*/
|
||||
public List<AppInfo> getApps() {
|
||||
return mApps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns fast scroller sections of all the current filtered applications.
|
||||
*/
|
||||
@@ -124,7 +121,8 @@ public class AlphabeticalAppsList implements AllAppsStore.OnUpdateListener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current filtered list of applications broken down into their sections.
|
||||
* Returns the current filtered list of applications broken down into their
|
||||
* sections.
|
||||
*/
|
||||
public List<AdapterItem> getAdapterItems() {
|
||||
return mAdapterItems;
|
||||
@@ -167,50 +165,25 @@ public class AlphabeticalAppsList implements AllAppsStore.OnUpdateListener {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether there are is a filter set.
|
||||
* Returns whether there are search results which will hide the A-Z list.
|
||||
*/
|
||||
public boolean hasFilter() {
|
||||
return (mSearchResults != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether there are no filtered results.
|
||||
*/
|
||||
public boolean hasNoFilteredResults() {
|
||||
return (mSearchResults != null) && mAccessibilityResultsCount == 0;
|
||||
public boolean hasSearchResults() {
|
||||
return !mSearchResults.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets results list for search
|
||||
*/
|
||||
public boolean setSearchResults(ArrayList<AdapterItem> results) {
|
||||
if (!Objects.equals(results, mSearchResults)) {
|
||||
mSearchResults = results;
|
||||
updateAdapterItems();
|
||||
return true;
|
||||
if (Objects.equals(results, mSearchResults)) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean appendSearchResults(ArrayList<AdapterItem> results) {
|
||||
if (mSearchResults != null && results != null && results.size() > 0) {
|
||||
updateSearchAdapterItems(results, mSearchResults.size());
|
||||
refreshRecyclerView();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void updateSearchAdapterItems(ArrayList<AdapterItem> list, int offset) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
AdapterItem adapterItem = list.get(i);
|
||||
adapterItem.position = offset + i;
|
||||
mAdapterItems.add(adapterItem);
|
||||
|
||||
if (adapterItem.isCountedForAccessibility()) {
|
||||
mAccessibilityResultsCount++;
|
||||
}
|
||||
mSearchResults.clear();
|
||||
if (results != null) {
|
||||
mSearchResults.addAll(results);
|
||||
}
|
||||
updateAdapterItems();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,132 +191,98 @@ public class AlphabeticalAppsList implements AllAppsStore.OnUpdateListener {
|
||||
*/
|
||||
@Override
|
||||
public void onAppsUpdated() {
|
||||
if (mAllAppsStore == null) {
|
||||
return;
|
||||
}
|
||||
// Sort the list of apps
|
||||
mApps.clear();
|
||||
|
||||
for (AppInfo app : mAllAppsStore.getApps()) {
|
||||
if (mItemFilter == null || mItemFilter.matches(app, null)) {
|
||||
mApps.add(app);
|
||||
}
|
||||
Stream<AppInfo> appSteam = Stream.of(mAllAppsStore.getApps());
|
||||
if (!hasSearchResults() && mItemFilter != null) {
|
||||
appSteam = appSteam.filter(mItemFilter);
|
||||
}
|
||||
appSteam = appSteam.sorted(mAppNameComparator);
|
||||
|
||||
Collections.sort(mApps, mAppNameComparator);
|
||||
|
||||
// As a special case for some languages (currently only Simplified Chinese), we may need to
|
||||
// As a special case for some languages (currently only Simplified Chinese), we
|
||||
// may need to
|
||||
// coalesce sections
|
||||
Locale curLocale = mLauncher.getResources().getConfiguration().locale;
|
||||
Locale curLocale = mActivityContext.getResources().getConfiguration().locale;
|
||||
boolean localeRequiresSectionSorting = curLocale.equals(Locale.SIMPLIFIED_CHINESE);
|
||||
if (localeRequiresSectionSorting) {
|
||||
// Compute the section headers. We use a TreeMap with the section name comparator to
|
||||
// Compute the section headers. We use a TreeMap with the section name
|
||||
// comparator to
|
||||
// ensure that the sections are ordered when we iterate over it later
|
||||
TreeMap<String, ArrayList<AppInfo>> sectionMap = new TreeMap<>(new LabelComparator());
|
||||
for (AppInfo info : mApps) {
|
||||
// Add the section to the cache
|
||||
String sectionName = info.sectionName;
|
||||
|
||||
// Add it to the mapping
|
||||
ArrayList<AppInfo> sectionApps = sectionMap.get(sectionName);
|
||||
if (sectionApps == null) {
|
||||
sectionApps = new ArrayList<>();
|
||||
sectionMap.put(sectionName, sectionApps);
|
||||
}
|
||||
sectionApps.add(info);
|
||||
}
|
||||
|
||||
// Add each of the section apps to the list in order
|
||||
mApps.clear();
|
||||
for (Map.Entry<String, ArrayList<AppInfo>> entry : sectionMap.entrySet()) {
|
||||
mApps.addAll(entry.getValue());
|
||||
}
|
||||
appSteam = appSteam.collect(Collectors.groupingBy(
|
||||
info -> info.sectionName,
|
||||
() -> new TreeMap<>(new LabelComparator()),
|
||||
Collectors.toCollection(ArrayList::new)))
|
||||
.values()
|
||||
.stream()
|
||||
.flatMap(ArrayList::stream);
|
||||
}
|
||||
|
||||
appSteam.forEachOrdered(mApps::add);
|
||||
// Recompose the set of adapter items from the current set of apps
|
||||
if (mSearchResults == null) {
|
||||
if (mSearchResults.isEmpty()) {
|
||||
updateAdapterItems();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the set of filtered apps with the current filter. At this point, we expect
|
||||
* Updates the set of filtered apps with the current filter. At this point, we
|
||||
* expect
|
||||
* mCachedSectionNames to have been calculated for the set of all apps in mApps.
|
||||
*/
|
||||
public void updateAdapterItems() {
|
||||
refillAdapterItems();
|
||||
refreshRecyclerView();
|
||||
}
|
||||
|
||||
private void refreshRecyclerView() {
|
||||
if (mAdapter != null) {
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void refillAdapterItems() {
|
||||
String lastSectionName = null;
|
||||
FastScrollSectionInfo lastFastScrollerSectionInfo = null;
|
||||
int position = 0;
|
||||
int appIndex = 0;
|
||||
|
||||
List<AdapterItem> oldItems = new ArrayList<>(mAdapterItems);
|
||||
// Prepare to update the list of sections, filtered apps, etc.
|
||||
mAccessibilityResultsCount = 0;
|
||||
mFastScrollerSections.clear();
|
||||
mAdapterItems.clear();
|
||||
mAccessibilityResultsCount = 0;
|
||||
|
||||
// Recreate the filtered and sectioned apps (for convenience for the grid layout) from the
|
||||
// Recreate the filtered and sectioned apps (for convenience for the grid
|
||||
// layout) from the
|
||||
// ordered set of sections
|
||||
|
||||
if (!hasFilter()) {
|
||||
mAccessibilityResultsCount = mApps.size();
|
||||
if (mWorkAdapterProvider != null) {
|
||||
position += mWorkAdapterProvider.addWorkItems(mAdapterItems);
|
||||
if (!mWorkAdapterProvider.shouldShowWorkApps()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (AppInfo info : mApps) {
|
||||
String sectionName = info.sectionName;
|
||||
|
||||
// Create a new section if the section names do not match
|
||||
if (!sectionName.equals(lastSectionName)) {
|
||||
lastSectionName = sectionName;
|
||||
lastFastScrollerSectionInfo = new FastScrollSectionInfo(sectionName);
|
||||
mFastScrollerSections.add(lastFastScrollerSectionInfo);
|
||||
}
|
||||
|
||||
// Create an app item
|
||||
AdapterItem appItem = AdapterItem.asApp(position++, sectionName, info,
|
||||
appIndex++);
|
||||
if (lastFastScrollerSectionInfo.fastScrollToItem == null) {
|
||||
lastFastScrollerSectionInfo.fastScrollToItem = appItem;
|
||||
}
|
||||
|
||||
mAdapterItems.add(appItem);
|
||||
}
|
||||
if (hasSearchResults()) {
|
||||
mAdapterItems.addAll(mSearchResults);
|
||||
} else {
|
||||
updateSearchAdapterItems(mSearchResults, 0);
|
||||
if (!FeatureFlags.ENABLE_DEVICE_SEARCH.get()) {
|
||||
// Append the search market item
|
||||
if (hasNoFilteredResults()) {
|
||||
mAdapterItems.add(AdapterItem.asEmptySearch(position++));
|
||||
} else {
|
||||
mAdapterItems.add(AdapterItem.asAllAppsDivider(position++));
|
||||
}
|
||||
mAdapterItems.add(AdapterItem.asMarketSearch(position++));
|
||||
int position = 0;
|
||||
boolean addApps = true;
|
||||
if (mWorkProviderManager != null) {
|
||||
position += mWorkProviderManager.addWorkItems(mAdapterItems);
|
||||
addApps = mWorkProviderManager.shouldShowWorkApps();
|
||||
}
|
||||
if (addApps) {
|
||||
String lastSectionName = null;
|
||||
for (AppInfo info : mApps) {
|
||||
mAdapterItems.add(AdapterItem.asApp(info));
|
||||
|
||||
String sectionName = info.sectionName;
|
||||
// Create a new section if the section names do not match
|
||||
if (!sectionName.equals(lastSectionName)) {
|
||||
lastSectionName = sectionName;
|
||||
mFastScrollerSections.add(new FastScrollSectionInfo(sectionName, position));
|
||||
}
|
||||
position++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mNumAppsPerRow != 0) {
|
||||
// Update the number of rows in the adapter after we do all the merging (otherwise, we
|
||||
mAccessibilityResultsCount = (int) mAdapterItems.stream()
|
||||
.filter(AdapterItem::isCountedForAccessibility).count();
|
||||
|
||||
if (mNumAppsPerRowAllApps != 0) {
|
||||
// Update the number of rows in the adapter after we do all the merging
|
||||
// (otherwise, we
|
||||
// would have to shift the values again)
|
||||
int numAppsInSection = 0;
|
||||
int numAppsInRow = 0;
|
||||
int rowIndex = -1;
|
||||
for (AdapterItem item : mAdapterItems) {
|
||||
item.rowIndex = 0;
|
||||
if (AllAppsGridAdapter.isDividerViewType(item.viewType)) {
|
||||
if (BaseAllAppsAdapter.isDividerViewType(item.viewType)) {
|
||||
numAppsInSection = 0;
|
||||
} else if (AllAppsGridAdapter.isIconViewType(item.viewType)) {
|
||||
if (numAppsInSection % mNumAppsPerRow == 0) {
|
||||
} else if (BaseAllAppsAdapter.isIconViewType(item.viewType)) {
|
||||
if (numAppsInSection % mNumAppsPerRowAllApps == 0) {
|
||||
numAppsInRow = 0;
|
||||
rowIndex++;
|
||||
}
|
||||
@@ -354,36 +293,43 @@ public class AlphabeticalAppsList implements AllAppsStore.OnUpdateListener {
|
||||
}
|
||||
}
|
||||
mNumAppRowsInAdapter = rowIndex + 1;
|
||||
}
|
||||
|
||||
// Pre-calculate all the fast scroller fractions
|
||||
switch (mFastScrollDistributionMode) {
|
||||
case FAST_SCROLL_FRACTION_DISTRIBUTE_BY_ROWS_FRACTION:
|
||||
float rowFraction = 1f / mNumAppRowsInAdapter;
|
||||
for (FastScrollSectionInfo info : mFastScrollerSections) {
|
||||
AdapterItem item = info.fastScrollToItem;
|
||||
if (!AllAppsGridAdapter.isIconViewType(item.viewType)) {
|
||||
info.touchFraction = 0f;
|
||||
continue;
|
||||
}
|
||||
|
||||
float subRowFraction = item.rowAppIndex * (rowFraction / mNumAppsPerRow);
|
||||
info.touchFraction = item.rowIndex * rowFraction + subRowFraction;
|
||||
}
|
||||
break;
|
||||
case FAST_SCROLL_FRACTION_DISTRIBUTE_BY_NUM_SECTIONS:
|
||||
float perSectionTouchFraction = 1f / mFastScrollerSections.size();
|
||||
float cumulativeTouchFraction = 0f;
|
||||
for (FastScrollSectionInfo info : mFastScrollerSections) {
|
||||
AdapterItem item = info.fastScrollToItem;
|
||||
if (!AllAppsGridAdapter.isIconViewType(item.viewType)) {
|
||||
info.touchFraction = 0f;
|
||||
continue;
|
||||
}
|
||||
info.touchFraction = cumulativeTouchFraction;
|
||||
cumulativeTouchFraction += perSectionTouchFraction;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (mAdapter != null) {
|
||||
DiffUtil.calculateDiff(new MyDiffCallback(oldItems, mAdapterItems), false)
|
||||
.dispatchUpdatesTo(mAdapter);
|
||||
}
|
||||
}
|
||||
|
||||
private static class MyDiffCallback extends DiffUtil.Callback {
|
||||
|
||||
private final List<AdapterItem> mOldList;
|
||||
private final List<AdapterItem> mNewList;
|
||||
|
||||
MyDiffCallback(List<AdapterItem> oldList, List<AdapterItem> newList) {
|
||||
mOldList = oldList;
|
||||
mNewList = newList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOldListSize() {
|
||||
return mOldList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNewListSize() {
|
||||
return mNewList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
return mOldList.get(oldItemPosition).isSameAs(mNewList.get(newItemPosition));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
return mOldList.get(oldItemPosition).isContentSame(mNewList.get(newItemPosition));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user