am f65951bf: am 0dd9ec14: Merge "Make ResolverActivity update dynamically." into ics-mr1

* commit 'f65951bf5db7a248651cbfa89886d773f95c3e03':
  Make ResolverActivity update dynamically.
This commit is contained in:
Dianne Hackborn
2012-03-02 08:36:05 -08:00
committed by Android Git Automerger

View File

@ -17,6 +17,8 @@
package com.android.internal.app; package com.android.internal.app;
import com.android.internal.R; import com.android.internal.R;
import com.android.internal.content.PackageMonitor;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -58,6 +60,12 @@ public class ResolverActivity extends AlertActivity implements
private TextView mClearDefaultHint; private TextView mClearDefaultHint;
private PackageManager mPm; private PackageManager mPm;
private final PackageMonitor mPackageMonitor = new PackageMonitor() {
@Override public void onSomePackagesChanged() {
mAdapter.handlePackagesChanged();
}
};
private Intent makeMyIntent() { private Intent makeMyIntent() {
Intent intent = new Intent(getIntent()); Intent intent = new Intent(getIntent());
// The resolver activity is set to be hidden from recent tasks. // The resolver activity is set to be hidden from recent tasks.
@ -88,6 +96,8 @@ public class ResolverActivity extends AlertActivity implements
ap.mTitle = title; ap.mTitle = title;
ap.mOnClickListener = this; ap.mOnClickListener = this;
mPackageMonitor.register(this, false);
if (alwaysUseOption) { if (alwaysUseOption) {
LayoutInflater inflater = (LayoutInflater) getSystemService( LayoutInflater inflater = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE); Context.LAYOUT_INFLATER_SERVICE);
@ -114,6 +124,19 @@ public class ResolverActivity extends AlertActivity implements
setupAlert(); setupAlert();
} }
@Override
protected void onRestart() {
super.onRestart();
mPackageMonitor.register(this, false);
mAdapter.handlePackagesChanged();
}
@Override
protected void onStop() {
super.onStop();
mPackageMonitor.unregister();
}
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
ResolveInfo ri = mAdapter.resolveInfoForPosition(which); ResolveInfo ri = mAdapter.resolveInfoForPosition(which);
Intent intent = mAdapter.intentForPosition(which); Intent intent = mAdapter.intentForPosition(which);
@ -225,29 +248,48 @@ public class ResolverActivity extends AlertActivity implements
} }
private final class ResolveListAdapter extends BaseAdapter { private final class ResolveListAdapter extends BaseAdapter {
private final Intent[] mInitialIntents;
private final List<ResolveInfo> mBaseResolveList;
private final Intent mIntent; private final Intent mIntent;
private final LayoutInflater mInflater; private final LayoutInflater mInflater;
private List<ResolveInfo> mCurrentResolveList;
private List<DisplayResolveInfo> mList; private List<DisplayResolveInfo> mList;
public ResolveListAdapter(Context context, Intent intent, public ResolveListAdapter(Context context, Intent intent,
Intent[] initialIntents, List<ResolveInfo> rList) { Intent[] initialIntents, List<ResolveInfo> rList) {
mIntent = new Intent(intent); mIntent = new Intent(intent);
mIntent.setComponent(null); mIntent.setComponent(null);
mInitialIntents = initialIntents;
mBaseResolveList = rList;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rebuildList();
}
if (rList == null) { public void handlePackagesChanged() {
rList = mPm.queryIntentActivities( rebuildList();
intent, PackageManager.MATCH_DEFAULT_ONLY notifyDataSetChanged();
if (mList.size() <= 0) {
// We no longer have any items... just finish the activity.
finish();
}
}
private void rebuildList() {
if (mBaseResolveList != null) {
mCurrentResolveList = mBaseResolveList;
} else {
mCurrentResolveList = mPm.queryIntentActivities(
mIntent, PackageManager.MATCH_DEFAULT_ONLY
| (mAlwaysCheck != null ? PackageManager.GET_RESOLVED_FILTER : 0)); | (mAlwaysCheck != null ? PackageManager.GET_RESOLVED_FILTER : 0));
} }
int N; int N;
if ((rList != null) && ((N = rList.size()) > 0)) { if ((mCurrentResolveList != null) && ((N = mCurrentResolveList.size()) > 0)) {
// Only display the first matches that are either of equal // Only display the first matches that are either of equal
// priority or have asked to be default options. // priority or have asked to be default options.
ResolveInfo r0 = rList.get(0); ResolveInfo r0 = mCurrentResolveList.get(0);
for (int i=1; i<N; i++) { for (int i=1; i<N; i++) {
ResolveInfo ri = rList.get(i); ResolveInfo ri = mCurrentResolveList.get(i);
if (false) Log.v( if (false) Log.v(
"ResolveListActivity", "ResolveListActivity",
r0.activityInfo.name + "=" + r0.activityInfo.name + "=" +
@ -257,7 +299,7 @@ public class ResolverActivity extends AlertActivity implements
if (r0.priority != ri.priority || if (r0.priority != ri.priority ||
r0.isDefault != ri.isDefault) { r0.isDefault != ri.isDefault) {
while (i < N) { while (i < N) {
rList.remove(i); mCurrentResolveList.remove(i);
N--; N--;
} }
} }
@ -265,15 +307,15 @@ public class ResolverActivity extends AlertActivity implements
if (N > 1) { if (N > 1) {
ResolveInfo.DisplayNameComparator rComparator = ResolveInfo.DisplayNameComparator rComparator =
new ResolveInfo.DisplayNameComparator(mPm); new ResolveInfo.DisplayNameComparator(mPm);
Collections.sort(rList, rComparator); Collections.sort(mCurrentResolveList, rComparator);
} }
mList = new ArrayList<DisplayResolveInfo>(); mList = new ArrayList<DisplayResolveInfo>();
// First put the initial items at the top. // First put the initial items at the top.
if (initialIntents != null) { if (mInitialIntents != null) {
for (int i=0; i<initialIntents.length; i++) { for (int i=0; i<mInitialIntents.length; i++) {
Intent ii = initialIntents[i]; Intent ii = mInitialIntents[i];
if (ii == null) { if (ii == null) {
continue; continue;
} }
@ -300,14 +342,14 @@ public class ResolverActivity extends AlertActivity implements
// Check for applications with same name and use application name or // Check for applications with same name and use application name or
// package name if necessary // package name if necessary
r0 = rList.get(0); r0 = mCurrentResolveList.get(0);
int start = 0; int start = 0;
CharSequence r0Label = r0.loadLabel(mPm); CharSequence r0Label = r0.loadLabel(mPm);
for (int i = 1; i < N; i++) { for (int i = 1; i < N; i++) {
if (r0Label == null) { if (r0Label == null) {
r0Label = r0.activityInfo.packageName; r0Label = r0.activityInfo.packageName;
} }
ResolveInfo ri = rList.get(i); ResolveInfo ri = mCurrentResolveList.get(i);
CharSequence riLabel = ri.loadLabel(mPm); CharSequence riLabel = ri.loadLabel(mPm);
if (riLabel == null) { if (riLabel == null) {
riLabel = ri.activityInfo.packageName; riLabel = ri.activityInfo.packageName;
@ -315,13 +357,13 @@ public class ResolverActivity extends AlertActivity implements
if (riLabel.equals(r0Label)) { if (riLabel.equals(r0Label)) {
continue; continue;
} }
processGroup(rList, start, (i-1), r0, r0Label); processGroup(mCurrentResolveList, start, (i-1), r0, r0Label);
r0 = ri; r0 = ri;
r0Label = riLabel; r0Label = riLabel;
start = i; start = i;
} }
// Process last group // Process last group
processGroup(rList, start, (N-1), r0, r0Label); processGroup(mCurrentResolveList, start, (N-1), r0, r0Label);
} }
} }