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