diff options
| author | 2022-12-01 21:08:52 +0000 | |
|---|---|---|
| committer | 2022-12-01 21:08:52 +0000 | |
| commit | bdef3194721acaaed5af97c357aae161df2b4c24 (patch) | |
| tree | 79d69f42e02974d7af307d8504b504694ce3aaaa /java/src | |
| parent | 3bd1033da86ba26d922e19a21bb21dec4bf7188f (diff) | |
| parent | 36202194c114d2f79c17f930f45e337e922839ba (diff) | |
Merge "Pre-work to decouple ChooserGridAdapter" into tm-qpr-dev
Diffstat (limited to 'java/src')
| -rw-r--r-- | java/src/com/android/intentresolver/ChooserActivity.java | 235 | ||||
| -rw-r--r-- | java/src/com/android/intentresolver/grid/DirectShareViewHolder.java | 10 |
2 files changed, 183 insertions, 62 deletions
diff --git a/java/src/com/android/intentresolver/ChooserActivity.java b/java/src/com/android/intentresolver/ChooserActivity.java index 5535d987..6b7bf614 100644 --- a/java/src/com/android/intentresolver/ChooserActivity.java +++ b/java/src/com/android/intentresolver/ChooserActivity.java @@ -1631,7 +1631,78 @@ public class ChooserActivity extends ResolverActivity implements getTargetIntent(), mChooserRequest, mMaxTargetsPerRow); - return new ChooserGridAdapter(chooserListAdapter); + + return new ChooserGridAdapter( + context, + new ChooserGridAdapter.ChooserActivityDelegate() { + @Override + public boolean shouldShowTabs() { + return ChooserActivity.this.shouldShowTabs(); + } + + @Override + public View buildContentPreview(ViewGroup parent) { + return createContentPreviewView(parent, mPreviewCoordinator); + } + + @Override + public void onTargetSelected(int itemIndex) { + startSelected(itemIndex, false, true); + } + + @Override + public void onTargetLongPressed(int selectedPosition) { + final TargetInfo longPressedTargetInfo = + mChooserMultiProfilePagerAdapter + .getActiveListAdapter() + .targetInfoForPosition( + selectedPosition, /* filtered= */ true); + // ItemViewHolder contents should always be "display resolve info" + // targets, but check just to make sure. + if (longPressedTargetInfo.isDisplayResolveInfo()) { + showTargetDetails(longPressedTargetInfo); + } + } + + @Override + public void updateProfileViewButton(View newButtonFromProfileRow) { + mProfileView = newButtonFromProfileRow; + mProfileView.setOnClickListener(ChooserActivity.this::onProfileClick); + ChooserActivity.this.updateProfileViewButton(); + } + + @Override + public int getValidTargetCount() { + return mChooserMultiProfilePagerAdapter + .getActiveListAdapter() + .getSelectableServiceTargetCount(); + } + + @Override + public void updateDirectShareExpansion(DirectShareViewHolder directShareGroup) { + RecyclerView activeAdapterView = + mChooserMultiProfilePagerAdapter.getActiveAdapterView(); + if (mResolverDrawerLayout.isCollapsed()) { + directShareGroup.collapse(activeAdapterView); + } else { + directShareGroup.expand(activeAdapterView); + } + } + + @Override + public void handleScrollToExpandDirectShare( + DirectShareViewHolder directShareGroup, int y, int oldy) { + directShareGroup.handleScroll( + mChooserMultiProfilePagerAdapter.getActiveAdapterView(), + y, + oldy, + mMaxTargetsPerRow); + } + }, + chooserListAdapter, + shouldShowContentPreview(), + mMaxTargetsPerRow, + getNumSheetExpansions()); } @VisibleForTesting @@ -2205,15 +2276,63 @@ public class ChooserActivity extends ResolverActivity implements * handled by {@link ChooserListAdapter} */ @VisibleForTesting - public final class ChooserGridAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { - private final ChooserListAdapter mChooserListAdapter; - private final LayoutInflater mLayoutInflater; - private final boolean mShowAzLabelIfPoss; + public static final class ChooserGridAdapter extends + RecyclerView.Adapter<RecyclerView.ViewHolder> { - private DirectShareViewHolder mDirectShareViewHolder; - private int mChooserTargetWidth = 0; - - private int mFooterHeight = 0; + /** + * Injectable interface for any considerations that should be delegated to other components + * in the {@link ChooserActivity}. + * TODO: determine whether any of these methods return parameters that can safely be + * precomputed; whether any should be converted to `ChooserGridAdapter` setters to be + * invoked by external callbacks; and whether any reflect requirements that should be moved + * out of `ChooserGridAdapter` altogether. + */ + interface ChooserActivityDelegate { + /** @return whether we're showing a tabbed (multi-profile) UI. */ + boolean shouldShowTabs(); + + /** + * @return a content preview {@link View} that's appropriate for the caller's share + * content, constructed for display in the provided {@code parent} group. + */ + View buildContentPreview(ViewGroup parent); + + /** Notify the client that the item with the selected {@code itemIndex} was selected. */ + void onTargetSelected(int itemIndex); + + /** + * Notify the client that the item with the selected {@code itemIndex} was + * long-pressed. + */ + void onTargetLongPressed(int itemIndex); + + /** + * Notify the client that the provided {@code View} should be configured as the new + * "profile view" button. Callers should attach their own click listeners to implement + * behaviors on this view. + */ + void updateProfileViewButton(View newButtonFromProfileRow); + + /** + * @return the number of "valid" targets in the active list adapter. + * TODO: define "valid." + */ + int getValidTargetCount(); + + /** + * Request that the client update our {@code directShareGroup} to match their desired + * state for the "expansion" UI. + */ + void updateDirectShareExpansion(DirectShareViewHolder directShareGroup); + + /** + * Request that the client handle a scroll event that should be taken as expanding the + * provided {@code directShareGroup}. Note that this currently never happens due to a + * hard-coded condition in {@link #canExpandDirectShare()}. + */ + void handleScrollToExpandDirectShare( + DirectShareViewHolder directShareGroup, int y, int oldy); + } private static final int VIEW_TYPE_DIRECT_SHARE = 0; private static final int VIEW_TYPE_NORMAL = 1; @@ -2225,12 +2344,44 @@ public class ChooserActivity extends ResolverActivity implements private static final int NUM_EXPANSIONS_TO_HIDE_AZ_LABEL = 20; - ChooserGridAdapter(ChooserListAdapter wrappedAdapter) { + private final ChooserActivityDelegate mChooserActivityDelegate; + private final ChooserListAdapter mChooserListAdapter; + private final LayoutInflater mLayoutInflater; + + private final int mMaxTargetsPerRow; + private final boolean mShouldShowContentPreview; + private final int mChooserWidthPixels; + private final int mChooserRowTextOptionTranslatePixelSize; + private final boolean mShowAzLabelIfPoss; + + private DirectShareViewHolder mDirectShareViewHolder; + private int mChooserTargetWidth = 0; + + private int mFooterHeight = 0; + + ChooserGridAdapter( + Context context, + ChooserActivityDelegate chooserActivityDelegate, + ChooserListAdapter wrappedAdapter, + boolean shouldShowContentPreview, + int maxTargetsPerRow, + int numSheetExpansions) { super(); + + mChooserActivityDelegate = chooserActivityDelegate; + mChooserListAdapter = wrappedAdapter; - mLayoutInflater = LayoutInflater.from(ChooserActivity.this); + mLayoutInflater = LayoutInflater.from(context); + + mShouldShowContentPreview = shouldShowContentPreview; + mMaxTargetsPerRow = maxTargetsPerRow; + + mChooserWidthPixels = context.getResources().getDimensionPixelSize( + R.dimen.chooser_width); + mChooserRowTextOptionTranslatePixelSize = context.getResources().getDimensionPixelSize( + R.dimen.chooser_row_text_option_translate); - mShowAzLabelIfPoss = getNumSheetExpansions() < NUM_EXPANSIONS_TO_HIDE_AZ_LABEL; + mShowAzLabelIfPoss = numSheetExpansions < NUM_EXPANSIONS_TO_HIDE_AZ_LABEL; wrappedAdapter.registerDataSetObserver(new DataSetObserver() { @Override @@ -2263,7 +2414,7 @@ public class ChooserActivity extends ResolverActivity implements } // Limit width to the maximum width of the chooser activity - int maxWidth = getResources().getDimensionPixelSize(R.dimen.chooser_width); + int maxWidth = mChooserWidthPixels; width = Math.min(maxWidth, width); int newWidth = width / mMaxTargetsPerRow; @@ -2295,11 +2446,11 @@ public class ChooserActivity extends ResolverActivity implements public int getSystemRowCount() { // For the tabbed case we show the sticky content preview above the tabs, // please refer to shouldShowStickyContentPreview - if (shouldShowTabs()) { + if (mChooserActivityDelegate.shouldShowTabs()) { return 0; } - if (!shouldShowContentPreview()) { + if (!mShouldShowContentPreview) { return 0; } @@ -2311,7 +2462,7 @@ public class ChooserActivity extends ResolverActivity implements } public int getProfileRowCount() { - if (shouldShowTabs()) { + if (mChooserActivityDelegate.shouldShowTabs()) { return 0; } return mChooserListAdapter.getOtherProfile() == null ? 0 : 1; @@ -2330,8 +2481,7 @@ public class ChooserActivity extends ResolverActivity implements // There can be at most one row in the listview, that is internally // a ViewGroup with 2 rows public int getServiceTargetRowCount() { - if (shouldShowContentPreview() - && !ActivityManager.isLowRamDeviceStatic()) { + if (mShouldShowContentPreview && !ActivityManager.isLowRamDeviceStatic()) { return 1; } return 0; @@ -2360,7 +2510,7 @@ public class ChooserActivity extends ResolverActivity implements switch (viewType) { case VIEW_TYPE_CONTENT_PREVIEW: return new ItemViewHolder( - createContentPreviewView(parent, mPreviewCoordinator), + mChooserActivityDelegate.buildContentPreview(parent), viewType, null, null); @@ -2380,22 +2530,8 @@ public class ChooserActivity extends ResolverActivity implements return new ItemViewHolder( mChooserListAdapter.createView(parent), viewType, - selectedPosition -> startSelected( - selectedPosition, - /* always= */ false, - /* filtered= */ true), - selectedPosition -> { - final TargetInfo longPressedTargetInfo = - mChooserMultiProfilePagerAdapter - .getActiveListAdapter() - .targetInfoForPosition( - selectedPosition, /* filtered= */ true); - // ItemViewHolder contents should always be "display resolve info" - // targets, but check just to make sure. - if (longPressedTargetInfo.isDisplayResolveInfo()) { - showTargetDetails(longPressedTargetInfo); - } - }); + mChooserActivityDelegate::onTargetSelected, + mChooserActivityDelegate::onTargetLongPressed); case VIEW_TYPE_DIRECT_SHARE: case VIEW_TYPE_CALLER_AND_RANK: return createItemGroupViewHolder(viewType, parent); @@ -2455,9 +2591,7 @@ public class ChooserActivity extends ResolverActivity implements private View createProfileView(ViewGroup parent) { View profileRow = mLayoutInflater.inflate(R.layout.chooser_profile_row, parent, false); - mProfileView = profileRow.findViewById(com.android.internal.R.id.profile_button); - mProfileView.setOnClickListener(ChooserActivity.this::onProfileClick); - updateProfileViewButton(); + mChooserActivityDelegate.updateProfileViewButton(profileRow); return profileRow; } @@ -2479,15 +2613,13 @@ public class ChooserActivity extends ResolverActivity implements v.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - startSelected(holder.getItemIndex(column), false, true); + mChooserActivityDelegate.onTargetSelected(holder.getItemIndex(column)); } }); // Show menu for both direct share and app share targets after long click. v.setOnLongClickListener(v1 -> { - TargetInfo ti = mChooserListAdapter.targetInfoForPosition( - holder.getItemIndex(column), true); - showTargetDetails(ti); + mChooserActivityDelegate.onTargetLongPressed(holder.getItemIndex(column)); return true; }); @@ -2548,7 +2680,7 @@ public class ChooserActivity extends ResolverActivity implements mDirectShareViewHolder = new DirectShareViewHolder(parentGroup, Lists.newArrayList(row1, row2), mMaxTargetsPerRow, viewType, - mChooserMultiProfilePagerAdapter::getActiveListAdapter); + mChooserActivityDelegate::getValidTargetCount); loadViewsIntoGroup(mDirectShareViewHolder); return mDirectShareViewHolder; @@ -2614,9 +2746,7 @@ public class ChooserActivity extends ResolverActivity implements ValueAnimator fadeAnim = ObjectAnimator.ofFloat(textView, "alpha", 0.0f, 1.0f); fadeAnim.setInterpolator(new DecelerateInterpolator(1.0f)); - float translationInPx = getResources().getDimensionPixelSize( - R.dimen.chooser_row_text_option_translate); - textView.setTranslationY(translationInPx); + textView.setTranslationY(mChooserRowTextOptionTranslatePixelSize); ValueAnimator translateAnim = ObjectAnimator.ofFloat(textView, "translationY", 0.0f); translateAnim.setInterpolator(new DecelerateInterpolator(1.0f)); @@ -2668,9 +2798,8 @@ public class ChooserActivity extends ResolverActivity implements public void handleScroll(View v, int y, int oldy) { boolean canExpandDirectShare = canExpandDirectShare(); if (mDirectShareViewHolder != null && canExpandDirectShare) { - mDirectShareViewHolder.handleScroll( - mChooserMultiProfilePagerAdapter.getActiveAdapterView(), y, oldy, - mMaxTargetsPerRow); + mChooserActivityDelegate.handleScrollToExpandDirectShare( + mDirectShareViewHolder, y, oldy); } } @@ -2695,13 +2824,7 @@ public class ChooserActivity extends ResolverActivity implements if (mDirectShareViewHolder == null || !canExpandDirectShare()) { return; } - RecyclerView activeAdapterView = - mChooserMultiProfilePagerAdapter.getActiveAdapterView(); - if (mResolverDrawerLayout.isCollapsed()) { - mDirectShareViewHolder.collapse(activeAdapterView); - } else { - mDirectShareViewHolder.expand(activeAdapterView); - } + mChooserActivityDelegate.updateDirectShareExpansion(mDirectShareViewHolder); } } diff --git a/java/src/com/android/intentresolver/grid/DirectShareViewHolder.java b/java/src/com/android/intentresolver/grid/DirectShareViewHolder.java index 95c61e3a..cfd54697 100644 --- a/java/src/com/android/intentresolver/grid/DirectShareViewHolder.java +++ b/java/src/com/android/intentresolver/grid/DirectShareViewHolder.java @@ -28,7 +28,6 @@ import android.view.animation.AccelerateInterpolator; import androidx.recyclerview.widget.RecyclerView; import com.android.intentresolver.ChooserActivity; -import com.android.intentresolver.ChooserListAdapter; import java.util.Arrays; import java.util.List; @@ -47,14 +46,14 @@ public class DirectShareViewHolder extends ItemGroupViewHolder { private final boolean[] mCellVisibility; - private final Supplier<ChooserListAdapter> mListAdapterSupplier; + private final Supplier<Integer> mDeferredTargetCountSupplier; public DirectShareViewHolder( ViewGroup parent, List<ViewGroup> rows, int cellCountPerRow, int viewType, - Supplier<ChooserListAdapter> listAdapterSupplier) { + Supplier<Integer> deferredTargetCountSupplier) { super(rows.size() * cellCountPerRow, parent, viewType); this.mParent = parent; @@ -62,7 +61,7 @@ public class DirectShareViewHolder extends ItemGroupViewHolder { this.mCellCountPerRow = cellCountPerRow; this.mCellVisibility = new boolean[rows.size() * cellCountPerRow]; Arrays.fill(mCellVisibility, true); - this.mListAdapterSupplier = listAdapterSupplier; + this.mDeferredTargetCountSupplier = deferredTargetCountSupplier; } public ViewGroup addView(int index, View v) { @@ -136,8 +135,7 @@ public class DirectShareViewHolder extends ItemGroupViewHolder { // only expand if we have more than maxTargetsPerRow, and delay that decision // until they start to scroll - ChooserListAdapter adapter = mListAdapterSupplier.get(); - int validTargets = adapter.getSelectableServiceTargetCount(); + final int validTargets = this.mDeferredTargetCountSupplier.get(); if (validTargets <= maxTargetsPerRow) { mHideDirectShareExpansion = true; return; |