blob: 3ea746d5f4174e844d6f50a352abcf7882af03d8 [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;
Andrei Onea15884392019-03-22 17:28:11 +000024import android.annotation.UnsupportedAppUsage;
Adam Powelle9414d92014-07-05 17:44:18 -070025import android.app.Activity;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080026import android.app.ActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070027import android.app.ActivityTaskManager;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070028import android.app.ActivityThread;
Adam Powell4c470d62015-06-19 17:46:17 -070029import android.app.VoiceInteractor.PickOptionRequest;
30import android.app.VoiceInteractor.PickOptionRequest.Option;
31import android.app.VoiceInteractor.Prompt;
shafik34064c32019-03-06 15:42:52 +000032import android.app.role.RoleManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.ComponentName;
34import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.Intent;
36import android.content.IntentFilter;
37import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010038import android.content.pm.ApplicationInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -070039import android.content.pm.LabeledIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070041import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010043import android.content.pm.UserInfo;
Jiongxuan Gao3365b162016-11-24 20:20:27 +090044import android.content.res.Configuration;
Adam Powellc5878612012-05-04 18:42:38 -070045import android.content.res.Resources;
Mike Digman9c4ae502019-03-19 17:02:25 -070046import android.graphics.Bitmap;
Matt Pietala4b30072019-04-04 13:44:36 -040047import android.graphics.ColorMatrix;
48import android.graphics.ColorMatrixColorFilter;
Mike Digman220587e2019-03-13 11:21:42 -070049import android.graphics.drawable.BitmapDrawable;
Dianne Hackborneb034652009-09-07 00:49:58 -070050import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.net.Uri;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080052import android.os.AsyncTask;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010053import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040055import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.PatternMatcher;
Mike Digman220587e2019-03-13 11:21:42 -070057import android.os.Process;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080058import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070059import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070060import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010061import android.os.UserManager;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080062import android.provider.MediaStore;
63import android.provider.Settings;
64import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.util.Log;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080066import android.util.Slog;
Adam Powell2d809622012-03-22 15:24:43 -070067import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.View;
69import android.view.ViewGroup;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080070import android.widget.AbsListView;
Adam Powell2d809622012-03-22 15:24:43 -070071import android.widget.AdapterView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.widget.BaseAdapter;
Adam Powellc5878612012-05-04 18:42:38 -070073import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070075import android.widget.ListView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010077import android.widget.Toast;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070078
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080079import com.android.internal.R;
80import com.android.internal.annotations.VisibleForTesting;
81import com.android.internal.content.PackageMonitor;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010082import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010083import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070084import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010087import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.Iterator;
89import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070090import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.util.Set;
92
93/**
94 * This activity is displayed when the system attempts to start an Intent for
95 * which there is more than one matching activity, allowing the user to decide
96 * which to go to. It is not normally used directly by application developers.
97 */
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080098@UiThread
Adam Powell7d758002015-05-06 17:49:36 -070099public class ResolverActivity extends Activity {
Adam Powellc5878612012-05-04 18:42:38 -0700100
Alison Cichowlas4691ed42018-11-13 15:59:55 -0500101 // Temporary flag for new chooser delegate behavior.
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -0500102 boolean mEnableChooserDelegate = true;
Alison Cichowlas4691ed42018-11-13 15:59:55 -0500103
Andrei Onea15884392019-03-22 17:28:11 +0000104 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800105 protected ResolveListAdapter mAdapter;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700106 private boolean mSafeForwardingMode;
Matt Pietal5b648562019-03-12 07:40:26 -0400107 protected AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -0700108 private Button mAlwaysButton;
109 private Button mOnceButton;
shafik69df96322018-12-18 15:41:19 +0000110 private Button mSettingsButton;
Matt Pietal74c6ed02019-04-18 13:38:46 -0400111 protected View mProfileView;
Adam Powellc5878612012-05-04 18:42:38 -0700112 private int mIconDpi;
Adam Powell24428412015-04-01 17:19:56 -0700113 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100114 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000115 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800116 private int mLayoutId;
Adam Powell2ed547e2015-04-29 18:45:04 -0700117 private final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700118 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800119 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800120 private CharSequence mTitle;
121 private int mDefaultTitleResId;
shafik69df96322018-12-18 15:41:19 +0000122 private boolean mUseLayoutForBrowsables;
Adam Powell09a65602014-07-20 16:23:14 -0700123
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800124 // Whether or not this activity supports choosing a default handler for the intent.
125 private boolean mSupportsAlwaysUseOption;
Adam Powell63b31692015-09-28 10:45:00 -0700126 protected ResolverDrawerLayout mResolverDrawerLayout;
Andrei Onea15884392019-03-22 17:28:11 +0000127 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800128 protected PackageManager mPm;
129 protected int mLaunchedFromUid;
130
131 private static final String TAG = "ResolverActivity";
132 private static final boolean DEBUG = false;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800133 private Runnable mPostListReadyRunnable;
Adam Powell63b31692015-09-28 10:45:00 -0700134
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700135 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100136
Matt Pietala4b30072019-04-04 13:44:36 -0400137 private ColorMatrixColorFilter mSuspendedMatrixColorFilter;
138
Jorim Jaggif631ef72017-02-24 13:49:47 +0100139 /** See {@link #setRetainInOnStop}. */
140 private boolean mRetainInOnStop;
141
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800142 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
143 @Override public void onSomePackagesChanged() {
144 mAdapter.handlePackagesChanged();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400145 bindProfileView();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800146 }
Xiaohui Chen393c8012017-02-14 14:55:07 -0800147
148 @Override
149 public boolean onPackageChanged(String packageName, int uid, String[] components) {
150 // We care about all package changes, not just the whole package itself which is
151 // default behavior.
152 return true;
153 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800154 };
155
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700156 /**
157 * Get the string resource to be used as a label for the link to the resolver activity for an
158 * action.
159 *
160 * @param action The action to resolve
161 *
162 * @return The string resource to be used as a label
163 */
164 public static @StringRes int getLabelRes(String action) {
165 return ActionTitle.forAction(action).labelRes;
166 }
167
Adam Powell278902c2014-07-12 18:33:22 -0700168 private enum ActionTitle {
169 VIEW(Intent.ACTION_VIEW,
170 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700171 com.android.internal.R.string.whichViewApplicationNamed,
172 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700173 EDIT(Intent.ACTION_EDIT,
174 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700175 com.android.internal.R.string.whichEditApplicationNamed,
176 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700177 SEND(Intent.ACTION_SEND,
178 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700179 com.android.internal.R.string.whichSendApplicationNamed,
180 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700181 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700182 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700183 com.android.internal.R.string.whichSendToApplicationNamed,
184 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700185 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
186 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700187 com.android.internal.R.string.whichSendApplicationNamed,
188 com.android.internal.R.string.whichSendApplicationLabel),
189 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
190 com.android.internal.R.string.whichImageCaptureApplication,
191 com.android.internal.R.string.whichImageCaptureApplicationNamed,
192 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700193 DEFAULT(null,
194 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700195 com.android.internal.R.string.whichApplicationNamed,
196 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700197 HOME(Intent.ACTION_MAIN,
198 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700199 com.android.internal.R.string.whichHomeApplicationNamed,
200 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700201
shafik0c7c5d52019-02-27 12:13:25 +0000202 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000203 public static final int BROWSABLE_TITLE_RES =
204 com.android.internal.R.string.whichGiveAccessToApplication;
205 public static final int BROWSABLE_NAMED_TITLE_RES =
206 com.android.internal.R.string.whichGiveAccessToApplicationNamed;
207
Adam Powell278902c2014-07-12 18:33:22 -0700208 public final String action;
209 public final int titleRes;
210 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700211 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700212
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700213 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700214 this.action = action;
215 this.titleRes = titleRes;
216 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700217 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700218 }
219
220 public static ActionTitle forAction(String action) {
221 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700222 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700223 return title;
224 }
225 }
226 return DEFAULT;
227 }
228 }
229
Dianne Hackborn905577f2011-09-07 18:31:28 -0700230 private Intent makeMyIntent() {
231 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700232 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700233 // The resolver activity is set to be hidden from recent tasks.
234 // we don't want this attribute to be propagated to the next activity
235 // being launched. Note that if the original Intent also had this
236 // flag set, we are now losing it. That should be a very rare case
237 // and we can live with this.
238 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
239 return intent;
240 }
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 @Override
243 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700244 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700245 final Intent intent = makeMyIntent();
246 final Set<String> categories = intent.getCategories();
247 if (Intent.ACTION_MAIN.equals(intent.getAction())
248 && categories != null
249 && categories.size() == 1
250 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100251 // Note: this field is not set to true in the compatibility version.
252 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700253 }
254
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700255 setSafeForwardingMode(true);
256
Adam Powella35c77a2014-09-25 16:46:36 -0700257 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
259
Adam Powell278902c2014-07-12 18:33:22 -0700260 /**
Adam Powell24428412015-04-01 17:19:56 -0700261 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700262 * a default title resource
263 */
Andrei Onea15884392019-03-22 17:28:11 +0000264 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700266 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800267 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
268 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
269 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700270 }
271
272 protected void onCreate(Bundle savedInstanceState, Intent intent,
273 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800274 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700275 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000277
278 // Determine whether we should show that intent is forwarded
279 // from managed profile to owner or other way around.
280 setProfileSwitchMessageId(intent.getContentUserHint());
281
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700282 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700283 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700284 getActivityToken());
285 } catch (RemoteException e) {
286 mLaunchedFromUid = -1;
287 }
Adam Powell7d758002015-05-06 17:49:36 -0700288
289 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
290 // Gulp!
291 finish();
292 return;
293 }
294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700296
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700297 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700298 mRegistered = true;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800299 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800300
Adam Powellc5878612012-05-04 18:42:38 -0700301 final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
302 mIconDpi = am.getLauncherLargeIconDensity();
Adam Powellc5878612012-05-04 18:42:38 -0700303
Adam Powell7d758002015-05-06 17:49:36 -0700304 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700305 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800306 mTitle = title;
307 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700308
shafik69df96322018-12-18 15:41:19 +0000309 mUseLayoutForBrowsables = getTargetIntent() == null
310 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000311 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000312
313 // We don't want to support Always Use if browsable layout is being used,
314 // as to mitigate Intent Capturing vulnerability
315 mSupportsAlwaysUseOption = supportsAlwaysUseOption && !mUseLayoutForBrowsables;
316
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800317 if (configureContentView(mIntents, initialIntents, rList)) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700318 return;
319 }
Adam Powell278902c2014-07-12 18:33:22 -0700320
Alan Viverette51efddb2017-04-05 10:00:01 -0400321 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700322 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700323 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700324 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700325 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700326 finish();
327 }
328 });
Adam Powell4c470d62015-06-19 17:46:17 -0700329 if (isVoiceInteraction()) {
330 rdl.setCollapsed(false);
331 }
Adam Powell63b31692015-09-28 10:45:00 -0700332 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700333 }
334
Adam Powell88831a22014-11-20 18:17:00 -0800335 mProfileView = findViewById(R.id.profile_button);
336 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400337 mProfileView.setOnClickListener(this::onProfileClick);
Adam Powell88831a22014-11-20 18:17:00 -0800338 bindProfileView();
339 }
Adam Powell4c470d62015-06-19 17:46:17 -0700340
Matt Pietala4b30072019-04-04 13:44:36 -0400341 initSuspendedColorMatrix();
342
Adam Powell4c470d62015-06-19 17:46:17 -0700343 if (isVoiceInteraction()) {
344 onSetupVoiceInteraction();
345 }
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100346 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100347 MetricsLogger.action(this, mAdapter.hasFilteredItem()
348 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
349 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
350 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100351 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700352 }
353
Matt Pietal74c6ed02019-04-18 13:38:46 -0400354 protected void onProfileClick(View v) {
355 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
356 if (dri == null) {
357 return;
358 }
359
360 // Do not show the profile switch message anymore.
361 mProfileSwitchMessageId = -1;
362
363 onTargetSelected(dri, false);
364 finish();
365 }
366
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900367 @Override
368 public void onConfigurationChanged(Configuration newConfig) {
369 super.onConfigurationChanged(newConfig);
370 mAdapter.handlePackagesChanged();
371 }
372
Matt Pietala4b30072019-04-04 13:44:36 -0400373 private void initSuspendedColorMatrix() {
374 int grayValue = 127;
375 float scale = 0.5f; // half bright
376
377 ColorMatrix tempBrightnessMatrix = new ColorMatrix();
378 float[] mat = tempBrightnessMatrix.getArray();
379 mat[0] = scale;
380 mat[6] = scale;
381 mat[12] = scale;
382 mat[4] = grayValue;
383 mat[9] = grayValue;
384 mat[14] = grayValue;
385
386 ColorMatrix matrix = new ColorMatrix();
387 matrix.setSaturation(0.0f);
388 matrix.preConcat(tempBrightnessMatrix);
389 mSuspendedMatrixColorFilter = new ColorMatrixColorFilter(matrix);
390 }
391
Adam Powell4c470d62015-06-19 17:46:17 -0700392 /**
393 * Perform any initialization needed for voice interaction.
394 */
Adam Powell23882512016-01-29 10:21:00 -0800395 public void onSetupVoiceInteraction() {
Adam Powell4c470d62015-06-19 17:46:17 -0700396 // Do it right now. Subclasses may delay this and send it later.
397 sendVoiceChoicesIfNeeded();
398 }
399
Adam Powell23882512016-01-29 10:21:00 -0800400 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700401 if (!isVoiceInteraction()) {
402 // Clearly not needed.
403 return;
404 }
405
406
407 final Option[] options = new Option[mAdapter.getCount()];
408 for (int i = 0, N = options.length; i < N; i++) {
409 options[i] = optionForChooserTarget(mAdapter.getItem(i), i);
410 }
411
412 mPickOptionRequest = new PickTargetOptionRequest(
413 new Prompt(getTitle()), options, null);
414 getVoiceInteractor().submitRequest(mPickOptionRequest);
415 }
416
417 Option optionForChooserTarget(TargetInfo target, int index) {
418 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800419 }
420
Adam Powell2ed547e2015-04-29 18:45:04 -0700421 protected final void setAdditionalTargets(Intent[] intents) {
422 if (intents != null) {
423 for (Intent intent : intents) {
424 mIntents.add(intent);
425 }
426 }
427 }
428
Adam Powell0ccc0e92015-04-23 17:19:37 -0700429 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700430 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700431 }
432
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800433 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700434 final Uri referrer = getReferrer();
435 if (referrer != null && "android-app".equals(referrer.getScheme())) {
436 return referrer.getHost();
437 }
438 return null;
439 }
440
Adam Powell23882512016-01-29 10:21:00 -0800441 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700442 return R.layout.resolver_list;
443 }
444
Matt Pietal74c6ed02019-04-18 13:38:46 -0400445 protected void bindProfileView() {
446 if (mProfileView == null) {
447 return;
448 }
449
Adam Powell88831a22014-11-20 18:17:00 -0800450 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
451 if (dri != null) {
452 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800453 View text = mProfileView.findViewById(R.id.profile_button);
454 if (!(text instanceof TextView)) {
455 text = mProfileView.findViewById(R.id.text1);
456 }
457 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800458 } else {
459 mProfileView.setVisibility(View.GONE);
460 }
Adam Powell278902c2014-07-12 18:33:22 -0700461 }
462
Sander Alewijnsef6545332014-10-31 12:39:02 +0000463 private void setProfileSwitchMessageId(int contentUserHint) {
464 if (contentUserHint != UserHandle.USER_CURRENT &&
465 contentUserHint != UserHandle.myUserId()) {
466 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
467 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
468 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
469 : false;
470 boolean targetIsManaged = userManager.isManagedProfile();
471 if (originIsManaged && !targetIsManaged) {
472 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
473 } else if (!originIsManaged && targetIsManaged) {
474 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
475 }
476 }
477 }
478
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700479 /**
480 * Turn on launch mode that is safe to use when forwarding intents received from
481 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
482 * instead of the normal Activity.startActivity for launching the activity selected
483 * by the user.
484 *
485 * <p>This mode is set to true by default if the activity is initialized through
486 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
487 * methods, it is set to false by default. You must set it before calling one of the
488 * more detailed onCreate methods, so that it will be set correctly in the case where
489 * there is only one intent to resolve and it is thus started immediately.</p>
490 */
491 public void setSafeForwardingMode(boolean safeForwarding) {
492 mSafeForwardingMode = safeForwarding;
493 }
494
shafik69df96322018-12-18 15:41:19 +0000495 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
496 final ActionTitle title = mResolvingHome
497 ? ActionTitle.HOME
498 : ActionTitle.forAction(intent.getAction());
499
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800500 // While there may already be a filtered item, we can only use it in the title if the list
501 // is already sorted and all information relevant to it is already in the list.
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800502 final boolean named = mAdapter.getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700503 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
504 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000505 } else if (isHttpSchemeAndViewAction(intent)) {
506 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000507 // they're giving access for the activity to open URLs from this specific host
508 return named
509 ? getString(ActionTitle.BROWSABLE_NAMED_TITLE_RES, intent.getData().getHost(),
510 mAdapter.getFilteredItem().getDisplayLabel())
511 : getString(ActionTitle.BROWSABLE_TITLE_RES, intent.getData().getHost());
Adam Powell278902c2014-07-12 18:33:22 -0700512 } else {
Adam Powell24428412015-04-01 17:19:56 -0700513 return named
514 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
515 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700516 }
Adam Powellc5878612012-05-04 18:42:38 -0700517 }
518
Adam Powelle9414d92014-07-05 17:44:18 -0700519 void dismiss() {
520 if (!isFinishing()) {
521 finish();
522 }
523 }
524
Adam Powellc5878612012-05-04 18:42:38 -0700525
Mike Digman220587e2019-03-13 11:21:42 -0700526 /**
Mike Digmanba232682019-03-27 14:55:26 -0700527 * Loads the icon and label for the provided ApplicationInfo. Defaults to using the application
528 * icon and label over any IntentFilter or Activity icon to increase user understanding, with an
529 * exception for applications that hold the right permission. Always attempts to use available
530 * resources over PackageManager loading mechanisms so badging can be done by iconloader. Uses
531 * Strings to strip creative formatting.
Mike Digman220587e2019-03-13 11:21:42 -0700532 */
Mike Digmanba232682019-03-27 14:55:26 -0700533 private abstract static class TargetPresentationGetter {
534 @Nullable abstract Drawable getIconSubstituteInternal();
535 @Nullable abstract String getAppSubLabelInternal();
Mike Digman220587e2019-03-13 11:21:42 -0700536
Mike Digmanba232682019-03-27 14:55:26 -0700537 private Context mCtx;
Mike Digmanba232682019-03-27 14:55:26 -0700538 private final int mIconDpi;
Mike Digman9c4ae502019-03-19 17:02:25 -0700539 private final boolean mHasSubstitutePermission;
Mike Digmanc0ef5542019-04-01 15:36:25 -0700540 private final ApplicationInfo mAi;
541
542 protected PackageManager mPm;
Mike Digman9c4ae502019-03-19 17:02:25 -0700543
Mike Digmanba232682019-03-27 14:55:26 -0700544 TargetPresentationGetter(Context ctx, int iconDpi, ApplicationInfo ai) {
545 mCtx = ctx;
546 mPm = ctx.getPackageManager();
Mike Digman9c4ae502019-03-19 17:02:25 -0700547 mAi = ai;
Mike Digmanba232682019-03-27 14:55:26 -0700548 mIconDpi = iconDpi;
Mike Digman9c4ae502019-03-19 17:02:25 -0700549 mHasSubstitutePermission = PackageManager.PERMISSION_GRANTED == mPm.checkPermission(
550 android.Manifest.permission.SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON,
551 mAi.packageName);
552 }
553
Mike Digmanb2e5e712019-04-19 15:49:10 -0700554 public Drawable getIcon(UserHandle userHandle) {
555 return new BitmapDrawable(mCtx.getResources(), getIconBitmap(userHandle));
Mike Digman9c4ae502019-03-19 17:02:25 -0700556 }
557
Mike Digmanb2e5e712019-04-19 15:49:10 -0700558 public Bitmap getIconBitmap(UserHandle userHandle) {
Mike Digman9c4ae502019-03-19 17:02:25 -0700559 Drawable dr = null;
560 if (mHasSubstitutePermission) {
Mike Digmanba232682019-03-27 14:55:26 -0700561 dr = getIconSubstituteInternal();
Mike Digman9c4ae502019-03-19 17:02:25 -0700562 }
563
564 if (dr == null) {
565 try {
566 if (mAi.icon != 0) {
567 dr = loadIconFromResource(mPm.getResourcesForApplication(mAi), mAi.icon);
Mike Digman220587e2019-03-13 11:21:42 -0700568 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700569 } catch (NameNotFoundException ignore) {
570 }
571 }
572
573 // Fall back to ApplicationInfo#loadIcon if nothing has been loaded
574 if (dr == null) {
575 dr = mAi.loadIcon(mPm);
576 }
577
Mike Digmanba232682019-03-27 14:55:26 -0700578 SimpleIconFactory sif = SimpleIconFactory.obtain(mCtx);
Mike Digmanb2e5e712019-04-19 15:49:10 -0700579 Bitmap icon = sif.createUserBadgedIconBitmap(dr, userHandle);
Mike Digman9c4ae502019-03-19 17:02:25 -0700580 sif.recycle();
581
582 return icon;
583 }
584
Mike Digmanba232682019-03-27 14:55:26 -0700585 public String getLabel() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700586 String label = null;
587 // Apps with the substitute permission will always show the sublabel as their label
588 if (mHasSubstitutePermission) {
Mike Digmanba232682019-03-27 14:55:26 -0700589 label = getAppSubLabelInternal();
Mike Digman9c4ae502019-03-19 17:02:25 -0700590 }
591
592 if (label == null) {
593 label = (String) mAi.loadLabel(mPm);
594 }
595
596 return label;
597 }
598
Mike Digmanba232682019-03-27 14:55:26 -0700599 public String getSubLabel() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700600 // Apps with the substitute permission will never have a sublabel
601 if (mHasSubstitutePermission) return null;
Mike Digmanba232682019-03-27 14:55:26 -0700602 return getAppSubLabelInternal();
603 }
604
605 protected String loadLabelFromResource(Resources res, int resId) {
606 return res.getString(resId);
Mike Digman9c4ae502019-03-19 17:02:25 -0700607 }
608
609 @Nullable
610 protected Drawable loadIconFromResource(Resources res, int resId) {
611 return res.getDrawableForDensity(resId, mIconDpi);
612 }
613
614 }
615
Mike Digmanba232682019-03-27 14:55:26 -0700616 /**
617 * Loads the icon and label for the provided ResolveInfo.
618 */
619 @VisibleForTesting
Mike Digmanc0ef5542019-04-01 15:36:25 -0700620 public static class ResolveInfoPresentationGetter extends ActivityInfoPresentationGetter {
Mike Digman9c4ae502019-03-19 17:02:25 -0700621 private final ResolveInfo mRi;
Mike Digmanba232682019-03-27 14:55:26 -0700622 public ResolveInfoPresentationGetter(Context ctx, int iconDpi, ResolveInfo ri) {
Mike Digmanc0ef5542019-04-01 15:36:25 -0700623 super(ctx, iconDpi, ri.activityInfo);
Mike Digman9c4ae502019-03-19 17:02:25 -0700624 mRi = ri;
625 }
626
627 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700628 Drawable getIconSubstituteInternal() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700629 Drawable dr = null;
630 try {
631 // Do not use ResolveInfo#getIconResource() as it defaults to the app
632 if (mRi.resolvePackageName != null && mRi.icon != 0) {
633 dr = loadIconFromResource(
634 mPm.getResourcesForApplication(mRi.resolvePackageName), mRi.icon);
Adam Powellc5878612012-05-04 18:42:38 -0700635 }
Mike Digman220587e2019-03-13 11:21:42 -0700636 } catch (NameNotFoundException e) {
637 Log.e(TAG, "SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON permission granted but "
638 + "couldn't find resources for package", e);
Adam Powellc5878612012-05-04 18:42:38 -0700639 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700640
Mike Digmanc0ef5542019-04-01 15:36:25 -0700641 // Fall back to ActivityInfo if no icon is found via ResolveInfo
642 if (dr == null) dr = super.getIconSubstituteInternal();
643
Mike Digman9c4ae502019-03-19 17:02:25 -0700644 return dr;
Adam Powellc5878612012-05-04 18:42:38 -0700645 }
Mike Digman220587e2019-03-13 11:21:42 -0700646
Mike Digman9c4ae502019-03-19 17:02:25 -0700647 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700648 String getAppSubLabelInternal() {
649 // Will default to app name if no intent filter or activity label set, make sure to
650 // check if subLabel matches label before final display
Mike Digman9c4ae502019-03-19 17:02:25 -0700651 return (String) mRi.loadLabel(mPm);
652 }
653 }
654
Mike Digmanba232682019-03-27 14:55:26 -0700655 ResolveInfoPresentationGetter makePresentationGetter(ResolveInfo ri) {
656 return new ResolveInfoPresentationGetter(this, mIconDpi, ri);
657 }
658
659 /**
660 * Loads the icon and label for the provided ActivityInfo.
661 */
662 @VisibleForTesting
663 public static class ActivityInfoPresentationGetter extends TargetPresentationGetter {
Mike Digman9c4ae502019-03-19 17:02:25 -0700664 private final ActivityInfo mActivityInfo;
Mike Digmanba232682019-03-27 14:55:26 -0700665 public ActivityInfoPresentationGetter(Context ctx, int iconDpi,
666 ActivityInfo activityInfo) {
667 super(ctx, iconDpi, activityInfo.applicationInfo);
Mike Digman9c4ae502019-03-19 17:02:25 -0700668 mActivityInfo = activityInfo;
669 }
670
671 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700672 Drawable getIconSubstituteInternal() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700673 Drawable dr = null;
Mike Digman220587e2019-03-13 11:21:42 -0700674 try {
Mike Digman9c4ae502019-03-19 17:02:25 -0700675 // Do not use ActivityInfo#getIconResource() as it defaults to the app
676 if (mActivityInfo.icon != 0) {
677 dr = loadIconFromResource(
678 mPm.getResourcesForApplication(mActivityInfo.applicationInfo),
679 mActivityInfo.icon);
680 }
681 } catch (NameNotFoundException e) {
682 Log.e(TAG, "SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON permission granted but "
683 + "couldn't find resources for package", e);
Mike Digman220587e2019-03-13 11:21:42 -0700684 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700685
686 return dr;
Mike Digman220587e2019-03-13 11:21:42 -0700687 }
688
Mike Digman9c4ae502019-03-19 17:02:25 -0700689 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700690 String getAppSubLabelInternal() {
691 // Will default to app name if no activity label set, make sure to check if subLabel
692 // matches label before final display
Mike Digman9c4ae502019-03-19 17:02:25 -0700693 return (String) mActivityInfo.loadLabel(mPm);
Mike Digman220587e2019-03-13 11:21:42 -0700694 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700695 }
Mike Digman220587e2019-03-13 11:21:42 -0700696
Mike Digmanba232682019-03-27 14:55:26 -0700697 protected ActivityInfoPresentationGetter makePresentationGetter(ActivityInfo ai) {
698 return new ActivityInfoPresentationGetter(this, mIconDpi, ai);
699 }
700
Mike Digman9c4ae502019-03-19 17:02:25 -0700701 Drawable loadIconForResolveInfo(ResolveInfo ri) {
Mike Digmanb2e5e712019-04-19 15:49:10 -0700702 // Load icons based on the current process. If in work profile icons should be badged.
703 return makePresentationGetter(ri).getIcon(Process.myUserHandle());
Adam Powellc5878612012-05-04 18:42:38 -0700704 }
705
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800706 @Override
707 protected void onRestart() {
708 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700709 if (!mRegistered) {
710 mPackageMonitor.register(this, getMainLooper(), false);
711 mRegistered = true;
712 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800713 mAdapter.handlePackagesChanged();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400714 bindProfileView();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800715 }
716
717 @Override
718 protected void onStop() {
719 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700720 if (mRegistered) {
721 mPackageMonitor.unregister();
722 mRegistered = false;
723 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700724 final Intent intent = getIntent();
725 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100726 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700727 // This resolver is in the unusual situation where it has been
728 // launched at the top of a new task. We don't let it be added
729 // to the recent tasks shown to the user, and we need to make sure
730 // that each time we are launched we get the correct launching
731 // uid (not re-using the same resolver from an old launching uid),
732 // so we will now finish ourself since being no longer visible,
733 // the user probably can't get back to us.
734 if (!isChangingConfigurations()) {
735 finish();
736 }
737 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800738 }
739
Adam Powellc5878612012-05-04 18:42:38 -0700740 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700741 protected void onDestroy() {
742 super.onDestroy();
743 if (!isChangingConfigurations() && mPickOptionRequest != null) {
744 mPickOptionRequest.cancel();
745 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800746 if (mPostListReadyRunnable != null) {
747 getMainThreadHandler().removeCallbacks(mPostListReadyRunnable);
748 mPostListReadyRunnable = null;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800749 }
Kang Li38a6da642017-04-05 12:30:55 -0700750 if (mAdapter != null && mAdapter.mResolverListController != null) {
751 mAdapter.mResolverListController.destroy();
752 }
Adam Powell4c470d62015-06-19 17:46:17 -0700753 }
754
755 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700756 protected void onRestoreInstanceState(Bundle savedInstanceState) {
757 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000758 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700759 }
760
shafik0c7c5d52019-02-27 12:13:25 +0000761 private boolean isHttpSchemeAndViewAction(Intent intent) {
762 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
763 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
764 && Intent.ACTION_VIEW.equals(intent.getAction());
765 }
766
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100767 private boolean hasManagedProfile() {
768 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
769 if (userManager == null) {
770 return false;
771 }
772
773 try {
774 List<UserInfo> profiles = userManager.getProfiles(getUserId());
775 for (UserInfo userInfo : profiles) {
776 if (userInfo != null && userInfo.isManagedProfile()) {
777 return true;
778 }
779 }
780 } catch (SecurityException e) {
781 return false;
782 }
783 return false;
784 }
785
786 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
787 try {
788 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
789 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700790 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100791 } catch (NameNotFoundException e) {
792 return false;
793 }
794 }
795
Adam Powell278902c2014-07-12 18:33:22 -0700796 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
797 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100798 boolean enabled = false;
799 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700800 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800801 if (ri == null) {
802 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
803 return;
804 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
805 Log.e(TAG, "Attempted to set selection to resolve info for another user");
806 return;
807 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100808 enabled = true;
809 }
810 }
811 mAlwaysButton.setEnabled(enabled);
812 }
813
Adam Powellc5878612012-05-04 18:42:38 -0700814 public void onButtonClick(View v) {
815 final int id = v.getId();
shafik69df96322018-12-18 15:41:19 +0000816 int which = mAdapter.hasFilteredItem()
817 ? mAdapter.getFilteredPosition()
818 : mAdapterView.getCheckedItemPosition();
819 boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem();
820 if (id == R.id.button_app_settings) {
821 showSettingsForSelected(which, hasIndexBeenFiltered);
822 } else {
823 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
824 }
825 }
826
827 private void showSettingsForSelected(int which, boolean hasIndexBeenFiltered) {
828 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
shafik34064c32019-03-06 15:42:52 +0000829 Intent intent = new Intent();
830 // For browsers, we open the Default Browser page
831 // For regular apps, we open the Open by Default page
832 if (ri.handleAllWebDataURI) {
833 intent.setAction(Intent.ACTION_MANAGE_DEFAULT_APP)
834 .putExtra(Intent.EXTRA_ROLE_NAME, RoleManager.ROLE_BROWSER);
835 } else {
836 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
837 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
838 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
839 }
840 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700841 }
842
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800843 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700844 if (isFinishing()) {
845 return;
846 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800847 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000848 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
849 Toast.makeText(this, String.format(getResources().getString(
850 com.android.internal.R.string.activity_resolver_work_profiles_support),
851 ri.activityInfo.loadLabel(getPackageManager()).toString()),
852 Toast.LENGTH_LONG).show();
853 return;
854 }
855
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800856 TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800857 if (target == null) {
858 return;
859 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700860 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800861 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100862 MetricsLogger.action(
863 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800864 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100865 MetricsLogger.action(
866 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
867 } else {
868 MetricsLogger.action(
869 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
870 }
871 MetricsLogger.action(this, mAdapter.hasFilteredItem()
872 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
873 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700874 finish();
875 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877
Adam Powelle49d9392014-07-17 18:45:19 -0700878 /**
879 * Replace me in subclasses!
880 */
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000881 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700882 return defIntent;
883 }
884
Adam Powell2ed547e2015-04-29 18:45:04 -0700885 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700886 final ResolveInfo ri = target.getResolveInfo();
887 final Intent intent = target != null ? target.getResolvedIntent() : null;
888
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800889 if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800890 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700891 // Build a reasonable intent filter, based on what matched.
892 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200893 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894
Henrik Engström3277cf12014-07-17 12:18:29 +0200895 if (intent.getSelector() != null) {
896 filterIntent = intent.getSelector();
897 } else {
898 filterIntent = intent;
899 }
900
901 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800902 if (action != null) {
903 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200905 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700906 if (categories != null) {
907 for (String cat : categories) {
908 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700911 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912
Adam Powell24428412015-04-01 17:19:56 -0700913 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200914 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700915 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200916 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700917 if (mimeType != null) {
918 try {
919 filter.addDataType(mimeType);
920 } catch (IntentFilter.MalformedMimeTypeException e) {
921 Log.w("ResolverActivity", e);
922 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
924 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700925 }
926 if (data != null && data.getScheme() != null) {
927 // We need the data specification if there was no type,
928 // OR if the scheme is not one of our magical "file:"
929 // or "content:" schemes (see IntentFilter for the reason).
930 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
931 || (!"file".equals(data.getScheme())
932 && !"content".equals(data.getScheme()))) {
933 filter.addDataScheme(data.getScheme());
934
935 // Look through the resolved filter to determine which part
936 // of it matched the original Intent.
937 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
938 if (pIt != null) {
939 String ssp = data.getSchemeSpecificPart();
940 while (ssp != null && pIt.hasNext()) {
941 PatternMatcher p = pIt.next();
942 if (p.match(ssp)) {
943 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
944 break;
945 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700946 }
947 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700948 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
949 if (aIt != null) {
950 while (aIt.hasNext()) {
951 IntentFilter.AuthorityEntry a = aIt.next();
952 if (a.match(data) >= 0) {
953 int port = a.getPort();
954 filter.addDataAuthority(a.getHost(),
955 port >= 0 ? Integer.toString(port) : null);
956 break;
957 }
958 }
959 }
960 pIt = ri.filter.pathsIterator();
961 if (pIt != null) {
962 String path = data.getPath();
963 while (path != null && pIt.hasNext()) {
964 PatternMatcher p = pIt.next();
965 if (p.match(path)) {
966 filter.addDataPath(p.getPath(), p.getType());
967 break;
968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 }
970 }
971 }
972 }
973
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700974 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800975 final int N = mAdapter.mUnfilteredResolveList.size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800976 ComponentName[] set;
977 // If we don't add back in the component for forwarding the intent to a managed
978 // profile, the preferred activity may not be updated correctly (as the set of
979 // components we tell it we knew about will have changed).
980 final boolean needToAddBackProfileForwardingComponent
981 = mAdapter.mOtherProfile != null;
982 if (!needToAddBackProfileForwardingComponent) {
983 set = new ComponentName[N];
984 } else {
985 set = new ComponentName[N + 1];
986 }
987
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700988 int bestMatch = 0;
989 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800990 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700991 set[i] = new ComponentName(r.activityInfo.packageName,
992 r.activityInfo.name);
993 if (r.match > bestMatch) bestMatch = r.match;
994 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800995
996 if (needToAddBackProfileForwardingComponent) {
997 set[N] = mAdapter.mOtherProfile.getResolvedComponentName();
998 final int otherProfileMatch = mAdapter.mOtherProfile.getResolveInfo().match;
999 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
1000 }
1001
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001002 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001003 final int userId = getUserId();
1004 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001005
1006 // Set the preferred Activity
1007 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
1008
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001009 if (ri.handleAllWebDataURI) {
1010 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001011 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001012 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001013 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001014 }
1015 } else {
1016 // Update Domain Verification status
1017 ComponentName cn = intent.getComponent();
1018 String packageName = cn.getPackageName();
1019 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001020
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001021 boolean isHttpOrHttps = (dataScheme != null) &&
1022 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
1023 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001024
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001025 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
1026 boolean hasCategoryBrowsable = (categories != null) &&
1027 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001028
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001029 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001030 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001031 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
1032 userId);
1033 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001034 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001035 } else {
1036 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -08001037 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001038 } catch (RemoteException re) {
1039 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1040 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043 }
1044
Adam Powell24428412015-04-01 17:19:56 -07001045 if (target != null) {
1046 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -04001047
1048 // Rely on the ActivityManager to pop up a dialog regarding app suspension
1049 // and return false
1050 if (target.isSuspended()) {
1051 return false;
1052 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001053 }
Matt Pietala4b30072019-04-04 13:44:36 -04001054
Adam Powell2ed547e2015-04-29 18:45:04 -07001055 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001056 }
1057
Adam Powell23882512016-01-29 10:21:00 -08001058 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -07001059 // We're dispatching intents that might be coming from legacy apps, so
1060 // don't kill ourselves.
1061 StrictMode.disableDeathOnFileUriExposure();
1062 try {
1063 safelyStartActivityInternal(cti);
1064 } finally {
1065 StrictMode.enableDeathOnFileUriExposure();
1066 }
1067 }
1068
1069 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +00001070 // If needed, show that intent is forwarded
1071 // from managed profile to owner or other way around.
1072 if (mProfileSwitchMessageId != -1) {
1073 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
1074 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001075 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -07001076 if (cti.start(this, null)) {
1077 onActivityStarted(cti);
1078 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001079 return;
1080 }
1081 try {
Adam Powell24428412015-04-01 17:19:56 -07001082 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
1083 onActivityStarted(cti);
1084 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001085 } catch (RuntimeException e) {
1086 String launchedFromPackage;
1087 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001088 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001089 getActivityToken());
1090 } catch (RemoteException e2) {
1091 launchedFromPackage = "??";
1092 }
1093 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
1094 + " package " + launchedFromPackage + ", while running in "
1095 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098
Alison Cichowlas3e340502018-08-07 17:15:01 -04001099
1100 boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
1101 int userId) {
1102 // Pass intent to delegate chooser activity with permission token.
1103 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
1104 // moves into systemui
1105 try {
1106 // TODO: Once this is a small springboard activity, it can move off the UI process
1107 // and we can move the request method to ActivityManagerInternal.
1108 IBinder permissionToken = ActivityTaskManager.getService()
1109 .requestStartActivityPermissionToken(getActivityToken());
1110 final Intent chooserIntent = new Intent();
1111 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1112 Resources.getSystem().getString(R.string.config_chooserActivity));
1113 chooserIntent.setClassName(delegateActivity.getPackageName(),
1114 delegateActivity.getClassName());
1115 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1116
1117 // TODO: These extras will change as chooser activity moves into systemui
1118 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1119 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1120 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1121 ignoreTargetSecurity);
1122 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001123 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1124 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001125 startActivity(chooserIntent);
1126 } catch (RemoteException e) {
1127 Log.e(TAG, e.toString());
1128 }
1129 return true;
1130 }
1131
Adam Powell23882512016-01-29 10:21:00 -08001132 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001133 // Do nothing
1134 }
1135
Adam Powell23882512016-01-29 10:21:00 -08001136 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001137 return false;
1138 }
1139
Adam Powell23882512016-01-29 10:21:00 -08001140 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001141 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001142 }
1143
Adam Powell23882512016-01-29 10:21:00 -08001144 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001145 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001146 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001147 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001148 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001149 }
1150
Adam Powell23882512016-01-29 10:21:00 -08001151 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents,
Adam Powell7d758002015-05-06 17:49:36 -07001152 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
1153 boolean filterLastUsed) {
1154 return new ResolveListAdapter(context, payloadIntents, initialIntents, rList,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001155 launchedFromUid, filterLastUsed, createListController());
1156 }
1157
1158 @VisibleForTesting
1159 protected ResolverListController createListController() {
1160 return new ResolverListController(
1161 this,
1162 mPm,
1163 getTargetIntent(),
1164 getReferrerPackageName(),
1165 mLaunchedFromUid);
Adam Powell88831a22014-11-20 18:17:00 -08001166 }
1167
Adam Powell39e94eb2015-09-08 17:01:49 -07001168 /**
1169 * Returns true if the activity is finishing and creation should halt
1170 */
Adam Powell23882512016-01-29 10:21:00 -08001171 public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents,
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001172 List<ResolveInfo> rList) {
Dianne Hackborn57dd7372015-07-27 18:11:14 -07001173 // The last argument of createAdapter is whether to do special handling
1174 // of the last used choice to highlight it in the list. We need to always
1175 // turn this off when running under voice interaction, since it results in
1176 // a more complicated UI that the current voice interaction flow is not able
1177 // to handle.
Adam Powell7d758002015-05-06 17:49:36 -07001178 mAdapter = createAdapter(this, payloadIntents, initialIntents, rList,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001179 mLaunchedFromUid, mSupportsAlwaysUseOption && !isVoiceInteraction());
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001180 boolean rebuildCompleted = mAdapter.rebuildList();
Adam Powell7d758002015-05-06 17:49:36 -07001181
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001182 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001183 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001184 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001185 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001186 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001187 setContentView(mLayoutId);
Adam Powell7d758002015-05-06 17:49:36 -07001188
Adam Powell50077352015-05-26 18:01:55 -07001189 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001190
1191 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1192 // we're already done, we can check if we should auto-launch immediately.
1193 if (rebuildCompleted) {
1194 if (count == 1 && mAdapter.getOtherProfile() == null) {
1195 // Only one target, so we're a candidate to auto-launch!
1196 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
1197 if (shouldAutoLaunchSingleChoice(target)) {
1198 safelyStartActivity(target);
1199 mPackageMonitor.unregister();
1200 mRegistered = false;
1201 finish();
1202 return true;
1203 }
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001204 }
1205 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001206
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001207
Alan Viverette51efddb2017-04-05 10:00:01 -04001208 mAdapterView = findViewById(R.id.resolver_list);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001209
1210 if (count == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001211 final TextView emptyView = findViewById(R.id.empty);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001212 emptyView.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -07001213 mAdapterView.setVisibility(View.GONE);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001214 } else {
1215 mAdapterView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001216 onPrepareAdapterView(mAdapterView, mAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001217 }
Adam Powell39e94eb2015-09-08 17:01:49 -07001218 return false;
Adam Powell7d758002015-05-06 17:49:36 -07001219 }
1220
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001221 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001222 final boolean useHeader = adapter.hasFilteredItem();
1223 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
1224
1225 adapterView.setAdapter(mAdapter);
1226
1227 final ItemClickListener listener = new ItemClickListener();
1228 adapterView.setOnItemClickListener(listener);
1229 adapterView.setOnItemLongClickListener(listener);
1230
shafik69df96322018-12-18 15:41:19 +00001231 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001232 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1233 }
1234
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001235 // In case this method is called again (due to activity recreation), avoid adding a new
1236 // header if one is already present.
1237 if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) {
Adam Powell7d758002015-05-06 17:49:36 -07001238 listView.addHeaderView(LayoutInflater.from(this).inflate(
1239 R.layout.resolver_different_item_header, listView, false));
1240 }
Adam Powell24428412015-04-01 17:19:56 -07001241 }
1242
Matt Pietal26038402019-01-08 07:29:34 -05001243 /**
1244 * Configure the area above the app selection list (title, content preview, etc).
1245 */
1246 public void setHeader() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001247 if (mAdapter.getCount() == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001248 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001249 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001250 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001251 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001252 }
1253
1254 CharSequence title = mTitle != null
1255 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001256 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001257
1258 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001259 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001260 if (titleView != null) {
1261 titleView.setText(title);
1262 }
1263 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001264 }
1265
Alan Viverette51efddb2017-04-05 10:00:01 -04001266 final ImageView iconView = findViewById(R.id.icon);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001267 final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem();
1268 if (iconView != null && iconInfo != null) {
Matt Pietalaf044ae2019-03-29 06:53:53 -04001269 new LoadIconTask(iconInfo, iconView).execute();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001270 }
1271 }
1272
shafik69df96322018-12-18 15:41:19 +00001273 private void resetButtonBar() {
1274 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1275 return;
1276 }
1277 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1278 if (buttonLayout != null) {
1279 buttonLayout.setVisibility(View.VISIBLE);
1280 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
1281 mSettingsButton = (Button) buttonLayout.findViewById(R.id.button_app_settings);
1282 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1283
1284 if (mUseLayoutForBrowsables) {
1285 resetSettingsOrOnceButtonBar();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001286 } else {
shafik69df96322018-12-18 15:41:19 +00001287 resetAlwaysOrOnceButtonBar();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001288 }
shafik69df96322018-12-18 15:41:19 +00001289 } else {
1290 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1291 }
1292 }
1293
1294 private void resetSettingsOrOnceButtonBar() {
1295 //unsetting always button
1296 mAlwaysButton.setVisibility(View.GONE);
1297
1298 // When the items load in, if an item was already selected,
1299 // enable the buttons
1300 if (mAdapterView != null
1301 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1302 mSettingsButton.setEnabled(true);
1303 mOnceButton.setEnabled(true);
1304 }
1305 }
1306
1307 private void resetAlwaysOrOnceButtonBar() {
1308 // This check needs to be made because layout with default
1309 // doesn't have a settings button
1310 if (mSettingsButton != null) {
1311 //unsetting always button
1312 mSettingsButton.setVisibility(View.GONE);
1313 mSettingsButton = null;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001314 }
1315
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001316 if (useLayoutWithDefault()
1317 && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001318 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
1319 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001320 return;
1321 }
1322
1323 // When the items load in, if an item was already selected, enable the buttons
1324 if (mAdapterView != null
1325 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1326 setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true);
1327 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001328 }
1329 }
1330
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001331 private boolean useLayoutWithDefault() {
1332 return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem();
1333 }
1334
Adam Powellc412be62015-06-24 13:54:10 -07001335 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001336 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1337 * called and we are launched in a new task.
1338 */
1339 protected void setRetainInOnStop(boolean retainInOnStop) {
1340 mRetainInOnStop = retainInOnStop;
1341 }
1342
1343 /**
Adam Powellc412be62015-06-24 13:54:10 -07001344 * Check a simple match for the component of two ResolveInfos.
1345 */
1346 static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
1347 return lhs == null ? rhs == null
1348 : lhs.activityInfo == null ? rhs.activityInfo == null
1349 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1350 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1351 }
1352
Adam Powell23882512016-01-29 10:21:00 -08001353 public final class DisplayResolveInfo implements TargetInfo {
Adam Powell24428412015-04-01 17:19:56 -07001354 private final ResolveInfo mResolveInfo;
1355 private final CharSequence mDisplayLabel;
1356 private Drawable mDisplayIcon;
Adam Powell00f4aad2015-09-17 13:38:16 -07001357 private Drawable mBadge;
Adam Powell24428412015-04-01 17:19:56 -07001358 private final CharSequence mExtendedInfo;
1359 private final Intent mResolvedIntent;
Adam Powell2ed547e2015-04-29 18:45:04 -07001360 private final List<Intent> mSourceIntents = new ArrayList<>();
Matt Pietala4b30072019-04-04 13:44:36 -04001361 private boolean mIsSuspended;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362
Adam Powell23882512016-01-29 10:21:00 -08001363 public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel,
Dianne Hackborneb034652009-09-07 00:49:58 -07001364 CharSequence pInfo, Intent pOrigIntent) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001365 mSourceIntents.add(originalIntent);
Adam Powell24428412015-04-01 17:19:56 -07001366 mResolveInfo = pri;
1367 mDisplayLabel = pLabel;
1368 mExtendedInfo = pInfo;
1369
1370 final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent :
Adam Powell2ed547e2015-04-29 18:45:04 -07001371 getReplacementIntent(pri.activityInfo, getTargetIntent()));
Adam Powell24428412015-04-01 17:19:56 -07001372 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1373 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
1374 final ActivityInfo ai = mResolveInfo.activityInfo;
1375 intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));
1376
Matt Pietala4b30072019-04-04 13:44:36 -04001377 mIsSuspended = (ai.applicationInfo.flags & ApplicationInfo.FLAG_SUSPENDED) != 0;
1378
Adam Powell24428412015-04-01 17:19:56 -07001379 mResolvedIntent = intent;
1380 }
1381
Adam Powell2ed547e2015-04-29 18:45:04 -07001382 private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) {
1383 mSourceIntents.addAll(other.getAllSourceIntents());
1384 mResolveInfo = other.mResolveInfo;
1385 mDisplayLabel = other.mDisplayLabel;
1386 mDisplayIcon = other.mDisplayIcon;
1387 mExtendedInfo = other.mExtendedInfo;
1388 mResolvedIntent = new Intent(other.mResolvedIntent);
1389 mResolvedIntent.fillIn(fillInIntent, flags);
1390 }
1391
Adam Powell24428412015-04-01 17:19:56 -07001392 public ResolveInfo getResolveInfo() {
1393 return mResolveInfo;
1394 }
1395
1396 public CharSequence getDisplayLabel() {
1397 return mDisplayLabel;
1398 }
1399
1400 public Drawable getDisplayIcon() {
1401 return mDisplayIcon;
1402 }
1403
Alan Viverettece5d92c2015-07-31 16:46:56 -04001404 @Override
Adam Powell2ed547e2015-04-29 18:45:04 -07001405 public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) {
1406 return new DisplayResolveInfo(this, fillInIntent, flags);
1407 }
1408
1409 @Override
1410 public List<Intent> getAllSourceIntents() {
1411 return mSourceIntents;
1412 }
1413
1414 public void addAlternateSourceIntent(Intent alt) {
1415 mSourceIntents.add(alt);
1416 }
1417
Adam Powell24428412015-04-01 17:19:56 -07001418 public void setDisplayIcon(Drawable icon) {
1419 mDisplayIcon = icon;
1420 }
1421
1422 public boolean hasDisplayIcon() {
1423 return mDisplayIcon != null;
1424 }
1425
1426 public CharSequence getExtendedInfo() {
1427 return mExtendedInfo;
1428 }
1429
1430 public Intent getResolvedIntent() {
1431 return mResolvedIntent;
1432 }
1433
1434 @Override
1435 public ComponentName getResolvedComponentName() {
1436 return new ComponentName(mResolveInfo.activityInfo.packageName,
1437 mResolveInfo.activityInfo.name);
1438 }
1439
1440 @Override
1441 public boolean start(Activity activity, Bundle options) {
1442 activity.startActivity(mResolvedIntent, options);
1443 return true;
1444 }
1445
1446 @Override
Alison Cichowlas3e340502018-08-07 17:15:01 -04001447 public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) {
Alison Cichowlas4691ed42018-11-13 15:59:55 -05001448 if (mEnableChooserDelegate) {
1449 return activity.startAsCallerImpl(mResolvedIntent, options, false, userId);
1450 } else {
1451 activity.startActivityAsCaller(mResolvedIntent, options, null, false, userId);
1452 return true;
1453 }
Adam Powell24428412015-04-01 17:19:56 -07001454 }
1455
1456 @Override
1457 public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
1458 activity.startActivityAsUser(mResolvedIntent, options, user);
1459 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001461
Matt Pietala4b30072019-04-04 13:44:36 -04001462 public boolean isSuspended() {
1463 return mIsSuspended;
1464 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001465 }
1466
1467 List<DisplayResolveInfo> getDisplayList() {
1468 return mAdapter.mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 }
1470
Adam Powell24428412015-04-01 17:19:56 -07001471 /**
1472 * A single target as represented in the chooser.
1473 */
1474 public interface TargetInfo {
1475 /**
1476 * Get the resolved intent that represents this target. Note that this may not be the
1477 * intent that will be launched by calling one of the <code>start</code> methods provided;
1478 * this is the intent that will be credited with the launch.
1479 *
1480 * @return the resolved intent for this target
1481 */
Adam Powell23882512016-01-29 10:21:00 -08001482 Intent getResolvedIntent();
Adam Powell24428412015-04-01 17:19:56 -07001483
1484 /**
1485 * Get the resolved component name that represents this target. Note that this may not
1486 * be the component that will be directly launched by calling one of the <code>start</code>
1487 * methods provided; this is the component that will be credited with the launch.
1488 *
1489 * @return the resolved ComponentName for this target
1490 */
Adam Powell23882512016-01-29 10:21:00 -08001491 ComponentName getResolvedComponentName();
Adam Powell24428412015-04-01 17:19:56 -07001492
1493 /**
1494 * Start the activity referenced by this target.
1495 *
1496 * @param activity calling Activity performing the launch
1497 * @param options ActivityOptions bundle
1498 * @return true if the start completed successfully
1499 */
Adam Powell23882512016-01-29 10:21:00 -08001500 boolean start(Activity activity, Bundle options);
Adam Powell24428412015-04-01 17:19:56 -07001501
1502 /**
1503 * Start the activity referenced by this target as if the ResolverActivity's caller
1504 * was performing the start operation.
1505 *
1506 * @param activity calling Activity (actually) performing the launch
1507 * @param options ActivityOptions bundle
1508 * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller
1509 * @return true if the start completed successfully
1510 */
Alison Cichowlas3e340502018-08-07 17:15:01 -04001511 boolean startAsCaller(ResolverActivity activity, Bundle options, int userId);
Adam Powell24428412015-04-01 17:19:56 -07001512
1513 /**
1514 * Start the activity referenced by this target as a given user.
1515 *
1516 * @param activity calling activity performing the launch
1517 * @param options ActivityOptions bundle
1518 * @param user handle for the user to start the activity as
1519 * @return true if the start completed successfully
1520 */
Adam Powell23882512016-01-29 10:21:00 -08001521 boolean startAsUser(Activity activity, Bundle options, UserHandle user);
Adam Powell24428412015-04-01 17:19:56 -07001522
1523 /**
1524 * Return the ResolveInfo about how and why this target matched the original query
1525 * for available targets.
1526 *
1527 * @return ResolveInfo representing this target's match
1528 */
Adam Powell23882512016-01-29 10:21:00 -08001529 ResolveInfo getResolveInfo();
Adam Powell24428412015-04-01 17:19:56 -07001530
1531 /**
1532 * Return the human-readable text label for this target.
1533 *
1534 * @return user-visible target label
1535 */
Adam Powell23882512016-01-29 10:21:00 -08001536 CharSequence getDisplayLabel();
Adam Powell24428412015-04-01 17:19:56 -07001537
1538 /**
1539 * Return any extended info for this target. This may be used to disambiguate
1540 * otherwise identical targets.
1541 *
1542 * @return human-readable disambig string or null if none present
1543 */
Adam Powell23882512016-01-29 10:21:00 -08001544 CharSequence getExtendedInfo();
Adam Powell24428412015-04-01 17:19:56 -07001545
1546 /**
Mike Digman9c4ae502019-03-19 17:02:25 -07001547 * @return The drawable that should be used to represent this target including badge
Adam Powell24428412015-04-01 17:19:56 -07001548 */
Adam Powell23882512016-01-29 10:21:00 -08001549 Drawable getDisplayIcon();
Adam Powell2ed547e2015-04-29 18:45:04 -07001550
1551 /**
1552 * Clone this target with the given fill-in information.
1553 */
Adam Powell23882512016-01-29 10:21:00 -08001554 TargetInfo cloneFilledIn(Intent fillInIntent, int flags);
Adam Powell2ed547e2015-04-29 18:45:04 -07001555
1556 /**
1557 * @return the list of supported source intents deduped against this single target
1558 */
Adam Powell23882512016-01-29 10:21:00 -08001559 List<Intent> getAllSourceIntents();
Matt Pietala4b30072019-04-04 13:44:36 -04001560
1561 /**
1562 * @return true if this target can be selected by the user
1563 */
1564 boolean isSuspended();
Adam Powell24428412015-04-01 17:19:56 -07001565 }
1566
Adam Powell23882512016-01-29 10:21:00 -08001567 public class ResolveListAdapter extends BaseAdapter {
Adam Powell7d758002015-05-06 17:49:36 -07001568 private final List<Intent> mIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001569 private final Intent[] mInitialIntents;
1570 private final List<ResolveInfo> mBaseResolveList;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001571 protected ResolveInfo mLastChosen;
Adam Powell88831a22014-11-20 18:17:00 -08001572 private DisplayResolveInfo mOtherProfile;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001573 private ResolverListController mResolverListController;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001574 private int mPlaceholderCount;
Adam Powell24428412015-04-01 17:19:56 -07001575
1576 protected final LayoutInflater mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001578 // This one is the list that the Adapter will actually present.
Adam Powell2ed547e2015-04-29 18:45:04 -07001579 List<DisplayResolveInfo> mDisplayList;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001580 List<ResolvedComponentInfo> mUnfilteredResolveList;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001581
Adam Powell278902c2014-07-12 18:33:22 -07001582 private int mLastChosenPosition = -1;
1583 private boolean mFilterLastUsed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584
Adam Powell7d758002015-05-06 17:49:36 -07001585 public ResolveListAdapter(Context context, List<Intent> payloadIntents,
1586 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001587 boolean filterLastUsed,
1588 ResolverListController resolverListController) {
Adam Powell7d758002015-05-06 17:49:36 -07001589 mIntents = payloadIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001590 mInitialIntents = initialIntents;
1591 mBaseResolveList = rList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001592 mLaunchedFromUid = launchedFromUid;
Adam Powelle9414d92014-07-05 17:44:18 -07001593 mInflater = LayoutInflater.from(context);
Adam Powell2ed547e2015-04-29 18:45:04 -07001594 mDisplayList = new ArrayList<>();
Adam Powell278902c2014-07-12 18:33:22 -07001595 mFilterLastUsed = filterLastUsed;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001596 mResolverListController = resolverListController;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001599 public void handlePackagesChanged() {
1600 rebuildList();
Esteban Talavera6de72662014-12-11 17:54:07 +00001601 if (getCount() == 0) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001602 // We no longer have any items... just finish the activity.
1603 finish();
Adam Powellc5878612012-05-04 18:42:38 -07001604 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001605 }
1606
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001607 public void setPlaceholderCount(int count) {
1608 mPlaceholderCount = count;
1609 }
1610
Sumir Katariadb688af2017-05-10 17:33:47 -07001611 public int getPlaceholderCount() { return mPlaceholderCount; }
1612
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001613 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001614 public DisplayResolveInfo getFilteredItem() {
1615 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1616 // Not using getItem since it offsets to dodge this position for the list
Adam Powell2ed547e2015-04-29 18:45:04 -07001617 return mDisplayList.get(mLastChosenPosition);
Adam Powell278902c2014-07-12 18:33:22 -07001618 }
1619 return null;
1620 }
1621
Adam Powell88831a22014-11-20 18:17:00 -08001622 public DisplayResolveInfo getOtherProfile() {
1623 return mOtherProfile;
1624 }
1625
Adam Powell278902c2014-07-12 18:33:22 -07001626 public int getFilteredPosition() {
1627 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1628 return mLastChosenPosition;
1629 }
1630 return AbsListView.INVALID_POSITION;
1631 }
1632
1633 public boolean hasFilteredItem() {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001634 return mFilterLastUsed && mLastChosen != null;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001635 }
1636
Adam Powella182e452015-07-06 16:57:56 -07001637 public float getScore(DisplayResolveInfo target) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001638 return mResolverListController.getScore(target);
Adam Powella182e452015-07-06 16:57:56 -07001639 }
1640
Kang Li0cef910d2017-01-05 09:14:36 -08001641 public void updateModel(ComponentName componentName) {
1642 mResolverListController.updateModel(componentName);
1643 }
1644
Kang Li9fa2a2c2017-01-06 13:33:24 -08001645 public void updateChooserCounts(String packageName, int userId, String action) {
1646 mResolverListController.updateChooserCounts(packageName, userId, action);
1647 }
1648
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001649 /**
1650 * Rebuild the list of resolvers. In some cases some parts will need some asynchronous work
1651 * to complete.
1652 *
1653 * @return Whether or not the list building is completed.
1654 */
1655 protected boolean rebuildList() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001656 List<ResolvedComponentInfo> currentResolveList = null;
Sudheer Shanka7e64e102015-01-23 10:37:45 +00001657 // Clear the value of mOtherProfile from previous call.
1658 mOtherProfile = null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001659 mLastChosen = null;
1660 mLastChosenPosition = -1;
Adam Powell2ed547e2015-04-29 18:45:04 -07001661 mDisplayList.clear();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001662 if (mBaseResolveList != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001663 currentResolveList = mUnfilteredResolveList = new ArrayList<>();
1664 mResolverListController.addResolveListDedupe(currentResolveList,
1665 getTargetIntent(),
1666 mBaseResolveList);
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001667 } else {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001668 currentResolveList = mUnfilteredResolveList =
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001669 mResolverListController.getResolversForIntent(shouldGetResolvedFilter(),
1670 shouldGetActivityMetadata(),
1671 mIntents);
1672 if (currentResolveList == null) {
1673 processSortedList(currentResolveList);
1674 return true;
Adam Powell2ed547e2015-04-29 18:45:04 -07001675 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001676 List<ResolvedComponentInfo> originalList =
1677 mResolverListController.filterIneligibleActivities(currentResolveList,
1678 true);
1679 if (originalList != null) {
1680 mUnfilteredResolveList = originalList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001681 }
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001682 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001683
1684 // So far we only support a single other profile at a time.
1685 // The first one we see gets special treatment.
1686 for (ResolvedComponentInfo info : currentResolveList) {
1687 if (info.getResolveInfoAt(0).targetUserId != UserHandle.USER_CURRENT) {
1688 mOtherProfile = new DisplayResolveInfo(info.getIntentAt(0),
1689 info.getResolveInfoAt(0),
1690 info.getResolveInfoAt(0).loadLabel(mPm),
1691 info.getResolveInfoAt(0).loadLabel(mPm),
1692 getReplacementIntent(info.getResolveInfoAt(0).activityInfo,
1693 info.getIntentAt(0)));
1694 currentResolveList.remove(info);
1695 break;
1696 }
1697 }
1698
1699 if (mOtherProfile == null) {
1700 try {
1701 mLastChosen = mResolverListController.getLastChosen();
1702 } catch (RemoteException re) {
1703 Log.d(TAG, "Error calling getLastChosenActivity\n" + re);
1704 }
1705 }
1706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 int N;
You Kim43a5070e2012-11-21 23:23:45 +09001708 if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001709 // We only care about fixing the unfilteredList if the current resolve list and
1710 // current resolve list are currently the same.
1711 List<ResolvedComponentInfo> originalList =
1712 mResolverListController.filterLowPriority(currentResolveList,
1713 mUnfilteredResolveList == currentResolveList);
1714 if (originalList != null) {
1715 mUnfilteredResolveList = originalList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001717
Hakan Seyalioglu873cbfd2017-02-21 19:23:43 -08001718 if (currentResolveList.size() > 1) {
Sumir Katariadb688af2017-05-10 17:33:47 -07001719 int placeholderCount = currentResolveList.size();
1720 if (useLayoutWithDefault()) {
1721 --placeholderCount;
1722 }
1723 setPlaceholderCount(placeholderCount);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001724 AsyncTask<List<ResolvedComponentInfo>,
1725 Void,
1726 List<ResolvedComponentInfo>> sortingTask =
1727 new AsyncTask<List<ResolvedComponentInfo>,
1728 Void,
1729 List<ResolvedComponentInfo>>() {
1730 @Override
1731 protected List<ResolvedComponentInfo> doInBackground(
1732 List<ResolvedComponentInfo>... params) {
1733 mResolverListController.sort(params[0]);
1734 return params[0];
1735 }
1736
1737 @Override
1738 protected void onPostExecute(List<ResolvedComponentInfo> sortedComponents) {
1739 processSortedList(sortedComponents);
Matt Pietal74c6ed02019-04-18 13:38:46 -04001740 bindProfileView();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001741 notifyDataSetChanged();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001742 }
1743 };
1744 sortingTask.execute(currentResolveList);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001745 postListReadyRunnable();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001746 return false;
1747 } else {
1748 processSortedList(currentResolveList);
1749 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001751 } else {
1752 processSortedList(currentResolveList);
1753 return true;
1754 }
1755 }
1756
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001757
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001758 private void processSortedList(List<ResolvedComponentInfo> sortedComponents) {
1759 int N;
1760 if (sortedComponents != null && (N = sortedComponents.size()) != 0) {
Dianne Hackborneb034652009-09-07 00:49:58 -07001761 // First put the initial items at the top.
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001762 if (mInitialIntents != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001763 for (int i = 0; i < mInitialIntents.length; i++) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001764 Intent ii = mInitialIntents[i];
Dianne Hackborneb034652009-09-07 00:49:58 -07001765 if (ii == null) {
1766 continue;
1767 }
1768 ActivityInfo ai = ii.resolveActivityInfo(
1769 getPackageManager(), 0);
1770 if (ai == null) {
Adam Powell09a65602014-07-20 16:23:14 -07001771 Log.w(TAG, "No activity found for " + ii);
Dianne Hackborneb034652009-09-07 00:49:58 -07001772 continue;
1773 }
1774 ResolveInfo ri = new ResolveInfo();
1775 ri.activityInfo = ai;
Nicolas Prevot1a815922014-10-10 16:22:38 +01001776 UserManager userManager =
1777 (UserManager) getSystemService(Context.USER_SERVICE);
Dianne Hackborneb034652009-09-07 00:49:58 -07001778 if (ii instanceof LabeledIntent) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001779 LabeledIntent li = (LabeledIntent) ii;
Dianne Hackborneb034652009-09-07 00:49:58 -07001780 ri.resolvePackageName = li.getSourcePackage();
1781 ri.labelRes = li.getLabelResource();
1782 ri.nonLocalizedLabel = li.getNonLocalizedLabel();
1783 ri.icon = li.getIconResource();
Sudheer Shanka9ded7602015-05-19 21:17:25 +01001784 ri.iconResourceId = ri.icon;
1785 }
1786 if (userManager.isManagedProfile()) {
1787 ri.noResourceId = true;
1788 ri.icon = 0;
Dianne Hackborneb034652009-09-07 00:49:58 -07001789 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001790 addResolveInfo(new DisplayResolveInfo(ii, ri,
Dianne Hackborneb034652009-09-07 00:49:58 -07001791 ri.loadLabel(getPackageManager()), null, ii));
1792 }
1793 }
You Kim43a5070e2012-11-21 23:23:45 +09001794
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001795
Mike Digmanba232682019-03-27 14:55:26 -07001796 for (ResolvedComponentInfo rci : sortedComponents) {
1797 final ResolveInfo ri = rci.getResolveInfoAt(0);
1798 if (ri != null) {
1799 ResolveInfoPresentationGetter pg = makePresentationGetter(ri);
1800 addResolveInfoWithAlternates(rci, pg.getSubLabel(), pg.getLabel());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001804
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001805
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001806 postListReadyRunnable();
1807 }
1808
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001809
1810
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001811 /**
1812 * Some necessary methods for creating the list are initiated in onCreate and will also
1813 * determine the layout known. We therefore can't update the UI inline and post to the
1814 * handler thread to update after the current task is finished.
1815 */
1816 private void postListReadyRunnable() {
1817 if (mPostListReadyRunnable == null) {
1818 mPostListReadyRunnable = new Runnable() {
1819 @Override
1820 public void run() {
Matt Pietal26038402019-01-08 07:29:34 -05001821 setHeader();
shafik69df96322018-12-18 15:41:19 +00001822 resetButtonBar();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001823 onListRebuilt();
1824 mPostListReadyRunnable = null;
1825 }
1826 };
1827 getMainThreadHandler().post(mPostListReadyRunnable);
1828 }
Adam Powell24428412015-04-01 17:19:56 -07001829 }
1830
1831 public void onListRebuilt() {
Xiaohui Chen393c8012017-02-14 14:55:07 -08001832 int count = getUnfilteredCount();
1833 if (count == 1 && getOtherProfile() == null) {
1834 // Only one target, so we're a candidate to auto-launch!
1835 final TargetInfo target = targetInfoForPosition(0, false);
1836 if (shouldAutoLaunchSingleChoice(target)) {
1837 safelyStartActivity(target);
1838 finish();
1839 }
1840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
1842
Adam Powellc6d5e3a2015-04-23 12:22:20 -07001843 public boolean shouldGetResolvedFilter() {
1844 return mFilterLastUsed;
1845 }
1846
Adam Powell2ed547e2015-04-29 18:45:04 -07001847 private void addResolveInfoWithAlternates(ResolvedComponentInfo rci,
1848 CharSequence extraInfo, CharSequence roLabel) {
1849 final int count = rci.getCount();
1850 final Intent intent = rci.getIntentAt(0);
1851 final ResolveInfo add = rci.getResolveInfoAt(0);
1852 final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent);
1853 final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel,
1854 extraInfo, replaceIntent);
1855 addResolveInfo(dri);
1856 if (replaceIntent == intent) {
1857 // Only add alternates if we didn't get a specific replacement from
1858 // the caller. If we have one it trumps potential alternates.
1859 for (int i = 1, N = count; i < N; i++) {
1860 final Intent altIntent = rci.getIntentAt(i);
1861 dri.addAlternateSourceIntent(altIntent);
1862 }
1863 }
1864 updateLastChosenPosition(add);
1865 }
1866
Esteban Talavera6de72662014-12-11 17:54:07 +00001867 private void updateLastChosenPosition(ResolveInfo info) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001868 // If another profile is present, ignore the last chosen entry.
1869 if (mOtherProfile != null) {
1870 mLastChosenPosition = -1;
1871 return;
1872 }
Esteban Talavera6de72662014-12-11 17:54:07 +00001873 if (mLastChosen != null
1874 && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName)
1875 && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001876 mLastChosenPosition = mDisplayList.size() - 1;
Esteban Talavera6de72662014-12-11 17:54:07 +00001877 }
1878 }
1879
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001880 // We assume that at this point we've already filtered out the only intent for a different
1881 // targetUserId which we're going to use.
Adam Powell88831a22014-11-20 18:17:00 -08001882 private void addResolveInfo(DisplayResolveInfo dri) {
Kang Li6afa4f22017-06-23 12:54:38 -07001883 if (dri != null && dri.mResolveInfo != null
1884 && dri.mResolveInfo.targetUserId == UserHandle.USER_CURRENT) {
1885 // Checks if this info is already listed in display.
1886 for (DisplayResolveInfo existingInfo : mDisplayList) {
1887 if (resolveInfoMatch(dri.mResolveInfo, existingInfo.mResolveInfo)) {
1888 return;
1889 }
1890 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001891 mDisplayList.add(dri);
Adam Powell88831a22014-11-20 18:17:00 -08001892 }
1893 }
1894
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001895 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001896 public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001897 TargetInfo target = targetInfoForPosition(position, filtered);
1898 if (target != null) {
1899 return target.getResolveInfo();
1900 }
1901 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
1903
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001904 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001905 public TargetInfo targetInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001906 if (filtered) {
1907 return getItem(position);
1908 }
1909 if (mDisplayList.size() > position) {
1910 return mDisplayList.get(position);
1911 }
1912 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 }
1914
1915 public int getCount() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001916 int totalSize = mDisplayList == null || mDisplayList.isEmpty() ? mPlaceholderCount :
1917 mDisplayList.size();
Adam Powell278902c2014-07-12 18:33:22 -07001918 if (mFilterLastUsed && mLastChosenPosition >= 0) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001919 totalSize--;
Adam Powell278902c2014-07-12 18:33:22 -07001920 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001921 return totalSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 }
1923
Adam Powell50077352015-05-26 18:01:55 -07001924 public int getUnfilteredCount() {
1925 return mDisplayList.size();
1926 }
1927
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001928 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001929 public TargetInfo getItem(int position) {
Adam Powell278902c2014-07-12 18:33:22 -07001930 if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) {
1931 position++;
1932 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001933 if (mDisplayList.size() > position) {
1934 return mDisplayList.get(position);
1935 } else {
1936 return null;
1937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939
1940 public long getItemId(int position) {
1941 return position;
1942 }
1943
Adam Powell23882512016-01-29 10:21:00 -08001944 public int getDisplayResolveInfoCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001945 return mDisplayList.size();
Adam Powell24428412015-04-01 17:19:56 -07001946 }
1947
Adam Powell23882512016-01-29 10:21:00 -08001948 public DisplayResolveInfo getDisplayResolveInfo(int index) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001949 // Used to query services. We only query services for primary targets, not alternates.
1950 return mDisplayList.get(index);
Adam Powell24428412015-04-01 17:19:56 -07001951 }
1952
1953 public final View getView(int position, View convertView, ViewGroup parent) {
Adam Powellfd1e93d2014-09-07 16:52:22 -07001954 View view = convertView;
1955 if (view == null) {
Adam Powell24428412015-04-01 17:19:56 -07001956 view = createView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
Adam Powell7d758002015-05-06 17:49:36 -07001958 onBindView(view, getItem(position));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 return view;
1960 }
1961
Adam Powell7d758002015-05-06 17:49:36 -07001962 public final View createView(ViewGroup parent) {
1963 final View view = onCreateView(parent);
1964 final ViewHolder holder = new ViewHolder(view);
1965 view.setTag(holder);
1966 return view;
1967 }
1968
1969 public View onCreateView(ViewGroup parent) {
Adam Powell24428412015-04-01 17:19:56 -07001970 return mInflater.inflate(
1971 com.android.internal.R.layout.resolve_list_item, parent, false);
1972 }
1973
Adam Powell7d758002015-05-06 17:49:36 -07001974 public final void bindView(int position, View view) {
1975 onBindView(view, getItem(position));
1976 }
1977
Mike Digmanba232682019-03-27 14:55:26 -07001978 protected void onBindView(View view, TargetInfo info) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001979 final ViewHolder holder = (ViewHolder) view.getTag();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001980 if (info == null) {
1981 holder.icon.setImageDrawable(
1982 getDrawable(R.drawable.resolver_icon_placeholder));
1983 return;
1984 }
Mike Digmanba232682019-03-27 14:55:26 -07001985
Adam Powell63b31692015-09-28 10:45:00 -07001986 final CharSequence label = info.getDisplayLabel();
1987 if (!TextUtils.equals(holder.text.getText(), label)) {
1988 holder.text.setText(info.getDisplayLabel());
1989 }
Mike Digmanba232682019-03-27 14:55:26 -07001990
1991 // Always show a subLabel for visual consistency across list items. Show an empty
1992 // subLabel if the subLabel is the same as the label
1993 CharSequence subLabel = info.getExtendedInfo();
1994 if (TextUtils.equals(label, subLabel)) subLabel = null;
1995
1996 if (!TextUtils.equals(holder.text2.getText(), subLabel)) {
1997 holder.text2.setText(subLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 }
Mike Digmanba232682019-03-27 14:55:26 -07001999
Matt Pietala4b30072019-04-04 13:44:36 -04002000 if (info.isSuspended()) {
2001 holder.icon.setColorFilter(mSuspendedMatrixColorFilter);
2002 } else {
2003 holder.icon.setColorFilter(null);
2004 }
2005
Adam Powell24428412015-04-01 17:19:56 -07002006 if (info instanceof DisplayResolveInfo
2007 && !((DisplayResolveInfo) info).hasDisplayIcon()) {
Matt Pietalaf044ae2019-03-29 06:53:53 -04002008 new LoadIconTask((DisplayResolveInfo) info, holder.icon).execute();
2009 } else {
2010 holder.icon.setImageDrawable(info.getDisplayIcon());
Dianne Hackborneb034652009-09-07 00:49:58 -07002011 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002012 }
2013 }
2014
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04002015
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08002016 @VisibleForTesting
2017 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07002018 public final ComponentName name;
2019 private final List<Intent> mIntents = new ArrayList<>();
2020 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
2021
2022 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
2023 this.name = name;
2024 add(intent, info);
2025 }
2026
2027 public void add(Intent intent, ResolveInfo info) {
2028 mIntents.add(intent);
2029 mResolveInfos.add(info);
2030 }
2031
2032 public int getCount() {
2033 return mIntents.size();
2034 }
2035
2036 public Intent getIntentAt(int index) {
2037 return index >= 0 ? mIntents.get(index) : null;
2038 }
2039
2040 public ResolveInfo getResolveInfoAt(int index) {
2041 return index >= 0 ? mResolveInfos.get(index) : null;
2042 }
2043
2044 public int findIntent(Intent intent) {
2045 for (int i = 0, N = mIntents.size(); i < N; i++) {
2046 if (intent.equals(mIntents.get(i))) {
2047 return i;
2048 }
2049 }
2050 return -1;
2051 }
2052
2053 public int findResolveInfo(ResolveInfo info) {
2054 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
2055 if (info.equals(mResolveInfos.get(i))) {
2056 return i;
2057 }
2058 }
2059 return -1;
2060 }
2061 }
2062
Adam Powell0256c6f2013-05-29 16:42:33 -07002063 static class ViewHolder {
Mike Digman4b83c212019-05-03 10:17:35 -07002064 public View itemView;
2065 public Drawable defaultItemViewBackground;
2066
Adam Powell0256c6f2013-05-29 16:42:33 -07002067 public TextView text;
2068 public TextView text2;
2069 public ImageView icon;
2070
2071 public ViewHolder(View view) {
Mike Digman4b83c212019-05-03 10:17:35 -07002072 itemView = view;
2073 defaultItemViewBackground = view.getBackground();
Adam Powell0256c6f2013-05-29 16:42:33 -07002074 text = (TextView) view.findViewById(com.android.internal.R.id.text1);
2075 text2 = (TextView) view.findViewById(com.android.internal.R.id.text2);
2076 icon = (ImageView) view.findViewById(R.id.icon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
2078 }
2079
Adam Powell7d758002015-05-06 17:49:36 -07002080 class ItemClickListener implements AdapterView.OnItemClickListener,
2081 AdapterView.OnItemLongClickListener {
2082 @Override
2083 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
2084 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2085 if (listView != null) {
2086 position -= listView.getHeaderViewsCount();
2087 }
2088 if (position < 0) {
2089 // Header views don't count.
2090 return;
2091 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002092 // If we're still loading, we can't yet enable the buttons.
2093 if (mAdapter.resolveInfoForPosition(position, true) == null) {
2094 return;
2095 }
2096
Adam Powell7d758002015-05-06 17:49:36 -07002097 final int checkedPos = mAdapterView.getCheckedItemPosition();
2098 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08002099 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07002100 && (!hasValidSelection || mLastSelected != checkedPos)
shafik69df96322018-12-18 15:41:19 +00002101 && (mAlwaysButton != null || mSettingsButton != null)) {
2102 if (mSettingsButton != null) {
2103 // this implies that the layout for browsables is being used
2104 mSettingsButton.setEnabled(true);
2105 } else {
2106 // this implies that mAlwaysButton != null
2107 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
2108 }
Adam Powell7d758002015-05-06 17:49:36 -07002109 mOnceButton.setEnabled(hasValidSelection);
2110 if (hasValidSelection) {
2111 mAdapterView.smoothScrollToPosition(checkedPos);
2112 }
2113 mLastSelected = checkedPos;
2114 } else {
2115 startSelected(position, false, true);
2116 }
2117 }
Adam Powell2d809622012-03-22 15:24:43 -07002118
2119 @Override
2120 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07002121 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2122 if (listView != null) {
2123 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07002124 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07002125 if (position < 0) {
2126 // Header views don't count.
2127 return false;
2128 }
Adam Powell278902c2014-07-12 18:33:22 -07002129 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08002130 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07002131 return true;
2132 }
2133
2134 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002135
Matt Pietalaf044ae2019-03-29 06:53:53 -04002136 class LoadIconTask extends AsyncTask<Void, Void, Drawable> {
Adam Powell24428412015-04-01 17:19:56 -07002137 protected final DisplayResolveInfo mDisplayResolveInfo;
2138 private final ResolveInfo mResolveInfo;
Matt Pietalaf044ae2019-03-29 06:53:53 -04002139 private final ImageView mTargetView;
Adam Powell24428412015-04-01 17:19:56 -07002140
Matt Pietalaf044ae2019-03-29 06:53:53 -04002141 LoadIconTask(DisplayResolveInfo dri, ImageView target) {
Adam Powell24428412015-04-01 17:19:56 -07002142 mDisplayResolveInfo = dri;
2143 mResolveInfo = dri.getResolveInfo();
Matt Pietalaf044ae2019-03-29 06:53:53 -04002144 mTargetView = target;
Adam Powell0256c6f2013-05-29 16:42:33 -07002145 }
2146
2147 @Override
Adam Powell24428412015-04-01 17:19:56 -07002148 protected Drawable doInBackground(Void... params) {
2149 return loadIconForResolveInfo(mResolveInfo);
2150 }
2151
2152 @Override
2153 protected void onPostExecute(Drawable d) {
Matt Pietal74c6ed02019-04-18 13:38:46 -04002154 if (mAdapter.getOtherProfile() == mDisplayResolveInfo) {
Adam Powell88831a22014-11-20 18:17:00 -08002155 bindProfileView();
Matt Pietalaf044ae2019-03-29 06:53:53 -04002156 } else {
2157 mDisplayResolveInfo.setDisplayIcon(d);
2158 mTargetView.setImageDrawable(d);
Adam Powell88831a22014-11-20 18:17:00 -08002159 }
Adam Powell278902c2014-07-12 18:33:22 -07002160 }
2161 }
Adam Powell09a65602014-07-20 16:23:14 -07002162
Dianne Hackbornec452d92014-11-11 17:16:56 -08002163 static final boolean isSpecificUriMatch(int match) {
2164 match = match&IntentFilter.MATCH_CATEGORY_MASK;
2165 return match >= IntentFilter.MATCH_CATEGORY_HOST
2166 && match <= IntentFilter.MATCH_CATEGORY_PATH;
2167 }
2168
Adam Powell4c470d62015-06-19 17:46:17 -07002169 static class PickTargetOptionRequest extends PickOptionRequest {
2170 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
2171 @Nullable Bundle extras) {
2172 super(prompt, options, extras);
2173 }
2174
2175 @Override
2176 public void onCancel() {
2177 super.onCancel();
2178 final ResolverActivity ra = (ResolverActivity) getActivity();
2179 if (ra != null) {
2180 ra.mPickOptionRequest = null;
2181 ra.finish();
2182 }
2183 }
2184
2185 @Override
2186 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
2187 super.onPickOptionResult(finished, selections, result);
2188 if (selections.length != 1) {
2189 // TODO In a better world we would filter the UI presented here and let the
2190 // user refine. Maybe later.
2191 return;
2192 }
2193
2194 final ResolverActivity ra = (ResolverActivity) getActivity();
2195 if (ra != null) {
2196 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
2197 if (ra.onTargetSelected(ti, false)) {
2198 ra.mPickOptionRequest = null;
2199 ra.finish();
2200 }
2201 }
2202 }
2203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204}