• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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  */
16 
17 package com.android.launcher3.widget;
18 
19 import static android.view.accessibility.AccessibilityNodeInfo.ACTION_CLICK;
20 
21 import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_TRAY;
22 import static com.android.launcher3.widget.util.WidgetSizes.getWidgetItemSizePx;
23 
24 import android.animation.Animator;
25 import android.animation.AnimatorSet;
26 import android.animation.ObjectAnimator;
27 import android.animation.TimeInterpolator;
28 import android.appwidget.AppWidgetProviderInfo;
29 import android.content.Context;
30 import android.graphics.Bitmap;
31 import android.graphics.Rect;
32 import android.graphics.drawable.Drawable;
33 import android.text.TextUtils;
34 import android.util.AttributeSet;
35 import android.util.Log;
36 import android.util.Size;
37 import android.view.Gravity;
38 import android.view.MotionEvent;
39 import android.view.View;
40 import android.view.ViewGroup;
41 import android.view.ViewPropertyAnimator;
42 import android.view.accessibility.AccessibilityNodeInfo;
43 import android.widget.Button;
44 import android.widget.FrameLayout;
45 import android.widget.LinearLayout;
46 import android.widget.RemoteViews;
47 import android.widget.TextView;
48 
49 import androidx.annotation.Nullable;
50 
51 import com.android.app.animation.Interpolators;
52 import com.android.launcher3.CheckLongPressHelper;
53 import com.android.launcher3.LauncherAppState;
54 import com.android.launcher3.R;
55 import com.android.launcher3.anim.AnimatedPropertySetter;
56 import com.android.launcher3.icons.FastBitmapDrawable;
57 import com.android.launcher3.icons.RoundDrawableWrapper;
58 import com.android.launcher3.model.WidgetItem;
59 import com.android.launcher3.model.data.ItemInfoWithIcon;
60 import com.android.launcher3.model.data.PackageItemInfo;
61 import com.android.launcher3.util.CancellableTask;
62 import com.android.launcher3.views.ActivityContext;
63 import com.android.launcher3.widget.DatabaseWidgetPreviewLoader.WidgetPreviewInfo;
64 import com.android.launcher3.widget.picker.util.WidgetPreviewContainerSize;
65 import com.android.launcher3.widget.util.WidgetSizes;
66 
67 /**
68  * Represents the individual cell of the widget inside the widget tray. The preview is drawn
69  * horizontally centered, and scaled down if needed.
70  *
71  * This view does not support padding. Since the image is scaled down to fit the view, padding will
72  * further decrease the scaling factor. Drag-n-drop uses the view bounds for showing a smooth
73  * transition from the view to drag view, so when adding padding support, DnD would need to
74  * consider the appropriate scaling factor.
75  */
76 public class WidgetCell extends LinearLayout {
77 
78     private static final String TAG = "WidgetCell";
79     private static final boolean DEBUG = false;
80 
81     private static final int FADE_IN_DURATION_MS = 90;
82     private static final int ADD_BUTTON_FADE_DURATION_MS = 100;
83 
84     /**
85      * The requested scale of the preview container. It can be lower than this as well.
86      */
87     private float mPreviewContainerScale = 1f;
88     private Size mPreviewContainerSize = new Size(0, 0);
89     private FrameLayout mWidgetImageContainer;
90     private WidgetImageView mWidgetImage;
91     private TextView mWidgetName;
92     private TextView mWidgetDims;
93     private TextView mWidgetDescription;
94     private Button mWidgetAddButton;
95     private LinearLayout mWidgetTextContainer;
96 
97     private WidgetItem mItem;
98     private Size mWidgetSize;
99 
100     private final DatabaseWidgetPreviewLoader mWidgetPreviewLoader;
101     @Nullable
102     private PreviewReadyListener mPreviewReadyListener = null;
103 
104     protected CancellableTask mActiveRequest;
105     private boolean mAnimatePreview = true;
106 
107     protected final ActivityContext mActivity;
108     private final CheckLongPressHelper mLongPressHelper;
109     private final float mEnforcedCornerRadius;
110 
111     private RemoteViews mRemoteViewsPreview;
112     private NavigableAppWidgetHostView mAppWidgetHostViewPreview;
113     private float mAppWidgetHostViewScale = 1f;
114     private int mSourceContainer = CONTAINER_WIDGETS_TRAY;
115 
116     private CancellableTask mIconLoadRequest;
117     private boolean mIsShowingAddButton = false;
118     // Height enforced by the parent to align all widget cells displayed by it.
119     private int mParentAlignedPreviewHeight;
WidgetCell(Context context)120     public WidgetCell(Context context) {
121         this(context, null);
122     }
123 
WidgetCell(Context context, AttributeSet attrs)124     public WidgetCell(Context context, AttributeSet attrs) {
125         this(context, attrs, 0);
126     }
127 
WidgetCell(Context context, AttributeSet attrs, int defStyle)128     public WidgetCell(Context context, AttributeSet attrs, int defStyle) {
129         super(context, attrs, defStyle);
130 
131         mActivity = ActivityContext.lookupContext(context);
132         mWidgetPreviewLoader = new DatabaseWidgetPreviewLoader(context);
133         mLongPressHelper = new CheckLongPressHelper(this);
134         mLongPressHelper.setLongPressTimeoutFactor(1);
135         mEnforcedCornerRadius = RoundedCornerEnforcement.computeEnforcedRadius(context);
136         mWidgetSize = new Size(0, 0);
137 
138         setClipToPadding(false);
139         setAccessibilityDelegate(mActivity.getAccessibilityDelegate());
140     }
141 
142     @Override
onFinishInflate()143     protected void onFinishInflate() {
144         super.onFinishInflate();
145 
146         mWidgetImageContainer = findViewById(R.id.widget_preview_container);
147         mWidgetImage = findViewById(R.id.widget_preview);
148         mWidgetName = findViewById(R.id.widget_name);
149         mWidgetDims = findViewById(R.id.widget_dims);
150         mWidgetDescription = findViewById(R.id.widget_description);
151         mWidgetTextContainer = findViewById(R.id.widget_text_container);
152         mWidgetAddButton = findViewById(R.id.widget_add_button);
153 
154         setAccessibilityDelegate(new AccessibilityDelegate() {
155             @Override
156             public void onInitializeAccessibilityNodeInfo(View host,
157                     AccessibilityNodeInfo info) {
158                 super.onInitializeAccessibilityNodeInfo(host, info);
159                 if (hasOnClickListeners()) {
160                     String accessibilityLabel = getResources().getString(
161                             mWidgetAddButton.isShown()
162                                     ? R.string.widget_cell_tap_to_hide_add_button_label
163                                     : R.string.widget_cell_tap_to_show_add_button_label);
164                     info.addAction(new AccessibilityNodeInfo.AccessibilityAction(ACTION_CLICK,
165                             accessibilityLabel));
166                 }
167             }
168         });
169         mWidgetAddButton.setVisibility(INVISIBLE);
170     }
171 
setRemoteViewsPreview(RemoteViews view)172     public void setRemoteViewsPreview(RemoteViews view) {
173         mRemoteViewsPreview = view;
174     }
175 
176     @Nullable
getRemoteViewsPreview()177     public RemoteViews getRemoteViewsPreview() {
178         return mRemoteViewsPreview;
179     }
180 
181     /** Returns the app widget host view scale, which is a value between [0f, 1f]. */
getAppWidgetHostViewScale()182     public float getAppWidgetHostViewScale() {
183         return mAppWidgetHostViewScale;
184     }
185 
186     /** Returns the {@link WidgetItem} for this {@link WidgetCell}. */
getWidgetItem()187     public WidgetItem getWidgetItem() {
188         return mItem;
189     }
190 
191     /**
192      * Called to clear the view and free attached resources. (e.g., {@link Bitmap}
193      */
clear()194     public void clear() {
195         if (DEBUG) {
196             Log.d(TAG, "reset called on:" + mWidgetName.getText());
197         }
198         mWidgetImage.animate().cancel();
199         mWidgetImage.setDrawable(null);
200         mWidgetImage.setVisibility(View.VISIBLE);
201         mWidgetName.setText(null);
202         mWidgetDims.setText(null);
203         mWidgetDescription.setText(null);
204         mWidgetDescription.setVisibility(GONE);
205         mPreviewReadyListener = null;
206         mParentAlignedPreviewHeight = 0;
207         showDescription(true);
208         showDimensions(true);
209 
210         hideAddButton(/* animate= */ false);
211 
212         if (mActiveRequest != null) {
213             mActiveRequest.cancel();
214             mActiveRequest = null;
215         }
216         mRemoteViewsPreview = null;
217         if (mAppWidgetHostViewPreview != null) {
218             mWidgetImageContainer.removeView(mAppWidgetHostViewPreview);
219         }
220         mAppWidgetHostViewPreview = null;
221         mPreviewContainerSize = new Size(0, 0);
222         mAppWidgetHostViewScale = 1f;
223         mPreviewContainerScale = 1f;
224         mItem = null;
225         mWidgetSize = new Size(0, 0);
226         showAppIconInWidgetTitle(false);
227     }
228 
setSourceContainer(int sourceContainer)229     public void setSourceContainer(int sourceContainer) {
230         this.mSourceContainer = sourceContainer;
231     }
232 
233     /**
234      * Applies the item to this view
235      */
applyFromCellItem(WidgetItem item)236     public void applyFromCellItem(WidgetItem item) {
237         Context context = getContext();
238         mItem = item;
239         mWidgetSize = getWidgetItemSizePx(getContext(), mActivity.getDeviceProfile(), mItem);
240         initPreviewContainerSizeAndScale();
241 
242         mWidgetName.setText(mItem.label);
243         mWidgetDims.setText(context.getString(R.string.widget_dims_format,
244                 mItem.spanX, mItem.spanY));
245         if (!TextUtils.isEmpty(mItem.description)) {
246             mWidgetDescription.setText(mItem.description);
247             mWidgetDescription.setVisibility(VISIBLE);
248         } else {
249             mWidgetDescription.setVisibility(GONE);
250         }
251 
252         // Setting the content description on the WidgetCell itself ensures that it remains
253         // screen reader focusable when the add button is showing and the text is hidden.
254         setContentDescription(createContentDescription(context));
255         if (mWidgetAddButton != null) {
256             mWidgetAddButton.setContentDescription(context.getString(
257                     R.string.widget_add_button_content_description, mItem.label));
258         }
259 
260         if (item.activityInfo != null) {
261             setTag(new PendingAddShortcutInfo(item.activityInfo));
262         } else {
263             setTag(new PendingAddWidgetInfo(item.widgetInfo, mSourceContainer));
264         }
265 
266         if (mRemoteViewsPreview != null) {
267             WidgetPreviewInfo previewInfo = new WidgetPreviewInfo();
268             previewInfo.providerInfo = item.widgetInfo;
269             previewInfo.remoteViews = mRemoteViewsPreview;
270             applyPreview(previewInfo);
271         } else {
272             if (mActiveRequest == null) {
273                 mActiveRequest = mWidgetPreviewLoader.loadPreview(
274                         mItem, mWidgetSize, this::applyPreview);
275             }
276         }
277     }
278 
applyPreview(WidgetPreviewInfo previewInfo)279     private void applyPreview(WidgetPreviewInfo previewInfo) {
280         if (previewInfo.providerInfo != null) {
281             mAppWidgetHostViewPreview = createAppWidgetHostView(getContext());
282             setAppWidgetHostViewPreview(mAppWidgetHostViewPreview, previewInfo.providerInfo,
283                     previewInfo.remoteViews);
284         } else {
285             applyBitmapPreview(previewInfo.previewBitmap);
286         }
287     }
288 
initPreviewContainerSizeAndScale()289     private void initPreviewContainerSizeAndScale() {
290         WidgetPreviewContainerSize previewSize = WidgetPreviewContainerSize.Companion.forItem(mItem,
291                 mActivity.getDeviceProfile());
292         mPreviewContainerSize = WidgetSizes.getWidgetSizePx(mActivity.getDeviceProfile(),
293                 previewSize.spanX, previewSize.spanY);
294 
295         float scaleX = (float) mPreviewContainerSize.getWidth() / mWidgetSize.getWidth();
296         float scaleY = (float) mPreviewContainerSize.getHeight() / mWidgetSize.getHeight();
297         mPreviewContainerScale = Math.min(scaleX, scaleY);
298     }
299 
createContentDescription(Context context)300     private String createContentDescription(Context context) {
301         String contentDescription =
302                 context.getString(R.string.widget_preview_name_and_dims_content_description,
303                         mItem.label, mItem.spanX, mItem.spanY);
304         if (!TextUtils.isEmpty(mItem.description)) {
305             contentDescription += " " + mItem.description;
306         }
307         return contentDescription;
308     }
309 
setAppWidgetHostViewPreview( NavigableAppWidgetHostView appWidgetHostViewPreview, AppWidgetProviderInfo providerInfo, @Nullable RemoteViews remoteViews)310     private void setAppWidgetHostViewPreview(
311             NavigableAppWidgetHostView appWidgetHostViewPreview,
312             AppWidgetProviderInfo providerInfo,
313             @Nullable RemoteViews remoteViews) {
314         appWidgetHostViewPreview.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
315         appWidgetHostViewPreview.setAppWidget(/* appWidgetId= */ -1, providerInfo);
316         appWidgetHostViewPreview.updateAppWidget(remoteViews);
317         appWidgetHostViewPreview.setClipToPadding(false);
318         appWidgetHostViewPreview.setClipChildren(false);
319 
320         FrameLayout.LayoutParams widgetHostLP = new FrameLayout.LayoutParams(
321                 mWidgetSize.getWidth(), mWidgetSize.getHeight(), Gravity.CENTER);
322         mWidgetImageContainer.addView(appWidgetHostViewPreview, /* index= */ 0, widgetHostLP);
323         mWidgetImage.setVisibility(View.GONE);
324         applyBitmapPreview(null);
325 
326         appWidgetHostViewPreview.addOnLayoutChangeListener(
327                 (v, l, t, r, b, ol, ot, or, ob) ->
328                         updateAppWidgetHostScale(appWidgetHostViewPreview));
329     }
330 
updateAppWidgetHostScale(NavigableAppWidgetHostView view)331     private void updateAppWidgetHostScale(NavigableAppWidgetHostView view) {
332         // Scale the content such that all of the content is visible
333         float contentWidth = view.getWidth();
334         float contentHeight = view.getHeight();
335 
336         if (view.getChildCount() == 1) {
337             View content = view.getChildAt(0);
338             // Take the content width based on the edge furthest from the center, so that when
339             // scaling the hostView, the farthest edge is still visible.
340             contentWidth = 2 * Math.max(contentWidth / 2 - content.getLeft(),
341                     content.getRight() - contentWidth / 2);
342             contentHeight = 2 * Math.max(contentHeight / 2 - content.getTop(),
343                     content.getBottom() - contentHeight / 2);
344         }
345 
346         if (contentWidth <= 0 || contentHeight <= 0) {
347             mAppWidgetHostViewScale = 1;
348         } else {
349             float pWidth = mWidgetImageContainer.getWidth();
350             float pHeight = mWidgetImageContainer.getHeight();
351             mAppWidgetHostViewScale = Math.min(pWidth / contentWidth, pHeight / contentHeight);
352         }
353         view.setScaleToFit(mAppWidgetHostViewScale);
354 
355         // layout based previews maybe ready at this point to inspect their inner height.
356         if (mPreviewReadyListener != null) {
357             mPreviewReadyListener.onPreviewAvailable();
358             mPreviewReadyListener = null;
359         }
360     }
361 
362     /**
363      * Returns a view (holding the previews) that can be dragged and dropped.
364      */
getDragAndDropView()365     public View getDragAndDropView() {
366         return mWidgetImageContainer;
367     }
368 
getWidgetView()369     public WidgetImageView getWidgetView() {
370         return mWidgetImage;
371     }
372 
373     @Nullable
getAppWidgetHostViewPreview()374     public NavigableAppWidgetHostView getAppWidgetHostViewPreview() {
375         return mAppWidgetHostViewPreview;
376     }
377 
setAnimatePreview(boolean shouldAnimate)378     public void setAnimatePreview(boolean shouldAnimate) {
379         mAnimatePreview = shouldAnimate;
380     }
381 
applyBitmapPreview(Bitmap bitmap)382     private void applyBitmapPreview(Bitmap bitmap) {
383         if (bitmap != null) {
384             Drawable drawable = new RoundDrawableWrapper(
385                     new FastBitmapDrawable(bitmap), mEnforcedCornerRadius);
386             mWidgetImage.setDrawable(drawable);
387             mWidgetImage.setVisibility(View.VISIBLE);
388             if (mAppWidgetHostViewPreview != null) {
389                 removeView(mAppWidgetHostViewPreview);
390                 mAppWidgetHostViewPreview = null;
391             }
392 
393             // Drawables of the image previews are available at this point to measure.
394             if (mPreviewReadyListener != null) {
395                 mPreviewReadyListener.onPreviewAvailable();
396                 mPreviewReadyListener = null;
397             }
398         }
399 
400         if (mAnimatePreview) {
401             mWidgetImageContainer.setAlpha(0f);
402             ViewPropertyAnimator anim = mWidgetImageContainer.animate();
403             anim.alpha(1.0f).setDuration(FADE_IN_DURATION_MS);
404         } else {
405             mWidgetImageContainer.setAlpha(1f);
406         }
407         if (mActiveRequest != null) {
408             mActiveRequest.cancel();
409             mActiveRequest = null;
410         }
411     }
412 
413     /**
414      * Shows or hides the long description displayed below each widget.
415      *
416      * @param show a flag that shows the long description of the widget if {@code true}, hides it if
417      *             {@code false}.
418      */
showDescription(boolean show)419     public void showDescription(boolean show) {
420         mWidgetDescription.setVisibility(show ? VISIBLE : GONE);
421     }
422 
423     /**
424      * Shows or hides the dimensions displayed below each widget.
425      *
426      * @param show a flag that shows the dimensions of the widget if {@code true}, hides it if
427      *             {@code false}.
428      */
showDimensions(boolean show)429     public void showDimensions(boolean show) {
430         mWidgetDims.setVisibility(show ? VISIBLE : GONE);
431     }
432 
433     /**
434      * Set whether the app icon, for the app that provides the widget, should be shown next to the
435      * title text of the widget.
436      *
437      * @param show true if the app icon should be shown in the title text of the cell, false hides
438      *             it.
439      */
showAppIconInWidgetTitle(boolean show)440     public void showAppIconInWidgetTitle(boolean show) {
441         if (show) {
442             if (mItem.widgetInfo != null) {
443                 loadHighResPackageIcon();
444 
445                 Drawable icon = mItem.bitmap.newIcon(getContext());
446                 int size = getResources().getDimensionPixelSize(R.dimen.widget_cell_app_icon_size);
447                 icon.setBounds(0, 0, size, size);
448                 mWidgetName.setCompoundDrawablesRelative(
449                         icon,
450                         null, null, null);
451             }
452         } else {
453             cancelIconLoadRequest();
454             mWidgetName.setCompoundDrawables(null, null, null, null);
455         }
456     }
457 
458     @Override
onTouchEvent(MotionEvent ev)459     public boolean onTouchEvent(MotionEvent ev) {
460         super.onTouchEvent(ev);
461         mLongPressHelper.onTouchEvent(ev);
462         return true;
463     }
464 
465     @Override
cancelLongPress()466     public void cancelLongPress() {
467         super.cancelLongPress();
468         mLongPressHelper.cancelLongPress();
469     }
470 
createAppWidgetHostView(Context context)471     private static LauncherAppWidgetHostView createAppWidgetHostView(Context context) {
472         return new LauncherAppWidgetHostView(context) {
473             @Override
474             protected boolean shouldAllowDirectClick() {
475                 return false;
476             }
477         };
478     }
479 
480     @Override
481     public CharSequence getAccessibilityClassName() {
482         return WidgetCell.class.getName();
483     }
484 
485     @Override
486     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
487         ViewGroup.LayoutParams containerLp = mWidgetImageContainer.getLayoutParams();
488         int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
489 
490         // mPreviewContainerScale ensures the needed scaling with respect to original widget size.
491         mAppWidgetHostViewScale = mPreviewContainerScale;
492         containerLp.width = mPreviewContainerSize.getWidth();
493         int height = mPreviewContainerSize.getHeight();
494 
495         // If we don't have enough available width, scale the preview container to fit.
496         if (containerLp.width > maxWidth) {
497             containerLp.width = maxWidth;
498             mAppWidgetHostViewScale = (float) containerLp.width / mPreviewContainerSize.getWidth();
499             height = Math.round(mPreviewContainerSize.getHeight() * mAppWidgetHostViewScale);
500         }
501 
502         // Use parent aligned height in set.
503         if (mParentAlignedPreviewHeight > 0) {
504             containerLp.height = Math.min(height, mParentAlignedPreviewHeight);
505         } else {
506             containerLp.height = height;
507         }
508 
509         // No need to call mWidgetImageContainer.setLayoutParams as we are in measure pass
510         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
511     }
512 
513     @Override
514     protected void onLayout(boolean changed, int l, int t, int r, int b) {
515         super.onLayout(changed, l, t, r, b);
516 
517         if (changed && isShowingAddButton()) {
518             post(this::setupIconOrTextButton);
519         }
520     }
521 
522     /**
523      * Sets the height of the preview as adjusted by the parent to have this cell's content aligned
524      * with other cells displayed by the parent.
525      */
526     public void setParentAlignedPreviewHeight(int previewHeight) {
527         mParentAlignedPreviewHeight = previewHeight;
528     }
529 
530     /**
531      * Returns the height of the preview without any empty space.
532      * In case of appwidget host views, it returns the height of first child. This way, if preview
533      * view provided by an app doesn't fill bounds, this will return actual height without white
534      * space.
535      */
536     public int getPreviewContentHeight() {
537         // By default assume scaled height.
538         int height = Math.round(mPreviewContainerScale * mWidgetSize.getHeight());
539 
540         if (mWidgetImage != null && mWidgetImage.getDrawable() != null) {
541             // getBitmapBounds returns the scaled bounds.
542             Rect bitmapBounds = mWidgetImage.getBitmapBounds();
543             height = bitmapBounds.height();
544         } else if (mAppWidgetHostViewPreview != null
545                 && mAppWidgetHostViewPreview.getChildCount() == 1) {
546             int contentHeight = Math.round(
547                     mPreviewContainerScale * mWidgetSize.getHeight());
548             int previewInnerHeight = Math.round(
549                     mAppWidgetHostViewScale * mAppWidgetHostViewPreview.getChildAt(
550                             0).getMeasuredHeight());
551             // Use either of the inner scaled height or the scaled widget height
552             height = Math.min(contentHeight, previewInnerHeight);
553         }
554 
555         return height;
556     }
557 
558     /**
559      * Loads a high resolution package icon to show next to the widget title.
560      */
561     public void loadHighResPackageIcon() {
562         cancelIconLoadRequest();
563         if (mItem.bitmap.isLowRes()) {
564             // We use the package icon instead of the receiver one so that the overall package that
565             // the widget came from can be identified in the recommended widgets. This matches with
566             // the package icon headings in the all widgets list.
567             PackageItemInfo tmpPackageItem = new PackageItemInfo(
568                     mItem.componentName.getPackageName(),
569                     mItem.user);
570             mIconLoadRequest = LauncherAppState.getInstance(getContext()).getIconCache()
571                     .updateIconInBackground(this::reapplyIconInfo, tmpPackageItem);
572         }
573     }
574 
575     /** Can be called to update the package icon shown in the label of recommended widgets. */
576     private void reapplyIconInfo(ItemInfoWithIcon info) {
577         if (mItem == null || info.bitmap.isNullOrLowRes()) {
578             showAppIconInWidgetTitle(false);
579             return;
580         }
581         mItem.bitmap = info.bitmap;
582         showAppIconInWidgetTitle(true);
583     }
584 
585     private void cancelIconLoadRequest() {
586         if (mIconLoadRequest != null) {
587             mIconLoadRequest.cancel();
588             mIconLoadRequest = null;
589         }
590     }
591 
592     /**
593      * Show tap to add button.
594      * @param callback Callback to be set on the button.
595      */
596     public void showAddButton(View.OnClickListener callback) {
597         if (mIsShowingAddButton) return;
598         mIsShowingAddButton = true;
599 
600         setupIconOrTextButton();
601         mWidgetAddButton.setOnClickListener(callback);
602         fadeThrough(/* hide= */ mWidgetTextContainer, /* show= */ mWidgetAddButton,
603                 ADD_BUTTON_FADE_DURATION_MS, Interpolators.LINEAR);
604     }
605 
606     /**
607      * Depending on the width of the cell, set up the add button to be icon-only or icon+text.
608      */
609     private void setupIconOrTextButton() {
610         String addText = getResources().getString(R.string.widget_add_button_label);
611         Rect textSize = new Rect();
612         mWidgetAddButton.getPaint().getTextBounds(addText, 0, addText.length(), textSize);
613         int startPadding = getResources()
614                 .getDimensionPixelSize(R.dimen.widget_cell_add_button_start_padding);
615         int endPadding = getResources()
616                 .getDimensionPixelSize(R.dimen.widget_cell_add_button_end_padding);
617         int drawableWidth = getResources()
618                 .getDimensionPixelSize(R.dimen.widget_cell_add_button_drawable_width);
619         int drawablePadding = getResources()
620                 .getDimensionPixelSize(R.dimen.widget_cell_add_button_drawable_padding);
621         int textButtonWidth = textSize.width() + startPadding + endPadding + drawableWidth
622                 + drawablePadding;
623         if (textButtonWidth > getMeasuredWidth()) {
624             // Setup icon-only button
625             mWidgetAddButton.setText(null);
626             int startIconPadding = getResources()
627                     .getDimensionPixelSize(R.dimen.widget_cell_add_icon_button_start_padding);
628             mWidgetAddButton.setPaddingRelative(/* start= */ startIconPadding, /* top= */ 0,
629                     /* end= */ endPadding, /* bottom= */ 0);
630             mWidgetAddButton.setCompoundDrawablePadding(0);
631         } else {
632             // Setup icon + text button
633             mWidgetAddButton.setText(addText);
634             mWidgetAddButton.setPaddingRelative(/* start= */ startPadding, /* top= */ 0,
635                     /* end= */ endPadding, /* bottom= */ 0);
636             mWidgetAddButton.setCompoundDrawablePadding(drawablePadding);
637         }
638     }
639 
640     /**
641      * Hide tap to add button.
642      */
643     public void hideAddButton(boolean animate) {
644         if (!mIsShowingAddButton) return;
645         mIsShowingAddButton = false;
646 
647         mWidgetAddButton.setOnClickListener(null);
648 
649         if (!animate) {
650             mWidgetAddButton.setVisibility(INVISIBLE);
651             mWidgetTextContainer.setVisibility(VISIBLE);
652             mWidgetTextContainer.setAlpha(1F);
653             return;
654         }
655 
656         fadeThrough(/* hide= */ mWidgetAddButton, /* show= */ mWidgetTextContainer,
657                 ADD_BUTTON_FADE_DURATION_MS, Interpolators.LINEAR);
658     }
659 
660     public boolean isShowingAddButton() {
661         return mIsShowingAddButton;
662     }
663 
664     private static void fadeThrough(View hide, View show, int durationMs,
665             TimeInterpolator interpolator) {
666         AnimatedPropertySetter setter = new AnimatedPropertySetter();
667 
668         Animator hideAnim = setter.setViewAlpha(hide, 0F, interpolator).setDuration(durationMs);
669         if (hideAnim instanceof ObjectAnimator anim) {
670             anim.setAutoCancel(true);
671         }
672 
673         Animator showAnim = setter.setViewAlpha(show, 1F, interpolator).setDuration(durationMs);
674         if (showAnim instanceof ObjectAnimator anim) {
675             anim.setAutoCancel(true);
676         }
677 
678         AnimatorSet set = new AnimatorSet();
679         set.playSequentially(hideAnim, showAnim);
680         set.start();
681     }
682 
683     /**
684      * Returns true if this WidgetCell is displaying the same item as info.
685      */
686     public boolean matchesItem(WidgetItem info) {
687         if (info == null || mItem == null) return false;
688         if (info.widgetInfo != null && mItem.widgetInfo != null) {
689             return info.widgetInfo.getUser().equals(mItem.widgetInfo.getUser())
690                     && info.widgetInfo.getComponent().equals(mItem.widgetInfo.getComponent());
691         } else if (info.activityInfo != null && mItem.activityInfo != null) {
692             return info.activityInfo.getUser().equals(mItem.activityInfo.getUser())
693                     && info.activityInfo.getComponent().equals(mItem.activityInfo.getComponent());
694         }
695         return false;
696     }
697 
698     /**
699      * Listener to notify when previews are available.
700      */
701     public void addPreviewReadyListener(PreviewReadyListener previewReadyListener) {
702         mPreviewReadyListener = previewReadyListener;
703     }
704 
705     /**
706      * Listener interface for subscribers to listen to preview's availability.
707      */
708     public interface PreviewReadyListener {
709         /** Handler on to invoke when previews are available. */
710         void onPreviewAvailable();
711     }
712 }
713