Home | History | Annotate | Download | only in popup
      1 package com.android.launcher3.popup;
      2 
      3 import android.content.Context;
      4 import android.graphics.Rect;
      5 import android.graphics.drawable.Drawable;
      6 import android.os.Bundle;
      7 import android.view.View;
      8 
      9 import com.android.launcher3.AbstractFloatingView;
     10 import com.android.launcher3.InfoDropTarget;
     11 import com.android.launcher3.ItemInfo;
     12 import com.android.launcher3.Launcher;
     13 import com.android.launcher3.R;
     14 import com.android.launcher3.model.WidgetItem;
     15 import com.android.launcher3.util.PackageUserKey;
     16 import com.android.launcher3.util.Themes;
     17 import com.android.launcher3.widget.WidgetsBottomSheet;
     18 
     19 import java.util.List;
     20 
     21 /**
     22  * Represents a system shortcut for a given app. The shortcut should have a static label and
     23  * icon, and an onClickListener that depends on the item that the shortcut services.
     24  *
     25  * Example system shortcuts, defined as inner classes, include Widgets and AppInfo.
     26  */
     27 public abstract class SystemShortcut {
     28     private final int mIconResId;
     29     private final int mLabelResId;
     30 
     31     public SystemShortcut(int iconResId, int labelResId) {
     32         mIconResId = iconResId;
     33         mLabelResId = labelResId;
     34     }
     35 
     36     public Drawable getIcon(Context context, int colorAttr) {
     37         Drawable icon = context.getResources().getDrawable(mIconResId, context.getTheme()).mutate();
     38         icon.setTint(Themes.getAttrColor(context, colorAttr));
     39         return icon;
     40     }
     41 
     42     public String getLabel(Context context) {
     43         return context.getString(mLabelResId);
     44     }
     45 
     46     public abstract View.OnClickListener getOnClickListener(final Launcher launcher,
     47             final ItemInfo itemInfo);
     48 
     49     public static class Widgets extends SystemShortcut {
     50 
     51         public Widgets() {
     52             super(R.drawable.ic_widget, R.string.widget_button_text);
     53         }
     54 
     55         @Override
     56         public View.OnClickListener getOnClickListener(final Launcher launcher,
     57                 final ItemInfo itemInfo) {
     58             final List<WidgetItem> widgets = launcher.getWidgetsForPackageUser(new PackageUserKey(
     59                     itemInfo.getTargetComponent().getPackageName(), itemInfo.user));
     60             if (widgets == null) {
     61                 return null;
     62             }
     63             return new View.OnClickListener() {
     64                 @Override
     65                 public void onClick(View view) {
     66                     AbstractFloatingView.closeAllOpenViews(launcher);
     67                     WidgetsBottomSheet widgetsBottomSheet =
     68                             (WidgetsBottomSheet) launcher.getLayoutInflater().inflate(
     69                                     R.layout.widgets_bottom_sheet, launcher.getDragLayer(), false);
     70                     widgetsBottomSheet.populateAndShow(itemInfo);
     71                 }
     72             };
     73         }
     74     }
     75 
     76     public static class AppInfo extends SystemShortcut {
     77         public AppInfo() {
     78             super(R.drawable.ic_info_no_shadow, R.string.app_info_drop_target_label);
     79         }
     80 
     81         @Override
     82         public View.OnClickListener getOnClickListener(final Launcher launcher,
     83                 final ItemInfo itemInfo) {
     84             return new View.OnClickListener() {
     85                 @Override
     86                 public void onClick(View view) {
     87                     Rect sourceBounds = launcher.getViewBounds(view);
     88                     Bundle opts = launcher.getActivityLaunchOptions(view);
     89                     InfoDropTarget.startDetailsActivityForInfo(itemInfo, launcher, null, sourceBounds, opts);
     90                 }
     91             };
     92         }
     93     }
     94 }
     95