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