Separating SystemShortcut and Factory

> This makes easier to extend the factory and callbacks separately
> Availability checks are only performed once when getting the shortcut

Change-Id: I413541eabfb2b9e987c852d5171c6696b1853958
This commit is contained in:
Sunny Goyal committed 2019-10-23 12:57:50 -07:00
1 parent c78308a9a7
commit 7805d49764
9 files changed
+517 -560

No files matched your search

@@ -382,8 +382,7 @@ public class PopupContainerWithArrow extends ArrowPopup implements DragSource,
@Override
public void onWidgetsBound() {
ItemInfo itemInfo = (ItemInfo) mOriginalIcon.getTag();
SystemShortcut widgetInfo = new SystemShortcut.Widgets();
View.OnClickListener onClickListener = widgetInfo.getOnClickListener(mLauncher, itemInfo);
SystemShortcut widgetInfo = SystemShortcut.WIDGETS.getShortcut(mLauncher, itemInfo);
View widgetsView = null;
int count = mSystemShortcutContainer.getChildCount();
for (int i = 0; i < count; i++) {
@@ -394,7 +393,7 @@ public class PopupContainerWithArrow extends ArrowPopup implements DragSource,
}
}
if (onClickListener != null && widgetsView == null) {
if (widgetInfo != null && widgetsView == null) {
// We didn't have any widgets cached but now there are some, so enable the shortcut.
if (mSystemShortcutContainer != this) {
initializeSystemShortcut(
@@ -407,7 +406,7 @@ public class PopupContainerWithArrow extends ArrowPopup implements DragSource,
close(false);
PopupContainerWithArrow.showForIcon(mOriginalIcon);
}
} else if (onClickListener == null && widgetsView != null) {
} else if (widgetInfo == null && widgetsView != null) {
// No widgets exist, but we previously added the shortcut so remove it.
if (mSystemShortcutContainer != this) {
mSystemShortcutContainer.removeView(widgetsView);
@@ -430,8 +429,7 @@ public class PopupContainerWithArrow extends ArrowPopup implements DragSource,
info.setIconAndContentDescriptionFor((ImageView) view);
}
view.setTag(info);
view.setOnClickListener(info.getOnClickListener(mLauncher,
(ItemInfo) mOriginalIcon.getTag()));
view.setOnClickListener(info);
}
/**
@@ -16,13 +16,19 @@
package com.android.launcher3.popup;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.app.RemoteAction;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.launcher3.AbstractFloatingView;
@@ -32,55 +38,75 @@ import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.userevent.nano.LauncherLogProto;
@TargetApi(Build.VERSION_CODES.Q)
public class RemoteActionShortcut extends SystemShortcut<BaseDraggingActivity> {
private static final String TAG = "RemoteActionShortcut";
private static final boolean DEBUG = Utilities.IS_DEBUG_DEVICE;
private final RemoteAction mAction;
public RemoteActionShortcut(RemoteAction action) {
super(action.getIcon(), action.getTitle(), action.getContentDescription(),
R.id.action_remote_action_shortcut);
public RemoteActionShortcut(RemoteAction action,
BaseDraggingActivity activity, ItemInfo itemInfo) {
super(0, R.id.action_remote_action_shortcut, activity, itemInfo);
mAction = action;
}
@Override
public View.OnClickListener getOnClickListener(
final BaseDraggingActivity activity, final ItemInfo itemInfo) {
return view -> {
AbstractFloatingView.closeAllOpenViews(activity);
public void setIconAndLabelFor(View iconView, TextView labelView) {
mAction.getIcon().loadDrawableAsync(iconView.getContext(),
iconView::setBackground,
MAIN_EXECUTOR.getHandler());
labelView.setText(mAction.getTitle());
}
final String actionIdentity = mAction.getTitle() + ", " +
itemInfo.getTargetComponent().getPackageName();
try {
if (DEBUG) Log.d(TAG, "Sending action: " + actionIdentity);
mAction.getActionIntent().send(
activity,
0,
new Intent().putExtra(
Intent.EXTRA_PACKAGE_NAME,
itemInfo.getTargetComponent().getPackageName()),
(pendingIntent, intent, resultCode, resultData, resultExtras) -> {
if (DEBUG) Log.d(TAG, "Action is complete: " + actionIdentity);
if (resultData != null && !resultData.isEmpty()) {
Log.e(TAG, "Remote action returned result: " + actionIdentity
+ " : " + resultData);
Toast.makeText(activity, resultData, Toast.LENGTH_SHORT).show();
}
},
new Handler(Looper.getMainLooper()));
} catch (PendingIntent.CanceledException e) {
Log.e(TAG, "Remote action canceled: " + actionIdentity, e);
Toast.makeText(activity, activity.getString(
R.string.remote_action_failed,
mAction.getTitle()),
Toast.LENGTH_SHORT)
.show();
}
@Override
public void setIconAndContentDescriptionFor(ImageView view) {
mAction.getIcon().loadDrawableAsync(view.getContext(),
view::setImageDrawable,
MAIN_EXECUTOR.getHandler());
view.setContentDescription(mAction.getContentDescription());
}
activity.getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.Touch.TAP,
LauncherLogProto.ControlType.REMOTE_ACTION_SHORTCUT, view);
};
@Override
public AccessibilityNodeInfo.AccessibilityAction createAccessibilityAction(Context context) {
return new AccessibilityNodeInfo.AccessibilityAction(
R.id.action_remote_action_shortcut, mAction.getContentDescription());
}
@Override
public void onClick(View view) {
AbstractFloatingView.closeAllOpenViews(mTarget);
final String actionIdentity = mAction.getTitle() + ", "
+ mItemInfo.getTargetComponent().getPackageName();
try {
if (DEBUG) Log.d(TAG, "Sending action: " + actionIdentity);
mAction.getActionIntent().send(
mTarget,
0,
new Intent().putExtra(
Intent.EXTRA_PACKAGE_NAME,
mItemInfo.getTargetComponent().getPackageName()),
(pendingIntent, intent, resultCode, resultData, resultExtras) -> {
if (DEBUG) Log.d(TAG, "Action is complete: " + actionIdentity);
if (resultData != null && !resultData.isEmpty()) {
Log.e(TAG, "Remote action returned result: " + actionIdentity
+ " : " + resultData);
Toast.makeText(mTarget, resultData, Toast.LENGTH_SHORT).show();
}
},
MAIN_EXECUTOR.getHandler());
} catch (PendingIntent.CanceledException e) {
Log.e(TAG, "Remote action canceled: " + actionIdentity, e);
Toast.makeText(mTarget, mTarget.getString(
R.string.remote_action_failed,
mAction.getTitle()),
Toast.LENGTH_SHORT)
.show();
}
mTarget.getUserEventDispatcher().logActionOnControl(LauncherLogProto.Action.Touch.TAP,
LauncherLogProto.ControlType.REMOTE_ACTION_SHORTCUT, view);
}
@Override
@@ -5,14 +5,13 @@ import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Icon;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.android.launcher3.AbstractFloatingView;
import com.android.launcher3.BaseDraggingActivity;
import com.android.launcher3.ItemInfo;
@@ -39,41 +38,30 @@ import java.util.List;
* Example system shortcuts, defined as inner classes, include Widgets and AppInfo.
* @param <T>
*/
public abstract class SystemShortcut<T extends BaseDraggingActivity>
extends ItemInfo {
public abstract class SystemShortcut<T extends BaseDraggingActivity> extends ItemInfo
implements View.OnClickListener {
private final int mIconResId;
private final int mLabelResId;
private final Icon mIcon;
private final CharSequence mLabel;
private final CharSequence mContentDescription;
private final int mAccessibilityActionId;
public SystemShortcut(int iconResId, int labelResId) {
protected final T mTarget;
protected final ItemInfo mItemInfo;
public SystemShortcut(int iconResId, int labelResId, T target, ItemInfo itemInfo) {
mIconResId = iconResId;
mLabelResId = labelResId;
mAccessibilityActionId = labelResId;
mIcon = null;
mLabel = null;
mContentDescription = null;
mTarget = target;
mItemInfo = itemInfo;
}
public SystemShortcut(Icon icon, CharSequence label, CharSequence contentDescription,
int accessibilityActionId) {
mIcon = icon;
mLabel = label;
mContentDescription = contentDescription;
mAccessibilityActionId = accessibilityActionId;
mIconResId = 0;
mLabelResId = 0;
}
public SystemShortcut(SystemShortcut other) {
public SystemShortcut(SystemShortcut<T> other) {
mIconResId = other.mIconResId;
mLabelResId = other.mLabelResId;
mIcon = other.mIcon;
mLabel = other.mLabel;
mContentDescription = other.mContentDescription;
mAccessibilityActionId = other.mAccessibilityActionId;
mTarget = other.mTarget;
mItemInfo = other.mItemInfo;
}
/**
@@ -84,150 +72,135 @@ public abstract class SystemShortcut<T extends BaseDraggingActivity>
}
public void setIconAndLabelFor(View iconView, TextView labelView) {
if (mIcon != null) {
mIcon.loadDrawableAsync(iconView.getContext(),
iconView::setBackground,
new Handler(Looper.getMainLooper()));
} else {
iconView.setBackgroundResource(mIconResId);
}
if (mLabel != null) {
labelView.setText(mLabel);
} else {
labelView.setText(mLabelResId);
}
iconView.setBackgroundResource(mIconResId);
labelView.setText(mLabelResId);
}
public void setIconAndContentDescriptionFor(ImageView view) {
if (mIcon != null) {
mIcon.loadDrawableAsync(view.getContext(),
view::setImageDrawable,
new Handler(Looper.getMainLooper()));
} else {
view.setImageResource(mIconResId);
}
view.setContentDescription(getContentDescription(view.getContext()));
}
private CharSequence getContentDescription(Context context) {
return mContentDescription != null ? mContentDescription : context.getText(mLabelResId);
view.setImageResource(mIconResId);
view.setContentDescription(view.getContext().getText(mLabelResId));
}
public AccessibilityNodeInfo.AccessibilityAction createAccessibilityAction(Context context) {
return new AccessibilityNodeInfo.AccessibilityAction(mAccessibilityActionId,
getContentDescription(context));
return new AccessibilityNodeInfo.AccessibilityAction(
mAccessibilityActionId, context.getText(mLabelResId));
}
public boolean hasHandlerForAction(int action) {
return mAccessibilityActionId == action;
}
public abstract View.OnClickListener getOnClickListener(T activity, ItemInfo itemInfo);
public interface Factory<T extends BaseDraggingActivity> {
@Nullable SystemShortcut<T> getShortcut(T activity, ItemInfo itemInfo);
}
public static final Factory<Launcher> WIDGETS = (launcher, itemInfo) -> {
if (itemInfo.getTargetComponent() == null) return null;
final List<WidgetItem> widgets =
launcher.getPopupDataProvider().getWidgetsForPackageUser(new PackageUserKey(
itemInfo.getTargetComponent().getPackageName(), itemInfo.user));
if (widgets == null) {
return null;
}
return new Widgets(launcher, itemInfo);
};
public static class Widgets extends SystemShortcut<Launcher> {
public Widgets() {
super(R.drawable.ic_widget, R.string.widget_button_text);
public Widgets(Launcher target, ItemInfo itemInfo) {
super(R.drawable.ic_widget, R.string.widget_button_text, target, itemInfo);
}
@Override
public View.OnClickListener getOnClickListener(final Launcher launcher,
final ItemInfo itemInfo) {
if (itemInfo.getTargetComponent() == null) return null;
final List<WidgetItem> widgets =
launcher.getPopupDataProvider().getWidgetsForPackageUser(new PackageUserKey(
itemInfo.getTargetComponent().getPackageName(), itemInfo.user));
if (widgets == null) {
return null;
}
return (view) -> {
AbstractFloatingView.closeAllOpenViews(launcher);
WidgetsBottomSheet widgetsBottomSheet =
(WidgetsBottomSheet) launcher.getLayoutInflater().inflate(
R.layout.widgets_bottom_sheet, launcher.getDragLayer(), false);
widgetsBottomSheet.populateAndShow(itemInfo);
launcher.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
ControlType.WIDGETS_BUTTON, view);
};
public void onClick(View view) {
AbstractFloatingView.closeAllOpenViews(mTarget);
WidgetsBottomSheet widgetsBottomSheet =
(WidgetsBottomSheet) mTarget.getLayoutInflater().inflate(
R.layout.widgets_bottom_sheet, mTarget.getDragLayer(), false);
widgetsBottomSheet.populateAndShow(mItemInfo);
mTarget.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
ControlType.WIDGETS_BUTTON, view);
}
}
public static final Factory<BaseDraggingActivity> APP_INFO = AppInfo::new;
public static class AppInfo extends SystemShortcut {
public AppInfo() {
super(R.drawable.ic_info_no_shadow, R.string.app_info_drop_target_label);
public AppInfo(BaseDraggingActivity target, ItemInfo itemInfo) {
super(R.drawable.ic_info_no_shadow, R.string.app_info_drop_target_label, target,
itemInfo);
}
@Override
public View.OnClickListener getOnClickListener(
BaseDraggingActivity activity, ItemInfo itemInfo) {
return (view) -> {
dismissTaskMenuView(activity);
Rect sourceBounds = activity.getViewBounds(view);
new PackageManagerHelper(activity).startDetailsActivityForInfo(
itemInfo, sourceBounds, ActivityOptions.makeBasic().toBundle());
activity.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
ControlType.APPINFO_TARGET, view);
};
public void onClick(View view) {
dismissTaskMenuView(mTarget);
Rect sourceBounds = mTarget.getViewBounds(view);
new PackageManagerHelper(mTarget).startDetailsActivityForInfo(
mItemInfo, sourceBounds, ActivityOptions.makeBasic().toBundle());
mTarget.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
ControlType.APPINFO_TARGET, view);
}
}
public static Factory<BaseDraggingActivity> INSTALL = (activity, itemInfo) -> {
boolean supportsWebUI = (itemInfo instanceof WorkspaceItemInfo)
&& ((WorkspaceItemInfo) itemInfo).hasStatusFlag(
WorkspaceItemInfo.FLAG_SUPPORTS_WEB_UI);
boolean isInstantApp = false;
if (itemInfo instanceof com.android.launcher3.AppInfo) {
com.android.launcher3.AppInfo appInfo = (com.android.launcher3.AppInfo) itemInfo;
isInstantApp = InstantAppResolver.newInstance(activity).isInstantApp(appInfo);
}
boolean enabled = supportsWebUI || isInstantApp;
if (!enabled) {
return null;
}
return new Install(activity, itemInfo);
};
public static class Install extends SystemShortcut {
public Install() {
super(R.drawable.ic_install_no_shadow, R.string.install_drop_target_label);
public Install(BaseDraggingActivity target, ItemInfo itemInfo) {
super(R.drawable.ic_install_no_shadow, R.string.install_drop_target_label,
target, itemInfo);
}
@Override
public View.OnClickListener getOnClickListener(
BaseDraggingActivity activity, ItemInfo itemInfo) {
boolean supportsWebUI = (itemInfo instanceof WorkspaceItemInfo) &&
((WorkspaceItemInfo) itemInfo).hasStatusFlag(WorkspaceItemInfo.FLAG_SUPPORTS_WEB_UI);
boolean isInstantApp = false;
if (itemInfo instanceof com.android.launcher3.AppInfo) {
com.android.launcher3.AppInfo appInfo = (com.android.launcher3.AppInfo) itemInfo;
isInstantApp = InstantAppResolver.newInstance(activity).isInstantApp(appInfo);
}
boolean enabled = supportsWebUI || isInstantApp;
if (!enabled) {
return null;
}
return createOnClickListener(activity, itemInfo);
}
public View.OnClickListener createOnClickListener(
BaseDraggingActivity activity, ItemInfo itemInfo) {
return view -> {
Intent intent = new PackageManagerHelper(view.getContext()).getMarketIntent(
itemInfo.getTargetComponent().getPackageName());
activity.startActivitySafely(view, intent, itemInfo, null);
AbstractFloatingView.closeAllOpenViews(activity);
};
public void onClick(View view) {
Intent intent = new PackageManagerHelper(view.getContext()).getMarketIntent(
mItemInfo.getTargetComponent().getPackageName());
mTarget.startActivitySafely(view, intent, mItemInfo, null);
AbstractFloatingView.closeAllOpenViews(mTarget);
}
}
public static Factory<Launcher> DISMISS_PREDICTION = (launcher, itemInfo) -> {
if (!FeatureFlags.ENABLE_PREDICTION_DISMISS.get()) return null;
if (itemInfo.container != LauncherSettings.Favorites.CONTAINER_PREDICTION) return null;
return new DismissPrediction(launcher, itemInfo);
};
public static class DismissPrediction extends SystemShortcut<Launcher> {
public DismissPrediction() {
super(R.drawable.ic_remove_no_shadow, R.string.dismiss_prediction_label);
public DismissPrediction(Launcher launcher, ItemInfo itemInfo) {
super(R.drawable.ic_remove_no_shadow, R.string.dismiss_prediction_label, launcher,
itemInfo);
}
@Override
public View.OnClickListener getOnClickListener(Launcher activity, ItemInfo itemInfo) {
if (!FeatureFlags.ENABLE_PREDICTION_DISMISS.get()) return null;
if (itemInfo.container != LauncherSettings.Favorites.CONTAINER_PREDICTION) return null;
return (view) -> {
PopupContainerWithArrow.closeAllOpenViews(activity);
activity.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
ControlType.DISMISS_PREDICTION, ContainerType.DEEPSHORTCUTS);
AppLaunchTracker.INSTANCE.get(view.getContext())
.onDismissApp(itemInfo.getTargetComponent(),
itemInfo.user,
AppLaunchTracker.CONTAINER_PREDICTIONS);
};
public void onClick(View view) {
PopupContainerWithArrow.closeAllOpenViews(mTarget);
mTarget.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
ControlType.DISMISS_PREDICTION, ContainerType.DEEPSHORTCUTS);
AppLaunchTracker.INSTANCE.get(view.getContext()).onDismissApp(
mItemInfo.getTargetComponent(),
mItemInfo.user,
AppLaunchTracker.CONTAINER_PREDICTIONS);
}
}
protected static void dismissTaskMenuView(BaseDraggingActivity activity) {
public static void dismissTaskMenuView(BaseDraggingActivity activity) {
AbstractFloatingView.closeOpenViews(activity, true,
AbstractFloatingView.TYPE_ALL & ~AbstractFloatingView.TYPE_REBIND_SAFE);
}
@@ -34,25 +34,24 @@ public class SystemShortcutFactory implements ResourceBasedOverride {
forOverride(SystemShortcutFactory.class, R.string.system_shortcut_factory_class);
/** Note that these are in order of priority. */
private final SystemShortcut[] mAllShortcuts;
private final SystemShortcut.Factory[] mAllFactories;
@SuppressWarnings("unused")
public SystemShortcutFactory() {
this(new SystemShortcut.AppInfo(),
new SystemShortcut.Widgets(),
new SystemShortcut.Install(),
new SystemShortcut.DismissPrediction());
this(SystemShortcut.APP_INFO, SystemShortcut.WIDGETS, SystemShortcut.INSTALL,
SystemShortcut.DISMISS_PREDICTION);
}
protected SystemShortcutFactory(SystemShortcut... shortcuts) {
mAllShortcuts = shortcuts;
protected SystemShortcutFactory(SystemShortcut.Factory... factories) {
mAllFactories = factories;
}
public @NonNull List<SystemShortcut> getEnabledShortcuts(Launcher launcher, ItemInfo info) {
List<SystemShortcut> systemShortcuts = new ArrayList<>();
for (SystemShortcut systemShortcut : mAllShortcuts) {
if (systemShortcut.getOnClickListener(launcher, info) != null) {
systemShortcuts.add(systemShortcut);
for (SystemShortcut.Factory factory : mAllFactories) {
SystemShortcut shortcut = factory.getShortcut(launcher, info);
if (shortcut != null) {
systemShortcuts.add(shortcut);
}
}