FileDocCategorySizeDatePackage
ActivityPicker.javaAPI DocAndroid 1.5 API15377Wed May 06 22:42:48 BST 2009com.android.settings

ActivityPicker

public class ActivityPicker extends com.android.internal.app.AlertActivity implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener
Displays a list of all activities matching the incoming {@link Intent#EXTRA_INTENT} query, along with any injected items.

Fields Summary
private PickAdapter
mAdapter
Adapter of items that are displayed in this dialog.
private android.content.Intent
mBaseIntent
Base {@link Intent} used when building list.
Constructors Summary
Methods Summary
protected android.content.IntentgetIntentForPosition(int position)
Build the specific {@link Intent} for a given list position. Convenience method that calls through to {@link PickAdapter.Item#getIntent(Intent)}.

        PickAdapter.Item item = (PickAdapter.Item) mAdapter.getItem(position);
        return item.getIntent(mBaseIntent);
    
protected java.util.ListgetItems()
Build and return list of items to be shown in dialog. Default implementation mixes activities matching {@link #mBaseIntent} from {@link #putIntentItems(Intent, List)} with any injected items from {@link Intent#EXTRA_SHORTCUT_NAME}. Override this method in subclasses to change the items shown.

        PackageManager packageManager = getPackageManager();
        List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();
        
        // Add any injected pick items
        final Intent intent = getIntent();
        ArrayList<String> labels =
            intent.getStringArrayListExtra(Intent.EXTRA_SHORTCUT_NAME);
        ArrayList<ShortcutIconResource> icons =
            intent.getParcelableArrayListExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
        
        if (labels != null && icons != null && labels.size() == icons.size()) {
            for (int i = 0; i < labels.size(); i++) {
                String label = labels.get(i);
                Drawable icon = null;
                
                try {
                    // Try loading icon from requested package
                    ShortcutIconResource iconResource = icons.get(i);
                    Resources res = packageManager.getResourcesForApplication(
                            iconResource.packageName);
                    icon = res.getDrawable(res.getIdentifier(
                            iconResource.resourceName, null, null));
                } catch (NameNotFoundException e) {
                }
                
                items.add(new PickAdapter.Item(this, label, icon));
            }
        }

        // Add any intent items if base was given
        if (mBaseIntent != null) {
            putIntentItems(mBaseIntent, items);
        }
        
        return items;
    
public voidonCancel(android.content.DialogInterface dialog)
Handle canceled dialog by passing back {@link Activity#RESULT_CANCELED}.

        setResult(Activity.RESULT_CANCELED);
        finish();
    
public voidonClick(android.content.DialogInterface dialog, int which)
Handle clicking of dialog item by passing back {@link #getIntentForPosition(int)} in {@link #setResult(int, Intent)}.

        Intent intent = getIntentForPosition(which);
        setResult(Activity.RESULT_OK, intent);
        finish();
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);
        
        final Intent intent = getIntent();
        
        // Read base intent from extras, otherwise assume default
        Parcelable parcel = intent.getParcelableExtra(Intent.EXTRA_INTENT);
        if (parcel instanceof Intent) {
            mBaseIntent = (Intent) parcel;
        } else {
            mBaseIntent = new Intent(Intent.ACTION_MAIN, null);
            mBaseIntent.addCategory(Intent.CATEGORY_DEFAULT);
        }

        // Create dialog parameters
        AlertController.AlertParams params = mAlertParams;
        params.mOnClickListener = this;
        params.mOnCancelListener = this;
        
        // Use custom title if provided, otherwise default window title
        if (intent.hasExtra(Intent.EXTRA_TITLE)) {
            params.mTitle = intent.getStringExtra(Intent.EXTRA_TITLE);
        } else {
            params.mTitle = getTitle();
        }
        
        // Build list adapter of pickable items
        List<PickAdapter.Item> items = getItems();
        mAdapter = new PickAdapter(this, items);
        params.mAdapter = mAdapter;

        setupAlert();
    
protected voidputIntentItems(android.content.Intent baseIntent, java.util.List items)
Fill the given list with any activities matching the base {@link Intent}.

        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(baseIntent,
                0 /* no flags */);
        Collections.sort(list, new ResolveInfo.DisplayNameComparator(packageManager));
        
        final int listSize = list.size();
        for (int i = 0; i < listSize; i++) {
            ResolveInfo resolveInfo = list.get(i);
            items.add(new PickAdapter.Item(this, packageManager, resolveInfo));
        }