Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.teameos.actions;
- import android.app.Dialog;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- public class ActionDialog extends Dialog {
- public interface OnComponentSelectedListener {
- void componentSelected(String componentName);
- }
- private OnComponentSelectedListener mListener;
- private Context mContext;
- private int mViewType;
- public static final int VIEW_LIST_NAMES = 1;
- public static final int VIEW_ICONS = 2;
- public static final int VIEW_LIST_BOTH = 3;
- private ArrayList<MyAppPackage> components;
- private static final String activityTitle = "Select an activity";
- private static final String iconTitle = "Select an icon";
- private String mTitle;
- public ActionDialog(Context context, OnComponentSelectedListener listener) {
- super(context);
- mContext = context;
- mViewType = VIEW_LIST_BOTH;
- mListener = listener;
- // TODO Auto-generated constructor stub
- }
- public ActionDialog(Context context, OnComponentSelectedListener listener, int ViewType) {
- super(context);
- mContext = context;
- mViewType = ViewType;
- mListener = listener;
- // TODO Auto-generated constructor stub
- }
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- switch (mViewType) {
- case VIEW_LIST_NAMES:
- mTitle = activityTitle;
- break;
- case VIEW_LIST_BOTH:
- mTitle = activityTitle;
- break;
- case VIEW_ICONS:
- mTitle = iconTitle;
- break;
- }
- PackageManager pm = mContext.getPackageManager();
- Intent intent = new Intent(Intent.ACTION_MAIN, null);
- intent.addCategory(Intent.CATEGORY_LAUNCHER);
- List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
- for (ResolveInfo info : activities) {
- MyAppPackage ap = new MyAppPackage(info, pm);
- components.add(ap);
- }
- // create layout for dialog
- LinearLayout dialogLayout = new LinearLayout(mContext);
- dialogLayout.setOrientation(LinearLayout.VERTICAL);
- dialogLayout.setGravity(android.view.Gravity.CENTER);
- LinearLayout.LayoutParams dLayoutParams = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- dLayoutParams.setMargins(10, 0, 10, 5);
- for (MyAppPackage mp : components) {
- // create layout for each component and add to dialog layout
- LinearLayout itemLayout = new LinearLayout(dialogLayout.getContext());
- itemLayout.setOrientation(LinearLayout.HORIZONTAL);
- itemLayout.setGravity(android.view.Gravity.LEFT);
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- layoutParams.setMargins(1, 1, 1, 1);
- ImageView iv = new ImageView(mContext);
- iv.setImageDrawable(mp.getIcon());
- itemLayout.addView(iv, layoutParams);
- TextView tv = new TextView(mContext);
- tv.setText(mp.getName());
- itemLayout.addView(tv, layoutParams);
- dialogLayout.addView(itemLayout);
- }
- setContentView(dialogLayout);
- setTitle(mTitle);
- }
- private class MyAppPackage {
- private ComponentName component;
- private String appName;
- private Drawable icon;
- MyAppPackage(ResolveInfo ri, PackageManager pm) {
- component = new ComponentName(ri.activityInfo.packageName,
- ri.activityInfo.name);
- appName = ri.activityInfo.loadLabel(pm).toString();
- icon = ri.activityInfo.loadIcon(pm);
- }
- ComponentName getComponentName() {
- return component;
- }
- Drawable getIcon() {
- return icon;
- }
- String getName() {
- return appName;
- }
- }
- }
Add Comment
Please, Sign In to add comment