Skip to content

Commit e8f2c7f

Browse files
author
Dianne Hackborn
committed
Make ResolverActivity update dynamically.
Watches for package changes so it can dynamically adjust to reflect the actual list of available activities. Change-Id: I3a2fef3dac4d13d1b2a7ed6fc117a7b814679669
1 parent ee32993 commit e8f2c7f

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

core/java/com/android/internal/app/ResolverActivity.java

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.android.internal.app;
1818

1919
import com.android.internal.R;
20+
import com.android.internal.content.PackageMonitor;
21+
2022
import android.content.ComponentName;
2123
import android.content.Context;
2224
import android.content.DialogInterface;
@@ -58,6 +60,12 @@ public class ResolverActivity extends AlertActivity implements
5860
private TextView mClearDefaultHint;
5961
private PackageManager mPm;
6062

63+
private final PackageMonitor mPackageMonitor = new PackageMonitor() {
64+
@Override public void onSomePackagesChanged() {
65+
mAdapter.handlePackagesChanged();
66+
}
67+
};
68+
6169
private Intent makeMyIntent() {
6270
Intent intent = new Intent(getIntent());
6371
// The resolver activity is set to be hidden from recent tasks.
@@ -88,6 +96,8 @@ protected void onCreate(Bundle savedInstanceState, Intent intent,
8896
ap.mTitle = title;
8997
ap.mOnClickListener = this;
9098

99+
mPackageMonitor.register(this, false);
100+
91101
if (alwaysUseOption) {
92102
LayoutInflater inflater = (LayoutInflater) getSystemService(
93103
Context.LAYOUT_INFLATER_SERVICE);
@@ -114,6 +124,19 @@ protected void onCreate(Bundle savedInstanceState, Intent intent,
114124
setupAlert();
115125
}
116126

127+
@Override
128+
protected void onRestart() {
129+
super.onRestart();
130+
mPackageMonitor.register(this, false);
131+
mAdapter.handlePackagesChanged();
132+
}
133+
134+
@Override
135+
protected void onStop() {
136+
super.onStop();
137+
mPackageMonitor.unregister();
138+
}
139+
117140
public void onClick(DialogInterface dialog, int which) {
118141
ResolveInfo ri = mAdapter.resolveInfoForPosition(which);
119142
Intent intent = mAdapter.intentForPosition(which);
@@ -225,29 +248,48 @@ private final class DisplayResolveInfo {
225248
}
226249

227250
private final class ResolveListAdapter extends BaseAdapter {
251+
private final Intent[] mInitialIntents;
252+
private final List<ResolveInfo> mBaseResolveList;
228253
private final Intent mIntent;
229254
private final LayoutInflater mInflater;
230255

256+
private List<ResolveInfo> mCurrentResolveList;
231257
private List<DisplayResolveInfo> mList;
232258

233259
public ResolveListAdapter(Context context, Intent intent,
234260
Intent[] initialIntents, List<ResolveInfo> rList) {
235261
mIntent = new Intent(intent);
236262
mIntent.setComponent(null);
263+
mInitialIntents = initialIntents;
264+
mBaseResolveList = rList;
237265
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
266+
rebuildList();
267+
}
238268

239-
if (rList == null) {
240-
rList = mPm.queryIntentActivities(
241-
intent, PackageManager.MATCH_DEFAULT_ONLY
269+
public void handlePackagesChanged() {
270+
rebuildList();
271+
notifyDataSetChanged();
272+
if (mList.size() <= 0) {
273+
// We no longer have any items... just finish the activity.
274+
finish();
275+
}
276+
}
277+
278+
private void rebuildList() {
279+
if (mBaseResolveList != null) {
280+
mCurrentResolveList = mBaseResolveList;
281+
} else {
282+
mCurrentResolveList = mPm.queryIntentActivities(
283+
mIntent, PackageManager.MATCH_DEFAULT_ONLY
242284
| (mAlwaysCheck != null ? PackageManager.GET_RESOLVED_FILTER : 0));
243285
}
244286
int N;
245-
if ((rList != null) && ((N = rList.size()) > 0)) {
287+
if ((mCurrentResolveList != null) && ((N = mCurrentResolveList.size()) > 0)) {
246288
// Only display the first matches that are either of equal
247289
// priority or have asked to be default options.
248-
ResolveInfo r0 = rList.get(0);
290+
ResolveInfo r0 = mCurrentResolveList.get(0);
249291
for (int i=1; i<N; i++) {
250-
ResolveInfo ri = rList.get(i);
292+
ResolveInfo ri = mCurrentResolveList.get(i);
251293
if (false) Log.v(
252294
"ResolveListActivity",
253295
r0.activityInfo.name + "=" +
@@ -257,23 +299,23 @@ public ResolveListAdapter(Context context, Intent intent,
257299
if (r0.priority != ri.priority ||
258300
r0.isDefault != ri.isDefault) {
259301
while (i < N) {
260-
rList.remove(i);
302+
mCurrentResolveList.remove(i);
261303
N--;
262304
}
263305
}
264306
}
265307
if (N > 1) {
266308
ResolveInfo.DisplayNameComparator rComparator =
267309
new ResolveInfo.DisplayNameComparator(mPm);
268-
Collections.sort(rList, rComparator);
310+
Collections.sort(mCurrentResolveList, rComparator);
269311
}
270312

271313
mList = new ArrayList<DisplayResolveInfo>();
272314

273315
// First put the initial items at the top.
274-
if (initialIntents != null) {
275-
for (int i=0; i<initialIntents.length; i++) {
276-
Intent ii = initialIntents[i];
316+
if (mInitialIntents != null) {
317+
for (int i=0; i<mInitialIntents.length; i++) {
318+
Intent ii = mInitialIntents[i];
277319
if (ii == null) {
278320
continue;
279321
}
@@ -300,28 +342,28 @@ public ResolveListAdapter(Context context, Intent intent,
300342

301343
// Check for applications with same name and use application name or
302344
// package name if necessary
303-
r0 = rList.get(0);
345+
r0 = mCurrentResolveList.get(0);
304346
int start = 0;
305347
CharSequence r0Label = r0.loadLabel(mPm);
306348
for (int i = 1; i < N; i++) {
307349
if (r0Label == null) {
308350
r0Label = r0.activityInfo.packageName;
309351
}
310-
ResolveInfo ri = rList.get(i);
352+
ResolveInfo ri = mCurrentResolveList.get(i);
311353
CharSequence riLabel = ri.loadLabel(mPm);
312354
if (riLabel == null) {
313355
riLabel = ri.activityInfo.packageName;
314356
}
315357
if (riLabel.equals(r0Label)) {
316358
continue;
317359
}
318-
processGroup(rList, start, (i-1), r0, r0Label);
360+
processGroup(mCurrentResolveList, start, (i-1), r0, r0Label);
319361
r0 = ri;
320362
r0Label = riLabel;
321363
start = i;
322364
}
323365
// Process last group
324-
processGroup(rList, start, (N-1), r0, r0Label);
366+
processGroup(mCurrentResolveList, start, (N-1), r0, r0Label);
325367
}
326368
}
327369

0 commit comments

Comments
 (0)