ResolverActivity.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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 android.content.ComponentName; 21import android.content.Context; 22import android.content.DialogInterface; 23import android.content.Intent; 24import android.content.IntentFilter; 25import android.content.pm.ActivityInfo; 26import android.content.pm.PackageManager; 27import android.content.pm.ResolveInfo; 28import android.net.Uri; 29import android.os.Bundle; 30import android.os.PatternMatcher; 31import android.util.Config; 32import android.util.Log; 33import android.view.View; 34import android.view.ViewGroup; 35import android.view.LayoutInflater; 36import android.widget.BaseAdapter; 37import android.widget.CheckBox; 38import android.widget.CompoundButton; 39import android.widget.ImageView; 40import android.widget.TextView; 41import java.util.ArrayList; 42import java.util.Collections; 43import java.util.HashSet; 44import java.util.Iterator; 45import java.util.List; 46import java.util.Set; 47 48/** 49 * This activity is displayed when the system attempts to start an Intent for 50 * which there is more than one matching activity, allowing the user to decide 51 * which to go to. It is not normally used directly by application developers. 52 */ 53public class ResolverActivity extends AlertActivity implements 54 DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener { 55 private ResolveListAdapter mAdapter; 56 private CheckBox mAlwaysCheck; 57 private TextView mClearDefaultHint; 58 private PackageManager mPm; 59 60 @Override 61 protected void onCreate(Bundle savedInstanceState) { 62 onCreate(savedInstanceState, new Intent(getIntent()), 63 getResources().getText(com.android.internal.R.string.whichApplication), 64 true); 65 } 66 67 protected void onCreate(Bundle savedInstanceState, Intent intent, 68 CharSequence title, boolean alwaysUseOption) { 69 super.onCreate(savedInstanceState); 70 mPm = getPackageManager(); 71 intent.setComponent(null); 72 73 AlertController.AlertParams ap = mAlertParams; 74 75 ap.mTitle = title; 76 ap.mOnClickListener = this; 77 78 if (alwaysUseOption) { 79 LayoutInflater inflater = (LayoutInflater) getSystemService( 80 Context.LAYOUT_INFLATER_SERVICE); 81 ap.mView = inflater.inflate(R.layout.always_use_checkbox, null); 82 mAlwaysCheck = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); 83 mAlwaysCheck.setText(R.string.alwaysUse); 84 mAlwaysCheck.setOnCheckedChangeListener(this); 85 mClearDefaultHint = (TextView)ap.mView.findViewById( 86 com.android.internal.R.id.clearDefaultHint); 87 mClearDefaultHint.setVisibility(View.GONE); 88 } 89 mAdapter = new ResolveListAdapter(this, intent); 90 if (mAdapter.getCount() > 1) { 91 ap.mAdapter = mAdapter; 92 } else if (mAdapter.getCount() == 1) { 93 startActivity(mAdapter.intentForPosition(0)); 94 finish(); 95 return; 96 } else { 97 ap.mMessage = getResources().getText(com.android.internal.R.string.noApplications); 98 } 99 100 setupAlert(); 101 } 102 103 public void onClick(DialogInterface dialog, int which) { 104 ResolveInfo ri = mAdapter.resolveInfoForPosition(which); 105 Intent intent = mAdapter.intentForPosition(which); 106 107 if ((mAlwaysCheck != null) && mAlwaysCheck.isChecked()) { 108 // Build a reasonable intent filter, based on what matched. 109 IntentFilter filter = new IntentFilter(); 110 111 if (intent.getAction() != null) { 112 filter.addAction(intent.getAction()); 113 } 114 Set<String> categories = intent.getCategories(); 115 if (categories != null) { 116 for (String cat : categories) { 117 filter.addCategory(cat); 118 } 119 } 120 filter.addCategory(Intent.CATEGORY_DEFAULT); 121 122 int cat = ri.match&IntentFilter.MATCH_CATEGORY_MASK; 123 Uri data = intent.getData(); 124 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) { 125 String mimeType = intent.resolveType(this); 126 if (mimeType != null) { 127 try { 128 filter.addDataType(mimeType); 129 } catch (IntentFilter.MalformedMimeTypeException e) { 130 Log.w("ResolverActivity", e); 131 filter = null; 132 } 133 } 134 } else if (data != null && data.getScheme() != null) { 135 filter.addDataScheme(data.getScheme()); 136 137 // Look through the resolved filter to determine which part 138 // of it matched the original Intent. 139 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator(); 140 if (aIt != null) { 141 while (aIt.hasNext()) { 142 IntentFilter.AuthorityEntry a = aIt.next(); 143 if (a.match(data) >= 0) { 144 int port = a.getPort(); 145 filter.addDataAuthority(a.getHost(), 146 port >= 0 ? Integer.toString(port) : null); 147 break; 148 } 149 } 150 } 151 Iterator<PatternMatcher> pIt = ri.filter.pathsIterator(); 152 if (pIt != null) { 153 String path = data.getPath(); 154 while (path != null && pIt.hasNext()) { 155 PatternMatcher p = pIt.next(); 156 if (p.match(path)) { 157 filter.addDataPath(p.getPath(), p.getType()); 158 break; 159 } 160 } 161 } 162 } 163 164 if (filter != null) { 165 final int N = mAdapter.mList.size(); 166 ComponentName[] set = new ComponentName[N]; 167 int bestMatch = 0; 168 for (int i=0; i<N; i++) { 169 ResolveInfo r = mAdapter.mList.get(i).ri; 170 set[i] = new ComponentName(r.activityInfo.packageName, 171 r.activityInfo.name); 172 if (r.match > bestMatch) bestMatch = r.match; 173 } 174 getPackageManager().addPreferredActivity(filter, bestMatch, set, 175 intent.getComponent()); 176 } 177 } 178 179 if (intent != null) { 180 startActivity(intent); 181 } 182 finish(); 183 } 184 185 private final class DisplayResolveInfo { 186 ResolveInfo ri; 187 CharSequence displayLabel; 188 CharSequence extendedInfo; 189 190 DisplayResolveInfo(ResolveInfo pri, CharSequence pLabel, CharSequence pInfo) { 191 ri = pri; 192 displayLabel = pLabel; 193 extendedInfo = pInfo; 194 } 195 } 196 197 private final class ResolveListAdapter extends BaseAdapter { 198 private final Intent mIntent; 199 private final LayoutInflater mInflater; 200 201 private List<DisplayResolveInfo> mList; 202 203 public ResolveListAdapter(Context context, Intent intent) { 204 mIntent = new Intent(intent); 205 mIntent.setComponent(null); 206 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 207 208 List<ResolveInfo> rList = mPm.queryIntentActivities( 209 intent, PackageManager.MATCH_DEFAULT_ONLY 210 | (mAlwaysCheck != null ? PackageManager.GET_RESOLVED_FILTER : 0)); 211 int N; 212 if ((rList != null) && ((N = rList.size()) > 0)) { 213 // Only display the first matches that are either of equal 214 // priority or have asked to be default options. 215 ResolveInfo r0 = rList.get(0); 216 for (int i=1; i<N; i++) { 217 ResolveInfo ri = rList.get(i); 218 if (Config.LOGV) Log.v( 219 "ResolveListActivity", 220 r0.activityInfo.name + "=" + 221 r0.priority + "/" + r0.isDefault + " vs " + 222 ri.activityInfo.name + "=" + 223 ri.priority + "/" + ri.isDefault); 224 if (r0.priority != ri.priority || 225 r0.isDefault != ri.isDefault) { 226 while (i < N) { 227 rList.remove(i); 228 N--; 229 } 230 } 231 } 232 if (N > 1) { 233 ResolveInfo.DisplayNameComparator rComparator = 234 new ResolveInfo.DisplayNameComparator(mPm); 235 Collections.sort(rList, rComparator); 236 } 237 // Check for applications with same name and use application name or 238 // package name if necessary 239 mList = new ArrayList<DisplayResolveInfo>(); 240 r0 = rList.get(0); 241 int start = 0; 242 CharSequence r0Label = r0.loadLabel(mPm); 243 for (int i = 1; i < N; i++) { 244 if (r0Label == null) { 245 r0Label = r0.activityInfo.packageName; 246 } 247 ResolveInfo ri = rList.get(i); 248 CharSequence riLabel = ri.loadLabel(mPm); 249 if (riLabel == null) { 250 riLabel = ri.activityInfo.packageName; 251 } 252 if (riLabel.equals(r0Label)) { 253 continue; 254 } 255 processGroup(rList, start, (i-1), r0, r0Label); 256 r0 = ri; 257 r0Label = riLabel; 258 start = i; 259 } 260 // Process last group 261 processGroup(rList, start, (N-1), r0, r0Label); 262 } 263 } 264 265 private void processGroup(List<ResolveInfo> rList, int start, int end, ResolveInfo ro, 266 CharSequence roLabel) { 267 // Process labels from start to i 268 int num = end - start+1; 269 if (num == 1) { 270 // No duplicate labels. Use label for entry at start 271 mList.add(new DisplayResolveInfo(ro, roLabel, null)); 272 } else { 273 boolean usePkg = false; 274 CharSequence startApp = ro.activityInfo.applicationInfo.loadLabel(mPm); 275 if (startApp == null) { 276 usePkg = true; 277 } 278 if (!usePkg) { 279 // Use HashSet to track duplicates 280 HashSet<CharSequence> duplicates = 281 new HashSet<CharSequence>(); 282 duplicates.add(startApp); 283 for (int j = start+1; j <= end ; j++) { 284 ResolveInfo jRi = rList.get(j); 285 CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm); 286 if ( (jApp == null) || (duplicates.contains(jApp))) { 287 usePkg = true; 288 break; 289 } else { 290 duplicates.add(jApp); 291 } 292 } 293 // Clear HashSet for later use 294 duplicates.clear(); 295 } 296 for (int k = start; k <= end; k++) { 297 ResolveInfo add = rList.get(k); 298 if (usePkg) { 299 // Use application name for all entries from start to end-1 300 mList.add(new DisplayResolveInfo(add, roLabel, 301 add.activityInfo.packageName)); 302 } else { 303 // Use package name for all entries from start to end-1 304 mList.add(new DisplayResolveInfo(add, roLabel, 305 add.activityInfo.applicationInfo.loadLabel(mPm))); 306 } 307 } 308 } 309 } 310 311 public ResolveInfo resolveInfoForPosition(int position) { 312 if (mList == null) { 313 return null; 314 } 315 316 return mList.get(position).ri; 317 } 318 319 public Intent intentForPosition(int position) { 320 if (mList == null) { 321 return null; 322 } 323 324 Intent intent = new Intent(mIntent); 325 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT 326 |Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 327 ActivityInfo ai = mList.get(position).ri.activityInfo; 328 intent.setComponent(new ComponentName( 329 ai.applicationInfo.packageName, ai.name)); 330 return intent; 331 } 332 333 public int getCount() { 334 return mList != null ? mList.size() : 0; 335 } 336 337 public Object getItem(int position) { 338 return position; 339 } 340 341 public long getItemId(int position) { 342 return position; 343 } 344 345 public View getView(int position, View convertView, ViewGroup parent) { 346 View view; 347 if (convertView == null) { 348 view = mInflater.inflate( 349 com.android.internal.R.layout.resolve_list_item, parent, false); 350 } else { 351 view = convertView; 352 } 353 bindView(view, mList.get(position)); 354 return view; 355 } 356 357 private final void bindView(View view, DisplayResolveInfo info) { 358 TextView text = (TextView)view.findViewById(com.android.internal.R.id.text1); 359 TextView text2 = (TextView)view.findViewById(com.android.internal.R.id.text2); 360 ImageView icon = (ImageView)view.findViewById(R.id.icon); 361 text.setText(info.displayLabel); 362 if (info.extendedInfo != null) { 363 text2.setVisibility(View.VISIBLE); 364 text2.setText(info.extendedInfo); 365 } else { 366 text2.setVisibility(View.GONE); 367 } 368 icon.setImageDrawable(info.ri.loadIcon(mPm)); 369 } 370 } 371 372 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 373 if (mClearDefaultHint == null) return; 374 375 if(isChecked) { 376 mClearDefaultHint.setVisibility(View.VISIBLE); 377 } else { 378 mClearDefaultHint.setVisibility(View.GONE); 379 } 380 } 381} 382 383