blob: dc7d341546cac3cbe179c53df5536147fefe0ff9 [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 */
Steven Ng2f5648a2021-02-08 17:18:25 +000016package com.android.launcher3.widget.picker;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070017
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;
Steven Ng391404f2021-02-17 15:58:23 +000025import android.content.pm.LauncherApps;
Alina Zaidic4f3f492021-03-19 14:22:43 +000026import android.content.res.Configuration;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070027import android.graphics.Rect;
Steven Ng391404f2021-02-17 15:58:23 +000028import android.os.Process;
29import android.os.UserHandle;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070030import android.util.AttributeSet;
Sunny Goyalde753212018-05-15 13:55:57 -070031import android.util.Pair;
Steven Ng391404f2021-02-17 15:58:23 +000032import android.util.SparseArray;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070033import android.view.LayoutInflater;
34import android.view.MotionEvent;
Sunny Goyalde753212018-05-15 13:55:57 -070035import android.view.View;
Steven Ng4235fc12021-03-19 20:13:16 +000036import android.view.ViewGroup;
Alina Zaidi76060eb2021-03-22 12:25:37 +000037import android.view.WindowInsets;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070038import android.view.animation.AnimationUtils;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070039import android.view.animation.Interpolator;
Steven Ng167f81b2021-02-23 10:48:46 +000040import android.widget.TextView;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070041
Steven Ng391404f2021-02-17 15:58:23 +000042import androidx.annotation.Nullable;
Tony Wickham9791bd12019-04-05 13:52:35 -070043import androidx.annotation.VisibleForTesting;
Steven Ng391404f2021-02-17 15:58:23 +000044import androidx.recyclerview.widget.RecyclerView;
Tony Wickham9791bd12019-04-05 13:52:35 -070045
Steven Nge8d92342021-02-19 21:29:18 +000046import com.android.launcher3.DeviceProfile;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070047import com.android.launcher3.Insettable;
48import com.android.launcher3.Launcher;
49import com.android.launcher3.LauncherAppState;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070050import com.android.launcher3.R;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070051import com.android.launcher3.anim.PendingAnimation;
vadimt9f48a8e2019-12-11 11:52:37 -080052import com.android.launcher3.compat.AccessibilityManagerCompat;
Steven Ng4235fc12021-03-19 20:13:16 +000053import com.android.launcher3.model.WidgetItem;
Sunny Goyal3661bfa2018-03-01 16:29:15 -080054import com.android.launcher3.views.RecyclerViewFastScroller;
55import com.android.launcher3.views.TopRoundedCornerView;
Steven Ng2f5648a2021-02-08 17:18:25 +000056import com.android.launcher3.widget.BaseWidgetSheet;
Yogisha Dixit741fae92021-02-22 14:03:44 +000057import com.android.launcher3.widget.LauncherAppWidgetHost.ProviderChangedListener;
Steven Ng391404f2021-02-17 15:58:23 +000058import com.android.launcher3.widget.model.WidgetsListBaseEntry;
Alina Zaidi334e6592021-03-11 16:10:27 +000059import com.android.launcher3.widget.picker.search.SearchModeListener;
60import com.android.launcher3.widget.picker.search.WidgetsSearchBar;
Steven Ng4235fc12021-03-19 20:13:16 +000061import com.android.launcher3.widget.util.WidgetsTableUtils;
Steven Ng391404f2021-02-17 15:58:23 +000062import com.android.launcher3.workprofile.PersonalWorkPagedView;
63import com.android.launcher3.workprofile.PersonalWorkSlidingTabStrip.OnActivePageChangedListener;
64
Alina Zaidi2e732e92021-03-12 17:39:38 +000065import java.util.ArrayList;
Steven Ng391404f2021-02-17 15:58:23 +000066import java.util.List;
67import java.util.function.Predicate;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070068
69/**
70 * Popup for showing the full list of available widgets
71 */
72public class WidgetsFullSheet extends BaseWidgetSheet
Steven Ng167f81b2021-02-23 10:48:46 +000073 implements Insettable, ProviderChangedListener, OnActivePageChangedListener,
Alina Zaidi334e6592021-03-11 16:10:27 +000074 WidgetsRecyclerView.HeaderViewDimensionsProvider, SearchModeListener {
Steven Ng4235fc12021-03-19 20:13:16 +000075 private static final String TAG = WidgetsFullSheet.class.getSimpleName();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070076
77 private static final long DEFAULT_OPEN_DURATION = 267;
78 private static final long FADE_IN_DURATION = 150;
79 private static final float VERTICAL_START_POSITION = 0.3f;
Steven Ng4235fc12021-03-19 20:13:16 +000080 // The widget recommendation table can easily take over the entire screen on devices with small
81 // resolution or landscape on phone. This ratio defines the max percentage of content area that
82 // the table can display.
83 private static final float RECOMMENDATION_TABLE_HEIGHT_RATIO = 0.75f;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070084
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070085 private final Rect mInsets = new Rect();
Steven Ng391404f2021-02-17 15:58:23 +000086 private final boolean mHasWorkProfile;
87 private final SparseArray<AdapterHolder> mAdapters = new SparseArray();
88 private final UserHandle mCurrentUser = Process.myUserHandle();
89 private final Predicate<WidgetsListBaseEntry> mPrimaryWidgetsFilter = entry ->
90 mCurrentUser.equals(entry.mPkgItem.user);
91 private final Predicate<WidgetsListBaseEntry> mWorkWidgetsFilter =
92 mPrimaryWidgetsFilter.negate();
Steven Ng4235fc12021-03-19 20:13:16 +000093 private final int mTabsHeight;
94 private final int mWidgetCellHorizontalPadding;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070095
Steven Ng391404f2021-02-17 15:58:23 +000096 @Nullable private PersonalWorkPagedView mViewPager;
Alina Zaidi334e6592021-03-11 16:10:27 +000097 private boolean mIsInSearchMode;
Steven Ng4235fc12021-03-19 20:13:16 +000098 private int mMaxSpansPerRow = 4;
Steven Ng167f81b2021-02-23 10:48:46 +000099 private View mTabsView;
Steven Ngd73d6e52021-03-09 22:44:04 +0000100 private TextView mNoWidgetsView;
Steven Ng167f81b2021-02-23 10:48:46 +0000101 private SearchAndRecommendationViewHolder mSearchAndRecommendationViewHolder;
102 private SearchAndRecommendationsScrollController mSearchAndRecommendationsScrollController;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700103
104 public WidgetsFullSheet(Context context, AttributeSet attrs, int defStyleAttr) {
105 super(context, attrs, defStyleAttr);
Steven Ng391404f2021-02-17 15:58:23 +0000106 mHasWorkProfile = context.getSystemService(LauncherApps.class).getProfiles().size() > 1;
107 mAdapters.put(AdapterHolder.PRIMARY, new AdapterHolder(AdapterHolder.PRIMARY));
108 mAdapters.put(AdapterHolder.WORK, new AdapterHolder(AdapterHolder.WORK));
Alina Zaidi334e6592021-03-11 16:10:27 +0000109 mAdapters.put(AdapterHolder.SEARCH, new AdapterHolder(AdapterHolder.SEARCH));
Steven Ng4235fc12021-03-19 20:13:16 +0000110 mTabsHeight = mHasWorkProfile
111 ? getContext().getResources()
112 .getDimensionPixelSize(R.dimen.all_apps_header_tab_height)
113 : 0;
114 mWidgetCellHorizontalPadding = 2 * getResources().getDimensionPixelOffset(
115 R.dimen.widget_cell_horizontal_padding);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700116 }
117
118 public WidgetsFullSheet(Context context, AttributeSet attrs) {
119 this(context, attrs, 0);
120 }
121
122 @Override
123 protected void onFinishInflate() {
124 super.onFinishInflate();
125 mContent = findViewById(R.id.container);
Sunny Goyal016d7e92018-03-09 11:09:20 -0800126 TopRoundedCornerView springLayout = (TopRoundedCornerView) mContent;
Steven Ng391404f2021-02-17 15:58:23 +0000127
128 LayoutInflater layoutInflater = LayoutInflater.from(getContext());
129 int contentLayoutRes = mHasWorkProfile ? R.layout.widgets_full_sheet_paged_view
130 : R.layout.widgets_full_sheet_recyclerview;
Steven Ng167f81b2021-02-23 10:48:46 +0000131 layoutInflater.inflate(contentLayoutRes, springLayout, true);
Steven Ng391404f2021-02-17 15:58:23 +0000132
Steven Ng167f81b2021-02-23 10:48:46 +0000133 RecyclerViewFastScroller fastScroller = findViewById(R.id.fast_scroller);
Steven Ng391404f2021-02-17 15:58:23 +0000134 if (mHasWorkProfile) {
135 mViewPager = findViewById(R.id.widgets_view_pager);
Steven Ng391404f2021-02-17 15:58:23 +0000136 mViewPager.initParentViews(this);
137 mViewPager.getPageIndicator().setOnActivePageChangedListener(this);
138 mViewPager.getPageIndicator().setActiveMarker(AdapterHolder.PRIMARY);
Steven Ng167f81b2021-02-23 10:48:46 +0000139 mTabsView = findViewById(R.id.tabs);
Steven Ng391404f2021-02-17 15:58:23 +0000140 findViewById(R.id.tab_personal)
141 .setOnClickListener((View view) -> mViewPager.snapToPage(0));
142 findViewById(R.id.tab_work)
143 .setOnClickListener((View view) -> mViewPager.snapToPage(1));
Steven Ng167f81b2021-02-23 10:48:46 +0000144 fastScroller.setIsRecyclerViewFirstChildInParent(false);
Steven Ng391404f2021-02-17 15:58:23 +0000145 springLayout.addSpringView(R.id.primary_widgets_list_view);
146 springLayout.addSpringView(R.id.work_widgets_list_view);
147 } else {
148 mViewPager = null;
149 springLayout.addSpringView(R.id.primary_widgets_list_view);
150 }
151
Steven Ng167f81b2021-02-23 10:48:46 +0000152 layoutInflater.inflate(R.layout.widgets_full_sheet_search_and_recommendations, springLayout,
153 true);
154 springLayout.addSpringView(R.id.search_and_recommendations_container);
155
156 mSearchAndRecommendationViewHolder = new SearchAndRecommendationViewHolder(
157 findViewById(R.id.search_and_recommendations_container));
158 mSearchAndRecommendationsScrollController = new SearchAndRecommendationsScrollController(
159 mHasWorkProfile,
Steven Ng4235fc12021-03-19 20:13:16 +0000160 mTabsHeight,
Steven Ng167f81b2021-02-23 10:48:46 +0000161 mSearchAndRecommendationViewHolder,
162 findViewById(R.id.primary_widgets_list_view),
163 mHasWorkProfile ? findViewById(R.id.work_widgets_list_view) : null,
Alina Zaidi334e6592021-03-11 16:10:27 +0000164 findViewById(R.id.search_widgets_list_view),
Steven Ng167f81b2021-02-23 10:48:46 +0000165 mTabsView,
166 mViewPager);
167 fastScroller.setOnFastScrollChangeListener(mSearchAndRecommendationsScrollController);
168
Steven Ngd73d6e52021-03-09 22:44:04 +0000169 mNoWidgetsView = findViewById(R.id.no_widgets_text);
170
Steven Ng4235fc12021-03-19 20:13:16 +0000171 onRecommendedWidgetsBound();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700172 onWidgetsBound();
Alina Zaidi334e6592021-03-11 16:10:27 +0000173
174 mSearchAndRecommendationViewHolder.mSearchBar.initialize(
175 mLauncher.getPopupDataProvider().getAllWidgets(), /* searchModeListener= */ this);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700176 }
177
Steven Ng391404f2021-02-17 15:58:23 +0000178 @Override
179 public void onActivePageChanged(int currentActivePage) {
Steven Ngd73d6e52021-03-09 22:44:04 +0000180 AdapterHolder currentAdapterHolder = mAdapters.get(currentActivePage);
Alina Zaidi334e6592021-03-11 16:10:27 +0000181 WidgetsRecyclerView currentRecyclerView =
182 mAdapters.get(currentActivePage).mWidgetsRecyclerView;
Steven Ng167f81b2021-02-23 10:48:46 +0000183
Steven Ngd73d6e52021-03-09 22:44:04 +0000184 updateNoWidgetsView(currentAdapterHolder);
Alina Zaidi334e6592021-03-11 16:10:27 +0000185 attachScrollbarToRecyclerView(currentRecyclerView);
Alina Zaidi2e732e92021-03-12 17:39:38 +0000186 resetExpandedHeaders();
Alina Zaidi334e6592021-03-11 16:10:27 +0000187 }
188
189 private void attachScrollbarToRecyclerView(WidgetsRecyclerView recyclerView) {
190 recyclerView.bindFastScrollbar();
191 mSearchAndRecommendationsScrollController.setCurrentRecyclerView(recyclerView);
Steven Ng167f81b2021-02-23 10:48:46 +0000192 reset();
193 }
194
Steven Ngd73d6e52021-03-09 22:44:04 +0000195 private void updateNoWidgetsView(AdapterHolder adapterHolder) {
196 boolean isWidgetAvailable = adapterHolder.mWidgetsListAdapter.getItemCount() > 0;
197 adapterHolder.mWidgetsRecyclerView.setVisibility(isWidgetAvailable ? VISIBLE : GONE);
198
199 // Always resets the text in case this is updated by search.
200 mNoWidgetsView.setText(R.string.no_widgets_available);
201 mNoWidgetsView.setVisibility(isWidgetAvailable ? GONE : VISIBLE);
202 }
203
Alina Zaidi2e732e92021-03-12 17:39:38 +0000204 private void updateNoSearchResultsView(boolean isVisible) {
205 mNoWidgetsView.setVisibility(isVisible ? VISIBLE : GONE);
206 if (isVisible) {
207 mNoWidgetsView.setText(R.string.no_search_results);
208 }
209 }
210
Steven Ng167f81b2021-02-23 10:48:46 +0000211 private void reset() {
212 mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView.scrollToTop();
213 if (mHasWorkProfile) {
214 mAdapters.get(AdapterHolder.WORK).mWidgetsRecyclerView.scrollToTop();
215 }
Alina Zaidi334e6592021-03-11 16:10:27 +0000216 mAdapters.get(AdapterHolder.SEARCH).mWidgetsRecyclerView.scrollToTop();
Steven Ng7f7b2f72021-03-03 14:51:36 +0000217 mSearchAndRecommendationsScrollController.reset();
Steven Ng391404f2021-02-17 15:58:23 +0000218 }
219
Sunny Goyal8b37c572020-03-31 12:12:14 -0700220 @VisibleForTesting
221 public WidgetsRecyclerView getRecyclerView() {
Alina Zaidi334e6592021-03-11 16:10:27 +0000222 if (mIsInSearchMode) {
223 return mAdapters.get(AdapterHolder.SEARCH).mWidgetsRecyclerView;
224 }
Steven Ng391404f2021-02-17 15:58:23 +0000225 if (!mHasWorkProfile || mViewPager.getCurrentPage() == AdapterHolder.PRIMARY) {
226 return mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView;
227 }
228 return mAdapters.get(AdapterHolder.WORK).mWidgetsRecyclerView;
Sunny Goyal8b37c572020-03-31 12:12:14 -0700229 }
230
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700231 @Override
Sunny Goyalde753212018-05-15 13:55:57 -0700232 protected Pair<View, String> getAccessibilityTarget() {
Steven Ng391404f2021-02-17 15:58:23 +0000233 return Pair.create(getRecyclerView(), getContext().getString(
Sunny Goyalde753212018-05-15 13:55:57 -0700234 mIsOpen ? R.string.widgets_list : R.string.widgets_list_closed));
235 }
236
237 @Override
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700238 protected void onAttachedToWindow() {
239 super.onAttachedToWindow();
240 mLauncher.getAppWidgetHost().addProviderChangeListener(this);
241 notifyWidgetProvidersChanged();
242 }
243
244 @Override
245 protected void onDetachedFromWindow() {
246 super.onDetachedFromWindow();
247 mLauncher.getAppWidgetHost().removeProviderChangeListener(this);
248 }
249
250 @Override
251 public void setInsets(Rect insets) {
252 mInsets.set(insets);
253
Steven Ng391404f2021-02-17 15:58:23 +0000254 setBottomPadding(mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView, insets.bottom);
Alina Zaidic4f3f492021-03-19 14:22:43 +0000255 setBottomPadding(mAdapters.get(AdapterHolder.SEARCH).mWidgetsRecyclerView, insets.bottom);
Steven Ng391404f2021-02-17 15:58:23 +0000256 if (mHasWorkProfile) {
257 setBottomPadding(mAdapters.get(AdapterHolder.WORK).mWidgetsRecyclerView, insets.bottom);
258 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700259 if (insets.bottom > 0) {
260 setupNavBarColor();
Sunny Goyalf8d56fc2018-01-31 15:18:11 -0800261 } else {
262 clearNavBarColor();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700263 }
Sunny Goyal3661bfa2018-03-01 16:29:15 -0800264
265 ((TopRoundedCornerView) mContent).setNavBarScrimHeight(mInsets.bottom);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700266 requestLayout();
267 }
268
Steven Ng391404f2021-02-17 15:58:23 +0000269 private void setBottomPadding(RecyclerView recyclerView, int bottomPadding) {
270 recyclerView.setPadding(
271 recyclerView.getPaddingLeft(),
272 recyclerView.getPaddingTop(),
273 recyclerView.getPaddingRight(),
274 bottomPadding);
275 }
276
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700277 @Override
278 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Steven Ng4235fc12021-03-19 20:13:16 +0000279 doMeasure(widthMeasureSpec, heightMeasureSpec);
280
281 if (mSearchAndRecommendationsScrollController.updateMarginAndPadding()) {
282 doMeasure(widthMeasureSpec, heightMeasureSpec);
283 }
284
285 if (updateMaxSpansPerRow()) {
286 doMeasure(widthMeasureSpec, heightMeasureSpec);
287
288 if (mSearchAndRecommendationsScrollController.updateMarginAndPadding()) {
289 doMeasure(widthMeasureSpec, heightMeasureSpec);
290 }
291 }
292 }
293
294 private void doMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Steven Nge8d92342021-02-19 21:29:18 +0000295 DeviceProfile deviceProfile = mLauncher.getDeviceProfile();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700296 int widthUsed;
297 if (mInsets.bottom > 0) {
Sunny Goyal786940a2020-06-02 02:31:31 -0700298 widthUsed = mInsets.left + mInsets.right;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700299 } else {
Steven Nge8d92342021-02-19 21:29:18 +0000300 Rect padding = deviceProfile.workspacePadding;
Sunny Goyal07b69292018-01-08 14:19:34 -0800301 widthUsed = Math.max(padding.left + padding.right,
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700302 2 * (mInsets.left + mInsets.right));
303 }
304
Steven Nge8d92342021-02-19 21:29:18 +0000305 int heightUsed = mInsets.top + deviceProfile.edgeMarginPx;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700306 measureChildWithMargins(mContent, widthMeasureSpec,
307 widthUsed, heightMeasureSpec, heightUsed);
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800308 setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
309 MeasureSpec.getSize(heightMeasureSpec));
Steven Ng4235fc12021-03-19 20:13:16 +0000310 }
Steven Nge8d92342021-02-19 21:29:18 +0000311
Steven Ng4235fc12021-03-19 20:13:16 +0000312 /** Returns {@code true} if the max spans have been updated. */
313 private boolean updateMaxSpansPerRow() {
314 if (getMeasuredWidth() == 0) return false;
315
316 int previousMaxSpansPerRow = mMaxSpansPerRow;
317 mMaxSpansPerRow = getMeasuredWidth()
318 / (mLauncher.getDeviceProfile().cellWidthPx + mWidgetCellHorizontalPadding);
319
320 if (previousMaxSpansPerRow != mMaxSpansPerRow) {
321 mAdapters.get(AdapterHolder.PRIMARY).mWidgetsListAdapter.setMaxHorizontalSpansPerRow(
322 mMaxSpansPerRow);
323 mAdapters.get(AdapterHolder.SEARCH).mWidgetsListAdapter.setMaxHorizontalSpansPerRow(
324 mMaxSpansPerRow);
325 if (mHasWorkProfile) {
326 mAdapters.get(AdapterHolder.WORK).mWidgetsListAdapter.setMaxHorizontalSpansPerRow(
327 mMaxSpansPerRow);
328 }
329 onRecommendedWidgetsBound();
330 return true;
Steven Nge8d92342021-02-19 21:29:18 +0000331 }
Steven Ng4235fc12021-03-19 20:13:16 +0000332 return false;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700333 }
334
335 @Override
336 protected void onLayout(boolean changed, int l, int t, int r, int b) {
337 int width = r - l;
338 int height = b - t;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700339
340 // Content is laid out as center bottom aligned
341 int contentWidth = mContent.getMeasuredWidth();
Sunny Goyal786940a2020-06-02 02:31:31 -0700342 int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700343 mContent.layout(contentLeft, height - mContent.getMeasuredHeight(),
344 contentLeft + contentWidth, height);
345
346 setTranslationShift(mTranslationShift);
347 }
348
349 @Override
350 public void notifyWidgetProvidersChanged() {
351 mLauncher.refreshAndBindWidgetsForPackageUser(null);
352 }
353
354 @Override
Sunny Goyal202ae0b2019-02-04 16:26:42 -0800355 public void onWidgetsBound() {
Steven Ng391404f2021-02-17 15:58:23 +0000356 List<WidgetsListBaseEntry> allWidgets = mLauncher.getPopupDataProvider().getAllWidgets();
357
358 AdapterHolder primaryUserAdapterHolder = mAdapters.get(AdapterHolder.PRIMARY);
359 primaryUserAdapterHolder.setup(findViewById(R.id.primary_widgets_list_view));
Alina Zaidi334e6592021-03-11 16:10:27 +0000360 AdapterHolder searchAdapterHolder = mAdapters.get(AdapterHolder.SEARCH);
361 searchAdapterHolder.setup(findViewById(R.id.search_widgets_list_view));
Steven Ng391404f2021-02-17 15:58:23 +0000362 primaryUserAdapterHolder.mWidgetsListAdapter.setWidgets(allWidgets);
Steven Ngd73d6e52021-03-09 22:44:04 +0000363 updateNoWidgetsView(primaryUserAdapterHolder);
364
Steven Ng391404f2021-02-17 15:58:23 +0000365 if (mHasWorkProfile) {
366 AdapterHolder workUserAdapterHolder = mAdapters.get(AdapterHolder.WORK);
367 workUserAdapterHolder.setup(findViewById(R.id.work_widgets_list_view));
368 workUserAdapterHolder.mWidgetsListAdapter.setWidgets(allWidgets);
369 onActivePageChanged(mViewPager.getCurrentPage());
370 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700371 }
372
Alina Zaidi334e6592021-03-11 16:10:27 +0000373 @Override
374 public void enterSearchMode() {
375 if (mIsInSearchMode) return;
376 setViewVisibilityBasedOnSearch(/*isInSearchMode= */ true);
377 attachScrollbarToRecyclerView(mAdapters.get(AdapterHolder.SEARCH).mWidgetsRecyclerView);
Alina Zaidi2e732e92021-03-12 17:39:38 +0000378 resetExpandedHeaders();
Alina Zaidi334e6592021-03-11 16:10:27 +0000379 }
380
381 @Override
382 public void exitSearchMode() {
Alina Zaidi2e732e92021-03-12 17:39:38 +0000383 onSearchResults(new ArrayList<>());
Alina Zaidi334e6592021-03-11 16:10:27 +0000384 setViewVisibilityBasedOnSearch(/*isInSearchMode=*/ false);
385 if (mHasWorkProfile) {
386 mViewPager.snapToPage(AdapterHolder.PRIMARY);
387 }
388 attachScrollbarToRecyclerView(mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView);
Steven Ng4235fc12021-03-19 20:13:16 +0000389
390 mSearchAndRecommendationsScrollController.updateMarginAndPadding();
Alina Zaidi334e6592021-03-11 16:10:27 +0000391 }
392
393 @Override
394 public void onSearchResults(List<WidgetsListBaseEntry> entries) {
395 mAdapters.get(AdapterHolder.SEARCH).mWidgetsListAdapter.setWidgetsOnSearch(entries);
Alina Zaidi2e732e92021-03-12 17:39:38 +0000396 updateNoSearchResultsView(
397 mAdapters.get(AdapterHolder.SEARCH).mWidgetsListAdapter.getItemCount() == 0);
Alina Zaidi334e6592021-03-11 16:10:27 +0000398 }
399
400 private void setViewVisibilityBasedOnSearch(boolean isInSearchMode) {
401 mIsInSearchMode = isInSearchMode;
Steven Ng4235fc12021-03-19 20:13:16 +0000402 mSearchAndRecommendationViewHolder.mRecommendedWidgetsTable
403 .setVisibility(isInSearchMode ? GONE : VISIBLE);
Alina Zaidi334e6592021-03-11 16:10:27 +0000404 if (mHasWorkProfile) {
405 mViewPager.setVisibility(isInSearchMode ? GONE : VISIBLE);
406 mTabsView.setVisibility(isInSearchMode ? GONE : VISIBLE);
407 } else {
408 mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView
409 .setVisibility(isInSearchMode ? GONE : VISIBLE);
410 }
411 mAdapters.get(AdapterHolder.SEARCH).mWidgetsRecyclerView
412 .setVisibility(mIsInSearchMode ? VISIBLE : GONE);
Alina Zaidi2e732e92021-03-12 17:39:38 +0000413 mNoWidgetsView.setVisibility(GONE);
414 }
415
416 private void resetExpandedHeaders() {
417 mAdapters.get(AdapterHolder.PRIMARY).mWidgetsListAdapter.resetExpandedHeader();
418 mAdapters.get(AdapterHolder.WORK).mWidgetsListAdapter.resetExpandedHeader();
Alina Zaidi334e6592021-03-11 16:10:27 +0000419 }
420
Steven Ng4235fc12021-03-19 20:13:16 +0000421 @Override
422 public void onRecommendedWidgetsBound() {
423 List<WidgetItem> recommendedWidgets =
424 mLauncher.getPopupDataProvider().getRecommendedWidgets();
425 WidgetsRecommendationTableLayout table =
426 mSearchAndRecommendationViewHolder.mRecommendedWidgetsTable;
427 if (recommendedWidgets.size() > 0) {
428 float maxTableHeight =
429 (mLauncher.getDeviceProfile().heightPx - mTabsHeight - getHeaderViewHeight())
430 * RECOMMENDATION_TABLE_HEIGHT_RATIO;
431 List<ArrayList<WidgetItem>> recommendedWidgetsInTable =
432 WidgetsTableUtils.groupWidgetItemsIntoTable(recommendedWidgets,
433 mMaxSpansPerRow);
434 table.setRecommendedWidgets(recommendedWidgetsInTable, maxTableHeight);
435 } else {
436 table.setVisibility(GONE);
437 }
438 }
439
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700440 private void open(boolean animate) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700441 if (animate) {
Sunny Goyaldb6cdb02018-07-13 11:03:04 -0700442 if (getPopupContainer().getInsets().bottom > 0) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700443 mContent.setAlpha(0);
444 setTranslationShift(VERTICAL_START_POSITION);
445 }
446 mOpenCloseAnimator.setValues(
447 PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED));
448 mOpenCloseAnimator
449 .setDuration(DEFAULT_OPEN_DURATION)
450 .setInterpolator(AnimationUtils.loadInterpolator(
451 getContext(), android.R.interpolator.linear_out_slow_in));
452 mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
453 @Override
454 public void onAnimationEnd(Animator animation) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700455 mOpenCloseAnimator.removeListener(this);
456 }
457 });
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800458 post(() -> {
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800459 mOpenCloseAnimator.start();
460 mContent.animate().alpha(1).setDuration(FADE_IN_DURATION);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700461 });
462 } else {
463 setTranslationShift(TRANSLATION_SHIFT_OPENED);
Sunny Goyalde753212018-05-15 13:55:57 -0700464 post(this::announceAccessibilityChanges);
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700465 }
466 }
467
468 @Override
469 protected void handleClose(boolean animate) {
470 handleClose(animate, DEFAULT_OPEN_DURATION);
471 }
472
473 @Override
474 protected boolean isOfType(int type) {
475 return (type & TYPE_WIDGETS_FULL_SHEET) != 0;
476 }
477
478 @Override
479 public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
480 // Disable swipe down when recycler view is scrolling
481 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
482 mNoIntercept = false;
Steven Ng391404f2021-02-17 15:58:23 +0000483 RecyclerViewFastScroller scroller = getRecyclerView().getScrollbar();
Steven Ng2f5648a2021-02-08 17:18:25 +0000484 if (scroller.getThumbOffsetY() >= 0
485 && getPopupContainer().isEventOverView(scroller, ev)) {
Sunny Goyal3661bfa2018-03-01 16:29:15 -0800486 mNoIntercept = true;
Sunny Goyaldb6cdb02018-07-13 11:03:04 -0700487 } else if (getPopupContainer().isEventOverView(mContent, ev)) {
Steven Ng391404f2021-02-17 15:58:23 +0000488 mNoIntercept = !getRecyclerView().shouldContainerScroll(ev, getPopupContainer());
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700489 }
490 }
491 return super.onControllerInterceptTouchEvent(ev);
492 }
493
Steven Ng2f5648a2021-02-08 17:18:25 +0000494 /** Shows the {@link WidgetsFullSheet} on the launcher. */
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700495 public static WidgetsFullSheet show(Launcher launcher, boolean animate) {
496 WidgetsFullSheet sheet = (WidgetsFullSheet) launcher.getLayoutInflater()
497 .inflate(R.layout.widgets_full_sheet, launcher.getDragLayer(), false);
Sunny Goyal1642f712018-09-18 11:40:35 -0700498 sheet.attachToContainer();
Sunny Goyalde753212018-05-15 13:55:57 -0700499 sheet.mIsOpen = true;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700500 sheet.open(animate);
501 return sheet;
502 }
Sunny Goyalaeb16432017-10-16 11:46:41 -0700503
Steven Ng2f5648a2021-02-08 17:18:25 +0000504 /** Gets the {@link WidgetsRecyclerView} which shows all widgets in {@link WidgetsFullSheet}. */
Vadim Tryshev2ce6a132018-06-18 19:14:44 -0700505 @VisibleForTesting
506 public static WidgetsRecyclerView getWidgetsView(Launcher launcher) {
Steven Ng391404f2021-02-17 15:58:23 +0000507 return launcher.findViewById(R.id.primary_widgets_list_view);
Vadim Tryshev2ce6a132018-06-18 19:14:44 -0700508 }
509
Sunny Goyalaeb16432017-10-16 11:46:41 -0700510 @Override
Sunny Goyalf3ac7032020-03-13 13:01:33 -0700511 public void addHintCloseAnim(
512 float distanceToMove, Interpolator interpolator, PendingAnimation target) {
Steven Ng391404f2021-02-17 15:58:23 +0000513 target.setFloat(getRecyclerView(), VIEW_TRANSLATE_Y, -distanceToMove, interpolator);
514 target.setViewAlpha(getRecyclerView(), 0.5f, interpolator);
Tony Wickham9791bd12019-04-05 13:52:35 -0700515 }
vadimt9f48a8e2019-12-11 11:52:37 -0800516
517 @Override
518 protected void onCloseComplete() {
519 super.onCloseComplete();
520 AccessibilityManagerCompat.sendStateEventToTest(getContext(), NORMAL_STATE_ORDINAL);
521 }
Steven Ng391404f2021-02-17 15:58:23 +0000522
Steven Ng167f81b2021-02-23 10:48:46 +0000523 @Override
524 public int getHeaderViewHeight() {
Steven Ng53d13642021-03-10 22:40:55 +0000525 return measureHeightWithVerticalMargins(mSearchAndRecommendationViewHolder.mCollapseHandle)
526 + measureHeightWithVerticalMargins(mSearchAndRecommendationViewHolder.mHeaderTitle)
Alina Zaidi16327522021-03-15 07:00:49 +0000527 + measureHeightWithVerticalMargins(
528 (View) mSearchAndRecommendationViewHolder.mSearchBar);
Steven Ng7f7b2f72021-03-03 14:51:36 +0000529 }
530
531 /** private the height, in pixel, + the vertical margins of a given view. */
532 private static int measureHeightWithVerticalMargins(View view) {
Steven Ng53d13642021-03-10 22:40:55 +0000533 if (view.getVisibility() != VISIBLE) {
534 return 0;
535 }
Steven Ng7f7b2f72021-03-03 14:51:36 +0000536 MarginLayoutParams marginLayoutParams = (MarginLayoutParams) view.getLayoutParams();
537 return view.getMeasuredHeight() + marginLayoutParams.bottomMargin
538 + marginLayoutParams.topMargin;
Steven Ng167f81b2021-02-23 10:48:46 +0000539 }
540
Alina Zaidic4f3f492021-03-19 14:22:43 +0000541 @Override
542 protected void onConfigurationChanged(Configuration newConfig) {
543 super.onConfigurationChanged(newConfig);
544 if (mIsInSearchMode) {
545 mSearchAndRecommendationViewHolder.mSearchBar.reset();
546 }
547 }
548
549 @Override
550 public boolean onBackPressed() {
551 if (mIsInSearchMode) {
552 mSearchAndRecommendationViewHolder.mSearchBar.reset();
553 return true;
554 }
555 return super.onBackPressed();
556 }
557
Alina Zaidi76060eb2021-03-22 12:25:37 +0000558 @Override
559 public void onDragStart(boolean start, float startDisplacement) {
560 super.onDragStart(start, startDisplacement);
561 getWindowInsetsController().hide(WindowInsets.Type.ime());
562 }
563
Steven Ng391404f2021-02-17 15:58:23 +0000564 /** A holder class for holding adapters & their corresponding recycler view. */
565 private final class AdapterHolder {
566 static final int PRIMARY = 0;
567 static final int WORK = 1;
Alina Zaidi334e6592021-03-11 16:10:27 +0000568 static final int SEARCH = 2;
Steven Ng391404f2021-02-17 15:58:23 +0000569
570 private final int mAdapterType;
571 private final WidgetsListAdapter mWidgetsListAdapter;
572
573 private WidgetsRecyclerView mWidgetsRecyclerView;
574
575 AdapterHolder(int adapterType) {
576 mAdapterType = adapterType;
577
578 Context context = getContext();
579 LauncherAppState apps = LauncherAppState.getInstance(context);
580 mWidgetsListAdapter = new WidgetsListAdapter(
581 context,
582 LayoutInflater.from(context),
583 apps.getWidgetCache(),
584 apps.getIconCache(),
585 /* iconClickListener= */ WidgetsFullSheet.this,
586 /* iconLongClickListener= */ WidgetsFullSheet.this);
Steven Nge85b5562021-03-19 23:52:12 +0000587 mWidgetsListAdapter.setHasStableIds(true);
Alina Zaidi334e6592021-03-11 16:10:27 +0000588 switch (mAdapterType) {
589 case PRIMARY:
590 mWidgetsListAdapter.setFilter(mPrimaryWidgetsFilter);
591 break;
592 case WORK:
593 mWidgetsListAdapter.setFilter(mWorkWidgetsFilter);
594 break;
595 default:
596 break;
597 }
Steven Ng391404f2021-02-17 15:58:23 +0000598 }
599
600 void setup(WidgetsRecyclerView recyclerView) {
601 mWidgetsRecyclerView = recyclerView;
602 mWidgetsRecyclerView.setAdapter(mWidgetsListAdapter);
Steven Nge85b5562021-03-19 23:52:12 +0000603 // Disables animation because it disrupts the item focus upon adapter item change.
604 mWidgetsRecyclerView.setItemAnimator(null);
Steven Ng167f81b2021-02-23 10:48:46 +0000605 mWidgetsRecyclerView.setHeaderViewDimensionsProvider(WidgetsFullSheet.this);
Steven Ng391404f2021-02-17 15:58:23 +0000606 mWidgetsRecyclerView.setEdgeEffectFactory(
607 ((TopRoundedCornerView) mContent).createEdgeEffectFactory());
608 mWidgetsListAdapter.setApplyBitmapDeferred(false, mWidgetsRecyclerView);
Steven Ng4235fc12021-03-19 20:13:16 +0000609 mWidgetsListAdapter.setMaxHorizontalSpansPerRow(mMaxSpansPerRow);
Steven Ng391404f2021-02-17 15:58:23 +0000610 }
611 }
Steven Ng167f81b2021-02-23 10:48:46 +0000612
613 final class SearchAndRecommendationViewHolder {
Steven Ng4235fc12021-03-19 20:13:16 +0000614 final ViewGroup mContainer;
Steven Ng167f81b2021-02-23 10:48:46 +0000615 final View mCollapseHandle;
Alina Zaidi334e6592021-03-11 16:10:27 +0000616 final WidgetsSearchBar mSearchBar;
Steven Ng167f81b2021-02-23 10:48:46 +0000617 final TextView mHeaderTitle;
Steven Ng4235fc12021-03-19 20:13:16 +0000618 final WidgetsRecommendationTableLayout mRecommendedWidgetsTable;
Steven Ng167f81b2021-02-23 10:48:46 +0000619
Steven Ng4235fc12021-03-19 20:13:16 +0000620 SearchAndRecommendationViewHolder(ViewGroup searchAndRecommendationContainer) {
Steven Ng167f81b2021-02-23 10:48:46 +0000621 mContainer = searchAndRecommendationContainer;
622 mCollapseHandle = mContainer.findViewById(R.id.collapse_handle);
623 mSearchBar = mContainer.findViewById(R.id.widgets_search_bar);
624 mHeaderTitle = mContainer.findViewById(R.id.title);
Steven Ng4235fc12021-03-19 20:13:16 +0000625 mRecommendedWidgetsTable = mContainer.findViewById(R.id.recommended_widget_table);
626 mRecommendedWidgetsTable.setWidgetCellOnTouchListener((view, event) -> {
627 getRecyclerView().onTouchEvent(event);
628 return false;
629 });
630 mRecommendedWidgetsTable.setWidgetCellLongClickListener(WidgetsFullSheet.this);
631 mRecommendedWidgetsTable.setWidgetCellOnClickListener(WidgetsFullSheet.this);
Steven Ng167f81b2021-02-23 10:48:46 +0000632 }
633 }
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700634}