Guest User

Untitled

a guest
Aug 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. package org.teameos.actions;
  2.  
  3. import android.app.Dialog;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.pm.PackageManager;
  8. import android.content.pm.ResolveInfo;
  9. import android.graphics.drawable.Drawable;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.widget.ImageView;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.List;
  18.  
  19. public class ActionDialog extends Dialog {
  20.  
  21.     public interface OnComponentSelectedListener {
  22.         void componentSelected(String componentName);
  23.     }
  24.  
  25.     private OnComponentSelectedListener mListener;
  26.     private Context mContext;
  27.     private int mViewType;
  28.     public static final int VIEW_LIST_NAMES = 1;
  29.     public static final int VIEW_ICONS = 2;
  30.     public static final int VIEW_LIST_BOTH = 3;
  31.     private ArrayList<MyAppPackage> components;
  32.     private static final String activityTitle = "Select an activity";
  33.     private static final String iconTitle = "Select an icon";
  34.     private String mTitle;
  35.  
  36.     public ActionDialog(Context context, OnComponentSelectedListener listener) {
  37.         super(context);
  38.         mContext = context;
  39.         mViewType = VIEW_LIST_BOTH;
  40.         mListener = listener;
  41.         // TODO Auto-generated constructor stub
  42.     }
  43.  
  44.     public ActionDialog(Context context, OnComponentSelectedListener listener, int ViewType) {
  45.         super(context);
  46.         mContext = context;
  47.         mViewType = ViewType;
  48.         mListener = listener;
  49.         // TODO Auto-generated constructor stub
  50.     }
  51.  
  52.     protected void onCreate(Bundle savedInstanceState) {
  53.         super.onCreate(savedInstanceState);
  54.  
  55.         switch (mViewType) {
  56.             case VIEW_LIST_NAMES:
  57.                 mTitle = activityTitle;
  58.                 break;
  59.             case VIEW_LIST_BOTH:
  60.                 mTitle = activityTitle;
  61.                 break;
  62.             case VIEW_ICONS:
  63.                 mTitle = iconTitle;
  64.                 break;
  65.         }
  66.  
  67.         PackageManager pm = mContext.getPackageManager();
  68.         Intent intent = new Intent(Intent.ACTION_MAIN, null);
  69.         intent.addCategory(Intent.CATEGORY_LAUNCHER);
  70.         List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
  71.         for (ResolveInfo info : activities) {
  72.             MyAppPackage ap = new MyAppPackage(info, pm);
  73.             components.add(ap);
  74.         }
  75.  
  76.         // create layout for dialog
  77.         LinearLayout dialogLayout = new LinearLayout(mContext);
  78.         dialogLayout.setOrientation(LinearLayout.VERTICAL);
  79.         dialogLayout.setGravity(android.view.Gravity.CENTER);
  80.         LinearLayout.LayoutParams dLayoutParams = new LinearLayout.LayoutParams(
  81.                 LinearLayout.LayoutParams.FILL_PARENT,
  82.                 LinearLayout.LayoutParams.WRAP_CONTENT);
  83.         dLayoutParams.setMargins(10, 0, 10, 5);
  84.  
  85.         for (MyAppPackage mp : components) {
  86.             // create layout for each component and add to dialog layout
  87.             LinearLayout itemLayout = new LinearLayout(dialogLayout.getContext());
  88.             itemLayout.setOrientation(LinearLayout.HORIZONTAL);
  89.             itemLayout.setGravity(android.view.Gravity.LEFT);
  90.             LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
  91.                     LinearLayout.LayoutParams.FILL_PARENT,
  92.                     LinearLayout.LayoutParams.WRAP_CONTENT);
  93.             layoutParams.setMargins(1, 1, 1, 1);
  94.  
  95.             ImageView iv = new ImageView(mContext);
  96.             iv.setImageDrawable(mp.getIcon());
  97.             itemLayout.addView(iv, layoutParams);
  98.  
  99.             TextView tv = new TextView(mContext);
  100.             tv.setText(mp.getName());
  101.             itemLayout.addView(tv, layoutParams);
  102.             dialogLayout.addView(itemLayout);
  103.         }
  104.         setContentView(dialogLayout);
  105.         setTitle(mTitle);
  106.     }
  107.  
  108.     private class MyAppPackage {
  109.         private ComponentName component;
  110.         private String appName;
  111.         private Drawable icon;
  112.  
  113.         MyAppPackage(ResolveInfo ri, PackageManager pm) {
  114.             component = new ComponentName(ri.activityInfo.packageName,
  115.                     ri.activityInfo.name);
  116.             appName = ri.activityInfo.loadLabel(pm).toString();
  117.             icon = ri.activityInfo.loadIcon(pm);
  118.         }
  119.  
  120.         ComponentName getComponentName() {
  121.             return component;
  122.         }
  123.  
  124.         Drawable getIcon() {
  125.             return icon;
  126.         }
  127.  
  128.         String getName() {
  129.             return appName;
  130.         }
  131.     }
  132.    
  133.    
  134. }
Add Comment
Please, Sign In to add comment