blob: 1beb1c5e912ad8916ee71992c4a9ea0ac6afce89 [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;
35import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070038import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010040import android.content.pm.UserInfo;
Jiongxuan Gao3365b162016-11-24 20:20:27 +090041import android.content.res.Configuration;
Adam Powellc5878612012-05-04 18:42:38 -070042import android.content.res.Resources;
Matt Pietal800136a2019-05-08 07:46:39 -040043import android.graphics.Insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.net.Uri;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010045import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040047import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.PatternMatcher;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080049import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070050import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070051import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010052import android.os.UserManager;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080053import android.provider.MediaStore;
54import android.provider.Settings;
55import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.Log;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080057import android.util.Slog;
Adam Powell2d809622012-03-22 15:24:43 -070058import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.View;
60import android.view.ViewGroup;
Matt Pietal800136a2019-05-08 07:46:39 -040061import android.view.ViewGroup.LayoutParams;
62import android.view.WindowInsets;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080063import android.widget.AbsListView;
Adam Powell2d809622012-03-22 15:24:43 -070064import android.widget.AdapterView;
Adam Powellc5878612012-05-04 18:42:38 -070065import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070067import android.widget.ListView;
Matt Pietal800136a2019-05-08 07:46:39 -040068import android.widget.Space;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010070import android.widget.Toast;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070071
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080072import com.android.internal.R;
73import com.android.internal.annotations.VisibleForTesting;
arangelovb0802dc2019-10-18 18:03:44 +010074import com.android.internal.app.chooser.DisplayResolveInfo;
75import com.android.internal.app.chooser.TargetInfo;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080076import com.android.internal.content.PackageMonitor;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010077import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010078import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070079import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010082import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.Iterator;
84import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070085import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.Set;
87
88/**
89 * This activity is displayed when the system attempts to start an Intent for
90 * which there is more than one matching activity, allowing the user to decide
91 * which to go to. It is not normally used directly by application developers.
92 */
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080093@UiThread
arangelovb0802dc2019-10-18 18:03:44 +010094public class ResolverActivity extends Activity implements
95 ResolverListAdapter.ResolverListCommunicator {
Alison Cichowlas4691ed42018-11-13 15:59:55 -050096
Andrei Onea15884392019-03-22 17:28:11 +000097 @UnsupportedAppUsage
arangelovb0802dc2019-10-18 18:03:44 +010098 protected ResolverListAdapter mAdapter;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070099 private boolean mSafeForwardingMode;
Matt Pietal5b648562019-03-12 07:40:26 -0400100 protected AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -0700101 private Button mAlwaysButton;
102 private Button mOnceButton;
Matt Pietal74c6ed02019-04-18 13:38:46 -0400103 protected View mProfileView;
Adam Powell24428412015-04-01 17:19:56 -0700104 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100105 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000106 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800107 private int mLayoutId;
arangelovb0802dc2019-10-18 18:03:44 +0100108 @VisibleForTesting
109 protected final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700110 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800111 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800112 private CharSequence mTitle;
113 private int mDefaultTitleResId;
arangelovb0802dc2019-10-18 18:03:44 +0100114
115 @VisibleForTesting
116 protected boolean mUseLayoutForBrowsables;
Adam Powell09a65602014-07-20 16:23:14 -0700117
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800118 // Whether or not this activity supports choosing a default handler for the intent.
arangelovb0802dc2019-10-18 18:03:44 +0100119 @VisibleForTesting
120 protected boolean mSupportsAlwaysUseOption;
Adam Powell63b31692015-09-28 10:45:00 -0700121 protected ResolverDrawerLayout mResolverDrawerLayout;
Andrei Onea15884392019-03-22 17:28:11 +0000122 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800123 protected PackageManager mPm;
124 protected int mLaunchedFromUid;
125
126 private static final String TAG = "ResolverActivity";
127 private static final boolean DEBUG = false;
Adam Powell63b31692015-09-28 10:45:00 -0700128
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700129 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100130
Matt Pietal800136a2019-05-08 07:46:39 -0400131 protected Insets mSystemWindowInsets = null;
132 private Space mFooterSpacer = null;
133
Jorim Jaggif631ef72017-02-24 13:49:47 +0100134 /** See {@link #setRetainInOnStop}. */
135 private boolean mRetainInOnStop;
136
Matt Pietal0d6834a2019-06-27 13:27:52 -0400137 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
138 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
139 private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state";
140
Matt Pietalab73a882019-06-05 07:04:55 -0400141 private final PackageMonitor mPackageMonitor = createPackageMonitor();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800142
Paul McLean07425c82019-10-18 12:00:11 -0600143 // Intent extra for connected audio devices
144 public static final String EXTRA_IS_AUDIO_CAPTURE_DEVICE = "is_audio_capture_device";
145
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700146 /**
147 * Get the string resource to be used as a label for the link to the resolver activity for an
148 * action.
149 *
150 * @param action The action to resolve
151 *
152 * @return The string resource to be used as a label
153 */
154 public static @StringRes int getLabelRes(String action) {
155 return ActionTitle.forAction(action).labelRes;
156 }
157
Adam Powell278902c2014-07-12 18:33:22 -0700158 private enum ActionTitle {
159 VIEW(Intent.ACTION_VIEW,
160 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700161 com.android.internal.R.string.whichViewApplicationNamed,
162 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700163 EDIT(Intent.ACTION_EDIT,
164 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700165 com.android.internal.R.string.whichEditApplicationNamed,
166 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700167 SEND(Intent.ACTION_SEND,
168 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700169 com.android.internal.R.string.whichSendApplicationNamed,
170 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700171 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700172 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700173 com.android.internal.R.string.whichSendToApplicationNamed,
174 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700175 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
176 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700177 com.android.internal.R.string.whichSendApplicationNamed,
178 com.android.internal.R.string.whichSendApplicationLabel),
179 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
180 com.android.internal.R.string.whichImageCaptureApplication,
181 com.android.internal.R.string.whichImageCaptureApplicationNamed,
182 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700183 DEFAULT(null,
184 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700185 com.android.internal.R.string.whichApplicationNamed,
186 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700187 HOME(Intent.ACTION_MAIN,
188 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700189 com.android.internal.R.string.whichHomeApplicationNamed,
190 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700191
shafik0c7c5d52019-02-27 12:13:25 +0000192 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000193 public static final int BROWSABLE_TITLE_RES =
Matt Pietal0d6834a2019-06-27 13:27:52 -0400194 com.android.internal.R.string.whichOpenLinksWith;
195 public static final int BROWSABLE_HOST_TITLE_RES =
196 com.android.internal.R.string.whichOpenHostLinksWith;
197 public static final int BROWSABLE_HOST_APP_TITLE_RES =
198 com.android.internal.R.string.whichOpenHostLinksWithApp;
199 public static final int BROWSABLE_APP_TITLE_RES =
200 com.android.internal.R.string.whichOpenLinksWithApp;
shafik69df96322018-12-18 15:41:19 +0000201
Adam Powell278902c2014-07-12 18:33:22 -0700202 public final String action;
203 public final int titleRes;
204 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700205 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700206
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700207 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700208 this.action = action;
209 this.titleRes = titleRes;
210 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700211 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700212 }
213
214 public static ActionTitle forAction(String action) {
215 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700216 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700217 return title;
218 }
219 }
220 return DEFAULT;
221 }
222 }
223
Matt Pietalab73a882019-06-05 07:04:55 -0400224 protected PackageMonitor createPackageMonitor() {
225 return new PackageMonitor() {
226 @Override
227 public void onSomePackagesChanged() {
228 mAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100229 updateProfileViewButton();
Matt Pietalab73a882019-06-05 07:04:55 -0400230 }
231
232 @Override
233 public boolean onPackageChanged(String packageName, int uid, String[] components) {
234 // We care about all package changes, not just the whole package itself which is
235 // default behavior.
236 return true;
237 }
238 };
239 }
240
Dianne Hackborn905577f2011-09-07 18:31:28 -0700241 private Intent makeMyIntent() {
242 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700243 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700244 // The resolver activity is set to be hidden from recent tasks.
245 // we don't want this attribute to be propagated to the next activity
246 // being launched. Note that if the original Intent also had this
247 // flag set, we are now losing it. That should be a very rare case
248 // and we can live with this.
249 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
250 return intent;
251 }
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 @Override
254 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700255 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700256 final Intent intent = makeMyIntent();
257 final Set<String> categories = intent.getCategories();
258 if (Intent.ACTION_MAIN.equals(intent.getAction())
259 && categories != null
260 && categories.size() == 1
261 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100262 // Note: this field is not set to true in the compatibility version.
263 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700264 }
265
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700266 setSafeForwardingMode(true);
267
Adam Powella35c77a2014-09-25 16:46:36 -0700268 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 }
270
Adam Powell278902c2014-07-12 18:33:22 -0700271 /**
Adam Powell24428412015-04-01 17:19:56 -0700272 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700273 * a default title resource
274 */
Andrei Onea15884392019-03-22 17:28:11 +0000275 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700277 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800278 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
279 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
280 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700281 }
282
283 protected void onCreate(Bundle savedInstanceState, Intent intent,
284 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800285 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700286 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000288
289 // Determine whether we should show that intent is forwarded
290 // from managed profile to owner or other way around.
291 setProfileSwitchMessageId(intent.getContentUserHint());
292
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700293 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700294 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700295 getActivityToken());
296 } catch (RemoteException e) {
297 mLaunchedFromUid = -1;
298 }
Adam Powell7d758002015-05-06 17:49:36 -0700299
300 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
301 // Gulp!
302 finish();
303 return;
304 }
305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700307
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700308 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700309 mRegistered = true;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800310 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800311
Adam Powell7d758002015-05-06 17:49:36 -0700312 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700313 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800314 mTitle = title;
315 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700316
shafik69df96322018-12-18 15:41:19 +0000317 mUseLayoutForBrowsables = getTargetIntent() == null
318 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000319 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000320
Matt Pietal0d6834a2019-06-27 13:27:52 -0400321 mSupportsAlwaysUseOption = supportsAlwaysUseOption;
shafik69df96322018-12-18 15:41:19 +0000322
arangelovb0802dc2019-10-18 18:03:44 +0100323 // The last argument of createAdapter is whether to do special handling
324 // of the last used choice to highlight it in the list. We need to always
325 // turn this off when running under voice interaction, since it results in
326 // a more complicated UI that the current voice interaction flow is not able
327 // to handle.
328 boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction();
329 mAdapter = createAdapter(this, mIntents, initialIntents, rList,
330 filterLastUsed, mUseLayoutForBrowsables);
331 configureContentView();
332
333 if (rebuildList()) {
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);
arangelovb0802dc2019-10-18 18:03:44 +0100359 updateProfileViewButton();
Adam Powell88831a22014-11-20 18:17:00 -0800360 }
Adam Powell4c470d62015-06-19 17:46:17 -0700361
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100362 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100363 MetricsLogger.action(this, mAdapter.hasFilteredItem()
364 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
365 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
366 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100367 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700368 }
369
Matt Pietal74c6ed02019-04-18 13:38:46 -0400370 protected void onProfileClick(View v) {
371 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
372 if (dri == null) {
373 return;
374 }
375
376 // Do not show the profile switch message anymore.
377 mProfileSwitchMessageId = -1;
378
379 onTargetSelected(dri, false);
380 finish();
381 }
382
Matt Pietal800136a2019-05-08 07:46:39 -0400383 protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
384 mSystemWindowInsets = insets.getSystemWindowInsets();
385
386 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
387 mSystemWindowInsets.right, 0);
388
Matt Pietald5071202019-10-10 11:21:28 -0400389 // Need extra padding so the list can fully scroll up
390 if (useLayoutWithDefault()) {
391 if (mFooterSpacer == null) {
392 mFooterSpacer = new Space(getApplicationContext());
393 } else {
394 ((ListView) mAdapterView).removeFooterView(mFooterSpacer);
395 }
396 mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
397 mSystemWindowInsets.bottom));
398 ((ListView) mAdapterView).addFooterView(mFooterSpacer);
Matt Pietal800136a2019-05-08 07:46:39 -0400399 } else {
Matt Pietald5071202019-10-10 11:21:28 -0400400 View emptyView = findViewById(R.id.empty);
401 if (emptyView != null) {
402 emptyView.setPadding(0, 0, 0, mSystemWindowInsets.bottom
403 + getResources().getDimensionPixelSize(
404 R.dimen.chooser_edge_margin_normal) * 2);
405 }
Matt Pietal800136a2019-05-08 07:46:39 -0400406 }
Matt Pietal800136a2019-05-08 07:46:39 -0400407
Matt Pietal78fdbd42019-06-05 07:08:08 -0400408 resetButtonBar();
409
Matt Pietal800136a2019-05-08 07:46:39 -0400410 return insets.consumeSystemWindowInsets();
411 }
412
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900413 @Override
414 public void onConfigurationChanged(Configuration newConfig) {
415 super.onConfigurationChanged(newConfig);
416 mAdapter.handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400417
418 if (mSystemWindowInsets != null) {
419 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
420 mSystemWindowInsets.right, 0);
421 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900422 }
423
arangelovb0802dc2019-10-18 18:03:44 +0100424 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -0800425 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700426 if (!isVoiceInteraction()) {
427 // Clearly not needed.
428 return;
429 }
430
Adam Powell4c470d62015-06-19 17:46:17 -0700431 final Option[] options = new Option[mAdapter.getCount()];
432 for (int i = 0, N = options.length; i < N; i++) {
Matt Pietal2cf4c6c2019-09-10 09:33:11 -0400433 TargetInfo target = mAdapter.getItem(i);
434 if (target == null) {
435 // If this occurs, a new set of targets is being loaded. Let that complete,
436 // and have the next call to send voice choices proceed instead.
437 return;
438 }
439 options[i] = optionForChooserTarget(target, i);
Adam Powell4c470d62015-06-19 17:46:17 -0700440 }
441
442 mPickOptionRequest = new PickTargetOptionRequest(
443 new Prompt(getTitle()), options, null);
444 getVoiceInteractor().submitRequest(mPickOptionRequest);
445 }
446
447 Option optionForChooserTarget(TargetInfo target, int index) {
448 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800449 }
450
Adam Powell2ed547e2015-04-29 18:45:04 -0700451 protected final void setAdditionalTargets(Intent[] intents) {
452 if (intents != null) {
453 for (Intent intent : intents) {
454 mIntents.add(intent);
455 }
456 }
457 }
458
arangelovb0802dc2019-10-18 18:03:44 +0100459 @Override // SelectableTargetInfoCommunicator ResolverListCommunicator
Adam Powell0ccc0e92015-04-23 17:19:37 -0700460 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700461 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700462 }
463
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800464 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700465 final Uri referrer = getReferrer();
466 if (referrer != null && "android-app".equals(referrer.getScheme())) {
467 return referrer.getHost();
468 }
469 return null;
470 }
471
Adam Powell23882512016-01-29 10:21:00 -0800472 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700473 return R.layout.resolver_list;
474 }
475
arangelovb0802dc2019-10-18 18:03:44 +0100476 @Override // ResolverListCommunicator
477 public void updateProfileViewButton() {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400478 if (mProfileView == null) {
479 return;
480 }
481
Adam Powell88831a22014-11-20 18:17:00 -0800482 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
483 if (dri != null) {
484 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800485 View text = mProfileView.findViewById(R.id.profile_button);
486 if (!(text instanceof TextView)) {
487 text = mProfileView.findViewById(R.id.text1);
488 }
489 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800490 } else {
491 mProfileView.setVisibility(View.GONE);
492 }
Adam Powell278902c2014-07-12 18:33:22 -0700493 }
494
Sander Alewijnsef6545332014-10-31 12:39:02 +0000495 private void setProfileSwitchMessageId(int contentUserHint) {
496 if (contentUserHint != UserHandle.USER_CURRENT &&
497 contentUserHint != UserHandle.myUserId()) {
498 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
499 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
500 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
501 : false;
502 boolean targetIsManaged = userManager.isManagedProfile();
503 if (originIsManaged && !targetIsManaged) {
504 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
505 } else if (!originIsManaged && targetIsManaged) {
506 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
507 }
508 }
509 }
510
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700511 /**
512 * Turn on launch mode that is safe to use when forwarding intents received from
513 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
514 * instead of the normal Activity.startActivity for launching the activity selected
515 * by the user.
516 *
517 * <p>This mode is set to true by default if the activity is initialized through
518 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
519 * methods, it is set to false by default. You must set it before calling one of the
520 * more detailed onCreate methods, so that it will be set correctly in the case where
521 * there is only one intent to resolve and it is thus started immediately.</p>
522 */
523 public void setSafeForwardingMode(boolean safeForwarding) {
524 mSafeForwardingMode = safeForwarding;
525 }
526
shafik69df96322018-12-18 15:41:19 +0000527 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
528 final ActionTitle title = mResolvingHome
529 ? ActionTitle.HOME
530 : ActionTitle.forAction(intent.getAction());
531
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800532 // While there may already be a filtered item, we can only use it in the title if the list
533 // is already sorted and all information relevant to it is already in the list.
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800534 final boolean named = mAdapter.getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700535 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
536 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000537 } else if (isHttpSchemeAndViewAction(intent)) {
538 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000539 // they're giving access for the activity to open URLs from this specific host
Matt Pietal0d6834a2019-06-27 13:27:52 -0400540 String dialogTitle = null;
541 if (named && !mUseLayoutForBrowsables) {
542 dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES,
543 mAdapter.getFilteredItem().getDisplayLabel());
544 } else if (named && mUseLayoutForBrowsables) {
545 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES,
546 intent.getData().getHost(),
547 mAdapter.getFilteredItem().getDisplayLabel());
548 } else if (mAdapter.areAllTargetsBrowsers()) {
Matt Pietald5071202019-10-10 11:21:28 -0400549 dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES);
Matt Pietal0d6834a2019-06-27 13:27:52 -0400550 } else {
551 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES,
552 intent.getData().getHost());
553 }
554 return dialogTitle;
Adam Powell278902c2014-07-12 18:33:22 -0700555 } else {
Adam Powell24428412015-04-01 17:19:56 -0700556 return named
557 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
558 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700559 }
Adam Powellc5878612012-05-04 18:42:38 -0700560 }
561
Adam Powelle9414d92014-07-05 17:44:18 -0700562 void dismiss() {
563 if (!isFinishing()) {
564 finish();
565 }
566 }
567
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800568 @Override
569 protected void onRestart() {
570 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700571 if (!mRegistered) {
572 mPackageMonitor.register(this, getMainLooper(), false);
573 mRegistered = true;
574 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800575 mAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100576 updateProfileViewButton();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800577 }
578
579 @Override
580 protected void onStop() {
581 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700582 if (mRegistered) {
583 mPackageMonitor.unregister();
584 mRegistered = false;
585 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700586 final Intent intent = getIntent();
587 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100588 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700589 // This resolver is in the unusual situation where it has been
590 // launched at the top of a new task. We don't let it be added
591 // to the recent tasks shown to the user, and we need to make sure
592 // that each time we are launched we get the correct launching
593 // uid (not re-using the same resolver from an old launching uid),
594 // so we will now finish ourself since being no longer visible,
595 // the user probably can't get back to us.
596 if (!isChangingConfigurations()) {
597 finish();
598 }
599 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800600 }
601
Adam Powellc5878612012-05-04 18:42:38 -0700602 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700603 protected void onDestroy() {
604 super.onDestroy();
605 if (!isChangingConfigurations() && mPickOptionRequest != null) {
606 mPickOptionRequest.cancel();
607 }
arangelovb0802dc2019-10-18 18:03:44 +0100608 if (mAdapter != null) {
609 mAdapter.onDestroy();
Kang Li38a6da642017-04-05 12:30:55 -0700610 }
Adam Powell4c470d62015-06-19 17:46:17 -0700611 }
612
613 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700614 protected void onRestoreInstanceState(Bundle savedInstanceState) {
615 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000616 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700617 }
618
shafik0c7c5d52019-02-27 12:13:25 +0000619 private boolean isHttpSchemeAndViewAction(Intent intent) {
620 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
621 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
622 && Intent.ACTION_VIEW.equals(intent.getAction());
623 }
624
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100625 private boolean hasManagedProfile() {
626 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
627 if (userManager == null) {
628 return false;
629 }
630
631 try {
632 List<UserInfo> profiles = userManager.getProfiles(getUserId());
633 for (UserInfo userInfo : profiles) {
634 if (userInfo != null && userInfo.isManagedProfile()) {
635 return true;
636 }
637 }
638 } catch (SecurityException e) {
639 return false;
640 }
641 return false;
642 }
643
644 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
645 try {
646 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
647 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700648 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100649 } catch (NameNotFoundException e) {
650 return false;
651 }
652 }
653
Adam Powell278902c2014-07-12 18:33:22 -0700654 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
655 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100656 boolean enabled = false;
657 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700658 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800659 if (ri == null) {
660 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
661 return;
662 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
663 Log.e(TAG, "Attempted to set selection to resolve info for another user");
664 return;
665 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100666 enabled = true;
667 }
Matt Pietal0d6834a2019-06-27 13:27:52 -0400668 if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) {
669 mAlwaysButton.setText(getResources()
670 .getString(R.string.activity_resolver_set_always));
671 } else {
672 mAlwaysButton.setText(getResources()
673 .getString(R.string.activity_resolver_use_always));
674 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100675 }
676 mAlwaysButton.setEnabled(enabled);
677 }
678
Adam Powellc5878612012-05-04 18:42:38 -0700679 public void onButtonClick(View v) {
680 final int id = v.getId();
shafik69df96322018-12-18 15:41:19 +0000681 int which = mAdapter.hasFilteredItem()
682 ? mAdapter.getFilteredPosition()
683 : mAdapterView.getCheckedItemPosition();
684 boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400685 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Matt Pietala310c1f2019-07-03 09:12:56 -0400686 if (mUseLayoutForBrowsables
687 && !ri.handleAllWebDataURI && id == R.id.button_always) {
Matt Pietal0d6834a2019-06-27 13:27:52 -0400688 showSettingsForSelected(ri);
shafik69df96322018-12-18 15:41:19 +0000689 } else {
690 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
691 }
692 }
693
Matt Pietal0d6834a2019-06-27 13:27:52 -0400694 private void showSettingsForSelected(ResolveInfo ri) {
shafik34064c32019-03-06 15:42:52 +0000695 Intent intent = new Intent();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400696
697 final String packageName = ri.activityInfo.packageName;
698 Bundle showFragmentArgs = new Bundle();
699 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY);
700 showFragmentArgs.putString("package", packageName);
701
shafik34064c32019-03-06 15:42:52 +0000702 // For regular apps, we open the Open by Default page
Matt Pietal0d6834a2019-06-27 13:27:52 -0400703 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
704 .setData(Uri.fromParts("package", packageName, null))
705 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
706 .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY)
707 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
708
shafik34064c32019-03-06 15:42:52 +0000709 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700710 }
711
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800712 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700713 if (isFinishing()) {
714 return;
715 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800716 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000717 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
718 Toast.makeText(this, String.format(getResources().getString(
719 com.android.internal.R.string.activity_resolver_work_profiles_support),
720 ri.activityInfo.loadLabel(getPackageManager()).toString()),
721 Toast.LENGTH_LONG).show();
722 return;
723 }
724
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800725 TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800726 if (target == null) {
727 return;
728 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700729 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800730 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100731 MetricsLogger.action(
732 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800733 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100734 MetricsLogger.action(
735 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
736 } else {
737 MetricsLogger.action(
738 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
739 }
740 MetricsLogger.action(this, mAdapter.hasFilteredItem()
741 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
742 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700743 finish();
744 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746
Adam Powelle49d9392014-07-17 18:45:19 -0700747 /**
748 * Replace me in subclasses!
749 */
arangelovb0802dc2019-10-18 18:03:44 +0100750 @Override // ResolverListCommunicator
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000751 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700752 return defIntent;
753 }
754
arangelovb0802dc2019-10-18 18:03:44 +0100755 @Override // ResolverListCommunicator
756 public void onPostListReady() {
757 setHeader();
758 resetButtonBar();
759 onListRebuilt();
760 }
761
762 protected void onListRebuilt() {
763 int count = mAdapter.getUnfilteredCount();
764 if (count == 1 && mAdapter.getOtherProfile() == null) {
765 // Only one target, so we're a candidate to auto-launch!
766 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
767 if (shouldAutoLaunchSingleChoice(target)) {
768 safelyStartActivity(target);
769 finish();
770 }
771 }
772 }
773
Adam Powell2ed547e2015-04-29 18:45:04 -0700774 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700775 final ResolveInfo ri = target.getResolveInfo();
776 final Intent intent = target != null ? target.getResolvedIntent() : null;
777
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800778 if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800779 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700780 // Build a reasonable intent filter, based on what matched.
781 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200782 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783
Henrik Engström3277cf12014-07-17 12:18:29 +0200784 if (intent.getSelector() != null) {
785 filterIntent = intent.getSelector();
786 } else {
787 filterIntent = intent;
788 }
789
790 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800791 if (action != null) {
792 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200794 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700795 if (categories != null) {
796 for (String cat : categories) {
797 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700800 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801
Adam Powell24428412015-04-01 17:19:56 -0700802 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200803 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700804 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200805 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700806 if (mimeType != null) {
807 try {
808 filter.addDataType(mimeType);
809 } catch (IntentFilter.MalformedMimeTypeException e) {
810 Log.w("ResolverActivity", e);
811 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 }
813 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700814 }
815 if (data != null && data.getScheme() != null) {
816 // We need the data specification if there was no type,
817 // OR if the scheme is not one of our magical "file:"
818 // or "content:" schemes (see IntentFilter for the reason).
819 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
820 || (!"file".equals(data.getScheme())
821 && !"content".equals(data.getScheme()))) {
822 filter.addDataScheme(data.getScheme());
823
824 // Look through the resolved filter to determine which part
825 // of it matched the original Intent.
826 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
827 if (pIt != null) {
828 String ssp = data.getSchemeSpecificPart();
829 while (ssp != null && pIt.hasNext()) {
830 PatternMatcher p = pIt.next();
831 if (p.match(ssp)) {
832 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
833 break;
834 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700835 }
836 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700837 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
838 if (aIt != null) {
839 while (aIt.hasNext()) {
840 IntentFilter.AuthorityEntry a = aIt.next();
841 if (a.match(data) >= 0) {
842 int port = a.getPort();
843 filter.addDataAuthority(a.getHost(),
844 port >= 0 ? Integer.toString(port) : null);
845 break;
846 }
847 }
848 }
849 pIt = ri.filter.pathsIterator();
850 if (pIt != null) {
851 String path = data.getPath();
852 while (path != null && pIt.hasNext()) {
853 PatternMatcher p = pIt.next();
854 if (p.match(path)) {
855 filter.addDataPath(p.getPath(), p.getType());
856 break;
857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859 }
860 }
861 }
862
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700863 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800864 final int N = mAdapter.mUnfilteredResolveList.size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800865 ComponentName[] set;
866 // If we don't add back in the component for forwarding the intent to a managed
867 // profile, the preferred activity may not be updated correctly (as the set of
868 // components we tell it we knew about will have changed).
arangelovb0802dc2019-10-18 18:03:44 +0100869 final boolean needToAddBackProfileForwardingComponent =
870 mAdapter.getOtherProfile() != null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800871 if (!needToAddBackProfileForwardingComponent) {
872 set = new ComponentName[N];
873 } else {
874 set = new ComponentName[N + 1];
875 }
876
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700877 int bestMatch = 0;
878 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800879 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700880 set[i] = new ComponentName(r.activityInfo.packageName,
881 r.activityInfo.name);
882 if (r.match > bestMatch) bestMatch = r.match;
883 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800884
885 if (needToAddBackProfileForwardingComponent) {
arangelovb0802dc2019-10-18 18:03:44 +0100886 set[N] = mAdapter.getOtherProfile().getResolvedComponentName();
887 final int otherProfileMatch = mAdapter.getOtherProfile().getResolveInfo().match;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800888 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
889 }
890
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700891 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700892 final int userId = getUserId();
893 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800894
895 // Set the preferred Activity
896 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
897
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700898 if (ri.handleAllWebDataURI) {
899 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700900 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700901 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700902 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700903 }
904 } else {
905 // Update Domain Verification status
906 ComponentName cn = intent.getComponent();
907 String packageName = cn.getPackageName();
908 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800909
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700910 boolean isHttpOrHttps = (dataScheme != null) &&
911 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
912 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800913
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700914 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
915 boolean hasCategoryBrowsable = (categories != null) &&
916 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800917
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700918 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700919 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700920 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
921 userId);
922 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800923 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700924 } else {
925 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800926 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700927 } catch (RemoteException re) {
928 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
929 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 }
932 }
933
Adam Powell24428412015-04-01 17:19:56 -0700934 if (target != null) {
935 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -0400936
937 // Rely on the ActivityManager to pop up a dialog regarding app suspension
938 // and return false
939 if (target.isSuspended()) {
940 return false;
941 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700942 }
Matt Pietala4b30072019-04-04 13:44:36 -0400943
Adam Powell2ed547e2015-04-29 18:45:04 -0700944 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700945 }
946
Adam Powell23882512016-01-29 10:21:00 -0800947 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -0700948 // We're dispatching intents that might be coming from legacy apps, so
949 // don't kill ourselves.
950 StrictMode.disableDeathOnFileUriExposure();
951 try {
952 safelyStartActivityInternal(cti);
953 } finally {
954 StrictMode.enableDeathOnFileUriExposure();
955 }
956 }
957
958 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +0000959 // If needed, show that intent is forwarded
960 // from managed profile to owner or other way around.
961 if (mProfileSwitchMessageId != -1) {
962 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
963 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700964 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -0700965 if (cti.start(this, null)) {
966 onActivityStarted(cti);
967 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700968 return;
969 }
970 try {
Adam Powell24428412015-04-01 17:19:56 -0700971 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
972 onActivityStarted(cti);
973 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700974 } catch (RuntimeException e) {
975 String launchedFromPackage;
976 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700977 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700978 getActivityToken());
979 } catch (RemoteException e2) {
980 launchedFromPackage = "??";
981 }
982 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
983 + " package " + launchedFromPackage + ", while running in "
984 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 }
987
Alison Cichowlas3e340502018-08-07 17:15:01 -0400988
arangelovb0802dc2019-10-18 18:03:44 +0100989 public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
Alison Cichowlas3e340502018-08-07 17:15:01 -0400990 int userId) {
991 // Pass intent to delegate chooser activity with permission token.
992 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
993 // moves into systemui
994 try {
995 // TODO: Once this is a small springboard activity, it can move off the UI process
996 // and we can move the request method to ActivityManagerInternal.
997 IBinder permissionToken = ActivityTaskManager.getService()
998 .requestStartActivityPermissionToken(getActivityToken());
999 final Intent chooserIntent = new Intent();
1000 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1001 Resources.getSystem().getString(R.string.config_chooserActivity));
1002 chooserIntent.setClassName(delegateActivity.getPackageName(),
1003 delegateActivity.getClassName());
1004 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1005
1006 // TODO: These extras will change as chooser activity moves into systemui
1007 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1008 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1009 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1010 ignoreTargetSecurity);
1011 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001012 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1013 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001014 startActivity(chooserIntent);
1015 } catch (RemoteException e) {
1016 Log.e(TAG, e.toString());
1017 }
1018 return true;
1019 }
1020
Adam Powell23882512016-01-29 10:21:00 -08001021 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001022 // Do nothing
1023 }
1024
arangelovb0802dc2019-10-18 18:03:44 +01001025 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -08001026 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001027 return false;
1028 }
1029
Adam Powell23882512016-01-29 10:21:00 -08001030 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001031 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001032 }
1033
Adam Powell23882512016-01-29 10:21:00 -08001034 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001035 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001036 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001037 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001038 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001039 }
1040
arangelovb0802dc2019-10-18 18:03:44 +01001041 public ResolverListAdapter createAdapter(Context context, List<Intent> payloadIntents,
1042 Intent[] initialIntents, List<ResolveInfo> rList,
1043 boolean filterLastUsed, boolean useLayoutForBrowsables) {
Paul McLean07425c82019-10-18 12:00:11 -06001044
1045 Intent startIntent = getIntent();
1046 boolean isAudioCaptureDevice =
1047 startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
1048
arangelovb0802dc2019-10-18 18:03:44 +01001049 return new ResolverListAdapter(context, payloadIntents, initialIntents, rList,
Paul McLean07425c82019-10-18 12:00:11 -06001050 filterLastUsed, createListController(), useLayoutForBrowsables, this,
1051 isAudioCaptureDevice);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001052 }
1053
1054 @VisibleForTesting
1055 protected ResolverListController createListController() {
1056 return new ResolverListController(
1057 this,
1058 mPm,
1059 getTargetIntent(),
1060 getReferrerPackageName(),
1061 mLaunchedFromUid);
Adam Powell88831a22014-11-20 18:17:00 -08001062 }
1063
Adam Powell39e94eb2015-09-08 17:01:49 -07001064 /**
arangelovb0802dc2019-10-18 18:03:44 +01001065 * Sets up the content view.
Adam Powell39e94eb2015-09-08 17:01:49 -07001066 */
arangelovb0802dc2019-10-18 18:03:44 +01001067 private void configureContentView() {
1068 if (mAdapter == null) {
1069 throw new IllegalStateException("mAdapter cannot be null.");
1070 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001071 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001072 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001073 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001074 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001075 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001076 setContentView(mLayoutId);
arangelovb0802dc2019-10-18 18:03:44 +01001077 mAdapterView = findViewById(R.id.resolver_list);
1078 }
Adam Powell7d758002015-05-06 17:49:36 -07001079
arangelovb0802dc2019-10-18 18:03:44 +01001080 /**
1081 * Returns true if the activity is finishing and creation should halt.
1082 * </p>Subclasses must call rebuildListInternal at the end of rebuildList.
1083 */
1084 protected boolean rebuildList() {
1085 return rebuildListInternal();
1086 }
1087
1088 /**
1089 * Returns true if the activity is finishing and creation should halt.
1090 */
1091 final boolean rebuildListInternal() {
1092 boolean rebuildCompleted = mAdapter.rebuildList();
Adam Powell50077352015-05-26 18:01:55 -07001093 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001094
1095 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1096 // we're already done, we can check if we should auto-launch immediately.
1097 if (rebuildCompleted) {
1098 if (count == 1 && mAdapter.getOtherProfile() == null) {
1099 // Only one target, so we're a candidate to auto-launch!
1100 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
1101 if (shouldAutoLaunchSingleChoice(target)) {
1102 safelyStartActivity(target);
1103 mPackageMonitor.unregister();
1104 mRegistered = false;
1105 finish();
1106 return true;
1107 }
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001108 }
1109 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001110
arangelovb0802dc2019-10-18 18:03:44 +01001111 if (count == 0 && mAdapter.getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001112 final TextView emptyView = findViewById(R.id.empty);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001113 emptyView.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -07001114 mAdapterView.setVisibility(View.GONE);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001115 } else {
1116 mAdapterView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001117 onPrepareAdapterView(mAdapterView, mAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001118 }
Adam Powell39e94eb2015-09-08 17:01:49 -07001119 return false;
Adam Powell7d758002015-05-06 17:49:36 -07001120 }
1121
arangelovb0802dc2019-10-18 18:03:44 +01001122 public void onPrepareAdapterView(AbsListView adapterView, ResolverListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001123 final boolean useHeader = adapter.hasFilteredItem();
1124 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
1125
1126 adapterView.setAdapter(mAdapter);
1127
1128 final ItemClickListener listener = new ItemClickListener();
1129 adapterView.setOnItemClickListener(listener);
1130 adapterView.setOnItemLongClickListener(listener);
1131
shafik69df96322018-12-18 15:41:19 +00001132 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001133 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1134 }
1135
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001136 // In case this method is called again (due to activity recreation), avoid adding a new
1137 // header if one is already present.
1138 if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) {
Matt Pietald5071202019-10-10 11:21:28 -04001139 listView.setHeaderDividersEnabled(true);
Adam Powell7d758002015-05-06 17:49:36 -07001140 listView.addHeaderView(LayoutInflater.from(this).inflate(
1141 R.layout.resolver_different_item_header, listView, false));
1142 }
Adam Powell24428412015-04-01 17:19:56 -07001143 }
1144
Matt Pietal26038402019-01-08 07:29:34 -05001145 /**
1146 * Configure the area above the app selection list (title, content preview, etc).
1147 */
1148 public void setHeader() {
arangelovb0802dc2019-10-18 18:03:44 +01001149 if (mAdapter.getCount() == 0 && mAdapter.getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001150 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001151 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001152 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001153 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001154 }
1155
1156 CharSequence title = mTitle != null
1157 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001158 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001159
1160 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001161 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001162 if (titleView != null) {
1163 titleView.setText(title);
1164 }
1165 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001166 }
1167
Alan Viverette51efddb2017-04-05 10:00:01 -04001168 final ImageView iconView = findViewById(R.id.icon);
arangelovb0802dc2019-10-18 18:03:44 +01001169 if (iconView != null) {
1170 mAdapter.loadFilteredItemIconTaskAsync(iconView);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001171 }
1172 }
1173
shafik69df96322018-12-18 15:41:19 +00001174 private void resetButtonBar() {
1175 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1176 return;
1177 }
1178 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1179 if (buttonLayout != null) {
1180 buttonLayout.setVisibility(View.VISIBLE);
Matt Pietal800136a2019-05-08 07:46:39 -04001181
Matt Pietald5071202019-10-10 11:21:28 -04001182 if (!useLayoutWithDefault()) {
1183 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1184 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
1185 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1186 R.dimen.resolver_button_bar_spacing) + inset);
1187 }
shafik69df96322018-12-18 15:41:19 +00001188 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
shafik69df96322018-12-18 15:41:19 +00001189 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1190
Matt Pietal0d6834a2019-06-27 13:27:52 -04001191 resetAlwaysOrOnceButtonBar();
shafik69df96322018-12-18 15:41:19 +00001192 } else {
1193 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1194 }
1195 }
1196
shafik69df96322018-12-18 15:41:19 +00001197 private void resetAlwaysOrOnceButtonBar() {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001198 if (useLayoutWithDefault()
1199 && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001200 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
1201 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001202 return;
1203 }
1204
1205 // When the items load in, if an item was already selected, enable the buttons
1206 if (mAdapterView != null
1207 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1208 setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true);
1209 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001210 }
1211 }
1212
arangelovb0802dc2019-10-18 18:03:44 +01001213 @Override // ResolverListCommunicator
1214 public boolean useLayoutWithDefault() {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001215 return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem();
1216 }
1217
Adam Powellc412be62015-06-24 13:54:10 -07001218 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001219 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1220 * called and we are launched in a new task.
1221 */
1222 protected void setRetainInOnStop(boolean retainInOnStop) {
1223 mRetainInOnStop = retainInOnStop;
1224 }
1225
1226 /**
Adam Powellc412be62015-06-24 13:54:10 -07001227 * Check a simple match for the component of two ResolveInfos.
1228 */
arangelovb0802dc2019-10-18 18:03:44 +01001229 @Override // ResolverListCommunicator
1230 public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
Adam Powellc412be62015-06-24 13:54:10 -07001231 return lhs == null ? rhs == null
1232 : lhs.activityInfo == null ? rhs.activityInfo == null
1233 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1234 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1235 }
1236
arangelovb0802dc2019-10-18 18:03:44 +01001237 @Override // ResolverListCommunicator
1238 public void onHandlePackagesChanged() {
1239 if (mAdapter.getCount() == 0) {
1240 // We no longer have any items... just finish the activity.
1241 finish();
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001242 }
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001243 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001244
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001245 @VisibleForTesting
1246 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001247 public final ComponentName name;
1248 private final List<Intent> mIntents = new ArrayList<>();
1249 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
1250
1251 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1252 this.name = name;
1253 add(intent, info);
1254 }
1255
1256 public void add(Intent intent, ResolveInfo info) {
1257 mIntents.add(intent);
1258 mResolveInfos.add(info);
1259 }
1260
1261 public int getCount() {
1262 return mIntents.size();
1263 }
1264
1265 public Intent getIntentAt(int index) {
1266 return index >= 0 ? mIntents.get(index) : null;
1267 }
1268
1269 public ResolveInfo getResolveInfoAt(int index) {
1270 return index >= 0 ? mResolveInfos.get(index) : null;
1271 }
1272
1273 public int findIntent(Intent intent) {
1274 for (int i = 0, N = mIntents.size(); i < N; i++) {
1275 if (intent.equals(mIntents.get(i))) {
1276 return i;
1277 }
1278 }
1279 return -1;
1280 }
1281
1282 public int findResolveInfo(ResolveInfo info) {
1283 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1284 if (info.equals(mResolveInfos.get(i))) {
1285 return i;
1286 }
1287 }
1288 return -1;
1289 }
1290 }
1291
Adam Powell7d758002015-05-06 17:49:36 -07001292 class ItemClickListener implements AdapterView.OnItemClickListener,
1293 AdapterView.OnItemLongClickListener {
1294 @Override
1295 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1296 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1297 if (listView != null) {
1298 position -= listView.getHeaderViewsCount();
1299 }
1300 if (position < 0) {
1301 // Header views don't count.
1302 return;
1303 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001304 // If we're still loading, we can't yet enable the buttons.
1305 if (mAdapter.resolveInfoForPosition(position, true) == null) {
1306 return;
1307 }
1308
Adam Powell7d758002015-05-06 17:49:36 -07001309 final int checkedPos = mAdapterView.getCheckedItemPosition();
1310 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001311 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07001312 && (!hasValidSelection || mLastSelected != checkedPos)
Matt Pietal0d6834a2019-06-27 13:27:52 -04001313 && mAlwaysButton != null) {
1314 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Adam Powell7d758002015-05-06 17:49:36 -07001315 mOnceButton.setEnabled(hasValidSelection);
1316 if (hasValidSelection) {
1317 mAdapterView.smoothScrollToPosition(checkedPos);
1318 }
1319 mLastSelected = checkedPos;
1320 } else {
1321 startSelected(position, false, true);
1322 }
1323 }
Adam Powell2d809622012-03-22 15:24:43 -07001324
1325 @Override
1326 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001327 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1328 if (listView != null) {
1329 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001330 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001331 if (position < 0) {
1332 // Header views don't count.
1333 return false;
1334 }
Adam Powell278902c2014-07-12 18:33:22 -07001335 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001336 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001337 return true;
1338 }
1339
1340 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001341
Dianne Hackbornec452d92014-11-11 17:16:56 -08001342 static final boolean isSpecificUriMatch(int match) {
1343 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1344 return match >= IntentFilter.MATCH_CATEGORY_HOST
1345 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1346 }
1347
Adam Powell4c470d62015-06-19 17:46:17 -07001348 static class PickTargetOptionRequest extends PickOptionRequest {
1349 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1350 @Nullable Bundle extras) {
1351 super(prompt, options, extras);
1352 }
1353
1354 @Override
1355 public void onCancel() {
1356 super.onCancel();
1357 final ResolverActivity ra = (ResolverActivity) getActivity();
1358 if (ra != null) {
1359 ra.mPickOptionRequest = null;
1360 ra.finish();
1361 }
1362 }
1363
1364 @Override
1365 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1366 super.onPickOptionResult(finished, selections, result);
1367 if (selections.length != 1) {
1368 // TODO In a better world we would filter the UI presented here and let the
1369 // user refine. Maybe later.
1370 return;
1371 }
1372
1373 final ResolverActivity ra = (ResolverActivity) getActivity();
1374 if (ra != null) {
1375 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
1376 if (ra.onTargetSelected(ti, false)) {
1377 ra.mPickOptionRequest = null;
1378 ra.finish();
1379 }
1380 }
1381 }
1382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383}