diff options
author | 2023-12-14 20:41:03 +0000 | |
---|---|---|
committer | 2023-12-14 22:22:52 +0000 | |
commit | 91c38786bac73496d8437f83741587120199cbfe (patch) | |
tree | 875e336736a9f0d800283688b06ea5d30caaa1d1 | |
parent | 3007d9f481e92ed57ca9e3783719b3d84797ef2c (diff) |
Favor 'profiles' over 'user handles'/'page numbers'
(Or at least, a first couple changes in that direction.)
Historically the pager-adapter 'profiles' and 'page numbers' have been
equivalent, but if we're trying to refer to a specific profile, we
shouldn't have to know its mapped page number.
These changes are as prototyped in ag/25335069 and described in
go/chooser-ntab-refactoring, in particular replicating the changes of
"snapshot 16" and "snapshot 18." There are more changes planned in
this general direction, but these first two happened to be
dependencies of unrelated work in the same overall refactoring effort,
so I wanted to get them in first.
The first snapshot just switches over a few obvious cases. The second
starts to introduce some distinction in the pager-adapter between
"profiles" and "page numbers," which had historically been equivalent.
There are no behavior changes so far, but this sets up forward-looking
"internal APIs" (helper methods) that we'll go on to use in subsequent
CLs to adapt the remaining usages. See the prototype for more context.
Bug: 310211468
Test: `ResolverActivityTest`/activity tests/presubmit
Change-Id: I226e3801da4514f495a1781a98fa5b4630506163
3 files changed, 27 insertions, 27 deletions
diff --git a/java/src/com/android/intentresolver/v2/ChooserActivity.java b/java/src/com/android/intentresolver/v2/ChooserActivity.java index 70812642..b791b1c1 100644 --- a/java/src/com/android/intentresolver/v2/ChooserActivity.java +++ b/java/src/com/android/intentresolver/v2/ChooserActivity.java @@ -787,10 +787,7 @@ public class ChooserActivity extends Hilt_ChooserActivity implements ChooserRequestParameters chooserRequest = requireChooserRequest(); if (!chooserRequest.getCallerChooserTargets().isEmpty()) { // Send the caller's chooser targets only to the default profile. - UserHandle defaultUser = (findSelectedProfile() == PROFILE_WORK) - ? requireAnnotatedUserHandles().workProfileUserHandle - : requireAnnotatedUserHandles().personalProfileUserHandle; - if (mChooserMultiProfilePagerAdapter.getCurrentUserHandle() == defaultUser) { + if (mChooserMultiProfilePagerAdapter.getActiveProfile() == findSelectedProfile()) { mChooserMultiProfilePagerAdapter.getActiveListAdapter().addServiceResults( /* origTarget */ null, new ArrayList<>(chooserRequest.getCallerChooserTargets()), @@ -1404,8 +1401,7 @@ public class ChooserActivity extends Hilt_ChooserActivity implements updateTabPadding(); } - UserHandle currentUserHandle = mChooserMultiProfilePagerAdapter.getCurrentUserHandle(); - int currentProfile = getProfileForUser(currentUserHandle); + int currentProfile = mChooserMultiProfilePagerAdapter.getActiveProfile(); int initialProfile = findSelectedProfile(); if (currentProfile != initialProfile) { return; diff --git a/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java b/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java index 2d9be816..aa161921 100644 --- a/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java +++ b/java/src/com/android/intentresolver/v2/MultiProfilePagerAdapter.java @@ -140,6 +140,14 @@ public class MultiProfilePagerAdapter< mPageViewInflater.get(), adapter, containerBottomPaddingOverrideSupplier); } + private @Profile int getProfileForPageNumber(int position) { + return position; + } + + private int getPageNumberForProfile(@Profile int profile) { + return profile; + } + public void setOnProfileSelectedListener(OnProfileSelectedListener listener) { mOnProfileSelectedListener = listener; } @@ -205,11 +213,7 @@ public class MultiProfilePagerAdapter< } public final @Profile int getActiveProfile() { - // TODO: here and elsewhere in this class, distinguish between a "profile ID" integer and - // its mapped "page index." When we support more than two profiles, this won't be a "stable - // mapping" -- some particular profile may not be represented by a "page," but the ones that - // are will be assigned contiguous page numbers that skip over the holes. - return getCurrentPage(); + return getProfileForPageNumber(getCurrentPage()); } @VisibleForTesting @@ -304,6 +308,10 @@ public class MultiProfilePagerAdapter< return null; } + private ListAdapterT getListAdapterForPageNumber(int pageNumber) { + return mListAdapterExtractor.apply(getAdapterForIndex(pageNumber)); + } + /** * Returns the {@link ListAdapterT} instance of the profile that is currently visible * to the user. @@ -313,7 +321,7 @@ public class MultiProfilePagerAdapter< */ @VisibleForTesting public final ListAdapterT getActiveListAdapter() { - return mListAdapterExtractor.apply(getAdapterForIndex(getCurrentPage())); + return getListAdapterForPageNumber(getCurrentPage()); } /** @@ -330,28 +338,24 @@ public class MultiProfilePagerAdapter< if (getCount() < 2) { return null; } - return mListAdapterExtractor.apply(getAdapterForIndex(1 - getCurrentPage())); + return getListAdapterForPageNumber(1 - getCurrentPage()); } public final ListAdapterT getPersonalListAdapter() { - return mListAdapterExtractor.apply(getAdapterForIndex(PROFILE_PERSONAL)); + 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) { - // TODO: here and elsewhere in this class, distinguish between a "profile ID" integer and - // its mapped "page index." When we support more than two profiles, this won't be a "stable - // mapping" -- some particular profile may not be represented by a "page," but the ones that - // are will be assigned contiguous page numbers that skip over the holes. - return hasAdapterForIndex(profile); + return hasAdapterForIndex(getPageNumberForProfile(profile)); } @Nullable public final ListAdapterT getWorkListAdapter() { - if (!hasAdapterForIndex(PROFILE_WORK)) { + if (!hasPageForProfile(PROFILE_WORK)) { return null; } - return mListAdapterExtractor.apply(getAdapterForIndex(PROFILE_WORK)); + return getListAdapterForPageNumber(getPageNumberForProfile(PROFILE_WORK)); } public final SinglePageAdapterT getCurrentRootAdapter() { @@ -480,9 +484,9 @@ public class MultiProfilePagerAdapter< private int userHandleToPageIndex(UserHandle userHandle) { if (userHandle.equals(getPersonalListAdapter().getUserHandle())) { - return PROFILE_PERSONAL; + return getPageNumberForProfile(PROFILE_PERSONAL); } else { - return PROFILE_WORK; + return getPageNumberForProfile(PROFILE_WORK); } } @@ -500,7 +504,7 @@ public class MultiProfilePagerAdapter< } private boolean hasAdapterForIndex(int pageIndex) { - return (pageIndex < getCount()); + return (pageIndex >= 0) && (pageIndex < getCount()); } /** diff --git a/java/src/com/android/intentresolver/v2/ResolverActivity.java b/java/src/com/android/intentresolver/v2/ResolverActivity.java index 2ba50ec3..cc91e9bf 100644 --- a/java/src/com/android/intentresolver/v2/ResolverActivity.java +++ b/java/src/com/android/intentresolver/v2/ResolverActivity.java @@ -934,8 +934,7 @@ public class ResolverActivity extends FragmentActivity implements } protected Unit onWorkProfileStatusUpdated() { - if (mMultiProfilePagerAdapter.getCurrentUserHandle().equals( - requireAnnotatedUserHandles().workProfileUserHandle)) { + if (mMultiProfilePagerAdapter.getActiveProfile() == PROFILE_WORK) { mMultiProfilePagerAdapter.rebuildActiveTab(true); } else { mMultiProfilePagerAdapter.clearInactiveProfileCache(); @@ -1358,7 +1357,8 @@ public class ResolverActivity extends FragmentActivity implements // In case of clonedProfile being active, we do not allow the 'Always' option in the // disambiguation dialog of Personal Profile as the package manager cannot distinguish // between cross-profile preferred activities. - if (hasCloneProfile() && (mMultiProfilePagerAdapter.getCurrentPage() == PROFILE_PERSONAL)) { + if (hasCloneProfile() + && (mMultiProfilePagerAdapter.getActiveProfile() == PROFILE_PERSONAL)) { mAlwaysButton.setEnabled(false); return; } |