ActivityPickerpublic class ActivityPicker extends com.android.internal.app.AlertActivity implements DialogInterface.OnClickListener, DialogInterface.OnCancelListenerDisplays a list of all activities matching the incoming
{@link Intent#EXTRA_INTENT} query, along with any injected items. |
Fields Summary |
---|
private PickAdapter | mAdapterAdapter of items that are displayed in this dialog. | private android.content.Intent | mBaseIntentBase {@link Intent} used when building list. |
Methods Summary |
---|
protected android.content.Intent | getIntentForPosition(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.List | getItems()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 void | onCancel(android.content.DialogInterface dialog)Handle canceled dialog by passing back {@link Activity#RESULT_CANCELED}.
setResult(Activity.RESULT_CANCELED);
finish();
| public void | onClick(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 void | onCreate(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 void | putIntentItems(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));
}
|
|