public voidonButtonClick(View v) {final int id =v.getId();
startSelected(mListView.getCheckedItemPosition(), id==R.id.button_always);
dismiss();
}void startSelected(int which, booleanalways) {if(isFinishing()) {return;
}
ResolveInfo ri=mAdapter.resolveInfoForPosition(which);
Intent intent=mAdapter.intentForPosition(which);
onIntentSelected(ri, intent, always);
finish();
}protected void onIntentSelected(ResolveInfo ri, Intent intent, booleanalwaysCheck) {if (mAlwaysUseOption && mAdapter.mOrigResolveList != null) {//Build a reasonable intent filter, based on what matched.
IntentFilter filter = newIntentFilter();if (intent.getAction() != null) {
filter.addAction(intent.getAction());
}
Set categories =intent.getCategories();if (categories != null) {for(String cat : categories) {
filter.addCategory(cat);
}
}
filter.addCategory(Intent.CATEGORY_DEFAULT);int cat = ri.match&IntentFilter.MATCH_CATEGORY_MASK;
Uri data=intent.getData();if (cat ==IntentFilter.MATCH_CATEGORY_TYPE) {
String mimeType= intent.resolveType(this);if (mimeType != null) {try{
filter.addDataType(mimeType);
}catch(IntentFilter.MalformedMimeTypeException e) {
Log.w("ResolverActivity", e);
filter= null;
}
}
}if (data != null && data.getScheme() != null) {//We need the data specification if there was no type,//OR if the scheme is not one of our magical "file:"//or "content:" schemes (see IntentFilter for the reason).
if (cat !=IntentFilter.MATCH_CATEGORY_TYPE|| (!"file".equals(data.getScheme())&& !"content".equals(data.getScheme()))) {//注意这里排除了"file://", "content://"
filter.addDataScheme(data.getScheme());//Look through the resolved filter to determine which part//of it matched the original Intent.
Iterator pIt =ri.filter.schemeSpecificPartsIterator();if (pIt != null) {
String ssp=data.getSchemeSpecificPart();while (ssp != null &&pIt.hasNext()) {
PatternMatcher p=pIt.next();if(p.match(ssp)) {
filter.addDataSchemeSpecificPart(p.getPath(), p.getType());break;
}
}
}
Iterator aIt =ri.filter.authoritiesIterator();if (aIt != null) {while(aIt.hasNext()) {
IntentFilter.AuthorityEntry a=aIt.next();if (a.match(data) >= 0) {int port =a.getPort();
filter.addDataAuthority(a.getHost(), port>= 0 ? Integer.toString(port) : null);break;
}
}
}
pIt=ri.filter.pathsIterator();if (pIt != null) {
String path=data.getPath();while (path != null &&pIt.hasNext()) {
PatternMatcher p=pIt.next();if(p.match(path)) {
filter.addDataPath(p.getPath(), p.getType());break;
}
}
}
}
}if (filter != null) {final int N =mAdapter.mOrigResolveList.size();
ComponentName[] set= newComponentName[N];int bestMatch = 0;for (int i=0; i
ResolveInfo r=mAdapter.mOrigResolveList.get(i);
set[i]= newComponentName(r.activityInfo.packageName, r.activityInfo.name);if (r.match > bestMatch) bestMatch =r.match;
}if(alwaysCheck) {
getPackageManager().addPreferredActivity(filter, bestMatch, set, intent.getComponent());//设置默认
}else{try{
AppGlobals.getPackageManager().setLastChosenActivity(intent,
intent.resolveTypeIfNeeded(getContentResolver()),
PackageManager.MATCH_DEFAULT_ONLY,
filter, bestMatch, intent.getComponent());
}catch(RemoteException re) {
Log.d(TAG,"Error calling setLastChosenActivity\n" +re);
}
}
}
}if (intent != null) {
startActivity(intent);
}
}