Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame^] | 16 | package com.android.launcher3.widget.picker; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 17 | |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 18 | import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_Y; |
vadimt | 9f48a8e | 2019-12-11 11:52:37 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.testing.TestProtocol.NORMAL_STATE_ORDINAL; |
| 20 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| 23 | import android.animation.PropertyValuesHolder; |
| 24 | import android.content.Context; |
| 25 | import android.graphics.Rect; |
| 26 | import android.util.AttributeSet; |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 27 | import android.util.Pair; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
| 29 | import android.view.MotionEvent; |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 30 | import android.view.View; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 31 | import android.view.animation.AnimationUtils; |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 32 | import android.view.animation.Interpolator; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 33 | |
Tony Wickham | 9791bd1 | 2019-04-05 13:52:35 -0700 | [diff] [blame] | 34 | import androidx.annotation.VisibleForTesting; |
| 35 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 36 | import com.android.launcher3.Insettable; |
| 37 | import com.android.launcher3.Launcher; |
| 38 | import com.android.launcher3.LauncherAppState; |
| 39 | import com.android.launcher3.LauncherAppWidgetHost.ProviderChangedListener; |
| 40 | import com.android.launcher3.R; |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 41 | import com.android.launcher3.anim.PendingAnimation; |
vadimt | 9f48a8e | 2019-12-11 11:52:37 -0800 | [diff] [blame] | 42 | import com.android.launcher3.compat.AccessibilityManagerCompat; |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 43 | import com.android.launcher3.views.RecyclerViewFastScroller; |
| 44 | import com.android.launcher3.views.TopRoundedCornerView; |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame^] | 45 | import com.android.launcher3.widget.BaseWidgetSheet; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Popup for showing the full list of available widgets |
| 49 | */ |
| 50 | public class WidgetsFullSheet extends BaseWidgetSheet |
| 51 | implements Insettable, ProviderChangedListener { |
| 52 | |
| 53 | private static final long DEFAULT_OPEN_DURATION = 267; |
| 54 | private static final long FADE_IN_DURATION = 150; |
| 55 | private static final float VERTICAL_START_POSITION = 0.3f; |
| 56 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 57 | private final Rect mInsets = new Rect(); |
| 58 | |
| 59 | private final WidgetsListAdapter mAdapter; |
| 60 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 61 | private WidgetsRecyclerView mRecyclerView; |
| 62 | |
| 63 | public WidgetsFullSheet(Context context, AttributeSet attrs, int defStyleAttr) { |
| 64 | super(context, attrs, defStyleAttr); |
| 65 | LauncherAppState apps = LauncherAppState.getInstance(context); |
| 66 | mAdapter = new WidgetsListAdapter(context, |
| 67 | LayoutInflater.from(context), apps.getWidgetCache(), apps.getIconCache(), |
| 68 | this, this); |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 69 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public WidgetsFullSheet(Context context, AttributeSet attrs) { |
| 73 | this(context, attrs, 0); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void onFinishInflate() { |
| 78 | super.onFinishInflate(); |
| 79 | mContent = findViewById(R.id.container); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 80 | |
| 81 | mRecyclerView = findViewById(R.id.widgets_list_view); |
| 82 | mRecyclerView.setAdapter(mAdapter); |
| 83 | mAdapter.setApplyBitmapDeferred(true, mRecyclerView); |
| 84 | |
Sunny Goyal | 016d7e9 | 2018-03-09 11:09:20 -0800 | [diff] [blame] | 85 | TopRoundedCornerView springLayout = (TopRoundedCornerView) mContent; |
| 86 | springLayout.addSpringView(R.id.widgets_list_view); |
| 87 | mRecyclerView.setEdgeEffectFactory(springLayout.createEdgeEffectFactory()); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 88 | onWidgetsBound(); |
| 89 | } |
| 90 | |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 91 | @VisibleForTesting |
| 92 | public WidgetsRecyclerView getRecyclerView() { |
| 93 | return mRecyclerView; |
| 94 | } |
| 95 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 96 | @Override |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 97 | protected Pair<View, String> getAccessibilityTarget() { |
| 98 | return Pair.create(mRecyclerView, getContext().getString( |
| 99 | mIsOpen ? R.string.widgets_list : R.string.widgets_list_closed)); |
| 100 | } |
| 101 | |
| 102 | @Override |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 103 | protected void onAttachedToWindow() { |
| 104 | super.onAttachedToWindow(); |
| 105 | mLauncher.getAppWidgetHost().addProviderChangeListener(this); |
| 106 | notifyWidgetProvidersChanged(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected void onDetachedFromWindow() { |
| 111 | super.onDetachedFromWindow(); |
| 112 | mLauncher.getAppWidgetHost().removeProviderChangeListener(this); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void setInsets(Rect insets) { |
| 117 | mInsets.set(insets); |
| 118 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 119 | mRecyclerView.setPadding( |
| 120 | mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop(), |
| 121 | mRecyclerView.getPaddingRight(), insets.bottom); |
| 122 | if (insets.bottom > 0) { |
| 123 | setupNavBarColor(); |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 124 | } else { |
| 125 | clearNavBarColor(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 126 | } |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 127 | |
| 128 | ((TopRoundedCornerView) mContent).setNavBarScrimHeight(mInsets.bottom); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 129 | requestLayout(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 134 | int widthUsed; |
| 135 | if (mInsets.bottom > 0) { |
Sunny Goyal | 786940a | 2020-06-02 02:31:31 -0700 | [diff] [blame] | 136 | widthUsed = mInsets.left + mInsets.right; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 137 | } else { |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 138 | Rect padding = mLauncher.getDeviceProfile().workspacePadding; |
| 139 | widthUsed = Math.max(padding.left + padding.right, |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 140 | 2 * (mInsets.left + mInsets.right)); |
| 141 | } |
| 142 | |
| 143 | int heightUsed = mInsets.top + mLauncher.getDeviceProfile().edgeMarginPx; |
| 144 | measureChildWithMargins(mContent, widthMeasureSpec, |
| 145 | widthUsed, heightMeasureSpec, heightUsed); |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 146 | setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), |
| 147 | MeasureSpec.getSize(heightMeasureSpec)); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 152 | int width = r - l; |
| 153 | int height = b - t; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 154 | |
| 155 | // Content is laid out as center bottom aligned |
| 156 | int contentWidth = mContent.getMeasuredWidth(); |
Sunny Goyal | 786940a | 2020-06-02 02:31:31 -0700 | [diff] [blame] | 157 | int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 158 | mContent.layout(contentLeft, height - mContent.getMeasuredHeight(), |
| 159 | contentLeft + contentWidth, height); |
| 160 | |
| 161 | setTranslationShift(mTranslationShift); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void notifyWidgetProvidersChanged() { |
| 166 | mLauncher.refreshAndBindWidgetsForPackageUser(null); |
| 167 | } |
| 168 | |
| 169 | @Override |
Sunny Goyal | 202ae0b | 2019-02-04 16:26:42 -0800 | [diff] [blame] | 170 | public void onWidgetsBound() { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 171 | mAdapter.setWidgets(mLauncher.getPopupDataProvider().getAllWidgets()); |
| 172 | } |
| 173 | |
| 174 | private void open(boolean animate) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 175 | if (animate) { |
Sunny Goyal | db6cdb0 | 2018-07-13 11:03:04 -0700 | [diff] [blame] | 176 | if (getPopupContainer().getInsets().bottom > 0) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 177 | mContent.setAlpha(0); |
| 178 | setTranslationShift(VERTICAL_START_POSITION); |
| 179 | } |
| 180 | mOpenCloseAnimator.setValues( |
| 181 | PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED)); |
| 182 | mOpenCloseAnimator |
| 183 | .setDuration(DEFAULT_OPEN_DURATION) |
| 184 | .setInterpolator(AnimationUtils.loadInterpolator( |
| 185 | getContext(), android.R.interpolator.linear_out_slow_in)); |
vadimt | 9915a7c | 2020-04-08 18:56:52 -0700 | [diff] [blame] | 186 | mRecyclerView.setLayoutFrozen(true); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 187 | mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() { |
| 188 | @Override |
| 189 | public void onAnimationEnd(Animator animation) { |
| 190 | mRecyclerView.setLayoutFrozen(false); |
| 191 | mAdapter.setApplyBitmapDeferred(false, mRecyclerView); |
| 192 | mOpenCloseAnimator.removeListener(this); |
| 193 | } |
| 194 | }); |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 195 | post(() -> { |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 196 | mOpenCloseAnimator.start(); |
| 197 | mContent.animate().alpha(1).setDuration(FADE_IN_DURATION); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 198 | }); |
| 199 | } else { |
| 200 | setTranslationShift(TRANSLATION_SHIFT_OPENED); |
| 201 | mAdapter.setApplyBitmapDeferred(false, mRecyclerView); |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 202 | post(this::announceAccessibilityChanges); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | protected void handleClose(boolean animate) { |
| 208 | handleClose(animate, DEFAULT_OPEN_DURATION); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | protected boolean isOfType(int type) { |
| 213 | return (type & TYPE_WIDGETS_FULL_SHEET) != 0; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public boolean onControllerInterceptTouchEvent(MotionEvent ev) { |
| 218 | // Disable swipe down when recycler view is scrolling |
| 219 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 220 | mNoIntercept = false; |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 221 | RecyclerViewFastScroller scroller = mRecyclerView.getScrollbar(); |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame^] | 222 | if (scroller.getThumbOffsetY() >= 0 |
| 223 | && getPopupContainer().isEventOverView(scroller, ev)) { |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 224 | mNoIntercept = true; |
Sunny Goyal | db6cdb0 | 2018-07-13 11:03:04 -0700 | [diff] [blame] | 225 | } else if (getPopupContainer().isEventOverView(mContent, ev)) { |
| 226 | mNoIntercept = !mRecyclerView.shouldContainerScroll(ev, getPopupContainer()); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | return super.onControllerInterceptTouchEvent(ev); |
| 230 | } |
| 231 | |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame^] | 232 | /** Shows the {@link WidgetsFullSheet} on the launcher. */ |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 233 | public static WidgetsFullSheet show(Launcher launcher, boolean animate) { |
| 234 | WidgetsFullSheet sheet = (WidgetsFullSheet) launcher.getLayoutInflater() |
| 235 | .inflate(R.layout.widgets_full_sheet, launcher.getDragLayer(), false); |
Sunny Goyal | 1642f71 | 2018-09-18 11:40:35 -0700 | [diff] [blame] | 236 | sheet.attachToContainer(); |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 237 | sheet.mIsOpen = true; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 238 | sheet.open(animate); |
| 239 | return sheet; |
| 240 | } |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 241 | |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame^] | 242 | /** Gets the {@link WidgetsRecyclerView} which shows all widgets in {@link WidgetsFullSheet}. */ |
Vadim Tryshev | 2ce6a13 | 2018-06-18 19:14:44 -0700 | [diff] [blame] | 243 | @VisibleForTesting |
| 244 | public static WidgetsRecyclerView getWidgetsView(Launcher launcher) { |
| 245 | return launcher.findViewById(R.id.widgets_list_view); |
| 246 | } |
| 247 | |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 248 | @Override |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 249 | public void addHintCloseAnim( |
| 250 | float distanceToMove, Interpolator interpolator, PendingAnimation target) { |
| 251 | target.setFloat(mRecyclerView, VIEW_TRANSLATE_Y, -distanceToMove, interpolator); |
| 252 | target.setViewAlpha(mRecyclerView, 0.5f, interpolator); |
Tony Wickham | 9791bd1 | 2019-04-05 13:52:35 -0700 | [diff] [blame] | 253 | } |
vadimt | 9f48a8e | 2019-12-11 11:52:37 -0800 | [diff] [blame] | 254 | |
| 255 | @Override |
| 256 | protected void onCloseComplete() { |
| 257 | super.onCloseComplete(); |
| 258 | AccessibilityManagerCompat.sendStateEventToTest(getContext(), NORMAL_STATE_ORDINAL); |
| 259 | } |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 260 | } |