blob: a1bfe88498581f757895d5d5cf8253af028c796f [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
18import android.animation.Animator;
19import android.animation.AnimatorListenerAdapter;
20import android.animation.PropertyValuesHolder;
21import android.content.Context;
22import android.graphics.Rect;
23import android.util.AttributeSet;
24import android.view.LayoutInflater;
25import android.view.MotionEvent;
26import android.view.View;
27import android.view.animation.AnimationUtils;
28
29import com.android.launcher3.Insettable;
30import com.android.launcher3.Launcher;
31import com.android.launcher3.LauncherAppState;
32import com.android.launcher3.LauncherAppWidgetHost.ProviderChangedListener;
33import com.android.launcher3.R;
34
35/**
36 * Popup for showing the full list of available widgets
37 */
38public class WidgetsFullSheet extends BaseWidgetSheet
39 implements Insettable, ProviderChangedListener {
40
41 private static final long DEFAULT_OPEN_DURATION = 267;
42 private static final long FADE_IN_DURATION = 150;
43 private static final float VERTICAL_START_POSITION = 0.3f;
44
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070045 private final Rect mInsets = new Rect();
46
47 private final WidgetsListAdapter mAdapter;
48
49 private View mNavBarScrim;
50 private WidgetsRecyclerView mRecyclerView;
51
52 public WidgetsFullSheet(Context context, AttributeSet attrs, int defStyleAttr) {
53 super(context, attrs, defStyleAttr);
54 LauncherAppState apps = LauncherAppState.getInstance(context);
55 mAdapter = new WidgetsListAdapter(context,
56 LayoutInflater.from(context), apps.getWidgetCache(), apps.getIconCache(),
57 this, this);
58 }
59
60 public WidgetsFullSheet(Context context, AttributeSet attrs) {
61 this(context, attrs, 0);
62 }
63
64 @Override
65 protected void onFinishInflate() {
66 super.onFinishInflate();
67 mContent = findViewById(R.id.container);
68 mNavBarScrim = findViewById(R.id.nav_bar_bg);
69
70 mRecyclerView = findViewById(R.id.widgets_list_view);
71 mRecyclerView.setAdapter(mAdapter);
72 mAdapter.setApplyBitmapDeferred(true, mRecyclerView);
73
74 mGradientView = findViewById(R.id.gradient_bg);
75 mGradientView.setProgress(1, false);
76
77 onWidgetsBound();
78 }
79
80 @Override
81 protected void onAttachedToWindow() {
82 super.onAttachedToWindow();
83 mLauncher.getAppWidgetHost().addProviderChangeListener(this);
84 notifyWidgetProvidersChanged();
85 }
86
87 @Override
88 protected void onDetachedFromWindow() {
89 super.onDetachedFromWindow();
90 mLauncher.getAppWidgetHost().removeProviderChangeListener(this);
91 }
92
93 @Override
94 public void setInsets(Rect insets) {
95 mInsets.set(insets);
96
97 mNavBarScrim.getLayoutParams().height = insets.bottom;
98 mRecyclerView.setPadding(
99 mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop(),
100 mRecyclerView.getPaddingRight(), insets.bottom);
101 if (insets.bottom > 0) {
102 setupNavBarColor();
103 }
104 requestLayout();
105 }
106
107 @Override
108 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
109 int widthUsed;
110 if (mInsets.bottom > 0) {
111 // If we have bottom insets, we do not show the scrim as it would overlap
112 // with the navbar scrim
113 mGradientView.setVisibility(View.INVISIBLE);
114 widthUsed = 0;
115 } else {
Sunny Goyal07b69292018-01-08 14:19:34 -0800116 Rect padding = mLauncher.getDeviceProfile().workspacePadding;
117 widthUsed = Math.max(padding.left + padding.right,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700118 2 * (mInsets.left + mInsets.right));
119 }
120
121 int heightUsed = mInsets.top + mLauncher.getDeviceProfile().edgeMarginPx;
122 measureChildWithMargins(mContent, widthMeasureSpec,
123 widthUsed, heightMeasureSpec, heightUsed);
124 measureChild(mGradientView, widthMeasureSpec, heightMeasureSpec);
125 setMeasuredDimension(mGradientView.getMeasuredWidth(), mGradientView.getMeasuredHeight());
126 }
127
128 @Override
129 protected void onLayout(boolean changed, int l, int t, int r, int b) {
130 int width = r - l;
131 int height = b - t;
132 mGradientView.layout(0, 0, width, height);
133
134 // Content is laid out as center bottom aligned
135 int contentWidth = mContent.getMeasuredWidth();
136 int contentLeft = (width - contentWidth) / 2;
137 mContent.layout(contentLeft, height - mContent.getMeasuredHeight(),
138 contentLeft + contentWidth, height);
139
140 setTranslationShift(mTranslationShift);
141 }
142
143 @Override
144 public void notifyWidgetProvidersChanged() {
145 mLauncher.refreshAndBindWidgetsForPackageUser(null);
146 }
147
148 @Override
149 protected void onWidgetsBound() {
150 mAdapter.setWidgets(mLauncher.getPopupDataProvider().getAllWidgets());
151 }
152
153 private void open(boolean animate) {
154 if (mIsOpen) {
155 return;
156 }
157 mIsOpen = true;
158 if (animate) {
159 if (mLauncher.getDragLayer().getInsets().bottom > 0) {
160 mContent.setAlpha(0);
161 setTranslationShift(VERTICAL_START_POSITION);
162 }
163 mOpenCloseAnimator.setValues(
164 PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED));
165 mOpenCloseAnimator
166 .setDuration(DEFAULT_OPEN_DURATION)
167 .setInterpolator(AnimationUtils.loadInterpolator(
168 getContext(), android.R.interpolator.linear_out_slow_in));
169 mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
170 @Override
171 public void onAnimationEnd(Animator animation) {
172 mRecyclerView.setLayoutFrozen(false);
173 mAdapter.setApplyBitmapDeferred(false, mRecyclerView);
174 mOpenCloseAnimator.removeListener(this);
175 }
176 });
177 post(new Runnable() {
178 @Override
179 public void run() {
180 mRecyclerView.setLayoutFrozen(true);
181 mOpenCloseAnimator.start();
182 mContent.animate().alpha(1).setDuration(FADE_IN_DURATION);
183 }
184 });
185 } else {
186 setTranslationShift(TRANSLATION_SHIFT_OPENED);
187 mAdapter.setApplyBitmapDeferred(false, mRecyclerView);
188 }
189 }
190
191 @Override
192 protected void handleClose(boolean animate) {
193 handleClose(animate, DEFAULT_OPEN_DURATION);
194 }
195
196 @Override
197 protected boolean isOfType(int type) {
198 return (type & TYPE_WIDGETS_FULL_SHEET) != 0;
199 }
200
201 @Override
202 public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
203 // Disable swipe down when recycler view is scrolling
204 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
205 mNoIntercept = false;
206 if (mLauncher.getDragLayer().isEventOverView(mContent, ev)) {
207 mNoIntercept = !mRecyclerView.shouldContainerScroll(ev, mLauncher.getDragLayer());
208 }
209 }
210 return super.onControllerInterceptTouchEvent(ev);
211 }
212
213 public static WidgetsFullSheet show(Launcher launcher, boolean animate) {
214 WidgetsFullSheet sheet = (WidgetsFullSheet) launcher.getLayoutInflater()
215 .inflate(R.layout.widgets_full_sheet, launcher.getDragLayer(), false);
216 launcher.getDragLayer().addView(sheet);
217 sheet.open(animate);
218 return sheet;
219 }
Sunny Goyalaeb16432017-10-16 11:46:41 -0700220
221 @Override
222 protected int getElementsRowCount() {
223 return mAdapter.getItemCount();
224 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700225}