blob: 068056f091d78417f65623a60672d76a41749d29 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.ComponentName;
33import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.Intent;
35import android.content.IntentFilter;
36import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010037import android.content.pm.ApplicationInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -070038import android.content.pm.LabeledIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070040import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010042import android.content.pm.UserInfo;
Jiongxuan Gao3365b162016-11-24 20:20:27 +090043import android.content.res.Configuration;
Adam Powellc5878612012-05-04 18:42:38 -070044import android.content.res.Resources;
Mike Digman9c4ae502019-03-19 17:02:25 -070045import android.graphics.Bitmap;
Matt Pietala4b30072019-04-04 13:44:36 -040046import android.graphics.ColorMatrix;
47import android.graphics.ColorMatrixColorFilter;
Matt Pietal800136a2019-05-08 07:46:39 -040048import android.graphics.Insets;
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;
Matt Pietal800136a2019-05-08 07:46:39 -040070import android.view.ViewGroup.LayoutParams;
71import android.view.WindowInsets;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080072import android.widget.AbsListView;
Adam Powell2d809622012-03-22 15:24:43 -070073import android.widget.AdapterView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.BaseAdapter;
Adam Powellc5878612012-05-04 18:42:38 -070075import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070077import android.widget.ListView;
Matt Pietal800136a2019-05-08 07:46:39 -040078import android.widget.Space;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010080import android.widget.Toast;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070081
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080082import com.android.internal.R;
83import com.android.internal.annotations.VisibleForTesting;
84import com.android.internal.content.PackageMonitor;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010085import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010086import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070087import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010090import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.util.Iterator;
92import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070093import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import java.util.Set;
95
96/**
97 * This activity is displayed when the system attempts to start an Intent for
98 * which there is more than one matching activity, allowing the user to decide
99 * which to go to. It is not normally used directly by application developers.
100 */
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800101@UiThread
Adam Powell7d758002015-05-06 17:49:36 -0700102public class ResolverActivity extends Activity {
Adam Powellc5878612012-05-04 18:42:38 -0700103
Alison Cichowlas4691ed42018-11-13 15:59:55 -0500104 // Temporary flag for new chooser delegate behavior.
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -0500105 boolean mEnableChooserDelegate = true;
Alison Cichowlas4691ed42018-11-13 15:59:55 -0500106
Andrei Onea15884392019-03-22 17:28:11 +0000107 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800108 protected ResolveListAdapter mAdapter;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700109 private boolean mSafeForwardingMode;
Matt Pietal5b648562019-03-12 07:40:26 -0400110 protected AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -0700111 private Button mAlwaysButton;
112 private Button mOnceButton;
Matt Pietal74c6ed02019-04-18 13:38:46 -0400113 protected View mProfileView;
Adam Powellc5878612012-05-04 18:42:38 -0700114 private int mIconDpi;
Adam Powell24428412015-04-01 17:19:56 -0700115 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100116 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000117 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800118 private int mLayoutId;
Adam Powell2ed547e2015-04-29 18:45:04 -0700119 private final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700120 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800121 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800122 private CharSequence mTitle;
123 private int mDefaultTitleResId;
shafik69df96322018-12-18 15:41:19 +0000124 private boolean mUseLayoutForBrowsables;
Adam Powell09a65602014-07-20 16:23:14 -0700125
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800126 // Whether or not this activity supports choosing a default handler for the intent.
127 private boolean mSupportsAlwaysUseOption;
Adam Powell63b31692015-09-28 10:45:00 -0700128 protected ResolverDrawerLayout mResolverDrawerLayout;
Andrei Onea15884392019-03-22 17:28:11 +0000129 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800130 protected PackageManager mPm;
131 protected int mLaunchedFromUid;
132
133 private static final String TAG = "ResolverActivity";
134 private static final boolean DEBUG = false;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800135 private Runnable mPostListReadyRunnable;
Adam Powell63b31692015-09-28 10:45:00 -0700136
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700137 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100138
Matt Pietala4b30072019-04-04 13:44:36 -0400139 private ColorMatrixColorFilter mSuspendedMatrixColorFilter;
140
Matt Pietal800136a2019-05-08 07:46:39 -0400141 protected Insets mSystemWindowInsets = null;
142 private Space mFooterSpacer = null;
143
Jorim Jaggif631ef72017-02-24 13:49:47 +0100144 /** See {@link #setRetainInOnStop}. */
145 private boolean mRetainInOnStop;
146
Matt Pietal0d6834a2019-06-27 13:27:52 -0400147 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
148 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
149 private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state";
150
Matt Pietalab73a882019-06-05 07:04:55 -0400151 private final PackageMonitor mPackageMonitor = createPackageMonitor();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800152
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700153 /**
154 * Get the string resource to be used as a label for the link to the resolver activity for an
155 * action.
156 *
157 * @param action The action to resolve
158 *
159 * @return The string resource to be used as a label
160 */
161 public static @StringRes int getLabelRes(String action) {
162 return ActionTitle.forAction(action).labelRes;
163 }
164
Adam Powell278902c2014-07-12 18:33:22 -0700165 private enum ActionTitle {
166 VIEW(Intent.ACTION_VIEW,
167 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700168 com.android.internal.R.string.whichViewApplicationNamed,
169 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700170 EDIT(Intent.ACTION_EDIT,
171 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700172 com.android.internal.R.string.whichEditApplicationNamed,
173 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700174 SEND(Intent.ACTION_SEND,
175 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700176 com.android.internal.R.string.whichSendApplicationNamed,
177 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700178 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700179 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700180 com.android.internal.R.string.whichSendToApplicationNamed,
181 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700182 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
183 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700184 com.android.internal.R.string.whichSendApplicationNamed,
185 com.android.internal.R.string.whichSendApplicationLabel),
186 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
187 com.android.internal.R.string.whichImageCaptureApplication,
188 com.android.internal.R.string.whichImageCaptureApplicationNamed,
189 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700190 DEFAULT(null,
191 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700192 com.android.internal.R.string.whichApplicationNamed,
193 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700194 HOME(Intent.ACTION_MAIN,
195 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700196 com.android.internal.R.string.whichHomeApplicationNamed,
197 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700198
shafik0c7c5d52019-02-27 12:13:25 +0000199 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000200 public static final int BROWSABLE_TITLE_RES =
Matt Pietal0d6834a2019-06-27 13:27:52 -0400201 com.android.internal.R.string.whichOpenLinksWith;
202 public static final int BROWSABLE_HOST_TITLE_RES =
203 com.android.internal.R.string.whichOpenHostLinksWith;
204 public static final int BROWSABLE_HOST_APP_TITLE_RES =
205 com.android.internal.R.string.whichOpenHostLinksWithApp;
206 public static final int BROWSABLE_APP_TITLE_RES =
207 com.android.internal.R.string.whichOpenLinksWithApp;
shafik69df96322018-12-18 15:41:19 +0000208
Adam Powell278902c2014-07-12 18:33:22 -0700209 public final String action;
210 public final int titleRes;
211 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700212 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700213
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700214 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700215 this.action = action;
216 this.titleRes = titleRes;
217 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700218 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700219 }
220
221 public static ActionTitle forAction(String action) {
222 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700223 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700224 return title;
225 }
226 }
227 return DEFAULT;
228 }
229 }
230
Matt Pietalab73a882019-06-05 07:04:55 -0400231 protected PackageMonitor createPackageMonitor() {
232 return new PackageMonitor() {
233 @Override
234 public void onSomePackagesChanged() {
235 mAdapter.handlePackagesChanged();
236 bindProfileView();
237 }
238
239 @Override
240 public boolean onPackageChanged(String packageName, int uid, String[] components) {
241 // We care about all package changes, not just the whole package itself which is
242 // default behavior.
243 return true;
244 }
245 };
246 }
247
Dianne Hackborn905577f2011-09-07 18:31:28 -0700248 private Intent makeMyIntent() {
249 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700250 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700251 // The resolver activity is set to be hidden from recent tasks.
252 // we don't want this attribute to be propagated to the next activity
253 // being launched. Note that if the original Intent also had this
254 // flag set, we are now losing it. That should be a very rare case
255 // and we can live with this.
256 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
257 return intent;
258 }
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 @Override
261 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700262 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700263 final Intent intent = makeMyIntent();
264 final Set<String> categories = intent.getCategories();
265 if (Intent.ACTION_MAIN.equals(intent.getAction())
266 && categories != null
267 && categories.size() == 1
268 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100269 // Note: this field is not set to true in the compatibility version.
270 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700271 }
272
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700273 setSafeForwardingMode(true);
274
Adam Powella35c77a2014-09-25 16:46:36 -0700275 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277
Adam Powell278902c2014-07-12 18:33:22 -0700278 /**
Adam Powell24428412015-04-01 17:19:56 -0700279 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700280 * a default title resource
281 */
Andrei Onea15884392019-03-22 17:28:11 +0000282 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700284 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800285 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
286 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
287 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700288 }
289
290 protected void onCreate(Bundle savedInstanceState, Intent intent,
291 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800292 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700293 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000295
296 // Determine whether we should show that intent is forwarded
297 // from managed profile to owner or other way around.
298 setProfileSwitchMessageId(intent.getContentUserHint());
299
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700300 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700301 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700302 getActivityToken());
303 } catch (RemoteException e) {
304 mLaunchedFromUid = -1;
305 }
Adam Powell7d758002015-05-06 17:49:36 -0700306
307 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
308 // Gulp!
309 finish();
310 return;
311 }
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700314
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700315 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700316 mRegistered = true;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800317 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800318
Adam Powellc5878612012-05-04 18:42:38 -0700319 final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
320 mIconDpi = am.getLauncherLargeIconDensity();
Adam Powellc5878612012-05-04 18:42:38 -0700321
Adam Powell7d758002015-05-06 17:49:36 -0700322 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700323 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800324 mTitle = title;
325 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700326
shafik69df96322018-12-18 15:41:19 +0000327 mUseLayoutForBrowsables = getTargetIntent() == null
328 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000329 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000330
Matt Pietal0d6834a2019-06-27 13:27:52 -0400331 mSupportsAlwaysUseOption = supportsAlwaysUseOption;
shafik69df96322018-12-18 15:41:19 +0000332
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800333 if (configureContentView(mIntents, initialIntents, rList)) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700334 return;
335 }
Adam Powell278902c2014-07-12 18:33:22 -0700336
Alan Viverette51efddb2017-04-05 10:00:01 -0400337 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700338 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700339 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700340 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700341 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700342 finish();
343 }
344 });
Adam Powell4c470d62015-06-19 17:46:17 -0700345 if (isVoiceInteraction()) {
346 rdl.setCollapsed(false);
347 }
Matt Pietal800136a2019-05-08 07:46:39 -0400348
349 rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
350 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
351 rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets);
352
Adam Powell63b31692015-09-28 10:45:00 -0700353 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700354 }
355
Adam Powell88831a22014-11-20 18:17:00 -0800356 mProfileView = findViewById(R.id.profile_button);
357 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400358 mProfileView.setOnClickListener(this::onProfileClick);
Adam Powell88831a22014-11-20 18:17:00 -0800359 bindProfileView();
360 }
Adam Powell4c470d62015-06-19 17:46:17 -0700361
Matt Pietala4b30072019-04-04 13:44:36 -0400362 initSuspendedColorMatrix();
363
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100364 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100365 MetricsLogger.action(this, mAdapter.hasFilteredItem()
366 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
367 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
368 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100369 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700370 }
371
Matt Pietal74c6ed02019-04-18 13:38:46 -0400372 protected void onProfileClick(View v) {
373 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
374 if (dri == null) {
375 return;
376 }
377
378 // Do not show the profile switch message anymore.
379 mProfileSwitchMessageId = -1;
380
381 onTargetSelected(dri, false);
382 finish();
383 }
384
Matt Pietal800136a2019-05-08 07:46:39 -0400385 protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
386 mSystemWindowInsets = insets.getSystemWindowInsets();
387
388 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
389 mSystemWindowInsets.right, 0);
390
Matt Pietal23a02f72019-10-10 11:21:28 -0400391 // Need extra padding so the list can fully scroll up
392 if (useLayoutWithDefault()) {
393 if (mFooterSpacer == null) {
394 mFooterSpacer = new Space(getApplicationContext());
395 } else {
396 ((ListView) mAdapterView).removeFooterView(mFooterSpacer);
397 }
398 mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
399 mSystemWindowInsets.bottom));
400 ((ListView) mAdapterView).addFooterView(mFooterSpacer);
Matt Pietal800136a2019-05-08 07:46:39 -0400401 } else {
Matt Pietal23a02f72019-10-10 11:21:28 -0400402 View emptyView = findViewById(R.id.empty);
403 if (emptyView != null) {
404 emptyView.setPadding(0, 0, 0, mSystemWindowInsets.bottom
405 + getResources().getDimensionPixelSize(
406 R.dimen.chooser_edge_margin_normal) * 2);
407 }
Matt Pietal800136a2019-05-08 07:46:39 -0400408 }
Matt Pietal800136a2019-05-08 07:46:39 -0400409
Matt Pietal78fdbd42019-06-05 07:08:08 -0400410 resetButtonBar();
411
Matt Pietal800136a2019-05-08 07:46:39 -0400412 return insets.consumeSystemWindowInsets();
413 }
414
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900415 @Override
416 public void onConfigurationChanged(Configuration newConfig) {
417 super.onConfigurationChanged(newConfig);
418 mAdapter.handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400419
420 if (mSystemWindowInsets != null) {
421 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
422 mSystemWindowInsets.right, 0);
423 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900424 }
425
Matt Pietala4b30072019-04-04 13:44:36 -0400426 private void initSuspendedColorMatrix() {
427 int grayValue = 127;
428 float scale = 0.5f; // half bright
429
430 ColorMatrix tempBrightnessMatrix = new ColorMatrix();
431 float[] mat = tempBrightnessMatrix.getArray();
432 mat[0] = scale;
433 mat[6] = scale;
434 mat[12] = scale;
435 mat[4] = grayValue;
436 mat[9] = grayValue;
437 mat[14] = grayValue;
438
439 ColorMatrix matrix = new ColorMatrix();
440 matrix.setSaturation(0.0f);
441 matrix.preConcat(tempBrightnessMatrix);
442 mSuspendedMatrixColorFilter = new ColorMatrixColorFilter(matrix);
443 }
444
Adam Powell23882512016-01-29 10:21:00 -0800445 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700446 if (!isVoiceInteraction()) {
447 // Clearly not needed.
448 return;
449 }
450
Adam Powell4c470d62015-06-19 17:46:17 -0700451 final Option[] options = new Option[mAdapter.getCount()];
452 for (int i = 0, N = options.length; i < N; i++) {
Matt Pietal553d8a42019-09-10 09:33:11 -0400453 TargetInfo target = mAdapter.getItem(i);
454 if (target == null) {
455 // If this occurs, a new set of targets is being loaded. Let that complete,
456 // and have the next call to send voice choices proceed instead.
457 return;
458 }
459 options[i] = optionForChooserTarget(target, i);
Adam Powell4c470d62015-06-19 17:46:17 -0700460 }
461
462 mPickOptionRequest = new PickTargetOptionRequest(
463 new Prompt(getTitle()), options, null);
464 getVoiceInteractor().submitRequest(mPickOptionRequest);
465 }
466
467 Option optionForChooserTarget(TargetInfo target, int index) {
468 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800469 }
470
Adam Powell2ed547e2015-04-29 18:45:04 -0700471 protected final void setAdditionalTargets(Intent[] intents) {
472 if (intents != null) {
473 for (Intent intent : intents) {
474 mIntents.add(intent);
475 }
476 }
477 }
478
Adam Powell0ccc0e92015-04-23 17:19:37 -0700479 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700480 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700481 }
482
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800483 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700484 final Uri referrer = getReferrer();
485 if (referrer != null && "android-app".equals(referrer.getScheme())) {
486 return referrer.getHost();
487 }
488 return null;
489 }
490
Adam Powell23882512016-01-29 10:21:00 -0800491 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700492 return R.layout.resolver_list;
493 }
494
Matt Pietal74c6ed02019-04-18 13:38:46 -0400495 protected void bindProfileView() {
496 if (mProfileView == null) {
497 return;
498 }
499
Adam Powell88831a22014-11-20 18:17:00 -0800500 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
501 if (dri != null) {
502 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800503 View text = mProfileView.findViewById(R.id.profile_button);
504 if (!(text instanceof TextView)) {
505 text = mProfileView.findViewById(R.id.text1);
506 }
507 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800508 } else {
509 mProfileView.setVisibility(View.GONE);
510 }
Adam Powell278902c2014-07-12 18:33:22 -0700511 }
512
Sander Alewijnsef6545332014-10-31 12:39:02 +0000513 private void setProfileSwitchMessageId(int contentUserHint) {
514 if (contentUserHint != UserHandle.USER_CURRENT &&
515 contentUserHint != UserHandle.myUserId()) {
516 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
517 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
518 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
519 : false;
520 boolean targetIsManaged = userManager.isManagedProfile();
521 if (originIsManaged && !targetIsManaged) {
522 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
523 } else if (!originIsManaged && targetIsManaged) {
524 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
525 }
526 }
527 }
528
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700529 /**
530 * Turn on launch mode that is safe to use when forwarding intents received from
531 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
532 * instead of the normal Activity.startActivity for launching the activity selected
533 * by the user.
534 *
535 * <p>This mode is set to true by default if the activity is initialized through
536 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
537 * methods, it is set to false by default. You must set it before calling one of the
538 * more detailed onCreate methods, so that it will be set correctly in the case where
539 * there is only one intent to resolve and it is thus started immediately.</p>
540 */
541 public void setSafeForwardingMode(boolean safeForwarding) {
542 mSafeForwardingMode = safeForwarding;
543 }
544
shafik69df96322018-12-18 15:41:19 +0000545 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
546 final ActionTitle title = mResolvingHome
547 ? ActionTitle.HOME
548 : ActionTitle.forAction(intent.getAction());
549
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800550 // While there may already be a filtered item, we can only use it in the title if the list
551 // is already sorted and all information relevant to it is already in the list.
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800552 final boolean named = mAdapter.getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700553 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
554 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000555 } else if (isHttpSchemeAndViewAction(intent)) {
556 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000557 // they're giving access for the activity to open URLs from this specific host
Matt Pietal0d6834a2019-06-27 13:27:52 -0400558 String dialogTitle = null;
559 if (named && !mUseLayoutForBrowsables) {
560 dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES,
561 mAdapter.getFilteredItem().getDisplayLabel());
562 } else if (named && mUseLayoutForBrowsables) {
563 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES,
564 intent.getData().getHost(),
565 mAdapter.getFilteredItem().getDisplayLabel());
566 } else if (mAdapter.areAllTargetsBrowsers()) {
Matt Pietal23a02f72019-10-10 11:21:28 -0400567 dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES);
Matt Pietal0d6834a2019-06-27 13:27:52 -0400568 } else {
569 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES,
570 intent.getData().getHost());
571 }
572 return dialogTitle;
Adam Powell278902c2014-07-12 18:33:22 -0700573 } else {
Adam Powell24428412015-04-01 17:19:56 -0700574 return named
575 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
576 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700577 }
Adam Powellc5878612012-05-04 18:42:38 -0700578 }
579
Adam Powelle9414d92014-07-05 17:44:18 -0700580 void dismiss() {
581 if (!isFinishing()) {
582 finish();
583 }
584 }
585
Adam Powellc5878612012-05-04 18:42:38 -0700586
Mike Digman220587e2019-03-13 11:21:42 -0700587 /**
Mike Digmanba232682019-03-27 14:55:26 -0700588 * Loads the icon and label for the provided ApplicationInfo. Defaults to using the application
589 * icon and label over any IntentFilter or Activity icon to increase user understanding, with an
590 * exception for applications that hold the right permission. Always attempts to use available
591 * resources over PackageManager loading mechanisms so badging can be done by iconloader. Uses
592 * Strings to strip creative formatting.
Mike Digman220587e2019-03-13 11:21:42 -0700593 */
Mike Digmanba232682019-03-27 14:55:26 -0700594 private abstract static class TargetPresentationGetter {
595 @Nullable abstract Drawable getIconSubstituteInternal();
596 @Nullable abstract String getAppSubLabelInternal();
Mike Digman220587e2019-03-13 11:21:42 -0700597
Mike Digmanba232682019-03-27 14:55:26 -0700598 private Context mCtx;
Mike Digmanba232682019-03-27 14:55:26 -0700599 private final int mIconDpi;
Mike Digman9c4ae502019-03-19 17:02:25 -0700600 private final boolean mHasSubstitutePermission;
Mike Digmanc0ef5542019-04-01 15:36:25 -0700601 private final ApplicationInfo mAi;
602
603 protected PackageManager mPm;
Mike Digman9c4ae502019-03-19 17:02:25 -0700604
Mike Digmanba232682019-03-27 14:55:26 -0700605 TargetPresentationGetter(Context ctx, int iconDpi, ApplicationInfo ai) {
606 mCtx = ctx;
607 mPm = ctx.getPackageManager();
Mike Digman9c4ae502019-03-19 17:02:25 -0700608 mAi = ai;
Mike Digmanba232682019-03-27 14:55:26 -0700609 mIconDpi = iconDpi;
Mike Digman9c4ae502019-03-19 17:02:25 -0700610 mHasSubstitutePermission = PackageManager.PERMISSION_GRANTED == mPm.checkPermission(
611 android.Manifest.permission.SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON,
612 mAi.packageName);
613 }
614
Mike Digmanb2e5e712019-04-19 15:49:10 -0700615 public Drawable getIcon(UserHandle userHandle) {
616 return new BitmapDrawable(mCtx.getResources(), getIconBitmap(userHandle));
Mike Digman9c4ae502019-03-19 17:02:25 -0700617 }
618
Mike Digmanb2e5e712019-04-19 15:49:10 -0700619 public Bitmap getIconBitmap(UserHandle userHandle) {
Mike Digman9c4ae502019-03-19 17:02:25 -0700620 Drawable dr = null;
621 if (mHasSubstitutePermission) {
Mike Digmanba232682019-03-27 14:55:26 -0700622 dr = getIconSubstituteInternal();
Mike Digman9c4ae502019-03-19 17:02:25 -0700623 }
624
625 if (dr == null) {
626 try {
627 if (mAi.icon != 0) {
628 dr = loadIconFromResource(mPm.getResourcesForApplication(mAi), mAi.icon);
Mike Digman220587e2019-03-13 11:21:42 -0700629 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700630 } catch (NameNotFoundException ignore) {
631 }
632 }
633
634 // Fall back to ApplicationInfo#loadIcon if nothing has been loaded
635 if (dr == null) {
636 dr = mAi.loadIcon(mPm);
637 }
638
Mike Digmanba232682019-03-27 14:55:26 -0700639 SimpleIconFactory sif = SimpleIconFactory.obtain(mCtx);
Mike Digmanb2e5e712019-04-19 15:49:10 -0700640 Bitmap icon = sif.createUserBadgedIconBitmap(dr, userHandle);
Mike Digman9c4ae502019-03-19 17:02:25 -0700641 sif.recycle();
642
643 return icon;
644 }
645
Mike Digmanba232682019-03-27 14:55:26 -0700646 public String getLabel() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700647 String label = null;
648 // Apps with the substitute permission will always show the sublabel as their label
649 if (mHasSubstitutePermission) {
Mike Digmanba232682019-03-27 14:55:26 -0700650 label = getAppSubLabelInternal();
Mike Digman9c4ae502019-03-19 17:02:25 -0700651 }
652
653 if (label == null) {
654 label = (String) mAi.loadLabel(mPm);
655 }
656
657 return label;
658 }
659
Mike Digmanba232682019-03-27 14:55:26 -0700660 public String getSubLabel() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700661 // Apps with the substitute permission will never have a sublabel
662 if (mHasSubstitutePermission) return null;
Mike Digmanba232682019-03-27 14:55:26 -0700663 return getAppSubLabelInternal();
664 }
665
666 protected String loadLabelFromResource(Resources res, int resId) {
667 return res.getString(resId);
Mike Digman9c4ae502019-03-19 17:02:25 -0700668 }
669
670 @Nullable
671 protected Drawable loadIconFromResource(Resources res, int resId) {
672 return res.getDrawableForDensity(resId, mIconDpi);
673 }
674
675 }
676
Mike Digmanba232682019-03-27 14:55:26 -0700677 /**
678 * Loads the icon and label for the provided ResolveInfo.
679 */
680 @VisibleForTesting
Mike Digmanc0ef5542019-04-01 15:36:25 -0700681 public static class ResolveInfoPresentationGetter extends ActivityInfoPresentationGetter {
Mike Digman9c4ae502019-03-19 17:02:25 -0700682 private final ResolveInfo mRi;
Mike Digmanba232682019-03-27 14:55:26 -0700683 public ResolveInfoPresentationGetter(Context ctx, int iconDpi, ResolveInfo ri) {
Mike Digmanc0ef5542019-04-01 15:36:25 -0700684 super(ctx, iconDpi, ri.activityInfo);
Mike Digman9c4ae502019-03-19 17:02:25 -0700685 mRi = ri;
686 }
687
688 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700689 Drawable getIconSubstituteInternal() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700690 Drawable dr = null;
691 try {
692 // Do not use ResolveInfo#getIconResource() as it defaults to the app
693 if (mRi.resolvePackageName != null && mRi.icon != 0) {
694 dr = loadIconFromResource(
695 mPm.getResourcesForApplication(mRi.resolvePackageName), mRi.icon);
Adam Powellc5878612012-05-04 18:42:38 -0700696 }
Mike Digman220587e2019-03-13 11:21:42 -0700697 } catch (NameNotFoundException e) {
698 Log.e(TAG, "SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON permission granted but "
699 + "couldn't find resources for package", e);
Adam Powellc5878612012-05-04 18:42:38 -0700700 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700701
Mike Digmanc0ef5542019-04-01 15:36:25 -0700702 // Fall back to ActivityInfo if no icon is found via ResolveInfo
703 if (dr == null) dr = super.getIconSubstituteInternal();
704
Mike Digman9c4ae502019-03-19 17:02:25 -0700705 return dr;
Adam Powellc5878612012-05-04 18:42:38 -0700706 }
Mike Digman220587e2019-03-13 11:21:42 -0700707
Mike Digman9c4ae502019-03-19 17:02:25 -0700708 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700709 String getAppSubLabelInternal() {
710 // Will default to app name if no intent filter or activity label set, make sure to
711 // check if subLabel matches label before final display
Mike Digman9c4ae502019-03-19 17:02:25 -0700712 return (String) mRi.loadLabel(mPm);
713 }
714 }
715
Mike Digmanba232682019-03-27 14:55:26 -0700716 ResolveInfoPresentationGetter makePresentationGetter(ResolveInfo ri) {
717 return new ResolveInfoPresentationGetter(this, mIconDpi, ri);
718 }
719
720 /**
721 * Loads the icon and label for the provided ActivityInfo.
722 */
723 @VisibleForTesting
724 public static class ActivityInfoPresentationGetter extends TargetPresentationGetter {
Mike Digman9c4ae502019-03-19 17:02:25 -0700725 private final ActivityInfo mActivityInfo;
Mike Digmanba232682019-03-27 14:55:26 -0700726 public ActivityInfoPresentationGetter(Context ctx, int iconDpi,
727 ActivityInfo activityInfo) {
728 super(ctx, iconDpi, activityInfo.applicationInfo);
Mike Digman9c4ae502019-03-19 17:02:25 -0700729 mActivityInfo = activityInfo;
730 }
731
732 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700733 Drawable getIconSubstituteInternal() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700734 Drawable dr = null;
Mike Digman220587e2019-03-13 11:21:42 -0700735 try {
Mike Digman9c4ae502019-03-19 17:02:25 -0700736 // Do not use ActivityInfo#getIconResource() as it defaults to the app
737 if (mActivityInfo.icon != 0) {
738 dr = loadIconFromResource(
739 mPm.getResourcesForApplication(mActivityInfo.applicationInfo),
740 mActivityInfo.icon);
741 }
742 } catch (NameNotFoundException e) {
743 Log.e(TAG, "SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON permission granted but "
744 + "couldn't find resources for package", e);
Mike Digman220587e2019-03-13 11:21:42 -0700745 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700746
747 return dr;
Mike Digman220587e2019-03-13 11:21:42 -0700748 }
749
Mike Digman9c4ae502019-03-19 17:02:25 -0700750 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700751 String getAppSubLabelInternal() {
752 // Will default to app name if no activity label set, make sure to check if subLabel
753 // matches label before final display
Mike Digman9c4ae502019-03-19 17:02:25 -0700754 return (String) mActivityInfo.loadLabel(mPm);
Mike Digman220587e2019-03-13 11:21:42 -0700755 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700756 }
Mike Digman220587e2019-03-13 11:21:42 -0700757
Mike Digmanba232682019-03-27 14:55:26 -0700758 protected ActivityInfoPresentationGetter makePresentationGetter(ActivityInfo ai) {
759 return new ActivityInfoPresentationGetter(this, mIconDpi, ai);
760 }
761
Mike Digman9c4ae502019-03-19 17:02:25 -0700762 Drawable loadIconForResolveInfo(ResolveInfo ri) {
Mike Digmanb2e5e712019-04-19 15:49:10 -0700763 // Load icons based on the current process. If in work profile icons should be badged.
764 return makePresentationGetter(ri).getIcon(Process.myUserHandle());
Adam Powellc5878612012-05-04 18:42:38 -0700765 }
766
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800767 @Override
768 protected void onRestart() {
769 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700770 if (!mRegistered) {
771 mPackageMonitor.register(this, getMainLooper(), false);
772 mRegistered = true;
773 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800774 mAdapter.handlePackagesChanged();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400775 bindProfileView();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800776 }
777
778 @Override
779 protected void onStop() {
780 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700781 if (mRegistered) {
782 mPackageMonitor.unregister();
783 mRegistered = false;
784 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700785 final Intent intent = getIntent();
786 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100787 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700788 // This resolver is in the unusual situation where it has been
789 // launched at the top of a new task. We don't let it be added
790 // to the recent tasks shown to the user, and we need to make sure
791 // that each time we are launched we get the correct launching
792 // uid (not re-using the same resolver from an old launching uid),
793 // so we will now finish ourself since being no longer visible,
794 // the user probably can't get back to us.
795 if (!isChangingConfigurations()) {
796 finish();
797 }
798 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800799 }
800
Adam Powellc5878612012-05-04 18:42:38 -0700801 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700802 protected void onDestroy() {
803 super.onDestroy();
804 if (!isChangingConfigurations() && mPickOptionRequest != null) {
805 mPickOptionRequest.cancel();
806 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800807 if (mPostListReadyRunnable != null) {
808 getMainThreadHandler().removeCallbacks(mPostListReadyRunnable);
809 mPostListReadyRunnable = null;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800810 }
Kang Li38a6da642017-04-05 12:30:55 -0700811 if (mAdapter != null && mAdapter.mResolverListController != null) {
812 mAdapter.mResolverListController.destroy();
813 }
Adam Powell4c470d62015-06-19 17:46:17 -0700814 }
815
816 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700817 protected void onRestoreInstanceState(Bundle savedInstanceState) {
818 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000819 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700820 }
821
shafik0c7c5d52019-02-27 12:13:25 +0000822 private boolean isHttpSchemeAndViewAction(Intent intent) {
823 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
824 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
825 && Intent.ACTION_VIEW.equals(intent.getAction());
826 }
827
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100828 private boolean hasManagedProfile() {
829 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
830 if (userManager == null) {
831 return false;
832 }
833
834 try {
835 List<UserInfo> profiles = userManager.getProfiles(getUserId());
836 for (UserInfo userInfo : profiles) {
837 if (userInfo != null && userInfo.isManagedProfile()) {
838 return true;
839 }
840 }
841 } catch (SecurityException e) {
842 return false;
843 }
844 return false;
845 }
846
847 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
848 try {
849 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
850 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700851 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100852 } catch (NameNotFoundException e) {
853 return false;
854 }
855 }
856
Adam Powell278902c2014-07-12 18:33:22 -0700857 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
858 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100859 boolean enabled = false;
860 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700861 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800862 if (ri == null) {
863 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
864 return;
865 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
866 Log.e(TAG, "Attempted to set selection to resolve info for another user");
867 return;
868 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100869 enabled = true;
870 }
Matt Pietal0d6834a2019-06-27 13:27:52 -0400871 if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) {
872 mAlwaysButton.setText(getResources()
873 .getString(R.string.activity_resolver_set_always));
874 } else {
875 mAlwaysButton.setText(getResources()
876 .getString(R.string.activity_resolver_use_always));
877 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100878 }
879 mAlwaysButton.setEnabled(enabled);
880 }
881
Adam Powellc5878612012-05-04 18:42:38 -0700882 public void onButtonClick(View v) {
883 final int id = v.getId();
shafik69df96322018-12-18 15:41:19 +0000884 int which = mAdapter.hasFilteredItem()
885 ? mAdapter.getFilteredPosition()
886 : mAdapterView.getCheckedItemPosition();
887 boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400888 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Matt Pietala310c1f2019-07-03 09:12:56 -0400889 if (mUseLayoutForBrowsables
890 && !ri.handleAllWebDataURI && id == R.id.button_always) {
Matt Pietal0d6834a2019-06-27 13:27:52 -0400891 showSettingsForSelected(ri);
shafik69df96322018-12-18 15:41:19 +0000892 } else {
893 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
894 }
895 }
896
Matt Pietal0d6834a2019-06-27 13:27:52 -0400897 private void showSettingsForSelected(ResolveInfo ri) {
shafik34064c32019-03-06 15:42:52 +0000898 Intent intent = new Intent();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400899
900 final String packageName = ri.activityInfo.packageName;
901 Bundle showFragmentArgs = new Bundle();
902 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY);
903 showFragmentArgs.putString("package", packageName);
904
shafik34064c32019-03-06 15:42:52 +0000905 // For regular apps, we open the Open by Default page
Matt Pietal0d6834a2019-06-27 13:27:52 -0400906 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
907 .setData(Uri.fromParts("package", packageName, null))
908 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
909 .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY)
910 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
911
shafik34064c32019-03-06 15:42:52 +0000912 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700913 }
914
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800915 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700916 if (isFinishing()) {
917 return;
918 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800919 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000920 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
921 Toast.makeText(this, String.format(getResources().getString(
922 com.android.internal.R.string.activity_resolver_work_profiles_support),
923 ri.activityInfo.loadLabel(getPackageManager()).toString()),
924 Toast.LENGTH_LONG).show();
925 return;
926 }
927
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800928 TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800929 if (target == null) {
930 return;
931 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700932 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800933 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100934 MetricsLogger.action(
935 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800936 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100937 MetricsLogger.action(
938 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
939 } else {
940 MetricsLogger.action(
941 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
942 }
943 MetricsLogger.action(this, mAdapter.hasFilteredItem()
944 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
945 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700946 finish();
947 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949
Adam Powelle49d9392014-07-17 18:45:19 -0700950 /**
951 * Replace me in subclasses!
952 */
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000953 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700954 return defIntent;
955 }
956
Adam Powell2ed547e2015-04-29 18:45:04 -0700957 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700958 final ResolveInfo ri = target.getResolveInfo();
959 final Intent intent = target != null ? target.getResolvedIntent() : null;
960
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800961 if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800962 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700963 // Build a reasonable intent filter, based on what matched.
964 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200965 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966
Henrik Engström3277cf12014-07-17 12:18:29 +0200967 if (intent.getSelector() != null) {
968 filterIntent = intent.getSelector();
969 } else {
970 filterIntent = intent;
971 }
972
973 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800974 if (action != null) {
975 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200977 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700978 if (categories != null) {
979 for (String cat : categories) {
980 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700983 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984
Adam Powell24428412015-04-01 17:19:56 -0700985 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200986 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700987 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200988 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700989 if (mimeType != null) {
990 try {
991 filter.addDataType(mimeType);
992 } catch (IntentFilter.MalformedMimeTypeException e) {
993 Log.w("ResolverActivity", e);
994 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700997 }
998 if (data != null && data.getScheme() != null) {
999 // We need the data specification if there was no type,
1000 // OR if the scheme is not one of our magical "file:"
1001 // or "content:" schemes (see IntentFilter for the reason).
1002 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
1003 || (!"file".equals(data.getScheme())
1004 && !"content".equals(data.getScheme()))) {
1005 filter.addDataScheme(data.getScheme());
1006
1007 // Look through the resolved filter to determine which part
1008 // of it matched the original Intent.
1009 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
1010 if (pIt != null) {
1011 String ssp = data.getSchemeSpecificPart();
1012 while (ssp != null && pIt.hasNext()) {
1013 PatternMatcher p = pIt.next();
1014 if (p.match(ssp)) {
1015 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
1016 break;
1017 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001018 }
1019 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001020 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
1021 if (aIt != null) {
1022 while (aIt.hasNext()) {
1023 IntentFilter.AuthorityEntry a = aIt.next();
1024 if (a.match(data) >= 0) {
1025 int port = a.getPort();
1026 filter.addDataAuthority(a.getHost(),
1027 port >= 0 ? Integer.toString(port) : null);
1028 break;
1029 }
1030 }
1031 }
1032 pIt = ri.filter.pathsIterator();
1033 if (pIt != null) {
1034 String path = data.getPath();
1035 while (path != null && pIt.hasNext()) {
1036 PatternMatcher p = pIt.next();
1037 if (p.match(path)) {
1038 filter.addDataPath(p.getPath(), p.getType());
1039 break;
1040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
1042 }
1043 }
1044 }
1045
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001046 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001047 final int N = mAdapter.mUnfilteredResolveList.size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001048 ComponentName[] set;
1049 // If we don't add back in the component for forwarding the intent to a managed
1050 // profile, the preferred activity may not be updated correctly (as the set of
1051 // components we tell it we knew about will have changed).
1052 final boolean needToAddBackProfileForwardingComponent
1053 = mAdapter.mOtherProfile != null;
1054 if (!needToAddBackProfileForwardingComponent) {
1055 set = new ComponentName[N];
1056 } else {
1057 set = new ComponentName[N + 1];
1058 }
1059
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001060 int bestMatch = 0;
1061 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001062 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001063 set[i] = new ComponentName(r.activityInfo.packageName,
1064 r.activityInfo.name);
1065 if (r.match > bestMatch) bestMatch = r.match;
1066 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001067
1068 if (needToAddBackProfileForwardingComponent) {
1069 set[N] = mAdapter.mOtherProfile.getResolvedComponentName();
1070 final int otherProfileMatch = mAdapter.mOtherProfile.getResolveInfo().match;
1071 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
1072 }
1073
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001074 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001075 final int userId = getUserId();
1076 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001077
1078 // Set the preferred Activity
1079 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
1080
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001081 if (ri.handleAllWebDataURI) {
1082 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001083 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001084 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001085 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001086 }
1087 } else {
1088 // Update Domain Verification status
1089 ComponentName cn = intent.getComponent();
1090 String packageName = cn.getPackageName();
1091 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001092
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001093 boolean isHttpOrHttps = (dataScheme != null) &&
1094 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
1095 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001096
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001097 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
1098 boolean hasCategoryBrowsable = (categories != null) &&
1099 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001100
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001101 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001102 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001103 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
1104 userId);
1105 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001106 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001107 } else {
1108 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -08001109 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001110 } catch (RemoteException re) {
1111 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1112 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115 }
1116
Adam Powell24428412015-04-01 17:19:56 -07001117 if (target != null) {
1118 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -04001119
1120 // Rely on the ActivityManager to pop up a dialog regarding app suspension
1121 // and return false
1122 if (target.isSuspended()) {
1123 return false;
1124 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001125 }
Matt Pietala4b30072019-04-04 13:44:36 -04001126
Adam Powell2ed547e2015-04-29 18:45:04 -07001127 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001128 }
1129
Adam Powell23882512016-01-29 10:21:00 -08001130 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -07001131 // We're dispatching intents that might be coming from legacy apps, so
1132 // don't kill ourselves.
1133 StrictMode.disableDeathOnFileUriExposure();
1134 try {
1135 safelyStartActivityInternal(cti);
1136 } finally {
1137 StrictMode.enableDeathOnFileUriExposure();
1138 }
1139 }
1140
1141 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +00001142 // If needed, show that intent is forwarded
1143 // from managed profile to owner or other way around.
1144 if (mProfileSwitchMessageId != -1) {
1145 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
1146 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001147 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -07001148 if (cti.start(this, null)) {
1149 onActivityStarted(cti);
1150 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001151 return;
1152 }
1153 try {
Adam Powell24428412015-04-01 17:19:56 -07001154 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
1155 onActivityStarted(cti);
1156 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001157 } catch (RuntimeException e) {
1158 String launchedFromPackage;
1159 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001160 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001161 getActivityToken());
1162 } catch (RemoteException e2) {
1163 launchedFromPackage = "??";
1164 }
1165 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
1166 + " package " + launchedFromPackage + ", while running in "
1167 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170
Alison Cichowlas3e340502018-08-07 17:15:01 -04001171
1172 boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
1173 int userId) {
1174 // Pass intent to delegate chooser activity with permission token.
1175 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
1176 // moves into systemui
1177 try {
1178 // TODO: Once this is a small springboard activity, it can move off the UI process
1179 // and we can move the request method to ActivityManagerInternal.
1180 IBinder permissionToken = ActivityTaskManager.getService()
1181 .requestStartActivityPermissionToken(getActivityToken());
1182 final Intent chooserIntent = new Intent();
1183 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1184 Resources.getSystem().getString(R.string.config_chooserActivity));
1185 chooserIntent.setClassName(delegateActivity.getPackageName(),
1186 delegateActivity.getClassName());
1187 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1188
1189 // TODO: These extras will change as chooser activity moves into systemui
1190 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1191 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1192 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1193 ignoreTargetSecurity);
1194 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001195 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1196 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001197 startActivity(chooserIntent);
1198 } catch (RemoteException e) {
1199 Log.e(TAG, e.toString());
1200 }
1201 return true;
1202 }
1203
Adam Powell23882512016-01-29 10:21:00 -08001204 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001205 // Do nothing
1206 }
1207
Adam Powell23882512016-01-29 10:21:00 -08001208 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001209 return false;
1210 }
1211
Adam Powell23882512016-01-29 10:21:00 -08001212 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001213 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001214 }
1215
Adam Powell23882512016-01-29 10:21:00 -08001216 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001217 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001218 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001219 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001220 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001221 }
1222
Adam Powell23882512016-01-29 10:21:00 -08001223 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents,
Adam Powell7d758002015-05-06 17:49:36 -07001224 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
1225 boolean filterLastUsed) {
1226 return new ResolveListAdapter(context, payloadIntents, initialIntents, rList,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001227 launchedFromUid, filterLastUsed, createListController());
1228 }
1229
1230 @VisibleForTesting
1231 protected ResolverListController createListController() {
1232 return new ResolverListController(
1233 this,
1234 mPm,
1235 getTargetIntent(),
1236 getReferrerPackageName(),
1237 mLaunchedFromUid);
Adam Powell88831a22014-11-20 18:17:00 -08001238 }
1239
Adam Powell39e94eb2015-09-08 17:01:49 -07001240 /**
1241 * Returns true if the activity is finishing and creation should halt
1242 */
Adam Powell23882512016-01-29 10:21:00 -08001243 public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents,
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001244 List<ResolveInfo> rList) {
Dianne Hackborn57dd7372015-07-27 18:11:14 -07001245 // The last argument of createAdapter is whether to do special handling
1246 // of the last used choice to highlight it in the list. We need to always
1247 // turn this off when running under voice interaction, since it results in
1248 // a more complicated UI that the current voice interaction flow is not able
1249 // to handle.
Adam Powell7d758002015-05-06 17:49:36 -07001250 mAdapter = createAdapter(this, payloadIntents, initialIntents, rList,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001251 mLaunchedFromUid, mSupportsAlwaysUseOption && !isVoiceInteraction());
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001252 boolean rebuildCompleted = mAdapter.rebuildList();
Adam Powell7d758002015-05-06 17:49:36 -07001253
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001254 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001255 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001256 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001257 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001258 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001259 setContentView(mLayoutId);
Adam Powell7d758002015-05-06 17:49:36 -07001260
Adam Powell50077352015-05-26 18:01:55 -07001261 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001262
1263 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1264 // we're already done, we can check if we should auto-launch immediately.
1265 if (rebuildCompleted) {
1266 if (count == 1 && mAdapter.getOtherProfile() == null) {
1267 // Only one target, so we're a candidate to auto-launch!
1268 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
1269 if (shouldAutoLaunchSingleChoice(target)) {
1270 safelyStartActivity(target);
1271 mPackageMonitor.unregister();
1272 mRegistered = false;
1273 finish();
1274 return true;
1275 }
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001276 }
1277 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001278
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001279
Alan Viverette51efddb2017-04-05 10:00:01 -04001280 mAdapterView = findViewById(R.id.resolver_list);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001281
1282 if (count == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001283 final TextView emptyView = findViewById(R.id.empty);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001284 emptyView.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -07001285 mAdapterView.setVisibility(View.GONE);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001286 } else {
1287 mAdapterView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001288 onPrepareAdapterView(mAdapterView, mAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001289 }
Adam Powell39e94eb2015-09-08 17:01:49 -07001290 return false;
Adam Powell7d758002015-05-06 17:49:36 -07001291 }
1292
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001293 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001294 final boolean useHeader = adapter.hasFilteredItem();
1295 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
1296
1297 adapterView.setAdapter(mAdapter);
1298
1299 final ItemClickListener listener = new ItemClickListener();
1300 adapterView.setOnItemClickListener(listener);
1301 adapterView.setOnItemLongClickListener(listener);
1302
shafik69df96322018-12-18 15:41:19 +00001303 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001304 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1305 }
1306
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001307 // In case this method is called again (due to activity recreation), avoid adding a new
1308 // header if one is already present.
1309 if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) {
Matt Pietal23a02f72019-10-10 11:21:28 -04001310 listView.setHeaderDividersEnabled(true);
Adam Powell7d758002015-05-06 17:49:36 -07001311 listView.addHeaderView(LayoutInflater.from(this).inflate(
1312 R.layout.resolver_different_item_header, listView, false));
1313 }
Adam Powell24428412015-04-01 17:19:56 -07001314 }
1315
Matt Pietal26038402019-01-08 07:29:34 -05001316 /**
1317 * Configure the area above the app selection list (title, content preview, etc).
1318 */
1319 public void setHeader() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001320 if (mAdapter.getCount() == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001321 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001322 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001323 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001324 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001325 }
1326
1327 CharSequence title = mTitle != null
1328 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001329 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001330
1331 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001332 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001333 if (titleView != null) {
1334 titleView.setText(title);
1335 }
1336 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001337 }
1338
Alan Viverette51efddb2017-04-05 10:00:01 -04001339 final ImageView iconView = findViewById(R.id.icon);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001340 final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem();
1341 if (iconView != null && iconInfo != null) {
Matt Pietalaf044ae2019-03-29 06:53:53 -04001342 new LoadIconTask(iconInfo, iconView).execute();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001343 }
1344 }
1345
shafik69df96322018-12-18 15:41:19 +00001346 private void resetButtonBar() {
1347 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1348 return;
1349 }
1350 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1351 if (buttonLayout != null) {
1352 buttonLayout.setVisibility(View.VISIBLE);
Matt Pietal800136a2019-05-08 07:46:39 -04001353
Matt Pietal23a02f72019-10-10 11:21:28 -04001354 if (!useLayoutWithDefault()) {
1355 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1356 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
1357 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1358 R.dimen.resolver_button_bar_spacing) + inset);
1359 }
shafik69df96322018-12-18 15:41:19 +00001360 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
shafik69df96322018-12-18 15:41:19 +00001361 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1362
Matt Pietal0d6834a2019-06-27 13:27:52 -04001363 resetAlwaysOrOnceButtonBar();
shafik69df96322018-12-18 15:41:19 +00001364 } else {
1365 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1366 }
1367 }
1368
shafik69df96322018-12-18 15:41:19 +00001369 private void resetAlwaysOrOnceButtonBar() {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001370 if (useLayoutWithDefault()
1371 && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001372 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
1373 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001374 return;
1375 }
1376
1377 // When the items load in, if an item was already selected, enable the buttons
1378 if (mAdapterView != null
1379 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1380 setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true);
1381 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001382 }
1383 }
1384
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001385 private boolean useLayoutWithDefault() {
1386 return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem();
1387 }
1388
Adam Powellc412be62015-06-24 13:54:10 -07001389 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001390 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1391 * called and we are launched in a new task.
1392 */
1393 protected void setRetainInOnStop(boolean retainInOnStop) {
1394 mRetainInOnStop = retainInOnStop;
1395 }
1396
1397 /**
Adam Powellc412be62015-06-24 13:54:10 -07001398 * Check a simple match for the component of two ResolveInfos.
1399 */
1400 static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
1401 return lhs == null ? rhs == null
1402 : lhs.activityInfo == null ? rhs.activityInfo == null
1403 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1404 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1405 }
1406
Adam Powell23882512016-01-29 10:21:00 -08001407 public final class DisplayResolveInfo implements TargetInfo {
Adam Powell24428412015-04-01 17:19:56 -07001408 private final ResolveInfo mResolveInfo;
1409 private final CharSequence mDisplayLabel;
1410 private Drawable mDisplayIcon;
Adam Powell00f4aad2015-09-17 13:38:16 -07001411 private Drawable mBadge;
Adam Powell24428412015-04-01 17:19:56 -07001412 private final CharSequence mExtendedInfo;
1413 private final Intent mResolvedIntent;
Adam Powell2ed547e2015-04-29 18:45:04 -07001414 private final List<Intent> mSourceIntents = new ArrayList<>();
Matt Pietala4b30072019-04-04 13:44:36 -04001415 private boolean mIsSuspended;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416
Adam Powell23882512016-01-29 10:21:00 -08001417 public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel,
Dianne Hackborneb034652009-09-07 00:49:58 -07001418 CharSequence pInfo, Intent pOrigIntent) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001419 mSourceIntents.add(originalIntent);
Adam Powell24428412015-04-01 17:19:56 -07001420 mResolveInfo = pri;
1421 mDisplayLabel = pLabel;
1422 mExtendedInfo = pInfo;
1423
1424 final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent :
Adam Powell2ed547e2015-04-29 18:45:04 -07001425 getReplacementIntent(pri.activityInfo, getTargetIntent()));
Adam Powell24428412015-04-01 17:19:56 -07001426 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1427 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
1428 final ActivityInfo ai = mResolveInfo.activityInfo;
1429 intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));
1430
Matt Pietala4b30072019-04-04 13:44:36 -04001431 mIsSuspended = (ai.applicationInfo.flags & ApplicationInfo.FLAG_SUSPENDED) != 0;
1432
Adam Powell24428412015-04-01 17:19:56 -07001433 mResolvedIntent = intent;
1434 }
1435
Adam Powell2ed547e2015-04-29 18:45:04 -07001436 private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) {
1437 mSourceIntents.addAll(other.getAllSourceIntents());
1438 mResolveInfo = other.mResolveInfo;
1439 mDisplayLabel = other.mDisplayLabel;
1440 mDisplayIcon = other.mDisplayIcon;
1441 mExtendedInfo = other.mExtendedInfo;
1442 mResolvedIntent = new Intent(other.mResolvedIntent);
1443 mResolvedIntent.fillIn(fillInIntent, flags);
1444 }
1445
Adam Powell24428412015-04-01 17:19:56 -07001446 public ResolveInfo getResolveInfo() {
1447 return mResolveInfo;
1448 }
1449
1450 public CharSequence getDisplayLabel() {
1451 return mDisplayLabel;
1452 }
1453
1454 public Drawable getDisplayIcon() {
1455 return mDisplayIcon;
1456 }
1457
Alan Viverettece5d92c2015-07-31 16:46:56 -04001458 @Override
Adam Powell2ed547e2015-04-29 18:45:04 -07001459 public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) {
1460 return new DisplayResolveInfo(this, fillInIntent, flags);
1461 }
1462
1463 @Override
1464 public List<Intent> getAllSourceIntents() {
1465 return mSourceIntents;
1466 }
1467
1468 public void addAlternateSourceIntent(Intent alt) {
1469 mSourceIntents.add(alt);
1470 }
1471
Adam Powell24428412015-04-01 17:19:56 -07001472 public void setDisplayIcon(Drawable icon) {
1473 mDisplayIcon = icon;
1474 }
1475
1476 public boolean hasDisplayIcon() {
1477 return mDisplayIcon != null;
1478 }
1479
1480 public CharSequence getExtendedInfo() {
1481 return mExtendedInfo;
1482 }
1483
1484 public Intent getResolvedIntent() {
1485 return mResolvedIntent;
1486 }
1487
1488 @Override
1489 public ComponentName getResolvedComponentName() {
1490 return new ComponentName(mResolveInfo.activityInfo.packageName,
1491 mResolveInfo.activityInfo.name);
1492 }
1493
1494 @Override
1495 public boolean start(Activity activity, Bundle options) {
1496 activity.startActivity(mResolvedIntent, options);
1497 return true;
1498 }
1499
1500 @Override
Alison Cichowlas3e340502018-08-07 17:15:01 -04001501 public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) {
Alison Cichowlas4691ed42018-11-13 15:59:55 -05001502 if (mEnableChooserDelegate) {
1503 return activity.startAsCallerImpl(mResolvedIntent, options, false, userId);
1504 } else {
1505 activity.startActivityAsCaller(mResolvedIntent, options, null, false, userId);
1506 return true;
1507 }
Adam Powell24428412015-04-01 17:19:56 -07001508 }
1509
1510 @Override
1511 public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
1512 activity.startActivityAsUser(mResolvedIntent, options, user);
1513 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001515
Matt Pietala4b30072019-04-04 13:44:36 -04001516 public boolean isSuspended() {
1517 return mIsSuspended;
1518 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001519 }
1520
1521 List<DisplayResolveInfo> getDisplayList() {
1522 return mAdapter.mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524
Adam Powell24428412015-04-01 17:19:56 -07001525 /**
1526 * A single target as represented in the chooser.
1527 */
1528 public interface TargetInfo {
1529 /**
1530 * Get the resolved intent that represents this target. Note that this may not be the
1531 * intent that will be launched by calling one of the <code>start</code> methods provided;
1532 * this is the intent that will be credited with the launch.
1533 *
1534 * @return the resolved intent for this target
1535 */
Adam Powell23882512016-01-29 10:21:00 -08001536 Intent getResolvedIntent();
Adam Powell24428412015-04-01 17:19:56 -07001537
1538 /**
1539 * Get the resolved component name that represents this target. Note that this may not
1540 * be the component that will be directly launched by calling one of the <code>start</code>
1541 * methods provided; this is the component that will be credited with the launch.
1542 *
1543 * @return the resolved ComponentName for this target
1544 */
Adam Powell23882512016-01-29 10:21:00 -08001545 ComponentName getResolvedComponentName();
Adam Powell24428412015-04-01 17:19:56 -07001546
1547 /**
1548 * Start the activity referenced by this target.
1549 *
1550 * @param activity calling Activity performing the launch
1551 * @param options ActivityOptions bundle
1552 * @return true if the start completed successfully
1553 */
Adam Powell23882512016-01-29 10:21:00 -08001554 boolean start(Activity activity, Bundle options);
Adam Powell24428412015-04-01 17:19:56 -07001555
1556 /**
1557 * Start the activity referenced by this target as if the ResolverActivity's caller
1558 * was performing the start operation.
1559 *
1560 * @param activity calling Activity (actually) performing the launch
1561 * @param options ActivityOptions bundle
1562 * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller
1563 * @return true if the start completed successfully
1564 */
Alison Cichowlas3e340502018-08-07 17:15:01 -04001565 boolean startAsCaller(ResolverActivity activity, Bundle options, int userId);
Adam Powell24428412015-04-01 17:19:56 -07001566
1567 /**
1568 * Start the activity referenced by this target as a given user.
1569 *
1570 * @param activity calling activity performing the launch
1571 * @param options ActivityOptions bundle
1572 * @param user handle for the user to start the activity as
1573 * @return true if the start completed successfully
1574 */
Adam Powell23882512016-01-29 10:21:00 -08001575 boolean startAsUser(Activity activity, Bundle options, UserHandle user);
Adam Powell24428412015-04-01 17:19:56 -07001576
1577 /**
1578 * Return the ResolveInfo about how and why this target matched the original query
1579 * for available targets.
1580 *
1581 * @return ResolveInfo representing this target's match
1582 */
Adam Powell23882512016-01-29 10:21:00 -08001583 ResolveInfo getResolveInfo();
Adam Powell24428412015-04-01 17:19:56 -07001584
1585 /**
1586 * Return the human-readable text label for this target.
1587 *
1588 * @return user-visible target label
1589 */
Adam Powell23882512016-01-29 10:21:00 -08001590 CharSequence getDisplayLabel();
Adam Powell24428412015-04-01 17:19:56 -07001591
1592 /**
1593 * Return any extended info for this target. This may be used to disambiguate
1594 * otherwise identical targets.
1595 *
1596 * @return human-readable disambig string or null if none present
1597 */
Adam Powell23882512016-01-29 10:21:00 -08001598 CharSequence getExtendedInfo();
Adam Powell24428412015-04-01 17:19:56 -07001599
1600 /**
Mike Digman9c4ae502019-03-19 17:02:25 -07001601 * @return The drawable that should be used to represent this target including badge
Adam Powell24428412015-04-01 17:19:56 -07001602 */
Adam Powell23882512016-01-29 10:21:00 -08001603 Drawable getDisplayIcon();
Adam Powell2ed547e2015-04-29 18:45:04 -07001604
1605 /**
1606 * Clone this target with the given fill-in information.
1607 */
Adam Powell23882512016-01-29 10:21:00 -08001608 TargetInfo cloneFilledIn(Intent fillInIntent, int flags);
Adam Powell2ed547e2015-04-29 18:45:04 -07001609
1610 /**
1611 * @return the list of supported source intents deduped against this single target
1612 */
Adam Powell23882512016-01-29 10:21:00 -08001613 List<Intent> getAllSourceIntents();
Matt Pietala4b30072019-04-04 13:44:36 -04001614
1615 /**
1616 * @return true if this target can be selected by the user
1617 */
1618 boolean isSuspended();
Adam Powell24428412015-04-01 17:19:56 -07001619 }
1620
Adam Powell23882512016-01-29 10:21:00 -08001621 public class ResolveListAdapter extends BaseAdapter {
Adam Powell7d758002015-05-06 17:49:36 -07001622 private final List<Intent> mIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001623 private final Intent[] mInitialIntents;
1624 private final List<ResolveInfo> mBaseResolveList;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001625 protected ResolveInfo mLastChosen;
Adam Powell88831a22014-11-20 18:17:00 -08001626 private DisplayResolveInfo mOtherProfile;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001627 private ResolverListController mResolverListController;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001628 private int mPlaceholderCount;
Matt Pietal0d6834a2019-06-27 13:27:52 -04001629 private boolean mAllTargetsAreBrowsers = false;
Adam Powell24428412015-04-01 17:19:56 -07001630
1631 protected final LayoutInflater mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001633 // This one is the list that the Adapter will actually present.
Adam Powell2ed547e2015-04-29 18:45:04 -07001634 List<DisplayResolveInfo> mDisplayList;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001635 List<ResolvedComponentInfo> mUnfilteredResolveList;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001636
Adam Powell278902c2014-07-12 18:33:22 -07001637 private int mLastChosenPosition = -1;
1638 private boolean mFilterLastUsed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639
Adam Powell7d758002015-05-06 17:49:36 -07001640 public ResolveListAdapter(Context context, List<Intent> payloadIntents,
1641 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001642 boolean filterLastUsed,
1643 ResolverListController resolverListController) {
Adam Powell7d758002015-05-06 17:49:36 -07001644 mIntents = payloadIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001645 mInitialIntents = initialIntents;
1646 mBaseResolveList = rList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001647 mLaunchedFromUid = launchedFromUid;
Adam Powelle9414d92014-07-05 17:44:18 -07001648 mInflater = LayoutInflater.from(context);
Adam Powell2ed547e2015-04-29 18:45:04 -07001649 mDisplayList = new ArrayList<>();
Adam Powell278902c2014-07-12 18:33:22 -07001650 mFilterLastUsed = filterLastUsed;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001651 mResolverListController = resolverListController;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001654 public void handlePackagesChanged() {
1655 rebuildList();
Esteban Talavera6de72662014-12-11 17:54:07 +00001656 if (getCount() == 0) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001657 // We no longer have any items... just finish the activity.
1658 finish();
Adam Powellc5878612012-05-04 18:42:38 -07001659 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001660 }
1661
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001662 public void setPlaceholderCount(int count) {
1663 mPlaceholderCount = count;
1664 }
1665
Sumir Katariadb688af2017-05-10 17:33:47 -07001666 public int getPlaceholderCount() { return mPlaceholderCount; }
1667
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001668 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001669 public DisplayResolveInfo getFilteredItem() {
1670 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1671 // Not using getItem since it offsets to dodge this position for the list
Adam Powell2ed547e2015-04-29 18:45:04 -07001672 return mDisplayList.get(mLastChosenPosition);
Adam Powell278902c2014-07-12 18:33:22 -07001673 }
1674 return null;
1675 }
1676
Adam Powell88831a22014-11-20 18:17:00 -08001677 public DisplayResolveInfo getOtherProfile() {
1678 return mOtherProfile;
1679 }
1680
Adam Powell278902c2014-07-12 18:33:22 -07001681 public int getFilteredPosition() {
1682 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1683 return mLastChosenPosition;
1684 }
1685 return AbsListView.INVALID_POSITION;
1686 }
1687
1688 public boolean hasFilteredItem() {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001689 return mFilterLastUsed && mLastChosen != null;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001690 }
1691
Adam Powella182e452015-07-06 16:57:56 -07001692 public float getScore(DisplayResolveInfo target) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001693 return mResolverListController.getScore(target);
Adam Powella182e452015-07-06 16:57:56 -07001694 }
1695
Kang Li0cef910d2017-01-05 09:14:36 -08001696 public void updateModel(ComponentName componentName) {
1697 mResolverListController.updateModel(componentName);
1698 }
1699
Kang Li9fa2a2c2017-01-06 13:33:24 -08001700 public void updateChooserCounts(String packageName, int userId, String action) {
1701 mResolverListController.updateChooserCounts(packageName, userId, action);
1702 }
1703
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001704 /**
Matt Pietal0d6834a2019-06-27 13:27:52 -04001705 * @return true if all items in the display list are defined as browsers by
1706 * ResolveInfo.handleAllWebDataURI
1707 */
1708 public boolean areAllTargetsBrowsers() {
1709 return mAllTargetsAreBrowsers;
1710 }
1711
1712 /**
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001713 * Rebuild the list of resolvers. In some cases some parts will need some asynchronous work
1714 * to complete.
1715 *
1716 * @return Whether or not the list building is completed.
1717 */
1718 protected boolean rebuildList() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001719 List<ResolvedComponentInfo> currentResolveList = null;
Sudheer Shanka7e64e102015-01-23 10:37:45 +00001720 // Clear the value of mOtherProfile from previous call.
1721 mOtherProfile = null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001722 mLastChosen = null;
1723 mLastChosenPosition = -1;
Matt Pietal0d6834a2019-06-27 13:27:52 -04001724 mAllTargetsAreBrowsers = false;
Adam Powell2ed547e2015-04-29 18:45:04 -07001725 mDisplayList.clear();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001726 if (mBaseResolveList != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001727 currentResolveList = mUnfilteredResolveList = new ArrayList<>();
1728 mResolverListController.addResolveListDedupe(currentResolveList,
1729 getTargetIntent(),
1730 mBaseResolveList);
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001731 } else {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001732 currentResolveList = mUnfilteredResolveList =
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001733 mResolverListController.getResolversForIntent(shouldGetResolvedFilter(),
1734 shouldGetActivityMetadata(),
1735 mIntents);
1736 if (currentResolveList == null) {
1737 processSortedList(currentResolveList);
1738 return true;
Adam Powell2ed547e2015-04-29 18:45:04 -07001739 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001740 List<ResolvedComponentInfo> originalList =
1741 mResolverListController.filterIneligibleActivities(currentResolveList,
1742 true);
1743 if (originalList != null) {
1744 mUnfilteredResolveList = originalList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001745 }
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001746 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001747
1748 // So far we only support a single other profile at a time.
1749 // The first one we see gets special treatment.
1750 for (ResolvedComponentInfo info : currentResolveList) {
1751 if (info.getResolveInfoAt(0).targetUserId != UserHandle.USER_CURRENT) {
1752 mOtherProfile = new DisplayResolveInfo(info.getIntentAt(0),
1753 info.getResolveInfoAt(0),
1754 info.getResolveInfoAt(0).loadLabel(mPm),
1755 info.getResolveInfoAt(0).loadLabel(mPm),
1756 getReplacementIntent(info.getResolveInfoAt(0).activityInfo,
1757 info.getIntentAt(0)));
1758 currentResolveList.remove(info);
1759 break;
1760 }
1761 }
1762
1763 if (mOtherProfile == null) {
1764 try {
1765 mLastChosen = mResolverListController.getLastChosen();
1766 } catch (RemoteException re) {
1767 Log.d(TAG, "Error calling getLastChosenActivity\n" + re);
1768 }
1769 }
1770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 int N;
You Kim43a5070e2012-11-21 23:23:45 +09001772 if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001773 // We only care about fixing the unfilteredList if the current resolve list and
1774 // current resolve list are currently the same.
1775 List<ResolvedComponentInfo> originalList =
1776 mResolverListController.filterLowPriority(currentResolveList,
1777 mUnfilteredResolveList == currentResolveList);
1778 if (originalList != null) {
1779 mUnfilteredResolveList = originalList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001781
Hakan Seyalioglu873cbfd2017-02-21 19:23:43 -08001782 if (currentResolveList.size() > 1) {
Sumir Katariadb688af2017-05-10 17:33:47 -07001783 int placeholderCount = currentResolveList.size();
1784 if (useLayoutWithDefault()) {
1785 --placeholderCount;
1786 }
1787 setPlaceholderCount(placeholderCount);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001788 AsyncTask<List<ResolvedComponentInfo>,
1789 Void,
1790 List<ResolvedComponentInfo>> sortingTask =
1791 new AsyncTask<List<ResolvedComponentInfo>,
1792 Void,
1793 List<ResolvedComponentInfo>>() {
1794 @Override
1795 protected List<ResolvedComponentInfo> doInBackground(
1796 List<ResolvedComponentInfo>... params) {
1797 mResolverListController.sort(params[0]);
1798 return params[0];
1799 }
1800
1801 @Override
1802 protected void onPostExecute(List<ResolvedComponentInfo> sortedComponents) {
1803 processSortedList(sortedComponents);
Matt Pietal74c6ed02019-04-18 13:38:46 -04001804 bindProfileView();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001805 notifyDataSetChanged();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001806 }
1807 };
1808 sortingTask.execute(currentResolveList);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001809 postListReadyRunnable();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001810 return false;
1811 } else {
1812 processSortedList(currentResolveList);
1813 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001815 } else {
1816 processSortedList(currentResolveList);
1817 return true;
1818 }
1819 }
1820
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001821
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001822 private void processSortedList(List<ResolvedComponentInfo> sortedComponents) {
1823 int N;
1824 if (sortedComponents != null && (N = sortedComponents.size()) != 0) {
Matt Pietal0d6834a2019-06-27 13:27:52 -04001825 mAllTargetsAreBrowsers = mUseLayoutForBrowsables;
1826
Dianne Hackborneb034652009-09-07 00:49:58 -07001827 // First put the initial items at the top.
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001828 if (mInitialIntents != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001829 for (int i = 0; i < mInitialIntents.length; i++) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001830 Intent ii = mInitialIntents[i];
Dianne Hackborneb034652009-09-07 00:49:58 -07001831 if (ii == null) {
1832 continue;
1833 }
1834 ActivityInfo ai = ii.resolveActivityInfo(
1835 getPackageManager(), 0);
1836 if (ai == null) {
Adam Powell09a65602014-07-20 16:23:14 -07001837 Log.w(TAG, "No activity found for " + ii);
Dianne Hackborneb034652009-09-07 00:49:58 -07001838 continue;
1839 }
1840 ResolveInfo ri = new ResolveInfo();
1841 ri.activityInfo = ai;
Nicolas Prevot1a815922014-10-10 16:22:38 +01001842 UserManager userManager =
1843 (UserManager) getSystemService(Context.USER_SERVICE);
Dianne Hackborneb034652009-09-07 00:49:58 -07001844 if (ii instanceof LabeledIntent) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001845 LabeledIntent li = (LabeledIntent) ii;
Dianne Hackborneb034652009-09-07 00:49:58 -07001846 ri.resolvePackageName = li.getSourcePackage();
1847 ri.labelRes = li.getLabelResource();
1848 ri.nonLocalizedLabel = li.getNonLocalizedLabel();
1849 ri.icon = li.getIconResource();
Sudheer Shanka9ded7602015-05-19 21:17:25 +01001850 ri.iconResourceId = ri.icon;
1851 }
1852 if (userManager.isManagedProfile()) {
1853 ri.noResourceId = true;
1854 ri.icon = 0;
Dianne Hackborneb034652009-09-07 00:49:58 -07001855 }
Matt Pietal0d6834a2019-06-27 13:27:52 -04001856 mAllTargetsAreBrowsers &= ri.handleAllWebDataURI;
1857
Adam Powell2ed547e2015-04-29 18:45:04 -07001858 addResolveInfo(new DisplayResolveInfo(ii, ri,
Dianne Hackborneb034652009-09-07 00:49:58 -07001859 ri.loadLabel(getPackageManager()), null, ii));
1860 }
1861 }
You Kim43a5070e2012-11-21 23:23:45 +09001862
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001863
Mike Digmanba232682019-03-27 14:55:26 -07001864 for (ResolvedComponentInfo rci : sortedComponents) {
1865 final ResolveInfo ri = rci.getResolveInfoAt(0);
1866 if (ri != null) {
Matt Pietal0d6834a2019-06-27 13:27:52 -04001867 mAllTargetsAreBrowsers &= ri.handleAllWebDataURI;
1868
Mike Digmanba232682019-03-27 14:55:26 -07001869 ResolveInfoPresentationGetter pg = makePresentationGetter(ri);
1870 addResolveInfoWithAlternates(rci, pg.getSubLabel(), pg.getLabel());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001874
Matt Pietal553d8a42019-09-10 09:33:11 -04001875 sendVoiceChoicesIfNeeded();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001876 postListReadyRunnable();
1877 }
1878
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001879
1880
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001881 /**
1882 * Some necessary methods for creating the list are initiated in onCreate and will also
1883 * determine the layout known. We therefore can't update the UI inline and post to the
1884 * handler thread to update after the current task is finished.
1885 */
1886 private void postListReadyRunnable() {
1887 if (mPostListReadyRunnable == null) {
1888 mPostListReadyRunnable = new Runnable() {
1889 @Override
1890 public void run() {
Matt Pietal26038402019-01-08 07:29:34 -05001891 setHeader();
shafik69df96322018-12-18 15:41:19 +00001892 resetButtonBar();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001893 onListRebuilt();
1894 mPostListReadyRunnable = null;
1895 }
1896 };
1897 getMainThreadHandler().post(mPostListReadyRunnable);
1898 }
Adam Powell24428412015-04-01 17:19:56 -07001899 }
1900
1901 public void onListRebuilt() {
Xiaohui Chen393c8012017-02-14 14:55:07 -08001902 int count = getUnfilteredCount();
1903 if (count == 1 && getOtherProfile() == null) {
1904 // Only one target, so we're a candidate to auto-launch!
1905 final TargetInfo target = targetInfoForPosition(0, false);
1906 if (shouldAutoLaunchSingleChoice(target)) {
1907 safelyStartActivity(target);
1908 finish();
1909 }
1910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
1912
Adam Powellc6d5e3a2015-04-23 12:22:20 -07001913 public boolean shouldGetResolvedFilter() {
1914 return mFilterLastUsed;
1915 }
1916
Adam Powell2ed547e2015-04-29 18:45:04 -07001917 private void addResolveInfoWithAlternates(ResolvedComponentInfo rci,
1918 CharSequence extraInfo, CharSequence roLabel) {
1919 final int count = rci.getCount();
1920 final Intent intent = rci.getIntentAt(0);
1921 final ResolveInfo add = rci.getResolveInfoAt(0);
1922 final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent);
1923 final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel,
1924 extraInfo, replaceIntent);
1925 addResolveInfo(dri);
1926 if (replaceIntent == intent) {
1927 // Only add alternates if we didn't get a specific replacement from
1928 // the caller. If we have one it trumps potential alternates.
1929 for (int i = 1, N = count; i < N; i++) {
1930 final Intent altIntent = rci.getIntentAt(i);
1931 dri.addAlternateSourceIntent(altIntent);
1932 }
1933 }
1934 updateLastChosenPosition(add);
1935 }
1936
Esteban Talavera6de72662014-12-11 17:54:07 +00001937 private void updateLastChosenPosition(ResolveInfo info) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001938 // If another profile is present, ignore the last chosen entry.
1939 if (mOtherProfile != null) {
1940 mLastChosenPosition = -1;
1941 return;
1942 }
Esteban Talavera6de72662014-12-11 17:54:07 +00001943 if (mLastChosen != null
1944 && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName)
1945 && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001946 mLastChosenPosition = mDisplayList.size() - 1;
Esteban Talavera6de72662014-12-11 17:54:07 +00001947 }
1948 }
1949
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001950 // We assume that at this point we've already filtered out the only intent for a different
1951 // targetUserId which we're going to use.
Adam Powell88831a22014-11-20 18:17:00 -08001952 private void addResolveInfo(DisplayResolveInfo dri) {
Kang Li6afa4f22017-06-23 12:54:38 -07001953 if (dri != null && dri.mResolveInfo != null
1954 && dri.mResolveInfo.targetUserId == UserHandle.USER_CURRENT) {
1955 // Checks if this info is already listed in display.
1956 for (DisplayResolveInfo existingInfo : mDisplayList) {
1957 if (resolveInfoMatch(dri.mResolveInfo, existingInfo.mResolveInfo)) {
1958 return;
1959 }
1960 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001961 mDisplayList.add(dri);
Adam Powell88831a22014-11-20 18:17:00 -08001962 }
1963 }
1964
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001965 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001966 public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001967 TargetInfo target = targetInfoForPosition(position, filtered);
1968 if (target != null) {
1969 return target.getResolveInfo();
1970 }
1971 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
1973
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001974 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001975 public TargetInfo targetInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001976 if (filtered) {
1977 return getItem(position);
1978 }
1979 if (mDisplayList.size() > position) {
1980 return mDisplayList.get(position);
1981 }
1982 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
1984
1985 public int getCount() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001986 int totalSize = mDisplayList == null || mDisplayList.isEmpty() ? mPlaceholderCount :
1987 mDisplayList.size();
Adam Powell278902c2014-07-12 18:33:22 -07001988 if (mFilterLastUsed && mLastChosenPosition >= 0) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001989 totalSize--;
Adam Powell278902c2014-07-12 18:33:22 -07001990 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001991 return totalSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993
Adam Powell50077352015-05-26 18:01:55 -07001994 public int getUnfilteredCount() {
1995 return mDisplayList.size();
1996 }
1997
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001998 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001999 public TargetInfo getItem(int position) {
Adam Powell278902c2014-07-12 18:33:22 -07002000 if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) {
2001 position++;
2002 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002003 if (mDisplayList.size() > position) {
2004 return mDisplayList.get(position);
2005 } else {
2006 return null;
2007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 }
2009
2010 public long getItemId(int position) {
2011 return position;
2012 }
2013
Adam Powell23882512016-01-29 10:21:00 -08002014 public int getDisplayResolveInfoCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07002015 return mDisplayList.size();
Adam Powell24428412015-04-01 17:19:56 -07002016 }
2017
Adam Powell23882512016-01-29 10:21:00 -08002018 public DisplayResolveInfo getDisplayResolveInfo(int index) {
Adam Powell2ed547e2015-04-29 18:45:04 -07002019 // Used to query services. We only query services for primary targets, not alternates.
2020 return mDisplayList.get(index);
Adam Powell24428412015-04-01 17:19:56 -07002021 }
2022
2023 public final View getView(int position, View convertView, ViewGroup parent) {
Adam Powellfd1e93d2014-09-07 16:52:22 -07002024 View view = convertView;
2025 if (view == null) {
Adam Powell24428412015-04-01 17:19:56 -07002026 view = createView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 }
Adam Powell7d758002015-05-06 17:49:36 -07002028 onBindView(view, getItem(position));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 return view;
2030 }
2031
Adam Powell7d758002015-05-06 17:49:36 -07002032 public final View createView(ViewGroup parent) {
2033 final View view = onCreateView(parent);
2034 final ViewHolder holder = new ViewHolder(view);
2035 view.setTag(holder);
2036 return view;
2037 }
2038
2039 public View onCreateView(ViewGroup parent) {
Adam Powell24428412015-04-01 17:19:56 -07002040 return mInflater.inflate(
2041 com.android.internal.R.layout.resolve_list_item, parent, false);
2042 }
2043
Adam Powell7d758002015-05-06 17:49:36 -07002044 public final void bindView(int position, View view) {
2045 onBindView(view, getItem(position));
2046 }
2047
Mike Digmanba232682019-03-27 14:55:26 -07002048 protected void onBindView(View view, TargetInfo info) {
Adam Powell0256c6f2013-05-29 16:42:33 -07002049 final ViewHolder holder = (ViewHolder) view.getTag();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002050 if (info == null) {
2051 holder.icon.setImageDrawable(
2052 getDrawable(R.drawable.resolver_icon_placeholder));
2053 return;
2054 }
Mike Digmanba232682019-03-27 14:55:26 -07002055
Adam Powell63b31692015-09-28 10:45:00 -07002056 final CharSequence label = info.getDisplayLabel();
2057 if (!TextUtils.equals(holder.text.getText(), label)) {
2058 holder.text.setText(info.getDisplayLabel());
2059 }
Mike Digmanba232682019-03-27 14:55:26 -07002060
2061 // Always show a subLabel for visual consistency across list items. Show an empty
2062 // subLabel if the subLabel is the same as the label
2063 CharSequence subLabel = info.getExtendedInfo();
2064 if (TextUtils.equals(label, subLabel)) subLabel = null;
2065
Matt Pietal23a02f72019-10-10 11:21:28 -04002066 if (!TextUtils.equals(holder.text2.getText(), subLabel)
2067 && !TextUtils.isEmpty(subLabel)) {
2068 holder.text2.setVisibility(View.VISIBLE);
Mike Digmanba232682019-03-27 14:55:26 -07002069 holder.text2.setText(subLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 }
Mike Digmanba232682019-03-27 14:55:26 -07002071
Matt Pietala4b30072019-04-04 13:44:36 -04002072 if (info.isSuspended()) {
2073 holder.icon.setColorFilter(mSuspendedMatrixColorFilter);
2074 } else {
2075 holder.icon.setColorFilter(null);
2076 }
2077
Adam Powell24428412015-04-01 17:19:56 -07002078 if (info instanceof DisplayResolveInfo
2079 && !((DisplayResolveInfo) info).hasDisplayIcon()) {
Matt Pietalaf044ae2019-03-29 06:53:53 -04002080 new LoadIconTask((DisplayResolveInfo) info, holder.icon).execute();
2081 } else {
2082 holder.icon.setImageDrawable(info.getDisplayIcon());
Dianne Hackborneb034652009-09-07 00:49:58 -07002083 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002084 }
2085 }
2086
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04002087
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08002088 @VisibleForTesting
2089 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07002090 public final ComponentName name;
2091 private final List<Intent> mIntents = new ArrayList<>();
2092 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
2093
2094 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
2095 this.name = name;
2096 add(intent, info);
2097 }
2098
2099 public void add(Intent intent, ResolveInfo info) {
2100 mIntents.add(intent);
2101 mResolveInfos.add(info);
2102 }
2103
2104 public int getCount() {
2105 return mIntents.size();
2106 }
2107
2108 public Intent getIntentAt(int index) {
2109 return index >= 0 ? mIntents.get(index) : null;
2110 }
2111
2112 public ResolveInfo getResolveInfoAt(int index) {
2113 return index >= 0 ? mResolveInfos.get(index) : null;
2114 }
2115
2116 public int findIntent(Intent intent) {
2117 for (int i = 0, N = mIntents.size(); i < N; i++) {
2118 if (intent.equals(mIntents.get(i))) {
2119 return i;
2120 }
2121 }
2122 return -1;
2123 }
2124
2125 public int findResolveInfo(ResolveInfo info) {
2126 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
2127 if (info.equals(mResolveInfos.get(i))) {
2128 return i;
2129 }
2130 }
2131 return -1;
2132 }
2133 }
2134
Adam Powell0256c6f2013-05-29 16:42:33 -07002135 static class ViewHolder {
Mike Digman4b83c212019-05-03 10:17:35 -07002136 public View itemView;
2137 public Drawable defaultItemViewBackground;
2138
Adam Powell0256c6f2013-05-29 16:42:33 -07002139 public TextView text;
2140 public TextView text2;
2141 public ImageView icon;
2142
2143 public ViewHolder(View view) {
Mike Digman4b83c212019-05-03 10:17:35 -07002144 itemView = view;
2145 defaultItemViewBackground = view.getBackground();
Adam Powell0256c6f2013-05-29 16:42:33 -07002146 text = (TextView) view.findViewById(com.android.internal.R.id.text1);
2147 text2 = (TextView) view.findViewById(com.android.internal.R.id.text2);
2148 icon = (ImageView) view.findViewById(R.id.icon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 }
2150 }
2151
Adam Powell7d758002015-05-06 17:49:36 -07002152 class ItemClickListener implements AdapterView.OnItemClickListener,
2153 AdapterView.OnItemLongClickListener {
2154 @Override
2155 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
2156 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2157 if (listView != null) {
2158 position -= listView.getHeaderViewsCount();
2159 }
2160 if (position < 0) {
2161 // Header views don't count.
2162 return;
2163 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002164 // If we're still loading, we can't yet enable the buttons.
2165 if (mAdapter.resolveInfoForPosition(position, true) == null) {
2166 return;
2167 }
2168
Adam Powell7d758002015-05-06 17:49:36 -07002169 final int checkedPos = mAdapterView.getCheckedItemPosition();
2170 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08002171 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07002172 && (!hasValidSelection || mLastSelected != checkedPos)
Matt Pietal0d6834a2019-06-27 13:27:52 -04002173 && mAlwaysButton != null) {
2174 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Adam Powell7d758002015-05-06 17:49:36 -07002175 mOnceButton.setEnabled(hasValidSelection);
2176 if (hasValidSelection) {
2177 mAdapterView.smoothScrollToPosition(checkedPos);
2178 }
2179 mLastSelected = checkedPos;
2180 } else {
2181 startSelected(position, false, true);
2182 }
2183 }
Adam Powell2d809622012-03-22 15:24:43 -07002184
2185 @Override
2186 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07002187 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2188 if (listView != null) {
2189 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07002190 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07002191 if (position < 0) {
2192 // Header views don't count.
2193 return false;
2194 }
Adam Powell278902c2014-07-12 18:33:22 -07002195 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08002196 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07002197 return true;
2198 }
2199
2200 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002201
Matt Pietalaf044ae2019-03-29 06:53:53 -04002202 class LoadIconTask extends AsyncTask<Void, Void, Drawable> {
Adam Powell24428412015-04-01 17:19:56 -07002203 protected final DisplayResolveInfo mDisplayResolveInfo;
2204 private final ResolveInfo mResolveInfo;
Matt Pietalaf044ae2019-03-29 06:53:53 -04002205 private final ImageView mTargetView;
Adam Powell24428412015-04-01 17:19:56 -07002206
Matt Pietalaf044ae2019-03-29 06:53:53 -04002207 LoadIconTask(DisplayResolveInfo dri, ImageView target) {
Adam Powell24428412015-04-01 17:19:56 -07002208 mDisplayResolveInfo = dri;
2209 mResolveInfo = dri.getResolveInfo();
Matt Pietalaf044ae2019-03-29 06:53:53 -04002210 mTargetView = target;
Adam Powell0256c6f2013-05-29 16:42:33 -07002211 }
2212
2213 @Override
Adam Powell24428412015-04-01 17:19:56 -07002214 protected Drawable doInBackground(Void... params) {
2215 return loadIconForResolveInfo(mResolveInfo);
2216 }
2217
2218 @Override
2219 protected void onPostExecute(Drawable d) {
Matt Pietal74c6ed02019-04-18 13:38:46 -04002220 if (mAdapter.getOtherProfile() == mDisplayResolveInfo) {
Adam Powell88831a22014-11-20 18:17:00 -08002221 bindProfileView();
Matt Pietalaf044ae2019-03-29 06:53:53 -04002222 } else {
2223 mDisplayResolveInfo.setDisplayIcon(d);
2224 mTargetView.setImageDrawable(d);
Adam Powell88831a22014-11-20 18:17:00 -08002225 }
Adam Powell278902c2014-07-12 18:33:22 -07002226 }
2227 }
Adam Powell09a65602014-07-20 16:23:14 -07002228
Dianne Hackbornec452d92014-11-11 17:16:56 -08002229 static final boolean isSpecificUriMatch(int match) {
2230 match = match&IntentFilter.MATCH_CATEGORY_MASK;
2231 return match >= IntentFilter.MATCH_CATEGORY_HOST
2232 && match <= IntentFilter.MATCH_CATEGORY_PATH;
2233 }
2234
Adam Powell4c470d62015-06-19 17:46:17 -07002235 static class PickTargetOptionRequest extends PickOptionRequest {
2236 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
2237 @Nullable Bundle extras) {
2238 super(prompt, options, extras);
2239 }
2240
2241 @Override
2242 public void onCancel() {
2243 super.onCancel();
2244 final ResolverActivity ra = (ResolverActivity) getActivity();
2245 if (ra != null) {
2246 ra.mPickOptionRequest = null;
2247 ra.finish();
2248 }
2249 }
2250
2251 @Override
2252 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
2253 super.onPickOptionResult(finished, selections, result);
2254 if (selections.length != 1) {
2255 // TODO In a better world we would filter the UI presented here and let the
2256 // user refine. Maybe later.
2257 return;
2258 }
2259
2260 final ResolverActivity ra = (ResolverActivity) getActivity();
2261 if (ra != null) {
2262 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
2263 if (ra.onTargetSelected(ti, false)) {
2264 ra.mPickOptionRequest = null;
2265 ra.finish();
2266 }
2267 }
2268 }
2269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270}