blob: c48ce140e549283e9d2672291e038f6d096f4641 [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
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700143 /**
144 * Get the string resource to be used as a label for the link to the resolver activity for an
145 * action.
146 *
147 * @param action The action to resolve
148 *
149 * @return The string resource to be used as a label
150 */
151 public static @StringRes int getLabelRes(String action) {
152 return ActionTitle.forAction(action).labelRes;
153 }
154
Adam Powell278902c2014-07-12 18:33:22 -0700155 private enum ActionTitle {
156 VIEW(Intent.ACTION_VIEW,
157 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700158 com.android.internal.R.string.whichViewApplicationNamed,
159 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700160 EDIT(Intent.ACTION_EDIT,
161 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700162 com.android.internal.R.string.whichEditApplicationNamed,
163 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700164 SEND(Intent.ACTION_SEND,
165 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700166 com.android.internal.R.string.whichSendApplicationNamed,
167 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700168 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700169 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700170 com.android.internal.R.string.whichSendToApplicationNamed,
171 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700172 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
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),
176 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
177 com.android.internal.R.string.whichImageCaptureApplication,
178 com.android.internal.R.string.whichImageCaptureApplicationNamed,
179 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700180 DEFAULT(null,
181 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700182 com.android.internal.R.string.whichApplicationNamed,
183 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700184 HOME(Intent.ACTION_MAIN,
185 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700186 com.android.internal.R.string.whichHomeApplicationNamed,
187 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700188
shafik0c7c5d52019-02-27 12:13:25 +0000189 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000190 public static final int BROWSABLE_TITLE_RES =
Matt Pietal0d6834a2019-06-27 13:27:52 -0400191 com.android.internal.R.string.whichOpenLinksWith;
192 public static final int BROWSABLE_HOST_TITLE_RES =
193 com.android.internal.R.string.whichOpenHostLinksWith;
194 public static final int BROWSABLE_HOST_APP_TITLE_RES =
195 com.android.internal.R.string.whichOpenHostLinksWithApp;
196 public static final int BROWSABLE_APP_TITLE_RES =
197 com.android.internal.R.string.whichOpenLinksWithApp;
shafik69df96322018-12-18 15:41:19 +0000198
Adam Powell278902c2014-07-12 18:33:22 -0700199 public final String action;
200 public final int titleRes;
201 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700202 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700203
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700204 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700205 this.action = action;
206 this.titleRes = titleRes;
207 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700208 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700209 }
210
211 public static ActionTitle forAction(String action) {
212 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700213 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700214 return title;
215 }
216 }
217 return DEFAULT;
218 }
219 }
220
Matt Pietalab73a882019-06-05 07:04:55 -0400221 protected PackageMonitor createPackageMonitor() {
222 return new PackageMonitor() {
223 @Override
224 public void onSomePackagesChanged() {
225 mAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100226 updateProfileViewButton();
Matt Pietalab73a882019-06-05 07:04:55 -0400227 }
228
229 @Override
230 public boolean onPackageChanged(String packageName, int uid, String[] components) {
231 // We care about all package changes, not just the whole package itself which is
232 // default behavior.
233 return true;
234 }
235 };
236 }
237
Dianne Hackborn905577f2011-09-07 18:31:28 -0700238 private Intent makeMyIntent() {
239 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700240 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700241 // The resolver activity is set to be hidden from recent tasks.
242 // we don't want this attribute to be propagated to the next activity
243 // being launched. Note that if the original Intent also had this
244 // flag set, we are now losing it. That should be a very rare case
245 // and we can live with this.
246 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
247 return intent;
248 }
249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 @Override
251 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700252 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700253 final Intent intent = makeMyIntent();
254 final Set<String> categories = intent.getCategories();
255 if (Intent.ACTION_MAIN.equals(intent.getAction())
256 && categories != null
257 && categories.size() == 1
258 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100259 // Note: this field is not set to true in the compatibility version.
260 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700261 }
262
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700263 setSafeForwardingMode(true);
264
Adam Powella35c77a2014-09-25 16:46:36 -0700265 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
267
Adam Powell278902c2014-07-12 18:33:22 -0700268 /**
Adam Powell24428412015-04-01 17:19:56 -0700269 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700270 * a default title resource
271 */
Andrei Onea15884392019-03-22 17:28:11 +0000272 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700274 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800275 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
276 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
277 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700278 }
279
280 protected void onCreate(Bundle savedInstanceState, Intent intent,
281 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800282 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700283 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000285
286 // Determine whether we should show that intent is forwarded
287 // from managed profile to owner or other way around.
288 setProfileSwitchMessageId(intent.getContentUserHint());
289
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700290 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700291 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700292 getActivityToken());
293 } catch (RemoteException e) {
294 mLaunchedFromUid = -1;
295 }
Adam Powell7d758002015-05-06 17:49:36 -0700296
297 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
298 // Gulp!
299 finish();
300 return;
301 }
302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700304
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700305 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700306 mRegistered = true;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800307 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800308
Adam Powell7d758002015-05-06 17:49:36 -0700309 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700310 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800311 mTitle = title;
312 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700313
shafik69df96322018-12-18 15:41:19 +0000314 mUseLayoutForBrowsables = getTargetIntent() == null
315 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000316 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000317
Matt Pietal0d6834a2019-06-27 13:27:52 -0400318 mSupportsAlwaysUseOption = supportsAlwaysUseOption;
shafik69df96322018-12-18 15:41:19 +0000319
arangelovb0802dc2019-10-18 18:03:44 +0100320 // The last argument of createAdapter is whether to do special handling
321 // of the last used choice to highlight it in the list. We need to always
322 // turn this off when running under voice interaction, since it results in
323 // a more complicated UI that the current voice interaction flow is not able
324 // to handle.
325 boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction();
326 mAdapter = createAdapter(this, mIntents, initialIntents, rList,
327 filterLastUsed, mUseLayoutForBrowsables);
arangelovb0802dc2019-10-18 18:03:44 +0100328
arangelovbb572332019-11-13 12:50:08 +0000329 if (configureContentView()) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700330 return;
331 }
Adam Powell278902c2014-07-12 18:33:22 -0700332
Alan Viverette51efddb2017-04-05 10:00:01 -0400333 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700334 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700335 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700336 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700337 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700338 finish();
339 }
340 });
Adam Powell4c470d62015-06-19 17:46:17 -0700341 if (isVoiceInteraction()) {
342 rdl.setCollapsed(false);
343 }
Matt Pietal800136a2019-05-08 07:46:39 -0400344
345 rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
346 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
347 rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets);
348
Adam Powell63b31692015-09-28 10:45:00 -0700349 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700350 }
351
Adam Powell88831a22014-11-20 18:17:00 -0800352 mProfileView = findViewById(R.id.profile_button);
353 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400354 mProfileView.setOnClickListener(this::onProfileClick);
arangelovb0802dc2019-10-18 18:03:44 +0100355 updateProfileViewButton();
Adam Powell88831a22014-11-20 18:17:00 -0800356 }
Adam Powell4c470d62015-06-19 17:46:17 -0700357
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100358 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100359 MetricsLogger.action(this, mAdapter.hasFilteredItem()
360 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
361 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
362 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100363 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700364 }
365
Matt Pietal74c6ed02019-04-18 13:38:46 -0400366 protected void onProfileClick(View v) {
367 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
368 if (dri == null) {
369 return;
370 }
371
372 // Do not show the profile switch message anymore.
373 mProfileSwitchMessageId = -1;
374
375 onTargetSelected(dri, false);
376 finish();
377 }
378
Matt Pietal800136a2019-05-08 07:46:39 -0400379 protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
380 mSystemWindowInsets = insets.getSystemWindowInsets();
381
382 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
383 mSystemWindowInsets.right, 0);
384
Matt Pietald5071202019-10-10 11:21:28 -0400385 // Need extra padding so the list can fully scroll up
386 if (useLayoutWithDefault()) {
387 if (mFooterSpacer == null) {
388 mFooterSpacer = new Space(getApplicationContext());
389 } else {
390 ((ListView) mAdapterView).removeFooterView(mFooterSpacer);
391 }
392 mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
393 mSystemWindowInsets.bottom));
394 ((ListView) mAdapterView).addFooterView(mFooterSpacer);
Matt Pietal800136a2019-05-08 07:46:39 -0400395 } else {
Matt Pietald5071202019-10-10 11:21:28 -0400396 View emptyView = findViewById(R.id.empty);
397 if (emptyView != null) {
398 emptyView.setPadding(0, 0, 0, mSystemWindowInsets.bottom
399 + getResources().getDimensionPixelSize(
400 R.dimen.chooser_edge_margin_normal) * 2);
401 }
Matt Pietal800136a2019-05-08 07:46:39 -0400402 }
Matt Pietal800136a2019-05-08 07:46:39 -0400403
Matt Pietal78fdbd42019-06-05 07:08:08 -0400404 resetButtonBar();
405
Matt Pietal800136a2019-05-08 07:46:39 -0400406 return insets.consumeSystemWindowInsets();
407 }
408
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900409 @Override
410 public void onConfigurationChanged(Configuration newConfig) {
411 super.onConfigurationChanged(newConfig);
412 mAdapter.handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400413
414 if (mSystemWindowInsets != null) {
415 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
416 mSystemWindowInsets.right, 0);
417 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900418 }
419
arangelovb0802dc2019-10-18 18:03:44 +0100420 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -0800421 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700422 if (!isVoiceInteraction()) {
423 // Clearly not needed.
424 return;
425 }
426
Adam Powell4c470d62015-06-19 17:46:17 -0700427 final Option[] options = new Option[mAdapter.getCount()];
428 for (int i = 0, N = options.length; i < N; i++) {
Matt Pietal2cf4c6c2019-09-10 09:33:11 -0400429 TargetInfo target = mAdapter.getItem(i);
430 if (target == null) {
431 // If this occurs, a new set of targets is being loaded. Let that complete,
432 // and have the next call to send voice choices proceed instead.
433 return;
434 }
435 options[i] = optionForChooserTarget(target, i);
Adam Powell4c470d62015-06-19 17:46:17 -0700436 }
437
438 mPickOptionRequest = new PickTargetOptionRequest(
439 new Prompt(getTitle()), options, null);
440 getVoiceInteractor().submitRequest(mPickOptionRequest);
441 }
442
443 Option optionForChooserTarget(TargetInfo target, int index) {
444 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800445 }
446
Adam Powell2ed547e2015-04-29 18:45:04 -0700447 protected final void setAdditionalTargets(Intent[] intents) {
448 if (intents != null) {
449 for (Intent intent : intents) {
450 mIntents.add(intent);
451 }
452 }
453 }
454
arangelovb0802dc2019-10-18 18:03:44 +0100455 @Override // SelectableTargetInfoCommunicator ResolverListCommunicator
Adam Powell0ccc0e92015-04-23 17:19:37 -0700456 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700457 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700458 }
459
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800460 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700461 final Uri referrer = getReferrer();
462 if (referrer != null && "android-app".equals(referrer.getScheme())) {
463 return referrer.getHost();
464 }
465 return null;
466 }
467
Adam Powell23882512016-01-29 10:21:00 -0800468 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700469 return R.layout.resolver_list;
470 }
471
arangelovb0802dc2019-10-18 18:03:44 +0100472 @Override // ResolverListCommunicator
473 public void updateProfileViewButton() {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400474 if (mProfileView == null) {
475 return;
476 }
477
Adam Powell88831a22014-11-20 18:17:00 -0800478 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
479 if (dri != null) {
480 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800481 View text = mProfileView.findViewById(R.id.profile_button);
482 if (!(text instanceof TextView)) {
483 text = mProfileView.findViewById(R.id.text1);
484 }
485 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800486 } else {
487 mProfileView.setVisibility(View.GONE);
488 }
Adam Powell278902c2014-07-12 18:33:22 -0700489 }
490
Sander Alewijnsef6545332014-10-31 12:39:02 +0000491 private void setProfileSwitchMessageId(int contentUserHint) {
492 if (contentUserHint != UserHandle.USER_CURRENT &&
493 contentUserHint != UserHandle.myUserId()) {
494 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
495 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
496 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
497 : false;
498 boolean targetIsManaged = userManager.isManagedProfile();
499 if (originIsManaged && !targetIsManaged) {
500 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
501 } else if (!originIsManaged && targetIsManaged) {
502 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
503 }
504 }
505 }
506
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700507 /**
508 * Turn on launch mode that is safe to use when forwarding intents received from
509 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
510 * instead of the normal Activity.startActivity for launching the activity selected
511 * by the user.
512 *
513 * <p>This mode is set to true by default if the activity is initialized through
514 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
515 * methods, it is set to false by default. You must set it before calling one of the
516 * more detailed onCreate methods, so that it will be set correctly in the case where
517 * there is only one intent to resolve and it is thus started immediately.</p>
518 */
519 public void setSafeForwardingMode(boolean safeForwarding) {
520 mSafeForwardingMode = safeForwarding;
521 }
522
shafik69df96322018-12-18 15:41:19 +0000523 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
524 final ActionTitle title = mResolvingHome
525 ? ActionTitle.HOME
526 : ActionTitle.forAction(intent.getAction());
527
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800528 // While there may already be a filtered item, we can only use it in the title if the list
529 // is already sorted and all information relevant to it is already in the list.
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800530 final boolean named = mAdapter.getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700531 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
532 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000533 } else if (isHttpSchemeAndViewAction(intent)) {
534 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000535 // they're giving access for the activity to open URLs from this specific host
Matt Pietal0d6834a2019-06-27 13:27:52 -0400536 String dialogTitle = null;
537 if (named && !mUseLayoutForBrowsables) {
538 dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES,
539 mAdapter.getFilteredItem().getDisplayLabel());
540 } else if (named && mUseLayoutForBrowsables) {
541 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES,
542 intent.getData().getHost(),
543 mAdapter.getFilteredItem().getDisplayLabel());
544 } else if (mAdapter.areAllTargetsBrowsers()) {
Matt Pietald5071202019-10-10 11:21:28 -0400545 dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES);
Matt Pietal0d6834a2019-06-27 13:27:52 -0400546 } else {
547 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES,
548 intent.getData().getHost());
549 }
550 return dialogTitle;
Adam Powell278902c2014-07-12 18:33:22 -0700551 } else {
Adam Powell24428412015-04-01 17:19:56 -0700552 return named
553 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
554 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700555 }
Adam Powellc5878612012-05-04 18:42:38 -0700556 }
557
Adam Powelle9414d92014-07-05 17:44:18 -0700558 void dismiss() {
559 if (!isFinishing()) {
560 finish();
561 }
562 }
563
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800564 @Override
565 protected void onRestart() {
566 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700567 if (!mRegistered) {
568 mPackageMonitor.register(this, getMainLooper(), false);
569 mRegistered = true;
570 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800571 mAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100572 updateProfileViewButton();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800573 }
574
575 @Override
576 protected void onStop() {
577 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700578 if (mRegistered) {
579 mPackageMonitor.unregister();
580 mRegistered = false;
581 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700582 final Intent intent = getIntent();
583 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100584 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700585 // This resolver is in the unusual situation where it has been
586 // launched at the top of a new task. We don't let it be added
587 // to the recent tasks shown to the user, and we need to make sure
588 // that each time we are launched we get the correct launching
589 // uid (not re-using the same resolver from an old launching uid),
590 // so we will now finish ourself since being no longer visible,
591 // the user probably can't get back to us.
592 if (!isChangingConfigurations()) {
593 finish();
594 }
595 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800596 }
597
Adam Powellc5878612012-05-04 18:42:38 -0700598 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700599 protected void onDestroy() {
600 super.onDestroy();
601 if (!isChangingConfigurations() && mPickOptionRequest != null) {
602 mPickOptionRequest.cancel();
603 }
arangelovb0802dc2019-10-18 18:03:44 +0100604 if (mAdapter != null) {
605 mAdapter.onDestroy();
Kang Li38a6da642017-04-05 12:30:55 -0700606 }
Adam Powell4c470d62015-06-19 17:46:17 -0700607 }
608
609 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700610 protected void onRestoreInstanceState(Bundle savedInstanceState) {
611 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000612 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700613 }
614
shafik0c7c5d52019-02-27 12:13:25 +0000615 private boolean isHttpSchemeAndViewAction(Intent intent) {
616 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
617 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
618 && Intent.ACTION_VIEW.equals(intent.getAction());
619 }
620
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100621 private boolean hasManagedProfile() {
622 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
623 if (userManager == null) {
624 return false;
625 }
626
627 try {
628 List<UserInfo> profiles = userManager.getProfiles(getUserId());
629 for (UserInfo userInfo : profiles) {
630 if (userInfo != null && userInfo.isManagedProfile()) {
631 return true;
632 }
633 }
634 } catch (SecurityException e) {
635 return false;
636 }
637 return false;
638 }
639
640 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
641 try {
642 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
643 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700644 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100645 } catch (NameNotFoundException e) {
646 return false;
647 }
648 }
649
Adam Powell278902c2014-07-12 18:33:22 -0700650 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
651 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100652 boolean enabled = false;
653 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700654 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800655 if (ri == null) {
656 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
657 return;
658 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
659 Log.e(TAG, "Attempted to set selection to resolve info for another user");
660 return;
661 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100662 enabled = true;
663 }
Matt Pietal0d6834a2019-06-27 13:27:52 -0400664 if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) {
665 mAlwaysButton.setText(getResources()
666 .getString(R.string.activity_resolver_set_always));
667 } else {
668 mAlwaysButton.setText(getResources()
669 .getString(R.string.activity_resolver_use_always));
670 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100671 }
672 mAlwaysButton.setEnabled(enabled);
673 }
674
Adam Powellc5878612012-05-04 18:42:38 -0700675 public void onButtonClick(View v) {
676 final int id = v.getId();
shafik69df96322018-12-18 15:41:19 +0000677 int which = mAdapter.hasFilteredItem()
678 ? mAdapter.getFilteredPosition()
679 : mAdapterView.getCheckedItemPosition();
680 boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400681 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Matt Pietala310c1f2019-07-03 09:12:56 -0400682 if (mUseLayoutForBrowsables
683 && !ri.handleAllWebDataURI && id == R.id.button_always) {
Matt Pietal0d6834a2019-06-27 13:27:52 -0400684 showSettingsForSelected(ri);
shafik69df96322018-12-18 15:41:19 +0000685 } else {
686 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
687 }
688 }
689
Matt Pietal0d6834a2019-06-27 13:27:52 -0400690 private void showSettingsForSelected(ResolveInfo ri) {
shafik34064c32019-03-06 15:42:52 +0000691 Intent intent = new Intent();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400692
693 final String packageName = ri.activityInfo.packageName;
694 Bundle showFragmentArgs = new Bundle();
695 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY);
696 showFragmentArgs.putString("package", packageName);
697
shafik34064c32019-03-06 15:42:52 +0000698 // For regular apps, we open the Open by Default page
Matt Pietal0d6834a2019-06-27 13:27:52 -0400699 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
700 .setData(Uri.fromParts("package", packageName, null))
701 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
702 .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY)
703 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
704
shafik34064c32019-03-06 15:42:52 +0000705 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700706 }
707
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800708 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700709 if (isFinishing()) {
710 return;
711 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800712 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000713 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
714 Toast.makeText(this, String.format(getResources().getString(
715 com.android.internal.R.string.activity_resolver_work_profiles_support),
716 ri.activityInfo.loadLabel(getPackageManager()).toString()),
717 Toast.LENGTH_LONG).show();
718 return;
719 }
720
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800721 TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800722 if (target == null) {
723 return;
724 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700725 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800726 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100727 MetricsLogger.action(
728 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800729 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100730 MetricsLogger.action(
731 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
732 } else {
733 MetricsLogger.action(
734 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
735 }
736 MetricsLogger.action(this, mAdapter.hasFilteredItem()
737 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
738 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700739 finish();
740 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
Adam Powelle49d9392014-07-17 18:45:19 -0700743 /**
744 * Replace me in subclasses!
745 */
arangelovb0802dc2019-10-18 18:03:44 +0100746 @Override // ResolverListCommunicator
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000747 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700748 return defIntent;
749 }
750
arangelovb0802dc2019-10-18 18:03:44 +0100751 @Override // ResolverListCommunicator
752 public void onPostListReady() {
753 setHeader();
754 resetButtonBar();
755 onListRebuilt();
756 }
757
758 protected void onListRebuilt() {
759 int count = mAdapter.getUnfilteredCount();
760 if (count == 1 && mAdapter.getOtherProfile() == null) {
761 // Only one target, so we're a candidate to auto-launch!
762 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
763 if (shouldAutoLaunchSingleChoice(target)) {
764 safelyStartActivity(target);
765 finish();
766 }
767 }
768 }
769
Adam Powell2ed547e2015-04-29 18:45:04 -0700770 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700771 final ResolveInfo ri = target.getResolveInfo();
772 final Intent intent = target != null ? target.getResolvedIntent() : null;
773
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800774 if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800775 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700776 // Build a reasonable intent filter, based on what matched.
777 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200778 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779
Henrik Engström3277cf12014-07-17 12:18:29 +0200780 if (intent.getSelector() != null) {
781 filterIntent = intent.getSelector();
782 } else {
783 filterIntent = intent;
784 }
785
786 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800787 if (action != null) {
788 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200790 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700791 if (categories != null) {
792 for (String cat : categories) {
793 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
795 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700796 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797
Adam Powell24428412015-04-01 17:19:56 -0700798 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200799 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700800 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200801 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700802 if (mimeType != null) {
803 try {
804 filter.addDataType(mimeType);
805 } catch (IntentFilter.MalformedMimeTypeException e) {
806 Log.w("ResolverActivity", e);
807 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700810 }
811 if (data != null && data.getScheme() != null) {
812 // We need the data specification if there was no type,
813 // OR if the scheme is not one of our magical "file:"
814 // or "content:" schemes (see IntentFilter for the reason).
815 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
816 || (!"file".equals(data.getScheme())
817 && !"content".equals(data.getScheme()))) {
818 filter.addDataScheme(data.getScheme());
819
820 // Look through the resolved filter to determine which part
821 // of it matched the original Intent.
822 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
823 if (pIt != null) {
824 String ssp = data.getSchemeSpecificPart();
825 while (ssp != null && pIt.hasNext()) {
826 PatternMatcher p = pIt.next();
827 if (p.match(ssp)) {
828 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
829 break;
830 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700831 }
832 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700833 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
834 if (aIt != null) {
835 while (aIt.hasNext()) {
836 IntentFilter.AuthorityEntry a = aIt.next();
837 if (a.match(data) >= 0) {
838 int port = a.getPort();
839 filter.addDataAuthority(a.getHost(),
840 port >= 0 ? Integer.toString(port) : null);
841 break;
842 }
843 }
844 }
845 pIt = ri.filter.pathsIterator();
846 if (pIt != null) {
847 String path = data.getPath();
848 while (path != null && pIt.hasNext()) {
849 PatternMatcher p = pIt.next();
850 if (p.match(path)) {
851 filter.addDataPath(p.getPath(), p.getType());
852 break;
853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
855 }
856 }
857 }
858
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700859 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800860 final int N = mAdapter.mUnfilteredResolveList.size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800861 ComponentName[] set;
862 // If we don't add back in the component for forwarding the intent to a managed
863 // profile, the preferred activity may not be updated correctly (as the set of
864 // components we tell it we knew about will have changed).
arangelovb0802dc2019-10-18 18:03:44 +0100865 final boolean needToAddBackProfileForwardingComponent =
866 mAdapter.getOtherProfile() != null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800867 if (!needToAddBackProfileForwardingComponent) {
868 set = new ComponentName[N];
869 } else {
870 set = new ComponentName[N + 1];
871 }
872
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700873 int bestMatch = 0;
874 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800875 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700876 set[i] = new ComponentName(r.activityInfo.packageName,
877 r.activityInfo.name);
878 if (r.match > bestMatch) bestMatch = r.match;
879 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800880
881 if (needToAddBackProfileForwardingComponent) {
arangelovb0802dc2019-10-18 18:03:44 +0100882 set[N] = mAdapter.getOtherProfile().getResolvedComponentName();
883 final int otherProfileMatch = mAdapter.getOtherProfile().getResolveInfo().match;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800884 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
885 }
886
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700887 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700888 final int userId = getUserId();
889 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800890
891 // Set the preferred Activity
892 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
893
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700894 if (ri.handleAllWebDataURI) {
895 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700896 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700897 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700898 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700899 }
900 } else {
901 // Update Domain Verification status
902 ComponentName cn = intent.getComponent();
903 String packageName = cn.getPackageName();
904 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800905
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700906 boolean isHttpOrHttps = (dataScheme != null) &&
907 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
908 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800909
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700910 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
911 boolean hasCategoryBrowsable = (categories != null) &&
912 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800913
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700914 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700915 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700916 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
917 userId);
918 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800919 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700920 } else {
921 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -0800922 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700923 } catch (RemoteException re) {
924 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
925 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 }
928 }
929
Adam Powell24428412015-04-01 17:19:56 -0700930 if (target != null) {
931 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -0400932
933 // Rely on the ActivityManager to pop up a dialog regarding app suspension
934 // and return false
935 if (target.isSuspended()) {
936 return false;
937 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700938 }
Matt Pietala4b30072019-04-04 13:44:36 -0400939
Adam Powell2ed547e2015-04-29 18:45:04 -0700940 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700941 }
942
Adam Powell23882512016-01-29 10:21:00 -0800943 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -0700944 // We're dispatching intents that might be coming from legacy apps, so
945 // don't kill ourselves.
946 StrictMode.disableDeathOnFileUriExposure();
947 try {
948 safelyStartActivityInternal(cti);
949 } finally {
950 StrictMode.enableDeathOnFileUriExposure();
951 }
952 }
953
954 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +0000955 // If needed, show that intent is forwarded
956 // from managed profile to owner or other way around.
957 if (mProfileSwitchMessageId != -1) {
958 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
959 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700960 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -0700961 if (cti.start(this, null)) {
962 onActivityStarted(cti);
963 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700964 return;
965 }
966 try {
Adam Powell24428412015-04-01 17:19:56 -0700967 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
968 onActivityStarted(cti);
969 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700970 } catch (RuntimeException e) {
971 String launchedFromPackage;
972 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700973 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700974 getActivityToken());
975 } catch (RemoteException e2) {
976 launchedFromPackage = "??";
977 }
978 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
979 + " package " + launchedFromPackage + ", while running in "
980 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
983
Alison Cichowlas3e340502018-08-07 17:15:01 -0400984
arangelovb0802dc2019-10-18 18:03:44 +0100985 public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
Alison Cichowlas3e340502018-08-07 17:15:01 -0400986 int userId) {
987 // Pass intent to delegate chooser activity with permission token.
988 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
989 // moves into systemui
990 try {
991 // TODO: Once this is a small springboard activity, it can move off the UI process
992 // and we can move the request method to ActivityManagerInternal.
993 IBinder permissionToken = ActivityTaskManager.getService()
994 .requestStartActivityPermissionToken(getActivityToken());
995 final Intent chooserIntent = new Intent();
996 final ComponentName delegateActivity = ComponentName.unflattenFromString(
997 Resources.getSystem().getString(R.string.config_chooserActivity));
998 chooserIntent.setClassName(delegateActivity.getPackageName(),
999 delegateActivity.getClassName());
1000 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1001
1002 // TODO: These extras will change as chooser activity moves into systemui
1003 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1004 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1005 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1006 ignoreTargetSecurity);
1007 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001008 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1009 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001010 startActivity(chooserIntent);
1011 } catch (RemoteException e) {
1012 Log.e(TAG, e.toString());
1013 }
1014 return true;
1015 }
1016
Adam Powell23882512016-01-29 10:21:00 -08001017 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001018 // Do nothing
1019 }
1020
arangelovb0802dc2019-10-18 18:03:44 +01001021 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -08001022 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001023 return false;
1024 }
1025
Adam Powell23882512016-01-29 10:21:00 -08001026 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001027 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001028 }
1029
Adam Powell23882512016-01-29 10:21:00 -08001030 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001031 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001032 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001033 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001034 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001035 }
1036
arangelovb0802dc2019-10-18 18:03:44 +01001037 public ResolverListAdapter createAdapter(Context context, List<Intent> payloadIntents,
1038 Intent[] initialIntents, List<ResolveInfo> rList,
1039 boolean filterLastUsed, boolean useLayoutForBrowsables) {
1040 return new ResolverListAdapter(context, payloadIntents, initialIntents, rList,
1041 filterLastUsed, createListController(), useLayoutForBrowsables, this);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001042 }
1043
1044 @VisibleForTesting
1045 protected ResolverListController createListController() {
1046 return new ResolverListController(
1047 this,
1048 mPm,
1049 getTargetIntent(),
1050 getReferrerPackageName(),
1051 mLaunchedFromUid);
Adam Powell88831a22014-11-20 18:17:00 -08001052 }
1053
Adam Powell39e94eb2015-09-08 17:01:49 -07001054 /**
arangelovb0802dc2019-10-18 18:03:44 +01001055 * Sets up the content view.
arangelovbb572332019-11-13 12:50:08 +00001056 * @return <code>true</code> if the activity is finishing and creation should halt.
Adam Powell39e94eb2015-09-08 17:01:49 -07001057 */
arangelovbb572332019-11-13 12:50:08 +00001058 private boolean configureContentView() {
arangelovb0802dc2019-10-18 18:03:44 +01001059 if (mAdapter == null) {
1060 throw new IllegalStateException("mAdapter cannot be null.");
1061 }
arangelovbb572332019-11-13 12:50:08 +00001062 boolean rebuildCompleted = mAdapter.rebuildList();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001063 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001064 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001065 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001066 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001067 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001068 setContentView(mLayoutId);
arangelovb0802dc2019-10-18 18:03:44 +01001069 mAdapterView = findViewById(R.id.resolver_list);
arangelovbb572332019-11-13 12:50:08 +00001070 return postRebuildList(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001071 }
Adam Powell7d758002015-05-06 17:49:36 -07001072
arangelovb0802dc2019-10-18 18:03:44 +01001073 /**
arangelovbb572332019-11-13 12:50:08 +00001074 * Finishing procedures to be performed after the list has been rebuilt.
1075 * </p>Subclasses must call postRebuildListInternal at the end of postRebuildList.
1076 * @param rebuildCompleted
1077 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001078 */
arangelovbb572332019-11-13 12:50:08 +00001079 protected boolean postRebuildList(boolean rebuildCompleted) {
1080 return postRebuildListInternal(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001081 }
1082
1083 /**
arangelovbb572332019-11-13 12:50:08 +00001084 * Finishing procedures to be performed after the list has been rebuilt.
1085 * @param rebuildCompleted
1086 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001087 */
arangelovbb572332019-11-13 12:50:08 +00001088 final boolean postRebuildListInternal(boolean rebuildCompleted) {
1089
Adam Powell50077352015-05-26 18:01:55 -07001090 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001091
1092 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1093 // we're already done, we can check if we should auto-launch immediately.
1094 if (rebuildCompleted) {
1095 if (count == 1 && mAdapter.getOtherProfile() == null) {
1096 // Only one target, so we're a candidate to auto-launch!
1097 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
1098 if (shouldAutoLaunchSingleChoice(target)) {
1099 safelyStartActivity(target);
1100 mPackageMonitor.unregister();
1101 mRegistered = false;
1102 finish();
1103 return true;
1104 }
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001105 }
1106 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001107
arangelovb0802dc2019-10-18 18:03:44 +01001108 if (count == 0 && mAdapter.getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001109 final TextView emptyView = findViewById(R.id.empty);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001110 emptyView.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -07001111 mAdapterView.setVisibility(View.GONE);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001112 } else {
1113 mAdapterView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001114 onPrepareAdapterView(mAdapterView, mAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001115 }
Adam Powell39e94eb2015-09-08 17:01:49 -07001116 return false;
Adam Powell7d758002015-05-06 17:49:36 -07001117 }
1118
arangelovb0802dc2019-10-18 18:03:44 +01001119 public void onPrepareAdapterView(AbsListView adapterView, ResolverListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001120 final boolean useHeader = adapter.hasFilteredItem();
1121 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
1122
1123 adapterView.setAdapter(mAdapter);
1124
1125 final ItemClickListener listener = new ItemClickListener();
1126 adapterView.setOnItemClickListener(listener);
1127 adapterView.setOnItemLongClickListener(listener);
1128
shafik69df96322018-12-18 15:41:19 +00001129 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001130 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1131 }
1132
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001133 // In case this method is called again (due to activity recreation), avoid adding a new
1134 // header if one is already present.
1135 if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) {
Matt Pietald5071202019-10-10 11:21:28 -04001136 listView.setHeaderDividersEnabled(true);
Adam Powell7d758002015-05-06 17:49:36 -07001137 listView.addHeaderView(LayoutInflater.from(this).inflate(
1138 R.layout.resolver_different_item_header, listView, false));
1139 }
Adam Powell24428412015-04-01 17:19:56 -07001140 }
1141
Matt Pietal26038402019-01-08 07:29:34 -05001142 /**
1143 * Configure the area above the app selection list (title, content preview, etc).
1144 */
1145 public void setHeader() {
arangelovb0802dc2019-10-18 18:03:44 +01001146 if (mAdapter.getCount() == 0 && mAdapter.getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001147 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001148 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001149 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001150 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001151 }
1152
1153 CharSequence title = mTitle != null
1154 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001155 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001156
1157 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001158 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001159 if (titleView != null) {
1160 titleView.setText(title);
1161 }
1162 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001163 }
1164
Alan Viverette51efddb2017-04-05 10:00:01 -04001165 final ImageView iconView = findViewById(R.id.icon);
arangelovb0802dc2019-10-18 18:03:44 +01001166 if (iconView != null) {
1167 mAdapter.loadFilteredItemIconTaskAsync(iconView);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001168 }
1169 }
1170
shafik69df96322018-12-18 15:41:19 +00001171 private void resetButtonBar() {
1172 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1173 return;
1174 }
1175 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1176 if (buttonLayout != null) {
1177 buttonLayout.setVisibility(View.VISIBLE);
Matt Pietal800136a2019-05-08 07:46:39 -04001178
Matt Pietald5071202019-10-10 11:21:28 -04001179 if (!useLayoutWithDefault()) {
1180 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1181 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
1182 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1183 R.dimen.resolver_button_bar_spacing) + inset);
1184 }
shafik69df96322018-12-18 15:41:19 +00001185 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
shafik69df96322018-12-18 15:41:19 +00001186 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1187
Matt Pietal0d6834a2019-06-27 13:27:52 -04001188 resetAlwaysOrOnceButtonBar();
shafik69df96322018-12-18 15:41:19 +00001189 } else {
1190 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1191 }
1192 }
1193
shafik69df96322018-12-18 15:41:19 +00001194 private void resetAlwaysOrOnceButtonBar() {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001195 if (useLayoutWithDefault()
1196 && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001197 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
1198 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001199 return;
1200 }
1201
1202 // When the items load in, if an item was already selected, enable the buttons
1203 if (mAdapterView != null
1204 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1205 setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true);
1206 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001207 }
1208 }
1209
arangelovb0802dc2019-10-18 18:03:44 +01001210 @Override // ResolverListCommunicator
1211 public boolean useLayoutWithDefault() {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001212 return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem();
1213 }
1214
Adam Powellc412be62015-06-24 13:54:10 -07001215 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001216 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1217 * called and we are launched in a new task.
1218 */
1219 protected void setRetainInOnStop(boolean retainInOnStop) {
1220 mRetainInOnStop = retainInOnStop;
1221 }
1222
1223 /**
Adam Powellc412be62015-06-24 13:54:10 -07001224 * Check a simple match for the component of two ResolveInfos.
1225 */
arangelovb0802dc2019-10-18 18:03:44 +01001226 @Override // ResolverListCommunicator
1227 public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
Adam Powellc412be62015-06-24 13:54:10 -07001228 return lhs == null ? rhs == null
1229 : lhs.activityInfo == null ? rhs.activityInfo == null
1230 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1231 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1232 }
1233
arangelovb0802dc2019-10-18 18:03:44 +01001234 @Override // ResolverListCommunicator
1235 public void onHandlePackagesChanged() {
1236 if (mAdapter.getCount() == 0) {
1237 // We no longer have any items... just finish the activity.
1238 finish();
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001239 }
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001240 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001241
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001242 @VisibleForTesting
1243 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001244 public final ComponentName name;
1245 private final List<Intent> mIntents = new ArrayList<>();
1246 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
1247
1248 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1249 this.name = name;
1250 add(intent, info);
1251 }
1252
1253 public void add(Intent intent, ResolveInfo info) {
1254 mIntents.add(intent);
1255 mResolveInfos.add(info);
1256 }
1257
1258 public int getCount() {
1259 return mIntents.size();
1260 }
1261
1262 public Intent getIntentAt(int index) {
1263 return index >= 0 ? mIntents.get(index) : null;
1264 }
1265
1266 public ResolveInfo getResolveInfoAt(int index) {
1267 return index >= 0 ? mResolveInfos.get(index) : null;
1268 }
1269
1270 public int findIntent(Intent intent) {
1271 for (int i = 0, N = mIntents.size(); i < N; i++) {
1272 if (intent.equals(mIntents.get(i))) {
1273 return i;
1274 }
1275 }
1276 return -1;
1277 }
1278
1279 public int findResolveInfo(ResolveInfo info) {
1280 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1281 if (info.equals(mResolveInfos.get(i))) {
1282 return i;
1283 }
1284 }
1285 return -1;
1286 }
1287 }
1288
Adam Powell7d758002015-05-06 17:49:36 -07001289 class ItemClickListener implements AdapterView.OnItemClickListener,
1290 AdapterView.OnItemLongClickListener {
1291 @Override
1292 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1293 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1294 if (listView != null) {
1295 position -= listView.getHeaderViewsCount();
1296 }
1297 if (position < 0) {
1298 // Header views don't count.
1299 return;
1300 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001301 // If we're still loading, we can't yet enable the buttons.
1302 if (mAdapter.resolveInfoForPosition(position, true) == null) {
1303 return;
1304 }
1305
Adam Powell7d758002015-05-06 17:49:36 -07001306 final int checkedPos = mAdapterView.getCheckedItemPosition();
1307 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001308 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07001309 && (!hasValidSelection || mLastSelected != checkedPos)
Matt Pietal0d6834a2019-06-27 13:27:52 -04001310 && mAlwaysButton != null) {
1311 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Adam Powell7d758002015-05-06 17:49:36 -07001312 mOnceButton.setEnabled(hasValidSelection);
1313 if (hasValidSelection) {
1314 mAdapterView.smoothScrollToPosition(checkedPos);
1315 }
1316 mLastSelected = checkedPos;
1317 } else {
1318 startSelected(position, false, true);
1319 }
1320 }
Adam Powell2d809622012-03-22 15:24:43 -07001321
1322 @Override
1323 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001324 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1325 if (listView != null) {
1326 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001327 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001328 if (position < 0) {
1329 // Header views don't count.
1330 return false;
1331 }
Adam Powell278902c2014-07-12 18:33:22 -07001332 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001333 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001334 return true;
1335 }
1336
1337 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001338
Dianne Hackbornec452d92014-11-11 17:16:56 -08001339 static final boolean isSpecificUriMatch(int match) {
1340 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1341 return match >= IntentFilter.MATCH_CATEGORY_HOST
1342 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1343 }
1344
Adam Powell4c470d62015-06-19 17:46:17 -07001345 static class PickTargetOptionRequest extends PickOptionRequest {
1346 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1347 @Nullable Bundle extras) {
1348 super(prompt, options, extras);
1349 }
1350
1351 @Override
1352 public void onCancel() {
1353 super.onCancel();
1354 final ResolverActivity ra = (ResolverActivity) getActivity();
1355 if (ra != null) {
1356 ra.mPickOptionRequest = null;
1357 ra.finish();
1358 }
1359 }
1360
1361 @Override
1362 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1363 super.onPickOptionResult(finished, selections, result);
1364 if (selections.length != 1) {
1365 // TODO In a better world we would filter the UI presented here and let the
1366 // user refine. Maybe later.
1367 return;
1368 }
1369
1370 final ResolverActivity ra = (ResolverActivity) getActivity();
1371 if (ra != null) {
1372 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
1373 if (ra.onTargetSelected(ti, false)) {
1374 ra.mPickOptionRequest = null;
1375 ra.finish();
1376 }
1377 }
1378 }
1379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380}