blob: 68a3ec51ee1aab24d7dc7bf77259fe2b7ddb774c [file] [log] [blame]
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -07001/*
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 */
16package com.android.launcher3.widget;
17
Sunny Goyalf3ac7032020-03-13 13:01:33 -070018import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_Y;
vadimt9f48a8e2019-12-11 11:52:37 -080019import static com.android.launcher3.testing.TestProtocol.NORMAL_STATE_ORDINAL;
20
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
23import android.animation.PropertyValuesHolder;
24import android.content.Context;
25import android.graphics.Rect;
26import android.util.AttributeSet;
Sunny Goyalde753212018-05-15 13:55:57 -070027import android.util.Pair;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070028import android.view.LayoutInflater;
29import android.view.MotionEvent;
Sunny Goyalde753212018-05-15 13:55:57 -070030import android.view.View;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070031import android.view.animation.AnimationUtils;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070032import android.view.animation.Interpolator;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070033
Tony Wickham9791bd12019-04-05 13:52:35 -070034import androidx.annotation.VisibleForTesting;
35
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070036import com.android.launcher3.Insettable;
37import com.android.launcher3.Launcher;
38import com.android.launcher3.LauncherAppState;
39import com.android.launcher3.LauncherAppWidgetHost.ProviderChangedListener;
40import com.android.launcher3.R;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070041import com.android.launcher3.anim.PendingAnimation;
vadimt9f48a8e2019-12-11 11:52:37 -080042import com.android.launcher3.compat.AccessibilityManagerCompat;
Sunny Goyal3661bfa2018-03-01 16:29:15 -080043import com.android.launcher3.views.RecyclerViewFastScroller;
44import com.android.launcher3.views.TopRoundedCornerView;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070045
46/**
47 * Popup for showing the full list of available widgets
48 */
49public 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 Goyalf1fbc3f2017-10-10 15:21:15 -070056 private final Rect mInsets = new Rect();
57
58 private final WidgetsListAdapter mAdapter;
59
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070060 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 Goyalde753212018-05-15 13:55:57 -070068
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070069 }
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 Goyalf1fbc3f2017-10-10 15:21:15 -070079
80 mRecyclerView = findViewById(R.id.widgets_list_view);
81 mRecyclerView.setAdapter(mAdapter);
82 mAdapter.setApplyBitmapDeferred(true, mRecyclerView);
83
Sunny Goyal016d7e92018-03-09 11:09:20 -080084 TopRoundedCornerView springLayout = (TopRoundedCornerView) mContent;
85 springLayout.addSpringView(R.id.widgets_list_view);
86 mRecyclerView.setEdgeEffectFactory(springLayout.createEdgeEffectFactory());
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070087 onWidgetsBound();
88 }
89
Sunny Goyal8b37c572020-03-31 12:12:14 -070090 @VisibleForTesting
91 public WidgetsRecyclerView getRecyclerView() {
92 return mRecyclerView;
93 }
94
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070095 @Override
Sunny Goyalde753212018-05-15 13:55:57 -070096 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 Goyalf1fbc3f2017-10-10 15:21:15 -0700102 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 Goyalf1fbc3f2017-10-10 15:21:15 -0700118 mRecyclerView.setPadding(
119 mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop(),
120 mRecyclerView.getPaddingRight(), insets.bottom);
121 if (insets.bottom > 0) {
122 setupNavBarColor();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800123 } else {
124 clearNavBarColor();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700125 }
Sunny Goyal3661bfa2018-03-01 16:29:15 -0800126
127 ((TopRoundedCornerView) mContent).setNavBarScrimHeight(mInsets.bottom);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700128 requestLayout();
129 }
130
131 @Override
132 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
133 int widthUsed;
134 if (mInsets.bottom > 0) {
Sunny Goyal786940a2020-06-02 02:31:31 -0700135 widthUsed = mInsets.left + mInsets.right;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700136 } else {
Sunny Goyal07b69292018-01-08 14:19:34 -0800137 Rect padding = mLauncher.getDeviceProfile().workspacePadding;
138 widthUsed = Math.max(padding.left + padding.right,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700139 2 * (mInsets.left + mInsets.right));
140 }
141
142 int heightUsed = mInsets.top + mLauncher.getDeviceProfile().edgeMarginPx;
143 measureChildWithMargins(mContent, widthMeasureSpec,
144 widthUsed, heightMeasureSpec, heightUsed);
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800145 setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
146 MeasureSpec.getSize(heightMeasureSpec));
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700147 }
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 Goyalf1fbc3f2017-10-10 15:21:15 -0700153
154 // Content is laid out as center bottom aligned
155 int contentWidth = mContent.getMeasuredWidth();
Sunny Goyal786940a2020-06-02 02:31:31 -0700156 int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700157 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 Goyal202ae0b2019-02-04 16:26:42 -0800169 public void onWidgetsBound() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700170 mAdapter.setWidgets(mLauncher.getPopupDataProvider().getAllWidgets());
171 }
172
173 private void open(boolean animate) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700174 if (animate) {
Sunny Goyaldb6cdb02018-07-13 11:03:04 -0700175 if (getPopupContainer().getInsets().bottom > 0) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700176 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));
vadimt9915a7c2020-04-08 18:56:52 -0700185 mRecyclerView.setLayoutFrozen(true);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700186 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 Goyal6639a5d2018-02-28 15:09:36 -0800194 post(() -> {
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800195 mOpenCloseAnimator.start();
196 mContent.animate().alpha(1).setDuration(FADE_IN_DURATION);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700197 });
198 } else {
199 setTranslationShift(TRANSLATION_SHIFT_OPENED);
200 mAdapter.setApplyBitmapDeferred(false, mRecyclerView);
Sunny Goyalde753212018-05-15 13:55:57 -0700201 post(this::announceAccessibilityChanges);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700202 }
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 Goyal3661bfa2018-03-01 16:29:15 -0800220 RecyclerViewFastScroller scroller = mRecyclerView.getScrollbar();
221 if (scroller.getThumbOffsetY() >= 0 &&
Sunny Goyaldb6cdb02018-07-13 11:03:04 -0700222 getPopupContainer().isEventOverView(scroller, ev)) {
Sunny Goyal3661bfa2018-03-01 16:29:15 -0800223 mNoIntercept = true;
Sunny Goyaldb6cdb02018-07-13 11:03:04 -0700224 } else if (getPopupContainer().isEventOverView(mContent, ev)) {
225 mNoIntercept = !mRecyclerView.shouldContainerScroll(ev, getPopupContainer());
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700226 }
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 Goyal1642f712018-09-18 11:40:35 -0700234 sheet.attachToContainer();
Sunny Goyalde753212018-05-15 13:55:57 -0700235 sheet.mIsOpen = true;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700236 sheet.open(animate);
237 return sheet;
238 }
Sunny Goyalaeb16432017-10-16 11:46:41 -0700239
Vadim Tryshev2ce6a132018-06-18 19:14:44 -0700240 @VisibleForTesting
241 public static WidgetsRecyclerView getWidgetsView(Launcher launcher) {
242 return launcher.findViewById(R.id.widgets_list_view);
243 }
244
Sunny Goyalaeb16432017-10-16 11:46:41 -0700245 @Override
246 protected int getElementsRowCount() {
247 return mAdapter.getItemCount();
248 }
Tony Wickham9791bd12019-04-05 13:52:35 -0700249
Tony Wickham9791bd12019-04-05 13:52:35 -0700250 @Override
Sunny Goyalf3ac7032020-03-13 13:01:33 -0700251 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 Wickham9791bd12019-04-05 13:52:35 -0700255 }
vadimt9f48a8e2019-12-11 11:52:37 -0800256
257 @Override
258 protected void onCloseComplete() {
259 super.onCloseComplete();
260 AccessibilityManagerCompat.sendStateEventToTest(getContext(), NORMAL_STATE_ORDINAL);
261 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700262}