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