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