| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.content.Context; |
| 20 |
import android.content.Intent; |
| 21 |
import android.content.pm.ComponentInfo; |
| 22 |
import android.content.pm.PackageManager; |
| 23 |
import android.content.pm.ResolveInfo; |
| 24 |
import android.content.res.Resources; |
| 25 |
import android.graphics.Bitmap; |
| 26 |
import android.graphics.Canvas; |
| 27 |
import android.graphics.Paint; |
| 28 |
import android.graphics.PaintFlagsDrawFilter; |
| 29 |
import android.graphics.PixelFormat; |
| 30 |
import android.graphics.Rect; |
| 31 |
import android.graphics.drawable.BitmapDrawable; |
| 32 |
import android.graphics.drawable.Drawable; |
| 33 |
import android.graphics.drawable.PaintDrawable; |
| 34 |
import android.os.Bundle; |
| 35 |
import android.view.LayoutInflater; |
| 36 |
import android.view.View; |
| 37 |
import android.view.ViewGroup; |
| 38 |
import android.view.Window; |
| 39 |
import android.view.View.OnClickListener; |
| 40 |
import android.widget.BaseAdapter; |
| 41 |
import android.widget.Button; |
| 42 |
import android.widget.Filter; |
| 43 |
import android.widget.Filterable; |
| 44 |
import android.widget.ListView; |
| 45 |
import android.widget.TextView; |
| 46 |
|
| 47 |
import java.util.ArrayList; |
| 48 |
import java.util.Collections; |
| 49 |
import java.util.List; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
@Deprecated |
| 61 |
public abstract class LauncherActivity extends ListActivity { |
| 62 |
Intent mIntent; |
| 63 |
PackageManager mPackageManager; |
| 64 |
IconResizer mIconResizer; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static class ListItem { |
| 70 |
public ResolveInfo resolveInfo; |
| 71 |
public CharSequence label; |
| 72 |
public Drawable icon; |
| 73 |
public String packageName; |
| 74 |
public String className; |
| 75 |
public Bundle extras; |
| 76 |
|
| 77 |
ListItem(PackageManager pm, ResolveInfo resolveInfo, IconResizer resizer) { |
| 78 |
this.resolveInfo = resolveInfo; |
| 79 |
label = resolveInfo.loadLabel(pm); |
| 80 |
ComponentInfo ci = resolveInfo.activityInfo; |
| 81 |
if (ci == null) ci = resolveInfo.serviceInfo; |
| 82 |
if (label == null && ci != null) { |
| 83 |
label = resolveInfo.activityInfo.name; |
| 84 |
} |
| 85 |
|
| 86 |
if (resizer != null) { |
| 87 |
icon = resizer.createIconThumbnail(resolveInfo.loadIcon(pm)); |
| 88 |
} |
| 89 |
packageName = ci.applicationInfo.packageName; |
| 90 |
className = ci.name; |
| 91 |
} |
| 92 |
|
| 93 |
public ListItem() { |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private class ActivityAdapter extends BaseAdapter implements Filterable { |
| 101 |
private final Object lock = new Object(); |
| 102 |
private ArrayList<ListItem> mOriginalValues; |
| 103 |
|
| 104 |
protected final IconResizer mIconResizer; |
| 105 |
protected final LayoutInflater mInflater; |
| 106 |
|
| 107 |
protected List<ListItem> mActivitiesList; |
| 108 |
|
| 109 |
private Filter mFilter; |
| 110 |
private final boolean mShowIcons; |
| 111 |
|
| 112 |
public ActivityAdapter(IconResizer resizer) { |
| 113 |
mIconResizer = resizer; |
| 114 |
mInflater = (LayoutInflater) LauncherActivity.this.getSystemService( |
| 115 |
Context.LAYOUT_INFLATER_SERVICE); |
| 116 |
mShowIcons = onEvaluateShowIcons(); |
| 117 |
mActivitiesList = makeListItems(); |
| 118 |
} |
| 119 |
|
| 120 |
public Intent intentForPosition(int position) { |
| 121 |
if (mActivitiesList == null) { |
| 122 |
return null; |
| 123 |
} |
| 124 |
|
| 125 |
Intent intent = new Intent(mIntent); |
| 126 |
ListItem item = mActivitiesList.get(position); |
| 127 |
intent.setClassName(item.packageName, item.className); |
| 128 |
if (item.extras != null) { |
| 129 |
intent.putExtras(item.extras); |
| 130 |
} |
| 131 |
return intent; |
| 132 |
} |
| 133 |
|
| 134 |
public ListItem itemForPosition(int position) { |
| 135 |
if (mActivitiesList == null) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
return mActivitiesList.get(position); |
| 140 |
} |
| 141 |
|
| 142 |
public int getCount() { |
| 143 |
return mActivitiesList != null ? mActivitiesList.size() : 0; |
| 144 |
} |
| 145 |
|
| 146 |
public Object getItem(int position) { |
| 147 |
return position; |
| 148 |
} |
| 149 |
|
| 150 |
public long getItemId(int position) { |
| 151 |
return position; |
| 152 |
} |
| 153 |
|
| 154 |
public View getView(int position, View convertView, ViewGroup parent) { |
| 155 |
View view; |
| 156 |
if (convertView == null) { |
| 157 |
view = mInflater.inflate( |
| 158 |
com.android.internal.R.layout.activity_list_item_2, parent, false); |
| 159 |
} else { |
| 160 |
view = convertView; |
| 161 |
} |
| 162 |
bindView(view, mActivitiesList.get(position)); |
| 163 |
return view; |
| 164 |
} |
| 165 |
|
| 166 |
private void bindView(View view, ListItem item) { |
| 167 |
TextView text = (TextView) view; |
| 168 |
text.setText(item.label); |
| 169 |
if (mShowIcons) { |
| 170 |
if (item.icon == null) { |
| 171 |
item.icon = mIconResizer.createIconThumbnail(item.resolveInfo.loadIcon(getPackageManager())); |
| 172 |
} |
| 173 |
text.setCompoundDrawablesRelativeWithIntrinsicBounds(item.icon, null, null, null); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
public Filter getFilter() { |
| 178 |
if (mFilter == null) { |
| 179 |
mFilter = new ArrayFilter(); |
| 180 |
} |
| 181 |
return mFilter; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private class ArrayFilter extends Filter { |
| 189 |
@Override |
| 190 |
protected FilterResults performFiltering(CharSequence prefix) { |
| 191 |
FilterResults results = new FilterResults(); |
| 192 |
|
| 193 |
if (mOriginalValues == null) { |
| 194 |
synchronized (lock) { |
| 195 |
mOriginalValues = new ArrayList<ListItem>(mActivitiesList); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if (prefix == null || prefix.length() == 0) { |
| 200 |
synchronized (lock) { |
| 201 |
ArrayList<ListItem> list = new ArrayList<ListItem>(mOriginalValues); |
| 202 |
results.values = list; |
| 203 |
results.count = list.size(); |
| 204 |
} |
| 205 |
} else { |
| 206 |
final String prefixString = prefix.toString().toLowerCase(); |
| 207 |
|
| 208 |
ArrayList<ListItem> values = mOriginalValues; |
| 209 |
int count = values.size(); |
| 210 |
|
| 211 |
ArrayList<ListItem> newValues = new ArrayList<ListItem>(count); |
| 212 |
|
| 213 |
for (int i = 0; i < count; i++) { |
| 214 |
ListItem item = values.get(i); |
| 215 |
|
| 216 |
String[] words = item.label.toString().toLowerCase().split(" "); |
| 217 |
int wordCount = words.length; |
| 218 |
|
| 219 |
for (int k = 0; k < wordCount; k++) { |
| 220 |
final String word = words[k]; |
| 221 |
|
| 222 |
if (word.startsWith(prefixString)) { |
| 223 |
newValues.add(item); |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
results.values = newValues; |
| 230 |
results.count = newValues.size(); |
| 231 |
} |
| 232 |
|
| 233 |
return results; |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
protected void publishResults(CharSequence constraint, FilterResults results) { |
| 238 |
|
| 239 |
mActivitiesList = (List<ListItem>) results.values; |
| 240 |
if (results.count > 0) { |
| 241 |
notifyDataSetChanged(); |
| 242 |
} else { |
| 243 |
notifyDataSetInvalidated(); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public class IconResizer { |
| 253 |
|
| 254 |
private int mIconWidth = -1; |
| 255 |
private int mIconHeight = -1; |
| 256 |
|
| 257 |
private final Rect mOldBounds = new Rect(); |
| 258 |
private Canvas mCanvas = new Canvas(); |
| 259 |
|
| 260 |
public IconResizer() { |
| 261 |
mCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 262 |
Paint.FILTER_BITMAP_FLAG)); |
| 263 |
|
| 264 |
final Resources resources = LauncherActivity.this.getResources(); |
| 265 |
mIconWidth = mIconHeight = (int) resources.getDimension( |
| 266 |
android.R.dimen.app_icon_size); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public Drawable createIconThumbnail(Drawable icon) { |
| 282 |
int width = mIconWidth; |
| 283 |
int height = mIconHeight; |
| 284 |
|
| 285 |
final int iconWidth = icon.getIntrinsicWidth(); |
| 286 |
final int iconHeight = icon.getIntrinsicHeight(); |
| 287 |
|
| 288 |
if (icon instanceof PaintDrawable) { |
| 289 |
PaintDrawable painter = (PaintDrawable) icon; |
| 290 |
painter.setIntrinsicWidth(width); |
| 291 |
painter.setIntrinsicHeight(height); |
| 292 |
} |
| 293 |
|
| 294 |
if (width > 0 && height > 0) { |
| 295 |
if (width < iconWidth || height < iconHeight) { |
| 296 |
final float ratio = (float) iconWidth / iconHeight; |
| 297 |
|
| 298 |
if (iconWidth > iconHeight) { |
| 299 |
height = (int) (width / ratio); |
| 300 |
} else if (iconHeight > iconWidth) { |
| 301 |
width = (int) (height * ratio); |
| 302 |
} |
| 303 |
|
| 304 |
final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? |
| 305 |
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; |
| 306 |
final Bitmap thumb = Bitmap.createBitmap(mIconWidth, mIconHeight, c); |
| 307 |
final Canvas canvas = mCanvas; |
| 308 |
canvas.setBitmap(thumb); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
mOldBounds.set(icon.getBounds()); |
| 315 |
final int x = (mIconWidth - width) / 2; |
| 316 |
final int y = (mIconHeight - height) / 2; |
| 317 |
icon.setBounds(x, y, x + width, y + height); |
| 318 |
icon.draw(canvas); |
| 319 |
icon.setBounds(mOldBounds); |
| 320 |
icon = new BitmapDrawable(getResources(), thumb); |
| 321 |
canvas.setBitmap(null); |
| 322 |
} else if (iconWidth < width && iconHeight < height) { |
| 323 |
final Bitmap.Config c = Bitmap.Config.ARGB_8888; |
| 324 |
final Bitmap thumb = Bitmap.createBitmap(mIconWidth, mIconHeight, c); |
| 325 |
final Canvas canvas = mCanvas; |
| 326 |
canvas.setBitmap(thumb); |
| 327 |
mOldBounds.set(icon.getBounds()); |
| 328 |
final int x = (width - iconWidth) / 2; |
| 329 |
final int y = (height - iconHeight) / 2; |
| 330 |
icon.setBounds(x, y, x + iconWidth, y + iconHeight); |
| 331 |
icon.draw(canvas); |
| 332 |
icon.setBounds(mOldBounds); |
| 333 |
icon = new BitmapDrawable(getResources(), thumb); |
| 334 |
canvas.setBitmap(null); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
return icon; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
protected void onCreate(Bundle icicle) { |
| 344 |
super.onCreate(icicle); |
| 345 |
|
| 346 |
mPackageManager = getPackageManager(); |
| 347 |
|
| 348 |
if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH)) { |
| 349 |
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); |
| 350 |
setProgressBarIndeterminateVisibility(true); |
| 351 |
} |
| 352 |
onSetContentView(); |
| 353 |
|
| 354 |
mIconResizer = new IconResizer(); |
| 355 |
|
| 356 |
mIntent = new Intent(getTargetIntent()); |
| 357 |
mIntent.setComponent(null); |
| 358 |
mAdapter = new ActivityAdapter(mIconResizer); |
| 359 |
|
| 360 |
setListAdapter(mAdapter); |
| 361 |
getListView().setTextFilterEnabled(true); |
| 362 |
|
| 363 |
updateAlertTitle(); |
| 364 |
updateButtonText(); |
| 365 |
|
| 366 |
if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH)) { |
| 367 |
setProgressBarIndeterminateVisibility(false); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
private void updateAlertTitle() { |
| 372 |
TextView alertTitle = (TextView) findViewById(com.android.internal.R.id.alertTitle); |
| 373 |
if (alertTitle != null) { |
| 374 |
alertTitle.setText(getTitle()); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
private void updateButtonText() { |
| 379 |
Button cancelButton = (Button) findViewById(com.android.internal.R.id.button1); |
| 380 |
if (cancelButton != null) { |
| 381 |
cancelButton.setOnClickListener(new OnClickListener() { |
| 382 |
public void onClick(View v) { |
| 383 |
finish(); |
| 384 |
} |
| 385 |
}); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
@Override |
| 390 |
public void setTitle(CharSequence title) { |
| 391 |
super.setTitle(title); |
| 392 |
updateAlertTitle(); |
| 393 |
} |
| 394 |
|
| 395 |
@Override |
| 396 |
public void setTitle(int titleId) { |
| 397 |
super.setTitle(titleId); |
| 398 |
updateAlertTitle(); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
protected void onSetContentView() { |
| 406 |
setContentView(com.android.internal.R.layout.activity_list); |
| 407 |
} |
| 408 |
|
| 409 |
@Override |
| 410 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
| 411 |
Intent intent = intentForPosition(position); |
| 412 |
startActivity(intent); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
protected Intent intentForPosition(int position) { |
| 421 |
ActivityAdapter adapter = (ActivityAdapter) mAdapter; |
| 422 |
return adapter.intentForPosition(position); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
protected ListItem itemForPosition(int position) { |
| 431 |
ActivityAdapter adapter = (ActivityAdapter) mAdapter; |
| 432 |
return adapter.itemForPosition(position); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
protected Intent getTargetIntent() { |
| 440 |
return new Intent(); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
protected List<ResolveInfo> onQueryPackageManager(Intent queryIntent) { |
| 448 |
return mPackageManager.queryIntentActivities(queryIntent, 0); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
protected void onSortResultList(List<ResolveInfo> results) { |
| 455 |
Collections.sort(results, new ResolveInfo.DisplayNameComparator(mPackageManager)); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public List<ListItem> makeListItems() { |
| 462 |
|
| 463 |
List<ResolveInfo> list = onQueryPackageManager(mIntent); |
| 464 |
onSortResultList(list); |
| 465 |
|
| 466 |
ArrayList<ListItem> result = new ArrayList<ListItem>(list.size()); |
| 467 |
int listSize = list.size(); |
| 468 |
for (int i = 0; i < listSize; i++) { |
| 469 |
ResolveInfo resolveInfo = list.get(i); |
| 470 |
result.add(new ListItem(mPackageManager, resolveInfo, null)); |
| 471 |
} |
| 472 |
|
| 473 |
return result; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
protected boolean onEvaluateShowIcons() { |
| 482 |
return true; |
| 483 |
} |
| 484 |
} |