Add tap-to-add button to widget picker

This change introduces an "Add" button that appears when a widget
preview is clicked in the widget picker. This button disappears when the
preview is clicked again, or another preview is clicked. When the button
is pressed, it adds that widget to the picker. The add button is
available in the app-specific widget sheet as well.

Bug: 323886237
Test: Manual
Flag: ACONFIG com.android.launcher3.enable_widget_tap_to_add DEVELOPMENT

Change-Id: I86a8a4c22119960c54a885fd2efeb91916b4f9a0
This commit is contained in:
Willie Koomson committed 2024-03-15 20:10:45 +00:00
1 parent a51a749bc9
commit cdc26951ff
16 files changed
+303 -43

No files matched your search

@@ -751,6 +751,9 @@ public class StatsLogManager implements ResourceBasedOverride {
+ " metric.")
LAUNCHER_SPLIT_SELECTION_EXIT_INTERRUPTED(1612),
@UiEvent(doc = "User tapped add widget button in widget sheet.")
LAUNCHER_WIDGET_ADD_BUTTON_TAP(1622),
// ADD MORE
;
@@ -18,6 +18,7 @@ package com.android.launcher3.widget;
import static com.android.app.animation.Interpolators.EMPHASIZED;
import static com.android.launcher3.Flags.enableCategorizedWidgetSuggestions;
import static com.android.launcher3.Flags.enableUnfoldedTwoPanePicker;
import static com.android.launcher3.Flags.enableWidgetTapToAdd;
import static com.android.launcher3.LauncherPrefs.WIDGETS_EDUCATION_TIP_SEEN;
import android.content.Context;
@@ -41,8 +42,10 @@ import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
import com.android.launcher3.Insettable;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherPrefs;
import com.android.launcher3.PendingAddItemInfo;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.logging.StatsLogManager;
import com.android.launcher3.popup.PopupDataProvider;
import com.android.launcher3.testing.TestLogging;
import com.android.launcher3.testing.shared.TestProtocol;
@@ -73,6 +76,8 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView<BaseActivity>
private boolean mDisableNavBarScrim = false;
@Nullable private WidgetCell mWidgetCellWithAddButton = null;
public BaseWidgetSheet(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContentHorizontalMargin = getWidgetListHorizontalMargin();
@@ -123,13 +128,49 @@ public abstract class BaseWidgetSheet extends AbstractSlideInView<BaseActivity>
@Override
public final void onClick(View v) {
if (v instanceof WidgetCell) {
mActivityContext.getItemOnClickListener().onClick(v);
} else if (v.getParent() instanceof WidgetCell wc) {
WidgetCell wc;
if (v instanceof WidgetCell view) {
wc = view;
} else if (v.getParent() instanceof WidgetCell parent) {
wc = parent;
} else {
return;
}
if (enableWidgetTapToAdd()) {
if (mWidgetCellWithAddButton != null) {
// If there is a add button currently showing, hide it.
mWidgetCellWithAddButton.hideAddButton(/* animate= */ true);
}
if (mWidgetCellWithAddButton != wc) {
// If click is on a cell not showing an add button, show it now.
final PendingAddItemInfo info = (PendingAddItemInfo) wc.getTag();
if (mActivityContext instanceof Launcher) {
wc.showAddButton((view) -> addWidget(info));
} else {
wc.showAddButton((view) -> mActivityContext.getItemOnClickListener()
.onClick(wc));
}
}
mWidgetCellWithAddButton = mWidgetCellWithAddButton != wc ? wc : null;
} else {
mActivityContext.getItemOnClickListener().onClick(wc);
}
}
/**
* Click handler for tap to add button.
*/
public void addWidget(PendingAddItemInfo info) {
mActivityContext.getStatsLogManager().logger().withItemInfo(info).log(
StatsLogManager.LauncherEvent.LAUNCHER_WIDGET_ADD_BUTTON_TAP);
handleClose(true);
Launcher.getLauncher(mActivityContext).getAccessibilityDelegate()
.addToWorkspace(info, /*accessibility=*/ false);
}
@Override
public boolean onLongClick(View v) {
TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "Widgets.onLongClick");
@@ -18,6 +18,7 @@ package com.android.launcher3.widget;
import static android.appwidget.AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN;
import static com.android.launcher3.Flags.enableWidgetTapToAdd;
import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_TRAY;
import static com.android.launcher3.widget.LauncherAppWidgetProviderInfo.fromProviderInfo;
import static com.android.launcher3.widget.util.WidgetSizes.getWidgetItemSizePx;
@@ -36,6 +37,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
@@ -77,6 +79,7 @@ public class WidgetCell extends LinearLayout {
private static final boolean DEBUG = false;
private static final int FADE_IN_DURATION_MS = 90;
private static final int ADD_BUTTON_FADE_DURATION_MS = 300;
/**
* The requested scale of the preview container. It can be lower than this as well.
@@ -89,6 +92,8 @@ public class WidgetCell extends LinearLayout {
private TextView mWidgetName;
private TextView mWidgetDims;
private TextView mWidgetDescription;
private Button mWidgetAddButton;
private LinearLayout mWidgetTextContainer;
private WidgetItem mItem;
private Size mWidgetSize;
@@ -141,6 +146,11 @@ public class WidgetCell extends LinearLayout {
mWidgetName = findViewById(R.id.widget_name);
mWidgetDims = findViewById(R.id.widget_dims);
mWidgetDescription = findViewById(R.id.widget_description);
mWidgetTextContainer = findViewById(R.id.widget_text_container);
mWidgetAddButton = findViewById(R.id.widget_add_button);
if (enableWidgetTapToAdd()) {
mWidgetAddButton.setVisibility(INVISIBLE);
}
}
public void setRemoteViewsPreview(RemoteViews view) {
@@ -181,6 +191,10 @@ public class WidgetCell extends LinearLayout {
showDescription(true);
showDimensions(true);
if (enableWidgetTapToAdd()) {
hideAddButton(/* animate= */ false);
}
if (mActiveRequest != null) {
mActiveRequest.cancel();
mActiveRequest = null;
@@ -223,12 +237,8 @@ public class WidgetCell extends LinearLayout {
initPreviewContainerSizeAndScale();
mWidgetName.setText(mItem.label);
mWidgetName.setContentDescription(
context.getString(R.string.widget_preview_context_description, mItem.label));
mWidgetDims.setText(context.getString(R.string.widget_dims_format,
mItem.spanX, mItem.spanY));
mWidgetDims.setContentDescription(context.getString(
R.string.widget_accessible_dims_format, mItem.spanX, mItem.spanY));
if (!TextUtils.isEmpty(mItem.description)) {
mWidgetDescription.setText(mItem.description);
mWidgetDescription.setVisibility(VISIBLE);
@@ -236,6 +246,14 @@ public class WidgetCell extends LinearLayout {
mWidgetDescription.setVisibility(GONE);
}
// Setting the content description on the WidgetCell itself ensures that it remains
// screen reader focusable when the add button is showing and the text is hidden.
setContentDescription(createContentDescription(context));
if (mWidgetAddButton != null) {
mWidgetAddButton.setContentDescription(context.getString(
R.string.widget_add_button_content_description, mItem.label));
}
if (item.activityInfo != null) {
setTag(new PendingAddShortcutInfo(item.activityInfo));
} else {
@@ -285,6 +303,16 @@ public class WidgetCell extends LinearLayout {
mPreviewContainerScale = Math.min(scaleX, scaleY);
}
private String createContentDescription(Context context) {
String contentDescription =
context.getString(R.string.widget_preview_name_and_dims_content_description,
mItem.label, mItem.spanX, mItem.spanY);
if (!TextUtils.isEmpty(mItem.description)) {
contentDescription += " " + mItem.description;
}
return contentDescription;
}
private void setAppWidgetHostViewPreview(
NavigableAppWidgetHostView appWidgetHostViewPreview,
LauncherAppWidgetProviderInfo providerInfo,
@@ -517,4 +545,55 @@ public class WidgetCell extends LinearLayout {
mIconLoadRequest = null;
}
}
/**
* Show tap to add button.
* @param callback Callback to be set on the button.
*/
public void showAddButton(View.OnClickListener callback) {
mWidgetAddButton.setAlpha(0F);
mWidgetAddButton.setVisibility(VISIBLE);
mWidgetAddButton.setOnClickListener(callback);
mWidgetAddButton.animate().cancel();
mWidgetAddButton.animate()
.alpha(1F)
.setDuration(ADD_BUTTON_FADE_DURATION_MS);
mWidgetTextContainer.animate().cancel();
mWidgetTextContainer.animate()
.alpha(0F)
.setDuration(ADD_BUTTON_FADE_DURATION_MS)
.withEndAction(() -> {
mWidgetTextContainer.setVisibility(INVISIBLE);
});
}
/**
* Hide tap to add button.
*/
public void hideAddButton(boolean animate) {
mWidgetAddButton.setOnClickListener(null);
mWidgetAddButton.animate().cancel();
mWidgetTextContainer.animate().cancel();
if (!animate) {
mWidgetAddButton.setVisibility(INVISIBLE);
mWidgetTextContainer.setVisibility(VISIBLE);
mWidgetTextContainer.setAlpha(1F);
return;
}
mWidgetAddButton.animate()
.alpha(0F)
.setDuration(ADD_BUTTON_FADE_DURATION_MS)
.withEndAction(() -> {
mWidgetAddButton.setVisibility(INVISIBLE);
});
mWidgetTextContainer.setAlpha(0F);
mWidgetTextContainer.setVisibility(VISIBLE);
mWidgetTextContainer.animate()
.alpha(1F)
.setDuration(ADD_BUTTON_FADE_DURATION_MS);
}
}