blob: 40142173ece38cfbbbd368ff04b5cc3e0ea103b6 [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;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -070020import android.annotation.StringRes;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080021import android.annotation.UiThread;
Adam Powelle9414d92014-07-05 17:44:18 -070022import android.app.Activity;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070023import android.app.ActivityThread;
Adam Powell4c470d62015-06-19 17:46:17 -070024import android.app.VoiceInteractor.PickOptionRequest;
25import android.app.VoiceInteractor.PickOptionRequest.Option;
26import android.app.VoiceInteractor.Prompt;
Adam Powell0256c6f2013-05-29 16:42:33 -070027import android.os.AsyncTask;
Kang Li53b43142016-11-14 14:38:25 -080028import android.os.RemoteException;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -070029import android.provider.MediaStore;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070030import android.provider.Settings;
Adam Powell11f59a02014-08-20 13:22:16 -070031import android.text.TextUtils;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070032import android.util.Slog;
Adam Powell278902c2014-07-12 18:33:22 -070033import android.widget.AbsListView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import com.android.internal.R;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080035import com.android.internal.annotations.VisibleForTesting;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -080036import com.android.internal.content.PackageMonitor;
37
Adam Powellc5878612012-05-04 18:42:38 -070038import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.ComponentName;
40import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.Intent;
42import android.content.IntentFilter;
43import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010044import android.content.pm.ApplicationInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -070045import android.content.pm.LabeledIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010049import android.content.pm.UserInfo;
Adam Powellc5878612012-05-04 18:42:38 -070050import android.content.res.Resources;
Dianne Hackborneb034652009-09-07 00:49:58 -070051import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.net.Uri;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010053import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Bundle;
55import android.os.PatternMatcher;
Jeff Sharkey37355a92016-02-05 16:19:10 -070056import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070057import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010058import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.util.Log;
Adam Powell2d809622012-03-22 15:24:43 -070060import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.View;
62import android.view.ViewGroup;
Adam Powell2d809622012-03-22 15:24:43 -070063import android.widget.AdapterView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.widget.BaseAdapter;
Adam Powellc5878612012-05-04 18:42:38 -070065import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070067import android.widget.ListView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010069import android.widget.Toast;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010070
71import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010072import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070073import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010076import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.util.HashSet;
78import java.util.Iterator;
79import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070080import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.util.Set;
82
Wale Ogunwale9014e662016-03-19 14:55:46 -070083import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Adrian Roos27c3ab62014-10-15 17:21:31 +020084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085/**
86 * This activity is displayed when the system attempts to start an Intent for
87 * which there is more than one matching activity, allowing the user to decide
88 * which to go to. It is not normally used directly by application developers.
89 */
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080090@UiThread
Adam Powell7d758002015-05-06 17:49:36 -070091public class ResolverActivity extends Activity {
Adam Powellc5878612012-05-04 18:42:38 -070092
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080093 protected ResolveListAdapter mAdapter;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070094 private boolean mSafeForwardingMode;
Adam Powellc5878612012-05-04 18:42:38 -070095 private boolean mAlwaysUseOption;
Adam Powell24428412015-04-01 17:19:56 -070096 private AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -070097 private Button mAlwaysButton;
98 private Button mOnceButton;
Adam Powell88831a22014-11-20 18:17:00 -080099 private View mProfileView;
Adam Powellc5878612012-05-04 18:42:38 -0700100 private int mIconDpi;
Adam Powell24428412015-04-01 17:19:56 -0700101 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100102 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000103 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800104 private int mLayoutId;
Adam Powell2ed547e2015-04-29 18:45:04 -0700105 private final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700106 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800107 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800108 private CharSequence mTitle;
109 private int mDefaultTitleResId;
Adam Powell09a65602014-07-20 16:23:14 -0700110
Adam Powell63b31692015-09-28 10:45:00 -0700111 protected ResolverDrawerLayout mResolverDrawerLayout;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800112 protected PackageManager mPm;
113 protected int mLaunchedFromUid;
114
115 private static final String TAG = "ResolverActivity";
116 private static final boolean DEBUG = false;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800117 private Runnable mPostListBuildRunnable;
Adam Powell63b31692015-09-28 10:45:00 -0700118
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700119 private boolean mRegistered;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800120 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
121 @Override public void onSomePackagesChanged() {
122 mAdapter.handlePackagesChanged();
Adam Powell88831a22014-11-20 18:17:00 -0800123 if (mProfileView != null) {
124 bindProfileView();
125 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800126 }
127 };
128
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700129 /**
130 * Get the string resource to be used as a label for the link to the resolver activity for an
131 * action.
132 *
133 * @param action The action to resolve
134 *
135 * @return The string resource to be used as a label
136 */
137 public static @StringRes int getLabelRes(String action) {
138 return ActionTitle.forAction(action).labelRes;
139 }
140
Adam Powell278902c2014-07-12 18:33:22 -0700141 private enum ActionTitle {
142 VIEW(Intent.ACTION_VIEW,
143 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700144 com.android.internal.R.string.whichViewApplicationNamed,
145 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700146 EDIT(Intent.ACTION_EDIT,
147 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700148 com.android.internal.R.string.whichEditApplicationNamed,
149 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700150 SEND(Intent.ACTION_SEND,
151 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700152 com.android.internal.R.string.whichSendApplicationNamed,
153 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700154 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700155 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700156 com.android.internal.R.string.whichSendToApplicationNamed,
157 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700158 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
159 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700160 com.android.internal.R.string.whichSendApplicationNamed,
161 com.android.internal.R.string.whichSendApplicationLabel),
162 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
163 com.android.internal.R.string.whichImageCaptureApplication,
164 com.android.internal.R.string.whichImageCaptureApplicationNamed,
165 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700166 DEFAULT(null,
167 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700168 com.android.internal.R.string.whichApplicationNamed,
169 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700170 HOME(Intent.ACTION_MAIN,
171 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700172 com.android.internal.R.string.whichHomeApplicationNamed,
173 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700174
175 public final String action;
176 public final int titleRes;
177 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700178 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700179
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700180 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700181 this.action = action;
182 this.titleRes = titleRes;
183 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700184 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700185 }
186
187 public static ActionTitle forAction(String action) {
188 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700189 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700190 return title;
191 }
192 }
193 return DEFAULT;
194 }
195 }
196
Dianne Hackborn905577f2011-09-07 18:31:28 -0700197 private Intent makeMyIntent() {
198 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700199 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700200 // The resolver activity is set to be hidden from recent tasks.
201 // we don't want this attribute to be propagated to the next activity
202 // being launched. Note that if the original Intent also had this
203 // flag set, we are now losing it. That should be a very rare case
204 // and we can live with this.
205 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
206 return intent;
207 }
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 @Override
210 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700211 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700212 final Intent intent = makeMyIntent();
213 final Set<String> categories = intent.getCategories();
214 if (Intent.ACTION_MAIN.equals(intent.getAction())
215 && categories != null
216 && categories.size() == 1
217 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100218 // Note: this field is not set to true in the compatibility version.
219 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700220 }
221
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700222 setSafeForwardingMode(true);
223
Adam Powella35c77a2014-09-25 16:46:36 -0700224 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 }
226
Adam Powell278902c2014-07-12 18:33:22 -0700227 /**
Adam Powell24428412015-04-01 17:19:56 -0700228 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700229 * a default title resource
230 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700232 CharSequence title, Intent[] initialIntents,
233 List<ResolveInfo> rList, boolean alwaysUseOption) {
Adam Powell09a65602014-07-20 16:23:14 -0700234 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, alwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700235 }
236
237 protected void onCreate(Bundle savedInstanceState, Intent intent,
238 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
239 List<ResolveInfo> rList, boolean alwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700240 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000242
243 // Determine whether we should show that intent is forwarded
244 // from managed profile to owner or other way around.
245 setProfileSwitchMessageId(intent.getContentUserHint());
246
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700247 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800248 mLaunchedFromUid = ActivityManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700249 getActivityToken());
250 } catch (RemoteException e) {
251 mLaunchedFromUid = -1;
252 }
Adam Powell7d758002015-05-06 17:49:36 -0700253
254 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
255 // Gulp!
256 finish();
257 return;
258 }
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700261
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700262 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700263 mRegistered = true;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800264 mReferrerPackage = getReferrerPackageName();
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800265 mAlwaysUseOption = alwaysUseOption;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800266
Adam Powellc5878612012-05-04 18:42:38 -0700267 final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
268 mIconDpi = am.getLauncherLargeIconDensity();
Adam Powellc5878612012-05-04 18:42:38 -0700269
Adam Powell7d758002015-05-06 17:49:36 -0700270 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700271 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800272 mTitle = title;
273 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700274
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800275 if (configureContentView(mIntents, initialIntents, rList)) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700276 return;
277 }
Adam Powell278902c2014-07-12 18:33:22 -0700278
Adam Powell4f6c2052014-07-07 18:49:10 -0700279 final ResolverDrawerLayout rdl = (ResolverDrawerLayout) findViewById(R.id.contentPanel);
280 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700281 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700282 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700283 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700284 finish();
285 }
286 });
Adam Powell4c470d62015-06-19 17:46:17 -0700287 if (isVoiceInteraction()) {
288 rdl.setCollapsed(false);
289 }
Adam Powell63b31692015-09-28 10:45:00 -0700290 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700291 }
292
Adam Powell88831a22014-11-20 18:17:00 -0800293 mProfileView = findViewById(R.id.profile_button);
294 if (mProfileView != null) {
295 mProfileView.setOnClickListener(new View.OnClickListener() {
296 @Override
297 public void onClick(View v) {
298 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
299 if (dri == null) {
300 return;
301 }
302
Sander Alewijnse053d3dd2015-03-09 15:31:10 +0000303 // Do not show the profile switch message anymore.
304 mProfileSwitchMessageId = -1;
305
Adam Powell24428412015-04-01 17:19:56 -0700306 onTargetSelected(dri, false);
Adam Powell88831a22014-11-20 18:17:00 -0800307 finish();
308 }
309 });
310 bindProfileView();
311 }
Adam Powell4c470d62015-06-19 17:46:17 -0700312
313 if (isVoiceInteraction()) {
314 onSetupVoiceInteraction();
315 }
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100316 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100317 MetricsLogger.action(this, mAdapter.hasFilteredItem()
318 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
319 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
320 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100321 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700322 }
323
324 /**
325 * Perform any initialization needed for voice interaction.
326 */
Adam Powell23882512016-01-29 10:21:00 -0800327 public void onSetupVoiceInteraction() {
Adam Powell4c470d62015-06-19 17:46:17 -0700328 // Do it right now. Subclasses may delay this and send it later.
329 sendVoiceChoicesIfNeeded();
330 }
331
Adam Powell23882512016-01-29 10:21:00 -0800332 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700333 if (!isVoiceInteraction()) {
334 // Clearly not needed.
335 return;
336 }
337
338
339 final Option[] options = new Option[mAdapter.getCount()];
340 for (int i = 0, N = options.length; i < N; i++) {
341 options[i] = optionForChooserTarget(mAdapter.getItem(i), i);
342 }
343
344 mPickOptionRequest = new PickTargetOptionRequest(
345 new Prompt(getTitle()), options, null);
346 getVoiceInteractor().submitRequest(mPickOptionRequest);
347 }
348
349 Option optionForChooserTarget(TargetInfo target, int index) {
350 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800351 }
352
Adam Powell2ed547e2015-04-29 18:45:04 -0700353 protected final void setAdditionalTargets(Intent[] intents) {
354 if (intents != null) {
355 for (Intent intent : intents) {
356 mIntents.add(intent);
357 }
358 }
359 }
360
Adam Powell0ccc0e92015-04-23 17:19:37 -0700361 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700362 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700363 }
364
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800365 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700366 final Uri referrer = getReferrer();
367 if (referrer != null && "android-app".equals(referrer.getScheme())) {
368 return referrer.getHost();
369 }
370 return null;
371 }
372
Adam Powell23882512016-01-29 10:21:00 -0800373 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700374 return R.layout.resolver_list;
375 }
376
Adam Powell88831a22014-11-20 18:17:00 -0800377 void bindProfileView() {
378 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
379 if (dri != null) {
380 mProfileView.setVisibility(View.VISIBLE);
Nicolas Prevot256423f2016-02-09 16:33:57 +0000381 final TextView text = (TextView) mProfileView.findViewById(R.id.profile_button);
Adam Powell24428412015-04-01 17:19:56 -0700382 text.setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800383 } else {
384 mProfileView.setVisibility(View.GONE);
385 }
Adam Powell278902c2014-07-12 18:33:22 -0700386 }
387
Sander Alewijnsef6545332014-10-31 12:39:02 +0000388 private void setProfileSwitchMessageId(int contentUserHint) {
389 if (contentUserHint != UserHandle.USER_CURRENT &&
390 contentUserHint != UserHandle.myUserId()) {
391 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
392 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
393 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
394 : false;
395 boolean targetIsManaged = userManager.isManagedProfile();
396 if (originIsManaged && !targetIsManaged) {
397 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
398 } else if (!originIsManaged && targetIsManaged) {
399 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
400 }
401 }
402 }
403
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700404 /**
405 * Turn on launch mode that is safe to use when forwarding intents received from
406 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
407 * instead of the normal Activity.startActivity for launching the activity selected
408 * by the user.
409 *
410 * <p>This mode is set to true by default if the activity is initialized through
411 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
412 * methods, it is set to false by default. You must set it before calling one of the
413 * more detailed onCreate methods, so that it will be set correctly in the case where
414 * there is only one intent to resolve and it is thus started immediately.</p>
415 */
416 public void setSafeForwardingMode(boolean safeForwarding) {
417 mSafeForwardingMode = safeForwarding;
418 }
419
Adam Powell278902c2014-07-12 18:33:22 -0700420 protected CharSequence getTitleForAction(String action, int defaultTitleRes) {
Adam Powella35c77a2014-09-25 16:46:36 -0700421 final ActionTitle title = mResolvingHome ? ActionTitle.HOME : ActionTitle.forAction(action);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800422 // While there may already be a filtered item, we can only use it in the title if the list
423 // is already sorted and all information relevant to it is already in the list.
424 final boolean named = mAdapter.getFilteredPosition() > 0;
Adam Powell278902c2014-07-12 18:33:22 -0700425 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
426 return getString(defaultTitleRes);
427 } else {
Adam Powell24428412015-04-01 17:19:56 -0700428 return named
429 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
430 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700431 }
Adam Powellc5878612012-05-04 18:42:38 -0700432 }
433
Adam Powelle9414d92014-07-05 17:44:18 -0700434 void dismiss() {
435 if (!isFinishing()) {
436 finish();
437 }
438 }
439
Adam Powellc5878612012-05-04 18:42:38 -0700440 Drawable getIcon(Resources res, int resId) {
441 Drawable result;
442 try {
443 result = res.getDrawableForDensity(resId, mIconDpi);
444 } catch (Resources.NotFoundException e) {
445 result = null;
446 }
447
448 return result;
449 }
450
451 Drawable loadIconForResolveInfo(ResolveInfo ri) {
452 Drawable dr;
453 try {
454 if (ri.resolvePackageName != null && ri.icon != 0) {
455 dr = getIcon(mPm.getResourcesForApplication(ri.resolvePackageName), ri.icon);
456 if (dr != null) {
457 return dr;
458 }
459 }
460 final int iconRes = ri.getIconResource();
461 if (iconRes != 0) {
462 dr = getIcon(mPm.getResourcesForApplication(ri.activityInfo.packageName), iconRes);
463 if (dr != null) {
464 return dr;
465 }
466 }
467 } catch (NameNotFoundException e) {
468 Log.e(TAG, "Couldn't find resources for package", e);
469 }
470 return ri.loadIcon(mPm);
471 }
472
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800473 @Override
474 protected void onRestart() {
475 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700476 if (!mRegistered) {
477 mPackageMonitor.register(this, getMainLooper(), false);
478 mRegistered = true;
479 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800480 mAdapter.handlePackagesChanged();
Adam Powell88831a22014-11-20 18:17:00 -0800481 if (mProfileView != null) {
482 bindProfileView();
483 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800484 }
485
486 @Override
487 protected void onStop() {
488 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700489 if (mRegistered) {
490 mPackageMonitor.unregister();
491 mRegistered = false;
492 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700493 final Intent intent = getIntent();
494 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
495 && !mResolvingHome) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700496 // This resolver is in the unusual situation where it has been
497 // launched at the top of a new task. We don't let it be added
498 // to the recent tasks shown to the user, and we need to make sure
499 // that each time we are launched we get the correct launching
500 // uid (not re-using the same resolver from an old launching uid),
501 // so we will now finish ourself since being no longer visible,
502 // the user probably can't get back to us.
503 if (!isChangingConfigurations()) {
504 finish();
505 }
506 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800507 }
508
Adam Powellc5878612012-05-04 18:42:38 -0700509 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700510 protected void onDestroy() {
511 super.onDestroy();
512 if (!isChangingConfigurations() && mPickOptionRequest != null) {
513 mPickOptionRequest.cancel();
514 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800515 if (mPostListBuildRunnable != null) {
516 getMainThreadHandler().removeCallbacks(mPostListBuildRunnable);
517 }
Adam Powell4c470d62015-06-19 17:46:17 -0700518 }
519
520 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700521 protected void onRestoreInstanceState(Bundle savedInstanceState) {
522 super.onRestoreInstanceState(savedInstanceState);
523 if (mAlwaysUseOption) {
Adam Powell24428412015-04-01 17:19:56 -0700524 final int checkedPos = mAdapterView.getCheckedItemPosition();
Nicolas Prevot50449882014-06-23 12:42:37 +0100525 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Adam Powelld81cc4a2012-07-19 13:51:39 -0700526 mLastSelected = checkedPos;
Adam Powell278902c2014-07-12 18:33:22 -0700527 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Nicolas Prevot50449882014-06-23 12:42:37 +0100528 mOnceButton.setEnabled(hasValidSelection);
529 if (hasValidSelection) {
Adam Powell24428412015-04-01 17:19:56 -0700530 mAdapterView.setSelection(checkedPos);
Adam Powell9bee4662012-05-08 11:07:23 -0700531 }
532 }
533 }
534
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100535 private boolean hasManagedProfile() {
536 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
537 if (userManager == null) {
538 return false;
539 }
540
541 try {
542 List<UserInfo> profiles = userManager.getProfiles(getUserId());
543 for (UserInfo userInfo : profiles) {
544 if (userInfo != null && userInfo.isManagedProfile()) {
545 return true;
546 }
547 }
548 } catch (SecurityException e) {
549 return false;
550 }
551 return false;
552 }
553
554 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
555 try {
556 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
557 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700558 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100559 } catch (NameNotFoundException e) {
560 return false;
561 }
562 }
563
Adam Powell278902c2014-07-12 18:33:22 -0700564 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
565 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100566 boolean enabled = false;
567 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700568 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Nicolas Prevot50449882014-06-23 12:42:37 +0100569 if (ri.targetUserId == UserHandle.USER_CURRENT) {
570 enabled = true;
571 }
572 }
573 mAlwaysButton.setEnabled(enabled);
574 }
575
Adam Powellc5878612012-05-04 18:42:38 -0700576 public void onButtonClick(View v) {
577 final int id = v.getId();
Adam Powell278902c2014-07-12 18:33:22 -0700578 startSelected(mAlwaysUseOption ?
Adam Powell24428412015-04-01 17:19:56 -0700579 mAdapterView.getCheckedItemPosition() : mAdapter.getFilteredPosition(),
Adam Powell278902c2014-07-12 18:33:22 -0700580 id == R.id.button_always,
581 mAlwaysUseOption);
Adam Powellc5878612012-05-04 18:42:38 -0700582 }
583
Adam Powell23882512016-01-29 10:21:00 -0800584 public void startSelected(int which, boolean always, boolean filtered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700585 if (isFinishing()) {
586 return;
587 }
Adam Powell278902c2014-07-12 18:33:22 -0700588 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, filtered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000589 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
590 Toast.makeText(this, String.format(getResources().getString(
591 com.android.internal.R.string.activity_resolver_work_profiles_support),
592 ri.activityInfo.loadLabel(getPackageManager()).toString()),
593 Toast.LENGTH_LONG).show();
594 return;
595 }
596
Adam Powell24428412015-04-01 17:19:56 -0700597 TargetInfo target = mAdapter.targetInfoForPosition(which, filtered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800598 if (target == null) {
599 return;
600 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700601 if (onTargetSelected(target, always)) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100602 if (always && filtered) {
603 MetricsLogger.action(
604 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
605 } else if (filtered) {
606 MetricsLogger.action(
607 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
608 } else {
609 MetricsLogger.action(
610 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
611 }
612 MetricsLogger.action(this, mAdapter.hasFilteredItem()
613 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
614 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700615 finish();
616 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618
Adam Powelle49d9392014-07-17 18:45:19 -0700619 /**
620 * Replace me in subclasses!
621 */
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000622 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700623 return defIntent;
624 }
625
Adam Powell2ed547e2015-04-29 18:45:04 -0700626 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700627 final ResolveInfo ri = target.getResolveInfo();
628 final Intent intent = target != null ? target.getResolvedIntent() : null;
629
630 if (intent != null && (mAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800631 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700632 // Build a reasonable intent filter, based on what matched.
633 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200634 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635
Henrik Engström3277cf12014-07-17 12:18:29 +0200636 if (intent.getSelector() != null) {
637 filterIntent = intent.getSelector();
638 } else {
639 filterIntent = intent;
640 }
641
642 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800643 if (action != null) {
644 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200646 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700647 if (categories != null) {
648 for (String cat : categories) {
649 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700652 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
Adam Powell24428412015-04-01 17:19:56 -0700654 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200655 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700656 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200657 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700658 if (mimeType != null) {
659 try {
660 filter.addDataType(mimeType);
661 } catch (IntentFilter.MalformedMimeTypeException e) {
662 Log.w("ResolverActivity", e);
663 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700666 }
667 if (data != null && data.getScheme() != null) {
668 // We need the data specification if there was no type,
669 // OR if the scheme is not one of our magical "file:"
670 // or "content:" schemes (see IntentFilter for the reason).
671 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
672 || (!"file".equals(data.getScheme())
673 && !"content".equals(data.getScheme()))) {
674 filter.addDataScheme(data.getScheme());
675
676 // Look through the resolved filter to determine which part
677 // of it matched the original Intent.
678 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
679 if (pIt != null) {
680 String ssp = data.getSchemeSpecificPart();
681 while (ssp != null && pIt.hasNext()) {
682 PatternMatcher p = pIt.next();
683 if (p.match(ssp)) {
684 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
685 break;
686 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700687 }
688 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700689 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
690 if (aIt != null) {
691 while (aIt.hasNext()) {
692 IntentFilter.AuthorityEntry a = aIt.next();
693 if (a.match(data) >= 0) {
694 int port = a.getPort();
695 filter.addDataAuthority(a.getHost(),
696 port >= 0 ? Integer.toString(port) : null);
697 break;
698 }
699 }
700 }
701 pIt = ri.filter.pathsIterator();
702 if (pIt != null) {
703 String path = data.getPath();
704 while (path != null && pIt.hasNext()) {
705 PatternMatcher p = pIt.next();
706 if (p.match(path)) {
707 filter.addDataPath(p.getPath(), p.getType());
708 break;
709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 }
712 }
713 }
714
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700715 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800716 final int N = mAdapter.mUnfilteredResolveList.size();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700717 ComponentName[] set = new ComponentName[N];
718 int bestMatch = 0;
719 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800720 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700721 set[i] = new ComponentName(r.activityInfo.packageName,
722 r.activityInfo.name);
723 if (r.match > bestMatch) bestMatch = r.match;
724 }
725 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700726 final int userId = getUserId();
727 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800728
729 // Set the preferred Activity
730 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
731
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700732 if (ri.handleAllWebDataURI) {
733 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700734 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700735 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700736 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700737 }
738 } else {
739 // Update Domain Verification status
740 ComponentName cn = intent.getComponent();
741 String packageName = cn.getPackageName();
742 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800743
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700744 boolean isHttpOrHttps = (dataScheme != null) &&
745 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
746 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800747
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700748 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
749 boolean hasCategoryBrowsable = (categories != null) &&
750 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800751
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700752 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700753 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700754 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
755 userId);
756 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800757 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700758 } else {
759 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800760 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700761 } catch (RemoteException re) {
762 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
763 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 }
766 }
767
Adam Powell24428412015-04-01 17:19:56 -0700768 if (target != null) {
769 safelyStartActivity(target);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700770 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700771 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700772 }
773
Adam Powell23882512016-01-29 10:21:00 -0800774 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -0700775 // We're dispatching intents that might be coming from legacy apps, so
776 // don't kill ourselves.
777 StrictMode.disableDeathOnFileUriExposure();
778 try {
779 safelyStartActivityInternal(cti);
780 } finally {
781 StrictMode.enableDeathOnFileUriExposure();
782 }
783 }
784
785 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +0000786 // If needed, show that intent is forwarded
787 // from managed profile to owner or other way around.
788 if (mProfileSwitchMessageId != -1) {
789 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
790 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700791 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -0700792 if (cti.start(this, null)) {
793 onActivityStarted(cti);
794 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700795 return;
796 }
797 try {
Adam Powell24428412015-04-01 17:19:56 -0700798 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
799 onActivityStarted(cti);
800 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700801 } catch (RuntimeException e) {
802 String launchedFromPackage;
803 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -0800804 launchedFromPackage = ActivityManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700805 getActivityToken());
806 } catch (RemoteException e2) {
807 launchedFromPackage = "??";
808 }
809 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
810 + " package " + launchedFromPackage + ", while running in "
811 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
814
Adam Powell23882512016-01-29 10:21:00 -0800815 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -0700816 // Do nothing
817 }
818
Adam Powell23882512016-01-29 10:21:00 -0800819 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -0700820 return false;
821 }
822
Adam Powell23882512016-01-29 10:21:00 -0800823 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700824 return true;
825 }
826
Adam Powell23882512016-01-29 10:21:00 -0800827 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700828 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -0700829 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -0700830 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -0700831 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -0700832 }
833
Adam Powell23882512016-01-29 10:21:00 -0800834 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents,
Adam Powell7d758002015-05-06 17:49:36 -0700835 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
836 boolean filterLastUsed) {
837 return new ResolveListAdapter(context, payloadIntents, initialIntents, rList,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800838 launchedFromUid, filterLastUsed, createListController());
839 }
840
841 @VisibleForTesting
842 protected ResolverListController createListController() {
843 return new ResolverListController(
844 this,
845 mPm,
846 getTargetIntent(),
847 getReferrerPackageName(),
848 mLaunchedFromUid);
Adam Powell88831a22014-11-20 18:17:00 -0800849 }
850
Adam Powell39e94eb2015-09-08 17:01:49 -0700851 /**
852 * Returns true if the activity is finishing and creation should halt
853 */
Adam Powell23882512016-01-29 10:21:00 -0800854 public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents,
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800855 List<ResolveInfo> rList) {
Dianne Hackborn57dd7372015-07-27 18:11:14 -0700856 // The last argument of createAdapter is whether to do special handling
857 // of the last used choice to highlight it in the list. We need to always
858 // turn this off when running under voice interaction, since it results in
859 // a more complicated UI that the current voice interaction flow is not able
860 // to handle.
Adam Powell7d758002015-05-06 17:49:36 -0700861 mAdapter = createAdapter(this, payloadIntents, initialIntents, rList,
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800862 mLaunchedFromUid, mAlwaysUseOption && !isVoiceInteraction());
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800863 boolean rebuildCompleted = mAdapter.rebuildList();
Adam Powell7d758002015-05-06 17:49:36 -0700864
Adam Powell7d758002015-05-06 17:49:36 -0700865 if (mAdapter.hasFilteredItem()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800866 mLayoutId = R.layout.resolver_list_with_default;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800867 mAlwaysUseOption = false;
Adam Powell7d758002015-05-06 17:49:36 -0700868 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800869 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -0700870 }
Adam Powell7d758002015-05-06 17:49:36 -0700871
Adam Powell50077352015-05-26 18:01:55 -0700872 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800873
874 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
875 // we're already done, we can check if we should auto-launch immediately.
876 if (rebuildCompleted) {
877 if (count == 1 && mAdapter.getOtherProfile() == null) {
878 // Only one target, so we're a candidate to auto-launch!
879 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
880 if (shouldAutoLaunchSingleChoice(target)) {
881 safelyStartActivity(target);
882 mPackageMonitor.unregister();
883 mRegistered = false;
884 finish();
885 return true;
886 }
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -0700887 }
888 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800889
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800890 setContentView(mLayoutId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800891 mAdapterView = (AbsListView) findViewById(R.id.resolver_list);
892
893 if (count == 0 && mAdapter.mPlaceholderCount == 0) {
Adam Powell7d758002015-05-06 17:49:36 -0700894 final TextView empty = (TextView) findViewById(R.id.empty);
895 empty.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -0700896 mAdapterView.setVisibility(View.GONE);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800897 } else {
898 mAdapterView.setVisibility(View.VISIBLE);
899 onPrepareAdapterView(mAdapterView, mAdapter, mAlwaysUseOption);
Adam Powell7d758002015-05-06 17:49:36 -0700900 }
Adam Powell39e94eb2015-09-08 17:01:49 -0700901 return false;
Adam Powell7d758002015-05-06 17:49:36 -0700902 }
903
Adam Powell23882512016-01-29 10:21:00 -0800904 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter,
Adam Powell7d758002015-05-06 17:49:36 -0700905 boolean alwaysUseOption) {
906 final boolean useHeader = adapter.hasFilteredItem();
907 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
908
909 adapterView.setAdapter(mAdapter);
910
911 final ItemClickListener listener = new ItemClickListener();
912 adapterView.setOnItemClickListener(listener);
913 adapterView.setOnItemLongClickListener(listener);
914
915 if (alwaysUseOption) {
916 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
917 }
918
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800919 // In case this method is called again (due to activity recreation), avoid adding a new
920 // header if one is already present.
921 if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) {
Adam Powell7d758002015-05-06 17:49:36 -0700922 listView.addHeaderView(LayoutInflater.from(this).inflate(
923 R.layout.resolver_different_item_header, listView, false));
924 }
Adam Powell24428412015-04-01 17:19:56 -0700925 }
926
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800927 public void setTitleAndIcon() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800928 if (mAdapter.getCount() == 0 && mAdapter.mPlaceholderCount == 0) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800929 final TextView titleView = (TextView) findViewById(R.id.title);
930 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800931 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800932 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800933 }
934
935 CharSequence title = mTitle != null
936 ? mTitle
937 : getTitleForAction(getTargetIntent().getAction(), mDefaultTitleResId);
938
939 if (!TextUtils.isEmpty(title)) {
940 final TextView titleView = (TextView) findViewById(R.id.title);
941 if (titleView != null) {
942 titleView.setText(title);
943 }
944 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800945
946 // Try to initialize the title icon if we have a view for it and a title to match
947 final ImageView titleIcon = (ImageView) findViewById(R.id.title_icon);
948 if (titleIcon != null) {
949 ApplicationInfo ai = null;
950 try {
951 if (!TextUtils.isEmpty(mReferrerPackage)) {
952 ai = mPm.getApplicationInfo(mReferrerPackage, 0);
953 }
954 } catch (NameNotFoundException e) {
955 Log.e(TAG, "Could not find referrer package " + mReferrerPackage);
956 }
957
958 if (ai != null) {
959 titleIcon.setImageDrawable(ai.loadIcon(mPm));
960 }
961 }
962 }
963
964 final ImageView iconView = (ImageView) findViewById(R.id.icon);
965 final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem();
966 if (iconView != null && iconInfo != null) {
967 new LoadIconIntoViewTask(iconInfo, iconView).execute();
968 }
969 }
970
971 public void resetAlwaysOrOnceButtonBar() {
972 if (mAlwaysUseOption || mAdapter.mLastChosen != null) {
973 final ViewGroup buttonLayout = (ViewGroup) findViewById(R.id.button_bar);
974 if (buttonLayout != null) {
975 buttonLayout.setVisibility(View.VISIBLE);
976 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
977 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
978 }
979 }
980
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800981 if (mAdapter.getFilteredPosition() >= 0) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800982 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
983 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800984 return;
985 }
986
987 // When the items load in, if an item was already selected, enable the buttons
988 if (mAdapterView != null
989 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
990 setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true);
991 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800992 }
993 }
994
Adam Powellc412be62015-06-24 13:54:10 -0700995 /**
996 * Check a simple match for the component of two ResolveInfos.
997 */
998 static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
999 return lhs == null ? rhs == null
1000 : lhs.activityInfo == null ? rhs.activityInfo == null
1001 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1002 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1003 }
1004
Adam Powell23882512016-01-29 10:21:00 -08001005 public final class DisplayResolveInfo implements TargetInfo {
Adam Powell24428412015-04-01 17:19:56 -07001006 private final ResolveInfo mResolveInfo;
1007 private final CharSequence mDisplayLabel;
1008 private Drawable mDisplayIcon;
Adam Powell00f4aad2015-09-17 13:38:16 -07001009 private Drawable mBadge;
Adam Powell24428412015-04-01 17:19:56 -07001010 private final CharSequence mExtendedInfo;
1011 private final Intent mResolvedIntent;
Adam Powell2ed547e2015-04-29 18:45:04 -07001012 private final List<Intent> mSourceIntents = new ArrayList<>();
Adam Powell23882512016-01-29 10:21:00 -08001013 private boolean mPinned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014
Adam Powell23882512016-01-29 10:21:00 -08001015 public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel,
Dianne Hackborneb034652009-09-07 00:49:58 -07001016 CharSequence pInfo, Intent pOrigIntent) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001017 mSourceIntents.add(originalIntent);
Adam Powell24428412015-04-01 17:19:56 -07001018 mResolveInfo = pri;
1019 mDisplayLabel = pLabel;
1020 mExtendedInfo = pInfo;
1021
1022 final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent :
Adam Powell2ed547e2015-04-29 18:45:04 -07001023 getReplacementIntent(pri.activityInfo, getTargetIntent()));
Adam Powell24428412015-04-01 17:19:56 -07001024 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1025 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
1026 final ActivityInfo ai = mResolveInfo.activityInfo;
1027 intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));
1028
1029 mResolvedIntent = intent;
1030 }
1031
Adam Powell2ed547e2015-04-29 18:45:04 -07001032 private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) {
1033 mSourceIntents.addAll(other.getAllSourceIntents());
1034 mResolveInfo = other.mResolveInfo;
1035 mDisplayLabel = other.mDisplayLabel;
1036 mDisplayIcon = other.mDisplayIcon;
1037 mExtendedInfo = other.mExtendedInfo;
1038 mResolvedIntent = new Intent(other.mResolvedIntent);
1039 mResolvedIntent.fillIn(fillInIntent, flags);
Adam Powell23882512016-01-29 10:21:00 -08001040 mPinned = other.mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001041 }
1042
Adam Powell24428412015-04-01 17:19:56 -07001043 public ResolveInfo getResolveInfo() {
1044 return mResolveInfo;
1045 }
1046
1047 public CharSequence getDisplayLabel() {
1048 return mDisplayLabel;
1049 }
1050
1051 public Drawable getDisplayIcon() {
1052 return mDisplayIcon;
1053 }
1054
Adam Powell7d758002015-05-06 17:49:36 -07001055 public Drawable getBadgeIcon() {
Adam Powell00f4aad2015-09-17 13:38:16 -07001056 // We only expose a badge if we have extended info.
1057 // The badge is a higher-priority disambiguation signal
1058 // but we don't need one if we wouldn't show extended info at all.
1059 if (TextUtils.isEmpty(getExtendedInfo())) {
1060 return null;
1061 }
1062
1063 if (mBadge == null && mResolveInfo != null && mResolveInfo.activityInfo != null
1064 && mResolveInfo.activityInfo.applicationInfo != null) {
1065 if (mResolveInfo.activityInfo.icon == 0 || mResolveInfo.activityInfo.icon
1066 == mResolveInfo.activityInfo.applicationInfo.icon) {
1067 // Badging an icon with exactly the same icon is silly.
1068 // If the activityInfo icon resid is 0 it will fall back
1069 // to the application's icon, making it a match.
1070 return null;
1071 }
1072 mBadge = mResolveInfo.activityInfo.applicationInfo.loadIcon(mPm);
1073 }
1074 return mBadge;
Adam Powell7d758002015-05-06 17:49:36 -07001075 }
1076
Adam Powell2ed547e2015-04-29 18:45:04 -07001077 @Override
Alan Viverettece5d92c2015-07-31 16:46:56 -04001078 public CharSequence getBadgeContentDescription() {
1079 return null;
1080 }
1081
1082 @Override
Adam Powell2ed547e2015-04-29 18:45:04 -07001083 public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) {
1084 return new DisplayResolveInfo(this, fillInIntent, flags);
1085 }
1086
1087 @Override
1088 public List<Intent> getAllSourceIntents() {
1089 return mSourceIntents;
1090 }
1091
1092 public void addAlternateSourceIntent(Intent alt) {
1093 mSourceIntents.add(alt);
1094 }
1095
Adam Powell24428412015-04-01 17:19:56 -07001096 public void setDisplayIcon(Drawable icon) {
1097 mDisplayIcon = icon;
1098 }
1099
1100 public boolean hasDisplayIcon() {
1101 return mDisplayIcon != null;
1102 }
1103
1104 public CharSequence getExtendedInfo() {
1105 return mExtendedInfo;
1106 }
1107
1108 public Intent getResolvedIntent() {
1109 return mResolvedIntent;
1110 }
1111
1112 @Override
1113 public ComponentName getResolvedComponentName() {
1114 return new ComponentName(mResolveInfo.activityInfo.packageName,
1115 mResolveInfo.activityInfo.name);
1116 }
1117
1118 @Override
1119 public boolean start(Activity activity, Bundle options) {
1120 activity.startActivity(mResolvedIntent, options);
1121 return true;
1122 }
1123
1124 @Override
1125 public boolean startAsCaller(Activity activity, Bundle options, int userId) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001126 activity.startActivityAsCaller(mResolvedIntent, options, false, userId);
Adam Powell24428412015-04-01 17:19:56 -07001127 return true;
1128 }
1129
1130 @Override
1131 public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
1132 activity.startActivityAsUser(mResolvedIntent, options, user);
1133 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 }
Adam Powell23882512016-01-29 10:21:00 -08001135
1136 @Override
1137 public boolean isPinned() {
1138 return mPinned;
1139 }
1140
1141 public void setPinned(boolean pinned) {
1142 mPinned = pinned;
1143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145
Adam Powell24428412015-04-01 17:19:56 -07001146 /**
1147 * A single target as represented in the chooser.
1148 */
1149 public interface TargetInfo {
1150 /**
1151 * Get the resolved intent that represents this target. Note that this may not be the
1152 * intent that will be launched by calling one of the <code>start</code> methods provided;
1153 * this is the intent that will be credited with the launch.
1154 *
1155 * @return the resolved intent for this target
1156 */
Adam Powell23882512016-01-29 10:21:00 -08001157 Intent getResolvedIntent();
Adam Powell24428412015-04-01 17:19:56 -07001158
1159 /**
1160 * Get the resolved component name that represents this target. Note that this may not
1161 * be the component that will be directly launched by calling one of the <code>start</code>
1162 * methods provided; this is the component that will be credited with the launch.
1163 *
1164 * @return the resolved ComponentName for this target
1165 */
Adam Powell23882512016-01-29 10:21:00 -08001166 ComponentName getResolvedComponentName();
Adam Powell24428412015-04-01 17:19:56 -07001167
1168 /**
1169 * Start the activity referenced by this target.
1170 *
1171 * @param activity calling Activity performing the launch
1172 * @param options ActivityOptions bundle
1173 * @return true if the start completed successfully
1174 */
Adam Powell23882512016-01-29 10:21:00 -08001175 boolean start(Activity activity, Bundle options);
Adam Powell24428412015-04-01 17:19:56 -07001176
1177 /**
1178 * Start the activity referenced by this target as if the ResolverActivity's caller
1179 * was performing the start operation.
1180 *
1181 * @param activity calling Activity (actually) performing the launch
1182 * @param options ActivityOptions bundle
1183 * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller
1184 * @return true if the start completed successfully
1185 */
Adam Powell23882512016-01-29 10:21:00 -08001186 boolean startAsCaller(Activity activity, Bundle options, int userId);
Adam Powell24428412015-04-01 17:19:56 -07001187
1188 /**
1189 * Start the activity referenced by this target as a given user.
1190 *
1191 * @param activity calling activity performing the launch
1192 * @param options ActivityOptions bundle
1193 * @param user handle for the user to start the activity as
1194 * @return true if the start completed successfully
1195 */
Adam Powell23882512016-01-29 10:21:00 -08001196 boolean startAsUser(Activity activity, Bundle options, UserHandle user);
Adam Powell24428412015-04-01 17:19:56 -07001197
1198 /**
1199 * Return the ResolveInfo about how and why this target matched the original query
1200 * for available targets.
1201 *
1202 * @return ResolveInfo representing this target's match
1203 */
Adam Powell23882512016-01-29 10:21:00 -08001204 ResolveInfo getResolveInfo();
Adam Powell24428412015-04-01 17:19:56 -07001205
1206 /**
1207 * Return the human-readable text label for this target.
1208 *
1209 * @return user-visible target label
1210 */
Adam Powell23882512016-01-29 10:21:00 -08001211 CharSequence getDisplayLabel();
Adam Powell24428412015-04-01 17:19:56 -07001212
1213 /**
1214 * Return any extended info for this target. This may be used to disambiguate
1215 * otherwise identical targets.
1216 *
1217 * @return human-readable disambig string or null if none present
1218 */
Adam Powell23882512016-01-29 10:21:00 -08001219 CharSequence getExtendedInfo();
Adam Powell24428412015-04-01 17:19:56 -07001220
1221 /**
1222 * @return The drawable that should be used to represent this target
1223 */
Adam Powell23882512016-01-29 10:21:00 -08001224 Drawable getDisplayIcon();
Adam Powell2ed547e2015-04-29 18:45:04 -07001225
1226 /**
Adam Powell7d758002015-05-06 17:49:36 -07001227 * @return The (small) icon to badge the target with
1228 */
Adam Powell23882512016-01-29 10:21:00 -08001229 Drawable getBadgeIcon();
Adam Powell7d758002015-05-06 17:49:36 -07001230
1231 /**
Alan Viverettece5d92c2015-07-31 16:46:56 -04001232 * @return The content description for the badge icon
1233 */
Adam Powell23882512016-01-29 10:21:00 -08001234 CharSequence getBadgeContentDescription();
Alan Viverettece5d92c2015-07-31 16:46:56 -04001235
1236 /**
Adam Powell2ed547e2015-04-29 18:45:04 -07001237 * Clone this target with the given fill-in information.
1238 */
Adam Powell23882512016-01-29 10:21:00 -08001239 TargetInfo cloneFilledIn(Intent fillInIntent, int flags);
Adam Powell2ed547e2015-04-29 18:45:04 -07001240
1241 /**
1242 * @return the list of supported source intents deduped against this single target
1243 */
Adam Powell23882512016-01-29 10:21:00 -08001244 List<Intent> getAllSourceIntents();
1245
1246 /**
1247 * @return true if this target should be pinned to the front by the request of the user
1248 */
1249 boolean isPinned();
Adam Powell24428412015-04-01 17:19:56 -07001250 }
1251
Adam Powell23882512016-01-29 10:21:00 -08001252 public class ResolveListAdapter extends BaseAdapter {
Adam Powell7d758002015-05-06 17:49:36 -07001253 private final List<Intent> mIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001254 private final Intent[] mInitialIntents;
1255 private final List<ResolveInfo> mBaseResolveList;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001256 protected ResolveInfo mLastChosen;
Adam Powell88831a22014-11-20 18:17:00 -08001257 private DisplayResolveInfo mOtherProfile;
Adam Powell24428412015-04-01 17:19:56 -07001258 private boolean mHasExtendedInfo;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001259 private ResolverListController mResolverListController;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001260 private int mPlaceholderCount;
Adam Powell24428412015-04-01 17:19:56 -07001261
1262 protected final LayoutInflater mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263
Adam Powell2ed547e2015-04-29 18:45:04 -07001264 List<DisplayResolveInfo> mDisplayList;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001265 List<ResolvedComponentInfo> mUnfilteredResolveList;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001266
Adam Powell278902c2014-07-12 18:33:22 -07001267 private int mLastChosenPosition = -1;
1268 private boolean mFilterLastUsed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269
Adam Powell7d758002015-05-06 17:49:36 -07001270 public ResolveListAdapter(Context context, List<Intent> payloadIntents,
1271 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001272 boolean filterLastUsed,
1273 ResolverListController resolverListController) {
Adam Powell7d758002015-05-06 17:49:36 -07001274 mIntents = payloadIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001275 mInitialIntents = initialIntents;
1276 mBaseResolveList = rList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001277 mLaunchedFromUid = launchedFromUid;
Adam Powelle9414d92014-07-05 17:44:18 -07001278 mInflater = LayoutInflater.from(context);
Adam Powell2ed547e2015-04-29 18:45:04 -07001279 mDisplayList = new ArrayList<>();
Adam Powell278902c2014-07-12 18:33:22 -07001280 mFilterLastUsed = filterLastUsed;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001281 mResolverListController = resolverListController;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001284 public void handlePackagesChanged() {
1285 rebuildList();
Esteban Talavera6de72662014-12-11 17:54:07 +00001286 if (getCount() == 0) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001287 // We no longer have any items... just finish the activity.
1288 finish();
Adam Powellc5878612012-05-04 18:42:38 -07001289 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001290 }
1291
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001292 public void setPlaceholderCount(int count) {
1293 mPlaceholderCount = count;
1294 }
1295
Adam Powell278902c2014-07-12 18:33:22 -07001296 public DisplayResolveInfo getFilteredItem() {
1297 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1298 // Not using getItem since it offsets to dodge this position for the list
Adam Powell2ed547e2015-04-29 18:45:04 -07001299 return mDisplayList.get(mLastChosenPosition);
Adam Powell278902c2014-07-12 18:33:22 -07001300 }
1301 return null;
1302 }
1303
Adam Powell88831a22014-11-20 18:17:00 -08001304 public DisplayResolveInfo getOtherProfile() {
1305 return mOtherProfile;
1306 }
1307
Adam Powell278902c2014-07-12 18:33:22 -07001308 public int getFilteredPosition() {
1309 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1310 return mLastChosenPosition;
1311 }
1312 return AbsListView.INVALID_POSITION;
1313 }
1314
1315 public boolean hasFilteredItem() {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001316 return mFilterLastUsed && mLastChosen != null;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001317 }
1318
Adam Powella182e452015-07-06 16:57:56 -07001319 public float getScore(DisplayResolveInfo target) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001320 return mResolverListController.getScore(target);
Adam Powella182e452015-07-06 16:57:56 -07001321 }
1322
Kang Li0cef910d2017-01-05 09:14:36 -08001323 public void updateModel(ComponentName componentName) {
1324 mResolverListController.updateModel(componentName);
1325 }
1326
Kang Li9fa2a2c2017-01-06 13:33:24 -08001327 public void updateChooserCounts(String packageName, int userId, String action) {
1328 mResolverListController.updateChooserCounts(packageName, userId, action);
1329 }
1330
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001331 /**
1332 * Rebuild the list of resolvers. In some cases some parts will need some asynchronous work
1333 * to complete.
1334 *
1335 * @return Whether or not the list building is completed.
1336 */
1337 protected boolean rebuildList() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001338 List<ResolvedComponentInfo> currentResolveList = null;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001339 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -08001340 mLastChosen = mResolverListController.getLastChosen();
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001341 } catch (RemoteException re) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001342 Log.d(TAG, "Error calling getLastChosenActivity\n" + re);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001343 }
1344
Sudheer Shanka7e64e102015-01-23 10:37:45 +00001345 // Clear the value of mOtherProfile from previous call.
1346 mOtherProfile = null;
Adam Powell2ed547e2015-04-29 18:45:04 -07001347 mDisplayList.clear();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001348 if (mBaseResolveList != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001349 currentResolveList = mUnfilteredResolveList = new ArrayList<>();
1350 mResolverListController.addResolveListDedupe(currentResolveList,
1351 getTargetIntent(),
1352 mBaseResolveList);
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001353 } else {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001354 currentResolveList = mUnfilteredResolveList =
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001355 mResolverListController.getResolversForIntent(shouldGetResolvedFilter(),
1356 shouldGetActivityMetadata(),
1357 mIntents);
1358 if (currentResolveList == null) {
1359 processSortedList(currentResolveList);
1360 return true;
Adam Powell2ed547e2015-04-29 18:45:04 -07001361 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001362 List<ResolvedComponentInfo> originalList =
1363 mResolverListController.filterIneligibleActivities(currentResolveList,
1364 true);
1365 if (originalList != null) {
1366 mUnfilteredResolveList = originalList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001367 }
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 int N;
You Kim43a5070e2012-11-21 23:23:45 +09001370 if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001371 // We only care about fixing the unfilteredList if the current resolve list and
1372 // current resolve list are currently the same.
1373 List<ResolvedComponentInfo> originalList =
1374 mResolverListController.filterLowPriority(currentResolveList,
1375 mUnfilteredResolveList == currentResolveList);
1376 if (originalList != null) {
1377 mUnfilteredResolveList = originalList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 if (N > 1) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001381 setPlaceholderCount(currentResolveList.size());
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001382 AsyncTask<List<ResolvedComponentInfo>,
1383 Void,
1384 List<ResolvedComponentInfo>> sortingTask =
1385 new AsyncTask<List<ResolvedComponentInfo>,
1386 Void,
1387 List<ResolvedComponentInfo>>() {
1388 @Override
1389 protected List<ResolvedComponentInfo> doInBackground(
1390 List<ResolvedComponentInfo>... params) {
1391 mResolverListController.sort(params[0]);
1392 return params[0];
1393 }
1394
1395 @Override
1396 protected void onPostExecute(List<ResolvedComponentInfo> sortedComponents) {
1397 processSortedList(sortedComponents);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001398 if (mProfileView != null) {
1399 bindProfileView();
1400 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001401 notifyDataSetChanged();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001402 }
1403 };
1404 sortingTask.execute(currentResolveList);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001405 if (mPostListBuildRunnable == null) {
1406 mPostListBuildRunnable = new Runnable() {
1407 @Override
1408 public void run() {
1409 setTitleAndIcon();
1410 resetAlwaysOrOnceButtonBar();
1411 onListRebuilt();
1412 disableLastChosenIfNeeded();
1413 mPostListBuildRunnable = null;
1414 }
1415 };
1416 getMainThreadHandler().post(mPostListBuildRunnable);
1417 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001418 return false;
1419 } else {
1420 processSortedList(currentResolveList);
1421 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001423 } else {
1424 processSortedList(currentResolveList);
1425 return true;
1426 }
1427 }
1428
1429 private void disableLastChosenIfNeeded() {
1430 // Layout doesn't handle both profile button and last chosen
1431 // so disable last chosen if profile button is present.
1432 if (mOtherProfile != null && mLastChosenPosition >= 0) {
1433 mLastChosenPosition = -1;
1434 mFilterLastUsed = false;
1435 }
1436 }
1437
1438
1439 private void processSortedList(List<ResolvedComponentInfo> sortedComponents) {
1440 int N;
1441 if (sortedComponents != null && (N = sortedComponents.size()) != 0) {
Dianne Hackborneb034652009-09-07 00:49:58 -07001442 // First put the initial items at the top.
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001443 if (mInitialIntents != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001444 for (int i = 0; i < mInitialIntents.length; i++) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001445 Intent ii = mInitialIntents[i];
Dianne Hackborneb034652009-09-07 00:49:58 -07001446 if (ii == null) {
1447 continue;
1448 }
1449 ActivityInfo ai = ii.resolveActivityInfo(
1450 getPackageManager(), 0);
1451 if (ai == null) {
Adam Powell09a65602014-07-20 16:23:14 -07001452 Log.w(TAG, "No activity found for " + ii);
Dianne Hackborneb034652009-09-07 00:49:58 -07001453 continue;
1454 }
1455 ResolveInfo ri = new ResolveInfo();
1456 ri.activityInfo = ai;
Nicolas Prevot1a815922014-10-10 16:22:38 +01001457 UserManager userManager =
1458 (UserManager) getSystemService(Context.USER_SERVICE);
Dianne Hackborneb034652009-09-07 00:49:58 -07001459 if (ii instanceof LabeledIntent) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001460 LabeledIntent li = (LabeledIntent) ii;
Dianne Hackborneb034652009-09-07 00:49:58 -07001461 ri.resolvePackageName = li.getSourcePackage();
1462 ri.labelRes = li.getLabelResource();
1463 ri.nonLocalizedLabel = li.getNonLocalizedLabel();
1464 ri.icon = li.getIconResource();
Sudheer Shanka9ded7602015-05-19 21:17:25 +01001465 ri.iconResourceId = ri.icon;
1466 }
1467 if (userManager.isManagedProfile()) {
1468 ri.noResourceId = true;
1469 ri.icon = 0;
Dianne Hackborneb034652009-09-07 00:49:58 -07001470 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001471 addResolveInfo(new DisplayResolveInfo(ii, ri,
Dianne Hackborneb034652009-09-07 00:49:58 -07001472 ri.loadLabel(getPackageManager()), null, ii));
1473 }
1474 }
You Kim43a5070e2012-11-21 23:23:45 +09001475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 // Check for applications with same name and use application name or
1477 // package name if necessary
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001478 ResolvedComponentInfo rci0 = sortedComponents.get(0);
1479 ResolveInfo r0 = rci0.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 int start = 0;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001481 CharSequence r0Label = r0.loadLabel(mPm);
Adam Powell24428412015-04-01 17:19:56 -07001482 mHasExtendedInfo = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 for (int i = 1; i < N; i++) {
1484 if (r0Label == null) {
1485 r0Label = r0.activityInfo.packageName;
1486 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001487 ResolvedComponentInfo rci = sortedComponents.get(i);
Adam Powell2ed547e2015-04-29 18:45:04 -07001488 ResolveInfo ri = rci.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 CharSequence riLabel = ri.loadLabel(mPm);
1490 if (riLabel == null) {
1491 riLabel = ri.activityInfo.packageName;
1492 }
1493 if (riLabel.equals(r0Label)) {
1494 continue;
1495 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001496 processGroup(sortedComponents, start, (i - 1), rci0, r0Label);
Adam Powell2ed547e2015-04-29 18:45:04 -07001497 rci0 = rci;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 r0 = ri;
1499 r0Label = riLabel;
1500 start = i;
1501 }
1502 // Process last group
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001503 processGroup(sortedComponents, start, (N - 1), rci0, r0Label);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 }
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001505
1506 setTitleAndIcon();
1507 resetAlwaysOrOnceButtonBar();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001508 disableLastChosenIfNeeded();
Adam Powell24428412015-04-01 17:19:56 -07001509 onListRebuilt();
1510 }
1511
1512 public void onListRebuilt() {
1513 // This space for rent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
1515
Adam Powellc6d5e3a2015-04-23 12:22:20 -07001516 public boolean shouldGetResolvedFilter() {
1517 return mFilterLastUsed;
1518 }
1519
Adam Powell2ed547e2015-04-29 18:45:04 -07001520 private void processGroup(List<ResolvedComponentInfo> rList, int start, int end,
1521 ResolvedComponentInfo ro, CharSequence roLabel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 // Process labels from start to i
1523 int num = end - start+1;
1524 if (num == 1) {
1525 // No duplicate labels. Use label for entry at start
Adam Powell2ed547e2015-04-29 18:45:04 -07001526 addResolveInfoWithAlternates(ro, null, roLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 } else {
Adam Powell24428412015-04-01 17:19:56 -07001528 mHasExtendedInfo = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 boolean usePkg = false;
Adam Powell00f4aad2015-09-17 13:38:16 -07001530 final ApplicationInfo ai = ro.getResolveInfoAt(0).activityInfo.applicationInfo;
1531 final CharSequence startApp = ai.loadLabel(mPm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 if (startApp == null) {
1533 usePkg = true;
1534 }
1535 if (!usePkg) {
1536 // Use HashSet to track duplicates
1537 HashSet<CharSequence> duplicates =
1538 new HashSet<CharSequence>();
1539 duplicates.add(startApp);
1540 for (int j = start+1; j <= end ; j++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001541 ResolveInfo jRi = rList.get(j).getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm);
1543 if ( (jApp == null) || (duplicates.contains(jApp))) {
1544 usePkg = true;
1545 break;
1546 } else {
1547 duplicates.add(jApp);
1548 }
1549 }
1550 // Clear HashSet for later use
1551 duplicates.clear();
1552 }
1553 for (int k = start; k <= end; k++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001554 final ResolvedComponentInfo rci = rList.get(k);
1555 final ResolveInfo add = rci.getResolveInfoAt(0);
1556 final CharSequence extraInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 if (usePkg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 // Use package name for all entries from start to end-1
Adam Powell2ed547e2015-04-29 18:45:04 -07001559 extraInfo = add.activityInfo.packageName;
1560 } else {
1561 // Use application name for all entries from start to end-1
1562 extraInfo = add.activityInfo.applicationInfo.loadLabel(mPm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001564 addResolveInfoWithAlternates(rci, extraInfo, roLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
1566 }
1567 }
1568
Adam Powell2ed547e2015-04-29 18:45:04 -07001569 private void addResolveInfoWithAlternates(ResolvedComponentInfo rci,
1570 CharSequence extraInfo, CharSequence roLabel) {
1571 final int count = rci.getCount();
1572 final Intent intent = rci.getIntentAt(0);
1573 final ResolveInfo add = rci.getResolveInfoAt(0);
1574 final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent);
1575 final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel,
1576 extraInfo, replaceIntent);
Adam Powell23882512016-01-29 10:21:00 -08001577 dri.setPinned(rci.isPinned());
Adam Powell2ed547e2015-04-29 18:45:04 -07001578 addResolveInfo(dri);
1579 if (replaceIntent == intent) {
1580 // Only add alternates if we didn't get a specific replacement from
1581 // the caller. If we have one it trumps potential alternates.
1582 for (int i = 1, N = count; i < N; i++) {
1583 final Intent altIntent = rci.getIntentAt(i);
1584 dri.addAlternateSourceIntent(altIntent);
1585 }
1586 }
1587 updateLastChosenPosition(add);
1588 }
1589
Esteban Talavera6de72662014-12-11 17:54:07 +00001590 private void updateLastChosenPosition(ResolveInfo info) {
1591 if (mLastChosen != null
1592 && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName)
1593 && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001594 mLastChosenPosition = mDisplayList.size() - 1;
Esteban Talavera6de72662014-12-11 17:54:07 +00001595 }
1596 }
1597
Adam Powell88831a22014-11-20 18:17:00 -08001598 private void addResolveInfo(DisplayResolveInfo dri) {
Adam Powell24428412015-04-01 17:19:56 -07001599 if (dri.mResolveInfo.targetUserId != UserHandle.USER_CURRENT && mOtherProfile == null) {
Adam Powell88831a22014-11-20 18:17:00 -08001600 // So far we only support a single other profile at a time.
1601 // The first one we see gets special treatment.
1602 mOtherProfile = dri;
1603 } else {
Adam Powell2ed547e2015-04-29 18:45:04 -07001604 mDisplayList.add(dri);
Adam Powell88831a22014-11-20 18:17:00 -08001605 }
1606 }
1607
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001608 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001609 public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001610 TargetInfo target = targetInfoForPosition(position, filtered);
1611 if (target != null) {
1612 return target.getResolveInfo();
1613 }
1614 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
1616
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001617 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001618 public TargetInfo targetInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001619 if (filtered) {
1620 return getItem(position);
1621 }
1622 if (mDisplayList.size() > position) {
1623 return mDisplayList.get(position);
1624 }
1625 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
1627
1628 public int getCount() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001629 int totalSize = mDisplayList == null || mDisplayList.isEmpty() ? mPlaceholderCount :
1630 mDisplayList.size();
Adam Powell278902c2014-07-12 18:33:22 -07001631 if (mFilterLastUsed && mLastChosenPosition >= 0) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001632 totalSize--;
Adam Powell278902c2014-07-12 18:33:22 -07001633 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001634 return totalSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 }
1636
Adam Powell50077352015-05-26 18:01:55 -07001637 public int getUnfilteredCount() {
1638 return mDisplayList.size();
1639 }
1640
1641 public int getDisplayInfoCount() {
1642 return mDisplayList.size();
1643 }
1644
1645 public DisplayResolveInfo getDisplayInfoAt(int index) {
1646 return mDisplayList.get(index);
1647 }
1648
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001649 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001650 public TargetInfo getItem(int position) {
Adam Powell278902c2014-07-12 18:33:22 -07001651 if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) {
1652 position++;
1653 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001654 if (mDisplayList.size() > position) {
1655 return mDisplayList.get(position);
1656 } else {
1657 return null;
1658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660
1661 public long getItemId(int position) {
1662 return position;
1663 }
1664
Adam Powell24428412015-04-01 17:19:56 -07001665 public boolean hasExtendedInfo() {
1666 return mHasExtendedInfo;
1667 }
1668
1669 public boolean hasResolvedTarget(ResolveInfo info) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001670 for (int i = 0, N = mDisplayList.size(); i < N; i++) {
Adam Powellc412be62015-06-24 13:54:10 -07001671 if (resolveInfoMatch(info, mDisplayList.get(i).getResolveInfo())) {
Adam Powell24428412015-04-01 17:19:56 -07001672 return true;
1673 }
1674 }
1675 return false;
1676 }
1677
Adam Powell23882512016-01-29 10:21:00 -08001678 public int getDisplayResolveInfoCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001679 return mDisplayList.size();
Adam Powell24428412015-04-01 17:19:56 -07001680 }
1681
Adam Powell23882512016-01-29 10:21:00 -08001682 public DisplayResolveInfo getDisplayResolveInfo(int index) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001683 // Used to query services. We only query services for primary targets, not alternates.
1684 return mDisplayList.get(index);
Adam Powell24428412015-04-01 17:19:56 -07001685 }
1686
1687 public final View getView(int position, View convertView, ViewGroup parent) {
Adam Powellfd1e93d2014-09-07 16:52:22 -07001688 View view = convertView;
1689 if (view == null) {
Adam Powell24428412015-04-01 17:19:56 -07001690 view = createView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
Adam Powell7d758002015-05-06 17:49:36 -07001692 onBindView(view, getItem(position));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 return view;
1694 }
1695
Adam Powell7d758002015-05-06 17:49:36 -07001696 public final View createView(ViewGroup parent) {
1697 final View view = onCreateView(parent);
1698 final ViewHolder holder = new ViewHolder(view);
1699 view.setTag(holder);
1700 return view;
1701 }
1702
1703 public View onCreateView(ViewGroup parent) {
Adam Powell24428412015-04-01 17:19:56 -07001704 return mInflater.inflate(
1705 com.android.internal.R.layout.resolve_list_item, parent, false);
1706 }
1707
1708 public boolean showsExtendedInfo(TargetInfo info) {
1709 return !TextUtils.isEmpty(info.getExtendedInfo());
1710 }
1711
Adam Powell23882512016-01-29 10:21:00 -08001712 public boolean isComponentPinned(ComponentName name) {
1713 return false;
1714 }
1715
Adam Powell7d758002015-05-06 17:49:36 -07001716 public final void bindView(int position, View view) {
1717 onBindView(view, getItem(position));
1718 }
1719
1720 private void onBindView(View view, TargetInfo info) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001721 final ViewHolder holder = (ViewHolder) view.getTag();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001722 if (info == null) {
1723 holder.icon.setImageDrawable(
1724 getDrawable(R.drawable.resolver_icon_placeholder));
1725 return;
1726 }
Adam Powell63b31692015-09-28 10:45:00 -07001727 final CharSequence label = info.getDisplayLabel();
1728 if (!TextUtils.equals(holder.text.getText(), label)) {
1729 holder.text.setText(info.getDisplayLabel());
1730 }
Adam Powell24428412015-04-01 17:19:56 -07001731 if (showsExtendedInfo(info)) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001732 holder.text2.setVisibility(View.VISIBLE);
Adam Powell24428412015-04-01 17:19:56 -07001733 holder.text2.setText(info.getExtendedInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 } else {
Adam Powell0256c6f2013-05-29 16:42:33 -07001735 holder.text2.setVisibility(View.GONE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 }
Adam Powell24428412015-04-01 17:19:56 -07001737 if (info instanceof DisplayResolveInfo
1738 && !((DisplayResolveInfo) info).hasDisplayIcon()) {
1739 new LoadAdapterIconTask((DisplayResolveInfo) info).execute();
Dianne Hackborneb034652009-09-07 00:49:58 -07001740 }
Adam Powell24428412015-04-01 17:19:56 -07001741 holder.icon.setImageDrawable(info.getDisplayIcon());
Adam Powell7d758002015-05-06 17:49:36 -07001742 if (holder.badge != null) {
1743 final Drawable badge = info.getBadgeIcon();
1744 if (badge != null) {
1745 holder.badge.setImageDrawable(badge);
Alan Viverettece5d92c2015-07-31 16:46:56 -04001746 holder.badge.setContentDescription(info.getBadgeContentDescription());
Adam Powell7d758002015-05-06 17:49:36 -07001747 holder.badge.setVisibility(View.VISIBLE);
1748 } else {
1749 holder.badge.setVisibility(View.GONE);
1750 }
1751 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001752 }
1753 }
1754
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001755 @VisibleForTesting
1756 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001757 public final ComponentName name;
Adam Powell23882512016-01-29 10:21:00 -08001758 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001759 private final List<Intent> mIntents = new ArrayList<>();
1760 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
1761
1762 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1763 this.name = name;
1764 add(intent, info);
1765 }
1766
1767 public void add(Intent intent, ResolveInfo info) {
1768 mIntents.add(intent);
1769 mResolveInfos.add(info);
1770 }
1771
1772 public int getCount() {
1773 return mIntents.size();
1774 }
1775
1776 public Intent getIntentAt(int index) {
1777 return index >= 0 ? mIntents.get(index) : null;
1778 }
1779
1780 public ResolveInfo getResolveInfoAt(int index) {
1781 return index >= 0 ? mResolveInfos.get(index) : null;
1782 }
1783
1784 public int findIntent(Intent intent) {
1785 for (int i = 0, N = mIntents.size(); i < N; i++) {
1786 if (intent.equals(mIntents.get(i))) {
1787 return i;
1788 }
1789 }
1790 return -1;
1791 }
1792
1793 public int findResolveInfo(ResolveInfo info) {
1794 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1795 if (info.equals(mResolveInfos.get(i))) {
1796 return i;
1797 }
1798 }
1799 return -1;
1800 }
Adam Powell23882512016-01-29 10:21:00 -08001801
1802 public boolean isPinned() {
1803 return mPinned;
1804 }
1805
1806 public void setPinned(boolean pinned) {
1807 mPinned = pinned;
1808 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001809 }
1810
Adam Powell0256c6f2013-05-29 16:42:33 -07001811 static class ViewHolder {
1812 public TextView text;
1813 public TextView text2;
1814 public ImageView icon;
Adam Powell7d758002015-05-06 17:49:36 -07001815 public ImageView badge;
Adam Powell0256c6f2013-05-29 16:42:33 -07001816
1817 public ViewHolder(View view) {
1818 text = (TextView) view.findViewById(com.android.internal.R.id.text1);
1819 text2 = (TextView) view.findViewById(com.android.internal.R.id.text2);
1820 icon = (ImageView) view.findViewById(R.id.icon);
Adam Powell7d758002015-05-06 17:49:36 -07001821 badge = (ImageView) view.findViewById(R.id.target_badge);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823 }
1824
Adam Powell7d758002015-05-06 17:49:36 -07001825 class ItemClickListener implements AdapterView.OnItemClickListener,
1826 AdapterView.OnItemLongClickListener {
1827 @Override
1828 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1829 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1830 if (listView != null) {
1831 position -= listView.getHeaderViewsCount();
1832 }
1833 if (position < 0) {
1834 // Header views don't count.
1835 return;
1836 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001837 // If we're still loading, we can't yet enable the buttons.
1838 if (mAdapter.resolveInfoForPosition(position, true) == null) {
1839 return;
1840 }
1841
Adam Powell7d758002015-05-06 17:49:36 -07001842 final int checkedPos = mAdapterView.getCheckedItemPosition();
1843 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
1844 if (mAlwaysUseOption && (!hasValidSelection || mLastSelected != checkedPos)) {
1845 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
1846 mOnceButton.setEnabled(hasValidSelection);
1847 if (hasValidSelection) {
1848 mAdapterView.smoothScrollToPosition(checkedPos);
1849 }
1850 mLastSelected = checkedPos;
1851 } else {
1852 startSelected(position, false, true);
1853 }
1854 }
Adam Powell2d809622012-03-22 15:24:43 -07001855
1856 @Override
1857 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001858 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1859 if (listView != null) {
1860 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001861 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001862 if (position < 0) {
1863 // Header views don't count.
1864 return false;
1865 }
Adam Powell278902c2014-07-12 18:33:22 -07001866 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001867 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001868 return true;
1869 }
1870
1871 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001872
Adam Powell24428412015-04-01 17:19:56 -07001873 abstract class LoadIconTask extends AsyncTask<Void, Void, Drawable> {
1874 protected final DisplayResolveInfo mDisplayResolveInfo;
1875 private final ResolveInfo mResolveInfo;
1876
1877 public LoadIconTask(DisplayResolveInfo dri) {
1878 mDisplayResolveInfo = dri;
1879 mResolveInfo = dri.getResolveInfo();
Adam Powell0256c6f2013-05-29 16:42:33 -07001880 }
1881
1882 @Override
Adam Powell24428412015-04-01 17:19:56 -07001883 protected Drawable doInBackground(Void... params) {
1884 return loadIconForResolveInfo(mResolveInfo);
1885 }
1886
1887 @Override
1888 protected void onPostExecute(Drawable d) {
1889 mDisplayResolveInfo.setDisplayIcon(d);
1890 }
1891 }
1892
1893 class LoadAdapterIconTask extends LoadIconTask {
1894 public LoadAdapterIconTask(DisplayResolveInfo dri) {
1895 super(dri);
1896 }
1897
1898 @Override
1899 protected void onPostExecute(Drawable d) {
1900 super.onPostExecute(d);
1901 if (mProfileView != null && mAdapter.getOtherProfile() == mDisplayResolveInfo) {
Adam Powell88831a22014-11-20 18:17:00 -08001902 bindProfileView();
1903 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001904 mAdapter.notifyDataSetChanged();
1905 }
1906 }
Adam Powell278902c2014-07-12 18:33:22 -07001907
Adam Powell24428412015-04-01 17:19:56 -07001908 class LoadIconIntoViewTask extends LoadIconTask {
1909 private final ImageView mTargetView;
Adam Powell278902c2014-07-12 18:33:22 -07001910
Adam Powell24428412015-04-01 17:19:56 -07001911 public LoadIconIntoViewTask(DisplayResolveInfo dri, ImageView target) {
1912 super(dri);
Adam Powell278902c2014-07-12 18:33:22 -07001913 mTargetView = target;
1914 }
1915
1916 @Override
Adam Powell24428412015-04-01 17:19:56 -07001917 protected void onPostExecute(Drawable d) {
1918 super.onPostExecute(d);
1919 mTargetView.setImageDrawable(d);
Adam Powell278902c2014-07-12 18:33:22 -07001920 }
1921 }
Adam Powell09a65602014-07-20 16:23:14 -07001922
Dianne Hackbornec452d92014-11-11 17:16:56 -08001923 static final boolean isSpecificUriMatch(int match) {
1924 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1925 return match >= IntentFilter.MATCH_CATEGORY_HOST
1926 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1927 }
1928
Adam Powell4c470d62015-06-19 17:46:17 -07001929 static class PickTargetOptionRequest extends PickOptionRequest {
1930 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1931 @Nullable Bundle extras) {
1932 super(prompt, options, extras);
1933 }
1934
1935 @Override
1936 public void onCancel() {
1937 super.onCancel();
1938 final ResolverActivity ra = (ResolverActivity) getActivity();
1939 if (ra != null) {
1940 ra.mPickOptionRequest = null;
1941 ra.finish();
1942 }
1943 }
1944
1945 @Override
1946 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1947 super.onPickOptionResult(finished, selections, result);
1948 if (selections.length != 1) {
1949 // TODO In a better world we would filter the UI presented here and let the
1950 // user refine. Maybe later.
1951 return;
1952 }
1953
1954 final ResolverActivity ra = (ResolverActivity) getActivity();
1955 if (ra != null) {
1956 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
1957 if (ra.onTargetSelected(ti, false)) {
1958 ra.mPickOptionRequest = null;
1959 ra.finish();
1960 }
1961 }
1962 }
1963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964}