Home | History | Annotate | Download | only in popup
      1 package com.android.launcher3.popup;
      2 
      3 import static com.android.launcher3.userevent.nano.LauncherLogProto.Action;
      4 import static com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
      5 
      6 import android.content.Intent;
      7 import android.graphics.Rect;
      8 import android.os.Bundle;
      9 import android.view.View;
     10 
     11 import com.android.launcher3.AbstractFloatingView;
     12 import com.android.launcher3.BaseDraggingActivity;
     13 import com.android.launcher3.ItemInfo;
     14 import com.android.launcher3.Launcher;
     15 import com.android.launcher3.R;
     16 import com.android.launcher3.ShortcutInfo;
     17 import com.android.launcher3.model.WidgetItem;
     18 import com.android.launcher3.util.InstantAppResolver;
     19 import com.android.launcher3.util.PackageManagerHelper;
     20 import com.android.launcher3.util.PackageUserKey;
     21 import com.android.launcher3.widget.WidgetsBottomSheet;
     22 
     23 import java.util.List;
     24 
     25 /**
     26  * Represents a system shortcut for a given app. The shortcut should have a static label and
     27  * icon, and an onClickListener that depends on the item that the shortcut services.
     28  *
     29  * Example system shortcuts, defined as inner classes, include Widgets and AppInfo.
     30  */
     31 public abstract class SystemShortcut<T extends BaseDraggingActivity> extends ItemInfo {
     32     public final int iconResId;
     33     public final int labelResId;
     34 
     35     public SystemShortcut(int iconResId, int labelResId) {
     36         this.iconResId = iconResId;
     37         this.labelResId = labelResId;
     38     }
     39 
     40     public abstract View.OnClickListener getOnClickListener(T activity, ItemInfo itemInfo);
     41 
     42     public static class Widgets extends SystemShortcut<Launcher> {
     43 
     44         public Widgets() {
     45             super(R.drawable.ic_widget, R.string.widget_button_text);
     46         }
     47 
     48         @Override
     49         public View.OnClickListener getOnClickListener(final Launcher launcher,
     50                 final ItemInfo itemInfo) {
     51             final List<WidgetItem> widgets =
     52                     launcher.getPopupDataProvider().getWidgetsForPackageUser(new PackageUserKey(
     53                             itemInfo.getTargetComponent().getPackageName(), itemInfo.user));
     54             if (widgets == null) {
     55                 return null;
     56             }
     57             return (view) -> {
     58                 AbstractFloatingView.closeAllOpenViews(launcher);
     59                 WidgetsBottomSheet widgetsBottomSheet =
     60                         (WidgetsBottomSheet) launcher.getLayoutInflater().inflate(
     61                                 R.layout.widgets_bottom_sheet, launcher.getDragLayer(), false);
     62                 widgetsBottomSheet.populateAndShow(itemInfo);
     63                 launcher.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
     64                         ControlType.WIDGETS_BUTTON, view);
     65             };
     66         }
     67     }
     68 
     69     public static class AppInfo extends SystemShortcut {
     70         public AppInfo() {
     71             super(R.drawable.ic_info_no_shadow, R.string.app_info_drop_target_label);
     72         }
     73 
     74         @Override
     75         public View.OnClickListener getOnClickListener(
     76                 BaseDraggingActivity activity, ItemInfo itemInfo) {
     77             return (view) -> {
     78                 Rect sourceBounds = activity.getViewBounds(view);
     79                 Bundle opts = activity.getActivityLaunchOptionsAsBundle(view);
     80                 new PackageManagerHelper(activity).startDetailsActivityForInfo(
     81                         itemInfo, sourceBounds, opts);
     82                 activity.getUserEventDispatcher().logActionOnControl(Action.Touch.TAP,
     83                         ControlType.APPINFO_TARGET, view);
     84             };
     85         }
     86     }
     87 
     88     public static class Install extends SystemShortcut {
     89         public Install() {
     90             super(R.drawable.ic_install_no_shadow, R.string.install_drop_target_label);
     91         }
     92 
     93         @Override
     94         public View.OnClickListener getOnClickListener(
     95                 BaseDraggingActivity activity, ItemInfo itemInfo) {
     96             boolean supportsWebUI = (itemInfo instanceof ShortcutInfo) &&
     97                     ((ShortcutInfo) itemInfo).hasStatusFlag(ShortcutInfo.FLAG_SUPPORTS_WEB_UI);
     98             boolean isInstantApp = false;
     99             if (itemInfo instanceof com.android.launcher3.AppInfo) {
    100                 com.android.launcher3.AppInfo appInfo = (com.android.launcher3.AppInfo) itemInfo;
    101                 isInstantApp = InstantAppResolver.newInstance(activity).isInstantApp(appInfo);
    102             }
    103             boolean enabled = supportsWebUI || isInstantApp;
    104             if (!enabled) {
    105                 return null;
    106             }
    107             return createOnClickListener(activity, itemInfo);
    108         }
    109 
    110         public View.OnClickListener createOnClickListener(
    111                 BaseDraggingActivity activity, ItemInfo itemInfo) {
    112             return view -> {
    113                 Intent intent = new PackageManagerHelper(view.getContext()).getMarketIntent(
    114                         itemInfo.getTargetComponent().getPackageName());
    115                 activity.startActivitySafely(view, intent, itemInfo);
    116                 AbstractFloatingView.closeAllOpenViews(activity);
    117             };
    118         }
    119     }
    120 }
    121