blob: 3c028d71b40db93cc175aa2040d9ca4ddad36715 [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;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070026import android.app.ActivityTaskManager;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070027import android.app.ActivityThread;
Adam Powell4c470d62015-06-19 17:46:17 -070028import android.app.VoiceInteractor.PickOptionRequest;
29import android.app.VoiceInteractor.PickOptionRequest.Option;
30import android.app.VoiceInteractor.Prompt;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.ComponentName;
32import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.Intent;
34import android.content.IntentFilter;
Paul McLean3a30e552019-12-07 14:47:29 -070035import android.content.PermissionChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010037import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070039import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010041import android.content.pm.UserInfo;
Jiongxuan Gao3365b162016-11-24 20:20:27 +090042import android.content.res.Configuration;
Adam Powellc5878612012-05-04 18:42:38 -070043import android.content.res.Resources;
Matt Pietal800136a2019-05-08 07:46:39 -040044import android.graphics.Insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.net.Uri;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010046import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040048import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.PatternMatcher;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080050import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070051import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070052import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010053import android.os.UserManager;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080054import android.provider.MediaStore;
55import android.provider.Settings;
56import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.util.Log;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080058import android.util.Slog;
Adam Powell2d809622012-03-22 15:24:43 -070059import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.View;
61import android.view.ViewGroup;
Matt Pietal800136a2019-05-08 07:46:39 -040062import android.view.ViewGroup.LayoutParams;
63import android.view.WindowInsets;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080064import android.widget.AbsListView;
Adam Powell2d809622012-03-22 15:24:43 -070065import android.widget.AdapterView;
Adam Powellc5878612012-05-04 18:42:38 -070066import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070068import android.widget.ListView;
Matt Pietal800136a2019-05-08 07:46:39 -040069import android.widget.Space;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010071import android.widget.Toast;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070072
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080073import com.android.internal.R;
74import com.android.internal.annotations.VisibleForTesting;
arangelovb0802dc2019-10-18 18:03:44 +010075import com.android.internal.app.chooser.DisplayResolveInfo;
76import com.android.internal.app.chooser.TargetInfo;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080077import com.android.internal.content.PackageMonitor;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010078import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010079import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070080import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010083import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.Iterator;
85import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070086import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.Set;
88
89/**
90 * This activity is displayed when the system attempts to start an Intent for
91 * which there is more than one matching activity, allowing the user to decide
92 * which to go to. It is not normally used directly by application developers.
93 */
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080094@UiThread
arangelovb0802dc2019-10-18 18:03:44 +010095public class ResolverActivity extends Activity implements
96 ResolverListAdapter.ResolverListCommunicator {
Alison Cichowlas4691ed42018-11-13 15:59:55 -050097
Andrei Onea15884392019-03-22 17:28:11 +000098 @UnsupportedAppUsage
Artur Satayev751e5512019-11-15 19:12:49 +000099 public ResolverActivity() {
100 }
101
102 @UnsupportedAppUsage
arangelovb0802dc2019-10-18 18:03:44 +0100103 protected ResolverListAdapter mAdapter;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700104 private boolean mSafeForwardingMode;
Zhen Zhangbde7b462019-11-11 11:49:33 -0800105 private AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -0700106 private Button mAlwaysButton;
107 private Button mOnceButton;
Matt Pietal74c6ed02019-04-18 13:38:46 -0400108 protected View mProfileView;
Adam Powell24428412015-04-01 17:19:56 -0700109 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100110 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000111 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800112 private int mLayoutId;
arangelovb0802dc2019-10-18 18:03:44 +0100113 @VisibleForTesting
114 protected final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700115 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800116 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800117 private CharSequence mTitle;
118 private int mDefaultTitleResId;
arangelovb0802dc2019-10-18 18:03:44 +0100119
120 @VisibleForTesting
121 protected boolean mUseLayoutForBrowsables;
Adam Powell09a65602014-07-20 16:23:14 -0700122
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800123 // Whether or not this activity supports choosing a default handler for the intent.
arangelovb0802dc2019-10-18 18:03:44 +0100124 @VisibleForTesting
125 protected boolean mSupportsAlwaysUseOption;
Adam Powell63b31692015-09-28 10:45:00 -0700126 protected ResolverDrawerLayout mResolverDrawerLayout;
Andrei Onea15884392019-03-22 17:28:11 +0000127 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800128 protected PackageManager mPm;
129 protected int mLaunchedFromUid;
130
131 private static final String TAG = "ResolverActivity";
132 private static final boolean DEBUG = false;
Adam Powell63b31692015-09-28 10:45:00 -0700133
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700134 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100135
Matt Pietal800136a2019-05-08 07:46:39 -0400136 protected Insets mSystemWindowInsets = null;
137 private Space mFooterSpacer = null;
138
Jorim Jaggif631ef72017-02-24 13:49:47 +0100139 /** See {@link #setRetainInOnStop}. */
140 private boolean mRetainInOnStop;
141
Matt Pietal0d6834a2019-06-27 13:27:52 -0400142 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
143 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
144 private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state";
145
Matt Pietalab73a882019-06-05 07:04:55 -0400146 private final PackageMonitor mPackageMonitor = createPackageMonitor();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800147
Paul McLean07425c82019-10-18 12:00:11 -0600148 // Intent extra for connected audio devices
149 public static final String EXTRA_IS_AUDIO_CAPTURE_DEVICE = "is_audio_capture_device";
150
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700151 /**
152 * Get the string resource to be used as a label for the link to the resolver activity for an
153 * action.
154 *
155 * @param action The action to resolve
156 *
157 * @return The string resource to be used as a label
158 */
159 public static @StringRes int getLabelRes(String action) {
160 return ActionTitle.forAction(action).labelRes;
161 }
162
Adam Powell278902c2014-07-12 18:33:22 -0700163 private enum ActionTitle {
164 VIEW(Intent.ACTION_VIEW,
165 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700166 com.android.internal.R.string.whichViewApplicationNamed,
167 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700168 EDIT(Intent.ACTION_EDIT,
169 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700170 com.android.internal.R.string.whichEditApplicationNamed,
171 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700172 SEND(Intent.ACTION_SEND,
173 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700174 com.android.internal.R.string.whichSendApplicationNamed,
175 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700176 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700177 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700178 com.android.internal.R.string.whichSendToApplicationNamed,
179 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700180 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
181 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700182 com.android.internal.R.string.whichSendApplicationNamed,
183 com.android.internal.R.string.whichSendApplicationLabel),
184 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
185 com.android.internal.R.string.whichImageCaptureApplication,
186 com.android.internal.R.string.whichImageCaptureApplicationNamed,
187 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700188 DEFAULT(null,
189 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700190 com.android.internal.R.string.whichApplicationNamed,
191 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700192 HOME(Intent.ACTION_MAIN,
193 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700194 com.android.internal.R.string.whichHomeApplicationNamed,
195 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700196
shafik0c7c5d52019-02-27 12:13:25 +0000197 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000198 public static final int BROWSABLE_TITLE_RES =
Matt Pietal0d6834a2019-06-27 13:27:52 -0400199 com.android.internal.R.string.whichOpenLinksWith;
200 public static final int BROWSABLE_HOST_TITLE_RES =
201 com.android.internal.R.string.whichOpenHostLinksWith;
202 public static final int BROWSABLE_HOST_APP_TITLE_RES =
203 com.android.internal.R.string.whichOpenHostLinksWithApp;
204 public static final int BROWSABLE_APP_TITLE_RES =
205 com.android.internal.R.string.whichOpenLinksWithApp;
shafik69df96322018-12-18 15:41:19 +0000206
Adam Powell278902c2014-07-12 18:33:22 -0700207 public final String action;
208 public final int titleRes;
209 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700210 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700211
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700212 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700213 this.action = action;
214 this.titleRes = titleRes;
215 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700216 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700217 }
218
219 public static ActionTitle forAction(String action) {
220 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700221 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700222 return title;
223 }
224 }
225 return DEFAULT;
226 }
227 }
228
Matt Pietalab73a882019-06-05 07:04:55 -0400229 protected PackageMonitor createPackageMonitor() {
230 return new PackageMonitor() {
231 @Override
232 public void onSomePackagesChanged() {
233 mAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100234 updateProfileViewButton();
Matt Pietalab73a882019-06-05 07:04:55 -0400235 }
236
237 @Override
238 public boolean onPackageChanged(String packageName, int uid, String[] components) {
239 // We care about all package changes, not just the whole package itself which is
240 // default behavior.
241 return true;
242 }
243 };
244 }
245
Dianne Hackborn905577f2011-09-07 18:31:28 -0700246 private Intent makeMyIntent() {
247 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700248 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700249 // The resolver activity is set to be hidden from recent tasks.
250 // we don't want this attribute to be propagated to the next activity
251 // being launched. Note that if the original Intent also had this
252 // flag set, we are now losing it. That should be a very rare case
253 // and we can live with this.
254 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
255 return intent;
256 }
257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 @Override
259 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700260 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700261 final Intent intent = makeMyIntent();
262 final Set<String> categories = intent.getCategories();
263 if (Intent.ACTION_MAIN.equals(intent.getAction())
264 && categories != null
265 && categories.size() == 1
266 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100267 // Note: this field is not set to true in the compatibility version.
268 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700269 }
270
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700271 setSafeForwardingMode(true);
272
Adam Powella35c77a2014-09-25 16:46:36 -0700273 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
Adam Powell278902c2014-07-12 18:33:22 -0700276 /**
Adam Powell24428412015-04-01 17:19:56 -0700277 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700278 * a default title resource
279 */
Andrei Onea15884392019-03-22 17:28:11 +0000280 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700282 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800283 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
284 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
285 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700286 }
287
288 protected void onCreate(Bundle savedInstanceState, Intent intent,
289 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800290 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700291 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000293
294 // Determine whether we should show that intent is forwarded
295 // from managed profile to owner or other way around.
296 setProfileSwitchMessageId(intent.getContentUserHint());
297
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700298 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700299 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700300 getActivityToken());
301 } catch (RemoteException e) {
302 mLaunchedFromUid = -1;
303 }
Adam Powell7d758002015-05-06 17:49:36 -0700304
305 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
306 // Gulp!
307 finish();
308 return;
309 }
310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700312
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700313 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700314 mRegistered = true;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800315 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800316
Adam Powell7d758002015-05-06 17:49:36 -0700317 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700318 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800319 mTitle = title;
320 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700321
shafik69df96322018-12-18 15:41:19 +0000322 mUseLayoutForBrowsables = getTargetIntent() == null
323 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000324 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000325
Matt Pietal0d6834a2019-06-27 13:27:52 -0400326 mSupportsAlwaysUseOption = supportsAlwaysUseOption;
shafik69df96322018-12-18 15:41:19 +0000327
arangelovb0802dc2019-10-18 18:03:44 +0100328 // The last argument of createAdapter is whether to do special handling
329 // of the last used choice to highlight it in the list. We need to always
330 // turn this off when running under voice interaction, since it results in
331 // a more complicated UI that the current voice interaction flow is not able
332 // to handle.
333 boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction();
334 mAdapter = createAdapter(this, mIntents, initialIntents, rList,
335 filterLastUsed, mUseLayoutForBrowsables);
arangelovb0802dc2019-10-18 18:03:44 +0100336
arangelovbb572332019-11-13 12:50:08 +0000337 if (configureContentView()) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700338 return;
339 }
Adam Powell278902c2014-07-12 18:33:22 -0700340
Alan Viverette51efddb2017-04-05 10:00:01 -0400341 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700342 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700343 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700344 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700345 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700346 finish();
347 }
348 });
Adam Powell4c470d62015-06-19 17:46:17 -0700349 if (isVoiceInteraction()) {
350 rdl.setCollapsed(false);
351 }
Matt Pietal800136a2019-05-08 07:46:39 -0400352
353 rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
354 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
355 rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets);
356
Adam Powell63b31692015-09-28 10:45:00 -0700357 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700358 }
359
Adam Powell88831a22014-11-20 18:17:00 -0800360 mProfileView = findViewById(R.id.profile_button);
361 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400362 mProfileView.setOnClickListener(this::onProfileClick);
arangelovb0802dc2019-10-18 18:03:44 +0100363 updateProfileViewButton();
Adam Powell88831a22014-11-20 18:17:00 -0800364 }
Adam Powell4c470d62015-06-19 17:46:17 -0700365
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100366 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100367 MetricsLogger.action(this, mAdapter.hasFilteredItem()
368 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
369 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
370 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100371 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700372 }
373
Matt Pietal74c6ed02019-04-18 13:38:46 -0400374 protected void onProfileClick(View v) {
375 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
376 if (dri == null) {
377 return;
378 }
379
380 // Do not show the profile switch message anymore.
381 mProfileSwitchMessageId = -1;
382
383 onTargetSelected(dri, false);
384 finish();
385 }
386
Matt Pietal800136a2019-05-08 07:46:39 -0400387 protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
388 mSystemWindowInsets = insets.getSystemWindowInsets();
389
390 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
391 mSystemWindowInsets.right, 0);
392
Matt Pietald5071202019-10-10 11:21:28 -0400393 // Need extra padding so the list can fully scroll up
394 if (useLayoutWithDefault()) {
395 if (mFooterSpacer == null) {
396 mFooterSpacer = new Space(getApplicationContext());
397 } else {
398 ((ListView) mAdapterView).removeFooterView(mFooterSpacer);
399 }
400 mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
401 mSystemWindowInsets.bottom));
402 ((ListView) mAdapterView).addFooterView(mFooterSpacer);
Matt Pietal800136a2019-05-08 07:46:39 -0400403 } else {
Matt Pietald5071202019-10-10 11:21:28 -0400404 View emptyView = findViewById(R.id.empty);
405 if (emptyView != null) {
406 emptyView.setPadding(0, 0, 0, mSystemWindowInsets.bottom
407 + getResources().getDimensionPixelSize(
408 R.dimen.chooser_edge_margin_normal) * 2);
409 }
Matt Pietal800136a2019-05-08 07:46:39 -0400410 }
Matt Pietal800136a2019-05-08 07:46:39 -0400411
Matt Pietal78fdbd42019-06-05 07:08:08 -0400412 resetButtonBar();
413
Matt Pietal800136a2019-05-08 07:46:39 -0400414 return insets.consumeSystemWindowInsets();
415 }
416
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900417 @Override
418 public void onConfigurationChanged(Configuration newConfig) {
419 super.onConfigurationChanged(newConfig);
420 mAdapter.handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400421
422 if (mSystemWindowInsets != null) {
423 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
424 mSystemWindowInsets.right, 0);
425 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900426 }
427
arangelovb0802dc2019-10-18 18:03:44 +0100428 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -0800429 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700430 if (!isVoiceInteraction()) {
431 // Clearly not needed.
432 return;
433 }
434
Adam Powell4c470d62015-06-19 17:46:17 -0700435 final Option[] options = new Option[mAdapter.getCount()];
436 for (int i = 0, N = options.length; i < N; i++) {
Matt Pietal2cf4c6c2019-09-10 09:33:11 -0400437 TargetInfo target = mAdapter.getItem(i);
438 if (target == null) {
439 // If this occurs, a new set of targets is being loaded. Let that complete,
440 // and have the next call to send voice choices proceed instead.
441 return;
442 }
443 options[i] = optionForChooserTarget(target, i);
Adam Powell4c470d62015-06-19 17:46:17 -0700444 }
445
446 mPickOptionRequest = new PickTargetOptionRequest(
447 new Prompt(getTitle()), options, null);
448 getVoiceInteractor().submitRequest(mPickOptionRequest);
449 }
450
451 Option optionForChooserTarget(TargetInfo target, int index) {
452 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800453 }
454
Adam Powell2ed547e2015-04-29 18:45:04 -0700455 protected final void setAdditionalTargets(Intent[] intents) {
456 if (intents != null) {
457 for (Intent intent : intents) {
458 mIntents.add(intent);
459 }
460 }
461 }
462
arangelovb0802dc2019-10-18 18:03:44 +0100463 @Override // SelectableTargetInfoCommunicator ResolverListCommunicator
Adam Powell0ccc0e92015-04-23 17:19:37 -0700464 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700465 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700466 }
467
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800468 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700469 final Uri referrer = getReferrer();
470 if (referrer != null && "android-app".equals(referrer.getScheme())) {
471 return referrer.getHost();
472 }
473 return null;
474 }
475
Adam Powell23882512016-01-29 10:21:00 -0800476 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700477 return R.layout.resolver_list;
478 }
479
arangelovb0802dc2019-10-18 18:03:44 +0100480 @Override // ResolverListCommunicator
481 public void updateProfileViewButton() {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400482 if (mProfileView == null) {
483 return;
484 }
485
Adam Powell88831a22014-11-20 18:17:00 -0800486 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
487 if (dri != null) {
488 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800489 View text = mProfileView.findViewById(R.id.profile_button);
490 if (!(text instanceof TextView)) {
491 text = mProfileView.findViewById(R.id.text1);
492 }
493 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800494 } else {
495 mProfileView.setVisibility(View.GONE);
496 }
Adam Powell278902c2014-07-12 18:33:22 -0700497 }
498
Sander Alewijnsef6545332014-10-31 12:39:02 +0000499 private void setProfileSwitchMessageId(int contentUserHint) {
500 if (contentUserHint != UserHandle.USER_CURRENT &&
501 contentUserHint != UserHandle.myUserId()) {
502 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
503 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
504 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
505 : false;
506 boolean targetIsManaged = userManager.isManagedProfile();
507 if (originIsManaged && !targetIsManaged) {
508 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
509 } else if (!originIsManaged && targetIsManaged) {
510 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
511 }
512 }
513 }
514
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700515 /**
516 * Turn on launch mode that is safe to use when forwarding intents received from
517 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
518 * instead of the normal Activity.startActivity for launching the activity selected
519 * by the user.
520 *
521 * <p>This mode is set to true by default if the activity is initialized through
522 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
523 * methods, it is set to false by default. You must set it before calling one of the
524 * more detailed onCreate methods, so that it will be set correctly in the case where
525 * there is only one intent to resolve and it is thus started immediately.</p>
526 */
527 public void setSafeForwardingMode(boolean safeForwarding) {
528 mSafeForwardingMode = safeForwarding;
529 }
530
shafik69df96322018-12-18 15:41:19 +0000531 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
532 final ActionTitle title = mResolvingHome
533 ? ActionTitle.HOME
534 : ActionTitle.forAction(intent.getAction());
535
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800536 // While there may already be a filtered item, we can only use it in the title if the list
537 // is already sorted and all information relevant to it is already in the list.
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800538 final boolean named = mAdapter.getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700539 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
540 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000541 } else if (isHttpSchemeAndViewAction(intent)) {
542 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000543 // they're giving access for the activity to open URLs from this specific host
Matt Pietal0d6834a2019-06-27 13:27:52 -0400544 String dialogTitle = null;
545 if (named && !mUseLayoutForBrowsables) {
546 dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES,
547 mAdapter.getFilteredItem().getDisplayLabel());
548 } else if (named && mUseLayoutForBrowsables) {
549 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES,
550 intent.getData().getHost(),
551 mAdapter.getFilteredItem().getDisplayLabel());
552 } else if (mAdapter.areAllTargetsBrowsers()) {
Matt Pietald5071202019-10-10 11:21:28 -0400553 dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES);
Matt Pietal0d6834a2019-06-27 13:27:52 -0400554 } else {
555 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES,
556 intent.getData().getHost());
557 }
558 return dialogTitle;
Adam Powell278902c2014-07-12 18:33:22 -0700559 } else {
Adam Powell24428412015-04-01 17:19:56 -0700560 return named
561 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
562 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700563 }
Adam Powellc5878612012-05-04 18:42:38 -0700564 }
565
Adam Powelle9414d92014-07-05 17:44:18 -0700566 void dismiss() {
567 if (!isFinishing()) {
568 finish();
569 }
570 }
571
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800572 @Override
573 protected void onRestart() {
574 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700575 if (!mRegistered) {
576 mPackageMonitor.register(this, getMainLooper(), false);
577 mRegistered = true;
578 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800579 mAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100580 updateProfileViewButton();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800581 }
582
583 @Override
584 protected void onStop() {
585 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700586 if (mRegistered) {
587 mPackageMonitor.unregister();
588 mRegistered = false;
589 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700590 final Intent intent = getIntent();
591 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100592 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700593 // This resolver is in the unusual situation where it has been
594 // launched at the top of a new task. We don't let it be added
595 // to the recent tasks shown to the user, and we need to make sure
596 // that each time we are launched we get the correct launching
597 // uid (not re-using the same resolver from an old launching uid),
598 // so we will now finish ourself since being no longer visible,
599 // the user probably can't get back to us.
600 if (!isChangingConfigurations()) {
601 finish();
602 }
603 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800604 }
605
Adam Powellc5878612012-05-04 18:42:38 -0700606 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700607 protected void onDestroy() {
608 super.onDestroy();
609 if (!isChangingConfigurations() && mPickOptionRequest != null) {
610 mPickOptionRequest.cancel();
611 }
arangelovb0802dc2019-10-18 18:03:44 +0100612 if (mAdapter != null) {
613 mAdapter.onDestroy();
Kang Li38a6da642017-04-05 12:30:55 -0700614 }
Adam Powell4c470d62015-06-19 17:46:17 -0700615 }
616
617 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700618 protected void onRestoreInstanceState(Bundle savedInstanceState) {
619 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000620 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700621 }
622
shafik0c7c5d52019-02-27 12:13:25 +0000623 private boolean isHttpSchemeAndViewAction(Intent intent) {
624 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
625 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
626 && Intent.ACTION_VIEW.equals(intent.getAction());
627 }
628
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100629 private boolean hasManagedProfile() {
630 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
631 if (userManager == null) {
632 return false;
633 }
634
635 try {
636 List<UserInfo> profiles = userManager.getProfiles(getUserId());
637 for (UserInfo userInfo : profiles) {
638 if (userInfo != null && userInfo.isManagedProfile()) {
639 return true;
640 }
641 }
642 } catch (SecurityException e) {
643 return false;
644 }
645 return false;
646 }
647
648 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
649 try {
650 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
651 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700652 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100653 } catch (NameNotFoundException e) {
654 return false;
655 }
656 }
657
Adam Powell278902c2014-07-12 18:33:22 -0700658 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
659 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100660 boolean enabled = false;
Paul McLean3a30e552019-12-07 14:47:29 -0700661 ResolveInfo ri = null;
Nicolas Prevot50449882014-06-23 12:42:37 +0100662 if (hasValidSelection) {
Paul McLean3a30e552019-12-07 14:47:29 -0700663 ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800664 if (ri == null) {
665 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
666 return;
667 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
668 Log.e(TAG, "Attempted to set selection to resolve info for another user");
669 return;
670 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100671 enabled = true;
672 }
Matt Pietal0d6834a2019-06-27 13:27:52 -0400673 if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) {
674 mAlwaysButton.setText(getResources()
675 .getString(R.string.activity_resolver_set_always));
676 } else {
677 mAlwaysButton.setText(getResources()
678 .getString(R.string.activity_resolver_use_always));
679 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100680 }
Paul McLean3a30e552019-12-07 14:47:29 -0700681
682 ActivityInfo activityInfo = ri.activityInfo;
683
684 boolean hasRecordPermission =
685 PermissionChecker.checkPermissionForPreflight(
686 getApplicationContext(),
687 android.Manifest.permission.RECORD_AUDIO, -1,
688 activityInfo.applicationInfo.uid,
689 activityInfo.packageName)
690 == android.content.pm.PackageManager.PERMISSION_GRANTED;
691
692 if (!hasRecordPermission) {
693 // OK, we know the record permission, is this a capture device
694 boolean hasAudioCapture =
695 getIntent().getBooleanExtra(
696 ResolverActivity.EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
697 enabled = !hasAudioCapture;
698 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100699 mAlwaysButton.setEnabled(enabled);
700 }
701
Adam Powellc5878612012-05-04 18:42:38 -0700702 public void onButtonClick(View v) {
703 final int id = v.getId();
shafik69df96322018-12-18 15:41:19 +0000704 int which = mAdapter.hasFilteredItem()
705 ? mAdapter.getFilteredPosition()
706 : mAdapterView.getCheckedItemPosition();
707 boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400708 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Matt Pietala310c1f2019-07-03 09:12:56 -0400709 if (mUseLayoutForBrowsables
710 && !ri.handleAllWebDataURI && id == R.id.button_always) {
Matt Pietal0d6834a2019-06-27 13:27:52 -0400711 showSettingsForSelected(ri);
shafik69df96322018-12-18 15:41:19 +0000712 } else {
713 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
714 }
715 }
716
Matt Pietal0d6834a2019-06-27 13:27:52 -0400717 private void showSettingsForSelected(ResolveInfo ri) {
shafik34064c32019-03-06 15:42:52 +0000718 Intent intent = new Intent();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400719
720 final String packageName = ri.activityInfo.packageName;
721 Bundle showFragmentArgs = new Bundle();
722 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY);
723 showFragmentArgs.putString("package", packageName);
724
shafik34064c32019-03-06 15:42:52 +0000725 // For regular apps, we open the Open by Default page
Matt Pietal0d6834a2019-06-27 13:27:52 -0400726 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
727 .setData(Uri.fromParts("package", packageName, null))
728 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
729 .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY)
730 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
731
shafik34064c32019-03-06 15:42:52 +0000732 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700733 }
734
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800735 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700736 if (isFinishing()) {
737 return;
738 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800739 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000740 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
741 Toast.makeText(this, String.format(getResources().getString(
742 com.android.internal.R.string.activity_resolver_work_profiles_support),
743 ri.activityInfo.loadLabel(getPackageManager()).toString()),
744 Toast.LENGTH_LONG).show();
745 return;
746 }
747
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800748 TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800749 if (target == null) {
750 return;
751 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700752 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800753 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100754 MetricsLogger.action(
755 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800756 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100757 MetricsLogger.action(
758 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
759 } else {
760 MetricsLogger.action(
761 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
762 }
763 MetricsLogger.action(this, mAdapter.hasFilteredItem()
764 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
765 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700766 finish();
767 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
Adam Powelle49d9392014-07-17 18:45:19 -0700770 /**
771 * Replace me in subclasses!
772 */
arangelovb0802dc2019-10-18 18:03:44 +0100773 @Override // ResolverListCommunicator
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000774 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700775 return defIntent;
776 }
777
arangelovb0802dc2019-10-18 18:03:44 +0100778 @Override // ResolverListCommunicator
779 public void onPostListReady() {
780 setHeader();
781 resetButtonBar();
782 onListRebuilt();
783 }
784
785 protected void onListRebuilt() {
786 int count = mAdapter.getUnfilteredCount();
787 if (count == 1 && mAdapter.getOtherProfile() == null) {
788 // Only one target, so we're a candidate to auto-launch!
789 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
790 if (shouldAutoLaunchSingleChoice(target)) {
791 safelyStartActivity(target);
792 finish();
793 }
794 }
795 }
796
Adam Powell2ed547e2015-04-29 18:45:04 -0700797 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700798 final ResolveInfo ri = target.getResolveInfo();
799 final Intent intent = target != null ? target.getResolvedIntent() : null;
800
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800801 if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800802 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700803 // Build a reasonable intent filter, based on what matched.
804 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200805 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806
Henrik Engström3277cf12014-07-17 12:18:29 +0200807 if (intent.getSelector() != null) {
808 filterIntent = intent.getSelector();
809 } else {
810 filterIntent = intent;
811 }
812
813 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800814 if (action != null) {
815 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200817 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700818 if (categories != null) {
819 for (String cat : categories) {
820 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700823 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824
Adam Powell24428412015-04-01 17:19:56 -0700825 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200826 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700827 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200828 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700829 if (mimeType != null) {
830 try {
831 filter.addDataType(mimeType);
832 } catch (IntentFilter.MalformedMimeTypeException e) {
833 Log.w("ResolverActivity", e);
834 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 }
836 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700837 }
838 if (data != null && data.getScheme() != null) {
839 // We need the data specification if there was no type,
840 // OR if the scheme is not one of our magical "file:"
841 // or "content:" schemes (see IntentFilter for the reason).
842 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
843 || (!"file".equals(data.getScheme())
844 && !"content".equals(data.getScheme()))) {
845 filter.addDataScheme(data.getScheme());
846
847 // Look through the resolved filter to determine which part
848 // of it matched the original Intent.
849 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
850 if (pIt != null) {
851 String ssp = data.getSchemeSpecificPart();
852 while (ssp != null && pIt.hasNext()) {
853 PatternMatcher p = pIt.next();
854 if (p.match(ssp)) {
855 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
856 break;
857 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700858 }
859 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700860 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
861 if (aIt != null) {
862 while (aIt.hasNext()) {
863 IntentFilter.AuthorityEntry a = aIt.next();
864 if (a.match(data) >= 0) {
865 int port = a.getPort();
866 filter.addDataAuthority(a.getHost(),
867 port >= 0 ? Integer.toString(port) : null);
868 break;
869 }
870 }
871 }
872 pIt = ri.filter.pathsIterator();
873 if (pIt != null) {
874 String path = data.getPath();
875 while (path != null && pIt.hasNext()) {
876 PatternMatcher p = pIt.next();
877 if (p.match(path)) {
878 filter.addDataPath(p.getPath(), p.getType());
879 break;
880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
882 }
883 }
884 }
885
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700886 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800887 final int N = mAdapter.mUnfilteredResolveList.size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800888 ComponentName[] set;
889 // If we don't add back in the component for forwarding the intent to a managed
890 // profile, the preferred activity may not be updated correctly (as the set of
891 // components we tell it we knew about will have changed).
arangelovb0802dc2019-10-18 18:03:44 +0100892 final boolean needToAddBackProfileForwardingComponent =
893 mAdapter.getOtherProfile() != null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800894 if (!needToAddBackProfileForwardingComponent) {
895 set = new ComponentName[N];
896 } else {
897 set = new ComponentName[N + 1];
898 }
899
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700900 int bestMatch = 0;
901 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800902 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700903 set[i] = new ComponentName(r.activityInfo.packageName,
904 r.activityInfo.name);
905 if (r.match > bestMatch) bestMatch = r.match;
906 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800907
908 if (needToAddBackProfileForwardingComponent) {
arangelovb0802dc2019-10-18 18:03:44 +0100909 set[N] = mAdapter.getOtherProfile().getResolvedComponentName();
910 final int otherProfileMatch = mAdapter.getOtherProfile().getResolveInfo().match;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800911 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
912 }
913
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700914 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700915 final int userId = getUserId();
916 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800917
918 // Set the preferred Activity
919 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
920
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700921 if (ri.handleAllWebDataURI) {
922 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700923 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700924 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700925 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700926 }
927 } else {
928 // Update Domain Verification status
929 ComponentName cn = intent.getComponent();
930 String packageName = cn.getPackageName();
931 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800932
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700933 boolean isHttpOrHttps = (dataScheme != null) &&
934 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
935 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800936
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700937 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
938 boolean hasCategoryBrowsable = (categories != null) &&
939 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800940
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700941 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700942 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700943 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
944 userId);
945 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800946 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700947 } else {
948 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800949 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700950 } catch (RemoteException re) {
951 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
952 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
955 }
956
Adam Powell24428412015-04-01 17:19:56 -0700957 if (target != null) {
958 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -0400959
960 // Rely on the ActivityManager to pop up a dialog regarding app suspension
961 // and return false
962 if (target.isSuspended()) {
963 return false;
964 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700965 }
Matt Pietala4b30072019-04-04 13:44:36 -0400966
Adam Powell2ed547e2015-04-29 18:45:04 -0700967 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700968 }
969
Adam Powell23882512016-01-29 10:21:00 -0800970 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -0700971 // We're dispatching intents that might be coming from legacy apps, so
972 // don't kill ourselves.
973 StrictMode.disableDeathOnFileUriExposure();
974 try {
975 safelyStartActivityInternal(cti);
976 } finally {
977 StrictMode.enableDeathOnFileUriExposure();
978 }
979 }
980
981 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +0000982 // If needed, show that intent is forwarded
983 // from managed profile to owner or other way around.
984 if (mProfileSwitchMessageId != -1) {
985 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
986 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700987 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -0700988 if (cti.start(this, null)) {
989 onActivityStarted(cti);
990 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700991 return;
992 }
993 try {
Adam Powell24428412015-04-01 17:19:56 -0700994 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
995 onActivityStarted(cti);
996 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700997 } catch (RuntimeException e) {
998 String launchedFromPackage;
999 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001000 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001001 getActivityToken());
1002 } catch (RemoteException e2) {
1003 launchedFromPackage = "??";
1004 }
1005 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
1006 + " package " + launchedFromPackage + ", while running in "
1007 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
1010
Alison Cichowlas3e340502018-08-07 17:15:01 -04001011
arangelovb0802dc2019-10-18 18:03:44 +01001012 public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
Alison Cichowlas3e340502018-08-07 17:15:01 -04001013 int userId) {
1014 // Pass intent to delegate chooser activity with permission token.
1015 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
1016 // moves into systemui
1017 try {
1018 // TODO: Once this is a small springboard activity, it can move off the UI process
1019 // and we can move the request method to ActivityManagerInternal.
1020 IBinder permissionToken = ActivityTaskManager.getService()
1021 .requestStartActivityPermissionToken(getActivityToken());
1022 final Intent chooserIntent = new Intent();
1023 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1024 Resources.getSystem().getString(R.string.config_chooserActivity));
1025 chooserIntent.setClassName(delegateActivity.getPackageName(),
1026 delegateActivity.getClassName());
1027 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1028
1029 // TODO: These extras will change as chooser activity moves into systemui
1030 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1031 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1032 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1033 ignoreTargetSecurity);
1034 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001035 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1036 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001037 startActivity(chooserIntent);
1038 } catch (RemoteException e) {
1039 Log.e(TAG, e.toString());
1040 }
1041 return true;
1042 }
1043
Adam Powell23882512016-01-29 10:21:00 -08001044 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001045 // Do nothing
1046 }
1047
arangelovb0802dc2019-10-18 18:03:44 +01001048 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -08001049 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001050 return false;
1051 }
1052
Adam Powell23882512016-01-29 10:21:00 -08001053 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001054 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001055 }
1056
Adam Powell23882512016-01-29 10:21:00 -08001057 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001058 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001059 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001060 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001061 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001062 }
1063
arangelovb0802dc2019-10-18 18:03:44 +01001064 public ResolverListAdapter createAdapter(Context context, List<Intent> payloadIntents,
1065 Intent[] initialIntents, List<ResolveInfo> rList,
1066 boolean filterLastUsed, boolean useLayoutForBrowsables) {
Paul McLean07425c82019-10-18 12:00:11 -06001067
1068 Intent startIntent = getIntent();
1069 boolean isAudioCaptureDevice =
1070 startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
arangelovb0802dc2019-10-18 18:03:44 +01001071 return new ResolverListAdapter(context, payloadIntents, initialIntents, rList,
Paul McLean07425c82019-10-18 12:00:11 -06001072 filterLastUsed, createListController(), useLayoutForBrowsables, this,
1073 isAudioCaptureDevice);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001074 }
1075
1076 @VisibleForTesting
1077 protected ResolverListController createListController() {
1078 return new ResolverListController(
1079 this,
1080 mPm,
1081 getTargetIntent(),
1082 getReferrerPackageName(),
1083 mLaunchedFromUid);
Adam Powell88831a22014-11-20 18:17:00 -08001084 }
1085
Adam Powell39e94eb2015-09-08 17:01:49 -07001086 /**
arangelovb0802dc2019-10-18 18:03:44 +01001087 * Sets up the content view.
arangelovbb572332019-11-13 12:50:08 +00001088 * @return <code>true</code> if the activity is finishing and creation should halt.
Adam Powell39e94eb2015-09-08 17:01:49 -07001089 */
arangelovbb572332019-11-13 12:50:08 +00001090 private boolean configureContentView() {
arangelovb0802dc2019-10-18 18:03:44 +01001091 if (mAdapter == null) {
1092 throw new IllegalStateException("mAdapter cannot be null.");
1093 }
arangelovbb572332019-11-13 12:50:08 +00001094 boolean rebuildCompleted = mAdapter.rebuildList();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001095 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001096 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001097 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001098 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001099 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001100 setContentView(mLayoutId);
arangelovbb572332019-11-13 12:50:08 +00001101 return postRebuildList(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001102 }
Adam Powell7d758002015-05-06 17:49:36 -07001103
arangelovb0802dc2019-10-18 18:03:44 +01001104 /**
arangelovbb572332019-11-13 12:50:08 +00001105 * Finishing procedures to be performed after the list has been rebuilt.
1106 * </p>Subclasses must call postRebuildListInternal at the end of postRebuildList.
1107 * @param rebuildCompleted
1108 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001109 */
arangelovbb572332019-11-13 12:50:08 +00001110 protected boolean postRebuildList(boolean rebuildCompleted) {
1111 return postRebuildListInternal(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001112 }
1113
1114 /**
arangelovbb572332019-11-13 12:50:08 +00001115 * Finishing procedures to be performed after the list has been rebuilt.
1116 * @param rebuildCompleted
1117 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001118 */
arangelovbb572332019-11-13 12:50:08 +00001119 final boolean postRebuildListInternal(boolean rebuildCompleted) {
1120
Adam Powell50077352015-05-26 18:01:55 -07001121 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001122
1123 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1124 // we're already done, we can check if we should auto-launch immediately.
1125 if (rebuildCompleted) {
1126 if (count == 1 && mAdapter.getOtherProfile() == null) {
1127 // Only one target, so we're a candidate to auto-launch!
1128 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
1129 if (shouldAutoLaunchSingleChoice(target)) {
1130 safelyStartActivity(target);
1131 mPackageMonitor.unregister();
1132 mRegistered = false;
1133 finish();
1134 return true;
1135 }
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001136 }
1137 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001138
Zhen Zhangbde7b462019-11-11 11:49:33 -08001139 boolean isAdapterViewVisible = true;
arangelovb0802dc2019-10-18 18:03:44 +01001140 if (count == 0 && mAdapter.getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001141 final TextView emptyView = findViewById(R.id.empty);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001142 emptyView.setVisibility(View.VISIBLE);
Zhen Zhangbde7b462019-11-11 11:49:33 -08001143 isAdapterViewVisible = false;
Adam Powell7d758002015-05-06 17:49:36 -07001144 }
Zhen Zhangbde7b462019-11-11 11:49:33 -08001145
1146 onPrepareAdapterView(mAdapter, isAdapterViewVisible);
Adam Powell39e94eb2015-09-08 17:01:49 -07001147 return false;
Adam Powell7d758002015-05-06 17:49:36 -07001148 }
1149
Zhen Zhangbde7b462019-11-11 11:49:33 -08001150 /**
1151 * Prepare the scrollable view which consumes data in the list adapter.
1152 * @param adapter The adapter used to provide data to item views.
1153 * @param isVisible True if the scrollable view should be visible; false, otherwise.
1154 */
1155 public void onPrepareAdapterView(ResolverListAdapter adapter, boolean isVisible) {
1156 mAdapterView = findViewById(R.id.resolver_list);
1157 if (!isVisible) {
1158 mAdapterView.setVisibility(View.GONE);
1159 return;
1160 }
1161 mAdapterView.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -07001162 final boolean useHeader = adapter.hasFilteredItem();
Zhen Zhangbde7b462019-11-11 11:49:33 -08001163 final ListView listView = mAdapterView instanceof ListView ? (ListView) mAdapterView : null;
Adam Powell7d758002015-05-06 17:49:36 -07001164
Zhen Zhangbde7b462019-11-11 11:49:33 -08001165 mAdapterView.setAdapter(mAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001166
1167 final ItemClickListener listener = new ItemClickListener();
Zhen Zhangbde7b462019-11-11 11:49:33 -08001168 mAdapterView.setOnItemClickListener(listener);
1169 mAdapterView.setOnItemLongClickListener(listener);
Adam Powell7d758002015-05-06 17:49:36 -07001170
shafik69df96322018-12-18 15:41:19 +00001171 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001172 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1173 }
1174
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001175 // In case this method is called again (due to activity recreation), avoid adding a new
1176 // header if one is already present.
1177 if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) {
Matt Pietald5071202019-10-10 11:21:28 -04001178 listView.setHeaderDividersEnabled(true);
Adam Powell7d758002015-05-06 17:49:36 -07001179 listView.addHeaderView(LayoutInflater.from(this).inflate(
1180 R.layout.resolver_different_item_header, listView, false));
1181 }
Adam Powell24428412015-04-01 17:19:56 -07001182 }
1183
Matt Pietal26038402019-01-08 07:29:34 -05001184 /**
1185 * Configure the area above the app selection list (title, content preview, etc).
1186 */
1187 public void setHeader() {
arangelovb0802dc2019-10-18 18:03:44 +01001188 if (mAdapter.getCount() == 0 && mAdapter.getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001189 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001190 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001191 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001192 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001193 }
1194
1195 CharSequence title = mTitle != null
1196 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001197 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001198
1199 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001200 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001201 if (titleView != null) {
1202 titleView.setText(title);
1203 }
1204 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001205 }
1206
Alan Viverette51efddb2017-04-05 10:00:01 -04001207 final ImageView iconView = findViewById(R.id.icon);
arangelovb0802dc2019-10-18 18:03:44 +01001208 if (iconView != null) {
1209 mAdapter.loadFilteredItemIconTaskAsync(iconView);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001210 }
1211 }
1212
shafik69df96322018-12-18 15:41:19 +00001213 private void resetButtonBar() {
1214 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1215 return;
1216 }
1217 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1218 if (buttonLayout != null) {
1219 buttonLayout.setVisibility(View.VISIBLE);
Matt Pietal800136a2019-05-08 07:46:39 -04001220
Matt Pietald5071202019-10-10 11:21:28 -04001221 if (!useLayoutWithDefault()) {
1222 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1223 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
1224 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1225 R.dimen.resolver_button_bar_spacing) + inset);
1226 }
shafik69df96322018-12-18 15:41:19 +00001227 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
shafik69df96322018-12-18 15:41:19 +00001228 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1229
Matt Pietal0d6834a2019-06-27 13:27:52 -04001230 resetAlwaysOrOnceButtonBar();
shafik69df96322018-12-18 15:41:19 +00001231 } else {
1232 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1233 }
1234 }
1235
shafik69df96322018-12-18 15:41:19 +00001236 private void resetAlwaysOrOnceButtonBar() {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001237 if (useLayoutWithDefault()
1238 && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001239 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
1240 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001241 return;
1242 }
1243
1244 // When the items load in, if an item was already selected, enable the buttons
1245 if (mAdapterView != null
1246 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1247 setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true);
1248 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001249 }
1250 }
1251
arangelovb0802dc2019-10-18 18:03:44 +01001252 @Override // ResolverListCommunicator
1253 public boolean useLayoutWithDefault() {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001254 return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem();
1255 }
1256
Adam Powellc412be62015-06-24 13:54:10 -07001257 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001258 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1259 * called and we are launched in a new task.
1260 */
1261 protected void setRetainInOnStop(boolean retainInOnStop) {
1262 mRetainInOnStop = retainInOnStop;
1263 }
1264
1265 /**
Adam Powellc412be62015-06-24 13:54:10 -07001266 * Check a simple match for the component of two ResolveInfos.
1267 */
arangelovb0802dc2019-10-18 18:03:44 +01001268 @Override // ResolverListCommunicator
1269 public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
Adam Powellc412be62015-06-24 13:54:10 -07001270 return lhs == null ? rhs == null
1271 : lhs.activityInfo == null ? rhs.activityInfo == null
1272 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1273 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1274 }
1275
arangelovb0802dc2019-10-18 18:03:44 +01001276 @Override // ResolverListCommunicator
1277 public void onHandlePackagesChanged() {
1278 if (mAdapter.getCount() == 0) {
1279 // We no longer have any items... just finish the activity.
1280 finish();
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001281 }
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001282 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001283
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001284 @VisibleForTesting
1285 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001286 public final ComponentName name;
1287 private final List<Intent> mIntents = new ArrayList<>();
1288 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001289 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001290
1291 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1292 this.name = name;
1293 add(intent, info);
1294 }
1295
1296 public void add(Intent intent, ResolveInfo info) {
1297 mIntents.add(intent);
1298 mResolveInfos.add(info);
1299 }
1300
1301 public int getCount() {
1302 return mIntents.size();
1303 }
1304
1305 public Intent getIntentAt(int index) {
1306 return index >= 0 ? mIntents.get(index) : null;
1307 }
1308
1309 public ResolveInfo getResolveInfoAt(int index) {
1310 return index >= 0 ? mResolveInfos.get(index) : null;
1311 }
1312
1313 public int findIntent(Intent intent) {
1314 for (int i = 0, N = mIntents.size(); i < N; i++) {
1315 if (intent.equals(mIntents.get(i))) {
1316 return i;
1317 }
1318 }
1319 return -1;
1320 }
1321
1322 public int findResolveInfo(ResolveInfo info) {
1323 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1324 if (info.equals(mResolveInfos.get(i))) {
1325 return i;
1326 }
1327 }
1328 return -1;
1329 }
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001330
1331 public boolean isPinned() {
1332 return mPinned;
1333 }
1334
1335 public void setPinned(boolean pinned) {
1336 mPinned = pinned;
1337 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001338 }
1339
Adam Powell7d758002015-05-06 17:49:36 -07001340 class ItemClickListener implements AdapterView.OnItemClickListener,
1341 AdapterView.OnItemLongClickListener {
1342 @Override
1343 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1344 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1345 if (listView != null) {
1346 position -= listView.getHeaderViewsCount();
1347 }
1348 if (position < 0) {
1349 // Header views don't count.
1350 return;
1351 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001352 // If we're still loading, we can't yet enable the buttons.
1353 if (mAdapter.resolveInfoForPosition(position, true) == null) {
1354 return;
1355 }
1356
Adam Powell7d758002015-05-06 17:49:36 -07001357 final int checkedPos = mAdapterView.getCheckedItemPosition();
1358 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001359 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07001360 && (!hasValidSelection || mLastSelected != checkedPos)
Matt Pietal0d6834a2019-06-27 13:27:52 -04001361 && mAlwaysButton != null) {
1362 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Adam Powell7d758002015-05-06 17:49:36 -07001363 mOnceButton.setEnabled(hasValidSelection);
1364 if (hasValidSelection) {
1365 mAdapterView.smoothScrollToPosition(checkedPos);
1366 }
1367 mLastSelected = checkedPos;
1368 } else {
1369 startSelected(position, false, true);
1370 }
1371 }
Adam Powell2d809622012-03-22 15:24:43 -07001372
1373 @Override
1374 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001375 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1376 if (listView != null) {
1377 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001378 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001379 if (position < 0) {
1380 // Header views don't count.
1381 return false;
1382 }
Adam Powell278902c2014-07-12 18:33:22 -07001383 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001384 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001385 return true;
1386 }
1387
1388 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001389
Dianne Hackbornec452d92014-11-11 17:16:56 -08001390 static final boolean isSpecificUriMatch(int match) {
1391 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1392 return match >= IntentFilter.MATCH_CATEGORY_HOST
1393 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1394 }
1395
Adam Powell4c470d62015-06-19 17:46:17 -07001396 static class PickTargetOptionRequest extends PickOptionRequest {
1397 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1398 @Nullable Bundle extras) {
1399 super(prompt, options, extras);
1400 }
1401
1402 @Override
1403 public void onCancel() {
1404 super.onCancel();
1405 final ResolverActivity ra = (ResolverActivity) getActivity();
1406 if (ra != null) {
1407 ra.mPickOptionRequest = null;
1408 ra.finish();
1409 }
1410 }
1411
1412 @Override
1413 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1414 super.onPickOptionResult(finished, selections, result);
1415 if (selections.length != 1) {
1416 // TODO In a better world we would filter the UI presented here and let the
1417 // user refine. Maybe later.
1418 return;
1419 }
1420
1421 final ResolverActivity ra = (ResolverActivity) getActivity();
1422 if (ra != null) {
1423 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
1424 if (ra.onTargetSelected(ti, false)) {
1425 ra.mPickOptionRequest = null;
1426 ra.finish();
1427 }
1428 }
1429 }
1430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431}