Automated import from //branches/master/...@141652,141652

This commit is contained in:
Jeffrey Sharkey
2009-03-24 20:43:02 -07:00
committed by The Android Open Source Project
parent 18f3afb9cd
commit 173545e63b
4 changed files with 510 additions and 105 deletions

View File

@@ -375,7 +375,7 @@
<activity android:name="ActivityPicker"
android:label="@string/activity_picker_label"
android:theme="@android:style/Theme.Light">
android:theme="@*android:style/Theme.Dialog.Alert">
<intent-filter>
<action android:name="android.intent.action.PICK_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
@@ -478,7 +478,7 @@
<!-- Standard picker for widgets -->
<activity android:name="AppWidgetPickActivity"
android:label="@string/widget_picker_title"
android:theme="@android:style/Theme.Light">
android:theme="@*android:style/Theme.Dialog.Alert">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_PICK" />
<category android:name="android.intent.category.DEFAULT" />

25
res/layout/pick_item.xml Executable file
View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLargeInverse"
android:gravity="center_vertical"
android:drawablePadding="14dip"
android:paddingLeft="15dip"
android:paddingRight="15dip" />

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009 Google Inc.
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,43 +16,395 @@
package com.android.settings;
import android.app.LauncherActivity;
import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Displays a list of all activities matching the incoming {@link Intent.EXTRA_INTENT}
* query, along with any applicable icons.
* Displays a list of all activities matching the incoming
* {@link Intent#EXTRA_INTENT} query, along with any injected items.
*/
public class ActivityPicker extends LauncherActivity {
public class ActivityPicker extends AlertActivity implements
DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
/**
* Adapter of items that are displayed in this dialog.
*/
private PickAdapter mAdapter;
/**
* Base {@link Intent} used when building list.
*/
private Intent mBaseIntent;
@Override
protected Intent getTargetIntent() {
Intent intent = this.getIntent();
Intent targetIntent = new Intent(Intent.ACTION_MAIN, null);
targetIntent.addCategory(Intent.CATEGORY_DEFAULT);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use a custom title for this dialog, if provided
if (intent.hasExtra(Intent.EXTRA_TITLE)) {
String title = intent.getStringExtra(Intent.EXTRA_TITLE);
setTitle(title);
}
final Intent intent = getIntent();
// Read base intent from extras, otherwise assume default
Parcelable parcel = intent.getParcelableExtra(Intent.EXTRA_INTENT);
if (parcel instanceof Intent) {
targetIntent = (Intent) parcel;
mBaseIntent = (Intent) parcel;
} else {
mBaseIntent = new Intent(Intent.ACTION_MAIN, null);
mBaseIntent.addCategory(Intent.CATEGORY_DEFAULT);
}
return targetIntent;
// 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();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = intentForPosition(position);
setResult(RESULT_OK, intent);
// Build list adapter of pickable items
List<PickAdapter.Item> items = getItems();
mAdapter = new PickAdapter(this, items);
params.mAdapter = mAdapter;
setupAlert();
}
/**
* Handle clicking of dialog item by passing back
* {@link #getIntentForPosition(int)} in {@link #setResult(int, Intent)}.
*/
public void onClick(DialogInterface dialog, int which) {
Intent intent = getIntentForPosition(which);
setResult(Activity.RESULT_OK, intent);
finish();
}
/**
* Handle canceled dialog by passing back {@link Activity#RESULT_CANCELED}.
*/
public void onCancel(DialogInterface dialog) {
setResult(Activity.RESULT_CANCELED);
finish();
}
/**
* Build the specific {@link Intent} for a given list position. Convenience
* method that calls through to {@link PickAdapter.Item#getIntent(Intent)}.
*/
protected Intent getIntentForPosition(int position) {
PickAdapter.Item item = (PickAdapter.Item) mAdapter.getItem(position);
return item.getIntent(mBaseIntent);
}
/**
* 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.
*/
protected List<PickAdapter.Item> getItems() {
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;
}
/**
* Fill the given list with any activities matching the base {@link Intent}.
*/
protected void putIntentItems(Intent baseIntent, List<PickAdapter.Item> items) {
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));
}
}
/**
* Adapter which shows the set of activities that can be performed for a
* given {@link Intent}.
*/
protected static class PickAdapter extends BaseAdapter {
/**
* Item that appears in a {@link PickAdapter} list.
*/
public static class Item {
protected static IconResizer sResizer;
protected IconResizer getResizer(Context context) {
if (sResizer == null) {
sResizer = new IconResizer(context);
}
return sResizer;
}
CharSequence label;
Drawable icon;
String packageName;
String className;
Bundle extras;
/**
* Create a list item from given label and icon.
*/
Item(Context context, CharSequence label, Drawable icon) {
this.label = label;
this.icon = getResizer(context).createIconThumbnail(icon);
}
/**
* Create a list item and fill it with details from the given
* {@link ResolveInfo} object.
*/
Item(Context context, PackageManager pm, ResolveInfo resolveInfo) {
label = resolveInfo.loadLabel(pm);
if (label == null && resolveInfo.activityInfo != null) {
label = resolveInfo.activityInfo.name;
}
icon = getResizer(context).createIconThumbnail(resolveInfo.loadIcon(pm));
packageName = resolveInfo.activityInfo.applicationInfo.packageName;
className = resolveInfo.activityInfo.name;
}
/**
* Build the {@link Intent} described by this item. If this item
* can't create a valid {@link ComponentName}, it will return
* {@link Intent#ACTION_CREATE_SHORTCUT} filled with the item label.
*/
Intent getIntent(Intent baseIntent) {
Intent intent = new Intent(baseIntent);
if (packageName != null && className != null) {
// Valid package and class, so fill details as normal intent
intent.setClassName(packageName, className);
if (extras != null) {
intent.putExtras(extras);
}
} else {
// No valid package or class, so treat as shortcut with label
intent.setAction(Intent.ACTION_CREATE_SHORTCUT);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
}
return intent;
}
}
private final LayoutInflater mInflater;
private List<Item> mItems;
private int mLayoutRes = R.layout.pick_item;
/**
* Create an adapter for the given items.
*/
public PickAdapter(Context context, List<Item> items) {
mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mItems = items;
}
/**
* {@inheritDoc}
*/
public int getCount() {
return mItems.size();
}
/**
* {@inheritDoc}
*/
public Object getItem(int position) {
return mItems.get(position);
}
/**
* {@inheritDoc}
*/
public long getItemId(int position) {
return position;
}
/**
* {@inheritDoc}
*/
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(mLayoutRes, parent, false);
}
Item item = (Item) getItem(position);
TextView textView = (TextView) convertView;
textView.setText(item.label);
textView.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null);
return convertView;
}
}
/**
* Utility class to resize icons to match default icon size. Code is mostly
* borrowed from Launcher.
*/
private static class IconResizer {
private int mIconWidth = -1;
private int mIconHeight = -1;
private final Rect mOldBounds = new Rect();
private Canvas mCanvas = new Canvas();
public IconResizer(Context context) {
mCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
Paint.FILTER_BITMAP_FLAG));
final Resources resources = context.getResources();
mIconWidth = mIconHeight = (int) resources.getDimension(
android.R.dimen.app_icon_size);
}
/**
* Returns a Drawable representing the thumbnail of the specified Drawable.
* The size of the thumbnail is defined by the dimension
* android.R.dimen.launcher_application_icon_size.
*
* This method is not thread-safe and should be invoked on the UI thread only.
*
* @param icon The icon to get a thumbnail of.
*
* @return A thumbnail for the specified icon or the icon itself if the
* thumbnail could not be created.
*/
public Drawable createIconThumbnail(Drawable icon) {
int width = mIconWidth;
int height = mIconHeight;
if (icon == null) {
return null;
}
final int iconWidth = icon.getIntrinsicWidth();
final int iconHeight = icon.getIntrinsicHeight();
if (icon instanceof PaintDrawable) {
PaintDrawable painter = (PaintDrawable) icon;
painter.setIntrinsicWidth(width);
painter.setIntrinsicHeight(height);
}
if (width > 0 && height > 0) {
if (width < iconWidth || height < iconHeight) {
final float ratio = (float) iconWidth / iconHeight;
if (iconWidth > iconHeight) {
height = (int) (width / ratio);
} else if (iconHeight > iconWidth) {
width = (int) (height * ratio);
}
final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
final Bitmap thumb = Bitmap.createBitmap(mIconWidth, mIconHeight, c);
final Canvas canvas = mCanvas;
canvas.setBitmap(thumb);
// Copy the old bounds to restore them later
// If we were to do oldBounds = icon.getBounds(),
// the call to setBounds() that follows would
// change the same instance and we would lose the
// old bounds
mOldBounds.set(icon.getBounds());
final int x = (mIconWidth - width) / 2;
final int y = (mIconHeight - height) / 2;
icon.setBounds(x, y, x + width, y + height);
icon.draw(canvas);
icon.setBounds(mOldBounds);
icon = new BitmapDrawable(thumb);
} else if (iconWidth < width && iconHeight < height) {
final Bitmap.Config c = Bitmap.Config.ARGB_8888;
final Bitmap thumb = Bitmap.createBitmap(mIconWidth, mIconHeight, c);
final Canvas canvas = mCanvas;
canvas.setBitmap(thumb);
mOldBounds.set(icon.getBounds());
final int x = (width - iconWidth) / 2;
final int y = (height - iconHeight) / 2;
icon.setBounds(x, y, x + iconWidth, y + iconHeight);
icon.draw(canvas);
icon.setBounds(mOldBounds);
icon = new BitmapDrawable(thumb);
}
}
return icon;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2008 The Android Open Source Project
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,17 +16,14 @@
package com.android.settings;
import android.app.LauncherActivity;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ListView;
import android.util.Log;
import java.text.Collator;
@@ -35,33 +32,59 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class AppWidgetPickActivity extends LauncherActivity
{
/**
* Displays a list of {@link AppWidgetProviderInfo} widgets, along with any
* injected special widgets specified through
* {@link AppWidgetManager#EXTRA_CUSTOM_INFO} and
* {@link AppWidgetManager#EXTRA_CUSTOM_EXTRAS}.
* <p>
* When an installed {@link AppWidgetProviderInfo} is selected, this activity
* will bind it to the given {@link AppWidgetManager#EXTRA_APPWIDGET_ID},
* otherwise it will return the requested extras.
*/
public class AppWidgetPickActivity extends ActivityPicker {
private static final String TAG = "AppWidgetPickActivity";
private static final boolean LOGD = false;
AppWidgetManager mAppWidgetManager;
int mAppWidgetId;
ArrayList mCustomInfo;
ArrayList mCustomExtras;
Drawable mDefaultIcon = null;
private PackageManager mPackageManager;
private AppWidgetManager mAppWidgetManager;
public AppWidgetPickActivity() {
mAppWidgetManager = AppWidgetManager.getInstance(this);
}
/**
* The allocated {@link AppWidgetManager#EXTRA_APPWIDGET_ID} that this
* activity is binding.
*/
private int mAppWidgetId;
@Override
public void onCreate(Bundle icicle) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
mPackageManager = getPackageManager();
mAppWidgetManager = AppWidgetManager.getInstance(this);
super.onCreate(icicle);
// Set default return data
setResultData(RESULT_CANCELED, null);
// Read the appWidgetId passed our direction, otherwise bail if not found
final Intent intent = getIntent();
if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
} else {
finish();
}
}
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
/**
* Create list entries for any custom widgets requested through
* {@link AppWidgetManager#EXTRA_CUSTOM_INFO}.
*/
void putCustomAppWidgets(List<PickAdapter.Item> items) {
final Bundle extras = getIntent().getExtras();
// get and validate the extras they gave us
ArrayList<Parcelable> customInfo = null;
ArrayList<AppWidgetProviderInfo> customExtras = null;
ArrayList<AppWidgetProviderInfo> customInfo = null;
ArrayList<Bundle> customExtras = null;
try_custom_items: {
customInfo = extras.getParcelableArrayList(AppWidgetManager.EXTRA_CUSTOM_INFO);
if (customInfo == null || customInfo.size() == 0) {
@@ -103,21 +126,19 @@ public class AppWidgetPickActivity extends LauncherActivity
break try_custom_items;
}
}
mCustomInfo = customInfo;
mCustomExtras = customExtras;
}
// After the stuff with mCustomInfo
super.onCreate(icicle);
setResultData(RESULT_CANCELED, null);
if (LOGD) Log.d(TAG, "Using " + customInfo.size() + " custom items");
putAppWidgetItems(customInfo, customExtras, items);
}
/**
* {@inheritDoc}
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
Intent intent = intentForPosition(position);
public void onClick(DialogInterface dialog, int which) {
Intent intent = getIntentForPosition(which);
int result;
if (intent.getExtras() != null) {
// If there are any extras, it's because this entry is custom.
@@ -140,72 +161,79 @@ public class AppWidgetPickActivity extends LauncherActivity
finish();
}
void makeItems(List<AppWidgetProviderInfo> items, ArrayList<Bundle> extras,
ArrayList<ListItem> result, IconResizer resizer, PackageManager pm) {
final int N = items.size();
for (int i=0; i<N; i++) {
AppWidgetProviderInfo info = items.get(i);
/**
* Create list entries for the given {@link AppWidgetProviderInfo} widgets,
* inserting extras if provided.
*/
void putAppWidgetItems(List<AppWidgetProviderInfo> appWidgets,
List<Bundle> customExtras, List<PickAdapter.Item> items) {
final int size = appWidgets.size();
for (int i = 0; i < size; i++) {
AppWidgetProviderInfo info = appWidgets.get(i);
CharSequence label = info.label;
Drawable icon = null;
if (info.icon != 0) {
icon = mPackageManager.getDrawable(info.provider.getPackageName(), info.icon, null);
if (icon == null) {
Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
+ " for provider: " + info.provider);
}
}
PickAdapter.Item item = new PickAdapter.Item(this, label, icon);
LauncherActivity.ListItem item = new LauncherActivity.ListItem();
item.packageName = info.provider.getPackageName();
item.className = info.provider.getClassName();
if (extras != null) {
item.extras = extras.get(i);
}
item.label = info.label;
if (info.icon != 0) {
Drawable d = pm.getDrawable(item.packageName, info.icon, null);
if (d != null) {
item.icon = resizer.createIconThumbnail(d);
} else {
Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon)
+ " for package: " + item.packageName);
}
}
if (item.icon == null) {
// (including error case above)
if (mDefaultIcon == null) {
// TODO: Load standard icon.
}
item.icon = mDefaultIcon;
if (customExtras != null) {
item.extras = customExtras.get(i);
}
result.add(item);
items.add(item);
}
}
/**
* Build and return list of items to be shown in dialog. This will mix both
* installed {@link AppWidgetProviderInfo} and those provided through
* {@link AppWidgetManager#EXTRA_CUSTOM_INFO}, sorting them alphabetically.
*/
@Override
public List<ListItem> makeListItems() {
List<AppWidgetProviderInfo> installed = mAppWidgetManager.getInstalledProviders();
PackageManager pm = getPackageManager();
protected List<PickAdapter.Item> getItems() {
List<PickAdapter.Item> items = new ArrayList<PickAdapter.Item>();
IconResizer resizer = new IconResizer();
ArrayList<ListItem> result = new ArrayList();
putInstalledAppWidgets(items);
putCustomAppWidgets(items);
// the ones from the package manager
makeItems(installed, null, result, resizer, pm);
// the ones provided in the intent we were launched with
if (mCustomInfo != null) {
Log.d(TAG, "Using " + mCustomInfo.size() + " custom items");
makeItems(mCustomInfo, mCustomExtras, result, resizer, pm);
}
// sort the results by name
Collections.sort(result, new Comparator<ListItem>() {
// Sort all items together by label
Collections.sort(items, new Comparator<PickAdapter.Item>() {
Collator mCollator = Collator.getInstance();
public int compare(ListItem lhs, ListItem rhs) {
public int compare(PickAdapter.Item lhs, PickAdapter.Item rhs) {
return mCollator.compare(lhs.label, rhs.label);
}
});
return result;
return items;
}
/**
* Create list entries for installed {@link AppWidgetProviderInfo} widgets.
*/
void putInstalledAppWidgets(List<PickAdapter.Item> items) {
List<AppWidgetProviderInfo> installed = mAppWidgetManager.getInstalledProviders();
putAppWidgetItems(installed, null, items);
}
/**
* Convenience method for setting the result code and intent. This method
* correctly injects the {@link AppWidgetManager#EXTRA_APPWIDGET_ID} that
* most hosts expect returned.
*/
void setResultData(int code, Intent intent) {
Intent result = intent != null ? intent : new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(code, result);
}
}