blob: b07a4f461d088206d6e6fe779d3b8f53910569aa [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
90 @Override
Sunny Goyalde753212018-05-15 13:55:57 -070091 protected Pair<View, String> getAccessibilityTarget() {
92 return Pair.create(mRecyclerView, getContext().getString(
93 mIsOpen ? R.string.widgets_list : R.string.widgets_list_closed));
94 }
95
96 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070097 protected void onAttachedToWindow() {
98 super.onAttachedToWindow();
99 mLauncher.getAppWidgetHost().addProviderChangeListener(this);
100 notifyWidgetProvidersChanged();
101 }
102
103 @Override
104 protected void onDetachedFromWindow() {
105 super.onDetachedFromWindow();
106 mLauncher.getAppWidgetHost().removeProviderChangeListener(this);
107 }
108
109 @Override
110 public void setInsets(Rect insets) {
111 mInsets.set(insets);
112
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700113 mRecyclerView.setPadding(
114 mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop(),
115 mRecyclerView.getPaddingRight(), insets.bottom);
116 if (insets.bottom > 0) {
117 setupNavBarColor();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800118 } else {
119 clearNavBarColor();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700120 }
Sunny Goyal3661bfa2018-03-01 16:29:15 -0800121
122 ((TopRoundedCornerView) mContent).setNavBarScrimHeight(mInsets.bottom);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700123 requestLayout();
124 }
125
126 @Override
127 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
128 int widthUsed;
129 if (mInsets.bottom > 0) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700130 widthUsed = 0;
131 } else {
Sunny Goyal07b69292018-01-08 14:19:34 -0800132 Rect padding = mLauncher.getDeviceProfile().workspacePadding;
133 widthUsed = Math.max(padding.left + padding.right,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700134 2 * (mInsets.left + mInsets.right));
135 }
136
137 int heightUsed = mInsets.top + mLauncher.getDeviceProfile().edgeMarginPx;
138 measureChildWithMargins(mContent, widthMeasureSpec,
139 widthUsed, heightMeasureSpec, heightUsed);
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800140 setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
141 MeasureSpec.getSize(heightMeasureSpec));
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142 }
143
144 @Override
145 protected void onLayout(boolean changed, int l, int t, int r, int b) {
146 int width = r - l;
147 int height = b - t;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700148
149 // Content is laid out as center bottom aligned
150 int contentWidth = mContent.getMeasuredWidth();
151 int contentLeft = (width - contentWidth) / 2;
152 mContent.layout(contentLeft, height - mContent.getMeasuredHeight(),
153 contentLeft + contentWidth, height);
154
155 setTranslationShift(mTranslationShift);
156 }
157
158 @Override
159 public void notifyWidgetProvidersChanged() {
160 mLauncher.refreshAndBindWidgetsForPackageUser(null);
161 }
162
163 @Override
Sunny Goyal202ae0b2019-02-04 16:26:42 -0800164 public void onWidgetsBound() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700165 mAdapter.setWidgets(mLauncher.getPopupDataProvider().getAllWidgets());
166 }
167
168 private void open(boolean animate) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700169 if (animate) {
Sunny Goyaldb6cdb02018-07-13 11:03:04 -0700170 if (getPopupContainer().getInsets().bottom > 0) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700171 mContent.setAlpha(0);
172 setTranslationShift(VERTICAL_START_POSITION);
173 }
174 mOpenCloseAnimator.setValues(
175 PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED));
176 mOpenCloseAnimator
177 .setDuration(DEFAULT_OPEN_DURATION)
178 .setInterpolator(AnimationUtils.loadInterpolator(
179 getContext(), android.R.interpolator.linear_out_slow_in));
180 mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
181 @Override
182 public void onAnimationEnd(Animator animation) {
183 mRecyclerView.setLayoutFrozen(false);
184 mAdapter.setApplyBitmapDeferred(false, mRecyclerView);
185 mOpenCloseAnimator.removeListener(this);
186 }
187 });
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800188 post(() -> {
189 mRecyclerView.setLayoutFrozen(true);
190 mOpenCloseAnimator.start();
191 mContent.animate().alpha(1).setDuration(FADE_IN_DURATION);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700192 });
193 } else {
194 setTranslationShift(TRANSLATION_SHIFT_OPENED);
195 mAdapter.setApplyBitmapDeferred(false, mRecyclerView);
Sunny Goyalde753212018-05-15 13:55:57 -0700196 post(this::announceAccessibilityChanges);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700197 }
198 }
199
200 @Override
201 protected void handleClose(boolean animate) {
202 handleClose(animate, DEFAULT_OPEN_DURATION);
203 }
204
205 @Override
206 protected boolean isOfType(int type) {
207 return (type & TYPE_WIDGETS_FULL_SHEET) != 0;
208 }
209
210 @Override
211 public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
212 // Disable swipe down when recycler view is scrolling
213 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
214 mNoIntercept = false;
Sunny Goyal3661bfa2018-03-01 16:29:15 -0800215 RecyclerViewFastScroller scroller = mRecyclerView.getScrollbar();
216 if (scroller.getThumbOffsetY() >= 0 &&
Sunny Goyaldb6cdb02018-07-13 11:03:04 -0700217 getPopupContainer().isEventOverView(scroller, ev)) {
Sunny Goyal3661bfa2018-03-01 16:29:15 -0800218 mNoIntercept = true;
Sunny Goyaldb6cdb02018-07-13 11:03:04 -0700219 } else if (getPopupContainer().isEventOverView(mContent, ev)) {
220 mNoIntercept = !mRecyclerView.shouldContainerScroll(ev, getPopupContainer());
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700221 }
222 }
223 return super.onControllerInterceptTouchEvent(ev);
224 }
225
226 public static WidgetsFullSheet show(Launcher launcher, boolean animate) {
227 WidgetsFullSheet sheet = (WidgetsFullSheet) launcher.getLayoutInflater()
228 .inflate(R.layout.widgets_full_sheet, launcher.getDragLayer(), false);
Sunny Goyal1642f712018-09-18 11:40:35 -0700229 sheet.attachToContainer();
Sunny Goyalde753212018-05-15 13:55:57 -0700230 sheet.mIsOpen = true;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700231 sheet.open(animate);
232 return sheet;
233 }
Sunny Goyalaeb16432017-10-16 11:46:41 -0700234
Vadim Tryshev2ce6a132018-06-18 19:14:44 -0700235 @VisibleForTesting
236 public static WidgetsRecyclerView getWidgetsView(Launcher launcher) {
237 return launcher.findViewById(R.id.widgets_list_view);
238 }
239
Sunny Goyalaeb16432017-10-16 11:46:41 -0700240 @Override
241 protected int getElementsRowCount() {
242 return mAdapter.getItemCount();
243 }
Tony Wickham9791bd12019-04-05 13:52:35 -0700244
Tony Wickham9791bd12019-04-05 13:52:35 -0700245 @Override
Sunny Goyalf3ac7032020-03-13 13:01:33 -0700246 public void addHintCloseAnim(
247 float distanceToMove, Interpolator interpolator, PendingAnimation target) {
248 target.setFloat(mRecyclerView, VIEW_TRANSLATE_Y, -distanceToMove, interpolator);
249 target.setViewAlpha(mRecyclerView, 0.5f, interpolator);
Tony Wickham9791bd12019-04-05 13:52:35 -0700250 }
vadimt9f48a8e2019-12-11 11:52:37 -0800251
252 @Override
253 protected void onCloseComplete() {
254 super.onCloseComplete();
255 AccessibilityManagerCompat.sendStateEventToTest(getContext(), NORMAL_STATE_ORDINAL);
256 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700257}