2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings;
19 import android.graphics.ColorFilter;
20 import android.util.DisplayMetrics;
21 import com.android.internal.app.AlertActivity;
22 import com.android.internal.app.AlertController;
24 import android.app.Activity;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.content.Intent;
28 import android.content.Intent.ShortcutIconResource;
29 import android.content.pm.PackageManager;
30 import android.content.pm.ResolveInfo;
31 import android.content.pm.PackageManager.NameNotFoundException;
32 import android.content.res.Resources;
33 import android.graphics.Bitmap;
34 import android.graphics.Canvas;
35 import android.graphics.Paint;
36 import android.graphics.PaintFlagsDrawFilter;
37 import android.graphics.PixelFormat;
38 import android.graphics.Rect;
39 import android.graphics.drawable.BitmapDrawable;
40 import android.graphics.drawable.Drawable;
41 import android.graphics.drawable.PaintDrawable;
42 import android.os.Bundle;
43 import android.os.Parcelable;
44 import android.view.LayoutInflater;
45 import android.view.View;
46 import android.view.ViewGroup;
47 import android.widget.BaseAdapter;
48 import android.widget.TextView;
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.List;
55 * Displays a list of all activities matching the incoming
56 * {@link Intent#EXTRA_INTENT} query, along with any injected items.
58 public class ActivityPicker extends AlertActivity implements
59 DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
62 * Adapter of items that are displayed in this dialog.
64 private PickAdapter mAdapter;
67 * Base {@link Intent} used when building list.
69 private Intent mBaseIntent;
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
75 final Intent intent = getIntent();
77 // Read base intent from extras, otherwise assume default
78 Parcelable parcel = intent.getParcelableExtra(Intent.EXTRA_INTENT);
79 if (parcel instanceof Intent) {
80 mBaseIntent = (Intent) parcel;
82 mBaseIntent = new Intent(Intent.ACTION_MAIN, null);
83 mBaseIntent.addCategory(Intent.CATEGORY_DEFAULT);
86 // Create dialog parameters
87 AlertController.AlertParams params = mAlertParams;
88 params.mOnClickListener = this;
89 params.mOnCancelListener = this;
91 // Use custom title if provided, otherwise default window title
92 if (intent.hasExtra(Intent.EXTRA_TITLE)) {
93 params.mTitle = intent.getStringExtra(Intent.EXTRA_TITLE);
95 params.mTitle = getTitle();
98 // Build list adapter of pickable items
99 List<PickAdapter.Item> items = getItems();
100 mAdapter = new PickAdapter(this, items);
101 params.mAdapter = mAdapter;
107 * Handle clicking of dialog item by passing back
108 * {@link #getIntentForPosition(int)} in {@link #setResult(int, Intent)}.
110 public void onClick(DialogInterface dialog, int which) {
111 Intent intent = getIntentForPosition(which);
112 setResult(Activity.RESULT_OK, intent);
117 * Handle canceled dialog by passing back {@link Activity#RESULT_CANCELED}.
119 public void onCancel(DialogInterface dialog) {
120 setResult(Activity.RESULT_CANCELED);
125 * Build the specific {@link Intent} for a given list position. Convenience
126 * method that calls through to {@link PickAdapter.Item#getIntent(Intent)}.
128 protected Intent getIntentForPosition(int position) {
129 PickAdapter.Item item = (PickAdapter.Item) mAdapter.getItem(position);
130 return item.getIntent(mBaseIntent);
134 * Build and return list of items to be shown in dialog. Default
135 * implementation mixes activities matching {@link #mBaseIntent} from
136 * {@link #putIntentItems(Intent, List)} with any injected items from
137 * {@link Intent#EXTRA_SHORTCUT_NAME}. Override this method in subclasses to
138 * change the items shown.
140 protected List<PickAdapter.Item> getItems() {
141 PackageManager packageManager = getPackageManager();
142 List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();
144 // Add any injected pick items
145 final Intent intent = getIntent();
146 ArrayList<String> labels =
147 intent.getStringArrayListExtra(Intent.EXTRA_SHORTCUT_NAME);
148 ArrayList<ShortcutIconResource> icons =
149 intent.getParcelableArrayListExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
151 if (labels != null && icons != null && labels.size() == icons.size()) {
152 for (int i = 0; i < labels.size(); i++) {
153 String label = labels.get(i);
154 Drawable icon = null;
157 // Try loading icon from requested package
158 ShortcutIconResource iconResource = icons.get(i);
159 Resources res = packageManager.getResourcesForApplication(
160 iconResource.packageName);
161 icon = res.getDrawable(res.getIdentifier(
162 iconResource.resourceName, null, null), null);
163 } catch (NameNotFoundException e) {
167 items.add(new PickAdapter.Item(this, label, icon));
171 // Add any intent items if base was given
172 if (mBaseIntent != null) {
173 putIntentItems(mBaseIntent, items);
180 * Fill the given list with any activities matching the base {@link Intent}.
182 protected void putIntentItems(Intent baseIntent, List<PickAdapter.Item> items) {
183 PackageManager packageManager = getPackageManager();
184 List<ResolveInfo> list = packageManager.queryIntentActivities(baseIntent,
186 Collections.sort(list, new ResolveInfo.DisplayNameComparator(packageManager));
188 final int listSize = list.size();
189 for (int i = 0; i < listSize; i++) {
190 ResolveInfo resolveInfo = list.get(i);
191 items.add(new PickAdapter.Item(this, packageManager, resolveInfo));
196 * Adapter which shows the set of activities that can be performed for a
197 * given {@link Intent}.
199 protected static class PickAdapter extends BaseAdapter {
202 * Item that appears in a {@link PickAdapter} list.
204 public static class Item implements AppWidgetLoader.LabelledItem {
205 protected static IconResizer sResizer;
207 protected IconResizer getResizer(Context context) {
208 if (sResizer == null) {
209 final Resources resources = context.getResources();
210 int size = (int) resources.getDimension(android.R.dimen.app_icon_size);
211 sResizer = new IconResizer(size, size, resources.getDisplayMetrics());
223 * Create a list item from given label and icon.
225 Item(Context context, CharSequence label, Drawable icon) {
227 this.icon = getResizer(context).createIconThumbnail(icon);
231 * Create a list item and fill it with details from the given
232 * {@link ResolveInfo} object.
234 Item(Context context, PackageManager pm, ResolveInfo resolveInfo) {
235 label = resolveInfo.loadLabel(pm);
236 if (label == null && resolveInfo.activityInfo != null) {
237 label = resolveInfo.activityInfo.name;
240 icon = getResizer(context).createIconThumbnail(resolveInfo.loadIcon(pm));
241 packageName = resolveInfo.activityInfo.applicationInfo.packageName;
242 className = resolveInfo.activityInfo.name;
246 * Build the {@link Intent} described by this item. If this item
247 * can't create a valid {@link android.content.ComponentName}, it will return
248 * {@link Intent#ACTION_CREATE_SHORTCUT} filled with the item label.
250 Intent getIntent(Intent baseIntent) {
251 Intent intent = new Intent(baseIntent);
252 if (packageName != null && className != null) {
253 // Valid package and class, so fill details as normal intent
254 intent.setClassName(packageName, className);
255 if (extras != null) {
256 intent.putExtras(extras);
259 // No valid package or class, so treat as shortcut with label
260 intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
261 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
266 public CharSequence getLabel() {
271 private final LayoutInflater mInflater;
272 private final List<Item> mItems;
275 * Create an adapter for the given items.
277 public PickAdapter(Context context, List<Item> items) {
278 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
285 public int getCount() {
286 return mItems.size();
292 public Object getItem(int position) {
293 return mItems.get(position);
299 public long getItemId(int position) {
306 public View getView(int position, View convertView, ViewGroup parent) {
307 if (convertView == null) {
308 convertView = mInflater.inflate(R.layout.pick_item, parent, false);
311 Item item = (Item) getItem(position);
312 TextView textView = (TextView) convertView;
313 textView.setText(item.label);
314 textView.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null);
321 * Utility class to resize icons to match default icon size. Code is mostly
322 * borrowed from Launcher.
324 private static class IconResizer {
325 private final int mIconWidth;
326 private final int mIconHeight;
328 private final DisplayMetrics mMetrics;
329 private final Rect mOldBounds = new Rect();
330 private final Canvas mCanvas = new Canvas();
332 public IconResizer(int width, int height, DisplayMetrics metrics) {
333 mCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
334 Paint.FILTER_BITMAP_FLAG));
338 mIconHeight = height;
342 * Returns a Drawable representing the thumbnail of the specified Drawable.
343 * The size of the thumbnail is defined by the dimension
344 * android.R.dimen.launcher_application_icon_size.
346 * This method is not thread-safe and should be invoked on the UI thread only.
348 * @param icon The icon to get a thumbnail of.
350 * @return A thumbnail for the specified icon or the icon itself if the
351 * thumbnail could not be created.
353 public Drawable createIconThumbnail(Drawable icon) {
354 int width = mIconWidth;
355 int height = mIconHeight;
358 return new EmptyDrawable(width, height);
362 if (icon instanceof PaintDrawable) {
363 PaintDrawable painter = (PaintDrawable) icon;
364 painter.setIntrinsicWidth(width);
365 painter.setIntrinsicHeight(height);
366 } else if (icon instanceof BitmapDrawable) {
367 // Ensure the bitmap has a density.
368 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
369 Bitmap bitmap = bitmapDrawable.getBitmap();
370 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
371 bitmapDrawable.setTargetDensity(mMetrics);
374 int iconWidth = icon.getIntrinsicWidth();
375 int iconHeight = icon.getIntrinsicHeight();
377 if (iconWidth > 0 && iconHeight > 0) {
378 if (width < iconWidth || height < iconHeight) {
379 final float ratio = (float) iconWidth / iconHeight;
381 if (iconWidth > iconHeight) {
382 height = (int) (width / ratio);
383 } else if (iconHeight > iconWidth) {
384 width = (int) (height * ratio);
387 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
388 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
389 final Bitmap thumb = Bitmap.createBitmap(mIconWidth, mIconHeight, c);
390 final Canvas canvas = mCanvas;
391 canvas.setBitmap(thumb);
392 // Copy the old bounds to restore them later
393 // If we were to do oldBounds = icon.getBounds(),
394 // the call to setBounds() that follows would
395 // change the same instance and we would lose the
397 mOldBounds.set(icon.getBounds());
398 final int x = (mIconWidth - width) / 2;
399 final int y = (mIconHeight - height) / 2;
400 icon.setBounds(x, y, x + width, y + height);
402 icon.setBounds(mOldBounds);
403 //noinspection deprecation
404 icon = new BitmapDrawable(thumb);
405 ((BitmapDrawable) icon).setTargetDensity(mMetrics);
406 canvas.setBitmap(null);
407 } else if (iconWidth < width && iconHeight < height) {
408 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
409 final Bitmap thumb = Bitmap.createBitmap(mIconWidth, mIconHeight, c);
410 final Canvas canvas = mCanvas;
411 canvas.setBitmap(thumb);
412 mOldBounds.set(icon.getBounds());
413 final int x = (width - iconWidth) / 2;
414 final int y = (height - iconHeight) / 2;
415 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
417 icon.setBounds(mOldBounds);
418 //noinspection deprecation
419 icon = new BitmapDrawable(thumb);
420 ((BitmapDrawable) icon).setTargetDensity(mMetrics);
421 canvas.setBitmap(null);
425 } catch (Throwable t) {
426 icon = new EmptyDrawable(width, height);
433 private static class EmptyDrawable extends Drawable {
434 private final int mWidth;
435 private final int mHeight;
437 EmptyDrawable(int width, int height) {
443 public int getIntrinsicWidth() {
448 public int getIntrinsicHeight() {
453 public int getMinimumWidth() {
458 public int getMinimumHeight() {
463 public void draw(Canvas canvas) {
467 public void setAlpha(int alpha) {
471 public void setColorFilter(ColorFilter cf) {
475 public int getOpacity() {
476 return PixelFormat.TRANSLUCENT;