From af6a22081dca5b68e99af1cd6127bae660651565 Mon Sep 17 00:00:00 2001 From: Joshua Trask Date: Tue, 19 Dec 2023 21:21:57 +0000 Subject: Clean up indexing/lookup methods in pager adapter This is originally from snapshot 33 in ag/25335069: "consolidate and rename-more-explicitly the assorted index conversion/lookup methods in `MultiProfilePagerAdapter`. Keeping these methods together (and implemented in terms of each other where possible) makes it easier to audit our logic as we prepare for 3+ tabs. Particularly, this establishes a 'source of truth' for the aspect of our business logic that assigns the clone profile as a sort of 'secondary handle' for the personal tab." Bug: 310211468 Test: `IntentResolver-tests-actvity`, `ResolverActivityTest` Change-Id: I5dda5909318d9741927f3e2657cb4aefdfab6c6b --- .../v2/ChooserMultiProfilePagerAdapter.java | 4 +- .../v2/MultiProfilePagerAdapter.java | 127 ++++++++++++--------- 2 files changed, 78 insertions(+), 53 deletions(-) (limited to 'java/src') diff --git a/java/src/com/android/intentresolver/v2/ChooserMultiProfilePagerAdapter.java b/java/src/com/android/intentresolver/v2/ChooserMultiProfilePagerAdapter.java index 06f4bfae..c3db4a51 100644 --- a/java/src/com/android/intentresolver/v2/ChooserMultiProfilePagerAdapter.java +++ b/java/src/com/android/intentresolver/v2/ChooserMultiProfilePagerAdapter.java @@ -135,7 +135,7 @@ public class ChooserMultiProfilePagerAdapter extends MultiProfilePagerAdapter< */ public void setIsCollapsed(boolean isCollapsed) { for (int i = 0, size = getItemCount(); i < size; i++) { - getAdapterForIndex(i).setAzLabelVisibility(!isCollapsed); + getPageAdapterForIndex(i).setAzLabelVisibility(!isCollapsed); } } @@ -170,7 +170,7 @@ public class ChooserMultiProfilePagerAdapter extends MultiProfilePagerAdapter< /** Apply the specified {@code height} as the footer in each tab's adapter. */ public void setFooterHeightInEveryAdapter(int height) { for (int i = 0; i < getItemCount(); ++i) { - getAdapterForIndex(i).setFooterHeight(height); + getPageAdapterForIndex(i).setFooterHeight(height); } } diff --git a/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java b/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java index 3387a73f..3f772775 100644 --- a/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java +++ b/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java @@ -167,8 +167,16 @@ class MultiProfilePagerAdapter< profile, mPageViewInflater.get(), adapter, containerBottomPaddingOverrideSupplier); } + private boolean hasPageForIndex(int pageIndex) { + return (pageIndex >= 0) && (pageIndex < getCount()); + } + + public final boolean hasPageForProfile(@Profile int profile) { + return hasPageForIndex(getPageNumberForProfile(profile)); + } + private @Profile int getProfileForPageNumber(int position) { - if (hasAdapterForIndex(position)) { + if (hasPageForIndex(position)) { return mItems.get(position).mProfile; } return -1; @@ -183,6 +191,56 @@ class MultiProfilePagerAdapter< return -1; } + private ListAdapterT getListAdapterForPageNumber(int pageNumber) { + SinglePageAdapterT pageAdapter = getPageAdapterForIndex(pageNumber); + if (pageAdapter == null) { + return null; + } + return mListAdapterExtractor.apply(pageAdapter); + } + + private @Profile int getProfileForUserHandle(UserHandle userHandle) { + if (userHandle.equals(getCloneUserHandle())) { + // TODO: can we push this special case elsewhere -- e.g., when we check against each + // list adapter's user handle in the loop below, could we instead ask the list adapter + // whether it "represents" the queried user handle, and have the personal list adapter + // return true because it knows it's also associated with the clone profile? Or if we + // don't want to make modifications to the list adapter, maybe we could at least specify + // it in our per-page configuration data that we use to build our tabs/pages, and then + // maintain the relevant bookkeeping in our own ProfileDescriptor? + return PROFILE_PERSONAL; + } + for (int i = 0; i < mItems.size(); ++i) { + ListAdapterT listAdapter = getListAdapterForPageNumber(i); + if (listAdapter.getUserHandle().equals(userHandle)) { + return mItems.get(i).mProfile; + } + } + return -1; + } + + private int getPageNumberForUserHandle(UserHandle userHandle) { + return getPageNumberForProfile(getProfileForUserHandle(userHandle)); + } + + /** + * Returns the {@link ListAdapterT} instance of the profile that represents + * userHandle. If there is no such adapter for the specified + * userHandle, returns {@code null}. + *

For example, if there is a work profile on the device with user id 10, calling this method + * with UserHandle.of(10) returns the work profile {@link ListAdapterT}. + */ + @Nullable + public final ListAdapterT getListAdapterForUserHandle(UserHandle userHandle) { + return getListAdapterForPageNumber(getPageNumberForUserHandle(userHandle)); + } + + @Nullable + private ProfileDescriptor getDescriptorForUserHandle( + UserHandle userHandle) { + return getItem(getPageNumberForUserHandle(userHandle)); + } + private void updateActiveTabStyle(TabHost tabHost) { int currentTab = tabHost.getCurrentTab(); @@ -355,7 +413,11 @@ class MultiProfilePagerAdapter< * 1 would return the work profile {@link ProfileDescriptor}. * */ + @Nullable private ProfileDescriptor getItem(int pageIndex) { + if (!hasPageForIndex(pageIndex)) { + return null; + } return mItems.get(pageIndex); } @@ -387,7 +449,10 @@ class MultiProfilePagerAdapter< * depending on the adapter type. */ @VisibleForTesting - public final SinglePageAdapterT getAdapterForIndex(int index) { + public final SinglePageAdapterT getPageAdapterForIndex(int index) { + if (!hasPageForIndex(index)) { + return null; + } return getItem(index).mAdapter; } @@ -396,30 +461,7 @@ class MultiProfilePagerAdapter< * by pageIndex. */ public final void setupListAdapter(int pageIndex) { - mAdapterBinder.bind(getListViewForIndex(pageIndex), getAdapterForIndex(pageIndex)); - } - - /** - * Returns the {@link ListAdapterT} instance of the profile that represents - * userHandle. If there is no such adapter for the specified - * userHandle, returns {@code null}. - *

For example, if there is a work profile on the device with user id 10, calling this method - * with UserHandle.of(10) returns the work profile {@link ListAdapterT}. - */ - @Nullable - public final ListAdapterT getListAdapterForUserHandle(UserHandle userHandle) { - if (getPersonalListAdapter().getUserHandle().equals(userHandle) - || userHandle.equals(getCloneUserHandle())) { - return getPersonalListAdapter(); - } else if ((getWorkListAdapter() != null) - && getWorkListAdapter().getUserHandle().equals(userHandle)) { - return getWorkListAdapter(); - } - return null; - } - - private ListAdapterT getListAdapterForPageNumber(int pageNumber) { - return mListAdapterExtractor.apply(getAdapterForIndex(pageNumber)); + mAdapterBinder.bind(getListViewForIndex(pageIndex), getPageAdapterForIndex(pageIndex)); } /** @@ -437,11 +479,6 @@ class MultiProfilePagerAdapter< return getListAdapterForPageNumber(getPageNumberForProfile(PROFILE_PERSONAL)); } - /** @return whether our tab data contains a page for the specified {@code profile} ID. */ - public final boolean hasPageForProfile(@Profile int profile) { - return hasAdapterForIndex(getPageNumberForProfile(profile)); - } - @Nullable public final ListAdapterT getWorkListAdapter() { if (!hasPageForProfile(PROFILE_WORK)) { @@ -451,7 +488,7 @@ class MultiProfilePagerAdapter< } public final SinglePageAdapterT getCurrentRootAdapter() { - return getAdapterForIndex(getCurrentPage()); + return getPageAdapterForIndex(getCurrentPage()); } public final PageViewT getActiveAdapterView() { @@ -460,7 +497,7 @@ class MultiProfilePagerAdapter< private boolean anyAdapterHasItems() { for (int i = 0; i < mItems.size(); ++i) { - ListAdapterT listAdapter = mListAdapterExtractor.apply(getAdapterForIndex(i)); + ListAdapterT listAdapter = getListAdapterForPageNumber(i); if (listAdapter.getCount() > 0) { return true; } @@ -573,14 +610,6 @@ class MultiProfilePagerAdapter< return allRebuildsComplete.get(); } - private int userHandleToPageIndex(UserHandle userHandle) { - if (userHandle.equals(getPersonalListAdapter().getUserHandle())) { - return getPageNumberForProfile(PROFILE_PERSONAL); - } else { - return getPageNumberForProfile(PROFILE_WORK); - } - } - protected void forEachPage(Consumer pageNumberHandler) { for (int pageNumber = 0; pageNumber < getItemCount(); ++pageNumber) { pageNumberHandler.accept(pageNumber); @@ -608,10 +637,6 @@ class MultiProfilePagerAdapter< return emptyState != null && emptyState.shouldSkipDataRebuild(); } - private boolean hasAdapterForIndex(int pageIndex) { - return (pageIndex >= 0) && (pageIndex < getCount()); - } - /** * The empty state screens are shown according to their priority: *

    @@ -640,8 +665,8 @@ class MultiProfilePagerAdapter< if (emptyState.getButtonClickListener() != null) { clickListener = v -> emptyState.getButtonClickListener().onClick(() -> { - ProfileDescriptor descriptor = getItem( - userHandleToPageIndex(listAdapter.getUserHandle())); + ProfileDescriptor descriptor = + getDescriptorForUserHandle(listAdapter.getUserHandle()); descriptor.mEmptyStateUi.showSpinner(); }); } @@ -665,8 +690,8 @@ class MultiProfilePagerAdapter< ListAdapterT activeListAdapter, EmptyState emptyState, View.OnClickListener buttonOnClick) { - ProfileDescriptor descriptor = getItem( - userHandleToPageIndex(activeListAdapter.getUserHandle())); + ProfileDescriptor descriptor = + getDescriptorForUserHandle(activeListAdapter.getUserHandle()); descriptor.mEmptyStateUi.showEmptyState(emptyState, buttonOnClick); activeListAdapter.markTabLoaded(); } @@ -680,8 +705,8 @@ class MultiProfilePagerAdapter< } public void showListView(ListAdapterT activeListAdapter) { - ProfileDescriptor descriptor = getItem( - userHandleToPageIndex(activeListAdapter.getUserHandle())); + ProfileDescriptor descriptor = + getDescriptorForUserHandle(activeListAdapter.getUserHandle()); descriptor.mEmptyStateUi.hide(); } -- cgit v1.2.3-59-g8ed1b