ResolverActivity.java revision 2d80962b2e93ae4e59d0197487bd0f6aad4cf26e
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 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.internal.app; 18 19import com.android.internal.R; 20import com.android.internal.content.PackageMonitor; 21 22import android.content.ComponentName; 23import android.content.Context; 24import android.content.DialogInterface; 25import android.content.Intent; 26import android.content.IntentFilter; 27import android.content.pm.ActivityInfo; 28import android.content.pm.LabeledIntent; 29import android.content.pm.PackageManager; 30import android.content.pm.ResolveInfo; 31import android.graphics.drawable.Drawable; 32import android.net.Uri; 33import android.os.Bundle; 34import android.os.PatternMatcher; 35import android.util.Log; 36import android.view.LayoutInflater; 37import android.view.View; 38import android.view.ViewGroup; 39import android.widget.AdapterView; 40import android.widget.BaseAdapter; 41import android.widget.CheckBox; 42import android.widget.CompoundButton; 43import android.widget.ImageView; 44import android.widget.ListView; 45import android.widget.TextView; 46 47import java.util.ArrayList; 48import java.util.Collections; 49import java.util.HashSet; 50import java.util.Iterator; 51import java.util.List; 52import java.util.Set; 53 54/** 55 * This activity is displayed when the system attempts to start an Intent for 56 * which there is more than one matching activity, allowing the user to decide 57 * which to go to. It is not normally used directly by application developers. 58 */ 59public class ResolverActivity extends AlertActivity implements 60 DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener { 61 private ResolveListAdapter mAdapter; 62 private CheckBox mAlwaysCheck; 63 private TextView mClearDefaultHint; 64 private PackageManager mPm; 65 66 private final PackageMonitor mPackageMonitor = new PackageMonitor() { 67 @Override public void onSomePackagesChanged() { 68 mAdapter.handlePackagesChanged(); 69 } 70 }; 71 72 private Intent makeMyIntent() { 73 Intent intent = new Intent(getIntent()); 74 // The resolver activity is set to be hidden from recent tasks. 75 // we don't want this attribute to be propagated to the next activity 76 // being launched. Note that if the original Intent also had this 77 // flag set, we are now losing it. That should be a very rare case 78 // and we can live with this. 79 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 80 return intent; 81 } 82 83 @Override 84 protected void onCreate(Bundle savedInstanceState) { 85 onCreate(savedInstanceState, makeMyIntent(), 86 getResources().getText(com.android.internal.R.string.whichApplication), 87 null, null, true); 88 } 89 90 protected void onCreate(Bundle savedInstanceState, Intent intent, 91 CharSequence title, Intent[] initialIntents, List<ResolveInfo> rList, 92 boolean alwaysUseOption) { 93 super.onCreate(savedInstanceState); 94 mPm = getPackageManager(); 95 intent.setComponent(null); 96 97 AlertController.AlertParams ap = mAlertParams; 98 99 ap.mTitle = title; 100 ap.mOnClickListener = this; 101 102 mPackageMonitor.register(this, false); 103 104 if (alwaysUseOption) { 105 LayoutInflater inflater = (LayoutInflater) getSystemService( 106 Context.LAYOUT_INFLATER_SERVICE); 107 ap.mView = inflater.inflate(R.layout.always_use_checkbox, null); 108 mAlwaysCheck = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); 109 mAlwaysCheck.setText(R.string.alwaysUse); 110 mAlwaysCheck.setOnCheckedChangeListener(this); 111 mClearDefaultHint = (TextView)ap.mView.findViewById( 112 com.android.internal.R.id.clearDefaultHint); 113 mClearDefaultHint.setVisibility(View.GONE); 114 } 115 mAdapter = new ResolveListAdapter(this, intent, initialIntents, rList); 116 int count = mAdapter.getCount(); 117 if (count > 1) { 118 ap.mAdapter = mAdapter; 119 } else if (count == 1) { 120 startActivity(mAdapter.intentForPosition(0)); 121 finish(); 122 return; 123 } else { 124 ap.mMessage = getResources().getText(com.android.internal.R.string.noApplications); 125 } 126 127 setupAlert(); 128 129 ListView lv = mAlert.getListView(); 130 if (lv != null) { 131 lv.setOnItemLongClickListener(new ItemLongClickListener()); 132 } 133 } 134 135 @Override 136 protected void onRestart() { 137 super.onRestart(); 138 mPackageMonitor.register(this, false); 139 mAdapter.handlePackagesChanged(); 140 } 141 142 @Override 143 protected void onStop() { 144 super.onStop(); 145 mPackageMonitor.unregister(); 146 } 147 148 public void onClick(DialogInterface dialog, int which) { 149 ResolveInfo ri = mAdapter.resolveInfoForPosition(which); 150 Intent intent = mAdapter.intentForPosition(which); 151 boolean alwaysCheck = (mAlwaysCheck != null && mAlwaysCheck.isChecked()); 152 onIntentSelected(ri, intent, alwaysCheck); 153 finish(); 154 } 155 156 protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) { 157 if (alwaysCheck) { 158 // Build a reasonable intent filter, based on what matched. 159 IntentFilter filter = new IntentFilter(); 160 161 if (intent.getAction() != null) { 162 filter.addAction(intent.getAction()); 163 } 164 Set<String> categories = intent.getCategories(); 165 if (categories != null) { 166 for (String cat : categories) { 167 filter.addCategory(cat); 168 } 169 } 170 filter.addCategory(Intent.CATEGORY_DEFAULT); 171 172 int cat = ri.match&IntentFilter.MATCH_CATEGORY_MASK; 173 Uri data = intent.getData(); 174 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) { 175 String mimeType = intent.resolveType(this); 176 if (mimeType != null) { 177 try { 178 filter.addDataType(mimeType); 179 } catch (IntentFilter.MalformedMimeTypeException e) { 180 Log.w("ResolverActivity", e); 181 filter = null; 182 } 183 } 184 } 185 if (data != null && data.getScheme() != null) { 186 // We need the data specification if there was no type, 187 // OR if the scheme is not one of our magical "file:" 188 // or "content:" schemes (see IntentFilter for the reason). 189 if (cat != IntentFilter.MATCH_CATEGORY_TYPE 190 || (!"file".equals(data.getScheme()) 191 && !"content".equals(data.getScheme()))) { 192 filter.addDataScheme(data.getScheme()); 193 194 // Look through the resolved filter to determine which part 195 // of it matched the original Intent. 196 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator(); 197 if (aIt != null) { 198 while (aIt.hasNext()) { 199 IntentFilter.AuthorityEntry a = aIt.next(); 200 if (a.match(data) >= 0) { 201 int port = a.getPort(); 202 filter.addDataAuthority(a.getHost(), 203 port >= 0 ? Integer.toString(port) : null); 204 break; 205 } 206 } 207 } 208 Iterator<PatternMatcher> pIt = ri.filter.pathsIterator(); 209 if (pIt != null) { 210 String path = data.getPath(); 211 while (path != null && pIt.hasNext()) { 212 PatternMatcher p = pIt.next(); 213 if (p.match(path)) { 214 filter.addDataPath(p.getPath(), p.getType()); 215 break; 216 } 217 } 218 } 219 } 220 } 221 222 if (filter != null) { 223 final int N = mAdapter.mList.size(); 224 ComponentName[] set = new ComponentName[N]; 225 int bestMatch = 0; 226 for (int i=0; i<N; i++) { 227 ResolveInfo r = mAdapter.mList.get(i).ri; 228 set[i] = new ComponentName(r.activityInfo.packageName, 229 r.activityInfo.name); 230 if (r.match > bestMatch) bestMatch = r.match; 231 } 232 getPackageManager().addPreferredActivity(filter, bestMatch, set, 233 intent.getComponent()); 234 } 235 } 236 237 if (intent != null) { 238 startActivity(intent); 239 } 240 } 241 242 private final class DisplayResolveInfo { 243 ResolveInfo ri; 244 CharSequence displayLabel; 245 Drawable displayIcon; 246 CharSequence extendedInfo; 247 Intent origIntent; 248 249 DisplayResolveInfo(ResolveInfo pri, CharSequence pLabel, 250 CharSequence pInfo, Intent pOrigIntent) { 251 ri = pri; 252 displayLabel = pLabel; 253 extendedInfo = pInfo; 254 origIntent = pOrigIntent; 255 } 256 } 257 258 private final class ResolveListAdapter extends BaseAdapter { 259 private final Intent[] mInitialIntents; 260 private final List<ResolveInfo> mBaseResolveList; 261 private final Intent mIntent; 262 private final LayoutInflater mInflater; 263 264 private List<ResolveInfo> mCurrentResolveList; 265 private List<DisplayResolveInfo> mList; 266 267 public ResolveListAdapter(Context context, Intent intent, 268 Intent[] initialIntents, List<ResolveInfo> rList) { 269 mIntent = new Intent(intent); 270 mIntent.setComponent(null); 271 mInitialIntents = initialIntents; 272 mBaseResolveList = rList; 273 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 274 rebuildList(); 275 } 276 277 public void handlePackagesChanged() { 278 rebuildList(); 279 notifyDataSetChanged(); 280 if (mList.size() <= 0) { 281 // We no longer have any items... just finish the activity. 282 finish(); 283 } 284 } 285 286 private void rebuildList() { 287 if (mBaseResolveList != null) { 288 mCurrentResolveList = mBaseResolveList; 289 } else { 290 mCurrentResolveList = mPm.queryIntentActivities( 291 mIntent, PackageManager.MATCH_DEFAULT_ONLY 292 | (mAlwaysCheck != null ? PackageManager.GET_RESOLVED_FILTER : 0)); 293 } 294 int N; 295 if ((mCurrentResolveList != null) && ((N = mCurrentResolveList.size()) > 0)) { 296 // Only display the first matches that are either of equal 297 // priority or have asked to be default options. 298 ResolveInfo r0 = mCurrentResolveList.get(0); 299 for (int i=1; i<N; i++) { 300 ResolveInfo ri = mCurrentResolveList.get(i); 301 if (false) Log.v( 302 "ResolveListActivity", 303 r0.activityInfo.name + "=" + 304 r0.priority + "/" + r0.isDefault + " vs " + 305 ri.activityInfo.name + "=" + 306 ri.priority + "/" + ri.isDefault); 307 if (r0.priority != ri.priority || 308 r0.isDefault != ri.isDefault) { 309 while (i < N) { 310 mCurrentResolveList.remove(i); 311 N--; 312 } 313 } 314 } 315 if (N > 1) { 316 ResolveInfo.DisplayNameComparator rComparator = 317 new ResolveInfo.DisplayNameComparator(mPm); 318 Collections.sort(mCurrentResolveList, rComparator); 319 } 320 321 mList = new ArrayList<DisplayResolveInfo>(); 322 323 // First put the initial items at the top. 324 if (mInitialIntents != null) { 325 for (int i=0; i<mInitialIntents.length; i++) { 326 Intent ii = mInitialIntents[i]; 327 if (ii == null) { 328 continue; 329 } 330 ActivityInfo ai = ii.resolveActivityInfo( 331 getPackageManager(), 0); 332 if (ai == null) { 333 Log.w("ResolverActivity", "No activity found for " 334 + ii); 335 continue; 336 } 337 ResolveInfo ri = new ResolveInfo(); 338 ri.activityInfo = ai; 339 if (ii instanceof LabeledIntent) { 340 LabeledIntent li = (LabeledIntent)ii; 341 ri.resolvePackageName = li.getSourcePackage(); 342 ri.labelRes = li.getLabelResource(); 343 ri.nonLocalizedLabel = li.getNonLocalizedLabel(); 344 ri.icon = li.getIconResource(); 345 } 346 mList.add(new DisplayResolveInfo(ri, 347 ri.loadLabel(getPackageManager()), null, ii)); 348 } 349 } 350 351 // Check for applications with same name and use application name or 352 // package name if necessary 353 r0 = mCurrentResolveList.get(0); 354 int start = 0; 355 CharSequence r0Label = r0.loadLabel(mPm); 356 for (int i = 1; i < N; i++) { 357 if (r0Label == null) { 358 r0Label = r0.activityInfo.packageName; 359 } 360 ResolveInfo ri = mCurrentResolveList.get(i); 361 CharSequence riLabel = ri.loadLabel(mPm); 362 if (riLabel == null) { 363 riLabel = ri.activityInfo.packageName; 364 } 365 if (riLabel.equals(r0Label)) { 366 continue; 367 } 368 processGroup(mCurrentResolveList, start, (i-1), r0, r0Label); 369 r0 = ri; 370 r0Label = riLabel; 371 start = i; 372 } 373 // Process last group 374 processGroup(mCurrentResolveList, start, (N-1), r0, r0Label); 375 } 376 } 377 378 private void processGroup(List<ResolveInfo> rList, int start, int end, ResolveInfo ro, 379 CharSequence roLabel) { 380 // Process labels from start to i 381 int num = end - start+1; 382 if (num == 1) { 383 // No duplicate labels. Use label for entry at start 384 mList.add(new DisplayResolveInfo(ro, roLabel, null, null)); 385 } else { 386 boolean usePkg = false; 387 CharSequence startApp = ro.activityInfo.applicationInfo.loadLabel(mPm); 388 if (startApp == null) { 389 usePkg = true; 390 } 391 if (!usePkg) { 392 // Use HashSet to track duplicates 393 HashSet<CharSequence> duplicates = 394 new HashSet<CharSequence>(); 395 duplicates.add(startApp); 396 for (int j = start+1; j <= end ; j++) { 397 ResolveInfo jRi = rList.get(j); 398 CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm); 399 if ( (jApp == null) || (duplicates.contains(jApp))) { 400 usePkg = true; 401 break; 402 } else { 403 duplicates.add(jApp); 404 } 405 } 406 // Clear HashSet for later use 407 duplicates.clear(); 408 } 409 for (int k = start; k <= end; k++) { 410 ResolveInfo add = rList.get(k); 411 if (usePkg) { 412 // Use application name for all entries from start to end-1 413 mList.add(new DisplayResolveInfo(add, roLabel, 414 add.activityInfo.packageName, null)); 415 } else { 416 // Use package name for all entries from start to end-1 417 mList.add(new DisplayResolveInfo(add, roLabel, 418 add.activityInfo.applicationInfo.loadLabel(mPm), null)); 419 } 420 } 421 } 422 } 423 424 public ResolveInfo resolveInfoForPosition(int position) { 425 if (mList == null) { 426 return null; 427 } 428 429 return mList.get(position).ri; 430 } 431 432 public Intent intentForPosition(int position) { 433 if (mList == null) { 434 return null; 435 } 436 437 DisplayResolveInfo dri = mList.get(position); 438 439 Intent intent = new Intent(dri.origIntent != null 440 ? dri.origIntent : mIntent); 441 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT 442 |Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 443 ActivityInfo ai = dri.ri.activityInfo; 444 intent.setComponent(new ComponentName( 445 ai.applicationInfo.packageName, ai.name)); 446 return intent; 447 } 448 449 public int getCount() { 450 return mList != null ? mList.size() : 0; 451 } 452 453 public Object getItem(int position) { 454 return position; 455 } 456 457 public long getItemId(int position) { 458 return position; 459 } 460 461 public View getView(int position, View convertView, ViewGroup parent) { 462 View view; 463 if (convertView == null) { 464 view = mInflater.inflate( 465 com.android.internal.R.layout.resolve_list_item, parent, false); 466 } else { 467 view = convertView; 468 } 469 bindView(view, mList.get(position)); 470 return view; 471 } 472 473 private final void bindView(View view, DisplayResolveInfo info) { 474 TextView text = (TextView)view.findViewById(com.android.internal.R.id.text1); 475 TextView text2 = (TextView)view.findViewById(com.android.internal.R.id.text2); 476 ImageView icon = (ImageView)view.findViewById(R.id.icon); 477 text.setText(info.displayLabel); 478 if (info.extendedInfo != null) { 479 text2.setVisibility(View.VISIBLE); 480 text2.setText(info.extendedInfo); 481 } else { 482 text2.setVisibility(View.GONE); 483 } 484 if (info.displayIcon == null) { 485 info.displayIcon = info.ri.loadIcon(mPm); 486 } 487 icon.setImageDrawable(info.displayIcon); 488 } 489 } 490 491 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 492 if (mClearDefaultHint == null) return; 493 494 if(isChecked) { 495 mClearDefaultHint.setVisibility(View.VISIBLE); 496 } else { 497 mClearDefaultHint.setVisibility(View.GONE); 498 } 499 } 500 501 class ItemLongClickListener implements AdapterView.OnItemLongClickListener { 502 503 @Override 504 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 505 ResolveInfo ri = mAdapter.resolveInfoForPosition(position); 506 Intent in = new Intent().setAction("android.settings.APPLICATION_DETAILS_SETTINGS") 507 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null)); 508 startActivity(in); 509 return true; 510 } 511 512 } 513} 514 515