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 | */ |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame] | 16 | package com.android.launcher3.widget.picker; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 17 | |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 18 | import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_Y; |
vadimt | 9f48a8e | 2019-12-11 11:52:37 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.testing.TestProtocol.NORMAL_STATE_ORDINAL; |
| 20 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| 23 | import android.animation.PropertyValuesHolder; |
| 24 | import android.content.Context; |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 25 | import android.content.pm.LauncherApps; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 26 | import android.graphics.Rect; |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 27 | import android.os.Process; |
| 28 | import android.os.UserHandle; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 30 | import android.util.Pair; |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 31 | import android.util.SparseArray; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
| 33 | import android.view.MotionEvent; |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 34 | import android.view.View; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 35 | import android.view.animation.AnimationUtils; |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 36 | import android.view.animation.Interpolator; |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 37 | import android.widget.EditText; |
| 38 | import android.widget.TextView; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 39 | |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 40 | import androidx.annotation.Nullable; |
Tony Wickham | 9791bd1 | 2019-04-05 13:52:35 -0700 | [diff] [blame] | 41 | import androidx.annotation.VisibleForTesting; |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 42 | import androidx.recyclerview.widget.RecyclerView; |
Tony Wickham | 9791bd1 | 2019-04-05 13:52:35 -0700 | [diff] [blame] | 43 | |
Steven Ng | e8d9234 | 2021-02-19 21:29:18 +0000 | [diff] [blame] | 44 | import com.android.launcher3.DeviceProfile; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 45 | import com.android.launcher3.Insettable; |
| 46 | import com.android.launcher3.Launcher; |
| 47 | import com.android.launcher3.LauncherAppState; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 48 | import com.android.launcher3.R; |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 49 | import com.android.launcher3.anim.PendingAnimation; |
vadimt | 9f48a8e | 2019-12-11 11:52:37 -0800 | [diff] [blame] | 50 | import com.android.launcher3.compat.AccessibilityManagerCompat; |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 51 | import com.android.launcher3.views.RecyclerViewFastScroller; |
| 52 | import com.android.launcher3.views.TopRoundedCornerView; |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame] | 53 | import com.android.launcher3.widget.BaseWidgetSheet; |
Yogisha Dixit | 741fae9 | 2021-02-22 14:03:44 +0000 | [diff] [blame] | 54 | import com.android.launcher3.widget.LauncherAppWidgetHost.ProviderChangedListener; |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 55 | import com.android.launcher3.widget.model.WidgetsListBaseEntry; |
| 56 | import com.android.launcher3.workprofile.PersonalWorkPagedView; |
| 57 | import com.android.launcher3.workprofile.PersonalWorkSlidingTabStrip.OnActivePageChangedListener; |
| 58 | |
| 59 | import java.util.List; |
| 60 | import java.util.function.Predicate; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Popup for showing the full list of available widgets |
| 64 | */ |
| 65 | public class WidgetsFullSheet extends BaseWidgetSheet |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 66 | implements Insettable, ProviderChangedListener, OnActivePageChangedListener, |
| 67 | WidgetsRecyclerView.HeaderViewDimensionsProvider { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 68 | |
| 69 | private static final long DEFAULT_OPEN_DURATION = 267; |
| 70 | private static final long FADE_IN_DURATION = 150; |
| 71 | private static final float VERTICAL_START_POSITION = 0.3f; |
| 72 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 73 | private final Rect mInsets = new Rect(); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 74 | private final boolean mHasWorkProfile; |
| 75 | private final SparseArray<AdapterHolder> mAdapters = new SparseArray(); |
| 76 | private final UserHandle mCurrentUser = Process.myUserHandle(); |
| 77 | private final Predicate<WidgetsListBaseEntry> mPrimaryWidgetsFilter = entry -> |
| 78 | mCurrentUser.equals(entry.mPkgItem.user); |
| 79 | private final Predicate<WidgetsListBaseEntry> mWorkWidgetsFilter = |
| 80 | mPrimaryWidgetsFilter.negate(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 81 | |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 82 | @Nullable private PersonalWorkPagedView mViewPager; |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 83 | private int mInitialTabsHeight = 0; |
| 84 | private View mTabsView; |
Steven Ng | d73d6e5 | 2021-03-09 22:44:04 +0000 | [diff] [blame] | 85 | private TextView mNoWidgetsView; |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 86 | private SearchAndRecommendationViewHolder mSearchAndRecommendationViewHolder; |
| 87 | private SearchAndRecommendationsScrollController mSearchAndRecommendationsScrollController; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 88 | |
| 89 | public WidgetsFullSheet(Context context, AttributeSet attrs, int defStyleAttr) { |
| 90 | super(context, attrs, defStyleAttr); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 91 | mHasWorkProfile = context.getSystemService(LauncherApps.class).getProfiles().size() > 1; |
| 92 | mAdapters.put(AdapterHolder.PRIMARY, new AdapterHolder(AdapterHolder.PRIMARY)); |
| 93 | mAdapters.put(AdapterHolder.WORK, new AdapterHolder(AdapterHolder.WORK)); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public WidgetsFullSheet(Context context, AttributeSet attrs) { |
| 97 | this(context, attrs, 0); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | protected void onFinishInflate() { |
| 102 | super.onFinishInflate(); |
| 103 | mContent = findViewById(R.id.container); |
Sunny Goyal | 016d7e9 | 2018-03-09 11:09:20 -0800 | [diff] [blame] | 104 | TopRoundedCornerView springLayout = (TopRoundedCornerView) mContent; |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 105 | |
| 106 | LayoutInflater layoutInflater = LayoutInflater.from(getContext()); |
| 107 | int contentLayoutRes = mHasWorkProfile ? R.layout.widgets_full_sheet_paged_view |
| 108 | : R.layout.widgets_full_sheet_recyclerview; |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 109 | layoutInflater.inflate(contentLayoutRes, springLayout, true); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 110 | |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 111 | RecyclerViewFastScroller fastScroller = findViewById(R.id.fast_scroller); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 112 | if (mHasWorkProfile) { |
| 113 | mViewPager = findViewById(R.id.widgets_view_pager); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 114 | mViewPager.initParentViews(this); |
| 115 | mViewPager.getPageIndicator().setOnActivePageChangedListener(this); |
| 116 | mViewPager.getPageIndicator().setActiveMarker(AdapterHolder.PRIMARY); |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 117 | mTabsView = findViewById(R.id.tabs); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 118 | findViewById(R.id.tab_personal) |
| 119 | .setOnClickListener((View view) -> mViewPager.snapToPage(0)); |
| 120 | findViewById(R.id.tab_work) |
| 121 | .setOnClickListener((View view) -> mViewPager.snapToPage(1)); |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 122 | fastScroller.setIsRecyclerViewFirstChildInParent(false); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 123 | springLayout.addSpringView(R.id.primary_widgets_list_view); |
| 124 | springLayout.addSpringView(R.id.work_widgets_list_view); |
| 125 | } else { |
| 126 | mViewPager = null; |
| 127 | springLayout.addSpringView(R.id.primary_widgets_list_view); |
| 128 | } |
| 129 | |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 130 | layoutInflater.inflate(R.layout.widgets_full_sheet_search_and_recommendations, springLayout, |
| 131 | true); |
| 132 | springLayout.addSpringView(R.id.search_and_recommendations_container); |
| 133 | |
| 134 | mSearchAndRecommendationViewHolder = new SearchAndRecommendationViewHolder( |
| 135 | findViewById(R.id.search_and_recommendations_container)); |
| 136 | mSearchAndRecommendationsScrollController = new SearchAndRecommendationsScrollController( |
| 137 | mHasWorkProfile, |
| 138 | mSearchAndRecommendationViewHolder, |
| 139 | findViewById(R.id.primary_widgets_list_view), |
| 140 | mHasWorkProfile ? findViewById(R.id.work_widgets_list_view) : null, |
| 141 | mTabsView, |
| 142 | mViewPager); |
| 143 | fastScroller.setOnFastScrollChangeListener(mSearchAndRecommendationsScrollController); |
| 144 | |
Steven Ng | d73d6e5 | 2021-03-09 22:44:04 +0000 | [diff] [blame] | 145 | mNoWidgetsView = findViewById(R.id.no_widgets_text); |
| 146 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 147 | onWidgetsBound(); |
| 148 | } |
| 149 | |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 150 | @Override |
| 151 | public void onActivePageChanged(int currentActivePage) { |
Steven Ng | d73d6e5 | 2021-03-09 22:44:04 +0000 | [diff] [blame] | 152 | AdapterHolder currentAdapterHolder = mAdapters.get(currentActivePage); |
| 153 | WidgetsRecyclerView currentRecyclerView = currentAdapterHolder.mWidgetsRecyclerView; |
Steven Ng | 7f7b2f7 | 2021-03-03 14:51:36 +0000 | [diff] [blame] | 154 | currentRecyclerView.bindFastScrollbar(); |
| 155 | mSearchAndRecommendationsScrollController.setCurrentRecyclerView(currentRecyclerView); |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 156 | |
Steven Ng | d73d6e5 | 2021-03-09 22:44:04 +0000 | [diff] [blame] | 157 | updateNoWidgetsView(currentAdapterHolder); |
| 158 | |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 159 | reset(); |
| 160 | } |
| 161 | |
Steven Ng | d73d6e5 | 2021-03-09 22:44:04 +0000 | [diff] [blame] | 162 | private void updateNoWidgetsView(AdapterHolder adapterHolder) { |
| 163 | boolean isWidgetAvailable = adapterHolder.mWidgetsListAdapter.getItemCount() > 0; |
| 164 | adapterHolder.mWidgetsRecyclerView.setVisibility(isWidgetAvailable ? VISIBLE : GONE); |
| 165 | |
| 166 | // Always resets the text in case this is updated by search. |
| 167 | mNoWidgetsView.setText(R.string.no_widgets_available); |
| 168 | mNoWidgetsView.setVisibility(isWidgetAvailable ? GONE : VISIBLE); |
| 169 | } |
| 170 | |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 171 | private void reset() { |
| 172 | mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView.scrollToTop(); |
| 173 | if (mHasWorkProfile) { |
| 174 | mAdapters.get(AdapterHolder.WORK).mWidgetsRecyclerView.scrollToTop(); |
| 175 | } |
Steven Ng | 7f7b2f7 | 2021-03-03 14:51:36 +0000 | [diff] [blame] | 176 | mSearchAndRecommendationsScrollController.reset(); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 179 | @VisibleForTesting |
| 180 | public WidgetsRecyclerView getRecyclerView() { |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 181 | if (!mHasWorkProfile || mViewPager.getCurrentPage() == AdapterHolder.PRIMARY) { |
| 182 | return mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView; |
| 183 | } |
| 184 | return mAdapters.get(AdapterHolder.WORK).mWidgetsRecyclerView; |
Sunny Goyal | 8b37c57 | 2020-03-31 12:12:14 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 187 | @Override |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 188 | protected Pair<View, String> getAccessibilityTarget() { |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 189 | return Pair.create(getRecyclerView(), getContext().getString( |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 190 | mIsOpen ? R.string.widgets_list : R.string.widgets_list_closed)); |
| 191 | } |
| 192 | |
| 193 | @Override |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 194 | protected void onAttachedToWindow() { |
| 195 | super.onAttachedToWindow(); |
| 196 | mLauncher.getAppWidgetHost().addProviderChangeListener(this); |
| 197 | notifyWidgetProvidersChanged(); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | protected void onDetachedFromWindow() { |
| 202 | super.onDetachedFromWindow(); |
| 203 | mLauncher.getAppWidgetHost().removeProviderChangeListener(this); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void setInsets(Rect insets) { |
| 208 | mInsets.set(insets); |
| 209 | |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 210 | setBottomPadding(mAdapters.get(AdapterHolder.PRIMARY).mWidgetsRecyclerView, insets.bottom); |
| 211 | if (mHasWorkProfile) { |
| 212 | setBottomPadding(mAdapters.get(AdapterHolder.WORK).mWidgetsRecyclerView, insets.bottom); |
| 213 | } |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 214 | if (insets.bottom > 0) { |
| 215 | setupNavBarColor(); |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 216 | } else { |
| 217 | clearNavBarColor(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 218 | } |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 219 | |
| 220 | ((TopRoundedCornerView) mContent).setNavBarScrimHeight(mInsets.bottom); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 221 | requestLayout(); |
| 222 | } |
| 223 | |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 224 | private void setBottomPadding(RecyclerView recyclerView, int bottomPadding) { |
| 225 | recyclerView.setPadding( |
| 226 | recyclerView.getPaddingLeft(), |
| 227 | recyclerView.getPaddingTop(), |
| 228 | recyclerView.getPaddingRight(), |
| 229 | bottomPadding); |
| 230 | } |
| 231 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 232 | @Override |
| 233 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Steven Ng | e8d9234 | 2021-02-19 21:29:18 +0000 | [diff] [blame] | 234 | DeviceProfile deviceProfile = mLauncher.getDeviceProfile(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 235 | int widthUsed; |
| 236 | if (mInsets.bottom > 0) { |
Sunny Goyal | 786940a | 2020-06-02 02:31:31 -0700 | [diff] [blame] | 237 | widthUsed = mInsets.left + mInsets.right; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 238 | } else { |
Steven Ng | e8d9234 | 2021-02-19 21:29:18 +0000 | [diff] [blame] | 239 | Rect padding = deviceProfile.workspacePadding; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 240 | widthUsed = Math.max(padding.left + padding.right, |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 241 | 2 * (mInsets.left + mInsets.right)); |
| 242 | } |
| 243 | |
Steven Ng | e8d9234 | 2021-02-19 21:29:18 +0000 | [diff] [blame] | 244 | int heightUsed = mInsets.top + deviceProfile.edgeMarginPx; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 245 | measureChildWithMargins(mContent, widthMeasureSpec, |
| 246 | widthUsed, heightMeasureSpec, heightUsed); |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 247 | setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), |
| 248 | MeasureSpec.getSize(heightMeasureSpec)); |
Steven Ng | e8d9234 | 2021-02-19 21:29:18 +0000 | [diff] [blame] | 249 | |
Steven Ng | 11773f4 | 2021-02-25 22:54:37 +0000 | [diff] [blame] | 250 | int paddingPx = 2 * getResources().getDimensionPixelOffset( |
| 251 | R.dimen.widget_cell_horizontal_padding); |
| 252 | int maxSpansPerRow = getMeasuredWidth() / (deviceProfile.cellWidthPx + paddingPx); |
Steven Ng | e8d9234 | 2021-02-19 21:29:18 +0000 | [diff] [blame] | 253 | mAdapters.get(AdapterHolder.PRIMARY).mWidgetsListAdapter.setMaxHorizontalSpansPerRow( |
| 254 | maxSpansPerRow); |
| 255 | if (mHasWorkProfile) { |
| 256 | mAdapters.get(AdapterHolder.WORK).mWidgetsListAdapter.setMaxHorizontalSpansPerRow( |
| 257 | maxSpansPerRow); |
| 258 | } |
Steven Ng | 7f7b2f7 | 2021-03-03 14:51:36 +0000 | [diff] [blame] | 259 | |
| 260 | if (mInitialTabsHeight == 0 && mTabsView != null) { |
| 261 | mInitialTabsHeight = measureHeightWithVerticalMargins(mTabsView); |
| 262 | } |
| 263 | |
| 264 | mSearchAndRecommendationsScrollController.updateMarginAndPadding(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
| 268 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 269 | int width = r - l; |
| 270 | int height = b - t; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 271 | |
| 272 | // Content is laid out as center bottom aligned |
| 273 | int contentWidth = mContent.getMeasuredWidth(); |
Sunny Goyal | 786940a | 2020-06-02 02:31:31 -0700 | [diff] [blame] | 274 | int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 275 | mContent.layout(contentLeft, height - mContent.getMeasuredHeight(), |
| 276 | contentLeft + contentWidth, height); |
| 277 | |
| 278 | setTranslationShift(mTranslationShift); |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public void notifyWidgetProvidersChanged() { |
| 283 | mLauncher.refreshAndBindWidgetsForPackageUser(null); |
| 284 | } |
| 285 | |
| 286 | @Override |
Sunny Goyal | 202ae0b | 2019-02-04 16:26:42 -0800 | [diff] [blame] | 287 | public void onWidgetsBound() { |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 288 | List<WidgetsListBaseEntry> allWidgets = mLauncher.getPopupDataProvider().getAllWidgets(); |
| 289 | |
| 290 | AdapterHolder primaryUserAdapterHolder = mAdapters.get(AdapterHolder.PRIMARY); |
| 291 | primaryUserAdapterHolder.setup(findViewById(R.id.primary_widgets_list_view)); |
| 292 | primaryUserAdapterHolder.mWidgetsListAdapter.setWidgets(allWidgets); |
Steven Ng | d73d6e5 | 2021-03-09 22:44:04 +0000 | [diff] [blame] | 293 | updateNoWidgetsView(primaryUserAdapterHolder); |
| 294 | |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 295 | if (mHasWorkProfile) { |
| 296 | AdapterHolder workUserAdapterHolder = mAdapters.get(AdapterHolder.WORK); |
| 297 | workUserAdapterHolder.setup(findViewById(R.id.work_widgets_list_view)); |
| 298 | workUserAdapterHolder.mWidgetsListAdapter.setWidgets(allWidgets); |
| 299 | onActivePageChanged(mViewPager.getCurrentPage()); |
| 300 | } |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | private void open(boolean animate) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 304 | if (animate) { |
Sunny Goyal | db6cdb0 | 2018-07-13 11:03:04 -0700 | [diff] [blame] | 305 | if (getPopupContainer().getInsets().bottom > 0) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 306 | mContent.setAlpha(0); |
| 307 | setTranslationShift(VERTICAL_START_POSITION); |
| 308 | } |
| 309 | mOpenCloseAnimator.setValues( |
| 310 | PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED)); |
| 311 | mOpenCloseAnimator |
| 312 | .setDuration(DEFAULT_OPEN_DURATION) |
| 313 | .setInterpolator(AnimationUtils.loadInterpolator( |
| 314 | getContext(), android.R.interpolator.linear_out_slow_in)); |
| 315 | mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() { |
| 316 | @Override |
| 317 | public void onAnimationEnd(Animator animation) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 318 | mOpenCloseAnimator.removeListener(this); |
| 319 | } |
| 320 | }); |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 321 | post(() -> { |
Sunny Goyal | 6639a5d | 2018-02-28 15:09:36 -0800 | [diff] [blame] | 322 | mOpenCloseAnimator.start(); |
| 323 | mContent.animate().alpha(1).setDuration(FADE_IN_DURATION); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 324 | }); |
| 325 | } else { |
| 326 | setTranslationShift(TRANSLATION_SHIFT_OPENED); |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 327 | post(this::announceAccessibilityChanges); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | protected void handleClose(boolean animate) { |
| 333 | handleClose(animate, DEFAULT_OPEN_DURATION); |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | protected boolean isOfType(int type) { |
| 338 | return (type & TYPE_WIDGETS_FULL_SHEET) != 0; |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public boolean onControllerInterceptTouchEvent(MotionEvent ev) { |
| 343 | // Disable swipe down when recycler view is scrolling |
| 344 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 345 | mNoIntercept = false; |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 346 | RecyclerViewFastScroller scroller = getRecyclerView().getScrollbar(); |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame] | 347 | if (scroller.getThumbOffsetY() >= 0 |
| 348 | && getPopupContainer().isEventOverView(scroller, ev)) { |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 349 | mNoIntercept = true; |
Sunny Goyal | db6cdb0 | 2018-07-13 11:03:04 -0700 | [diff] [blame] | 350 | } else if (getPopupContainer().isEventOverView(mContent, ev)) { |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 351 | mNoIntercept = !getRecyclerView().shouldContainerScroll(ev, getPopupContainer()); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | return super.onControllerInterceptTouchEvent(ev); |
| 355 | } |
| 356 | |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame] | 357 | /** Shows the {@link WidgetsFullSheet} on the launcher. */ |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 358 | public static WidgetsFullSheet show(Launcher launcher, boolean animate) { |
| 359 | WidgetsFullSheet sheet = (WidgetsFullSheet) launcher.getLayoutInflater() |
| 360 | .inflate(R.layout.widgets_full_sheet, launcher.getDragLayer(), false); |
Sunny Goyal | 1642f71 | 2018-09-18 11:40:35 -0700 | [diff] [blame] | 361 | sheet.attachToContainer(); |
Sunny Goyal | de75321 | 2018-05-15 13:55:57 -0700 | [diff] [blame] | 362 | sheet.mIsOpen = true; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 363 | sheet.open(animate); |
| 364 | return sheet; |
| 365 | } |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 366 | |
Steven Ng | 2f5648a | 2021-02-08 17:18:25 +0000 | [diff] [blame] | 367 | /** Gets the {@link WidgetsRecyclerView} which shows all widgets in {@link WidgetsFullSheet}. */ |
Vadim Tryshev | 2ce6a13 | 2018-06-18 19:14:44 -0700 | [diff] [blame] | 368 | @VisibleForTesting |
| 369 | public static WidgetsRecyclerView getWidgetsView(Launcher launcher) { |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 370 | return launcher.findViewById(R.id.primary_widgets_list_view); |
Vadim Tryshev | 2ce6a13 | 2018-06-18 19:14:44 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 373 | @Override |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 374 | public void addHintCloseAnim( |
| 375 | float distanceToMove, Interpolator interpolator, PendingAnimation target) { |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 376 | target.setFloat(getRecyclerView(), VIEW_TRANSLATE_Y, -distanceToMove, interpolator); |
| 377 | target.setViewAlpha(getRecyclerView(), 0.5f, interpolator); |
Tony Wickham | 9791bd1 | 2019-04-05 13:52:35 -0700 | [diff] [blame] | 378 | } |
vadimt | 9f48a8e | 2019-12-11 11:52:37 -0800 | [diff] [blame] | 379 | |
| 380 | @Override |
| 381 | protected void onCloseComplete() { |
| 382 | super.onCloseComplete(); |
| 383 | AccessibilityManagerCompat.sendStateEventToTest(getContext(), NORMAL_STATE_ORDINAL); |
| 384 | } |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 385 | |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 386 | @Override |
| 387 | public int getHeaderViewHeight() { |
Steven Ng | 53d1364 | 2021-03-10 22:40:55 +0000 | [diff] [blame^] | 388 | return measureHeightWithVerticalMargins(mSearchAndRecommendationViewHolder.mCollapseHandle) |
| 389 | + measureHeightWithVerticalMargins(mSearchAndRecommendationViewHolder.mHeaderTitle) |
| 390 | + measureHeightWithVerticalMargins(mSearchAndRecommendationViewHolder.mSearchBar); |
Steven Ng | 7f7b2f7 | 2021-03-03 14:51:36 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** private the height, in pixel, + the vertical margins of a given view. */ |
| 394 | private static int measureHeightWithVerticalMargins(View view) { |
Steven Ng | 53d1364 | 2021-03-10 22:40:55 +0000 | [diff] [blame^] | 395 | if (view.getVisibility() != VISIBLE) { |
| 396 | return 0; |
| 397 | } |
Steven Ng | 7f7b2f7 | 2021-03-03 14:51:36 +0000 | [diff] [blame] | 398 | MarginLayoutParams marginLayoutParams = (MarginLayoutParams) view.getLayoutParams(); |
| 399 | return view.getMeasuredHeight() + marginLayoutParams.bottomMargin |
| 400 | + marginLayoutParams.topMargin; |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 403 | /** A holder class for holding adapters & their corresponding recycler view. */ |
| 404 | private final class AdapterHolder { |
| 405 | static final int PRIMARY = 0; |
| 406 | static final int WORK = 1; |
| 407 | |
| 408 | private final int mAdapterType; |
| 409 | private final WidgetsListAdapter mWidgetsListAdapter; |
| 410 | |
| 411 | private WidgetsRecyclerView mWidgetsRecyclerView; |
| 412 | |
| 413 | AdapterHolder(int adapterType) { |
| 414 | mAdapterType = adapterType; |
| 415 | |
| 416 | Context context = getContext(); |
| 417 | LauncherAppState apps = LauncherAppState.getInstance(context); |
| 418 | mWidgetsListAdapter = new WidgetsListAdapter( |
| 419 | context, |
| 420 | LayoutInflater.from(context), |
| 421 | apps.getWidgetCache(), |
| 422 | apps.getIconCache(), |
| 423 | /* iconClickListener= */ WidgetsFullSheet.this, |
| 424 | /* iconLongClickListener= */ WidgetsFullSheet.this); |
| 425 | mWidgetsListAdapter.setFilter( |
| 426 | mAdapterType == PRIMARY ? mPrimaryWidgetsFilter : mWorkWidgetsFilter); |
| 427 | } |
| 428 | |
| 429 | void setup(WidgetsRecyclerView recyclerView) { |
| 430 | mWidgetsRecyclerView = recyclerView; |
| 431 | mWidgetsRecyclerView.setAdapter(mWidgetsListAdapter); |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 432 | mWidgetsRecyclerView.setHeaderViewDimensionsProvider(WidgetsFullSheet.this); |
Steven Ng | 391404f | 2021-02-17 15:58:23 +0000 | [diff] [blame] | 433 | mWidgetsRecyclerView.setEdgeEffectFactory( |
| 434 | ((TopRoundedCornerView) mContent).createEdgeEffectFactory()); |
| 435 | mWidgetsListAdapter.setApplyBitmapDeferred(false, mWidgetsRecyclerView); |
| 436 | } |
| 437 | } |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 438 | |
| 439 | final class SearchAndRecommendationViewHolder { |
| 440 | final View mContainer; |
| 441 | final View mCollapseHandle; |
| 442 | final EditText mSearchBar; |
| 443 | final TextView mHeaderTitle; |
| 444 | |
| 445 | SearchAndRecommendationViewHolder(View searchAndRecommendationContainer) { |
| 446 | mContainer = searchAndRecommendationContainer; |
| 447 | mCollapseHandle = mContainer.findViewById(R.id.collapse_handle); |
| 448 | mSearchBar = mContainer.findViewById(R.id.widgets_search_bar); |
| 449 | mHeaderTitle = mContainer.findViewById(R.id.title); |
| 450 | } |
| 451 | } |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 452 | } |