blob: ff680e2002ac4d04c1444ba13a36865f6c48bd83 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Adam Powell4c470d62015-06-19 17:46:17 -070019import android.annotation.Nullable;
Adam Powelle9414d92014-07-05 17:44:18 -070020import android.app.Activity;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070021import android.app.ActivityThread;
Adam Powell4c470d62015-06-19 17:46:17 -070022import android.app.VoiceInteractor.PickOptionRequest;
23import android.app.VoiceInteractor.PickOptionRequest.Option;
24import android.app.VoiceInteractor.Prompt;
Adam Powell23882512016-01-29 10:21:00 -080025import android.content.pm.ComponentInfo;
Adam Powell0256c6f2013-05-29 16:42:33 -070026import android.os.AsyncTask;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070027import android.provider.Settings;
Adam Powell11f59a02014-08-20 13:22:16 -070028import android.text.TextUtils;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070029import android.util.Slog;
Adam Powell278902c2014-07-12 18:33:22 -070030import android.widget.AbsListView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import com.android.internal.R;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -080032import com.android.internal.content.PackageMonitor;
33
Adam Powellc5878612012-05-04 18:42:38 -070034import android.app.ActivityManager;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070035import android.app.ActivityManagerNative;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -070036import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.ComponentName;
38import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.Intent;
40import android.content.IntentFilter;
41import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010042import android.content.pm.ApplicationInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -070043import android.content.pm.LabeledIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070045import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010047import android.content.pm.UserInfo;
Adam Powellc5878612012-05-04 18:42:38 -070048import android.content.res.Resources;
Dianne Hackborneb034652009-09-07 00:49:58 -070049import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.net.Uri;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010051import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Bundle;
53import android.os.PatternMatcher;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070054import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070055import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070056import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010057import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.Log;
Adam Powell2d809622012-03-22 15:24:43 -070059import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.View;
61import android.view.ViewGroup;
Adam Powell2d809622012-03-22 15:24:43 -070062import android.widget.AdapterView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.widget.BaseAdapter;
Adam Powellc5878612012-05-04 18:42:38 -070064import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070066import android.widget.ListView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010068import android.widget.Toast;
Adam Powell4f6c2052014-07-07 18:49:10 -070069import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import java.util.ArrayList;
72import java.util.Collections;
73import java.util.HashSet;
74import java.util.Iterator;
75import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070076import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.util.Set;
78
Wale Ogunwale9014e662016-03-19 14:55:46 -070079import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Adrian Roos27c3ab62014-10-15 17:21:31 +020080import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
81import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083/**
84 * This activity is displayed when the system attempts to start an Intent for
85 * which there is more than one matching activity, allowing the user to decide
86 * which to go to. It is not normally used directly by application developers.
87 */
Adam Powell7d758002015-05-06 17:49:36 -070088public class ResolverActivity extends Activity {
Adam Powellc5878612012-05-04 18:42:38 -070089 private static final String TAG = "ResolverActivity";
Adam Powell09a65602014-07-20 16:23:14 -070090 private static final boolean DEBUG = false;
Adam Powellc5878612012-05-04 18:42:38 -070091
Dianne Hackborn5320eb82012-05-18 12:05:04 -070092 private int mLaunchedFromUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 private ResolveListAdapter mAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private PackageManager mPm;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070095 private boolean mSafeForwardingMode;
Adam Powellc5878612012-05-04 18:42:38 -070096 private boolean mAlwaysUseOption;
Adam Powell24428412015-04-01 17:19:56 -070097 private AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -070098 private Button mAlwaysButton;
99 private Button mOnceButton;
Adam Powell88831a22014-11-20 18:17:00 -0800100 private View mProfileView;
Adam Powellc5878612012-05-04 18:42:38 -0700101 private int mIconDpi;
Adam Powell24428412015-04-01 17:19:56 -0700102 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100103 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000104 private int mProfileSwitchMessageId = -1;
Adam Powell2ed547e2015-04-29 18:45:04 -0700105 private final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powelld25267c2015-06-05 18:02:21 -0700106 private ResolverComparator mResolverComparator;
Adam Powell4c470d62015-06-19 17:46:17 -0700107 private PickTargetOptionRequest mPickOptionRequest;
Adam Powell09a65602014-07-20 16:23:14 -0700108
Adam Powell63b31692015-09-28 10:45:00 -0700109 protected ResolverDrawerLayout mResolverDrawerLayout;
110
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700111 private boolean mRegistered;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800112 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
113 @Override public void onSomePackagesChanged() {
114 mAdapter.handlePackagesChanged();
Adam Powell88831a22014-11-20 18:17:00 -0800115 if (mProfileView != null) {
116 bindProfileView();
117 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800118 }
119 };
120
Adam Powell278902c2014-07-12 18:33:22 -0700121 private enum ActionTitle {
122 VIEW(Intent.ACTION_VIEW,
123 com.android.internal.R.string.whichViewApplication,
124 com.android.internal.R.string.whichViewApplicationNamed),
125 EDIT(Intent.ACTION_EDIT,
126 com.android.internal.R.string.whichEditApplication,
127 com.android.internal.R.string.whichEditApplicationNamed),
128 SEND(Intent.ACTION_SEND,
129 com.android.internal.R.string.whichSendApplication,
130 com.android.internal.R.string.whichSendApplicationNamed),
131 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700132 com.android.internal.R.string.whichSendToApplication,
133 com.android.internal.R.string.whichSendToApplicationNamed),
Adam Powell278902c2014-07-12 18:33:22 -0700134 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
135 com.android.internal.R.string.whichSendApplication,
136 com.android.internal.R.string.whichSendApplicationNamed),
137 DEFAULT(null,
138 com.android.internal.R.string.whichApplication,
Adam Powella35c77a2014-09-25 16:46:36 -0700139 com.android.internal.R.string.whichApplicationNamed),
140 HOME(Intent.ACTION_MAIN,
141 com.android.internal.R.string.whichHomeApplication,
142 com.android.internal.R.string.whichHomeApplicationNamed);
Adam Powell278902c2014-07-12 18:33:22 -0700143
144 public final String action;
145 public final int titleRes;
146 public final int namedTitleRes;
147
148 ActionTitle(String action, int titleRes, int namedTitleRes) {
149 this.action = action;
150 this.titleRes = titleRes;
151 this.namedTitleRes = namedTitleRes;
152 }
153
154 public static ActionTitle forAction(String action) {
155 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700156 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700157 return title;
158 }
159 }
160 return DEFAULT;
161 }
162 }
163
Dianne Hackborn905577f2011-09-07 18:31:28 -0700164 private Intent makeMyIntent() {
165 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700166 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700167 // The resolver activity is set to be hidden from recent tasks.
168 // we don't want this attribute to be propagated to the next activity
169 // being launched. Note that if the original Intent also had this
170 // flag set, we are now losing it. That should be a very rare case
171 // and we can live with this.
172 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
173 return intent;
174 }
175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 @Override
177 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700178 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700179 final Intent intent = makeMyIntent();
180 final Set<String> categories = intent.getCategories();
181 if (Intent.ACTION_MAIN.equals(intent.getAction())
182 && categories != null
183 && categories.size() == 1
184 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100185 // Note: this field is not set to true in the compatibility version.
186 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700187 }
188
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700189 setSafeForwardingMode(true);
190
Adam Powella35c77a2014-09-25 16:46:36 -0700191 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 }
193
Adam Powell278902c2014-07-12 18:33:22 -0700194 /**
Adam Powell24428412015-04-01 17:19:56 -0700195 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700196 * a default title resource
197 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700199 CharSequence title, Intent[] initialIntents,
200 List<ResolveInfo> rList, boolean alwaysUseOption) {
Adam Powell09a65602014-07-20 16:23:14 -0700201 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, alwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700202 }
203
204 protected void onCreate(Bundle savedInstanceState, Intent intent,
205 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
206 List<ResolveInfo> rList, boolean alwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700207 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000209
210 // Determine whether we should show that intent is forwarded
211 // from managed profile to owner or other way around.
212 setProfileSwitchMessageId(intent.getContentUserHint());
213
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700214 try {
215 mLaunchedFromUid = ActivityManagerNative.getDefault().getLaunchedFromUid(
216 getActivityToken());
217 } catch (RemoteException e) {
218 mLaunchedFromUid = -1;
219 }
Adam Powell7d758002015-05-06 17:49:36 -0700220
221 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
222 // Gulp!
223 finish();
224 return;
225 }
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700228
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700229 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700230 mRegistered = true;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800231
Adam Powellc5878612012-05-04 18:42:38 -0700232 final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
233 mIconDpi = am.getLauncherLargeIconDensity();
Adam Powellc5878612012-05-04 18:42:38 -0700234
Adam Powell7d758002015-05-06 17:49:36 -0700235 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700236 mIntents.add(0, new Intent(intent));
Adam Powell278902c2014-07-12 18:33:22 -0700237
Adam Powelld25267c2015-06-05 18:02:21 -0700238 final String referrerPackage = getReferrerPackageName();
239
240 mResolverComparator = new ResolverComparator(this, getTargetIntent(), referrerPackage);
241
Adam Powell39e94eb2015-09-08 17:01:49 -0700242 if (configureContentView(mIntents, initialIntents, rList, alwaysUseOption)) {
243 return;
244 }
Adam Powell278902c2014-07-12 18:33:22 -0700245
Adam Powell4f6c2052014-07-07 18:49:10 -0700246 final ResolverDrawerLayout rdl = (ResolverDrawerLayout) findViewById(R.id.contentPanel);
247 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700248 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700249 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700250 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700251 finish();
252 }
253 });
Adam Powell4c470d62015-06-19 17:46:17 -0700254 if (isVoiceInteraction()) {
255 rdl.setCollapsed(false);
256 }
Adam Powell63b31692015-09-28 10:45:00 -0700257 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700258 }
259
Adam Powell11f59a02014-08-20 13:22:16 -0700260 if (title == null) {
261 title = getTitleForAction(intent.getAction(), defaultTitleRes);
262 }
263 if (!TextUtils.isEmpty(title)) {
264 final TextView titleView = (TextView) findViewById(R.id.title);
265 if (titleView != null) {
266 titleView.setText(title);
Adam Powell278902c2014-07-12 18:33:22 -0700267 }
Adam Powell11f59a02014-08-20 13:22:16 -0700268 setTitle(title);
Adam Powell24428412015-04-01 17:19:56 -0700269
270 // Try to initialize the title icon if we have a view for it and a title to match
271 final ImageView titleIcon = (ImageView) findViewById(R.id.title_icon);
272 if (titleIcon != null) {
Adam Powell24428412015-04-01 17:19:56 -0700273 ApplicationInfo ai = null;
274 try {
275 if (!TextUtils.isEmpty(referrerPackage)) {
276 ai = mPm.getApplicationInfo(referrerPackage, 0);
277 }
278 } catch (NameNotFoundException e) {
279 Log.e(TAG, "Could not find referrer package " + referrerPackage);
280 }
281
282 if (ai != null) {
283 titleIcon.setImageDrawable(ai.loadIcon(mPm));
284 }
285 }
Adam Powelle9414d92014-07-05 17:44:18 -0700286 }
Adam Powell2d809622012-03-22 15:24:43 -0700287
Adam Powell278902c2014-07-12 18:33:22 -0700288 final ImageView iconView = (ImageView) findViewById(R.id.icon);
289 final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem();
290 if (iconView != null && iconInfo != null) {
Adam Powell24428412015-04-01 17:19:56 -0700291 new LoadIconIntoViewTask(iconInfo, iconView).execute();
Adam Powell278902c2014-07-12 18:33:22 -0700292 }
293
294 if (alwaysUseOption || mAdapter.hasFilteredItem()) {
Adam Powellc5878612012-05-04 18:42:38 -0700295 final ViewGroup buttonLayout = (ViewGroup) findViewById(R.id.button_bar);
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700296 if (buttonLayout != null) {
297 buttonLayout.setVisibility(View.VISIBLE);
298 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
299 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
300 } else {
301 mAlwaysUseOption = false;
302 }
Adam Powell278902c2014-07-12 18:33:22 -0700303 }
304
305 if (mAdapter.hasFilteredItem()) {
306 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
307 mOnceButton.setEnabled(true);
308 }
Adam Powell88831a22014-11-20 18:17:00 -0800309
310 mProfileView = findViewById(R.id.profile_button);
311 if (mProfileView != null) {
312 mProfileView.setOnClickListener(new View.OnClickListener() {
313 @Override
314 public void onClick(View v) {
315 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
316 if (dri == null) {
317 return;
318 }
319
Sander Alewijnse053d3dd2015-03-09 15:31:10 +0000320 // Do not show the profile switch message anymore.
321 mProfileSwitchMessageId = -1;
322
Adam Powell24428412015-04-01 17:19:56 -0700323 onTargetSelected(dri, false);
Adam Powell88831a22014-11-20 18:17:00 -0800324 finish();
325 }
326 });
327 bindProfileView();
328 }
Adam Powell4c470d62015-06-19 17:46:17 -0700329
330 if (isVoiceInteraction()) {
331 onSetupVoiceInteraction();
332 }
333 }
334
335 /**
336 * Perform any initialization needed for voice interaction.
337 */
Adam Powell23882512016-01-29 10:21:00 -0800338 public void onSetupVoiceInteraction() {
Adam Powell4c470d62015-06-19 17:46:17 -0700339 // Do it right now. Subclasses may delay this and send it later.
340 sendVoiceChoicesIfNeeded();
341 }
342
Adam Powell23882512016-01-29 10:21:00 -0800343 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700344 if (!isVoiceInteraction()) {
345 // Clearly not needed.
346 return;
347 }
348
349
350 final Option[] options = new Option[mAdapter.getCount()];
351 for (int i = 0, N = options.length; i < N; i++) {
352 options[i] = optionForChooserTarget(mAdapter.getItem(i), i);
353 }
354
355 mPickOptionRequest = new PickTargetOptionRequest(
356 new Prompt(getTitle()), options, null);
357 getVoiceInteractor().submitRequest(mPickOptionRequest);
358 }
359
360 Option optionForChooserTarget(TargetInfo target, int index) {
361 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800362 }
363
Adam Powell2ed547e2015-04-29 18:45:04 -0700364 protected final void setAdditionalTargets(Intent[] intents) {
365 if (intents != null) {
366 for (Intent intent : intents) {
367 mIntents.add(intent);
368 }
369 }
370 }
371
Adam Powell0ccc0e92015-04-23 17:19:37 -0700372 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700373 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700374 }
375
Adam Powell24428412015-04-01 17:19:56 -0700376 private String getReferrerPackageName() {
377 final Uri referrer = getReferrer();
378 if (referrer != null && "android-app".equals(referrer.getScheme())) {
379 return referrer.getHost();
380 }
381 return null;
382 }
383
Adam Powell23882512016-01-29 10:21:00 -0800384 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700385 return R.layout.resolver_list;
386 }
387
Adam Powell88831a22014-11-20 18:17:00 -0800388 void bindProfileView() {
389 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
390 if (dri != null) {
391 mProfileView.setVisibility(View.VISIBLE);
Nicolas Prevot256423f2016-02-09 16:33:57 +0000392 final TextView text = (TextView) mProfileView.findViewById(R.id.profile_button);
Adam Powell24428412015-04-01 17:19:56 -0700393 text.setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800394 } else {
395 mProfileView.setVisibility(View.GONE);
396 }
Adam Powell278902c2014-07-12 18:33:22 -0700397 }
398
Sander Alewijnsef6545332014-10-31 12:39:02 +0000399 private void setProfileSwitchMessageId(int contentUserHint) {
400 if (contentUserHint != UserHandle.USER_CURRENT &&
401 contentUserHint != UserHandle.myUserId()) {
402 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
403 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
404 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
405 : false;
406 boolean targetIsManaged = userManager.isManagedProfile();
407 if (originIsManaged && !targetIsManaged) {
408 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
409 } else if (!originIsManaged && targetIsManaged) {
410 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
411 }
412 }
413 }
414
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700415 /**
416 * Turn on launch mode that is safe to use when forwarding intents received from
417 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
418 * instead of the normal Activity.startActivity for launching the activity selected
419 * by the user.
420 *
421 * <p>This mode is set to true by default if the activity is initialized through
422 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
423 * methods, it is set to false by default. You must set it before calling one of the
424 * more detailed onCreate methods, so that it will be set correctly in the case where
425 * there is only one intent to resolve and it is thus started immediately.</p>
426 */
427 public void setSafeForwardingMode(boolean safeForwarding) {
428 mSafeForwardingMode = safeForwarding;
429 }
430
Adam Powell278902c2014-07-12 18:33:22 -0700431 protected CharSequence getTitleForAction(String action, int defaultTitleRes) {
Adam Powella35c77a2014-09-25 16:46:36 -0700432 final ActionTitle title = mResolvingHome ? ActionTitle.HOME : ActionTitle.forAction(action);
Adam Powell278902c2014-07-12 18:33:22 -0700433 final boolean named = mAdapter.hasFilteredItem();
434 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
435 return getString(defaultTitleRes);
436 } else {
Adam Powell24428412015-04-01 17:19:56 -0700437 return named
438 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
439 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700440 }
Adam Powellc5878612012-05-04 18:42:38 -0700441 }
442
Adam Powelle9414d92014-07-05 17:44:18 -0700443 void dismiss() {
444 if (!isFinishing()) {
445 finish();
446 }
447 }
448
Adam Powellc5878612012-05-04 18:42:38 -0700449 Drawable getIcon(Resources res, int resId) {
450 Drawable result;
451 try {
452 result = res.getDrawableForDensity(resId, mIconDpi);
453 } catch (Resources.NotFoundException e) {
454 result = null;
455 }
456
457 return result;
458 }
459
460 Drawable loadIconForResolveInfo(ResolveInfo ri) {
461 Drawable dr;
462 try {
463 if (ri.resolvePackageName != null && ri.icon != 0) {
464 dr = getIcon(mPm.getResourcesForApplication(ri.resolvePackageName), ri.icon);
465 if (dr != null) {
466 return dr;
467 }
468 }
469 final int iconRes = ri.getIconResource();
470 if (iconRes != 0) {
471 dr = getIcon(mPm.getResourcesForApplication(ri.activityInfo.packageName), iconRes);
472 if (dr != null) {
473 return dr;
474 }
475 }
476 } catch (NameNotFoundException e) {
477 Log.e(TAG, "Couldn't find resources for package", e);
478 }
479 return ri.loadIcon(mPm);
480 }
481
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800482 @Override
483 protected void onRestart() {
484 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700485 if (!mRegistered) {
486 mPackageMonitor.register(this, getMainLooper(), false);
487 mRegistered = true;
488 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800489 mAdapter.handlePackagesChanged();
Adam Powell88831a22014-11-20 18:17:00 -0800490 if (mProfileView != null) {
491 bindProfileView();
492 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800493 }
494
495 @Override
496 protected void onStop() {
497 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700498 if (mRegistered) {
499 mPackageMonitor.unregister();
500 mRegistered = false;
501 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700502 final Intent intent = getIntent();
503 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
504 && !mResolvingHome) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700505 // This resolver is in the unusual situation where it has been
506 // launched at the top of a new task. We don't let it be added
507 // to the recent tasks shown to the user, and we need to make sure
508 // that each time we are launched we get the correct launching
509 // uid (not re-using the same resolver from an old launching uid),
510 // so we will now finish ourself since being no longer visible,
511 // the user probably can't get back to us.
512 if (!isChangingConfigurations()) {
513 finish();
514 }
515 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800516 }
517
Adam Powellc5878612012-05-04 18:42:38 -0700518 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700519 protected void onDestroy() {
520 super.onDestroy();
521 if (!isChangingConfigurations() && mPickOptionRequest != null) {
522 mPickOptionRequest.cancel();
523 }
524 }
525
526 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700527 protected void onRestoreInstanceState(Bundle savedInstanceState) {
528 super.onRestoreInstanceState(savedInstanceState);
529 if (mAlwaysUseOption) {
Adam Powell24428412015-04-01 17:19:56 -0700530 final int checkedPos = mAdapterView.getCheckedItemPosition();
Nicolas Prevot50449882014-06-23 12:42:37 +0100531 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Adam Powelld81cc4a2012-07-19 13:51:39 -0700532 mLastSelected = checkedPos;
Adam Powell278902c2014-07-12 18:33:22 -0700533 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Nicolas Prevot50449882014-06-23 12:42:37 +0100534 mOnceButton.setEnabled(hasValidSelection);
535 if (hasValidSelection) {
Adam Powell24428412015-04-01 17:19:56 -0700536 mAdapterView.setSelection(checkedPos);
Adam Powell9bee4662012-05-08 11:07:23 -0700537 }
538 }
539 }
540
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100541 private boolean hasManagedProfile() {
542 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
543 if (userManager == null) {
544 return false;
545 }
546
547 try {
548 List<UserInfo> profiles = userManager.getProfiles(getUserId());
549 for (UserInfo userInfo : profiles) {
550 if (userInfo != null && userInfo.isManagedProfile()) {
551 return true;
552 }
553 }
554 } catch (SecurityException e) {
555 return false;
556 }
557 return false;
558 }
559
560 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
561 try {
562 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
563 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700564 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100565 } catch (NameNotFoundException e) {
566 return false;
567 }
568 }
569
Adam Powell278902c2014-07-12 18:33:22 -0700570 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
571 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100572 boolean enabled = false;
573 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700574 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Nicolas Prevot50449882014-06-23 12:42:37 +0100575 if (ri.targetUserId == UserHandle.USER_CURRENT) {
576 enabled = true;
577 }
578 }
579 mAlwaysButton.setEnabled(enabled);
580 }
581
Adam Powellc5878612012-05-04 18:42:38 -0700582 public void onButtonClick(View v) {
583 final int id = v.getId();
Adam Powell278902c2014-07-12 18:33:22 -0700584 startSelected(mAlwaysUseOption ?
Adam Powell24428412015-04-01 17:19:56 -0700585 mAdapterView.getCheckedItemPosition() : mAdapter.getFilteredPosition(),
Adam Powell278902c2014-07-12 18:33:22 -0700586 id == R.id.button_always,
587 mAlwaysUseOption);
Adam Powellc5878612012-05-04 18:42:38 -0700588 }
589
Adam Powell23882512016-01-29 10:21:00 -0800590 public void startSelected(int which, boolean always, boolean filtered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700591 if (isFinishing()) {
592 return;
593 }
Adam Powell278902c2014-07-12 18:33:22 -0700594 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, filtered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000595 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
596 Toast.makeText(this, String.format(getResources().getString(
597 com.android.internal.R.string.activity_resolver_work_profiles_support),
598 ri.activityInfo.loadLabel(getPackageManager()).toString()),
599 Toast.LENGTH_LONG).show();
600 return;
601 }
602
Adam Powell24428412015-04-01 17:19:56 -0700603 TargetInfo target = mAdapter.targetInfoForPosition(which, filtered);
Adam Powell2ed547e2015-04-29 18:45:04 -0700604 if (onTargetSelected(target, always)) {
605 finish();
606 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608
Adam Powelle49d9392014-07-17 18:45:19 -0700609 /**
610 * Replace me in subclasses!
611 */
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000612 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700613 return defIntent;
614 }
615
Adam Powell2ed547e2015-04-29 18:45:04 -0700616 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700617 final ResolveInfo ri = target.getResolveInfo();
618 final Intent intent = target != null ? target.getResolvedIntent() : null;
619
620 if (intent != null && (mAlwaysUseOption || mAdapter.hasFilteredItem())
621 && mAdapter.mOrigResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700622 // Build a reasonable intent filter, based on what matched.
623 IntentFilter filter = new IntentFilter();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800624 String action = intent.getAction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800626 if (action != null) {
627 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700629 Set<String> categories = intent.getCategories();
630 if (categories != null) {
631 for (String cat : categories) {
632 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700635 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636
Adam Powell24428412015-04-01 17:19:56 -0700637 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700638 Uri data = intent.getData();
639 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
640 String mimeType = intent.resolveType(this);
641 if (mimeType != null) {
642 try {
643 filter.addDataType(mimeType);
644 } catch (IntentFilter.MalformedMimeTypeException e) {
645 Log.w("ResolverActivity", e);
646 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700649 }
650 if (data != null && data.getScheme() != null) {
651 // We need the data specification if there was no type,
652 // OR if the scheme is not one of our magical "file:"
653 // or "content:" schemes (see IntentFilter for the reason).
654 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
655 || (!"file".equals(data.getScheme())
656 && !"content".equals(data.getScheme()))) {
657 filter.addDataScheme(data.getScheme());
658
659 // Look through the resolved filter to determine which part
660 // of it matched the original Intent.
661 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
662 if (pIt != null) {
663 String ssp = data.getSchemeSpecificPart();
664 while (ssp != null && pIt.hasNext()) {
665 PatternMatcher p = pIt.next();
666 if (p.match(ssp)) {
667 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
668 break;
669 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700670 }
671 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700672 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
673 if (aIt != null) {
674 while (aIt.hasNext()) {
675 IntentFilter.AuthorityEntry a = aIt.next();
676 if (a.match(data) >= 0) {
677 int port = a.getPort();
678 filter.addDataAuthority(a.getHost(),
679 port >= 0 ? Integer.toString(port) : null);
680 break;
681 }
682 }
683 }
684 pIt = ri.filter.pathsIterator();
685 if (pIt != null) {
686 String path = data.getPath();
687 while (path != null && pIt.hasNext()) {
688 PatternMatcher p = pIt.next();
689 if (p.match(path)) {
690 filter.addDataPath(p.getPath(), p.getType());
691 break;
692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694 }
695 }
696 }
697
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700698 if (filter != null) {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700699 final int N = mAdapter.mOrigResolveList.size();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700700 ComponentName[] set = new ComponentName[N];
701 int bestMatch = 0;
702 for (int i=0; i<N; i++) {
Adam Powell2ed547e2015-04-29 18:45:04 -0700703 ResolveInfo r = mAdapter.mOrigResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700704 set[i] = new ComponentName(r.activityInfo.packageName,
705 r.activityInfo.name);
706 if (r.match > bestMatch) bestMatch = r.match;
707 }
708 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700709 final int userId = getUserId();
710 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800711
712 // Set the preferred Activity
713 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
714
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700715 if (ri.handleAllWebDataURI) {
716 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700717 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700718 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700719 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700720 }
721 } else {
722 // Update Domain Verification status
723 ComponentName cn = intent.getComponent();
724 String packageName = cn.getPackageName();
725 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800726
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700727 boolean isHttpOrHttps = (dataScheme != null) &&
728 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
729 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800730
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700731 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
732 boolean hasCategoryBrowsable = (categories != null) &&
733 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800734
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700735 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700736 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700737 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
738 userId);
739 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800740 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700741 } else {
742 try {
743 AppGlobals.getPackageManager().setLastChosenActivity(intent,
744 intent.resolveTypeIfNeeded(getContentResolver()),
745 PackageManager.MATCH_DEFAULT_ONLY,
746 filter, bestMatch, intent.getComponent());
747 } catch (RemoteException re) {
748 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
749 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 }
752 }
753
Adam Powell24428412015-04-01 17:19:56 -0700754 if (target != null) {
755 safelyStartActivity(target);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700756 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700757 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700758 }
759
Adam Powell23882512016-01-29 10:21:00 -0800760 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -0700761 // We're dispatching intents that might be coming from legacy apps, so
762 // don't kill ourselves.
763 StrictMode.disableDeathOnFileUriExposure();
764 try {
765 safelyStartActivityInternal(cti);
766 } finally {
767 StrictMode.enableDeathOnFileUriExposure();
768 }
769 }
770
771 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +0000772 // If needed, show that intent is forwarded
773 // from managed profile to owner or other way around.
774 if (mProfileSwitchMessageId != -1) {
775 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
776 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700777 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -0700778 if (cti.start(this, null)) {
779 onActivityStarted(cti);
780 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700781 return;
782 }
783 try {
Adam Powell24428412015-04-01 17:19:56 -0700784 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
785 onActivityStarted(cti);
786 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700787 } catch (RuntimeException e) {
788 String launchedFromPackage;
789 try {
790 launchedFromPackage = ActivityManagerNative.getDefault().getLaunchedFromPackage(
791 getActivityToken());
792 } catch (RemoteException e2) {
793 launchedFromPackage = "??";
794 }
795 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
796 + " package " + launchedFromPackage + ", while running in "
797 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
800
Adam Powell23882512016-01-29 10:21:00 -0800801 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -0700802 // Do nothing
803 }
804
Adam Powell23882512016-01-29 10:21:00 -0800805 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -0700806 return false;
807 }
808
Adam Powell23882512016-01-29 10:21:00 -0800809 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700810 return true;
811 }
812
Adam Powell23882512016-01-29 10:21:00 -0800813 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700814 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -0700815 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -0700816 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -0700817 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -0700818 }
819
Adam Powell23882512016-01-29 10:21:00 -0800820 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents,
Adam Powell7d758002015-05-06 17:49:36 -0700821 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
822 boolean filterLastUsed) {
823 return new ResolveListAdapter(context, payloadIntents, initialIntents, rList,
824 launchedFromUid, filterLastUsed);
Adam Powell88831a22014-11-20 18:17:00 -0800825 }
826
Adam Powell39e94eb2015-09-08 17:01:49 -0700827 /**
828 * Returns true if the activity is finishing and creation should halt
829 */
Adam Powell23882512016-01-29 10:21:00 -0800830 public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents,
Adam Powell7d758002015-05-06 17:49:36 -0700831 List<ResolveInfo> rList, boolean alwaysUseOption) {
Dianne Hackborn57dd7372015-07-27 18:11:14 -0700832 // The last argument of createAdapter is whether to do special handling
833 // of the last used choice to highlight it in the list. We need to always
834 // turn this off when running under voice interaction, since it results in
835 // a more complicated UI that the current voice interaction flow is not able
836 // to handle.
Adam Powell7d758002015-05-06 17:49:36 -0700837 mAdapter = createAdapter(this, payloadIntents, initialIntents, rList,
Dianne Hackborn57dd7372015-07-27 18:11:14 -0700838 mLaunchedFromUid, alwaysUseOption && !isVoiceInteraction());
Adam Powell7d758002015-05-06 17:49:36 -0700839
840 final int layoutId;
841 if (mAdapter.hasFilteredItem()) {
842 layoutId = R.layout.resolver_list_with_default;
843 alwaysUseOption = false;
844 } else {
845 layoutId = getLayoutResource();
846 }
847 mAlwaysUseOption = alwaysUseOption;
848
Adam Powell50077352015-05-26 18:01:55 -0700849 int count = mAdapter.getUnfilteredCount();
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -0700850 if (count == 1 && mAdapter.getOtherProfile() == null) {
851 // Only one target, so we're a candidate to auto-launch!
852 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
853 if (shouldAutoLaunchSingleChoice(target)) {
854 safelyStartActivity(target);
855 mPackageMonitor.unregister();
856 mRegistered = false;
857 finish();
858 return true;
859 }
860 }
861 if (count > 0) {
Adam Powell7d758002015-05-06 17:49:36 -0700862 setContentView(layoutId);
863 mAdapterView = (AbsListView) findViewById(R.id.resolver_list);
864 onPrepareAdapterView(mAdapterView, mAdapter, alwaysUseOption);
Adam Powell7d758002015-05-06 17:49:36 -0700865 } else {
866 setContentView(R.layout.resolver_list);
867
868 final TextView empty = (TextView) findViewById(R.id.empty);
869 empty.setVisibility(View.VISIBLE);
870
871 mAdapterView = (AbsListView) findViewById(R.id.resolver_list);
872 mAdapterView.setVisibility(View.GONE);
873 }
Adam Powell39e94eb2015-09-08 17:01:49 -0700874 return false;
Adam Powell7d758002015-05-06 17:49:36 -0700875 }
876
Adam Powell23882512016-01-29 10:21:00 -0800877 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter,
Adam Powell7d758002015-05-06 17:49:36 -0700878 boolean alwaysUseOption) {
879 final boolean useHeader = adapter.hasFilteredItem();
880 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
881
882 adapterView.setAdapter(mAdapter);
883
884 final ItemClickListener listener = new ItemClickListener();
885 adapterView.setOnItemClickListener(listener);
886 adapterView.setOnItemLongClickListener(listener);
887
888 if (alwaysUseOption) {
889 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
890 }
891
892 if (useHeader && listView != null) {
893 listView.addHeaderView(LayoutInflater.from(this).inflate(
894 R.layout.resolver_different_item_header, listView, false));
895 }
Adam Powell24428412015-04-01 17:19:56 -0700896 }
897
Adam Powellc412be62015-06-24 13:54:10 -0700898 /**
899 * Check a simple match for the component of two ResolveInfos.
900 */
901 static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
902 return lhs == null ? rhs == null
903 : lhs.activityInfo == null ? rhs.activityInfo == null
904 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
905 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
906 }
907
Adam Powell23882512016-01-29 10:21:00 -0800908 public final class DisplayResolveInfo implements TargetInfo {
Adam Powell24428412015-04-01 17:19:56 -0700909 private final ResolveInfo mResolveInfo;
910 private final CharSequence mDisplayLabel;
911 private Drawable mDisplayIcon;
Adam Powell00f4aad2015-09-17 13:38:16 -0700912 private Drawable mBadge;
Adam Powell24428412015-04-01 17:19:56 -0700913 private final CharSequence mExtendedInfo;
914 private final Intent mResolvedIntent;
Adam Powell2ed547e2015-04-29 18:45:04 -0700915 private final List<Intent> mSourceIntents = new ArrayList<>();
Adam Powell23882512016-01-29 10:21:00 -0800916 private boolean mPinned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917
Adam Powell23882512016-01-29 10:21:00 -0800918 public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel,
Dianne Hackborneb034652009-09-07 00:49:58 -0700919 CharSequence pInfo, Intent pOrigIntent) {
Adam Powell2ed547e2015-04-29 18:45:04 -0700920 mSourceIntents.add(originalIntent);
Adam Powell24428412015-04-01 17:19:56 -0700921 mResolveInfo = pri;
922 mDisplayLabel = pLabel;
923 mExtendedInfo = pInfo;
924
925 final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent :
Adam Powell2ed547e2015-04-29 18:45:04 -0700926 getReplacementIntent(pri.activityInfo, getTargetIntent()));
Adam Powell24428412015-04-01 17:19:56 -0700927 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
928 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
929 final ActivityInfo ai = mResolveInfo.activityInfo;
930 intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));
931
932 mResolvedIntent = intent;
933 }
934
Adam Powell2ed547e2015-04-29 18:45:04 -0700935 private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) {
936 mSourceIntents.addAll(other.getAllSourceIntents());
937 mResolveInfo = other.mResolveInfo;
938 mDisplayLabel = other.mDisplayLabel;
939 mDisplayIcon = other.mDisplayIcon;
940 mExtendedInfo = other.mExtendedInfo;
941 mResolvedIntent = new Intent(other.mResolvedIntent);
942 mResolvedIntent.fillIn(fillInIntent, flags);
Adam Powell23882512016-01-29 10:21:00 -0800943 mPinned = other.mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -0700944 }
945
Adam Powell24428412015-04-01 17:19:56 -0700946 public ResolveInfo getResolveInfo() {
947 return mResolveInfo;
948 }
949
950 public CharSequence getDisplayLabel() {
951 return mDisplayLabel;
952 }
953
954 public Drawable getDisplayIcon() {
955 return mDisplayIcon;
956 }
957
Adam Powell7d758002015-05-06 17:49:36 -0700958 public Drawable getBadgeIcon() {
Adam Powell00f4aad2015-09-17 13:38:16 -0700959 // We only expose a badge if we have extended info.
960 // The badge is a higher-priority disambiguation signal
961 // but we don't need one if we wouldn't show extended info at all.
962 if (TextUtils.isEmpty(getExtendedInfo())) {
963 return null;
964 }
965
966 if (mBadge == null && mResolveInfo != null && mResolveInfo.activityInfo != null
967 && mResolveInfo.activityInfo.applicationInfo != null) {
968 if (mResolveInfo.activityInfo.icon == 0 || mResolveInfo.activityInfo.icon
969 == mResolveInfo.activityInfo.applicationInfo.icon) {
970 // Badging an icon with exactly the same icon is silly.
971 // If the activityInfo icon resid is 0 it will fall back
972 // to the application's icon, making it a match.
973 return null;
974 }
975 mBadge = mResolveInfo.activityInfo.applicationInfo.loadIcon(mPm);
976 }
977 return mBadge;
Adam Powell7d758002015-05-06 17:49:36 -0700978 }
979
Adam Powell2ed547e2015-04-29 18:45:04 -0700980 @Override
Alan Viverettece5d92c2015-07-31 16:46:56 -0400981 public CharSequence getBadgeContentDescription() {
982 return null;
983 }
984
985 @Override
Adam Powell2ed547e2015-04-29 18:45:04 -0700986 public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) {
987 return new DisplayResolveInfo(this, fillInIntent, flags);
988 }
989
990 @Override
991 public List<Intent> getAllSourceIntents() {
992 return mSourceIntents;
993 }
994
995 public void addAlternateSourceIntent(Intent alt) {
996 mSourceIntents.add(alt);
997 }
998
Adam Powell24428412015-04-01 17:19:56 -0700999 public void setDisplayIcon(Drawable icon) {
1000 mDisplayIcon = icon;
1001 }
1002
1003 public boolean hasDisplayIcon() {
1004 return mDisplayIcon != null;
1005 }
1006
1007 public CharSequence getExtendedInfo() {
1008 return mExtendedInfo;
1009 }
1010
1011 public Intent getResolvedIntent() {
1012 return mResolvedIntent;
1013 }
1014
1015 @Override
1016 public ComponentName getResolvedComponentName() {
1017 return new ComponentName(mResolveInfo.activityInfo.packageName,
1018 mResolveInfo.activityInfo.name);
1019 }
1020
1021 @Override
1022 public boolean start(Activity activity, Bundle options) {
1023 activity.startActivity(mResolvedIntent, options);
1024 return true;
1025 }
1026
1027 @Override
1028 public boolean startAsCaller(Activity activity, Bundle options, int userId) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001029 activity.startActivityAsCaller(mResolvedIntent, options, false, userId);
Adam Powell24428412015-04-01 17:19:56 -07001030 return true;
1031 }
1032
1033 @Override
1034 public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
1035 activity.startActivityAsUser(mResolvedIntent, options, user);
1036 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
Adam Powell23882512016-01-29 10:21:00 -08001038
1039 @Override
1040 public boolean isPinned() {
1041 return mPinned;
1042 }
1043
1044 public void setPinned(boolean pinned) {
1045 mPinned = pinned;
1046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 }
1048
Adam Powell24428412015-04-01 17:19:56 -07001049 /**
1050 * A single target as represented in the chooser.
1051 */
1052 public interface TargetInfo {
1053 /**
1054 * Get the resolved intent that represents this target. Note that this may not be the
1055 * intent that will be launched by calling one of the <code>start</code> methods provided;
1056 * this is the intent that will be credited with the launch.
1057 *
1058 * @return the resolved intent for this target
1059 */
Adam Powell23882512016-01-29 10:21:00 -08001060 Intent getResolvedIntent();
Adam Powell24428412015-04-01 17:19:56 -07001061
1062 /**
1063 * Get the resolved component name that represents this target. Note that this may not
1064 * be the component that will be directly launched by calling one of the <code>start</code>
1065 * methods provided; this is the component that will be credited with the launch.
1066 *
1067 * @return the resolved ComponentName for this target
1068 */
Adam Powell23882512016-01-29 10:21:00 -08001069 ComponentName getResolvedComponentName();
Adam Powell24428412015-04-01 17:19:56 -07001070
1071 /**
1072 * Start the activity referenced by this target.
1073 *
1074 * @param activity calling Activity performing the launch
1075 * @param options ActivityOptions bundle
1076 * @return true if the start completed successfully
1077 */
Adam Powell23882512016-01-29 10:21:00 -08001078 boolean start(Activity activity, Bundle options);
Adam Powell24428412015-04-01 17:19:56 -07001079
1080 /**
1081 * Start the activity referenced by this target as if the ResolverActivity's caller
1082 * was performing the start operation.
1083 *
1084 * @param activity calling Activity (actually) performing the launch
1085 * @param options ActivityOptions bundle
1086 * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller
1087 * @return true if the start completed successfully
1088 */
Adam Powell23882512016-01-29 10:21:00 -08001089 boolean startAsCaller(Activity activity, Bundle options, int userId);
Adam Powell24428412015-04-01 17:19:56 -07001090
1091 /**
1092 * Start the activity referenced by this target as a given user.
1093 *
1094 * @param activity calling activity performing the launch
1095 * @param options ActivityOptions bundle
1096 * @param user handle for the user to start the activity as
1097 * @return true if the start completed successfully
1098 */
Adam Powell23882512016-01-29 10:21:00 -08001099 boolean startAsUser(Activity activity, Bundle options, UserHandle user);
Adam Powell24428412015-04-01 17:19:56 -07001100
1101 /**
1102 * Return the ResolveInfo about how and why this target matched the original query
1103 * for available targets.
1104 *
1105 * @return ResolveInfo representing this target's match
1106 */
Adam Powell23882512016-01-29 10:21:00 -08001107 ResolveInfo getResolveInfo();
Adam Powell24428412015-04-01 17:19:56 -07001108
1109 /**
1110 * Return the human-readable text label for this target.
1111 *
1112 * @return user-visible target label
1113 */
Adam Powell23882512016-01-29 10:21:00 -08001114 CharSequence getDisplayLabel();
Adam Powell24428412015-04-01 17:19:56 -07001115
1116 /**
1117 * Return any extended info for this target. This may be used to disambiguate
1118 * otherwise identical targets.
1119 *
1120 * @return human-readable disambig string or null if none present
1121 */
Adam Powell23882512016-01-29 10:21:00 -08001122 CharSequence getExtendedInfo();
Adam Powell24428412015-04-01 17:19:56 -07001123
1124 /**
1125 * @return The drawable that should be used to represent this target
1126 */
Adam Powell23882512016-01-29 10:21:00 -08001127 Drawable getDisplayIcon();
Adam Powell2ed547e2015-04-29 18:45:04 -07001128
1129 /**
Adam Powell7d758002015-05-06 17:49:36 -07001130 * @return The (small) icon to badge the target with
1131 */
Adam Powell23882512016-01-29 10:21:00 -08001132 Drawable getBadgeIcon();
Adam Powell7d758002015-05-06 17:49:36 -07001133
1134 /**
Alan Viverettece5d92c2015-07-31 16:46:56 -04001135 * @return The content description for the badge icon
1136 */
Adam Powell23882512016-01-29 10:21:00 -08001137 CharSequence getBadgeContentDescription();
Alan Viverettece5d92c2015-07-31 16:46:56 -04001138
1139 /**
Adam Powell2ed547e2015-04-29 18:45:04 -07001140 * Clone this target with the given fill-in information.
1141 */
Adam Powell23882512016-01-29 10:21:00 -08001142 TargetInfo cloneFilledIn(Intent fillInIntent, int flags);
Adam Powell2ed547e2015-04-29 18:45:04 -07001143
1144 /**
1145 * @return the list of supported source intents deduped against this single target
1146 */
Adam Powell23882512016-01-29 10:21:00 -08001147 List<Intent> getAllSourceIntents();
1148
1149 /**
1150 * @return true if this target should be pinned to the front by the request of the user
1151 */
1152 boolean isPinned();
Adam Powell24428412015-04-01 17:19:56 -07001153 }
1154
Adam Powell23882512016-01-29 10:21:00 -08001155 public class ResolveListAdapter extends BaseAdapter {
Adam Powell7d758002015-05-06 17:49:36 -07001156 private final List<Intent> mIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001157 private final Intent[] mInitialIntents;
1158 private final List<ResolveInfo> mBaseResolveList;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001159 private ResolveInfo mLastChosen;
Adam Powell88831a22014-11-20 18:17:00 -08001160 private DisplayResolveInfo mOtherProfile;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001161 private final int mLaunchedFromUid;
Adam Powell24428412015-04-01 17:19:56 -07001162 private boolean mHasExtendedInfo;
1163
1164 protected final LayoutInflater mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165
Adam Powell2ed547e2015-04-29 18:45:04 -07001166 List<DisplayResolveInfo> mDisplayList;
1167 List<ResolvedComponentInfo> mOrigResolveList;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001168
Adam Powell278902c2014-07-12 18:33:22 -07001169 private int mLastChosenPosition = -1;
1170 private boolean mFilterLastUsed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171
Adam Powell7d758002015-05-06 17:49:36 -07001172 public ResolveListAdapter(Context context, List<Intent> payloadIntents,
1173 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
1174 boolean filterLastUsed) {
1175 mIntents = payloadIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001176 mInitialIntents = initialIntents;
1177 mBaseResolveList = rList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001178 mLaunchedFromUid = launchedFromUid;
Adam Powelle9414d92014-07-05 17:44:18 -07001179 mInflater = LayoutInflater.from(context);
Adam Powell2ed547e2015-04-29 18:45:04 -07001180 mDisplayList = new ArrayList<>();
Adam Powell278902c2014-07-12 18:33:22 -07001181 mFilterLastUsed = filterLastUsed;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001182 rebuildList();
1183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001185 public void handlePackagesChanged() {
1186 rebuildList();
1187 notifyDataSetChanged();
Esteban Talavera6de72662014-12-11 17:54:07 +00001188 if (getCount() == 0) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001189 // We no longer have any items... just finish the activity.
1190 finish();
Adam Powellc5878612012-05-04 18:42:38 -07001191 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001192 }
1193
Adam Powell278902c2014-07-12 18:33:22 -07001194 public DisplayResolveInfo getFilteredItem() {
1195 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1196 // Not using getItem since it offsets to dodge this position for the list
Adam Powell2ed547e2015-04-29 18:45:04 -07001197 return mDisplayList.get(mLastChosenPosition);
Adam Powell278902c2014-07-12 18:33:22 -07001198 }
1199 return null;
1200 }
1201
Adam Powell88831a22014-11-20 18:17:00 -08001202 public DisplayResolveInfo getOtherProfile() {
1203 return mOtherProfile;
1204 }
1205
Adam Powell278902c2014-07-12 18:33:22 -07001206 public int getFilteredPosition() {
1207 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1208 return mLastChosenPosition;
1209 }
1210 return AbsListView.INVALID_POSITION;
1211 }
1212
1213 public boolean hasFilteredItem() {
1214 return mFilterLastUsed && mLastChosenPosition >= 0;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001215 }
1216
Adam Powella182e452015-07-06 16:57:56 -07001217 public float getScore(DisplayResolveInfo target) {
1218 return mResolverComparator.getScore(target.getResolvedComponentName());
1219 }
1220
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001221 private void rebuildList() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001222 List<ResolvedComponentInfo> currentResolveList = null;
You Kim43a5070e2012-11-21 23:23:45 +09001223
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001224 try {
Adam Powell2ed547e2015-04-29 18:45:04 -07001225 final Intent primaryIntent = getTargetIntent();
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001226 mLastChosen = AppGlobals.getPackageManager().getLastChosenActivity(
Adam Powell2ed547e2015-04-29 18:45:04 -07001227 primaryIntent, primaryIntent.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001228 PackageManager.MATCH_DEFAULT_ONLY);
1229 } catch (RemoteException re) {
1230 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1231 }
1232
Sudheer Shanka7e64e102015-01-23 10:37:45 +00001233 // Clear the value of mOtherProfile from previous call.
1234 mOtherProfile = null;
Adam Powell2ed547e2015-04-29 18:45:04 -07001235 mDisplayList.clear();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001236 if (mBaseResolveList != null) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001237 currentResolveList = mOrigResolveList = new ArrayList<>();
1238 addResolveListDedupe(currentResolveList, getTargetIntent(), mBaseResolveList);
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001239 } else {
Adam Powell2ed547e2015-04-29 18:45:04 -07001240 final boolean shouldGetResolvedFilter = shouldGetResolvedFilter();
1241 final boolean shouldGetActivityMetadata = shouldGetActivityMetadata();
1242 for (int i = 0, N = mIntents.size(); i < N; i++) {
1243 final Intent intent = mIntents.get(i);
1244 final List<ResolveInfo> infos = mPm.queryIntentActivities(intent,
1245 PackageManager.MATCH_DEFAULT_ONLY
1246 | (shouldGetResolvedFilter ? PackageManager.GET_RESOLVED_FILTER : 0)
1247 | (shouldGetActivityMetadata ? PackageManager.GET_META_DATA : 0));
1248 if (infos != null) {
1249 if (currentResolveList == null) {
1250 currentResolveList = mOrigResolveList = new ArrayList<>();
1251 }
1252 addResolveListDedupe(currentResolveList, intent, infos);
1253 }
1254 }
1255
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001256 // Filter out any activities that the launched uid does not
Nicolas Prevot697397a2016-01-11 11:14:32 +00001257 // have permission for.
1258 // Also filter out those that are suspended because they couldn't
1259 // be started. We don't do this when we have an explicit
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001260 // list of resolved activities, because that only happens when
1261 // we are being subclassed, so we can safely launch whatever
1262 // they gave us.
You Kim43a5070e2012-11-21 23:23:45 +09001263 if (currentResolveList != null) {
1264 for (int i=currentResolveList.size()-1; i >= 0; i--) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001265 ActivityInfo ai = currentResolveList.get(i)
1266 .getResolveInfoAt(0).activityInfo;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001267 int granted = ActivityManager.checkComponentPermission(
1268 ai.permission, mLaunchedFromUid,
1269 ai.applicationInfo.uid, ai.exported);
Nicolas Prevot697397a2016-01-11 11:14:32 +00001270 boolean suspended = (ai.applicationInfo.flags
1271 & ApplicationInfo.FLAG_SUSPENDED) != 0;
1272 if (granted != PackageManager.PERMISSION_GRANTED || suspended) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001273 // Access not allowed!
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001274 if (mOrigResolveList == currentResolveList) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001275 mOrigResolveList = new ArrayList<>(mOrigResolveList);
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001276 }
You Kim43a5070e2012-11-21 23:23:45 +09001277 currentResolveList.remove(i);
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001278 }
1279 }
1280 }
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 int N;
You Kim43a5070e2012-11-21 23:23:45 +09001283 if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 // Only display the first matches that are either of equal
1285 // priority or have asked to be default options.
Adam Powell2ed547e2015-04-29 18:45:04 -07001286 ResolvedComponentInfo rci0 = currentResolveList.get(0);
1287 ResolveInfo r0 = rci0.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 for (int i=1; i<N; i++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001289 ResolveInfo ri = currentResolveList.get(i).getResolveInfoAt(0);
You Kim43a5070e2012-11-21 23:23:45 +09001290 if (DEBUG) Log.v(
Adam Powell09a65602014-07-20 16:23:14 -07001291 TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 r0.activityInfo.name + "=" +
1293 r0.priority + "/" + r0.isDefault + " vs " +
1294 ri.activityInfo.name + "=" +
1295 ri.priority + "/" + ri.isDefault);
You Kim43a5070e2012-11-21 23:23:45 +09001296 if (r0.priority != ri.priority ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 r0.isDefault != ri.isDefault) {
1298 while (i < N) {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001299 if (mOrigResolveList == currentResolveList) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001300 mOrigResolveList = new ArrayList<>(mOrigResolveList);
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001301 }
You Kim43a5070e2012-11-21 23:23:45 +09001302 currentResolveList.remove(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 N--;
1304 }
1305 }
1306 }
1307 if (N > 1) {
Adam Powelld25267c2015-06-05 18:02:21 -07001308 mResolverComparator.compute(currentResolveList);
1309 Collections.sort(currentResolveList, mResolverComparator);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
Dianne Hackborneb034652009-09-07 00:49:58 -07001311 // First put the initial items at the top.
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001312 if (mInitialIntents != null) {
1313 for (int i=0; i<mInitialIntents.length; i++) {
1314 Intent ii = mInitialIntents[i];
Dianne Hackborneb034652009-09-07 00:49:58 -07001315 if (ii == null) {
1316 continue;
1317 }
1318 ActivityInfo ai = ii.resolveActivityInfo(
1319 getPackageManager(), 0);
1320 if (ai == null) {
Adam Powell09a65602014-07-20 16:23:14 -07001321 Log.w(TAG, "No activity found for " + ii);
Dianne Hackborneb034652009-09-07 00:49:58 -07001322 continue;
1323 }
1324 ResolveInfo ri = new ResolveInfo();
1325 ri.activityInfo = ai;
Nicolas Prevot1a815922014-10-10 16:22:38 +01001326 UserManager userManager =
1327 (UserManager) getSystemService(Context.USER_SERVICE);
Dianne Hackborneb034652009-09-07 00:49:58 -07001328 if (ii instanceof LabeledIntent) {
1329 LabeledIntent li = (LabeledIntent)ii;
1330 ri.resolvePackageName = li.getSourcePackage();
1331 ri.labelRes = li.getLabelResource();
1332 ri.nonLocalizedLabel = li.getNonLocalizedLabel();
1333 ri.icon = li.getIconResource();
Sudheer Shanka9ded7602015-05-19 21:17:25 +01001334 ri.iconResourceId = ri.icon;
1335 }
1336 if (userManager.isManagedProfile()) {
1337 ri.noResourceId = true;
1338 ri.icon = 0;
Dianne Hackborneb034652009-09-07 00:49:58 -07001339 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001340 addResolveInfo(new DisplayResolveInfo(ii, ri,
Dianne Hackborneb034652009-09-07 00:49:58 -07001341 ri.loadLabel(getPackageManager()), null, ii));
1342 }
1343 }
You Kim43a5070e2012-11-21 23:23:45 +09001344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 // Check for applications with same name and use application name or
1346 // package name if necessary
Adam Powell2ed547e2015-04-29 18:45:04 -07001347 rci0 = currentResolveList.get(0);
1348 r0 = rci0.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 int start = 0;
1350 CharSequence r0Label = r0.loadLabel(mPm);
Adam Powell24428412015-04-01 17:19:56 -07001351 mHasExtendedInfo = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 for (int i = 1; i < N; i++) {
1353 if (r0Label == null) {
1354 r0Label = r0.activityInfo.packageName;
1355 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001356 ResolvedComponentInfo rci = currentResolveList.get(i);
1357 ResolveInfo ri = rci.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 CharSequence riLabel = ri.loadLabel(mPm);
1359 if (riLabel == null) {
1360 riLabel = ri.activityInfo.packageName;
1361 }
1362 if (riLabel.equals(r0Label)) {
1363 continue;
1364 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001365 processGroup(currentResolveList, start, (i-1), rci0, r0Label);
1366 rci0 = rci;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 r0 = ri;
1368 r0Label = riLabel;
1369 start = i;
1370 }
1371 // Process last group
Adam Powell2ed547e2015-04-29 18:45:04 -07001372 processGroup(currentResolveList, start, (N-1), rci0, r0Label);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
Kenny Guyb8a2df12014-12-12 16:56:08 +00001374
1375 // Layout doesn't handle both profile button and last chosen
1376 // so disable last chosen if profile button is present.
1377 if (mOtherProfile != null && mLastChosenPosition >= 0) {
1378 mLastChosenPosition = -1;
1379 mFilterLastUsed = false;
1380 }
Adam Powell24428412015-04-01 17:19:56 -07001381
1382 onListRebuilt();
1383 }
1384
Adam Powell2ed547e2015-04-29 18:45:04 -07001385 private void addResolveListDedupe(List<ResolvedComponentInfo> into, Intent intent,
1386 List<ResolveInfo> from) {
1387 final int fromCount = from.size();
1388 final int intoCount = into.size();
1389 for (int i = 0; i < fromCount; i++) {
1390 final ResolveInfo newInfo = from.get(i);
1391 boolean found = false;
1392 // Only loop to the end of into as it was before we started; no dupes in from.
1393 for (int j = 0; j < intoCount; j++) {
1394 final ResolvedComponentInfo rci = into.get(i);
1395 if (isSameResolvedComponent(newInfo, rci)) {
1396 found = true;
1397 rci.add(intent, newInfo);
1398 break;
1399 }
1400 }
1401 if (!found) {
Adam Powell23882512016-01-29 10:21:00 -08001402 final ComponentName name = new ComponentName(
1403 newInfo.activityInfo.packageName, newInfo.activityInfo.name);
1404 final ResolvedComponentInfo rci = new ResolvedComponentInfo(name,
1405 intent, newInfo);
1406 rci.setPinned(isComponentPinned(name));
1407 into.add(rci);
Adam Powell2ed547e2015-04-29 18:45:04 -07001408 }
1409 }
1410 }
1411
1412 private boolean isSameResolvedComponent(ResolveInfo a, ResolvedComponentInfo b) {
1413 final ActivityInfo ai = a.activityInfo;
1414 return ai.packageName.equals(b.name.getPackageName())
1415 && ai.name.equals(b.name.getClassName());
1416 }
1417
Adam Powell24428412015-04-01 17:19:56 -07001418 public void onListRebuilt() {
1419 // This space for rent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421
Adam Powellc6d5e3a2015-04-23 12:22:20 -07001422 public boolean shouldGetResolvedFilter() {
1423 return mFilterLastUsed;
1424 }
1425
Adam Powell2ed547e2015-04-29 18:45:04 -07001426 private void processGroup(List<ResolvedComponentInfo> rList, int start, int end,
1427 ResolvedComponentInfo ro, CharSequence roLabel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 // Process labels from start to i
1429 int num = end - start+1;
1430 if (num == 1) {
1431 // No duplicate labels. Use label for entry at start
Adam Powell2ed547e2015-04-29 18:45:04 -07001432 addResolveInfoWithAlternates(ro, null, roLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 } else {
Adam Powell24428412015-04-01 17:19:56 -07001434 mHasExtendedInfo = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 boolean usePkg = false;
Adam Powell00f4aad2015-09-17 13:38:16 -07001436 final ApplicationInfo ai = ro.getResolveInfoAt(0).activityInfo.applicationInfo;
1437 final CharSequence startApp = ai.loadLabel(mPm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 if (startApp == null) {
1439 usePkg = true;
1440 }
1441 if (!usePkg) {
1442 // Use HashSet to track duplicates
1443 HashSet<CharSequence> duplicates =
1444 new HashSet<CharSequence>();
1445 duplicates.add(startApp);
1446 for (int j = start+1; j <= end ; j++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001447 ResolveInfo jRi = rList.get(j).getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm);
1449 if ( (jApp == null) || (duplicates.contains(jApp))) {
1450 usePkg = true;
1451 break;
1452 } else {
1453 duplicates.add(jApp);
1454 }
1455 }
1456 // Clear HashSet for later use
1457 duplicates.clear();
1458 }
1459 for (int k = start; k <= end; k++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001460 final ResolvedComponentInfo rci = rList.get(k);
1461 final ResolveInfo add = rci.getResolveInfoAt(0);
1462 final CharSequence extraInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 if (usePkg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 // Use package name for all entries from start to end-1
Adam Powell2ed547e2015-04-29 18:45:04 -07001465 extraInfo = add.activityInfo.packageName;
1466 } else {
1467 // Use application name for all entries from start to end-1
1468 extraInfo = add.activityInfo.applicationInfo.loadLabel(mPm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001470 addResolveInfoWithAlternates(rci, extraInfo, roLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
1472 }
1473 }
1474
Adam Powell2ed547e2015-04-29 18:45:04 -07001475 private void addResolveInfoWithAlternates(ResolvedComponentInfo rci,
1476 CharSequence extraInfo, CharSequence roLabel) {
1477 final int count = rci.getCount();
1478 final Intent intent = rci.getIntentAt(0);
1479 final ResolveInfo add = rci.getResolveInfoAt(0);
1480 final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent);
1481 final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel,
1482 extraInfo, replaceIntent);
Adam Powell23882512016-01-29 10:21:00 -08001483 dri.setPinned(rci.isPinned());
Adam Powell2ed547e2015-04-29 18:45:04 -07001484 addResolveInfo(dri);
1485 if (replaceIntent == intent) {
1486 // Only add alternates if we didn't get a specific replacement from
1487 // the caller. If we have one it trumps potential alternates.
1488 for (int i = 1, N = count; i < N; i++) {
1489 final Intent altIntent = rci.getIntentAt(i);
1490 dri.addAlternateSourceIntent(altIntent);
1491 }
1492 }
1493 updateLastChosenPosition(add);
1494 }
1495
Esteban Talavera6de72662014-12-11 17:54:07 +00001496 private void updateLastChosenPosition(ResolveInfo info) {
1497 if (mLastChosen != null
1498 && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName)
1499 && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001500 mLastChosenPosition = mDisplayList.size() - 1;
Esteban Talavera6de72662014-12-11 17:54:07 +00001501 }
1502 }
1503
Adam Powell88831a22014-11-20 18:17:00 -08001504 private void addResolveInfo(DisplayResolveInfo dri) {
Adam Powell24428412015-04-01 17:19:56 -07001505 if (dri.mResolveInfo.targetUserId != UserHandle.USER_CURRENT && mOtherProfile == null) {
Adam Powell88831a22014-11-20 18:17:00 -08001506 // So far we only support a single other profile at a time.
1507 // The first one we see gets special treatment.
1508 mOtherProfile = dri;
1509 } else {
Adam Powell2ed547e2015-04-29 18:45:04 -07001510 mDisplayList.add(dri);
Adam Powell88831a22014-11-20 18:17:00 -08001511 }
1512 }
1513
Adam Powell278902c2014-07-12 18:33:22 -07001514 public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001515 return (filtered ? getItem(position) : mDisplayList.get(position))
1516 .getResolveInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
1518
Adam Powell24428412015-04-01 17:19:56 -07001519 public TargetInfo targetInfoForPosition(int position, boolean filtered) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001520 return filtered ? getItem(position) : mDisplayList.get(position);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 }
1522
1523 public int getCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001524 int result = mDisplayList.size();
Adam Powell278902c2014-07-12 18:33:22 -07001525 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1526 result--;
1527 }
1528 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 }
1530
Adam Powell50077352015-05-26 18:01:55 -07001531 public int getUnfilteredCount() {
1532 return mDisplayList.size();
1533 }
1534
1535 public int getDisplayInfoCount() {
1536 return mDisplayList.size();
1537 }
1538
1539 public DisplayResolveInfo getDisplayInfoAt(int index) {
1540 return mDisplayList.get(index);
1541 }
1542
Adam Powell24428412015-04-01 17:19:56 -07001543 public TargetInfo getItem(int position) {
Adam Powell278902c2014-07-12 18:33:22 -07001544 if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) {
1545 position++;
1546 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001547 return mDisplayList.get(position);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
1549
1550 public long getItemId(int position) {
1551 return position;
1552 }
1553
Adam Powell24428412015-04-01 17:19:56 -07001554 public boolean hasExtendedInfo() {
1555 return mHasExtendedInfo;
1556 }
1557
1558 public boolean hasResolvedTarget(ResolveInfo info) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001559 for (int i = 0, N = mDisplayList.size(); i < N; i++) {
Adam Powellc412be62015-06-24 13:54:10 -07001560 if (resolveInfoMatch(info, mDisplayList.get(i).getResolveInfo())) {
Adam Powell24428412015-04-01 17:19:56 -07001561 return true;
1562 }
1563 }
1564 return false;
1565 }
1566
Adam Powell23882512016-01-29 10:21:00 -08001567 public int getDisplayResolveInfoCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001568 return mDisplayList.size();
Adam Powell24428412015-04-01 17:19:56 -07001569 }
1570
Adam Powell23882512016-01-29 10:21:00 -08001571 public DisplayResolveInfo getDisplayResolveInfo(int index) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001572 // Used to query services. We only query services for primary targets, not alternates.
1573 return mDisplayList.get(index);
Adam Powell24428412015-04-01 17:19:56 -07001574 }
1575
1576 public final View getView(int position, View convertView, ViewGroup parent) {
Adam Powellfd1e93d2014-09-07 16:52:22 -07001577 View view = convertView;
1578 if (view == null) {
Adam Powell24428412015-04-01 17:19:56 -07001579 view = createView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
Adam Powell7d758002015-05-06 17:49:36 -07001581 onBindView(view, getItem(position));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 return view;
1583 }
1584
Adam Powell7d758002015-05-06 17:49:36 -07001585 public final View createView(ViewGroup parent) {
1586 final View view = onCreateView(parent);
1587 final ViewHolder holder = new ViewHolder(view);
1588 view.setTag(holder);
1589 return view;
1590 }
1591
1592 public View onCreateView(ViewGroup parent) {
Adam Powell24428412015-04-01 17:19:56 -07001593 return mInflater.inflate(
1594 com.android.internal.R.layout.resolve_list_item, parent, false);
1595 }
1596
1597 public boolean showsExtendedInfo(TargetInfo info) {
1598 return !TextUtils.isEmpty(info.getExtendedInfo());
1599 }
1600
Adam Powell23882512016-01-29 10:21:00 -08001601 public boolean isComponentPinned(ComponentName name) {
1602 return false;
1603 }
1604
Adam Powell7d758002015-05-06 17:49:36 -07001605 public final void bindView(int position, View view) {
1606 onBindView(view, getItem(position));
1607 }
1608
1609 private void onBindView(View view, TargetInfo info) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001610 final ViewHolder holder = (ViewHolder) view.getTag();
Adam Powell63b31692015-09-28 10:45:00 -07001611 final CharSequence label = info.getDisplayLabel();
1612 if (!TextUtils.equals(holder.text.getText(), label)) {
1613 holder.text.setText(info.getDisplayLabel());
1614 }
Adam Powell24428412015-04-01 17:19:56 -07001615 if (showsExtendedInfo(info)) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001616 holder.text2.setVisibility(View.VISIBLE);
Adam Powell24428412015-04-01 17:19:56 -07001617 holder.text2.setText(info.getExtendedInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 } else {
Adam Powell0256c6f2013-05-29 16:42:33 -07001619 holder.text2.setVisibility(View.GONE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 }
Adam Powell24428412015-04-01 17:19:56 -07001621 if (info instanceof DisplayResolveInfo
1622 && !((DisplayResolveInfo) info).hasDisplayIcon()) {
1623 new LoadAdapterIconTask((DisplayResolveInfo) info).execute();
Dianne Hackborneb034652009-09-07 00:49:58 -07001624 }
Adam Powell24428412015-04-01 17:19:56 -07001625 holder.icon.setImageDrawable(info.getDisplayIcon());
Adam Powell7d758002015-05-06 17:49:36 -07001626 if (holder.badge != null) {
1627 final Drawable badge = info.getBadgeIcon();
1628 if (badge != null) {
1629 holder.badge.setImageDrawable(badge);
Alan Viverettece5d92c2015-07-31 16:46:56 -04001630 holder.badge.setContentDescription(info.getBadgeContentDescription());
Adam Powell7d758002015-05-06 17:49:36 -07001631 holder.badge.setVisibility(View.VISIBLE);
1632 } else {
1633 holder.badge.setVisibility(View.GONE);
1634 }
1635 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001636 }
1637 }
1638
Adam Powell2ed547e2015-04-29 18:45:04 -07001639 static final class ResolvedComponentInfo {
1640 public final ComponentName name;
Adam Powell23882512016-01-29 10:21:00 -08001641 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001642 private final List<Intent> mIntents = new ArrayList<>();
1643 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
1644
1645 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1646 this.name = name;
1647 add(intent, info);
1648 }
1649
1650 public void add(Intent intent, ResolveInfo info) {
1651 mIntents.add(intent);
1652 mResolveInfos.add(info);
1653 }
1654
1655 public int getCount() {
1656 return mIntents.size();
1657 }
1658
1659 public Intent getIntentAt(int index) {
1660 return index >= 0 ? mIntents.get(index) : null;
1661 }
1662
1663 public ResolveInfo getResolveInfoAt(int index) {
1664 return index >= 0 ? mResolveInfos.get(index) : null;
1665 }
1666
1667 public int findIntent(Intent intent) {
1668 for (int i = 0, N = mIntents.size(); i < N; i++) {
1669 if (intent.equals(mIntents.get(i))) {
1670 return i;
1671 }
1672 }
1673 return -1;
1674 }
1675
1676 public int findResolveInfo(ResolveInfo info) {
1677 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1678 if (info.equals(mResolveInfos.get(i))) {
1679 return i;
1680 }
1681 }
1682 return -1;
1683 }
Adam Powell23882512016-01-29 10:21:00 -08001684
1685 public boolean isPinned() {
1686 return mPinned;
1687 }
1688
1689 public void setPinned(boolean pinned) {
1690 mPinned = pinned;
1691 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001692 }
1693
Adam Powell0256c6f2013-05-29 16:42:33 -07001694 static class ViewHolder {
1695 public TextView text;
1696 public TextView text2;
1697 public ImageView icon;
Adam Powell7d758002015-05-06 17:49:36 -07001698 public ImageView badge;
Adam Powell0256c6f2013-05-29 16:42:33 -07001699
1700 public ViewHolder(View view) {
1701 text = (TextView) view.findViewById(com.android.internal.R.id.text1);
1702 text2 = (TextView) view.findViewById(com.android.internal.R.id.text2);
1703 icon = (ImageView) view.findViewById(R.id.icon);
Adam Powell7d758002015-05-06 17:49:36 -07001704 badge = (ImageView) view.findViewById(R.id.target_badge);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 }
1706 }
1707
Adam Powell7d758002015-05-06 17:49:36 -07001708 class ItemClickListener implements AdapterView.OnItemClickListener,
1709 AdapterView.OnItemLongClickListener {
1710 @Override
1711 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1712 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1713 if (listView != null) {
1714 position -= listView.getHeaderViewsCount();
1715 }
1716 if (position < 0) {
1717 // Header views don't count.
1718 return;
1719 }
1720 final int checkedPos = mAdapterView.getCheckedItemPosition();
1721 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
1722 if (mAlwaysUseOption && (!hasValidSelection || mLastSelected != checkedPos)) {
1723 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
1724 mOnceButton.setEnabled(hasValidSelection);
1725 if (hasValidSelection) {
1726 mAdapterView.smoothScrollToPosition(checkedPos);
1727 }
1728 mLastSelected = checkedPos;
1729 } else {
1730 startSelected(position, false, true);
1731 }
1732 }
Adam Powell2d809622012-03-22 15:24:43 -07001733
1734 @Override
1735 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001736 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1737 if (listView != null) {
1738 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001739 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001740 if (position < 0) {
1741 // Header views don't count.
1742 return false;
1743 }
Adam Powell278902c2014-07-12 18:33:22 -07001744 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001745 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001746 return true;
1747 }
1748
1749 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001750
Adam Powell24428412015-04-01 17:19:56 -07001751 abstract class LoadIconTask extends AsyncTask<Void, Void, Drawable> {
1752 protected final DisplayResolveInfo mDisplayResolveInfo;
1753 private final ResolveInfo mResolveInfo;
1754
1755 public LoadIconTask(DisplayResolveInfo dri) {
1756 mDisplayResolveInfo = dri;
1757 mResolveInfo = dri.getResolveInfo();
Adam Powell0256c6f2013-05-29 16:42:33 -07001758 }
1759
1760 @Override
Adam Powell24428412015-04-01 17:19:56 -07001761 protected Drawable doInBackground(Void... params) {
1762 return loadIconForResolveInfo(mResolveInfo);
1763 }
1764
1765 @Override
1766 protected void onPostExecute(Drawable d) {
1767 mDisplayResolveInfo.setDisplayIcon(d);
1768 }
1769 }
1770
1771 class LoadAdapterIconTask extends LoadIconTask {
1772 public LoadAdapterIconTask(DisplayResolveInfo dri) {
1773 super(dri);
1774 }
1775
1776 @Override
1777 protected void onPostExecute(Drawable d) {
1778 super.onPostExecute(d);
1779 if (mProfileView != null && mAdapter.getOtherProfile() == mDisplayResolveInfo) {
Adam Powell88831a22014-11-20 18:17:00 -08001780 bindProfileView();
1781 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001782 mAdapter.notifyDataSetChanged();
1783 }
1784 }
Adam Powell278902c2014-07-12 18:33:22 -07001785
Adam Powell24428412015-04-01 17:19:56 -07001786 class LoadIconIntoViewTask extends LoadIconTask {
1787 private final ImageView mTargetView;
Adam Powell278902c2014-07-12 18:33:22 -07001788
Adam Powell24428412015-04-01 17:19:56 -07001789 public LoadIconIntoViewTask(DisplayResolveInfo dri, ImageView target) {
1790 super(dri);
Adam Powell278902c2014-07-12 18:33:22 -07001791 mTargetView = target;
1792 }
1793
1794 @Override
Adam Powell24428412015-04-01 17:19:56 -07001795 protected void onPostExecute(Drawable d) {
1796 super.onPostExecute(d);
1797 mTargetView.setImageDrawable(d);
Adam Powell278902c2014-07-12 18:33:22 -07001798 }
1799 }
Adam Powell09a65602014-07-20 16:23:14 -07001800
Dianne Hackbornec452d92014-11-11 17:16:56 -08001801 static final boolean isSpecificUriMatch(int match) {
1802 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1803 return match >= IntentFilter.MATCH_CATEGORY_HOST
1804 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1805 }
1806
Adam Powell4c470d62015-06-19 17:46:17 -07001807 static class PickTargetOptionRequest extends PickOptionRequest {
1808 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1809 @Nullable Bundle extras) {
1810 super(prompt, options, extras);
1811 }
1812
1813 @Override
1814 public void onCancel() {
1815 super.onCancel();
1816 final ResolverActivity ra = (ResolverActivity) getActivity();
1817 if (ra != null) {
1818 ra.mPickOptionRequest = null;
1819 ra.finish();
1820 }
1821 }
1822
1823 @Override
1824 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1825 super.onPickOptionResult(finished, selections, result);
1826 if (selections.length != 1) {
1827 // TODO In a better world we would filter the UI presented here and let the
1828 // user refine. Maybe later.
1829 return;
1830 }
1831
1832 final ResolverActivity ra = (ResolverActivity) getActivity();
1833 if (ra != null) {
1834 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
1835 if (ra.onTargetSelected(ti, false)) {
1836 ra.mPickOptionRequest = null;
1837 ra.finish();
1838 }
1839 }
1840 }
1841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842}