blob: ae61944cbe1406c91e0e19dfecdbcefb00136a8a [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
Jeffrey Sharkey173545e2009-03-24 20:43:02 -07002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08003 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.settings;
18
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070019import android.app.Activity;
20import android.content.Context;
21import android.content.DialogInterface;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080022import android.content.Intent;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070023import android.content.Intent.ShortcutIconResource;
24import android.content.pm.PackageManager;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070025import android.content.pm.PackageManager.NameNotFoundException;
Jason Monk39b46742015-09-10 15:52:51 -040026import android.content.pm.ResolveInfo;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070027import android.content.res.Resources;
28import android.graphics.Bitmap;
29import android.graphics.Canvas;
Jason Monk39b46742015-09-10 15:52:51 -040030import android.graphics.ColorFilter;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070031import android.graphics.Paint;
32import android.graphics.PaintFlagsDrawFilter;
33import android.graphics.PixelFormat;
34import android.graphics.Rect;
35import android.graphics.drawable.BitmapDrawable;
36import android.graphics.drawable.Drawable;
37import android.graphics.drawable.PaintDrawable;
38import android.os.Bundle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080039import android.os.Parcelable;
Jason Monk39b46742015-09-10 15:52:51 -040040import android.util.DisplayMetrics;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070041import android.view.LayoutInflater;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042import android.view.View;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070043import android.view.ViewGroup;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070044import android.widget.BaseAdapter;
45import android.widget.TextView;
46
Jason Monk39b46742015-09-10 15:52:51 -040047import com.android.internal.app.AlertActivity;
48import com.android.internal.app.AlertController;
49
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070050import java.util.ArrayList;
51import java.util.Collections;
52import java.util.List;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080053
54/**
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070055 * Displays a list of all activities matching the incoming
56 * {@link Intent#EXTRA_INTENT} query, along with any injected items.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080057 */
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070058public class ActivityPicker extends AlertActivity implements
59 DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
60
61 /**
62 * Adapter of items that are displayed in this dialog.
63 */
64 private PickAdapter mAdapter;
65
66 /**
67 * Base {@link Intent} used when building list.
68 */
69 private Intent mBaseIntent;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080070
71 @Override
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070072 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080074
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070075 final Intent intent = getIntent();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080076
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070077 // Read base intent from extras, otherwise assume default
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080078 Parcelable parcel = intent.getParcelableExtra(Intent.EXTRA_INTENT);
79 if (parcel instanceof Intent) {
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070080 mBaseIntent = (Intent) parcel;
Jeff Sharkey3f218e82017-06-12 17:33:58 -060081 mBaseIntent.setFlags(mBaseIntent.getFlags() & ~(Intent.FLAG_GRANT_READ_URI_PERMISSION
82 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
83 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
84 | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION));
Jeffrey Sharkey173545e2009-03-24 20:43:02 -070085 } else {
86 mBaseIntent = new Intent(Intent.ACTION_MAIN, null);
87 mBaseIntent.addCategory(Intent.CATEGORY_DEFAULT);
88 }
89
90 // Create dialog parameters
91 AlertController.AlertParams params = mAlertParams;
92 params.mOnClickListener = this;
93 params.mOnCancelListener = this;
94
95 // Use custom title if provided, otherwise default window title
96 if (intent.hasExtra(Intent.EXTRA_TITLE)) {
97 params.mTitle = intent.getStringExtra(Intent.EXTRA_TITLE);
98 } else {
99 params.mTitle = getTitle();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800100 }
101
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700102 // Build list adapter of pickable items
103 List<PickAdapter.Item> items = getItems();
104 mAdapter = new PickAdapter(this, items);
105 params.mAdapter = mAdapter;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800106
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700107 setupAlert();
108 }
109
110 /**
111 * Handle clicking of dialog item by passing back
112 * {@link #getIntentForPosition(int)} in {@link #setResult(int, Intent)}.
113 */
114 public void onClick(DialogInterface dialog, int which) {
115 Intent intent = getIntentForPosition(which);
116 setResult(Activity.RESULT_OK, intent);
117 finish();
118 }
119
120 /**
121 * Handle canceled dialog by passing back {@link Activity#RESULT_CANCELED}.
122 */
123 public void onCancel(DialogInterface dialog) {
124 setResult(Activity.RESULT_CANCELED);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125 finish();
126 }
127
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700128 /**
129 * Build the specific {@link Intent} for a given list position. Convenience
130 * method that calls through to {@link PickAdapter.Item#getIntent(Intent)}.
131 */
132 protected Intent getIntentForPosition(int position) {
133 PickAdapter.Item item = (PickAdapter.Item) mAdapter.getItem(position);
134 return item.getIntent(mBaseIntent);
135 }
136
137 /**
138 * Build and return list of items to be shown in dialog. Default
139 * implementation mixes activities matching {@link #mBaseIntent} from
140 * {@link #putIntentItems(Intent, List)} with any injected items from
141 * {@link Intent#EXTRA_SHORTCUT_NAME}. Override this method in subclasses to
142 * change the items shown.
143 */
144 protected List<PickAdapter.Item> getItems() {
145 PackageManager packageManager = getPackageManager();
146 List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();
147
148 // Add any injected pick items
149 final Intent intent = getIntent();
150 ArrayList<String> labels =
151 intent.getStringArrayListExtra(Intent.EXTRA_SHORTCUT_NAME);
152 ArrayList<ShortcutIconResource> icons =
153 intent.getParcelableArrayListExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
154
155 if (labels != null && icons != null && labels.size() == icons.size()) {
156 for (int i = 0; i < labels.size(); i++) {
157 String label = labels.get(i);
158 Drawable icon = null;
159
160 try {
161 // Try loading icon from requested package
162 ShortcutIconResource iconResource = icons.get(i);
163 Resources res = packageManager.getResourcesForApplication(
164 iconResource.packageName);
165 icon = res.getDrawable(res.getIdentifier(
Alan Viverette0ba89bd2014-10-10 10:58:58 -0700166 iconResource.resourceName, null, null), null);
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700167 } catch (NameNotFoundException e) {
Romain Guy267a47e2009-12-09 12:17:33 -0800168 // Ignore
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700169 }
170
171 items.add(new PickAdapter.Item(this, label, icon));
172 }
173 }
174
175 // Add any intent items if base was given
176 if (mBaseIntent != null) {
177 putIntentItems(mBaseIntent, items);
178 }
179
180 return items;
181 }
182
183 /**
184 * Fill the given list with any activities matching the base {@link Intent}.
185 */
186 protected void putIntentItems(Intent baseIntent, List<PickAdapter.Item> items) {
187 PackageManager packageManager = getPackageManager();
188 List<ResolveInfo> list = packageManager.queryIntentActivities(baseIntent,
189 0 /* no flags */);
190 Collections.sort(list, new ResolveInfo.DisplayNameComparator(packageManager));
191
192 final int listSize = list.size();
193 for (int i = 0; i < listSize; i++) {
194 ResolveInfo resolveInfo = list.get(i);
195 items.add(new PickAdapter.Item(this, packageManager, resolveInfo));
196 }
197 }
198
199 /**
200 * Adapter which shows the set of activities that can be performed for a
201 * given {@link Intent}.
202 */
203 protected static class PickAdapter extends BaseAdapter {
204
205 /**
206 * Item that appears in a {@link PickAdapter} list.
207 */
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700208 public static class Item implements AppWidgetLoader.LabelledItem {
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700209 protected static IconResizer sResizer;
210
211 protected IconResizer getResizer(Context context) {
212 if (sResizer == null) {
Romain Guy267a47e2009-12-09 12:17:33 -0800213 final Resources resources = context.getResources();
214 int size = (int) resources.getDimension(android.R.dimen.app_icon_size);
215 sResizer = new IconResizer(size, size, resources.getDisplayMetrics());
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700216 }
217 return sResizer;
218 }
219
220 CharSequence label;
221 Drawable icon;
222 String packageName;
223 String className;
224 Bundle extras;
225
226 /**
227 * Create a list item from given label and icon.
228 */
229 Item(Context context, CharSequence label, Drawable icon) {
230 this.label = label;
231 this.icon = getResizer(context).createIconThumbnail(icon);
232 }
233
234 /**
235 * Create a list item and fill it with details from the given
236 * {@link ResolveInfo} object.
237 */
238 Item(Context context, PackageManager pm, ResolveInfo resolveInfo) {
239 label = resolveInfo.loadLabel(pm);
240 if (label == null && resolveInfo.activityInfo != null) {
241 label = resolveInfo.activityInfo.name;
242 }
Romain Guy267a47e2009-12-09 12:17:33 -0800243
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700244 icon = getResizer(context).createIconThumbnail(resolveInfo.loadIcon(pm));
245 packageName = resolveInfo.activityInfo.applicationInfo.packageName;
246 className = resolveInfo.activityInfo.name;
247 }
Romain Guy267a47e2009-12-09 12:17:33 -0800248
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700249 /**
250 * Build the {@link Intent} described by this item. If this item
Romain Guy267a47e2009-12-09 12:17:33 -0800251 * can't create a valid {@link android.content.ComponentName}, it will return
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700252 * {@link Intent#ACTION_CREATE_SHORTCUT} filled with the item label.
253 */
254 Intent getIntent(Intent baseIntent) {
255 Intent intent = new Intent(baseIntent);
256 if (packageName != null && className != null) {
257 // Valid package and class, so fill details as normal intent
258 intent.setClassName(packageName, className);
259 if (extras != null) {
260 intent.putExtras(extras);
261 }
262 } else {
263 // No valid package or class, so treat as shortcut with label
264 intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
265 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
266 }
267 return intent;
268 }
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700269
270 public CharSequence getLabel() {
271 return label;
272 }
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700273 }
274
275 private final LayoutInflater mInflater;
Romain Guy267a47e2009-12-09 12:17:33 -0800276 private final List<Item> mItems;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700277
278 /**
279 * Create an adapter for the given items.
280 */
281 public PickAdapter(Context context, List<Item> items) {
Romain Guy267a47e2009-12-09 12:17:33 -0800282 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700283 mItems = items;
284 }
285
286 /**
287 * {@inheritDoc}
288 */
289 public int getCount() {
290 return mItems.size();
291 }
292
293 /**
294 * {@inheritDoc}
295 */
296 public Object getItem(int position) {
297 return mItems.get(position);
298 }
299
300 /**
301 * {@inheritDoc}
302 */
303 public long getItemId(int position) {
304 return position;
305 }
306
307 /**
308 * {@inheritDoc}
309 */
310 public View getView(int position, View convertView, ViewGroup parent) {
311 if (convertView == null) {
Romain Guy267a47e2009-12-09 12:17:33 -0800312 convertView = mInflater.inflate(R.layout.pick_item, parent, false);
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700313 }
314
315 Item item = (Item) getItem(position);
316 TextView textView = (TextView) convertView;
317 textView.setText(item.label);
318 textView.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null);
319
320 return convertView;
321 }
322 }
323
324 /**
325 * Utility class to resize icons to match default icon size. Code is mostly
326 * borrowed from Launcher.
327 */
328 private static class IconResizer {
Romain Guy267a47e2009-12-09 12:17:33 -0800329 private final int mIconWidth;
330 private final int mIconHeight;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700331
Romain Guy267a47e2009-12-09 12:17:33 -0800332 private final DisplayMetrics mMetrics;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700333 private final Rect mOldBounds = new Rect();
Romain Guy267a47e2009-12-09 12:17:33 -0800334 private final Canvas mCanvas = new Canvas();
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700335
Romain Guy267a47e2009-12-09 12:17:33 -0800336 public IconResizer(int width, int height, DisplayMetrics metrics) {
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700337 mCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
338 Paint.FILTER_BITMAP_FLAG));
Romain Guy267a47e2009-12-09 12:17:33 -0800339
340 mMetrics = metrics;
341 mIconWidth = width;
342 mIconHeight = height;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700343 }
344
345 /**
346 * Returns a Drawable representing the thumbnail of the specified Drawable.
347 * The size of the thumbnail is defined by the dimension
348 * android.R.dimen.launcher_application_icon_size.
349 *
350 * This method is not thread-safe and should be invoked on the UI thread only.
351 *
352 * @param icon The icon to get a thumbnail of.
353 *
354 * @return A thumbnail for the specified icon or the icon itself if the
355 * thumbnail could not be created.
356 */
357 public Drawable createIconThumbnail(Drawable icon) {
358 int width = mIconWidth;
359 int height = mIconHeight;
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700360
Romain Guy3418cb42009-12-14 14:22:37 -0800361 if (icon == null) {
362 return new EmptyDrawable(width, height);
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700363 }
Romain Guy3418cb42009-12-14 14:22:37 -0800364
365 try {
366 if (icon instanceof PaintDrawable) {
367 PaintDrawable painter = (PaintDrawable) icon;
368 painter.setIntrinsicWidth(width);
369 painter.setIntrinsicHeight(height);
370 } else if (icon instanceof BitmapDrawable) {
371 // Ensure the bitmap has a density.
372 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
373 Bitmap bitmap = bitmapDrawable.getBitmap();
374 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
375 bitmapDrawable.setTargetDensity(mMetrics);
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700376 }
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700377 }
Romain Guy3418cb42009-12-14 14:22:37 -0800378 int iconWidth = icon.getIntrinsicWidth();
379 int iconHeight = icon.getIntrinsicHeight();
380
381 if (iconWidth > 0 && iconHeight > 0) {
382 if (width < iconWidth || height < iconHeight) {
383 final float ratio = (float) iconWidth / iconHeight;
384
385 if (iconWidth > iconHeight) {
386 height = (int) (width / ratio);
387 } else if (iconHeight > iconWidth) {
388 width = (int) (height * ratio);
389 }
390
391 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
392 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
393 final Bitmap thumb = Bitmap.createBitmap(mIconWidth, mIconHeight, c);
394 final Canvas canvas = mCanvas;
395 canvas.setBitmap(thumb);
396 // Copy the old bounds to restore them later
397 // If we were to do oldBounds = icon.getBounds(),
398 // the call to setBounds() that follows would
399 // change the same instance and we would lose the
400 // old bounds
401 mOldBounds.set(icon.getBounds());
402 final int x = (mIconWidth - width) / 2;
403 final int y = (mIconHeight - height) / 2;
404 icon.setBounds(x, y, x + width, y + height);
405 icon.draw(canvas);
406 icon.setBounds(mOldBounds);
407 //noinspection deprecation
408 icon = new BitmapDrawable(thumb);
409 ((BitmapDrawable) icon).setTargetDensity(mMetrics);
Dianne Hackborn419740d2011-08-02 17:01:30 -0700410 canvas.setBitmap(null);
Romain Guy3418cb42009-12-14 14:22:37 -0800411 } else if (iconWidth < width && iconHeight < height) {
412 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
413 final Bitmap thumb = Bitmap.createBitmap(mIconWidth, mIconHeight, c);
414 final Canvas canvas = mCanvas;
415 canvas.setBitmap(thumb);
416 mOldBounds.set(icon.getBounds());
417 final int x = (width - iconWidth) / 2;
418 final int y = (height - iconHeight) / 2;
419 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
420 icon.draw(canvas);
421 icon.setBounds(mOldBounds);
422 //noinspection deprecation
423 icon = new BitmapDrawable(thumb);
424 ((BitmapDrawable) icon).setTargetDensity(mMetrics);
Dianne Hackborn419740d2011-08-02 17:01:30 -0700425 canvas.setBitmap(null);
Romain Guy3418cb42009-12-14 14:22:37 -0800426 }
427 }
428
429 } catch (Throwable t) {
430 icon = new EmptyDrawable(width, height);
Jeffrey Sharkey173545e2009-03-24 20:43:02 -0700431 }
432
433 return icon;
434 }
435 }
Romain Guy3418cb42009-12-14 14:22:37 -0800436
437 private static class EmptyDrawable extends Drawable {
438 private final int mWidth;
439 private final int mHeight;
440
441 EmptyDrawable(int width, int height) {
442 mWidth = width;
443 mHeight = height;
444 }
445
446 @Override
447 public int getIntrinsicWidth() {
448 return mWidth;
449 }
450
451 @Override
452 public int getIntrinsicHeight() {
453 return mHeight;
454 }
455
456 @Override
457 public int getMinimumWidth() {
458 return mWidth;
459 }
460
461 @Override
462 public int getMinimumHeight() {
463 return mHeight;
464 }
465
466 @Override
467 public void draw(Canvas canvas) {
468 }
469
470 @Override
471 public void setAlpha(int alpha) {
472 }
473
474 @Override
475 public void setColorFilter(ColorFilter cf) {
476 }
477
478 @Override
479 public int getOpacity() {
480 return PixelFormat.TRANSLUCENT;
481 }
Michael Jurka0b2bd8d2012-10-30 15:32:27 -0700482 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800483}