blob: c3743141ffbbd2c44cd5d1695dcfb8c186827687 [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
arangelov7981b122020-01-16 10:58:27 +000019import static android.Manifest.permission.INTERACT_ACROSS_PROFILES;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070020import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
arangelov7981b122020-01-16 10:58:27 +000021import static android.content.PermissionChecker.PID_UNKNOWN;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070022
arangelov38a6fce2019-12-02 18:21:22 +000023import static com.android.internal.app.AbstractMultiProfilePagerAdapter.PROFILE_PERSONAL;
24import static com.android.internal.app.AbstractMultiProfilePagerAdapter.PROFILE_WORK;
25
Adam Powell4c470d62015-06-19 17:46:17 -070026import android.annotation.Nullable;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -070027import android.annotation.StringRes;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080028import android.annotation.UiThread;
Adam Powelle9414d92014-07-05 17:44:18 -070029import android.app.Activity;
arangelov38a6fce2019-12-02 18:21:22 +000030import android.app.ActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070031import android.app.ActivityTaskManager;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070032import android.app.ActivityThread;
Adam Powell4c470d62015-06-19 17:46:17 -070033import android.app.VoiceInteractor.PickOptionRequest;
34import android.app.VoiceInteractor.PickOptionRequest.Option;
35import android.app.VoiceInteractor.Prompt;
arangelov4bf17472020-02-17 20:21:46 +000036import android.app.admin.DevicePolicyEventLogger;
Artur Satayeved5a6ae2019-12-10 17:47:54 +000037import android.compat.annotation.UnsupportedAppUsage;
arangelov7f8743d2020-02-13 20:34:30 +000038import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.ComponentName;
40import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.Intent;
42import android.content.IntentFilter;
arangelov7981b122020-01-16 10:58:27 +000043import android.content.PermissionChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010045import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010049import android.content.pm.UserInfo;
Jiongxuan Gao3365b162016-11-24 20:20:27 +090050import android.content.res.Configuration;
Adam Powellc5878612012-05-04 18:42:38 -070051import android.content.res.Resources;
arangelov4b8ad342020-03-17 13:56:00 +000052import android.content.res.TypedArray;
Matt Pietal800136a2019-05-08 07:46:39 -040053import android.graphics.Insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.net.Uri;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010055import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040057import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.PatternMatcher;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080059import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070060import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070061import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010062import android.os.UserManager;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080063import android.provider.MediaStore;
64import android.provider.Settings;
arangelov4bf17472020-02-17 20:21:46 +000065import android.stats.devicepolicy.DevicePolicyEnums;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080066import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.Log;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080068import android.util.Slog;
arangelov4b8ad342020-03-17 13:56:00 +000069import android.util.TypedValue;
arangelovcf268642020-01-15 15:09:51 +000070import android.view.Gravity;
Adam Powell2d809622012-03-22 15:24:43 -070071import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.View;
73import android.view.ViewGroup;
Matt Pietal800136a2019-05-08 07:46:39 -040074import android.view.ViewGroup.LayoutParams;
75import android.view.WindowInsets;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080076import android.widget.AbsListView;
Adam Powell2d809622012-03-22 15:24:43 -070077import android.widget.AdapterView;
Adam Powellc5878612012-05-04 18:42:38 -070078import android.widget.Button;
arangelovcf268642020-01-15 15:09:51 +000079import android.widget.FrameLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070081import android.widget.ListView;
Matt Pietal800136a2019-05-08 07:46:39 -040082import android.widget.Space;
arangelovcf268642020-01-15 15:09:51 +000083import android.widget.TabHost;
84import android.widget.TabWidget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010086import android.widget.Toast;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070087
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080088import com.android.internal.R;
89import com.android.internal.annotations.VisibleForTesting;
arangelov38a6fce2019-12-02 18:21:22 +000090import com.android.internal.app.AbstractMultiProfilePagerAdapter.Profile;
arangelov4bf17472020-02-17 20:21:46 +000091import com.android.internal.app.chooser.ChooserTargetInfo;
arangelovb0802dc2019-10-18 18:03:44 +010092import com.android.internal.app.chooser.DisplayResolveInfo;
93import com.android.internal.app.chooser.TargetInfo;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080094import com.android.internal.content.PackageMonitor;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010095import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010096import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070097import com.android.internal.widget.ResolverDrawerLayout;
arangelovcf268642020-01-15 15:09:51 +000098import com.android.internal.widget.ViewPager;
Adam Powell2d809622012-03-22 15:24:43 -070099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100101import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.util.Iterator;
103import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -0700104import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.util.Set;
106
arangelov4bf17472020-02-17 20:21:46 +0000107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108/**
109 * This activity is displayed when the system attempts to start an Intent for
110 * which there is more than one matching activity, allowing the user to decide
111 * which to go to. It is not normally used directly by application developers.
112 */
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800113@UiThread
arangelovb0802dc2019-10-18 18:03:44 +0100114public class ResolverActivity extends Activity implements
115 ResolverListAdapter.ResolverListCommunicator {
Alison Cichowlas4691ed42018-11-13 15:59:55 -0500116
Andrei Onea15884392019-03-22 17:28:11 +0000117 @UnsupportedAppUsage
Artur Satayev751e5512019-11-15 19:12:49 +0000118 public ResolverActivity() {
119 }
120
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700121 private boolean mSafeForwardingMode;
Adam Powellc5878612012-05-04 18:42:38 -0700122 private Button mAlwaysButton;
123 private Button mOnceButton;
Matt Pietal74c6ed02019-04-18 13:38:46 -0400124 protected View mProfileView;
Adam Powell24428412015-04-01 17:19:56 -0700125 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100126 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000127 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800128 private int mLayoutId;
arangelovb0802dc2019-10-18 18:03:44 +0100129 @VisibleForTesting
130 protected final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700131 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800132 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800133 private CharSequence mTitle;
134 private int mDefaultTitleResId;
arangelovb0802dc2019-10-18 18:03:44 +0100135
136 @VisibleForTesting
137 protected boolean mUseLayoutForBrowsables;
Adam Powell09a65602014-07-20 16:23:14 -0700138
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800139 // Whether or not this activity supports choosing a default handler for the intent.
arangelovb0802dc2019-10-18 18:03:44 +0100140 @VisibleForTesting
141 protected boolean mSupportsAlwaysUseOption;
Adam Powell63b31692015-09-28 10:45:00 -0700142 protected ResolverDrawerLayout mResolverDrawerLayout;
Andrei Onea15884392019-03-22 17:28:11 +0000143 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800144 protected PackageManager mPm;
145 protected int mLaunchedFromUid;
146
147 private static final String TAG = "ResolverActivity";
148 private static final boolean DEBUG = false;
Adam Powell63b31692015-09-28 10:45:00 -0700149
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700150 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100151
Matt Pietal800136a2019-05-08 07:46:39 -0400152 protected Insets mSystemWindowInsets = null;
153 private Space mFooterSpacer = null;
154
Jorim Jaggif631ef72017-02-24 13:49:47 +0100155 /** See {@link #setRetainInOnStop}. */
156 private boolean mRetainInOnStop;
157
Matt Pietal0d6834a2019-06-27 13:27:52 -0400158 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
159 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
160 private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state";
arangelov4bf17472020-02-17 20:21:46 +0000161 protected static final String METRICS_CATEGORY_RESOLVER = "intent_resolver";
162 protected static final String METRICS_CATEGORY_CHOOSER = "intent_chooser";
Matt Pietal0d6834a2019-06-27 13:27:52 -0400163
arangelov38a6fce2019-12-02 18:21:22 +0000164 /**
165 * TODO(arangelov): Remove a couple of weeks after work/personal tabs are finalized.
166 */
arangelovcf268642020-01-15 15:09:51 +0000167 @VisibleForTesting
arangelov85974ed2020-03-06 15:48:19 +0000168 public static boolean ENABLE_TABBED_VIEW = true;
arangelovcf268642020-01-15 15:09:51 +0000169 private static final String TAB_TAG_PERSONAL = "personal";
170 private static final String TAB_TAG_WORK = "work";
arangelov38a6fce2019-12-02 18:21:22 +0000171
arangelov7981b122020-01-16 10:58:27 +0000172 private PackageMonitor mPersonalPackageMonitor;
arangelovbdb40052020-01-09 18:01:58 +0000173 private PackageMonitor mWorkPackageMonitor;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800174
arangelov38a6fce2019-12-02 18:21:22 +0000175 @VisibleForTesting
176 protected AbstractMultiProfilePagerAdapter mMultiProfilePagerAdapter;
177
Paul McLean07425c82019-10-18 12:00:11 -0600178 // Intent extra for connected audio devices
179 public static final String EXTRA_IS_AUDIO_CAPTURE_DEVICE = "is_audio_capture_device";
180
arangelov7f8743d2020-02-13 20:34:30 +0000181 private BroadcastReceiver mWorkProfileStateReceiver;
182
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700183 /**
184 * Get the string resource to be used as a label for the link to the resolver activity for an
185 * action.
186 *
187 * @param action The action to resolve
188 *
189 * @return The string resource to be used as a label
190 */
191 public static @StringRes int getLabelRes(String action) {
192 return ActionTitle.forAction(action).labelRes;
193 }
194
Adam Powell278902c2014-07-12 18:33:22 -0700195 private enum ActionTitle {
196 VIEW(Intent.ACTION_VIEW,
197 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700198 com.android.internal.R.string.whichViewApplicationNamed,
199 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700200 EDIT(Intent.ACTION_EDIT,
201 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700202 com.android.internal.R.string.whichEditApplicationNamed,
203 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700204 SEND(Intent.ACTION_SEND,
205 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700206 com.android.internal.R.string.whichSendApplicationNamed,
207 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700208 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700209 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700210 com.android.internal.R.string.whichSendToApplicationNamed,
211 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700212 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
213 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700214 com.android.internal.R.string.whichSendApplicationNamed,
215 com.android.internal.R.string.whichSendApplicationLabel),
216 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
217 com.android.internal.R.string.whichImageCaptureApplication,
218 com.android.internal.R.string.whichImageCaptureApplicationNamed,
219 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700220 DEFAULT(null,
221 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700222 com.android.internal.R.string.whichApplicationNamed,
223 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700224 HOME(Intent.ACTION_MAIN,
225 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700226 com.android.internal.R.string.whichHomeApplicationNamed,
227 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700228
shafik0c7c5d52019-02-27 12:13:25 +0000229 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000230 public static final int BROWSABLE_TITLE_RES =
Matt Pietal0d6834a2019-06-27 13:27:52 -0400231 com.android.internal.R.string.whichOpenLinksWith;
232 public static final int BROWSABLE_HOST_TITLE_RES =
233 com.android.internal.R.string.whichOpenHostLinksWith;
234 public static final int BROWSABLE_HOST_APP_TITLE_RES =
235 com.android.internal.R.string.whichOpenHostLinksWithApp;
236 public static final int BROWSABLE_APP_TITLE_RES =
237 com.android.internal.R.string.whichOpenLinksWithApp;
shafik69df96322018-12-18 15:41:19 +0000238
Adam Powell278902c2014-07-12 18:33:22 -0700239 public final String action;
240 public final int titleRes;
241 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700242 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700243
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700244 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700245 this.action = action;
246 this.titleRes = titleRes;
247 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700248 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700249 }
250
251 public static ActionTitle forAction(String action) {
252 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700253 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700254 return title;
255 }
256 }
257 return DEFAULT;
258 }
259 }
260
arangelov7981b122020-01-16 10:58:27 +0000261 protected PackageMonitor createPackageMonitor(ResolverListAdapter listAdapter) {
Matt Pietalab73a882019-06-05 07:04:55 -0400262 return new PackageMonitor() {
263 @Override
264 public void onSomePackagesChanged() {
arangelov7981b122020-01-16 10:58:27 +0000265 listAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100266 updateProfileViewButton();
Matt Pietalab73a882019-06-05 07:04:55 -0400267 }
268
269 @Override
270 public boolean onPackageChanged(String packageName, int uid, String[] components) {
271 // We care about all package changes, not just the whole package itself which is
272 // default behavior.
273 return true;
274 }
275 };
276 }
277
Dianne Hackborn905577f2011-09-07 18:31:28 -0700278 private Intent makeMyIntent() {
279 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700280 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700281 // The resolver activity is set to be hidden from recent tasks.
282 // we don't want this attribute to be propagated to the next activity
283 // being launched. Note that if the original Intent also had this
284 // flag set, we are now losing it. That should be a very rare case
285 // and we can live with this.
286 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
287 return intent;
288 }
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 @Override
291 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700292 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700293 final Intent intent = makeMyIntent();
294 final Set<String> categories = intent.getCategories();
295 if (Intent.ACTION_MAIN.equals(intent.getAction())
296 && categories != null
297 && categories.size() == 1
298 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100299 // Note: this field is not set to true in the compatibility version.
300 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700301 }
302
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700303 setSafeForwardingMode(true);
304
Adam Powella35c77a2014-09-25 16:46:36 -0700305 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307
Adam Powell278902c2014-07-12 18:33:22 -0700308 /**
Adam Powell24428412015-04-01 17:19:56 -0700309 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700310 * a default title resource
311 */
Andrei Onea15884392019-03-22 17:28:11 +0000312 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700314 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800315 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
316 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
317 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700318 }
319
320 protected void onCreate(Bundle savedInstanceState, Intent intent,
321 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800322 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700323 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000325
326 // Determine whether we should show that intent is forwarded
327 // from managed profile to owner or other way around.
328 setProfileSwitchMessageId(intent.getContentUserHint());
329
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700330 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700331 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700332 getActivityToken());
333 } catch (RemoteException e) {
334 mLaunchedFromUid = -1;
335 }
Adam Powell7d758002015-05-06 17:49:36 -0700336
337 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
338 // Gulp!
339 finish();
340 return;
341 }
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700344
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800345 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800346
Adam Powell7d758002015-05-06 17:49:36 -0700347 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700348 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800349 mTitle = title;
350 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700351
shafik69df96322018-12-18 15:41:19 +0000352 mUseLayoutForBrowsables = getTargetIntent() == null
353 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000354 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000355
Matt Pietal0d6834a2019-06-27 13:27:52 -0400356 mSupportsAlwaysUseOption = supportsAlwaysUseOption;
shafik69df96322018-12-18 15:41:19 +0000357
arangelov38a6fce2019-12-02 18:21:22 +0000358 // The last argument of createResolverListAdapter is whether to do special handling
arangelovb0802dc2019-10-18 18:03:44 +0100359 // of the last used choice to highlight it in the list. We need to always
360 // turn this off when running under voice interaction, since it results in
361 // a more complicated UI that the current voice interaction flow is not able
362 // to handle.
363 boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction();
arangelov38a6fce2019-12-02 18:21:22 +0000364 mMultiProfilePagerAdapter = createMultiProfilePagerAdapter(initialIntents, rList, filterLastUsed);
arangelovbb572332019-11-13 12:50:08 +0000365 if (configureContentView()) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700366 return;
367 }
Adam Powell278902c2014-07-12 18:33:22 -0700368
arangelov7981b122020-01-16 10:58:27 +0000369 mPersonalPackageMonitor = createPackageMonitor(
370 mMultiProfilePagerAdapter.getPersonalListAdapter());
371 mPersonalPackageMonitor.register(
372 this, getMainLooper(), getPersonalProfileUserHandle(), false);
arangelovf163a882020-02-18 17:18:47 +0000373 if (shouldShowTabs()) {
arangelov7981b122020-01-16 10:58:27 +0000374 mWorkPackageMonitor = createPackageMonitor(
375 mMultiProfilePagerAdapter.getWorkListAdapter());
376 mWorkPackageMonitor.register(this, getMainLooper(), getWorkProfileUserHandle(), false);
377 }
378
379 mRegistered = true;
380
Alan Viverette51efddb2017-04-05 10:00:01 -0400381 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700382 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700383 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700384 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700385 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700386 finish();
387 }
388 });
Sergey Troshin40e979e2019-12-30 17:29:50 +0100389
390 boolean hasTouchScreen = getPackageManager()
391 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
392
393 if (isVoiceInteraction() || !hasTouchScreen) {
Adam Powell4c470d62015-06-19 17:46:17 -0700394 rdl.setCollapsed(false);
395 }
Matt Pietal800136a2019-05-08 07:46:39 -0400396
397 rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
398 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
399 rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets);
400
arangelovc4dbdbd2020-02-18 20:54:16 +0000401 if (shouldShowTabs() && isIntentPicker()) {
402 rdl.setMaxCollapsedHeight(getResources()
403 .getDimensionPixelSize(R.dimen.resolver_max_collapsed_height_with_tabs));
404 }
405
Adam Powell63b31692015-09-28 10:45:00 -0700406 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700407 }
408
Adam Powell88831a22014-11-20 18:17:00 -0800409 mProfileView = findViewById(R.id.profile_button);
410 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400411 mProfileView.setOnClickListener(this::onProfileClick);
arangelovb0802dc2019-10-18 18:03:44 +0100412 updateProfileViewButton();
Adam Powell88831a22014-11-20 18:17:00 -0800413 }
Adam Powell4c470d62015-06-19 17:46:17 -0700414
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100415 final Set<String> categories = intent.getCategories();
arangelova3912cf2019-12-13 14:34:45 +0000416 MetricsLogger.action(this, mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem()
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100417 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
418 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
419 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100420 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700421 }
422
arangelovc4dbdbd2020-02-18 20:54:16 +0000423 private boolean isIntentPicker() {
424 return getClass().equals(ResolverActivity.class);
425 }
426
arangelov38a6fce2019-12-02 18:21:22 +0000427 protected AbstractMultiProfilePagerAdapter createMultiProfilePagerAdapter(
428 Intent[] initialIntents,
429 List<ResolveInfo> rList,
430 boolean filterLastUsed) {
431 AbstractMultiProfilePagerAdapter resolverMultiProfilePagerAdapter = null;
arangelovf163a882020-02-18 17:18:47 +0000432 if (shouldShowTabs()) {
arangelov38a6fce2019-12-02 18:21:22 +0000433 resolverMultiProfilePagerAdapter =
434 createResolverMultiProfilePagerAdapterForTwoProfiles(
435 initialIntents, rList, filterLastUsed);
436 } else {
437 resolverMultiProfilePagerAdapter = createResolverMultiProfilePagerAdapterForOneProfile(
438 initialIntents, rList, filterLastUsed);
439 }
440 return resolverMultiProfilePagerAdapter;
441 }
442
443 private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForOneProfile(
444 Intent[] initialIntents,
445 List<ResolveInfo> rList, boolean filterLastUsed) {
446 ResolverListAdapter adapter = createResolverListAdapter(
447 /* context */ this,
448 /* payloadIntents */ mIntents,
449 initialIntents,
450 rList,
451 filterLastUsed,
452 mUseLayoutForBrowsables,
453 /* userHandle */ UserHandle.of(UserHandle.myUserId()));
454 return new ResolverMultiProfilePagerAdapter(
455 /* context */ this,
arangelov7981b122020-01-16 10:58:27 +0000456 adapter,
457 getPersonalProfileUserHandle(),
458 /* workProfileUserHandle= */ null);
arangelov38a6fce2019-12-02 18:21:22 +0000459 }
460
461 private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForTwoProfiles(
462 Intent[] initialIntents,
463 List<ResolveInfo> rList,
464 boolean filterLastUsed) {
arangelovcf268642020-01-15 15:09:51 +0000465 // We only show the default app for the profile of the current user. The filterLastUsed
466 // flag determines whether to show a default app and that app is not shown in the
467 // resolver list. So filterLastUsed should be false for the other profile.
arangelov38a6fce2019-12-02 18:21:22 +0000468 ResolverListAdapter personalAdapter = createResolverListAdapter(
469 /* context */ this,
470 /* payloadIntents */ mIntents,
471 initialIntents,
472 rList,
arangelovcf268642020-01-15 15:09:51 +0000473 (filterLastUsed && UserHandle.myUserId()
474 == getPersonalProfileUserHandle().getIdentifier()),
arangelov38a6fce2019-12-02 18:21:22 +0000475 mUseLayoutForBrowsables,
476 /* userHandle */ getPersonalProfileUserHandle());
arangelov7981b122020-01-16 10:58:27 +0000477 UserHandle workProfileUserHandle = getWorkProfileUserHandle();
arangelov38a6fce2019-12-02 18:21:22 +0000478 ResolverListAdapter workAdapter = createResolverListAdapter(
479 /* context */ this,
480 /* payloadIntents */ mIntents,
481 initialIntents,
482 rList,
arangelovcf268642020-01-15 15:09:51 +0000483 (filterLastUsed && UserHandle.myUserId()
arangelov7981b122020-01-16 10:58:27 +0000484 == workProfileUserHandle.getIdentifier()),
arangelov38a6fce2019-12-02 18:21:22 +0000485 mUseLayoutForBrowsables,
arangelov7981b122020-01-16 10:58:27 +0000486 /* userHandle */ workProfileUserHandle);
arangelov38a6fce2019-12-02 18:21:22 +0000487 return new ResolverMultiProfilePagerAdapter(
488 /* context */ this,
489 personalAdapter,
490 workAdapter,
arangelov7981b122020-01-16 10:58:27 +0000491 /* defaultProfile */ getCurrentProfile(),
492 getPersonalProfileUserHandle(),
493 getWorkProfileUserHandle());
arangelov38a6fce2019-12-02 18:21:22 +0000494 }
495
496 protected @Profile int getCurrentProfile() {
497 return (UserHandle.myUserId() == UserHandle.USER_SYSTEM ? PROFILE_PERSONAL : PROFILE_WORK);
498 }
499
500 protected UserHandle getPersonalProfileUserHandle() {
501 return UserHandle.of(ActivityManager.getCurrentUser());
502 }
503 protected @Nullable UserHandle getWorkProfileUserHandle() {
504 UserManager userManager = getSystemService(UserManager.class);
505 for (final UserInfo userInfo : userManager.getProfiles(ActivityManager.getCurrentUser())) {
506 if (userInfo.isManagedProfile()) {
507 return userInfo.getUserHandle();
508 }
509 }
510 return null;
511 }
512
arangelovf163a882020-02-18 17:18:47 +0000513 private boolean hasWorkProfile() {
arangelov38a6fce2019-12-02 18:21:22 +0000514 return getWorkProfileUserHandle() != null;
515 }
516
arangelovf163a882020-02-18 17:18:47 +0000517 protected boolean shouldShowTabs() {
518 return hasWorkProfile() && ENABLE_TABBED_VIEW;
519 }
520
Matt Pietal74c6ed02019-04-18 13:38:46 -0400521 protected void onProfileClick(View v) {
arangelov38a6fce2019-12-02 18:21:22 +0000522 final DisplayResolveInfo dri =
arangelova3912cf2019-12-13 14:34:45 +0000523 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400524 if (dri == null) {
525 return;
526 }
527
528 // Do not show the profile switch message anymore.
529 mProfileSwitchMessageId = -1;
530
531 onTargetSelected(dri, false);
532 finish();
533 }
534
Matt Pietal9236adc2019-12-12 09:24:23 -0500535 /**
536 * Numerous layouts are supported, each with optional ViewGroups.
537 * Make sure the inset gets added to the correct View, using
538 * a footer for Lists so it can properly scroll under the navbar.
539 */
540 protected boolean shouldAddFooterView() {
541 if (useLayoutWithDefault()) return true;
542
543 View buttonBar = findViewById(R.id.button_bar);
544 if (buttonBar == null || buttonBar.getVisibility() == View.GONE) return true;
545
546 return false;
547 }
548
549 protected void applyFooterView(int height) {
550 if (mFooterSpacer == null) {
551 mFooterSpacer = new Space(getApplicationContext());
552 } else {
553 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
arangelovcf268642020-01-15 15:09:51 +0000554 .getActiveAdapterView().removeFooterView(mFooterSpacer);
Matt Pietal9236adc2019-12-12 09:24:23 -0500555 }
556 mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
557 mSystemWindowInsets.bottom));
558 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
arangelovcf268642020-01-15 15:09:51 +0000559 .getActiveAdapterView().addFooterView(mFooterSpacer);
Matt Pietal9236adc2019-12-12 09:24:23 -0500560 }
561
Matt Pietal800136a2019-05-08 07:46:39 -0400562 protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
563 mSystemWindowInsets = insets.getSystemWindowInsets();
564
565 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
566 mSystemWindowInsets.right, 0);
567
Matt Pietal9236adc2019-12-12 09:24:23 -0500568 resetButtonBar();
569
Matt Pietald5071202019-10-10 11:21:28 -0400570 // Need extra padding so the list can fully scroll up
Matt Pietal9236adc2019-12-12 09:24:23 -0500571 if (shouldAddFooterView()) {
572 applyFooterView(mSystemWindowInsets.bottom);
Matt Pietal800136a2019-05-08 07:46:39 -0400573 }
Matt Pietal800136a2019-05-08 07:46:39 -0400574
575 return insets.consumeSystemWindowInsets();
576 }
577
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900578 @Override
579 public void onConfigurationChanged(Configuration newConfig) {
580 super.onConfigurationChanged(newConfig);
arangelova3912cf2019-12-13 14:34:45 +0000581 mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400582
583 if (mSystemWindowInsets != null) {
584 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
585 mSystemWindowInsets.right, 0);
586 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900587 }
588
arangelovb0802dc2019-10-18 18:03:44 +0100589 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -0800590 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700591 if (!isVoiceInteraction()) {
592 // Clearly not needed.
593 return;
594 }
595
arangelova3912cf2019-12-13 14:34:45 +0000596 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getCount();
arangelov38a6fce2019-12-02 18:21:22 +0000597 final Option[] options = new Option[count];
Adam Powell4c470d62015-06-19 17:46:17 -0700598 for (int i = 0, N = options.length; i < N; i++) {
arangelova3912cf2019-12-13 14:34:45 +0000599 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter().getItem(i);
Matt Pietal2cf4c6c2019-09-10 09:33:11 -0400600 if (target == null) {
601 // If this occurs, a new set of targets is being loaded. Let that complete,
602 // and have the next call to send voice choices proceed instead.
603 return;
604 }
605 options[i] = optionForChooserTarget(target, i);
Adam Powell4c470d62015-06-19 17:46:17 -0700606 }
607
608 mPickOptionRequest = new PickTargetOptionRequest(
609 new Prompt(getTitle()), options, null);
610 getVoiceInteractor().submitRequest(mPickOptionRequest);
611 }
612
613 Option optionForChooserTarget(TargetInfo target, int index) {
614 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800615 }
616
Adam Powell2ed547e2015-04-29 18:45:04 -0700617 protected final void setAdditionalTargets(Intent[] intents) {
618 if (intents != null) {
619 for (Intent intent : intents) {
620 mIntents.add(intent);
621 }
622 }
623 }
624
arangelovb0802dc2019-10-18 18:03:44 +0100625 @Override // SelectableTargetInfoCommunicator ResolverListCommunicator
Adam Powell0ccc0e92015-04-23 17:19:37 -0700626 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700627 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700628 }
629
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800630 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700631 final Uri referrer = getReferrer();
632 if (referrer != null && "android-app".equals(referrer.getScheme())) {
633 return referrer.getHost();
634 }
635 return null;
636 }
637
Adam Powell23882512016-01-29 10:21:00 -0800638 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700639 return R.layout.resolver_list;
640 }
641
arangelovb0802dc2019-10-18 18:03:44 +0100642 @Override // ResolverListCommunicator
643 public void updateProfileViewButton() {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400644 if (mProfileView == null) {
645 return;
646 }
647
arangelov38a6fce2019-12-02 18:21:22 +0000648 final DisplayResolveInfo dri =
arangelova3912cf2019-12-13 14:34:45 +0000649 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile();
arangelov2a4d0b12020-03-04 18:15:04 +0000650 if (dri != null && !shouldShowTabs()) {
Adam Powell88831a22014-11-20 18:17:00 -0800651 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800652 View text = mProfileView.findViewById(R.id.profile_button);
653 if (!(text instanceof TextView)) {
654 text = mProfileView.findViewById(R.id.text1);
655 }
656 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800657 } else {
658 mProfileView.setVisibility(View.GONE);
659 }
Adam Powell278902c2014-07-12 18:33:22 -0700660 }
661
Sander Alewijnsef6545332014-10-31 12:39:02 +0000662 private void setProfileSwitchMessageId(int contentUserHint) {
663 if (contentUserHint != UserHandle.USER_CURRENT &&
664 contentUserHint != UserHandle.myUserId()) {
665 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
666 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
667 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
668 : false;
669 boolean targetIsManaged = userManager.isManagedProfile();
670 if (originIsManaged && !targetIsManaged) {
671 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
672 } else if (!originIsManaged && targetIsManaged) {
673 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
674 }
675 }
676 }
677
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700678 /**
679 * Turn on launch mode that is safe to use when forwarding intents received from
680 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
681 * instead of the normal Activity.startActivity for launching the activity selected
682 * by the user.
683 *
684 * <p>This mode is set to true by default if the activity is initialized through
685 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
686 * methods, it is set to false by default. You must set it before calling one of the
687 * more detailed onCreate methods, so that it will be set correctly in the case where
688 * there is only one intent to resolve and it is thus started immediately.</p>
689 */
690 public void setSafeForwardingMode(boolean safeForwarding) {
691 mSafeForwardingMode = safeForwarding;
692 }
693
shafik69df96322018-12-18 15:41:19 +0000694 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
695 final ActionTitle title = mResolvingHome
696 ? ActionTitle.HOME
697 : ActionTitle.forAction(intent.getAction());
698
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800699 // While there may already be a filtered item, we can only use it in the title if the list
700 // is already sorted and all information relevant to it is already in the list.
arangelov38a6fce2019-12-02 18:21:22 +0000701 final boolean named =
arangelova3912cf2019-12-13 14:34:45 +0000702 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700703 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
704 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000705 } else if (isHttpSchemeAndViewAction(intent)) {
706 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000707 // they're giving access for the activity to open URLs from this specific host
Matt Pietal0d6834a2019-06-27 13:27:52 -0400708 String dialogTitle = null;
709 if (named && !mUseLayoutForBrowsables) {
710 dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES,
arangelova3912cf2019-12-13 14:34:45 +0000711 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredItem()
arangelov38a6fce2019-12-02 18:21:22 +0000712 .getDisplayLabel());
Matt Pietal0d6834a2019-06-27 13:27:52 -0400713 } else if (named && mUseLayoutForBrowsables) {
714 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES,
715 intent.getData().getHost(),
arangelova3912cf2019-12-13 14:34:45 +0000716 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredItem()
arangelov38a6fce2019-12-02 18:21:22 +0000717 .getDisplayLabel());
arangelova3912cf2019-12-13 14:34:45 +0000718 } else if (mMultiProfilePagerAdapter.getActiveListAdapter().areAllTargetsBrowsers()) {
Matt Pietald5071202019-10-10 11:21:28 -0400719 dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES);
Matt Pietal0d6834a2019-06-27 13:27:52 -0400720 } else {
721 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES,
722 intent.getData().getHost());
723 }
724 return dialogTitle;
Adam Powell278902c2014-07-12 18:33:22 -0700725 } else {
Adam Powell24428412015-04-01 17:19:56 -0700726 return named
arangelov38a6fce2019-12-02 18:21:22 +0000727 ? getString(title.namedTitleRes, mMultiProfilePagerAdapter
arangelova3912cf2019-12-13 14:34:45 +0000728 .getActiveListAdapter().getFilteredItem().getDisplayLabel())
Adam Powell24428412015-04-01 17:19:56 -0700729 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700730 }
Adam Powellc5878612012-05-04 18:42:38 -0700731 }
732
Adam Powelle9414d92014-07-05 17:44:18 -0700733 void dismiss() {
734 if (!isFinishing()) {
735 finish();
736 }
737 }
738
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800739 @Override
740 protected void onRestart() {
741 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700742 if (!mRegistered) {
arangelovbdb40052020-01-09 18:01:58 +0000743 mPersonalPackageMonitor.register(this, getMainLooper(),
744 getPersonalProfileUserHandle(), false);
arangelovf163a882020-02-18 17:18:47 +0000745 if (shouldShowTabs()) {
arangelovbdb40052020-01-09 18:01:58 +0000746 if (mWorkPackageMonitor == null) {
arangelov7981b122020-01-16 10:58:27 +0000747 mWorkPackageMonitor = createPackageMonitor(
748 mMultiProfilePagerAdapter.getWorkListAdapter());
arangelovbdb40052020-01-09 18:01:58 +0000749 }
750 mWorkPackageMonitor.register(this, getMainLooper(),
751 getWorkProfileUserHandle(), false);
752 }
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700753 mRegistered = true;
754 }
arangelova3912cf2019-12-13 14:34:45 +0000755 mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100756 updateProfileViewButton();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800757 }
758
759 @Override
arangelov7f8743d2020-02-13 20:34:30 +0000760 protected void onStart() {
761 super.onStart();
arangelovf163a882020-02-18 17:18:47 +0000762 if (shouldShowTabs()) {
arangelov7f8743d2020-02-13 20:34:30 +0000763 mWorkProfileStateReceiver = createWorkProfileStateReceiver();
764 registerWorkProfileStateReceiver();
765 }
766 }
767
768 private void registerWorkProfileStateReceiver() {
769 IntentFilter filter = new IntentFilter();
arangelov2dd9f7b2020-02-27 10:54:24 +0000770 filter.addAction(Intent.ACTION_USER_UNLOCKED);
arangelov7f8743d2020-02-13 20:34:30 +0000771 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
772 registerReceiverAsUser(mWorkProfileStateReceiver, UserHandle.ALL, filter, null, null);
773 }
774
775 @Override
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800776 protected void onStop() {
777 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700778 if (mRegistered) {
arangelovbdb40052020-01-09 18:01:58 +0000779 mPersonalPackageMonitor.unregister();
780 if (mWorkPackageMonitor != null) {
781 mWorkPackageMonitor.unregister();
782 }
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700783 mRegistered = false;
784 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700785 final Intent intent = getIntent();
786 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100787 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700788 // This resolver is in the unusual situation where it has been
789 // launched at the top of a new task. We don't let it be added
790 // to the recent tasks shown to the user, and we need to make sure
791 // that each time we are launched we get the correct launching
792 // uid (not re-using the same resolver from an old launching uid),
793 // so we will now finish ourself since being no longer visible,
794 // the user probably can't get back to us.
795 if (!isChangingConfigurations()) {
796 finish();
797 }
798 }
arangelov7f8743d2020-02-13 20:34:30 +0000799 if (mWorkPackageMonitor != null) {
800 unregisterReceiver(mWorkProfileStateReceiver);
801 mWorkPackageMonitor = null;
802 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800803 }
804
Adam Powellc5878612012-05-04 18:42:38 -0700805 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700806 protected void onDestroy() {
807 super.onDestroy();
808 if (!isChangingConfigurations() && mPickOptionRequest != null) {
809 mPickOptionRequest.cancel();
810 }
arangelova3912cf2019-12-13 14:34:45 +0000811 if (mMultiProfilePagerAdapter.getActiveListAdapter() != null) {
812 mMultiProfilePagerAdapter.getActiveListAdapter().onDestroy();
Kang Li38a6da642017-04-05 12:30:55 -0700813 }
Adam Powell4c470d62015-06-19 17:46:17 -0700814 }
815
816 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700817 protected void onRestoreInstanceState(Bundle savedInstanceState) {
818 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000819 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700820 }
821
shafik0c7c5d52019-02-27 12:13:25 +0000822 private boolean isHttpSchemeAndViewAction(Intent intent) {
823 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
824 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
825 && Intent.ACTION_VIEW.equals(intent.getAction());
826 }
827
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100828 private boolean hasManagedProfile() {
829 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
830 if (userManager == null) {
831 return false;
832 }
833
834 try {
835 List<UserInfo> profiles = userManager.getProfiles(getUserId());
836 for (UserInfo userInfo : profiles) {
837 if (userInfo != null && userInfo.isManagedProfile()) {
838 return true;
839 }
840 }
841 } catch (SecurityException e) {
842 return false;
843 }
844 return false;
845 }
846
847 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
848 try {
849 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
850 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700851 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100852 } catch (NameNotFoundException e) {
853 return false;
854 }
855 }
856
Adam Powell278902c2014-07-12 18:33:22 -0700857 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
858 boolean filtered) {
arangelov349af6a2020-03-05 11:53:40 +0000859 if (mMultiProfilePagerAdapter.getCurrentUserHandle() != getUser()) {
860 // Never allow the inactive profile to always open an app.
861 mAlwaysButton.setEnabled(false);
862 return;
863 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100864 boolean enabled = false;
Paul McLean3a30e552019-12-07 14:47:29 -0700865 ResolveInfo ri = null;
Nicolas Prevot50449882014-06-23 12:42:37 +0100866 if (hasValidSelection) {
arangelova3912cf2019-12-13 14:34:45 +0000867 ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +0000868 .resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800869 if (ri == null) {
870 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
871 return;
872 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
873 Log.e(TAG, "Attempted to set selection to resolve info for another user");
874 return;
875 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100876 enabled = true;
877 }
Matt Pietal0d6834a2019-06-27 13:27:52 -0400878 if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) {
879 mAlwaysButton.setText(getResources()
880 .getString(R.string.activity_resolver_set_always));
881 } else {
882 mAlwaysButton.setText(getResources()
883 .getString(R.string.activity_resolver_use_always));
884 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100885 }
Paul McLean3a30e552019-12-07 14:47:29 -0700886
arangelov349af6a2020-03-05 11:53:40 +0000887 if (ri != null) {
888 ActivityInfo activityInfo = ri.activityInfo;
Paul McLean3a30e552019-12-07 14:47:29 -0700889
arangelov349af6a2020-03-05 11:53:40 +0000890 boolean hasRecordPermission =
891 mPm.checkPermission(android.Manifest.permission.RECORD_AUDIO,
892 activityInfo.packageName)
893 == android.content.pm.PackageManager.PERMISSION_GRANTED;
Paul McLean3a30e552019-12-07 14:47:29 -0700894
arangelov349af6a2020-03-05 11:53:40 +0000895 if (!hasRecordPermission) {
896 // OK, we know the record permission, is this a capture device
897 boolean hasAudioCapture =
898 getIntent().getBooleanExtra(
899 ResolverActivity.EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
900 enabled = !hasAudioCapture;
901 }
Paul McLean3a30e552019-12-07 14:47:29 -0700902 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100903 mAlwaysButton.setEnabled(enabled);
904 }
905
Adam Powellc5878612012-05-04 18:42:38 -0700906 public void onButtonClick(View v) {
907 final int id = v.getId();
arangelovcf268642020-01-15 15:09:51 +0000908 ListView listView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelova3912cf2019-12-13 14:34:45 +0000909 ResolverListAdapter currentListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
arangelov38a6fce2019-12-02 18:21:22 +0000910 int which = currentListAdapter.hasFilteredItem()
911 ? currentListAdapter.getFilteredPosition()
912 : listView.getCheckedItemPosition();
913 boolean hasIndexBeenFiltered = !currentListAdapter.hasFilteredItem();
914 ResolveInfo ri = currentListAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Matt Pietala310c1f2019-07-03 09:12:56 -0400915 if (mUseLayoutForBrowsables
916 && !ri.handleAllWebDataURI && id == R.id.button_always) {
Matt Pietal0d6834a2019-06-27 13:27:52 -0400917 showSettingsForSelected(ri);
shafik69df96322018-12-18 15:41:19 +0000918 } else {
919 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
920 }
921 }
922
Matt Pietal0d6834a2019-06-27 13:27:52 -0400923 private void showSettingsForSelected(ResolveInfo ri) {
shafik34064c32019-03-06 15:42:52 +0000924 Intent intent = new Intent();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400925
926 final String packageName = ri.activityInfo.packageName;
927 Bundle showFragmentArgs = new Bundle();
928 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY);
929 showFragmentArgs.putString("package", packageName);
930
shafik34064c32019-03-06 15:42:52 +0000931 // For regular apps, we open the Open by Default page
Matt Pietal0d6834a2019-06-27 13:27:52 -0400932 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
933 .setData(Uri.fromParts("package", packageName, null))
934 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
935 .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY)
936 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
937
shafik34064c32019-03-06 15:42:52 +0000938 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700939 }
940
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800941 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700942 if (isFinishing()) {
943 return;
944 }
arangelova3912cf2019-12-13 14:34:45 +0000945 ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +0000946 .resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000947 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
948 Toast.makeText(this, String.format(getResources().getString(
949 com.android.internal.R.string.activity_resolver_work_profiles_support),
950 ri.activityInfo.loadLabel(getPackageManager()).toString()),
951 Toast.LENGTH_LONG).show();
952 return;
953 }
954
arangelova3912cf2019-12-13 14:34:45 +0000955 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +0000956 .targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800957 if (target == null) {
958 return;
959 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700960 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800961 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100962 MetricsLogger.action(
963 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800964 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100965 MetricsLogger.action(
966 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
967 } else {
968 MetricsLogger.action(
969 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
970 }
arangelov38a6fce2019-12-02 18:21:22 +0000971 MetricsLogger.action(this,
arangelova3912cf2019-12-13 14:34:45 +0000972 mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem()
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100973 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
974 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700975 finish();
976 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978
Adam Powelle49d9392014-07-17 18:45:19 -0700979 /**
980 * Replace me in subclasses!
981 */
arangelovb0802dc2019-10-18 18:03:44 +0100982 @Override // ResolverListCommunicator
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000983 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700984 return defIntent;
985 }
986
arangelovb0802dc2019-10-18 18:03:44 +0100987 @Override // ResolverListCommunicator
arangelov590fba32020-02-11 18:05:42 +0000988 public final void onPostListReady(ResolverListAdapter listAdapter, boolean doPostProcessing) {
arangelov7981b122020-01-16 10:58:27 +0000989 if (isAutolaunching() || maybeAutolaunchActivity()) {
990 return;
arangelovcf268642020-01-15 15:09:51 +0000991 }
arangelov590fba32020-02-11 18:05:42 +0000992 if (shouldShowEmptyState(listAdapter)) {
993 mMultiProfilePagerAdapter.showEmptyState(listAdapter);
arangelov4c0513d2020-03-05 22:24:13 +0000994 } else {
995 mMultiProfilePagerAdapter.showListView(listAdapter);
arangelov590fba32020-02-11 18:05:42 +0000996 }
arangelov7981b122020-01-16 10:58:27 +0000997 if (doPostProcessing) {
998 if (mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier()
999 == UserHandle.myUserId()) {
1000 setHeader();
1001 }
1002 resetButtonBar();
1003 onListRebuilt(listAdapter);
1004 }
arangelovb0802dc2019-10-18 18:03:44 +01001005 }
1006
arangelova3912cf2019-12-13 14:34:45 +00001007 protected void onListRebuilt(ResolverListAdapter listAdapter) {
arangelovcf268642020-01-15 15:09:51 +00001008 final ItemClickListener listener = new ItemClickListener();
1009 setupAdapterListView((ListView) mMultiProfilePagerAdapter.getActiveAdapterView(), listener);
arangelovb0802dc2019-10-18 18:03:44 +01001010 }
1011
Adam Powell2ed547e2015-04-29 18:45:04 -07001012 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -07001013 final ResolveInfo ri = target.getResolveInfo();
1014 final Intent intent = target != null ? target.getResolvedIntent() : null;
1015
arangelov38a6fce2019-12-02 18:21:22 +00001016 if (intent != null && (mSupportsAlwaysUseOption
arangelova3912cf2019-12-13 14:34:45 +00001017 || mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem())
1018 && mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredResolveList() != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001019 // Build a reasonable intent filter, based on what matched.
1020 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +02001021 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022
Henrik Engström3277cf12014-07-17 12:18:29 +02001023 if (intent.getSelector() != null) {
1024 filterIntent = intent.getSelector();
1025 } else {
1026 filterIntent = intent;
1027 }
1028
1029 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001030 if (action != null) {
1031 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 }
Henrik Engström3277cf12014-07-17 12:18:29 +02001033 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001034 if (categories != null) {
1035 for (String cat : categories) {
1036 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001039 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040
Adam Powell24428412015-04-01 17:19:56 -07001041 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +02001042 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001043 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +02001044 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001045 if (mimeType != null) {
1046 try {
1047 filter.addDataType(mimeType);
1048 } catch (IntentFilter.MalformedMimeTypeException e) {
1049 Log.w("ResolverActivity", e);
1050 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001053 }
1054 if (data != null && data.getScheme() != null) {
1055 // We need the data specification if there was no type,
1056 // OR if the scheme is not one of our magical "file:"
1057 // or "content:" schemes (see IntentFilter for the reason).
1058 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
1059 || (!"file".equals(data.getScheme())
1060 && !"content".equals(data.getScheme()))) {
1061 filter.addDataScheme(data.getScheme());
1062
1063 // Look through the resolved filter to determine which part
1064 // of it matched the original Intent.
1065 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
1066 if (pIt != null) {
1067 String ssp = data.getSchemeSpecificPart();
1068 while (ssp != null && pIt.hasNext()) {
1069 PatternMatcher p = pIt.next();
1070 if (p.match(ssp)) {
1071 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
1072 break;
1073 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001074 }
1075 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001076 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
1077 if (aIt != null) {
1078 while (aIt.hasNext()) {
1079 IntentFilter.AuthorityEntry a = aIt.next();
1080 if (a.match(data) >= 0) {
1081 int port = a.getPort();
1082 filter.addDataAuthority(a.getHost(),
1083 port >= 0 ? Integer.toString(port) : null);
1084 break;
1085 }
1086 }
1087 }
1088 pIt = ri.filter.pathsIterator();
1089 if (pIt != null) {
1090 String path = data.getPath();
1091 while (path != null && pIt.hasNext()) {
1092 PatternMatcher p = pIt.next();
1093 if (p.match(path)) {
1094 filter.addDataPath(p.getPath(), p.getType());
1095 break;
1096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098 }
1099 }
1100 }
1101
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001102 if (filter != null) {
arangelova3912cf2019-12-13 14:34:45 +00001103 final int N = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001104 .getUnfilteredResolveList().size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001105 ComponentName[] set;
1106 // If we don't add back in the component for forwarding the intent to a managed
1107 // profile, the preferred activity may not be updated correctly (as the set of
1108 // components we tell it we knew about will have changed).
arangelovb0802dc2019-10-18 18:03:44 +01001109 final boolean needToAddBackProfileForwardingComponent =
arangelova3912cf2019-12-13 14:34:45 +00001110 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001111 if (!needToAddBackProfileForwardingComponent) {
1112 set = new ComponentName[N];
1113 } else {
1114 set = new ComponentName[N + 1];
1115 }
1116
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001117 int bestMatch = 0;
1118 for (int i=0; i<N; i++) {
arangelova3912cf2019-12-13 14:34:45 +00001119 ResolveInfo r = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001120 .getUnfilteredResolveList().get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001121 set[i] = new ComponentName(r.activityInfo.packageName,
1122 r.activityInfo.name);
1123 if (r.match > bestMatch) bestMatch = r.match;
1124 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001125
1126 if (needToAddBackProfileForwardingComponent) {
arangelova3912cf2019-12-13 14:34:45 +00001127 set[N] = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001128 .getOtherProfile().getResolvedComponentName();
arangelova3912cf2019-12-13 14:34:45 +00001129 final int otherProfileMatch = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001130 .getOtherProfile().getResolveInfo().match;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001131 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
1132 }
1133
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001134 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001135 final int userId = getUserId();
1136 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001137
1138 // Set the preferred Activity
1139 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
1140
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001141 if (ri.handleAllWebDataURI) {
1142 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001143 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001144 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001145 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001146 }
1147 } else {
1148 // Update Domain Verification status
1149 ComponentName cn = intent.getComponent();
1150 String packageName = cn.getPackageName();
1151 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001152
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001153 boolean isHttpOrHttps = (dataScheme != null) &&
1154 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
1155 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001156
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001157 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
1158 boolean hasCategoryBrowsable = (categories != null) &&
1159 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001160
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001161 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001162 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001163 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
1164 userId);
1165 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001166 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001167 } else {
1168 try {
arangelova3912cf2019-12-13 14:34:45 +00001169 mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001170 .mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001171 } catch (RemoteException re) {
1172 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1173 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 }
1177
Adam Powell24428412015-04-01 17:19:56 -07001178 if (target != null) {
arangelov9e148522020-01-15 21:11:51 +00001179 if (intent != null) {
1180 intent.fixUris(UserHandle.myUserId());
1181 }
Adam Powell24428412015-04-01 17:19:56 -07001182 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -04001183
1184 // Rely on the ActivityManager to pop up a dialog regarding app suspension
1185 // and return false
1186 if (target.isSuspended()) {
1187 return false;
1188 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001189 }
Matt Pietala4b30072019-04-04 13:44:36 -04001190
Adam Powell2ed547e2015-04-29 18:45:04 -07001191 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001192 }
1193
arangelovcf268642020-01-15 15:09:51 +00001194 @VisibleForTesting
Adam Powell23882512016-01-29 10:21:00 -08001195 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -07001196 // We're dispatching intents that might be coming from legacy apps, so
1197 // don't kill ourselves.
1198 StrictMode.disableDeathOnFileUriExposure();
1199 try {
1200 safelyStartActivityInternal(cti);
1201 } finally {
1202 StrictMode.enableDeathOnFileUriExposure();
1203 }
1204 }
1205
1206 private void safelyStartActivityInternal(TargetInfo cti) {
arangelov96dec882020-01-31 20:23:40 +00001207 if (mPersonalPackageMonitor != null) {
1208 mPersonalPackageMonitor.unregister();
1209 }
arangelov7981b122020-01-16 10:58:27 +00001210 if (mWorkPackageMonitor != null) {
1211 mWorkPackageMonitor.unregister();
1212 }
1213 mRegistered = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +00001214 // If needed, show that intent is forwarded
1215 // from managed profile to owner or other way around.
1216 if (mProfileSwitchMessageId != -1) {
1217 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
1218 }
arangelov38a6fce2019-12-02 18:21:22 +00001219 UserHandle currentUserHandle = mMultiProfilePagerAdapter.getCurrentUserHandle();
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001220 if (!mSafeForwardingMode) {
arangelov38a6fce2019-12-02 18:21:22 +00001221 if (cti.startAsUser(this, null, currentUserHandle)) {
Adam Powell24428412015-04-01 17:19:56 -07001222 onActivityStarted(cti);
arangelov4bf17472020-02-17 20:21:46 +00001223 maybeLogCrossProfileTargetLaunch(cti, currentUserHandle);
Adam Powell24428412015-04-01 17:19:56 -07001224 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001225 return;
1226 }
1227 try {
arangelov38a6fce2019-12-02 18:21:22 +00001228 if (cti.startAsCaller(this, null, currentUserHandle.getIdentifier())) {
Adam Powell24428412015-04-01 17:19:56 -07001229 onActivityStarted(cti);
arangelov4bf17472020-02-17 20:21:46 +00001230 maybeLogCrossProfileTargetLaunch(cti, currentUserHandle);
Adam Powell24428412015-04-01 17:19:56 -07001231 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001232 } catch (RuntimeException e) {
1233 String launchedFromPackage;
1234 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001235 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001236 getActivityToken());
1237 } catch (RemoteException e2) {
1238 launchedFromPackage = "??";
1239 }
1240 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
1241 + " package " + launchedFromPackage + ", while running in "
1242 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
1245
arangelov4bf17472020-02-17 20:21:46 +00001246 private void maybeLogCrossProfileTargetLaunch(TargetInfo cti, UserHandle currentUserHandle) {
arangelove5b369c2020-03-12 17:36:05 +00001247 if (!hasWorkProfile() || currentUserHandle.equals(getUser())) {
arangelov4bf17472020-02-17 20:21:46 +00001248 return;
1249 }
1250 DevicePolicyEventLogger
1251 .createEvent(DevicePolicyEnums.RESOLVER_CROSS_PROFILE_TARGET_OPENED)
arangelove5b369c2020-03-12 17:36:05 +00001252 .setBoolean(currentUserHandle.equals(getPersonalProfileUserHandle()))
arangelov4bf17472020-02-17 20:21:46 +00001253 .setStrings(getMetricsCategory(),
1254 cti instanceof ChooserTargetInfo ? "direct_share" : "other_target")
1255 .write();
1256 }
1257
Alison Cichowlas3e340502018-08-07 17:15:01 -04001258
arangelovb0802dc2019-10-18 18:03:44 +01001259 public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
Alison Cichowlas3e340502018-08-07 17:15:01 -04001260 int userId) {
1261 // Pass intent to delegate chooser activity with permission token.
1262 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
1263 // moves into systemui
1264 try {
1265 // TODO: Once this is a small springboard activity, it can move off the UI process
1266 // and we can move the request method to ActivityManagerInternal.
1267 IBinder permissionToken = ActivityTaskManager.getService()
1268 .requestStartActivityPermissionToken(getActivityToken());
1269 final Intent chooserIntent = new Intent();
1270 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1271 Resources.getSystem().getString(R.string.config_chooserActivity));
1272 chooserIntent.setClassName(delegateActivity.getPackageName(),
1273 delegateActivity.getClassName());
1274 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1275
1276 // TODO: These extras will change as chooser activity moves into systemui
1277 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1278 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1279 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1280 ignoreTargetSecurity);
1281 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001282 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1283 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001284 startActivity(chooserIntent);
1285 } catch (RemoteException e) {
1286 Log.e(TAG, e.toString());
1287 }
1288 return true;
1289 }
1290
Adam Powell23882512016-01-29 10:21:00 -08001291 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001292 // Do nothing
1293 }
1294
arangelovb0802dc2019-10-18 18:03:44 +01001295 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -08001296 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001297 return false;
1298 }
1299
Adam Powell23882512016-01-29 10:21:00 -08001300 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001301 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001302 }
1303
Alison Cichowlas19ee2922019-12-16 19:43:12 -05001304 void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001305 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001306 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001307 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001308 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001309 }
1310
arangelov38a6fce2019-12-02 18:21:22 +00001311 @VisibleForTesting
1312 protected ResolverListAdapter createResolverListAdapter(Context context,
1313 List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList,
1314 boolean filterLastUsed, boolean useLayoutForBrowsables, UserHandle userHandle) {
Paul McLean07425c82019-10-18 12:00:11 -06001315 Intent startIntent = getIntent();
1316 boolean isAudioCaptureDevice =
1317 startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
arangelovb0802dc2019-10-18 18:03:44 +01001318 return new ResolverListAdapter(context, payloadIntents, initialIntents, rList,
arangelov38a6fce2019-12-02 18:21:22 +00001319 filterLastUsed, createListController(userHandle), useLayoutForBrowsables, this,
Paul McLean07425c82019-10-18 12:00:11 -06001320 isAudioCaptureDevice);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001321 }
1322
1323 @VisibleForTesting
arangelov38a6fce2019-12-02 18:21:22 +00001324 protected ResolverListController createListController(UserHandle userHandle) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001325 return new ResolverListController(
1326 this,
1327 mPm,
1328 getTargetIntent(),
1329 getReferrerPackageName(),
arangelov38a6fce2019-12-02 18:21:22 +00001330 mLaunchedFromUid,
1331 userHandle);
Adam Powell88831a22014-11-20 18:17:00 -08001332 }
1333
Adam Powell39e94eb2015-09-08 17:01:49 -07001334 /**
arangelovb0802dc2019-10-18 18:03:44 +01001335 * Sets up the content view.
arangelovbb572332019-11-13 12:50:08 +00001336 * @return <code>true</code> if the activity is finishing and creation should halt.
Adam Powell39e94eb2015-09-08 17:01:49 -07001337 */
arangelovbb572332019-11-13 12:50:08 +00001338 private boolean configureContentView() {
arangelova3912cf2019-12-13 14:34:45 +00001339 if (mMultiProfilePagerAdapter.getActiveListAdapter() == null) {
1340 throw new IllegalStateException("mMultiProfilePagerAdapter.getCurrentListAdapter() "
1341 + "cannot be null.");
arangelovb0802dc2019-10-18 18:03:44 +01001342 }
arangelov7981b122020-01-16 10:58:27 +00001343 // We partially rebuild the inactive adapter to determine if we should auto launch
arangelovb91d08f2020-03-05 21:50:14 +00001344 // isTabLoaded will be true here if the empty state screen is shown instead of the list.
1345 boolean rebuildCompleted = mMultiProfilePagerAdapter.rebuildActiveTab(true)
1346 || mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded();
arangelov2a4d0b12020-03-04 18:15:04 +00001347 if (shouldShowTabs()) {
arangelovb91d08f2020-03-05 21:50:14 +00001348 boolean rebuildInactiveCompleted = mMultiProfilePagerAdapter.rebuildInactiveTab(false)
1349 || mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded();
arangelovdb0c1f12020-02-14 17:35:12 +00001350 rebuildCompleted = rebuildCompleted && rebuildInactiveCompleted;
1351 }
arangelov7981b122020-01-16 10:58:27 +00001352
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001353 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001354 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001355 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001356 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001357 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001358 setContentView(mLayoutId);
arangelov38a6fce2019-12-02 18:21:22 +00001359 mMultiProfilePagerAdapter.setupViewPager(findViewById(R.id.profile_pager));
arangelovdb0c1f12020-02-14 17:35:12 +00001360 return postRebuildList(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001361 }
Adam Powell7d758002015-05-06 17:49:36 -07001362
arangelovb0802dc2019-10-18 18:03:44 +01001363 /**
arangelovbb572332019-11-13 12:50:08 +00001364 * Finishing procedures to be performed after the list has been rebuilt.
1365 * </p>Subclasses must call postRebuildListInternal at the end of postRebuildList.
1366 * @param rebuildCompleted
1367 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001368 */
arangelovbb572332019-11-13 12:50:08 +00001369 protected boolean postRebuildList(boolean rebuildCompleted) {
1370 return postRebuildListInternal(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001371 }
1372
1373 /**
arangelovbb572332019-11-13 12:50:08 +00001374 * Finishing procedures to be performed after the list has been rebuilt.
1375 * @param rebuildCompleted
1376 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001377 */
arangelovbb572332019-11-13 12:50:08 +00001378 final boolean postRebuildListInternal(boolean rebuildCompleted) {
arangelova3912cf2019-12-13 14:34:45 +00001379 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001380
1381 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1382 // we're already done, we can check if we should auto-launch immediately.
arangelov7981b122020-01-16 10:58:27 +00001383 if (rebuildCompleted && maybeAutolaunchActivity()) {
1384 return true;
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001385 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001386
arangelovcf268642020-01-15 15:09:51 +00001387 setupViewVisibilities();
1388
arangelovf163a882020-02-18 17:18:47 +00001389 if (shouldShowTabs()) {
arangelovcf268642020-01-15 15:09:51 +00001390 setupProfileTabs();
1391 }
1392
arangelov38a6fce2019-12-02 18:21:22 +00001393 return false;
1394 }
1395
arangelov7981b122020-01-16 10:58:27 +00001396 private int isPermissionGranted(String permission, int uid) {
1397 return ActivityManager.checkComponentPermission(permission, uid,
1398 /* owningUid= */-1, /* exported= */ true);
1399 }
1400
1401 /**
1402 * @return {@code true} if a resolved target is autolaunched, otherwise {@code false}
1403 */
1404 private boolean maybeAutolaunchActivity() {
1405 int numberOfProfiles = mMultiProfilePagerAdapter.getItemCount();
1406 if (numberOfProfiles == 1 && maybeAutolaunchIfSingleTarget()) {
1407 return true;
arangelovf6986d42020-02-12 15:03:22 +00001408 } else if (numberOfProfiles == 2
arangelovb91d08f2020-03-05 21:50:14 +00001409 && mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded()
1410 && mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded()
arangelovf6986d42020-02-12 15:03:22 +00001411 && (maybeAutolaunchIfNoAppsOnInactiveTab()
1412 || maybeAutolaunchIfCrossProfileSupported())) {
arangelov7981b122020-01-16 10:58:27 +00001413 return true;
1414 }
1415 return false;
1416 }
1417
1418 private boolean maybeAutolaunchIfSingleTarget() {
1419 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1420 if (count != 1) {
1421 return false;
1422 }
1423
arangelovdb0c1f12020-02-14 17:35:12 +00001424 if (mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null) {
1425 return false;
1426 }
1427
arangelov7981b122020-01-16 10:58:27 +00001428 // Only one target, so we're a candidate to auto-launch!
1429 final TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
1430 .targetInfoForPosition(0, false);
1431 if (shouldAutoLaunchSingleChoice(target)) {
1432 safelyStartActivity(target);
1433 finish();
1434 return true;
1435 }
1436 return false;
1437 }
1438
arangelovf6986d42020-02-12 15:03:22 +00001439 private boolean maybeAutolaunchIfNoAppsOnInactiveTab() {
1440 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1441 if (count != 1) {
1442 return false;
1443 }
1444 ResolverListAdapter inactiveListAdapter =
1445 mMultiProfilePagerAdapter.getInactiveListAdapter();
1446 if (inactiveListAdapter.getUnfilteredCount() != 0) {
1447 return false;
1448 }
1449 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
1450 .targetInfoForPosition(0, false);
1451 safelyStartActivity(target);
1452 finish();
1453 return true;
1454 }
1455
arangelov7981b122020-01-16 10:58:27 +00001456 /**
1457 * When we have a personal and a work profile, we auto launch in the following scenario:
1458 * - There is 1 resolved target on each profile
1459 * - That target is the same app on both profiles
1460 * - The target app has permission to communicate cross profiles
1461 * - The target app has declared it supports cross-profile communication via manifest metadata
1462 */
1463 private boolean maybeAutolaunchIfCrossProfileSupported() {
arangelov4bf17472020-02-17 20:21:46 +00001464 ResolverListAdapter activeListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
1465 int count = activeListAdapter.getUnfilteredCount();
arangelov7981b122020-01-16 10:58:27 +00001466 if (count != 1) {
1467 return false;
1468 }
1469 ResolverListAdapter inactiveListAdapter =
1470 mMultiProfilePagerAdapter.getInactiveListAdapter();
1471 if (inactiveListAdapter.getUnfilteredCount() != 1) {
1472 return false;
1473 }
arangelov4bf17472020-02-17 20:21:46 +00001474 TargetInfo activeProfileTarget = activeListAdapter
arangelov7981b122020-01-16 10:58:27 +00001475 .targetInfoForPosition(0, false);
1476 TargetInfo inactiveProfileTarget = inactiveListAdapter.targetInfoForPosition(0, false);
1477 if (!Objects.equals(activeProfileTarget.getResolvedComponentName(),
1478 inactiveProfileTarget.getResolvedComponentName())) {
1479 return false;
1480 }
1481 if (!shouldAutoLaunchSingleChoice(activeProfileTarget)) {
1482 return false;
1483 }
1484 String packageName = activeProfileTarget.getResolvedComponentName().getPackageName();
1485 if (!canAppInteractCrossProfiles(packageName)) {
1486 return false;
1487 }
1488
arangelov4bf17472020-02-17 20:21:46 +00001489 DevicePolicyEventLogger
1490 .createEvent(DevicePolicyEnums.RESOLVER_AUTOLAUNCH_CROSS_PROFILE_TARGET)
arangelove5b369c2020-03-12 17:36:05 +00001491 .setBoolean(activeListAdapter.getUserHandle()
1492 .equals(getPersonalProfileUserHandle()))
arangelov4bf17472020-02-17 20:21:46 +00001493 .setStrings(getMetricsCategory())
1494 .write();
arangelov7981b122020-01-16 10:58:27 +00001495 safelyStartActivity(activeProfileTarget);
1496 finish();
1497 return true;
1498 }
1499
1500 /**
1501 * Returns whether the package has the necessary permissions to interact across profiles on
1502 * behalf of a given user.
1503 *
1504 * <p>This means meeting the following condition:
1505 * <ul>
1506 * <li>The app's {@link ApplicationInfo#crossProfile} flag must be true, and at least
1507 * one of the following conditions must be fulfilled</li>
1508 * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS_FULL} granted.</li>
1509 * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS} granted.</li>
1510 * <li>{@code Manifest.permission.INTERACT_ACROSS_PROFILES} granted, or the corresponding
1511 * AppOps {@code android:interact_across_profiles} is set to "allow".</li>
1512 * </ul>
1513 *
1514 */
1515 private boolean canAppInteractCrossProfiles(String packageName) {
1516 ApplicationInfo applicationInfo;
1517 try {
1518 applicationInfo = getPackageManager().getApplicationInfo(packageName, 0);
1519 } catch (NameNotFoundException e) {
1520 Log.e(TAG, "Package " + packageName + " does not exist on current user.");
1521 return false;
1522 }
1523 if (!applicationInfo.crossProfile) {
1524 return false;
1525 }
1526
1527 int packageUid = applicationInfo.uid;
1528
1529 if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1530 packageUid) == PackageManager.PERMISSION_GRANTED) {
1531 return true;
1532 }
1533 if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS, packageUid)
1534 == PackageManager.PERMISSION_GRANTED) {
1535 return true;
1536 }
1537 if (PermissionChecker.checkPermissionForPreflight(this, INTERACT_ACROSS_PROFILES,
1538 PID_UNKNOWN, packageUid, packageName) == PackageManager.PERMISSION_GRANTED) {
1539 return true;
1540 }
1541 return false;
1542 }
1543
1544 private boolean isAutolaunching() {
1545 return !mRegistered && isFinishing();
1546 }
1547
arangelovcf268642020-01-15 15:09:51 +00001548 private void setupProfileTabs() {
1549 TabHost tabHost = findViewById(R.id.profile_tabhost);
1550 tabHost.setup();
1551 ViewPager viewPager = findViewById(R.id.profile_pager);
1552 TabHost.TabSpec tabSpec = tabHost.newTabSpec(TAB_TAG_PERSONAL)
1553 .setContent(R.id.profile_pager)
1554 .setIndicator(getString(R.string.resolver_personal_tab));
1555 tabHost.addTab(tabSpec);
1556
1557 tabSpec = tabHost.newTabSpec(TAB_TAG_WORK)
1558 .setContent(R.id.profile_pager)
1559 .setIndicator(getString(R.string.resolver_work_tab));
1560 tabHost.addTab(tabSpec);
1561
1562 TabWidget tabWidget = tabHost.getTabWidget();
1563 tabWidget.setVisibility(View.VISIBLE);
1564 resetTabsHeaderStyle(tabWidget);
1565 updateActiveTabStyle(tabHost);
1566
1567 tabHost.setOnTabChangedListener(tabId -> {
1568 resetTabsHeaderStyle(tabWidget);
1569 updateActiveTabStyle(tabHost);
1570 if (TAB_TAG_PERSONAL.equals(tabId)) {
1571 viewPager.setCurrentItem(0);
1572 } else {
1573 viewPager.setCurrentItem(1);
1574 }
1575 setupViewVisibilities();
arangelov4bf17472020-02-17 20:21:46 +00001576 DevicePolicyEventLogger
1577 .createEvent(DevicePolicyEnums.RESOLVER_SWITCH_TABS)
1578 .setInt(viewPager.getCurrentItem())
1579 .setStrings(getMetricsCategory())
1580 .write();
arangelovcf268642020-01-15 15:09:51 +00001581 });
1582
1583 viewPager.setVisibility(View.VISIBLE);
1584 tabHost.setCurrentTab(mMultiProfilePagerAdapter.getCurrentPage());
arangelov349af6a2020-03-05 11:53:40 +00001585 mMultiProfilePagerAdapter.setOnProfileSelectedListener(
1586 index -> {
1587 tabHost.setCurrentTab(index);
1588 resetButtonBar();
1589 resetCheckedItem();
1590 });
arangelov934c64c2020-02-14 13:48:23 +00001591 findViewById(R.id.resolver_tab_divider).setVisibility(View.VISIBLE);
arangelovcf268642020-01-15 15:09:51 +00001592 }
1593
arangelov349af6a2020-03-05 11:53:40 +00001594 private void resetCheckedItem() {
1595 if (!isIntentPicker()) {
1596 return;
1597 }
1598 mLastSelected = ListView.INVALID_POSITION;
1599 ListView inactiveListView = (ListView) mMultiProfilePagerAdapter.getInactiveAdapterView();
1600 if (inactiveListView.getCheckedItemCount() > 0) {
1601 inactiveListView.setItemChecked(inactiveListView.getCheckedItemPosition(), false);
1602 }
1603 }
1604
arangelovcf268642020-01-15 15:09:51 +00001605 private void resetTabsHeaderStyle(TabWidget tabWidget) {
arangelov78fd3602020-02-28 15:21:20 +00001606 String workContentDescription = getString(R.string.resolver_work_tab_accessibility);
1607 String personalContentDescription = getString(R.string.resolver_personal_tab_accessibility);
arangelovcf268642020-01-15 15:09:51 +00001608 for (int i = 0; i < tabWidget.getChildCount(); i++) {
arangelov78fd3602020-02-28 15:21:20 +00001609 View tabView = tabWidget.getChildAt(i);
1610 TextView title = tabView.findViewById(android.R.id.title);
arangelov4b8ad342020-03-17 13:56:00 +00001611 title.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_DialogWindowTitle);
1612 title.setTextColor(getAttrColor(this, android.R.attr.textColorTertiary));
1613 title.setTextSize(TypedValue.COMPLEX_UNIT_PX,
1614 getResources().getDimension(R.dimen.resolver_tab_text_size));
arangelov78fd3602020-02-28 15:21:20 +00001615 if (title.getText().equals(getString(R.string.resolver_personal_tab))) {
1616 tabView.setContentDescription(personalContentDescription);
1617 } else if (title.getText().equals(getString(R.string.resolver_work_tab))) {
1618 tabView.setContentDescription(workContentDescription);
1619 }
arangelovcf268642020-01-15 15:09:51 +00001620 }
1621 }
1622
arangelov4b8ad342020-03-17 13:56:00 +00001623 private static int getAttrColor(Context context, int attr) {
1624 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
1625 int colorAccent = ta.getColor(0, 0);
1626 ta.recycle();
1627 return colorAccent;
1628 }
1629
arangelovcf268642020-01-15 15:09:51 +00001630 private void updateActiveTabStyle(TabHost tabHost) {
1631 TextView title = tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
1632 .findViewById(android.R.id.title);
arangelov4b8ad342020-03-17 13:56:00 +00001633 title.setTextColor(getAttrColor(this, android.R.attr.colorAccent));
arangelovcf268642020-01-15 15:09:51 +00001634 }
1635
1636 private void setupViewVisibilities() {
arangelov590fba32020-02-11 18:05:42 +00001637 ResolverListAdapter activeListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
1638 if (!shouldShowEmptyState(activeListAdapter)) {
1639 addUseDifferentAppLabelIfNecessary(activeListAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001640 }
1641 }
1642
arangelov590fba32020-02-11 18:05:42 +00001643 private boolean shouldShowEmptyState(ResolverListAdapter listAdapter) {
1644 int count = listAdapter.getUnfilteredCount();
1645 return count == 0 && listAdapter.getPlaceholderCount() == 0;
1646 }
1647
Zhen Zhangbde7b462019-11-11 11:49:33 -08001648 /**
arangelovcf268642020-01-15 15:09:51 +00001649 * Add a label to signify that the user can pick a different app.
Zhen Zhangbde7b462019-11-11 11:49:33 -08001650 * @param adapter The adapter used to provide data to item views.
Zhen Zhangbde7b462019-11-11 11:49:33 -08001651 */
arangelovcf268642020-01-15 15:09:51 +00001652 public void addUseDifferentAppLabelIfNecessary(ResolverListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001653 final boolean useHeader = adapter.hasFilteredItem();
arangelovcf268642020-01-15 15:09:51 +00001654 if (useHeader) {
1655 FrameLayout stub = findViewById(R.id.stub);
1656 stub.setVisibility(View.VISIBLE);
1657 TextView textView = (TextView) LayoutInflater.from(this).inflate(
1658 R.layout.resolver_different_item_header, null, false);
arangelovf163a882020-02-18 17:18:47 +00001659 if (shouldShowTabs()) {
arangelovcf268642020-01-15 15:09:51 +00001660 textView.setGravity(Gravity.CENTER);
1661 }
1662 stub.addView(textView);
1663 }
1664 }
1665
1666 private void setupAdapterListView(ListView listView, ItemClickListener listener) {
arangelov38a6fce2019-12-02 18:21:22 +00001667 listView.setOnItemClickListener(listener);
1668 listView.setOnItemLongClickListener(listener);
Adam Powell7d758002015-05-06 17:49:36 -07001669
shafik69df96322018-12-18 15:41:19 +00001670 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001671 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1672 }
Adam Powell24428412015-04-01 17:19:56 -07001673 }
1674
Matt Pietal26038402019-01-08 07:29:34 -05001675 /**
1676 * Configure the area above the app selection list (title, content preview, etc).
1677 */
1678 public void setHeader() {
arangelova3912cf2019-12-13 14:34:45 +00001679 if (mMultiProfilePagerAdapter.getActiveListAdapter().getCount() == 0
1680 && mMultiProfilePagerAdapter.getActiveListAdapter().getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001681 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001682 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001683 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001684 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001685 }
1686
1687 CharSequence title = mTitle != null
1688 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001689 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001690
1691 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001692 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001693 if (titleView != null) {
1694 titleView.setText(title);
1695 }
1696 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001697 }
1698
Alan Viverette51efddb2017-04-05 10:00:01 -04001699 final ImageView iconView = findViewById(R.id.icon);
arangelovb0802dc2019-10-18 18:03:44 +01001700 if (iconView != null) {
arangelova3912cf2019-12-13 14:34:45 +00001701 mMultiProfilePagerAdapter.getActiveListAdapter().loadFilteredItemIconTaskAsync(iconView);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001702 }
1703 }
1704
arangelov38a6fce2019-12-02 18:21:22 +00001705 protected void resetButtonBar() {
shafik69df96322018-12-18 15:41:19 +00001706 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1707 return;
1708 }
1709 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1710 if (buttonLayout != null) {
1711 buttonLayout.setVisibility(View.VISIBLE);
Matt Pietal800136a2019-05-08 07:46:39 -04001712
Matt Pietald5071202019-10-10 11:21:28 -04001713 if (!useLayoutWithDefault()) {
1714 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1715 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
1716 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1717 R.dimen.resolver_button_bar_spacing) + inset);
1718 }
shafik69df96322018-12-18 15:41:19 +00001719 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
shafik69df96322018-12-18 15:41:19 +00001720 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1721
Matt Pietal0d6834a2019-06-27 13:27:52 -04001722 resetAlwaysOrOnceButtonBar();
shafik69df96322018-12-18 15:41:19 +00001723 } else {
1724 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1725 }
1726 }
1727
shafik69df96322018-12-18 15:41:19 +00001728 private void resetAlwaysOrOnceButtonBar() {
arangelov349af6a2020-03-05 11:53:40 +00001729 // Disable both buttons initially
1730 setAlwaysButtonEnabled(false, ListView.INVALID_POSITION, false);
1731 mOnceButton.setEnabled(false);
1732
arangelova3912cf2019-12-13 14:34:45 +00001733 int filteredPosition = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001734 .getFilteredPosition();
1735 if (useLayoutWithDefault() && filteredPosition != ListView.INVALID_POSITION) {
1736 setAlwaysButtonEnabled(true, filteredPosition, false);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001737 mOnceButton.setEnabled(true);
Sergey Troshin40e979e2019-12-30 17:29:50 +01001738 // Focus the button if we already have the default option
1739 mOnceButton.requestFocus();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001740 return;
1741 }
1742
1743 // When the items load in, if an item was already selected, enable the buttons
arangelovcf268642020-01-15 15:09:51 +00001744 ListView currentAdapterView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelov38a6fce2019-12-02 18:21:22 +00001745 if (currentAdapterView != null
1746 && currentAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1747 setAlwaysButtonEnabled(true, currentAdapterView.getCheckedItemPosition(), true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001748 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001749 }
1750 }
1751
arangelovb0802dc2019-10-18 18:03:44 +01001752 @Override // ResolverListCommunicator
1753 public boolean useLayoutWithDefault() {
arangelovcf268642020-01-15 15:09:51 +00001754 // We only use the default app layout when the profile of the active user has a
1755 // filtered item. We always show the same default app even in the inactive user profile.
1756 boolean currentUserAdapterHasFilteredItem;
1757 if (mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier()
1758 == UserHandle.myUserId()) {
1759 currentUserAdapterHasFilteredItem =
1760 mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem();
1761 } else {
1762 currentUserAdapterHasFilteredItem =
1763 mMultiProfilePagerAdapter.getInactiveListAdapter().hasFilteredItem();
1764 }
1765 return mSupportsAlwaysUseOption && currentUserAdapterHasFilteredItem;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001766 }
1767
Adam Powellc412be62015-06-24 13:54:10 -07001768 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001769 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1770 * called and we are launched in a new task.
1771 */
1772 protected void setRetainInOnStop(boolean retainInOnStop) {
1773 mRetainInOnStop = retainInOnStop;
1774 }
1775
1776 /**
Adam Powellc412be62015-06-24 13:54:10 -07001777 * Check a simple match for the component of two ResolveInfos.
1778 */
arangelovb0802dc2019-10-18 18:03:44 +01001779 @Override // ResolverListCommunicator
1780 public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
Adam Powellc412be62015-06-24 13:54:10 -07001781 return lhs == null ? rhs == null
1782 : lhs.activityInfo == null ? rhs.activityInfo == null
1783 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1784 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1785 }
1786
arangelov4bf17472020-02-17 20:21:46 +00001787 protected String getMetricsCategory() {
1788 return METRICS_CATEGORY_RESOLVER;
1789 }
1790
arangelovb0802dc2019-10-18 18:03:44 +01001791 @Override // ResolverListCommunicator
arangelov7981b122020-01-16 10:58:27 +00001792 public void onHandlePackagesChanged(ResolverListAdapter listAdapter) {
1793 if (listAdapter == mMultiProfilePagerAdapter.getActiveListAdapter()) {
arangelove5b369c2020-03-12 17:36:05 +00001794 if (listAdapter.getUserHandle().equals(getWorkProfileUserHandle())
arangelov2dd9f7b2020-02-27 10:54:24 +00001795 && mMultiProfilePagerAdapter.isWaitingToEnableWorkProfile()) {
1796 // We have just turned on the work profile and entered the pass code to start it,
1797 // now we are waiting to receive the ACTION_USER_UNLOCKED broadcast. There is no
1798 // point in reloading the list now, since the work profile user is still
1799 // turning on.
1800 return;
1801 }
arangelov7981b122020-01-16 10:58:27 +00001802 boolean listRebuilt = mMultiProfilePagerAdapter.rebuildActiveTab(true);
1803 if (listRebuilt) {
1804 ResolverListAdapter activeListAdapter =
1805 mMultiProfilePagerAdapter.getActiveListAdapter();
1806 activeListAdapter.notifyDataSetChanged();
1807 if (activeListAdapter.getCount() == 0) {
1808 // We no longer have any items... just finish the activity.
1809 finish();
1810 }
1811 }
1812 } else {
1813 mMultiProfilePagerAdapter.clearInactiveProfileCache();
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001814 }
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001815 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001816
arangelov7f8743d2020-02-13 20:34:30 +00001817 private BroadcastReceiver createWorkProfileStateReceiver() {
1818 return new BroadcastReceiver() {
1819 @Override
1820 public void onReceive(Context context, Intent intent) {
1821 String action = intent.getAction();
arangelov2dd9f7b2020-02-27 10:54:24 +00001822 if (!TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)
arangelov7f8743d2020-02-13 20:34:30 +00001823 && !TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) {
1824 return;
1825 }
arangelov2dd9f7b2020-02-27 10:54:24 +00001826 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1827 if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)
1828 && userHandle != getWorkProfileUserHandle().getIdentifier()) {
1829 return;
1830 }
1831 if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)) {
1832 mMultiProfilePagerAdapter.markWorkProfileEnabledBroadcastReceived();
1833 }
arangelov7f8743d2020-02-13 20:34:30 +00001834 if (mMultiProfilePagerAdapter.getCurrentUserHandle()
arangelove5b369c2020-03-12 17:36:05 +00001835 .equals(getWorkProfileUserHandle())) {
arangelov7f8743d2020-02-13 20:34:30 +00001836 mMultiProfilePagerAdapter.rebuildActiveTab(true);
1837 } else {
1838 mMultiProfilePagerAdapter.clearInactiveProfileCache();
1839 }
1840 }
1841 };
1842 }
1843
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001844 @VisibleForTesting
1845 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001846 public final ComponentName name;
1847 private final List<Intent> mIntents = new ArrayList<>();
1848 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001849 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001850
1851 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1852 this.name = name;
1853 add(intent, info);
1854 }
1855
1856 public void add(Intent intent, ResolveInfo info) {
1857 mIntents.add(intent);
1858 mResolveInfos.add(info);
1859 }
1860
1861 public int getCount() {
1862 return mIntents.size();
1863 }
1864
1865 public Intent getIntentAt(int index) {
1866 return index >= 0 ? mIntents.get(index) : null;
1867 }
1868
1869 public ResolveInfo getResolveInfoAt(int index) {
1870 return index >= 0 ? mResolveInfos.get(index) : null;
1871 }
1872
1873 public int findIntent(Intent intent) {
1874 for (int i = 0, N = mIntents.size(); i < N; i++) {
1875 if (intent.equals(mIntents.get(i))) {
1876 return i;
1877 }
1878 }
1879 return -1;
1880 }
1881
1882 public int findResolveInfo(ResolveInfo info) {
1883 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1884 if (info.equals(mResolveInfos.get(i))) {
1885 return i;
1886 }
1887 }
1888 return -1;
1889 }
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001890
1891 public boolean isPinned() {
1892 return mPinned;
1893 }
1894
1895 public void setPinned(boolean pinned) {
1896 mPinned = pinned;
1897 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001898 }
1899
Adam Powell7d758002015-05-06 17:49:36 -07001900 class ItemClickListener implements AdapterView.OnItemClickListener,
1901 AdapterView.OnItemLongClickListener {
1902 @Override
1903 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1904 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1905 if (listView != null) {
1906 position -= listView.getHeaderViewsCount();
1907 }
1908 if (position < 0) {
1909 // Header views don't count.
1910 return;
1911 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001912 // If we're still loading, we can't yet enable the buttons.
arangelova3912cf2019-12-13 14:34:45 +00001913 if (mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001914 .resolveInfoForPosition(position, true) == null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001915 return;
1916 }
arangelov38a6fce2019-12-02 18:21:22 +00001917 ListView currentAdapterView =
arangelovcf268642020-01-15 15:09:51 +00001918 (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelov38a6fce2019-12-02 18:21:22 +00001919 final int checkedPos = currentAdapterView.getCheckedItemPosition();
Adam Powell7d758002015-05-06 17:49:36 -07001920 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001921 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07001922 && (!hasValidSelection || mLastSelected != checkedPos)
Matt Pietal0d6834a2019-06-27 13:27:52 -04001923 && mAlwaysButton != null) {
1924 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Adam Powell7d758002015-05-06 17:49:36 -07001925 mOnceButton.setEnabled(hasValidSelection);
1926 if (hasValidSelection) {
arangelov38a6fce2019-12-02 18:21:22 +00001927 currentAdapterView.smoothScrollToPosition(checkedPos);
Sergey Troshin40e979e2019-12-30 17:29:50 +01001928 mOnceButton.requestFocus();
Adam Powell7d758002015-05-06 17:49:36 -07001929 }
1930 mLastSelected = checkedPos;
1931 } else {
1932 startSelected(position, false, true);
1933 }
1934 }
Adam Powell2d809622012-03-22 15:24:43 -07001935
1936 @Override
1937 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001938 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1939 if (listView != null) {
1940 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001941 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001942 if (position < 0) {
1943 // Header views don't count.
1944 return false;
1945 }
arangelova3912cf2019-12-13 14:34:45 +00001946 ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001947 .resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001948 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001949 return true;
1950 }
1951
1952 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001953
Dianne Hackbornec452d92014-11-11 17:16:56 -08001954 static final boolean isSpecificUriMatch(int match) {
1955 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1956 return match >= IntentFilter.MATCH_CATEGORY_HOST
1957 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1958 }
1959
Adam Powell4c470d62015-06-19 17:46:17 -07001960 static class PickTargetOptionRequest extends PickOptionRequest {
1961 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1962 @Nullable Bundle extras) {
1963 super(prompt, options, extras);
1964 }
1965
1966 @Override
1967 public void onCancel() {
1968 super.onCancel();
1969 final ResolverActivity ra = (ResolverActivity) getActivity();
1970 if (ra != null) {
1971 ra.mPickOptionRequest = null;
1972 ra.finish();
1973 }
1974 }
1975
1976 @Override
1977 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1978 super.onPickOptionResult(finished, selections, result);
1979 if (selections.length != 1) {
1980 // TODO In a better world we would filter the UI presented here and let the
1981 // user refine. Maybe later.
1982 return;
1983 }
1984
1985 final ResolverActivity ra = (ResolverActivity) getActivity();
1986 if (ra != null) {
arangelova3912cf2019-12-13 14:34:45 +00001987 final TargetInfo ti = ra.mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001988 .getItem(selections[0].getIndex());
Adam Powell4c470d62015-06-19 17:46:17 -07001989 if (ra.onTargetSelected(ti, false)) {
1990 ra.mPickOptionRequest = null;
1991 ra.finish();
1992 }
1993 }
1994 }
1995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996}