blob: 011cc041f0c1ca5830ee1f388c929cc8a0253d7e [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
Hyunyoung Song16b7af42018-10-05 14:00:42 -070019import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
20
Adam Powell4c470d62015-06-19 17:46:17 -070021import android.annotation.Nullable;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -070022import android.annotation.StringRes;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080023import android.annotation.UiThread;
Adam Powelle9414d92014-07-05 17:44:18 -070024import android.app.Activity;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080025import android.app.ActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070026import android.app.ActivityTaskManager;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070027import android.app.ActivityThread;
Adam Powell4c470d62015-06-19 17:46:17 -070028import android.app.VoiceInteractor.PickOptionRequest;
29import android.app.VoiceInteractor.PickOptionRequest.Option;
30import android.app.VoiceInteractor.Prompt;
shafik34064c32019-03-06 15:42:52 +000031import android.app.role.RoleManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.ComponentName;
33import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.Intent;
35import android.content.IntentFilter;
36import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010037import android.content.pm.ApplicationInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -070038import android.content.pm.LabeledIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070040import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010042import android.content.pm.UserInfo;
Jiongxuan Gao3365b162016-11-24 20:20:27 +090043import android.content.res.Configuration;
Adam Powellc5878612012-05-04 18:42:38 -070044import android.content.res.Resources;
Dianne Hackborneb034652009-09-07 00:49:58 -070045import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.net.Uri;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080047import android.os.AsyncTask;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010048import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040050import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.PatternMatcher;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080052import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070053import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070054import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010055import android.os.UserManager;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080056import android.provider.MediaStore;
57import android.provider.Settings;
58import android.text.TextUtils;
Hyunyoung Songdadb9e12017-08-24 18:08:35 -070059import android.util.IconDrawableFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.Log;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080061import android.util.Slog;
Adam Powell2d809622012-03-22 15:24:43 -070062import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.view.View;
64import android.view.ViewGroup;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080065import android.widget.AbsListView;
Adam Powell2d809622012-03-22 15:24:43 -070066import android.widget.AdapterView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.widget.BaseAdapter;
Adam Powellc5878612012-05-04 18:42:38 -070068import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070070import android.widget.ListView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010072import android.widget.Toast;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070073
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080074import com.android.internal.R;
75import com.android.internal.annotations.VisibleForTesting;
76import com.android.internal.content.PackageMonitor;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010077import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010078import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070079import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010082import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.HashSet;
84import java.util.Iterator;
85import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070086import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.Set;
88
89/**
90 * This activity is displayed when the system attempts to start an Intent for
91 * which there is more than one matching activity, allowing the user to decide
92 * which to go to. It is not normally used directly by application developers.
93 */
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080094@UiThread
Adam Powell7d758002015-05-06 17:49:36 -070095public class ResolverActivity extends Activity {
Adam Powellc5878612012-05-04 18:42:38 -070096
Alison Cichowlas4691ed42018-11-13 15:59:55 -050097 // Temporary flag for new chooser delegate behavior.
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -050098 boolean mEnableChooserDelegate = true;
Alison Cichowlas4691ed42018-11-13 15:59:55 -050099
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800100 protected ResolveListAdapter mAdapter;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700101 private boolean mSafeForwardingMode;
Adam Powell24428412015-04-01 17:19:56 -0700102 private AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -0700103 private Button mAlwaysButton;
104 private Button mOnceButton;
shafik69df96322018-12-18 15:41:19 +0000105 private Button mSettingsButton;
Adam Powell88831a22014-11-20 18:17:00 -0800106 private View mProfileView;
Adam Powellc5878612012-05-04 18:42:38 -0700107 private int mIconDpi;
Adam Powell24428412015-04-01 17:19:56 -0700108 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100109 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000110 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800111 private int mLayoutId;
Adam Powell2ed547e2015-04-29 18:45:04 -0700112 private final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700113 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800114 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800115 private CharSequence mTitle;
116 private int mDefaultTitleResId;
shafik69df96322018-12-18 15:41:19 +0000117 private boolean mUseLayoutForBrowsables;
Adam Powell09a65602014-07-20 16:23:14 -0700118
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800119 // Whether or not this activity supports choosing a default handler for the intent.
120 private boolean mSupportsAlwaysUseOption;
Adam Powell63b31692015-09-28 10:45:00 -0700121 protected ResolverDrawerLayout mResolverDrawerLayout;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800122 protected PackageManager mPm;
123 protected int mLaunchedFromUid;
124
125 private static final String TAG = "ResolverActivity";
126 private static final boolean DEBUG = false;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800127 private Runnable mPostListReadyRunnable;
Adam Powell63b31692015-09-28 10:45:00 -0700128
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700129 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100130
131 /** See {@link #setRetainInOnStop}. */
132 private boolean mRetainInOnStop;
133
Hyunyoung Songdadb9e12017-08-24 18:08:35 -0700134 IconDrawableFactory mIconFactory;
135
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800136 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
137 @Override public void onSomePackagesChanged() {
138 mAdapter.handlePackagesChanged();
Adam Powell88831a22014-11-20 18:17:00 -0800139 if (mProfileView != null) {
140 bindProfileView();
141 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800142 }
Xiaohui Chen393c8012017-02-14 14:55:07 -0800143
144 @Override
145 public boolean onPackageChanged(String packageName, int uid, String[] components) {
146 // We care about all package changes, not just the whole package itself which is
147 // default behavior.
148 return true;
149 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800150 };
151
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700152 /**
153 * Get the string resource to be used as a label for the link to the resolver activity for an
154 * action.
155 *
156 * @param action The action to resolve
157 *
158 * @return The string resource to be used as a label
159 */
160 public static @StringRes int getLabelRes(String action) {
161 return ActionTitle.forAction(action).labelRes;
162 }
163
Adam Powell278902c2014-07-12 18:33:22 -0700164 private enum ActionTitle {
165 VIEW(Intent.ACTION_VIEW,
166 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700167 com.android.internal.R.string.whichViewApplicationNamed,
168 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700169 EDIT(Intent.ACTION_EDIT,
170 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700171 com.android.internal.R.string.whichEditApplicationNamed,
172 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700173 SEND(Intent.ACTION_SEND,
174 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700175 com.android.internal.R.string.whichSendApplicationNamed,
176 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700177 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700178 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700179 com.android.internal.R.string.whichSendToApplicationNamed,
180 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700181 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
182 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700183 com.android.internal.R.string.whichSendApplicationNamed,
184 com.android.internal.R.string.whichSendApplicationLabel),
185 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
186 com.android.internal.R.string.whichImageCaptureApplication,
187 com.android.internal.R.string.whichImageCaptureApplicationNamed,
188 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700189 DEFAULT(null,
190 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700191 com.android.internal.R.string.whichApplicationNamed,
192 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700193 HOME(Intent.ACTION_MAIN,
194 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700195 com.android.internal.R.string.whichHomeApplicationNamed,
196 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700197
shafik0c7c5d52019-02-27 12:13:25 +0000198 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000199 public static final int BROWSABLE_TITLE_RES =
200 com.android.internal.R.string.whichGiveAccessToApplication;
201 public static final int BROWSABLE_NAMED_TITLE_RES =
202 com.android.internal.R.string.whichGiveAccessToApplicationNamed;
203
Adam Powell278902c2014-07-12 18:33:22 -0700204 public final String action;
205 public final int titleRes;
206 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700207 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700208
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700209 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700210 this.action = action;
211 this.titleRes = titleRes;
212 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700213 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700214 }
215
216 public static ActionTitle forAction(String action) {
217 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700218 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700219 return title;
220 }
221 }
222 return DEFAULT;
223 }
224 }
225
Dianne Hackborn905577f2011-09-07 18:31:28 -0700226 private Intent makeMyIntent() {
227 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700228 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700229 // The resolver activity is set to be hidden from recent tasks.
230 // we don't want this attribute to be propagated to the next activity
231 // being launched. Note that if the original Intent also had this
232 // flag set, we are now losing it. That should be a very rare case
233 // and we can live with this.
234 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
235 return intent;
236 }
237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 @Override
239 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700240 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700241 final Intent intent = makeMyIntent();
242 final Set<String> categories = intent.getCategories();
243 if (Intent.ACTION_MAIN.equals(intent.getAction())
244 && categories != null
245 && categories.size() == 1
246 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100247 // Note: this field is not set to true in the compatibility version.
248 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700249 }
250
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700251 setSafeForwardingMode(true);
252
Adam Powella35c77a2014-09-25 16:46:36 -0700253 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
255
Adam Powell278902c2014-07-12 18:33:22 -0700256 /**
Adam Powell24428412015-04-01 17:19:56 -0700257 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700258 * a default title resource
259 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700261 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800262 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
263 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
264 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700265 }
266
267 protected void onCreate(Bundle savedInstanceState, Intent intent,
268 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800269 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700270 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000272
273 // Determine whether we should show that intent is forwarded
274 // from managed profile to owner or other way around.
275 setProfileSwitchMessageId(intent.getContentUserHint());
276
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700277 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700278 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700279 getActivityToken());
280 } catch (RemoteException e) {
281 mLaunchedFromUid = -1;
282 }
Adam Powell7d758002015-05-06 17:49:36 -0700283
284 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
285 // Gulp!
286 finish();
287 return;
288 }
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700291
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700292 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700293 mRegistered = true;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800294 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800295
Adam Powellc5878612012-05-04 18:42:38 -0700296 final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
297 mIconDpi = am.getLauncherLargeIconDensity();
Adam Powellc5878612012-05-04 18:42:38 -0700298
Adam Powell7d758002015-05-06 17:49:36 -0700299 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700300 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800301 mTitle = title;
302 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700303
shafik69df96322018-12-18 15:41:19 +0000304 mUseLayoutForBrowsables = getTargetIntent() == null
305 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000306 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000307
308 // We don't want to support Always Use if browsable layout is being used,
309 // as to mitigate Intent Capturing vulnerability
310 mSupportsAlwaysUseOption = supportsAlwaysUseOption && !mUseLayoutForBrowsables;
311
Hyunyoung Song16b7af42018-10-05 14:00:42 -0700312 mIconFactory = IconDrawableFactory.newInstance(this, true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800313 if (configureContentView(mIntents, initialIntents, rList)) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700314 return;
315 }
Adam Powell278902c2014-07-12 18:33:22 -0700316
Alan Viverette51efddb2017-04-05 10:00:01 -0400317 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700318 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700319 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700320 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700321 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700322 finish();
323 }
324 });
Adam Powell4c470d62015-06-19 17:46:17 -0700325 if (isVoiceInteraction()) {
326 rdl.setCollapsed(false);
327 }
Adam Powell63b31692015-09-28 10:45:00 -0700328 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700329 }
330
Adam Powell88831a22014-11-20 18:17:00 -0800331 mProfileView = findViewById(R.id.profile_button);
332 if (mProfileView != null) {
333 mProfileView.setOnClickListener(new View.OnClickListener() {
334 @Override
335 public void onClick(View v) {
336 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
337 if (dri == null) {
338 return;
339 }
340
Sander Alewijnse053d3dd2015-03-09 15:31:10 +0000341 // Do not show the profile switch message anymore.
342 mProfileSwitchMessageId = -1;
343
Adam Powell24428412015-04-01 17:19:56 -0700344 onTargetSelected(dri, false);
Adam Powell88831a22014-11-20 18:17:00 -0800345 finish();
346 }
347 });
348 bindProfileView();
349 }
Adam Powell4c470d62015-06-19 17:46:17 -0700350
351 if (isVoiceInteraction()) {
352 onSetupVoiceInteraction();
353 }
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100354 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100355 MetricsLogger.action(this, mAdapter.hasFilteredItem()
356 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
357 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
358 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100359 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700360 }
361
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900362 @Override
363 public void onConfigurationChanged(Configuration newConfig) {
364 super.onConfigurationChanged(newConfig);
365 mAdapter.handlePackagesChanged();
366 }
367
Adam Powell4c470d62015-06-19 17:46:17 -0700368 /**
369 * Perform any initialization needed for voice interaction.
370 */
Adam Powell23882512016-01-29 10:21:00 -0800371 public void onSetupVoiceInteraction() {
Adam Powell4c470d62015-06-19 17:46:17 -0700372 // Do it right now. Subclasses may delay this and send it later.
373 sendVoiceChoicesIfNeeded();
374 }
375
Adam Powell23882512016-01-29 10:21:00 -0800376 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700377 if (!isVoiceInteraction()) {
378 // Clearly not needed.
379 return;
380 }
381
382
383 final Option[] options = new Option[mAdapter.getCount()];
384 for (int i = 0, N = options.length; i < N; i++) {
385 options[i] = optionForChooserTarget(mAdapter.getItem(i), i);
386 }
387
388 mPickOptionRequest = new PickTargetOptionRequest(
389 new Prompt(getTitle()), options, null);
390 getVoiceInteractor().submitRequest(mPickOptionRequest);
391 }
392
393 Option optionForChooserTarget(TargetInfo target, int index) {
394 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800395 }
396
Adam Powell2ed547e2015-04-29 18:45:04 -0700397 protected final void setAdditionalTargets(Intent[] intents) {
398 if (intents != null) {
399 for (Intent intent : intents) {
400 mIntents.add(intent);
401 }
402 }
403 }
404
Adam Powell0ccc0e92015-04-23 17:19:37 -0700405 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700406 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700407 }
408
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800409 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700410 final Uri referrer = getReferrer();
411 if (referrer != null && "android-app".equals(referrer.getScheme())) {
412 return referrer.getHost();
413 }
414 return null;
415 }
416
Adam Powell23882512016-01-29 10:21:00 -0800417 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700418 return R.layout.resolver_list;
419 }
420
Adam Powell88831a22014-11-20 18:17:00 -0800421 void bindProfileView() {
422 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
423 if (dri != null) {
424 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800425 View text = mProfileView.findViewById(R.id.profile_button);
426 if (!(text instanceof TextView)) {
427 text = mProfileView.findViewById(R.id.text1);
428 }
429 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800430 } else {
431 mProfileView.setVisibility(View.GONE);
432 }
Adam Powell278902c2014-07-12 18:33:22 -0700433 }
434
Sander Alewijnsef6545332014-10-31 12:39:02 +0000435 private void setProfileSwitchMessageId(int contentUserHint) {
436 if (contentUserHint != UserHandle.USER_CURRENT &&
437 contentUserHint != UserHandle.myUserId()) {
438 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
439 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
440 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
441 : false;
442 boolean targetIsManaged = userManager.isManagedProfile();
443 if (originIsManaged && !targetIsManaged) {
444 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
445 } else if (!originIsManaged && targetIsManaged) {
446 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
447 }
448 }
449 }
450
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700451 /**
452 * Turn on launch mode that is safe to use when forwarding intents received from
453 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
454 * instead of the normal Activity.startActivity for launching the activity selected
455 * by the user.
456 *
457 * <p>This mode is set to true by default if the activity is initialized through
458 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
459 * methods, it is set to false by default. You must set it before calling one of the
460 * more detailed onCreate methods, so that it will be set correctly in the case where
461 * there is only one intent to resolve and it is thus started immediately.</p>
462 */
463 public void setSafeForwardingMode(boolean safeForwarding) {
464 mSafeForwardingMode = safeForwarding;
465 }
466
shafik69df96322018-12-18 15:41:19 +0000467 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
468 final ActionTitle title = mResolvingHome
469 ? ActionTitle.HOME
470 : ActionTitle.forAction(intent.getAction());
471
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800472 // While there may already be a filtered item, we can only use it in the title if the list
473 // is already sorted and all information relevant to it is already in the list.
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800474 final boolean named = mAdapter.getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700475 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
476 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000477 } else if (isHttpSchemeAndViewAction(intent)) {
478 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000479 // they're giving access for the activity to open URLs from this specific host
480 return named
481 ? getString(ActionTitle.BROWSABLE_NAMED_TITLE_RES, intent.getData().getHost(),
482 mAdapter.getFilteredItem().getDisplayLabel())
483 : getString(ActionTitle.BROWSABLE_TITLE_RES, intent.getData().getHost());
Adam Powell278902c2014-07-12 18:33:22 -0700484 } else {
Adam Powell24428412015-04-01 17:19:56 -0700485 return named
486 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
487 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700488 }
Adam Powellc5878612012-05-04 18:42:38 -0700489 }
490
Adam Powelle9414d92014-07-05 17:44:18 -0700491 void dismiss() {
492 if (!isFinishing()) {
493 finish();
494 }
495 }
496
Adam Powellc5878612012-05-04 18:42:38 -0700497 Drawable getIcon(Resources res, int resId) {
498 Drawable result;
499 try {
500 result = res.getDrawableForDensity(resId, mIconDpi);
501 } catch (Resources.NotFoundException e) {
502 result = null;
503 }
504
505 return result;
506 }
507
508 Drawable loadIconForResolveInfo(ResolveInfo ri) {
509 Drawable dr;
510 try {
511 if (ri.resolvePackageName != null && ri.icon != 0) {
512 dr = getIcon(mPm.getResourcesForApplication(ri.resolvePackageName), ri.icon);
513 if (dr != null) {
Hyunyoung Songdadb9e12017-08-24 18:08:35 -0700514 return mIconFactory.getShadowedIcon(dr);
Adam Powellc5878612012-05-04 18:42:38 -0700515 }
516 }
517 final int iconRes = ri.getIconResource();
518 if (iconRes != 0) {
519 dr = getIcon(mPm.getResourcesForApplication(ri.activityInfo.packageName), iconRes);
520 if (dr != null) {
Hyunyoung Songdadb9e12017-08-24 18:08:35 -0700521 return mIconFactory.getShadowedIcon(dr);
Adam Powellc5878612012-05-04 18:42:38 -0700522 }
523 }
524 } catch (NameNotFoundException e) {
525 Log.e(TAG, "Couldn't find resources for package", e);
526 }
Hyunyoung Songdadb9e12017-08-24 18:08:35 -0700527 return mIconFactory.getBadgedIcon(ri.activityInfo.applicationInfo);
Adam Powellc5878612012-05-04 18:42:38 -0700528 }
529
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800530 @Override
531 protected void onRestart() {
532 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700533 if (!mRegistered) {
534 mPackageMonitor.register(this, getMainLooper(), false);
535 mRegistered = true;
536 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800537 mAdapter.handlePackagesChanged();
Adam Powell88831a22014-11-20 18:17:00 -0800538 if (mProfileView != null) {
539 bindProfileView();
540 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800541 }
542
543 @Override
544 protected void onStop() {
545 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700546 if (mRegistered) {
547 mPackageMonitor.unregister();
548 mRegistered = false;
549 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700550 final Intent intent = getIntent();
551 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100552 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700553 // This resolver is in the unusual situation where it has been
554 // launched at the top of a new task. We don't let it be added
555 // to the recent tasks shown to the user, and we need to make sure
556 // that each time we are launched we get the correct launching
557 // uid (not re-using the same resolver from an old launching uid),
558 // so we will now finish ourself since being no longer visible,
559 // the user probably can't get back to us.
560 if (!isChangingConfigurations()) {
561 finish();
562 }
563 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800564 }
565
Adam Powellc5878612012-05-04 18:42:38 -0700566 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700567 protected void onDestroy() {
568 super.onDestroy();
569 if (!isChangingConfigurations() && mPickOptionRequest != null) {
570 mPickOptionRequest.cancel();
571 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800572 if (mPostListReadyRunnable != null) {
573 getMainThreadHandler().removeCallbacks(mPostListReadyRunnable);
574 mPostListReadyRunnable = null;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800575 }
Kang Li38a6da642017-04-05 12:30:55 -0700576 if (mAdapter != null && mAdapter.mResolverListController != null) {
577 mAdapter.mResolverListController.destroy();
578 }
Adam Powell4c470d62015-06-19 17:46:17 -0700579 }
580
581 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700582 protected void onRestoreInstanceState(Bundle savedInstanceState) {
583 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000584 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700585 }
586
shafik0c7c5d52019-02-27 12:13:25 +0000587 private boolean isHttpSchemeAndViewAction(Intent intent) {
588 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
589 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
590 && Intent.ACTION_VIEW.equals(intent.getAction());
591 }
592
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100593 private boolean hasManagedProfile() {
594 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
595 if (userManager == null) {
596 return false;
597 }
598
599 try {
600 List<UserInfo> profiles = userManager.getProfiles(getUserId());
601 for (UserInfo userInfo : profiles) {
602 if (userInfo != null && userInfo.isManagedProfile()) {
603 return true;
604 }
605 }
606 } catch (SecurityException e) {
607 return false;
608 }
609 return false;
610 }
611
612 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
613 try {
614 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
615 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700616 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100617 } catch (NameNotFoundException e) {
618 return false;
619 }
620 }
621
Adam Powell278902c2014-07-12 18:33:22 -0700622 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
623 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100624 boolean enabled = false;
625 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700626 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800627 if (ri == null) {
628 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
629 return;
630 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
631 Log.e(TAG, "Attempted to set selection to resolve info for another user");
632 return;
633 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100634 enabled = true;
635 }
636 }
637 mAlwaysButton.setEnabled(enabled);
638 }
639
Adam Powellc5878612012-05-04 18:42:38 -0700640 public void onButtonClick(View v) {
641 final int id = v.getId();
shafik69df96322018-12-18 15:41:19 +0000642 int which = mAdapter.hasFilteredItem()
643 ? mAdapter.getFilteredPosition()
644 : mAdapterView.getCheckedItemPosition();
645 boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem();
646 if (id == R.id.button_app_settings) {
647 showSettingsForSelected(which, hasIndexBeenFiltered);
648 } else {
649 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
650 }
651 }
652
653 private void showSettingsForSelected(int which, boolean hasIndexBeenFiltered) {
654 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
shafik34064c32019-03-06 15:42:52 +0000655 Intent intent = new Intent();
656 // For browsers, we open the Default Browser page
657 // For regular apps, we open the Open by Default page
658 if (ri.handleAllWebDataURI) {
659 intent.setAction(Intent.ACTION_MANAGE_DEFAULT_APP)
660 .putExtra(Intent.EXTRA_ROLE_NAME, RoleManager.ROLE_BROWSER);
661 } else {
662 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
663 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
664 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
665 }
666 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700667 }
668
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800669 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700670 if (isFinishing()) {
671 return;
672 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800673 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000674 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
675 Toast.makeText(this, String.format(getResources().getString(
676 com.android.internal.R.string.activity_resolver_work_profiles_support),
677 ri.activityInfo.loadLabel(getPackageManager()).toString()),
678 Toast.LENGTH_LONG).show();
679 return;
680 }
681
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800682 TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800683 if (target == null) {
684 return;
685 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700686 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800687 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100688 MetricsLogger.action(
689 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800690 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100691 MetricsLogger.action(
692 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
693 } else {
694 MetricsLogger.action(
695 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
696 }
697 MetricsLogger.action(this, mAdapter.hasFilteredItem()
698 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
699 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700700 finish();
701 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703
Adam Powelle49d9392014-07-17 18:45:19 -0700704 /**
705 * Replace me in subclasses!
706 */
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000707 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700708 return defIntent;
709 }
710
Adam Powell2ed547e2015-04-29 18:45:04 -0700711 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700712 final ResolveInfo ri = target.getResolveInfo();
713 final Intent intent = target != null ? target.getResolvedIntent() : null;
714
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800715 if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800716 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700717 // Build a reasonable intent filter, based on what matched.
718 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200719 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720
Henrik Engström3277cf12014-07-17 12:18:29 +0200721 if (intent.getSelector() != null) {
722 filterIntent = intent.getSelector();
723 } else {
724 filterIntent = intent;
725 }
726
727 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800728 if (action != null) {
729 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200731 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700732 if (categories != null) {
733 for (String cat : categories) {
734 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700737 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738
Adam Powell24428412015-04-01 17:19:56 -0700739 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200740 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700741 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200742 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700743 if (mimeType != null) {
744 try {
745 filter.addDataType(mimeType);
746 } catch (IntentFilter.MalformedMimeTypeException e) {
747 Log.w("ResolverActivity", e);
748 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700751 }
752 if (data != null && data.getScheme() != null) {
753 // We need the data specification if there was no type,
754 // OR if the scheme is not one of our magical "file:"
755 // or "content:" schemes (see IntentFilter for the reason).
756 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
757 || (!"file".equals(data.getScheme())
758 && !"content".equals(data.getScheme()))) {
759 filter.addDataScheme(data.getScheme());
760
761 // Look through the resolved filter to determine which part
762 // of it matched the original Intent.
763 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
764 if (pIt != null) {
765 String ssp = data.getSchemeSpecificPart();
766 while (ssp != null && pIt.hasNext()) {
767 PatternMatcher p = pIt.next();
768 if (p.match(ssp)) {
769 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
770 break;
771 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700772 }
773 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700774 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
775 if (aIt != null) {
776 while (aIt.hasNext()) {
777 IntentFilter.AuthorityEntry a = aIt.next();
778 if (a.match(data) >= 0) {
779 int port = a.getPort();
780 filter.addDataAuthority(a.getHost(),
781 port >= 0 ? Integer.toString(port) : null);
782 break;
783 }
784 }
785 }
786 pIt = ri.filter.pathsIterator();
787 if (pIt != null) {
788 String path = data.getPath();
789 while (path != null && pIt.hasNext()) {
790 PatternMatcher p = pIt.next();
791 if (p.match(path)) {
792 filter.addDataPath(p.getPath(), p.getType());
793 break;
794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
796 }
797 }
798 }
799
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700800 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800801 final int N = mAdapter.mUnfilteredResolveList.size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800802 ComponentName[] set;
803 // If we don't add back in the component for forwarding the intent to a managed
804 // profile, the preferred activity may not be updated correctly (as the set of
805 // components we tell it we knew about will have changed).
806 final boolean needToAddBackProfileForwardingComponent
807 = mAdapter.mOtherProfile != null;
808 if (!needToAddBackProfileForwardingComponent) {
809 set = new ComponentName[N];
810 } else {
811 set = new ComponentName[N + 1];
812 }
813
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700814 int bestMatch = 0;
815 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800816 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700817 set[i] = new ComponentName(r.activityInfo.packageName,
818 r.activityInfo.name);
819 if (r.match > bestMatch) bestMatch = r.match;
820 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800821
822 if (needToAddBackProfileForwardingComponent) {
823 set[N] = mAdapter.mOtherProfile.getResolvedComponentName();
824 final int otherProfileMatch = mAdapter.mOtherProfile.getResolveInfo().match;
825 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
826 }
827
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700828 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700829 final int userId = getUserId();
830 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800831
832 // Set the preferred Activity
833 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
834
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700835 if (ri.handleAllWebDataURI) {
836 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700837 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700838 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700839 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700840 }
841 } else {
842 // Update Domain Verification status
843 ComponentName cn = intent.getComponent();
844 String packageName = cn.getPackageName();
845 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800846
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700847 boolean isHttpOrHttps = (dataScheme != null) &&
848 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
849 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800850
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700851 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
852 boolean hasCategoryBrowsable = (categories != null) &&
853 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800854
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700855 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700856 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700857 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
858 userId);
859 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800860 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700861 } else {
862 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800863 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700864 } catch (RemoteException re) {
865 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
866 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 }
869 }
870
Adam Powell24428412015-04-01 17:19:56 -0700871 if (target != null) {
872 safelyStartActivity(target);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700873 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700874 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700875 }
876
Adam Powell23882512016-01-29 10:21:00 -0800877 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -0700878 // We're dispatching intents that might be coming from legacy apps, so
879 // don't kill ourselves.
880 StrictMode.disableDeathOnFileUriExposure();
881 try {
882 safelyStartActivityInternal(cti);
883 } finally {
884 StrictMode.enableDeathOnFileUriExposure();
885 }
886 }
887
888 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +0000889 // If needed, show that intent is forwarded
890 // from managed profile to owner or other way around.
891 if (mProfileSwitchMessageId != -1) {
892 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
893 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700894 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -0700895 if (cti.start(this, null)) {
896 onActivityStarted(cti);
897 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700898 return;
899 }
900 try {
Adam Powell24428412015-04-01 17:19:56 -0700901 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
902 onActivityStarted(cti);
903 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700904 } catch (RuntimeException e) {
905 String launchedFromPackage;
906 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700907 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700908 getActivityToken());
909 } catch (RemoteException e2) {
910 launchedFromPackage = "??";
911 }
912 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
913 + " package " + launchedFromPackage + ", while running in "
914 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
917
Alison Cichowlas3e340502018-08-07 17:15:01 -0400918
919 boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
920 int userId) {
921 // Pass intent to delegate chooser activity with permission token.
922 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
923 // moves into systemui
924 try {
925 // TODO: Once this is a small springboard activity, it can move off the UI process
926 // and we can move the request method to ActivityManagerInternal.
927 IBinder permissionToken = ActivityTaskManager.getService()
928 .requestStartActivityPermissionToken(getActivityToken());
929 final Intent chooserIntent = new Intent();
930 final ComponentName delegateActivity = ComponentName.unflattenFromString(
931 Resources.getSystem().getString(R.string.config_chooserActivity));
932 chooserIntent.setClassName(delegateActivity.getPackageName(),
933 delegateActivity.getClassName());
934 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
935
936 // TODO: These extras will change as chooser activity moves into systemui
937 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
938 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
939 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
940 ignoreTargetSecurity);
941 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -0500942 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
943 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -0400944 startActivity(chooserIntent);
945 } catch (RemoteException e) {
946 Log.e(TAG, e.toString());
947 }
948 return true;
949 }
950
Adam Powell23882512016-01-29 10:21:00 -0800951 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -0700952 // Do nothing
953 }
954
Adam Powell23882512016-01-29 10:21:00 -0800955 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -0700956 return false;
957 }
958
Adam Powell23882512016-01-29 10:21:00 -0800959 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700960 return true;
961 }
962
Adam Powell23882512016-01-29 10:21:00 -0800963 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700964 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -0700965 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -0700966 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -0700967 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -0700968 }
969
Adam Powell23882512016-01-29 10:21:00 -0800970 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents,
Adam Powell7d758002015-05-06 17:49:36 -0700971 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
972 boolean filterLastUsed) {
973 return new ResolveListAdapter(context, payloadIntents, initialIntents, rList,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800974 launchedFromUid, filterLastUsed, createListController());
975 }
976
977 @VisibleForTesting
978 protected ResolverListController createListController() {
979 return new ResolverListController(
980 this,
981 mPm,
982 getTargetIntent(),
983 getReferrerPackageName(),
984 mLaunchedFromUid);
Adam Powell88831a22014-11-20 18:17:00 -0800985 }
986
Adam Powell39e94eb2015-09-08 17:01:49 -0700987 /**
988 * Returns true if the activity is finishing and creation should halt
989 */
Adam Powell23882512016-01-29 10:21:00 -0800990 public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents,
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800991 List<ResolveInfo> rList) {
Dianne Hackborn57dd7372015-07-27 18:11:14 -0700992 // The last argument of createAdapter is whether to do special handling
993 // of the last used choice to highlight it in the list. We need to always
994 // turn this off when running under voice interaction, since it results in
995 // a more complicated UI that the current voice interaction flow is not able
996 // to handle.
Adam Powell7d758002015-05-06 17:49:36 -0700997 mAdapter = createAdapter(this, payloadIntents, initialIntents, rList,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800998 mLaunchedFromUid, mSupportsAlwaysUseOption && !isVoiceInteraction());
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800999 boolean rebuildCompleted = mAdapter.rebuildList();
Adam Powell7d758002015-05-06 17:49:36 -07001000
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001001 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001002 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001003 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001004 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001005 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001006 setContentView(mLayoutId);
Adam Powell7d758002015-05-06 17:49:36 -07001007
Adam Powell50077352015-05-26 18:01:55 -07001008 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001009
1010 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1011 // we're already done, we can check if we should auto-launch immediately.
1012 if (rebuildCompleted) {
1013 if (count == 1 && mAdapter.getOtherProfile() == null) {
1014 // Only one target, so we're a candidate to auto-launch!
1015 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
1016 if (shouldAutoLaunchSingleChoice(target)) {
1017 safelyStartActivity(target);
1018 mPackageMonitor.unregister();
1019 mRegistered = false;
1020 finish();
1021 return true;
1022 }
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001023 }
1024 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001025
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001026
Alan Viverette51efddb2017-04-05 10:00:01 -04001027 mAdapterView = findViewById(R.id.resolver_list);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001028
1029 if (count == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001030 final TextView emptyView = findViewById(R.id.empty);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001031 emptyView.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -07001032 mAdapterView.setVisibility(View.GONE);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001033 } else {
1034 mAdapterView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001035 onPrepareAdapterView(mAdapterView, mAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001036 }
Adam Powell39e94eb2015-09-08 17:01:49 -07001037 return false;
Adam Powell7d758002015-05-06 17:49:36 -07001038 }
1039
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001040 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001041 final boolean useHeader = adapter.hasFilteredItem();
1042 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
1043
1044 adapterView.setAdapter(mAdapter);
1045
1046 final ItemClickListener listener = new ItemClickListener();
1047 adapterView.setOnItemClickListener(listener);
1048 adapterView.setOnItemLongClickListener(listener);
1049
shafik69df96322018-12-18 15:41:19 +00001050 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001051 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1052 }
1053
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001054 // In case this method is called again (due to activity recreation), avoid adding a new
1055 // header if one is already present.
1056 if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) {
Adam Powell7d758002015-05-06 17:49:36 -07001057 listView.addHeaderView(LayoutInflater.from(this).inflate(
1058 R.layout.resolver_different_item_header, listView, false));
1059 }
Adam Powell24428412015-04-01 17:19:56 -07001060 }
1061
Matt Pietal26038402019-01-08 07:29:34 -05001062 /**
1063 * Configure the area above the app selection list (title, content preview, etc).
1064 */
1065 public void setHeader() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001066 if (mAdapter.getCount() == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001067 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001068 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001069 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001070 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001071 }
1072
1073 CharSequence title = mTitle != null
1074 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001075 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001076
1077 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001078 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001079 if (titleView != null) {
1080 titleView.setText(title);
1081 }
1082 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001083 }
1084
Alan Viverette51efddb2017-04-05 10:00:01 -04001085 final ImageView iconView = findViewById(R.id.icon);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001086 final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem();
1087 if (iconView != null && iconInfo != null) {
1088 new LoadIconIntoViewTask(iconInfo, iconView).execute();
1089 }
1090 }
1091
shafik69df96322018-12-18 15:41:19 +00001092 private void resetButtonBar() {
1093 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1094 return;
1095 }
1096 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1097 if (buttonLayout != null) {
1098 buttonLayout.setVisibility(View.VISIBLE);
1099 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
1100 mSettingsButton = (Button) buttonLayout.findViewById(R.id.button_app_settings);
1101 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1102
1103 if (mUseLayoutForBrowsables) {
1104 resetSettingsOrOnceButtonBar();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001105 } else {
shafik69df96322018-12-18 15:41:19 +00001106 resetAlwaysOrOnceButtonBar();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001107 }
shafik69df96322018-12-18 15:41:19 +00001108 } else {
1109 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1110 }
1111 }
1112
1113 private void resetSettingsOrOnceButtonBar() {
1114 //unsetting always button
1115 mAlwaysButton.setVisibility(View.GONE);
1116
1117 // When the items load in, if an item was already selected,
1118 // enable the buttons
1119 if (mAdapterView != null
1120 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1121 mSettingsButton.setEnabled(true);
1122 mOnceButton.setEnabled(true);
1123 }
1124 }
1125
1126 private void resetAlwaysOrOnceButtonBar() {
1127 // This check needs to be made because layout with default
1128 // doesn't have a settings button
1129 if (mSettingsButton != null) {
1130 //unsetting always button
1131 mSettingsButton.setVisibility(View.GONE);
1132 mSettingsButton = null;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001133 }
1134
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001135 if (useLayoutWithDefault()
1136 && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001137 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
1138 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001139 return;
1140 }
1141
1142 // When the items load in, if an item was already selected, enable the buttons
1143 if (mAdapterView != null
1144 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1145 setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true);
1146 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001147 }
1148 }
1149
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001150 private boolean useLayoutWithDefault() {
1151 return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem();
1152 }
1153
Adam Powellc412be62015-06-24 13:54:10 -07001154 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001155 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1156 * called and we are launched in a new task.
1157 */
1158 protected void setRetainInOnStop(boolean retainInOnStop) {
1159 mRetainInOnStop = retainInOnStop;
1160 }
1161
1162 /**
Adam Powellc412be62015-06-24 13:54:10 -07001163 * Check a simple match for the component of two ResolveInfos.
1164 */
1165 static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
1166 return lhs == null ? rhs == null
1167 : lhs.activityInfo == null ? rhs.activityInfo == null
1168 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1169 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1170 }
1171
Adam Powell23882512016-01-29 10:21:00 -08001172 public final class DisplayResolveInfo implements TargetInfo {
Adam Powell24428412015-04-01 17:19:56 -07001173 private final ResolveInfo mResolveInfo;
1174 private final CharSequence mDisplayLabel;
1175 private Drawable mDisplayIcon;
Adam Powell00f4aad2015-09-17 13:38:16 -07001176 private Drawable mBadge;
Adam Powell24428412015-04-01 17:19:56 -07001177 private final CharSequence mExtendedInfo;
1178 private final Intent mResolvedIntent;
Adam Powell2ed547e2015-04-29 18:45:04 -07001179 private final List<Intent> mSourceIntents = new ArrayList<>();
Adam Powell23882512016-01-29 10:21:00 -08001180 private boolean mPinned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181
Adam Powell23882512016-01-29 10:21:00 -08001182 public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel,
Dianne Hackborneb034652009-09-07 00:49:58 -07001183 CharSequence pInfo, Intent pOrigIntent) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001184 mSourceIntents.add(originalIntent);
Adam Powell24428412015-04-01 17:19:56 -07001185 mResolveInfo = pri;
1186 mDisplayLabel = pLabel;
1187 mExtendedInfo = pInfo;
1188
1189 final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent :
Adam Powell2ed547e2015-04-29 18:45:04 -07001190 getReplacementIntent(pri.activityInfo, getTargetIntent()));
Adam Powell24428412015-04-01 17:19:56 -07001191 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1192 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
1193 final ActivityInfo ai = mResolveInfo.activityInfo;
1194 intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));
1195
1196 mResolvedIntent = intent;
1197 }
1198
Adam Powell2ed547e2015-04-29 18:45:04 -07001199 private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) {
1200 mSourceIntents.addAll(other.getAllSourceIntents());
1201 mResolveInfo = other.mResolveInfo;
1202 mDisplayLabel = other.mDisplayLabel;
1203 mDisplayIcon = other.mDisplayIcon;
1204 mExtendedInfo = other.mExtendedInfo;
1205 mResolvedIntent = new Intent(other.mResolvedIntent);
1206 mResolvedIntent.fillIn(fillInIntent, flags);
Adam Powell23882512016-01-29 10:21:00 -08001207 mPinned = other.mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001208 }
1209
Adam Powell24428412015-04-01 17:19:56 -07001210 public ResolveInfo getResolveInfo() {
1211 return mResolveInfo;
1212 }
1213
1214 public CharSequence getDisplayLabel() {
1215 return mDisplayLabel;
1216 }
1217
1218 public Drawable getDisplayIcon() {
1219 return mDisplayIcon;
1220 }
1221
Adam Powell7d758002015-05-06 17:49:36 -07001222 public Drawable getBadgeIcon() {
Adam Powell00f4aad2015-09-17 13:38:16 -07001223 // We only expose a badge if we have extended info.
1224 // The badge is a higher-priority disambiguation signal
1225 // but we don't need one if we wouldn't show extended info at all.
1226 if (TextUtils.isEmpty(getExtendedInfo())) {
1227 return null;
1228 }
1229
1230 if (mBadge == null && mResolveInfo != null && mResolveInfo.activityInfo != null
1231 && mResolveInfo.activityInfo.applicationInfo != null) {
1232 if (mResolveInfo.activityInfo.icon == 0 || mResolveInfo.activityInfo.icon
1233 == mResolveInfo.activityInfo.applicationInfo.icon) {
1234 // Badging an icon with exactly the same icon is silly.
1235 // If the activityInfo icon resid is 0 it will fall back
1236 // to the application's icon, making it a match.
1237 return null;
1238 }
1239 mBadge = mResolveInfo.activityInfo.applicationInfo.loadIcon(mPm);
1240 }
1241 return mBadge;
Adam Powell7d758002015-05-06 17:49:36 -07001242 }
1243
Adam Powell2ed547e2015-04-29 18:45:04 -07001244 @Override
Alan Viverettece5d92c2015-07-31 16:46:56 -04001245 public CharSequence getBadgeContentDescription() {
1246 return null;
1247 }
1248
1249 @Override
Adam Powell2ed547e2015-04-29 18:45:04 -07001250 public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) {
1251 return new DisplayResolveInfo(this, fillInIntent, flags);
1252 }
1253
1254 @Override
1255 public List<Intent> getAllSourceIntents() {
1256 return mSourceIntents;
1257 }
1258
1259 public void addAlternateSourceIntent(Intent alt) {
1260 mSourceIntents.add(alt);
1261 }
1262
Adam Powell24428412015-04-01 17:19:56 -07001263 public void setDisplayIcon(Drawable icon) {
1264 mDisplayIcon = icon;
1265 }
1266
1267 public boolean hasDisplayIcon() {
1268 return mDisplayIcon != null;
1269 }
1270
1271 public CharSequence getExtendedInfo() {
1272 return mExtendedInfo;
1273 }
1274
1275 public Intent getResolvedIntent() {
1276 return mResolvedIntent;
1277 }
1278
1279 @Override
1280 public ComponentName getResolvedComponentName() {
1281 return new ComponentName(mResolveInfo.activityInfo.packageName,
1282 mResolveInfo.activityInfo.name);
1283 }
1284
1285 @Override
1286 public boolean start(Activity activity, Bundle options) {
1287 activity.startActivity(mResolvedIntent, options);
1288 return true;
1289 }
1290
1291 @Override
Alison Cichowlas3e340502018-08-07 17:15:01 -04001292 public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) {
Alison Cichowlas4691ed42018-11-13 15:59:55 -05001293
1294 if (mEnableChooserDelegate) {
1295 return activity.startAsCallerImpl(mResolvedIntent, options, false, userId);
1296 } else {
1297 activity.startActivityAsCaller(mResolvedIntent, options, null, false, userId);
1298 return true;
1299 }
Adam Powell24428412015-04-01 17:19:56 -07001300 }
1301
1302 @Override
1303 public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
1304 activity.startActivityAsUser(mResolvedIntent, options, user);
1305 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 }
Adam Powell23882512016-01-29 10:21:00 -08001307
1308 @Override
1309 public boolean isPinned() {
1310 return mPinned;
1311 }
1312
1313 public void setPinned(boolean pinned) {
1314 mPinned = pinned;
1315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
1317
Adam Powell24428412015-04-01 17:19:56 -07001318 /**
1319 * A single target as represented in the chooser.
1320 */
1321 public interface TargetInfo {
1322 /**
1323 * Get the resolved intent that represents this target. Note that this may not be the
1324 * intent that will be launched by calling one of the <code>start</code> methods provided;
1325 * this is the intent that will be credited with the launch.
1326 *
1327 * @return the resolved intent for this target
1328 */
Adam Powell23882512016-01-29 10:21:00 -08001329 Intent getResolvedIntent();
Adam Powell24428412015-04-01 17:19:56 -07001330
1331 /**
1332 * Get the resolved component name that represents this target. Note that this may not
1333 * be the component that will be directly launched by calling one of the <code>start</code>
1334 * methods provided; this is the component that will be credited with the launch.
1335 *
1336 * @return the resolved ComponentName for this target
1337 */
Adam Powell23882512016-01-29 10:21:00 -08001338 ComponentName getResolvedComponentName();
Adam Powell24428412015-04-01 17:19:56 -07001339
1340 /**
1341 * Start the activity referenced by this target.
1342 *
1343 * @param activity calling Activity performing the launch
1344 * @param options ActivityOptions bundle
1345 * @return true if the start completed successfully
1346 */
Adam Powell23882512016-01-29 10:21:00 -08001347 boolean start(Activity activity, Bundle options);
Adam Powell24428412015-04-01 17:19:56 -07001348
1349 /**
1350 * Start the activity referenced by this target as if the ResolverActivity's caller
1351 * was performing the start operation.
1352 *
1353 * @param activity calling Activity (actually) performing the launch
1354 * @param options ActivityOptions bundle
1355 * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller
1356 * @return true if the start completed successfully
1357 */
Alison Cichowlas3e340502018-08-07 17:15:01 -04001358 boolean startAsCaller(ResolverActivity activity, Bundle options, int userId);
Adam Powell24428412015-04-01 17:19:56 -07001359
1360 /**
1361 * Start the activity referenced by this target as a given user.
1362 *
1363 * @param activity calling activity performing the launch
1364 * @param options ActivityOptions bundle
1365 * @param user handle for the user to start the activity as
1366 * @return true if the start completed successfully
1367 */
Adam Powell23882512016-01-29 10:21:00 -08001368 boolean startAsUser(Activity activity, Bundle options, UserHandle user);
Adam Powell24428412015-04-01 17:19:56 -07001369
1370 /**
1371 * Return the ResolveInfo about how and why this target matched the original query
1372 * for available targets.
1373 *
1374 * @return ResolveInfo representing this target's match
1375 */
Adam Powell23882512016-01-29 10:21:00 -08001376 ResolveInfo getResolveInfo();
Adam Powell24428412015-04-01 17:19:56 -07001377
1378 /**
1379 * Return the human-readable text label for this target.
1380 *
1381 * @return user-visible target label
1382 */
Adam Powell23882512016-01-29 10:21:00 -08001383 CharSequence getDisplayLabel();
Adam Powell24428412015-04-01 17:19:56 -07001384
1385 /**
1386 * Return any extended info for this target. This may be used to disambiguate
1387 * otherwise identical targets.
1388 *
1389 * @return human-readable disambig string or null if none present
1390 */
Adam Powell23882512016-01-29 10:21:00 -08001391 CharSequence getExtendedInfo();
Adam Powell24428412015-04-01 17:19:56 -07001392
1393 /**
1394 * @return The drawable that should be used to represent this target
1395 */
Adam Powell23882512016-01-29 10:21:00 -08001396 Drawable getDisplayIcon();
Adam Powell2ed547e2015-04-29 18:45:04 -07001397
1398 /**
Adam Powell7d758002015-05-06 17:49:36 -07001399 * @return The (small) icon to badge the target with
1400 */
Adam Powell23882512016-01-29 10:21:00 -08001401 Drawable getBadgeIcon();
Adam Powell7d758002015-05-06 17:49:36 -07001402
1403 /**
Alan Viverettece5d92c2015-07-31 16:46:56 -04001404 * @return The content description for the badge icon
1405 */
Adam Powell23882512016-01-29 10:21:00 -08001406 CharSequence getBadgeContentDescription();
Alan Viverettece5d92c2015-07-31 16:46:56 -04001407
1408 /**
Adam Powell2ed547e2015-04-29 18:45:04 -07001409 * Clone this target with the given fill-in information.
1410 */
Adam Powell23882512016-01-29 10:21:00 -08001411 TargetInfo cloneFilledIn(Intent fillInIntent, int flags);
Adam Powell2ed547e2015-04-29 18:45:04 -07001412
1413 /**
1414 * @return the list of supported source intents deduped against this single target
1415 */
Adam Powell23882512016-01-29 10:21:00 -08001416 List<Intent> getAllSourceIntents();
1417
1418 /**
1419 * @return true if this target should be pinned to the front by the request of the user
1420 */
1421 boolean isPinned();
Adam Powell24428412015-04-01 17:19:56 -07001422 }
1423
Adam Powell23882512016-01-29 10:21:00 -08001424 public class ResolveListAdapter extends BaseAdapter {
Adam Powell7d758002015-05-06 17:49:36 -07001425 private final List<Intent> mIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001426 private final Intent[] mInitialIntents;
1427 private final List<ResolveInfo> mBaseResolveList;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001428 protected ResolveInfo mLastChosen;
Adam Powell88831a22014-11-20 18:17:00 -08001429 private DisplayResolveInfo mOtherProfile;
Adam Powell24428412015-04-01 17:19:56 -07001430 private boolean mHasExtendedInfo;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001431 private ResolverListController mResolverListController;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001432 private int mPlaceholderCount;
Adam Powell24428412015-04-01 17:19:56 -07001433
1434 protected final LayoutInflater mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435
Adam Powell2ed547e2015-04-29 18:45:04 -07001436 List<DisplayResolveInfo> mDisplayList;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001437 List<ResolvedComponentInfo> mUnfilteredResolveList;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001438
Adam Powell278902c2014-07-12 18:33:22 -07001439 private int mLastChosenPosition = -1;
1440 private boolean mFilterLastUsed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441
Adam Powell7d758002015-05-06 17:49:36 -07001442 public ResolveListAdapter(Context context, List<Intent> payloadIntents,
1443 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001444 boolean filterLastUsed,
1445 ResolverListController resolverListController) {
Adam Powell7d758002015-05-06 17:49:36 -07001446 mIntents = payloadIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001447 mInitialIntents = initialIntents;
1448 mBaseResolveList = rList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001449 mLaunchedFromUid = launchedFromUid;
Adam Powelle9414d92014-07-05 17:44:18 -07001450 mInflater = LayoutInflater.from(context);
Adam Powell2ed547e2015-04-29 18:45:04 -07001451 mDisplayList = new ArrayList<>();
Adam Powell278902c2014-07-12 18:33:22 -07001452 mFilterLastUsed = filterLastUsed;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001453 mResolverListController = resolverListController;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001456 public void handlePackagesChanged() {
1457 rebuildList();
Esteban Talavera6de72662014-12-11 17:54:07 +00001458 if (getCount() == 0) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001459 // We no longer have any items... just finish the activity.
1460 finish();
Adam Powellc5878612012-05-04 18:42:38 -07001461 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001462 }
1463
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001464 public void setPlaceholderCount(int count) {
1465 mPlaceholderCount = count;
1466 }
1467
Sumir Katariadb688af2017-05-10 17:33:47 -07001468 public int getPlaceholderCount() { return mPlaceholderCount; }
1469
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001470 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001471 public DisplayResolveInfo getFilteredItem() {
1472 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1473 // Not using getItem since it offsets to dodge this position for the list
Adam Powell2ed547e2015-04-29 18:45:04 -07001474 return mDisplayList.get(mLastChosenPosition);
Adam Powell278902c2014-07-12 18:33:22 -07001475 }
1476 return null;
1477 }
1478
Adam Powell88831a22014-11-20 18:17:00 -08001479 public DisplayResolveInfo getOtherProfile() {
1480 return mOtherProfile;
1481 }
1482
Adam Powell278902c2014-07-12 18:33:22 -07001483 public int getFilteredPosition() {
1484 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1485 return mLastChosenPosition;
1486 }
1487 return AbsListView.INVALID_POSITION;
1488 }
1489
1490 public boolean hasFilteredItem() {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001491 return mFilterLastUsed && mLastChosen != null;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001492 }
1493
Adam Powella182e452015-07-06 16:57:56 -07001494 public float getScore(DisplayResolveInfo target) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001495 return mResolverListController.getScore(target);
Adam Powella182e452015-07-06 16:57:56 -07001496 }
1497
Kang Li0cef910d2017-01-05 09:14:36 -08001498 public void updateModel(ComponentName componentName) {
1499 mResolverListController.updateModel(componentName);
1500 }
1501
Kang Li9fa2a2c2017-01-06 13:33:24 -08001502 public void updateChooserCounts(String packageName, int userId, String action) {
1503 mResolverListController.updateChooserCounts(packageName, userId, action);
1504 }
1505
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001506 /**
1507 * Rebuild the list of resolvers. In some cases some parts will need some asynchronous work
1508 * to complete.
1509 *
1510 * @return Whether or not the list building is completed.
1511 */
1512 protected boolean rebuildList() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001513 List<ResolvedComponentInfo> currentResolveList = null;
Sudheer Shanka7e64e102015-01-23 10:37:45 +00001514 // Clear the value of mOtherProfile from previous call.
1515 mOtherProfile = null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001516 mLastChosen = null;
1517 mLastChosenPosition = -1;
Adam Powell2ed547e2015-04-29 18:45:04 -07001518 mDisplayList.clear();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001519 if (mBaseResolveList != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001520 currentResolveList = mUnfilteredResolveList = new ArrayList<>();
1521 mResolverListController.addResolveListDedupe(currentResolveList,
1522 getTargetIntent(),
1523 mBaseResolveList);
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001524 } else {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001525 currentResolveList = mUnfilteredResolveList =
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001526 mResolverListController.getResolversForIntent(shouldGetResolvedFilter(),
1527 shouldGetActivityMetadata(),
1528 mIntents);
1529 if (currentResolveList == null) {
1530 processSortedList(currentResolveList);
1531 return true;
Adam Powell2ed547e2015-04-29 18:45:04 -07001532 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001533 List<ResolvedComponentInfo> originalList =
1534 mResolverListController.filterIneligibleActivities(currentResolveList,
1535 true);
1536 if (originalList != null) {
1537 mUnfilteredResolveList = originalList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001538 }
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001539 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001540
1541 // So far we only support a single other profile at a time.
1542 // The first one we see gets special treatment.
1543 for (ResolvedComponentInfo info : currentResolveList) {
1544 if (info.getResolveInfoAt(0).targetUserId != UserHandle.USER_CURRENT) {
1545 mOtherProfile = new DisplayResolveInfo(info.getIntentAt(0),
1546 info.getResolveInfoAt(0),
1547 info.getResolveInfoAt(0).loadLabel(mPm),
1548 info.getResolveInfoAt(0).loadLabel(mPm),
1549 getReplacementIntent(info.getResolveInfoAt(0).activityInfo,
1550 info.getIntentAt(0)));
1551 currentResolveList.remove(info);
1552 break;
1553 }
1554 }
1555
1556 if (mOtherProfile == null) {
1557 try {
1558 mLastChosen = mResolverListController.getLastChosen();
1559 } catch (RemoteException re) {
1560 Log.d(TAG, "Error calling getLastChosenActivity\n" + re);
1561 }
1562 }
1563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 int N;
You Kim43a5070e2012-11-21 23:23:45 +09001565 if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001566 // We only care about fixing the unfilteredList if the current resolve list and
1567 // current resolve list are currently the same.
1568 List<ResolvedComponentInfo> originalList =
1569 mResolverListController.filterLowPriority(currentResolveList,
1570 mUnfilteredResolveList == currentResolveList);
1571 if (originalList != null) {
1572 mUnfilteredResolveList = originalList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001574
Hakan Seyalioglu873cbfd2017-02-21 19:23:43 -08001575 if (currentResolveList.size() > 1) {
Sumir Katariadb688af2017-05-10 17:33:47 -07001576 int placeholderCount = currentResolveList.size();
1577 if (useLayoutWithDefault()) {
1578 --placeholderCount;
1579 }
1580 setPlaceholderCount(placeholderCount);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001581 AsyncTask<List<ResolvedComponentInfo>,
1582 Void,
1583 List<ResolvedComponentInfo>> sortingTask =
1584 new AsyncTask<List<ResolvedComponentInfo>,
1585 Void,
1586 List<ResolvedComponentInfo>>() {
1587 @Override
1588 protected List<ResolvedComponentInfo> doInBackground(
1589 List<ResolvedComponentInfo>... params) {
1590 mResolverListController.sort(params[0]);
1591 return params[0];
1592 }
1593
1594 @Override
1595 protected void onPostExecute(List<ResolvedComponentInfo> sortedComponents) {
1596 processSortedList(sortedComponents);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001597 if (mProfileView != null) {
1598 bindProfileView();
1599 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001600 notifyDataSetChanged();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001601 }
1602 };
1603 sortingTask.execute(currentResolveList);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001604 postListReadyRunnable();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001605 return false;
1606 } else {
1607 processSortedList(currentResolveList);
1608 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001610 } else {
1611 processSortedList(currentResolveList);
1612 return true;
1613 }
1614 }
1615
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001616 private void processSortedList(List<ResolvedComponentInfo> sortedComponents) {
1617 int N;
1618 if (sortedComponents != null && (N = sortedComponents.size()) != 0) {
Dianne Hackborneb034652009-09-07 00:49:58 -07001619 // First put the initial items at the top.
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001620 if (mInitialIntents != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001621 for (int i = 0; i < mInitialIntents.length; i++) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001622 Intent ii = mInitialIntents[i];
Dianne Hackborneb034652009-09-07 00:49:58 -07001623 if (ii == null) {
1624 continue;
1625 }
1626 ActivityInfo ai = ii.resolveActivityInfo(
1627 getPackageManager(), 0);
1628 if (ai == null) {
Adam Powell09a65602014-07-20 16:23:14 -07001629 Log.w(TAG, "No activity found for " + ii);
Dianne Hackborneb034652009-09-07 00:49:58 -07001630 continue;
1631 }
1632 ResolveInfo ri = new ResolveInfo();
1633 ri.activityInfo = ai;
Nicolas Prevot1a815922014-10-10 16:22:38 +01001634 UserManager userManager =
1635 (UserManager) getSystemService(Context.USER_SERVICE);
Dianne Hackborneb034652009-09-07 00:49:58 -07001636 if (ii instanceof LabeledIntent) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001637 LabeledIntent li = (LabeledIntent) ii;
Dianne Hackborneb034652009-09-07 00:49:58 -07001638 ri.resolvePackageName = li.getSourcePackage();
1639 ri.labelRes = li.getLabelResource();
1640 ri.nonLocalizedLabel = li.getNonLocalizedLabel();
1641 ri.icon = li.getIconResource();
Sudheer Shanka9ded7602015-05-19 21:17:25 +01001642 ri.iconResourceId = ri.icon;
1643 }
1644 if (userManager.isManagedProfile()) {
1645 ri.noResourceId = true;
1646 ri.icon = 0;
Dianne Hackborneb034652009-09-07 00:49:58 -07001647 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001648 addResolveInfo(new DisplayResolveInfo(ii, ri,
Dianne Hackborneb034652009-09-07 00:49:58 -07001649 ri.loadLabel(getPackageManager()), null, ii));
1650 }
1651 }
You Kim43a5070e2012-11-21 23:23:45 +09001652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 // Check for applications with same name and use application name or
1654 // package name if necessary
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001655 ResolvedComponentInfo rci0 = sortedComponents.get(0);
1656 ResolveInfo r0 = rci0.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 int start = 0;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001658 CharSequence r0Label = r0.loadLabel(mPm);
Adam Powell24428412015-04-01 17:19:56 -07001659 mHasExtendedInfo = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 for (int i = 1; i < N; i++) {
1661 if (r0Label == null) {
1662 r0Label = r0.activityInfo.packageName;
1663 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001664 ResolvedComponentInfo rci = sortedComponents.get(i);
Adam Powell2ed547e2015-04-29 18:45:04 -07001665 ResolveInfo ri = rci.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 CharSequence riLabel = ri.loadLabel(mPm);
1667 if (riLabel == null) {
1668 riLabel = ri.activityInfo.packageName;
1669 }
1670 if (riLabel.equals(r0Label)) {
1671 continue;
1672 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001673 processGroup(sortedComponents, start, (i - 1), rci0, r0Label);
Adam Powell2ed547e2015-04-29 18:45:04 -07001674 rci0 = rci;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 r0 = ri;
1676 r0Label = riLabel;
1677 start = i;
1678 }
1679 // Process last group
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001680 processGroup(sortedComponents, start, (N - 1), rci0, r0Label);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001682
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001683 postListReadyRunnable();
1684 }
1685
1686 /**
1687 * Some necessary methods for creating the list are initiated in onCreate and will also
1688 * determine the layout known. We therefore can't update the UI inline and post to the
1689 * handler thread to update after the current task is finished.
1690 */
1691 private void postListReadyRunnable() {
1692 if (mPostListReadyRunnable == null) {
1693 mPostListReadyRunnable = new Runnable() {
1694 @Override
1695 public void run() {
Matt Pietal26038402019-01-08 07:29:34 -05001696 setHeader();
shafik69df96322018-12-18 15:41:19 +00001697 resetButtonBar();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001698 onListRebuilt();
1699 mPostListReadyRunnable = null;
1700 }
1701 };
1702 getMainThreadHandler().post(mPostListReadyRunnable);
1703 }
Adam Powell24428412015-04-01 17:19:56 -07001704 }
1705
1706 public void onListRebuilt() {
Xiaohui Chen393c8012017-02-14 14:55:07 -08001707 int count = getUnfilteredCount();
1708 if (count == 1 && getOtherProfile() == null) {
1709 // Only one target, so we're a candidate to auto-launch!
1710 final TargetInfo target = targetInfoForPosition(0, false);
1711 if (shouldAutoLaunchSingleChoice(target)) {
1712 safelyStartActivity(target);
1713 finish();
1714 }
1715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
1717
Adam Powellc6d5e3a2015-04-23 12:22:20 -07001718 public boolean shouldGetResolvedFilter() {
1719 return mFilterLastUsed;
1720 }
1721
Adam Powell2ed547e2015-04-29 18:45:04 -07001722 private void processGroup(List<ResolvedComponentInfo> rList, int start, int end,
1723 ResolvedComponentInfo ro, CharSequence roLabel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 // Process labels from start to i
1725 int num = end - start+1;
1726 if (num == 1) {
1727 // No duplicate labels. Use label for entry at start
Adam Powell2ed547e2015-04-29 18:45:04 -07001728 addResolveInfoWithAlternates(ro, null, roLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 } else {
Adam Powell24428412015-04-01 17:19:56 -07001730 mHasExtendedInfo = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 boolean usePkg = false;
Adam Powell00f4aad2015-09-17 13:38:16 -07001732 final ApplicationInfo ai = ro.getResolveInfoAt(0).activityInfo.applicationInfo;
1733 final CharSequence startApp = ai.loadLabel(mPm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 if (startApp == null) {
1735 usePkg = true;
1736 }
1737 if (!usePkg) {
1738 // Use HashSet to track duplicates
1739 HashSet<CharSequence> duplicates =
1740 new HashSet<CharSequence>();
1741 duplicates.add(startApp);
1742 for (int j = start+1; j <= end ; j++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001743 ResolveInfo jRi = rList.get(j).getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm);
1745 if ( (jApp == null) || (duplicates.contains(jApp))) {
1746 usePkg = true;
1747 break;
1748 } else {
1749 duplicates.add(jApp);
1750 }
1751 }
1752 // Clear HashSet for later use
1753 duplicates.clear();
1754 }
1755 for (int k = start; k <= end; k++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001756 final ResolvedComponentInfo rci = rList.get(k);
1757 final ResolveInfo add = rci.getResolveInfoAt(0);
1758 final CharSequence extraInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 if (usePkg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 // Use package name for all entries from start to end-1
Adam Powell2ed547e2015-04-29 18:45:04 -07001761 extraInfo = add.activityInfo.packageName;
1762 } else {
1763 // Use application name for all entries from start to end-1
1764 extraInfo = add.activityInfo.applicationInfo.loadLabel(mPm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001766 addResolveInfoWithAlternates(rci, extraInfo, roLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 }
1768 }
1769 }
1770
Adam Powell2ed547e2015-04-29 18:45:04 -07001771 private void addResolveInfoWithAlternates(ResolvedComponentInfo rci,
1772 CharSequence extraInfo, CharSequence roLabel) {
1773 final int count = rci.getCount();
1774 final Intent intent = rci.getIntentAt(0);
1775 final ResolveInfo add = rci.getResolveInfoAt(0);
1776 final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent);
1777 final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel,
1778 extraInfo, replaceIntent);
Adam Powell23882512016-01-29 10:21:00 -08001779 dri.setPinned(rci.isPinned());
Adam Powell2ed547e2015-04-29 18:45:04 -07001780 addResolveInfo(dri);
1781 if (replaceIntent == intent) {
1782 // Only add alternates if we didn't get a specific replacement from
1783 // the caller. If we have one it trumps potential alternates.
1784 for (int i = 1, N = count; i < N; i++) {
1785 final Intent altIntent = rci.getIntentAt(i);
1786 dri.addAlternateSourceIntent(altIntent);
1787 }
1788 }
1789 updateLastChosenPosition(add);
1790 }
1791
Esteban Talavera6de72662014-12-11 17:54:07 +00001792 private void updateLastChosenPosition(ResolveInfo info) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001793 // If another profile is present, ignore the last chosen entry.
1794 if (mOtherProfile != null) {
1795 mLastChosenPosition = -1;
1796 return;
1797 }
Esteban Talavera6de72662014-12-11 17:54:07 +00001798 if (mLastChosen != null
1799 && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName)
1800 && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001801 mLastChosenPosition = mDisplayList.size() - 1;
Esteban Talavera6de72662014-12-11 17:54:07 +00001802 }
1803 }
1804
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001805 // We assume that at this point we've already filtered out the only intent for a different
1806 // targetUserId which we're going to use.
Adam Powell88831a22014-11-20 18:17:00 -08001807 private void addResolveInfo(DisplayResolveInfo dri) {
Kang Li6afa4f22017-06-23 12:54:38 -07001808 if (dri != null && dri.mResolveInfo != null
1809 && dri.mResolveInfo.targetUserId == UserHandle.USER_CURRENT) {
1810 // Checks if this info is already listed in display.
1811 for (DisplayResolveInfo existingInfo : mDisplayList) {
1812 if (resolveInfoMatch(dri.mResolveInfo, existingInfo.mResolveInfo)) {
1813 return;
1814 }
1815 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001816 mDisplayList.add(dri);
Adam Powell88831a22014-11-20 18:17:00 -08001817 }
1818 }
1819
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001820 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001821 public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001822 TargetInfo target = targetInfoForPosition(position, filtered);
1823 if (target != null) {
1824 return target.getResolveInfo();
1825 }
1826 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 }
1828
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001829 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001830 public TargetInfo targetInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001831 if (filtered) {
1832 return getItem(position);
1833 }
1834 if (mDisplayList.size() > position) {
1835 return mDisplayList.get(position);
1836 }
1837 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
1839
1840 public int getCount() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001841 int totalSize = mDisplayList == null || mDisplayList.isEmpty() ? mPlaceholderCount :
1842 mDisplayList.size();
Adam Powell278902c2014-07-12 18:33:22 -07001843 if (mFilterLastUsed && mLastChosenPosition >= 0) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001844 totalSize--;
Adam Powell278902c2014-07-12 18:33:22 -07001845 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001846 return totalSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
1848
Adam Powell50077352015-05-26 18:01:55 -07001849 public int getUnfilteredCount() {
1850 return mDisplayList.size();
1851 }
1852
1853 public int getDisplayInfoCount() {
1854 return mDisplayList.size();
1855 }
1856
1857 public DisplayResolveInfo getDisplayInfoAt(int index) {
1858 return mDisplayList.get(index);
1859 }
1860
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001861 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001862 public TargetInfo getItem(int position) {
Adam Powell278902c2014-07-12 18:33:22 -07001863 if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) {
1864 position++;
1865 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001866 if (mDisplayList.size() > position) {
1867 return mDisplayList.get(position);
1868 } else {
1869 return null;
1870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
1872
1873 public long getItemId(int position) {
1874 return position;
1875 }
1876
Adam Powell24428412015-04-01 17:19:56 -07001877 public boolean hasExtendedInfo() {
1878 return mHasExtendedInfo;
1879 }
1880
1881 public boolean hasResolvedTarget(ResolveInfo info) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001882 for (int i = 0, N = mDisplayList.size(); i < N; i++) {
Adam Powellc412be62015-06-24 13:54:10 -07001883 if (resolveInfoMatch(info, mDisplayList.get(i).getResolveInfo())) {
Adam Powell24428412015-04-01 17:19:56 -07001884 return true;
1885 }
1886 }
1887 return false;
1888 }
1889
Adam Powell23882512016-01-29 10:21:00 -08001890 public int getDisplayResolveInfoCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001891 return mDisplayList.size();
Adam Powell24428412015-04-01 17:19:56 -07001892 }
1893
Adam Powell23882512016-01-29 10:21:00 -08001894 public DisplayResolveInfo getDisplayResolveInfo(int index) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001895 // Used to query services. We only query services for primary targets, not alternates.
1896 return mDisplayList.get(index);
Adam Powell24428412015-04-01 17:19:56 -07001897 }
1898
1899 public final View getView(int position, View convertView, ViewGroup parent) {
Adam Powellfd1e93d2014-09-07 16:52:22 -07001900 View view = convertView;
1901 if (view == null) {
Adam Powell24428412015-04-01 17:19:56 -07001902 view = createView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 }
Adam Powell7d758002015-05-06 17:49:36 -07001904 onBindView(view, getItem(position));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 return view;
1906 }
1907
Adam Powell7d758002015-05-06 17:49:36 -07001908 public final View createView(ViewGroup parent) {
1909 final View view = onCreateView(parent);
1910 final ViewHolder holder = new ViewHolder(view);
1911 view.setTag(holder);
1912 return view;
1913 }
1914
1915 public View onCreateView(ViewGroup parent) {
Adam Powell24428412015-04-01 17:19:56 -07001916 return mInflater.inflate(
1917 com.android.internal.R.layout.resolve_list_item, parent, false);
1918 }
1919
1920 public boolean showsExtendedInfo(TargetInfo info) {
1921 return !TextUtils.isEmpty(info.getExtendedInfo());
1922 }
1923
Adam Powell23882512016-01-29 10:21:00 -08001924 public boolean isComponentPinned(ComponentName name) {
1925 return false;
1926 }
1927
Adam Powell7d758002015-05-06 17:49:36 -07001928 public final void bindView(int position, View view) {
1929 onBindView(view, getItem(position));
1930 }
1931
1932 private void onBindView(View view, TargetInfo info) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001933 final ViewHolder holder = (ViewHolder) view.getTag();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001934 if (info == null) {
1935 holder.icon.setImageDrawable(
1936 getDrawable(R.drawable.resolver_icon_placeholder));
1937 return;
1938 }
Adam Powell63b31692015-09-28 10:45:00 -07001939 final CharSequence label = info.getDisplayLabel();
1940 if (!TextUtils.equals(holder.text.getText(), label)) {
1941 holder.text.setText(info.getDisplayLabel());
1942 }
Adam Powell24428412015-04-01 17:19:56 -07001943 if (showsExtendedInfo(info)) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001944 holder.text2.setVisibility(View.VISIBLE);
Adam Powell24428412015-04-01 17:19:56 -07001945 holder.text2.setText(info.getExtendedInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 } else {
Adam Powell0256c6f2013-05-29 16:42:33 -07001947 holder.text2.setVisibility(View.GONE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
Adam Powell24428412015-04-01 17:19:56 -07001949 if (info instanceof DisplayResolveInfo
1950 && !((DisplayResolveInfo) info).hasDisplayIcon()) {
1951 new LoadAdapterIconTask((DisplayResolveInfo) info).execute();
Dianne Hackborneb034652009-09-07 00:49:58 -07001952 }
Adam Powell24428412015-04-01 17:19:56 -07001953 holder.icon.setImageDrawable(info.getDisplayIcon());
Adam Powell7d758002015-05-06 17:49:36 -07001954 if (holder.badge != null) {
1955 final Drawable badge = info.getBadgeIcon();
1956 if (badge != null) {
1957 holder.badge.setImageDrawable(badge);
Alan Viverettece5d92c2015-07-31 16:46:56 -04001958 holder.badge.setContentDescription(info.getBadgeContentDescription());
Adam Powell7d758002015-05-06 17:49:36 -07001959 holder.badge.setVisibility(View.VISIBLE);
1960 } else {
1961 holder.badge.setVisibility(View.GONE);
1962 }
1963 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001964 }
1965 }
1966
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001967 @VisibleForTesting
1968 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001969 public final ComponentName name;
Adam Powell23882512016-01-29 10:21:00 -08001970 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001971 private final List<Intent> mIntents = new ArrayList<>();
1972 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
1973
1974 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1975 this.name = name;
1976 add(intent, info);
1977 }
1978
1979 public void add(Intent intent, ResolveInfo info) {
1980 mIntents.add(intent);
1981 mResolveInfos.add(info);
1982 }
1983
1984 public int getCount() {
1985 return mIntents.size();
1986 }
1987
1988 public Intent getIntentAt(int index) {
1989 return index >= 0 ? mIntents.get(index) : null;
1990 }
1991
1992 public ResolveInfo getResolveInfoAt(int index) {
1993 return index >= 0 ? mResolveInfos.get(index) : null;
1994 }
1995
1996 public int findIntent(Intent intent) {
1997 for (int i = 0, N = mIntents.size(); i < N; i++) {
1998 if (intent.equals(mIntents.get(i))) {
1999 return i;
2000 }
2001 }
2002 return -1;
2003 }
2004
2005 public int findResolveInfo(ResolveInfo info) {
2006 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
2007 if (info.equals(mResolveInfos.get(i))) {
2008 return i;
2009 }
2010 }
2011 return -1;
2012 }
Adam Powell23882512016-01-29 10:21:00 -08002013
2014 public boolean isPinned() {
2015 return mPinned;
2016 }
2017
2018 public void setPinned(boolean pinned) {
2019 mPinned = pinned;
2020 }
Adam Powell2ed547e2015-04-29 18:45:04 -07002021 }
2022
Adam Powell0256c6f2013-05-29 16:42:33 -07002023 static class ViewHolder {
2024 public TextView text;
2025 public TextView text2;
2026 public ImageView icon;
Adam Powell7d758002015-05-06 17:49:36 -07002027 public ImageView badge;
Adam Powell0256c6f2013-05-29 16:42:33 -07002028
2029 public ViewHolder(View view) {
2030 text = (TextView) view.findViewById(com.android.internal.R.id.text1);
2031 text2 = (TextView) view.findViewById(com.android.internal.R.id.text2);
2032 icon = (ImageView) view.findViewById(R.id.icon);
Adam Powell7d758002015-05-06 17:49:36 -07002033 badge = (ImageView) view.findViewById(R.id.target_badge);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 }
2035 }
2036
Adam Powell7d758002015-05-06 17:49:36 -07002037 class ItemClickListener implements AdapterView.OnItemClickListener,
2038 AdapterView.OnItemLongClickListener {
2039 @Override
2040 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
2041 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2042 if (listView != null) {
2043 position -= listView.getHeaderViewsCount();
2044 }
2045 if (position < 0) {
2046 // Header views don't count.
2047 return;
2048 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002049 // If we're still loading, we can't yet enable the buttons.
2050 if (mAdapter.resolveInfoForPosition(position, true) == null) {
2051 return;
2052 }
2053
Adam Powell7d758002015-05-06 17:49:36 -07002054 final int checkedPos = mAdapterView.getCheckedItemPosition();
2055 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08002056 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07002057 && (!hasValidSelection || mLastSelected != checkedPos)
shafik69df96322018-12-18 15:41:19 +00002058 && (mAlwaysButton != null || mSettingsButton != null)) {
2059 if (mSettingsButton != null) {
2060 // this implies that the layout for browsables is being used
2061 mSettingsButton.setEnabled(true);
2062 } else {
2063 // this implies that mAlwaysButton != null
2064 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
2065 }
Adam Powell7d758002015-05-06 17:49:36 -07002066 mOnceButton.setEnabled(hasValidSelection);
2067 if (hasValidSelection) {
2068 mAdapterView.smoothScrollToPosition(checkedPos);
2069 }
2070 mLastSelected = checkedPos;
2071 } else {
2072 startSelected(position, false, true);
2073 }
2074 }
Adam Powell2d809622012-03-22 15:24:43 -07002075
2076 @Override
2077 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07002078 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2079 if (listView != null) {
2080 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07002081 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07002082 if (position < 0) {
2083 // Header views don't count.
2084 return false;
2085 }
Adam Powell278902c2014-07-12 18:33:22 -07002086 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08002087 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07002088 return true;
2089 }
2090
2091 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002092
Adam Powell24428412015-04-01 17:19:56 -07002093 abstract class LoadIconTask extends AsyncTask<Void, Void, Drawable> {
2094 protected final DisplayResolveInfo mDisplayResolveInfo;
2095 private final ResolveInfo mResolveInfo;
2096
2097 public LoadIconTask(DisplayResolveInfo dri) {
2098 mDisplayResolveInfo = dri;
2099 mResolveInfo = dri.getResolveInfo();
Adam Powell0256c6f2013-05-29 16:42:33 -07002100 }
2101
2102 @Override
Adam Powell24428412015-04-01 17:19:56 -07002103 protected Drawable doInBackground(Void... params) {
2104 return loadIconForResolveInfo(mResolveInfo);
2105 }
2106
2107 @Override
2108 protected void onPostExecute(Drawable d) {
2109 mDisplayResolveInfo.setDisplayIcon(d);
2110 }
2111 }
2112
2113 class LoadAdapterIconTask extends LoadIconTask {
2114 public LoadAdapterIconTask(DisplayResolveInfo dri) {
2115 super(dri);
2116 }
2117
2118 @Override
2119 protected void onPostExecute(Drawable d) {
2120 super.onPostExecute(d);
2121 if (mProfileView != null && mAdapter.getOtherProfile() == mDisplayResolveInfo) {
Adam Powell88831a22014-11-20 18:17:00 -08002122 bindProfileView();
2123 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002124 mAdapter.notifyDataSetChanged();
2125 }
2126 }
Adam Powell278902c2014-07-12 18:33:22 -07002127
Adam Powell24428412015-04-01 17:19:56 -07002128 class LoadIconIntoViewTask extends LoadIconTask {
2129 private final ImageView mTargetView;
Adam Powell278902c2014-07-12 18:33:22 -07002130
Adam Powell24428412015-04-01 17:19:56 -07002131 public LoadIconIntoViewTask(DisplayResolveInfo dri, ImageView target) {
2132 super(dri);
Adam Powell278902c2014-07-12 18:33:22 -07002133 mTargetView = target;
2134 }
2135
2136 @Override
Adam Powell24428412015-04-01 17:19:56 -07002137 protected void onPostExecute(Drawable d) {
2138 super.onPostExecute(d);
2139 mTargetView.setImageDrawable(d);
Adam Powell278902c2014-07-12 18:33:22 -07002140 }
2141 }
Adam Powell09a65602014-07-20 16:23:14 -07002142
Dianne Hackbornec452d92014-11-11 17:16:56 -08002143 static final boolean isSpecificUriMatch(int match) {
2144 match = match&IntentFilter.MATCH_CATEGORY_MASK;
2145 return match >= IntentFilter.MATCH_CATEGORY_HOST
2146 && match <= IntentFilter.MATCH_CATEGORY_PATH;
2147 }
2148
Adam Powell4c470d62015-06-19 17:46:17 -07002149 static class PickTargetOptionRequest extends PickOptionRequest {
2150 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
2151 @Nullable Bundle extras) {
2152 super(prompt, options, extras);
2153 }
2154
2155 @Override
2156 public void onCancel() {
2157 super.onCancel();
2158 final ResolverActivity ra = (ResolverActivity) getActivity();
2159 if (ra != null) {
2160 ra.mPickOptionRequest = null;
2161 ra.finish();
2162 }
2163 }
2164
2165 @Override
2166 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
2167 super.onPickOptionResult(finished, selections, result);
2168 if (selections.length != 1) {
2169 // TODO In a better world we would filter the UI presented here and let the
2170 // user refine. Maybe later.
2171 return;
2172 }
2173
2174 final ResolverActivity ra = (ResolverActivity) getActivity();
2175 if (ra != null) {
2176 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
2177 if (ra.onTargetSelected(ti, false)) {
2178 ra.mPickOptionRequest = null;
2179 ra.finish();
2180 }
2181 }
2182 }
2183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184}