blob: dd3a6603f46a81c337a0357c25994c1f99debe73 [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
Adam Powell63b31692015-09-28 10:45:00 -0700401 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700402 }
403
Adam Powell88831a22014-11-20 18:17:00 -0800404 mProfileView = findViewById(R.id.profile_button);
405 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400406 mProfileView.setOnClickListener(this::onProfileClick);
arangelovb0802dc2019-10-18 18:03:44 +0100407 updateProfileViewButton();
Adam Powell88831a22014-11-20 18:17:00 -0800408 }
Adam Powell4c470d62015-06-19 17:46:17 -0700409
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100410 final Set<String> categories = intent.getCategories();
arangelova3912cf2019-12-13 14:34:45 +0000411 MetricsLogger.action(this, mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem()
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100412 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
413 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
414 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100415 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700416 }
417
arangelovc4dbdbd2020-02-18 20:54:16 +0000418 private boolean isIntentPicker() {
419 return getClass().equals(ResolverActivity.class);
420 }
421
arangelov38a6fce2019-12-02 18:21:22 +0000422 protected AbstractMultiProfilePagerAdapter createMultiProfilePagerAdapter(
423 Intent[] initialIntents,
424 List<ResolveInfo> rList,
425 boolean filterLastUsed) {
426 AbstractMultiProfilePagerAdapter resolverMultiProfilePagerAdapter = null;
arangelovf163a882020-02-18 17:18:47 +0000427 if (shouldShowTabs()) {
arangelov38a6fce2019-12-02 18:21:22 +0000428 resolverMultiProfilePagerAdapter =
429 createResolverMultiProfilePagerAdapterForTwoProfiles(
430 initialIntents, rList, filterLastUsed);
431 } else {
432 resolverMultiProfilePagerAdapter = createResolverMultiProfilePagerAdapterForOneProfile(
433 initialIntents, rList, filterLastUsed);
434 }
435 return resolverMultiProfilePagerAdapter;
436 }
437
438 private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForOneProfile(
439 Intent[] initialIntents,
440 List<ResolveInfo> rList, boolean filterLastUsed) {
441 ResolverListAdapter adapter = createResolverListAdapter(
442 /* context */ this,
443 /* payloadIntents */ mIntents,
444 initialIntents,
445 rList,
446 filterLastUsed,
447 mUseLayoutForBrowsables,
448 /* userHandle */ UserHandle.of(UserHandle.myUserId()));
449 return new ResolverMultiProfilePagerAdapter(
450 /* context */ this,
arangelov7981b122020-01-16 10:58:27 +0000451 adapter,
452 getPersonalProfileUserHandle(),
453 /* workProfileUserHandle= */ null);
arangelov38a6fce2019-12-02 18:21:22 +0000454 }
455
456 private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForTwoProfiles(
457 Intent[] initialIntents,
458 List<ResolveInfo> rList,
459 boolean filterLastUsed) {
arangelovcf268642020-01-15 15:09:51 +0000460 // We only show the default app for the profile of the current user. The filterLastUsed
461 // flag determines whether to show a default app and that app is not shown in the
462 // resolver list. So filterLastUsed should be false for the other profile.
arangelov38a6fce2019-12-02 18:21:22 +0000463 ResolverListAdapter personalAdapter = createResolverListAdapter(
464 /* context */ this,
465 /* payloadIntents */ mIntents,
466 initialIntents,
467 rList,
arangelovcf268642020-01-15 15:09:51 +0000468 (filterLastUsed && UserHandle.myUserId()
469 == getPersonalProfileUserHandle().getIdentifier()),
arangelov38a6fce2019-12-02 18:21:22 +0000470 mUseLayoutForBrowsables,
471 /* userHandle */ getPersonalProfileUserHandle());
arangelov7981b122020-01-16 10:58:27 +0000472 UserHandle workProfileUserHandle = getWorkProfileUserHandle();
arangelov38a6fce2019-12-02 18:21:22 +0000473 ResolverListAdapter workAdapter = createResolverListAdapter(
474 /* context */ this,
475 /* payloadIntents */ mIntents,
476 initialIntents,
477 rList,
arangelovcf268642020-01-15 15:09:51 +0000478 (filterLastUsed && UserHandle.myUserId()
arangelov7981b122020-01-16 10:58:27 +0000479 == workProfileUserHandle.getIdentifier()),
arangelov38a6fce2019-12-02 18:21:22 +0000480 mUseLayoutForBrowsables,
arangelov7981b122020-01-16 10:58:27 +0000481 /* userHandle */ workProfileUserHandle);
arangelov38a6fce2019-12-02 18:21:22 +0000482 return new ResolverMultiProfilePagerAdapter(
483 /* context */ this,
484 personalAdapter,
485 workAdapter,
arangelov7981b122020-01-16 10:58:27 +0000486 /* defaultProfile */ getCurrentProfile(),
487 getPersonalProfileUserHandle(),
488 getWorkProfileUserHandle());
arangelov38a6fce2019-12-02 18:21:22 +0000489 }
490
491 protected @Profile int getCurrentProfile() {
492 return (UserHandle.myUserId() == UserHandle.USER_SYSTEM ? PROFILE_PERSONAL : PROFILE_WORK);
493 }
494
495 protected UserHandle getPersonalProfileUserHandle() {
496 return UserHandle.of(ActivityManager.getCurrentUser());
497 }
498 protected @Nullable UserHandle getWorkProfileUserHandle() {
499 UserManager userManager = getSystemService(UserManager.class);
500 for (final UserInfo userInfo : userManager.getProfiles(ActivityManager.getCurrentUser())) {
501 if (userInfo.isManagedProfile()) {
502 return userInfo.getUserHandle();
503 }
504 }
505 return null;
506 }
507
arangelovf163a882020-02-18 17:18:47 +0000508 private boolean hasWorkProfile() {
arangelov38a6fce2019-12-02 18:21:22 +0000509 return getWorkProfileUserHandle() != null;
510 }
511
arangelovf163a882020-02-18 17:18:47 +0000512 protected boolean shouldShowTabs() {
513 return hasWorkProfile() && ENABLE_TABBED_VIEW;
514 }
515
Matt Pietal74c6ed02019-04-18 13:38:46 -0400516 protected void onProfileClick(View v) {
arangelov38a6fce2019-12-02 18:21:22 +0000517 final DisplayResolveInfo dri =
arangelova3912cf2019-12-13 14:34:45 +0000518 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400519 if (dri == null) {
520 return;
521 }
522
523 // Do not show the profile switch message anymore.
524 mProfileSwitchMessageId = -1;
525
526 onTargetSelected(dri, false);
527 finish();
528 }
529
Matt Pietal9236adc2019-12-12 09:24:23 -0500530 /**
531 * Numerous layouts are supported, each with optional ViewGroups.
532 * Make sure the inset gets added to the correct View, using
533 * a footer for Lists so it can properly scroll under the navbar.
534 */
535 protected boolean shouldAddFooterView() {
536 if (useLayoutWithDefault()) return true;
537
538 View buttonBar = findViewById(R.id.button_bar);
539 if (buttonBar == null || buttonBar.getVisibility() == View.GONE) return true;
540
541 return false;
542 }
543
544 protected void applyFooterView(int height) {
545 if (mFooterSpacer == null) {
546 mFooterSpacer = new Space(getApplicationContext());
547 } else {
548 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
arangelovcf268642020-01-15 15:09:51 +0000549 .getActiveAdapterView().removeFooterView(mFooterSpacer);
Matt Pietal9236adc2019-12-12 09:24:23 -0500550 }
551 mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
552 mSystemWindowInsets.bottom));
553 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
arangelovcf268642020-01-15 15:09:51 +0000554 .getActiveAdapterView().addFooterView(mFooterSpacer);
Matt Pietal9236adc2019-12-12 09:24:23 -0500555 }
556
Matt Pietal800136a2019-05-08 07:46:39 -0400557 protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
558 mSystemWindowInsets = insets.getSystemWindowInsets();
559
560 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
561 mSystemWindowInsets.right, 0);
562
Matt Pietal9236adc2019-12-12 09:24:23 -0500563 resetButtonBar();
564
Matt Pietald5071202019-10-10 11:21:28 -0400565 // Need extra padding so the list can fully scroll up
Matt Pietal9236adc2019-12-12 09:24:23 -0500566 if (shouldAddFooterView()) {
567 applyFooterView(mSystemWindowInsets.bottom);
Matt Pietal800136a2019-05-08 07:46:39 -0400568 }
Matt Pietal800136a2019-05-08 07:46:39 -0400569
570 return insets.consumeSystemWindowInsets();
571 }
572
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900573 @Override
574 public void onConfigurationChanged(Configuration newConfig) {
575 super.onConfigurationChanged(newConfig);
arangelova3912cf2019-12-13 14:34:45 +0000576 mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400577
578 if (mSystemWindowInsets != null) {
579 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
580 mSystemWindowInsets.right, 0);
581 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900582 }
583
arangelovb0802dc2019-10-18 18:03:44 +0100584 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -0800585 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700586 if (!isVoiceInteraction()) {
587 // Clearly not needed.
588 return;
589 }
590
arangelova3912cf2019-12-13 14:34:45 +0000591 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getCount();
arangelov38a6fce2019-12-02 18:21:22 +0000592 final Option[] options = new Option[count];
Adam Powell4c470d62015-06-19 17:46:17 -0700593 for (int i = 0, N = options.length; i < N; i++) {
arangelova3912cf2019-12-13 14:34:45 +0000594 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter().getItem(i);
Matt Pietal2cf4c6c2019-09-10 09:33:11 -0400595 if (target == null) {
596 // If this occurs, a new set of targets is being loaded. Let that complete,
597 // and have the next call to send voice choices proceed instead.
598 return;
599 }
600 options[i] = optionForChooserTarget(target, i);
Adam Powell4c470d62015-06-19 17:46:17 -0700601 }
602
603 mPickOptionRequest = new PickTargetOptionRequest(
604 new Prompt(getTitle()), options, null);
605 getVoiceInteractor().submitRequest(mPickOptionRequest);
606 }
607
608 Option optionForChooserTarget(TargetInfo target, int index) {
609 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800610 }
611
Adam Powell2ed547e2015-04-29 18:45:04 -0700612 protected final void setAdditionalTargets(Intent[] intents) {
613 if (intents != null) {
614 for (Intent intent : intents) {
615 mIntents.add(intent);
616 }
617 }
618 }
619
arangelovb0802dc2019-10-18 18:03:44 +0100620 @Override // SelectableTargetInfoCommunicator ResolverListCommunicator
Adam Powell0ccc0e92015-04-23 17:19:37 -0700621 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700622 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700623 }
624
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800625 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700626 final Uri referrer = getReferrer();
627 if (referrer != null && "android-app".equals(referrer.getScheme())) {
628 return referrer.getHost();
629 }
630 return null;
631 }
632
Adam Powell23882512016-01-29 10:21:00 -0800633 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700634 return R.layout.resolver_list;
635 }
636
arangelovb0802dc2019-10-18 18:03:44 +0100637 @Override // ResolverListCommunicator
638 public void updateProfileViewButton() {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400639 if (mProfileView == null) {
640 return;
641 }
642
arangelov38a6fce2019-12-02 18:21:22 +0000643 final DisplayResolveInfo dri =
arangelova3912cf2019-12-13 14:34:45 +0000644 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile();
arangelov2a4d0b12020-03-04 18:15:04 +0000645 if (dri != null && !shouldShowTabs()) {
Adam Powell88831a22014-11-20 18:17:00 -0800646 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800647 View text = mProfileView.findViewById(R.id.profile_button);
648 if (!(text instanceof TextView)) {
649 text = mProfileView.findViewById(R.id.text1);
650 }
651 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800652 } else {
653 mProfileView.setVisibility(View.GONE);
654 }
Adam Powell278902c2014-07-12 18:33:22 -0700655 }
656
Sander Alewijnsef6545332014-10-31 12:39:02 +0000657 private void setProfileSwitchMessageId(int contentUserHint) {
658 if (contentUserHint != UserHandle.USER_CURRENT &&
659 contentUserHint != UserHandle.myUserId()) {
660 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
661 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
662 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
663 : false;
664 boolean targetIsManaged = userManager.isManagedProfile();
665 if (originIsManaged && !targetIsManaged) {
666 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
667 } else if (!originIsManaged && targetIsManaged) {
668 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
669 }
670 }
671 }
672
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700673 /**
674 * Turn on launch mode that is safe to use when forwarding intents received from
675 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
676 * instead of the normal Activity.startActivity for launching the activity selected
677 * by the user.
678 *
679 * <p>This mode is set to true by default if the activity is initialized through
680 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
681 * methods, it is set to false by default. You must set it before calling one of the
682 * more detailed onCreate methods, so that it will be set correctly in the case where
683 * there is only one intent to resolve and it is thus started immediately.</p>
684 */
685 public void setSafeForwardingMode(boolean safeForwarding) {
686 mSafeForwardingMode = safeForwarding;
687 }
688
shafik69df96322018-12-18 15:41:19 +0000689 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
690 final ActionTitle title = mResolvingHome
691 ? ActionTitle.HOME
692 : ActionTitle.forAction(intent.getAction());
693
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800694 // While there may already be a filtered item, we can only use it in the title if the list
695 // is already sorted and all information relevant to it is already in the list.
arangelov38a6fce2019-12-02 18:21:22 +0000696 final boolean named =
arangelova3912cf2019-12-13 14:34:45 +0000697 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700698 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
699 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000700 } else if (isHttpSchemeAndViewAction(intent)) {
701 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000702 // they're giving access for the activity to open URLs from this specific host
Matt Pietal0d6834a2019-06-27 13:27:52 -0400703 String dialogTitle = null;
704 if (named && !mUseLayoutForBrowsables) {
705 dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES,
arangelova3912cf2019-12-13 14:34:45 +0000706 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredItem()
arangelov38a6fce2019-12-02 18:21:22 +0000707 .getDisplayLabel());
Matt Pietal0d6834a2019-06-27 13:27:52 -0400708 } else if (named && mUseLayoutForBrowsables) {
709 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES,
710 intent.getData().getHost(),
arangelova3912cf2019-12-13 14:34:45 +0000711 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredItem()
arangelov38a6fce2019-12-02 18:21:22 +0000712 .getDisplayLabel());
arangelova3912cf2019-12-13 14:34:45 +0000713 } else if (mMultiProfilePagerAdapter.getActiveListAdapter().areAllTargetsBrowsers()) {
Matt Pietald5071202019-10-10 11:21:28 -0400714 dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES);
Matt Pietal0d6834a2019-06-27 13:27:52 -0400715 } else {
716 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES,
717 intent.getData().getHost());
718 }
719 return dialogTitle;
Adam Powell278902c2014-07-12 18:33:22 -0700720 } else {
Adam Powell24428412015-04-01 17:19:56 -0700721 return named
arangelov38a6fce2019-12-02 18:21:22 +0000722 ? getString(title.namedTitleRes, mMultiProfilePagerAdapter
arangelova3912cf2019-12-13 14:34:45 +0000723 .getActiveListAdapter().getFilteredItem().getDisplayLabel())
Adam Powell24428412015-04-01 17:19:56 -0700724 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700725 }
Adam Powellc5878612012-05-04 18:42:38 -0700726 }
727
Adam Powelle9414d92014-07-05 17:44:18 -0700728 void dismiss() {
729 if (!isFinishing()) {
730 finish();
731 }
732 }
733
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800734 @Override
735 protected void onRestart() {
736 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700737 if (!mRegistered) {
arangelovbdb40052020-01-09 18:01:58 +0000738 mPersonalPackageMonitor.register(this, getMainLooper(),
739 getPersonalProfileUserHandle(), false);
arangelovf163a882020-02-18 17:18:47 +0000740 if (shouldShowTabs()) {
arangelovbdb40052020-01-09 18:01:58 +0000741 if (mWorkPackageMonitor == null) {
arangelov7981b122020-01-16 10:58:27 +0000742 mWorkPackageMonitor = createPackageMonitor(
743 mMultiProfilePagerAdapter.getWorkListAdapter());
arangelovbdb40052020-01-09 18:01:58 +0000744 }
745 mWorkPackageMonitor.register(this, getMainLooper(),
746 getWorkProfileUserHandle(), false);
747 }
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700748 mRegistered = true;
749 }
arangelovafec9d02020-03-17 12:41:01 +0000750 if (shouldShowTabs() && mMultiProfilePagerAdapter.isWaitingToEnableWorkProfile()) {
751 if (mMultiProfilePagerAdapter.isQuietModeEnabled(getWorkProfileUserHandle())) {
752 mMultiProfilePagerAdapter.markWorkProfileEnabledBroadcastReceived();
753 }
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 }
arangelovce7d4a62020-04-01 13:26:51 +0100992 if (mMultiProfilePagerAdapter.shouldShowEmptyStateScreen(listAdapter)) {
arangelov27608552020-03-27 16:57:50 +0000993 mMultiProfilePagerAdapter.showEmptyResolverListEmptyState(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);
arangelovbf806752020-04-09 18:31:50 +01001010 if (shouldShowTabs() && isIntentPicker()) {
1011 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
1012 if (rdl != null) {
1013 rdl.setMaxCollapsedHeight(getResources()
1014 .getDimensionPixelSize(useLayoutWithDefault()
1015 ? R.dimen.resolver_max_collapsed_height_with_default_with_tabs
1016 : R.dimen.resolver_max_collapsed_height_with_tabs));
1017 }
1018 }
arangelovb0802dc2019-10-18 18:03:44 +01001019 }
1020
Adam Powell2ed547e2015-04-29 18:45:04 -07001021 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -07001022 final ResolveInfo ri = target.getResolveInfo();
1023 final Intent intent = target != null ? target.getResolvedIntent() : null;
1024
arangelov38a6fce2019-12-02 18:21:22 +00001025 if (intent != null && (mSupportsAlwaysUseOption
arangelova3912cf2019-12-13 14:34:45 +00001026 || mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem())
1027 && mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredResolveList() != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001028 // Build a reasonable intent filter, based on what matched.
1029 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +02001030 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031
Henrik Engström3277cf12014-07-17 12:18:29 +02001032 if (intent.getSelector() != null) {
1033 filterIntent = intent.getSelector();
1034 } else {
1035 filterIntent = intent;
1036 }
1037
1038 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001039 if (action != null) {
1040 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
Henrik Engström3277cf12014-07-17 12:18:29 +02001042 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001043 if (categories != null) {
1044 for (String cat : categories) {
1045 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001048 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049
Adam Powell24428412015-04-01 17:19:56 -07001050 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +02001051 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001052 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +02001053 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001054 if (mimeType != null) {
1055 try {
1056 filter.addDataType(mimeType);
1057 } catch (IntentFilter.MalformedMimeTypeException e) {
1058 Log.w("ResolverActivity", e);
1059 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001062 }
1063 if (data != null && data.getScheme() != null) {
1064 // We need the data specification if there was no type,
1065 // OR if the scheme is not one of our magical "file:"
1066 // or "content:" schemes (see IntentFilter for the reason).
1067 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
1068 || (!"file".equals(data.getScheme())
1069 && !"content".equals(data.getScheme()))) {
1070 filter.addDataScheme(data.getScheme());
1071
1072 // Look through the resolved filter to determine which part
1073 // of it matched the original Intent.
1074 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
1075 if (pIt != null) {
1076 String ssp = data.getSchemeSpecificPart();
1077 while (ssp != null && pIt.hasNext()) {
1078 PatternMatcher p = pIt.next();
1079 if (p.match(ssp)) {
1080 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
1081 break;
1082 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001083 }
1084 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001085 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
1086 if (aIt != null) {
1087 while (aIt.hasNext()) {
1088 IntentFilter.AuthorityEntry a = aIt.next();
1089 if (a.match(data) >= 0) {
1090 int port = a.getPort();
1091 filter.addDataAuthority(a.getHost(),
1092 port >= 0 ? Integer.toString(port) : null);
1093 break;
1094 }
1095 }
1096 }
1097 pIt = ri.filter.pathsIterator();
1098 if (pIt != null) {
1099 String path = data.getPath();
1100 while (path != null && pIt.hasNext()) {
1101 PatternMatcher p = pIt.next();
1102 if (p.match(path)) {
1103 filter.addDataPath(p.getPath(), p.getType());
1104 break;
1105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 }
1107 }
1108 }
1109 }
1110
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001111 if (filter != null) {
arangelova3912cf2019-12-13 14:34:45 +00001112 final int N = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001113 .getUnfilteredResolveList().size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001114 ComponentName[] set;
1115 // If we don't add back in the component for forwarding the intent to a managed
1116 // profile, the preferred activity may not be updated correctly (as the set of
1117 // components we tell it we knew about will have changed).
arangelovb0802dc2019-10-18 18:03:44 +01001118 final boolean needToAddBackProfileForwardingComponent =
arangelova3912cf2019-12-13 14:34:45 +00001119 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001120 if (!needToAddBackProfileForwardingComponent) {
1121 set = new ComponentName[N];
1122 } else {
1123 set = new ComponentName[N + 1];
1124 }
1125
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001126 int bestMatch = 0;
1127 for (int i=0; i<N; i++) {
arangelova3912cf2019-12-13 14:34:45 +00001128 ResolveInfo r = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001129 .getUnfilteredResolveList().get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001130 set[i] = new ComponentName(r.activityInfo.packageName,
1131 r.activityInfo.name);
1132 if (r.match > bestMatch) bestMatch = r.match;
1133 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001134
1135 if (needToAddBackProfileForwardingComponent) {
arangelova3912cf2019-12-13 14:34:45 +00001136 set[N] = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001137 .getOtherProfile().getResolvedComponentName();
arangelova3912cf2019-12-13 14:34:45 +00001138 final int otherProfileMatch = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001139 .getOtherProfile().getResolveInfo().match;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001140 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
1141 }
1142
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001143 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001144 final int userId = getUserId();
1145 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001146
1147 // Set the preferred Activity
1148 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
1149
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001150 if (ri.handleAllWebDataURI) {
1151 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001152 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001153 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001154 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001155 }
1156 } else {
1157 // Update Domain Verification status
1158 ComponentName cn = intent.getComponent();
1159 String packageName = cn.getPackageName();
1160 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001161
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001162 boolean isHttpOrHttps = (dataScheme != null) &&
1163 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
1164 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001165
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001166 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
1167 boolean hasCategoryBrowsable = (categories != null) &&
1168 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001169
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001170 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001171 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001172 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
1173 userId);
1174 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001175 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001176 } else {
1177 try {
arangelova3912cf2019-12-13 14:34:45 +00001178 mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001179 .mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001180 } catch (RemoteException re) {
1181 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1182 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 }
1185 }
1186
Adam Powell24428412015-04-01 17:19:56 -07001187 if (target != null) {
arangelov9e148522020-01-15 21:11:51 +00001188 if (intent != null) {
1189 intent.fixUris(UserHandle.myUserId());
1190 }
Adam Powell24428412015-04-01 17:19:56 -07001191 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -04001192
1193 // Rely on the ActivityManager to pop up a dialog regarding app suspension
1194 // and return false
1195 if (target.isSuspended()) {
1196 return false;
1197 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001198 }
Matt Pietala4b30072019-04-04 13:44:36 -04001199
Adam Powell2ed547e2015-04-29 18:45:04 -07001200 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001201 }
1202
arangelovcf268642020-01-15 15:09:51 +00001203 @VisibleForTesting
Adam Powell23882512016-01-29 10:21:00 -08001204 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -07001205 // We're dispatching intents that might be coming from legacy apps, so
1206 // don't kill ourselves.
1207 StrictMode.disableDeathOnFileUriExposure();
1208 try {
1209 safelyStartActivityInternal(cti);
1210 } finally {
1211 StrictMode.enableDeathOnFileUriExposure();
1212 }
1213 }
1214
1215 private void safelyStartActivityInternal(TargetInfo cti) {
arangelov96dec882020-01-31 20:23:40 +00001216 if (mPersonalPackageMonitor != null) {
1217 mPersonalPackageMonitor.unregister();
1218 }
arangelov7981b122020-01-16 10:58:27 +00001219 if (mWorkPackageMonitor != null) {
1220 mWorkPackageMonitor.unregister();
1221 }
1222 mRegistered = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +00001223 // If needed, show that intent is forwarded
1224 // from managed profile to owner or other way around.
1225 if (mProfileSwitchMessageId != -1) {
1226 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
1227 }
arangelov38a6fce2019-12-02 18:21:22 +00001228 UserHandle currentUserHandle = mMultiProfilePagerAdapter.getCurrentUserHandle();
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001229 if (!mSafeForwardingMode) {
arangelov38a6fce2019-12-02 18:21:22 +00001230 if (cti.startAsUser(this, null, currentUserHandle)) {
Adam Powell24428412015-04-01 17:19:56 -07001231 onActivityStarted(cti);
arangelov4bf17472020-02-17 20:21:46 +00001232 maybeLogCrossProfileTargetLaunch(cti, currentUserHandle);
Adam Powell24428412015-04-01 17:19:56 -07001233 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001234 return;
1235 }
1236 try {
arangelov38a6fce2019-12-02 18:21:22 +00001237 if (cti.startAsCaller(this, null, currentUserHandle.getIdentifier())) {
Adam Powell24428412015-04-01 17:19:56 -07001238 onActivityStarted(cti);
arangelov4bf17472020-02-17 20:21:46 +00001239 maybeLogCrossProfileTargetLaunch(cti, currentUserHandle);
Adam Powell24428412015-04-01 17:19:56 -07001240 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001241 } catch (RuntimeException e) {
1242 String launchedFromPackage;
1243 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001244 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001245 getActivityToken());
1246 } catch (RemoteException e2) {
1247 launchedFromPackage = "??";
1248 }
1249 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
1250 + " package " + launchedFromPackage + ", while running in "
1251 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254
arangelov4bf17472020-02-17 20:21:46 +00001255 private void maybeLogCrossProfileTargetLaunch(TargetInfo cti, UserHandle currentUserHandle) {
arangelove5b369c2020-03-12 17:36:05 +00001256 if (!hasWorkProfile() || currentUserHandle.equals(getUser())) {
arangelov4bf17472020-02-17 20:21:46 +00001257 return;
1258 }
1259 DevicePolicyEventLogger
1260 .createEvent(DevicePolicyEnums.RESOLVER_CROSS_PROFILE_TARGET_OPENED)
arangelove5b369c2020-03-12 17:36:05 +00001261 .setBoolean(currentUserHandle.equals(getPersonalProfileUserHandle()))
arangelov4bf17472020-02-17 20:21:46 +00001262 .setStrings(getMetricsCategory(),
1263 cti instanceof ChooserTargetInfo ? "direct_share" : "other_target")
1264 .write();
1265 }
1266
Alison Cichowlas3e340502018-08-07 17:15:01 -04001267
arangelovb0802dc2019-10-18 18:03:44 +01001268 public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
Alison Cichowlas3e340502018-08-07 17:15:01 -04001269 int userId) {
1270 // Pass intent to delegate chooser activity with permission token.
1271 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
1272 // moves into systemui
1273 try {
1274 // TODO: Once this is a small springboard activity, it can move off the UI process
1275 // and we can move the request method to ActivityManagerInternal.
1276 IBinder permissionToken = ActivityTaskManager.getService()
1277 .requestStartActivityPermissionToken(getActivityToken());
1278 final Intent chooserIntent = new Intent();
1279 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1280 Resources.getSystem().getString(R.string.config_chooserActivity));
1281 chooserIntent.setClassName(delegateActivity.getPackageName(),
1282 delegateActivity.getClassName());
1283 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1284
1285 // TODO: These extras will change as chooser activity moves into systemui
1286 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1287 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1288 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1289 ignoreTargetSecurity);
1290 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001291 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1292 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001293 startActivity(chooserIntent);
1294 } catch (RemoteException e) {
1295 Log.e(TAG, e.toString());
1296 }
1297 return true;
1298 }
1299
Adam Powell23882512016-01-29 10:21:00 -08001300 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001301 // Do nothing
1302 }
1303
arangelovb0802dc2019-10-18 18:03:44 +01001304 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -08001305 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001306 return false;
1307 }
1308
Adam Powell23882512016-01-29 10:21:00 -08001309 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001310 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001311 }
1312
Alison Cichowlas19ee2922019-12-16 19:43:12 -05001313 void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001314 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001315 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001316 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
arangelovebf3c052020-03-17 13:11:37 +00001317 startActivityAsUser(in, mMultiProfilePagerAdapter.getCurrentUserHandle());
Adam Powellc5878612012-05-04 18:42:38 -07001318 }
1319
arangelov38a6fce2019-12-02 18:21:22 +00001320 @VisibleForTesting
1321 protected ResolverListAdapter createResolverListAdapter(Context context,
1322 List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList,
1323 boolean filterLastUsed, boolean useLayoutForBrowsables, UserHandle userHandle) {
Paul McLean07425c82019-10-18 12:00:11 -06001324 Intent startIntent = getIntent();
1325 boolean isAudioCaptureDevice =
1326 startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
arangelovb0802dc2019-10-18 18:03:44 +01001327 return new ResolverListAdapter(context, payloadIntents, initialIntents, rList,
arangelov38a6fce2019-12-02 18:21:22 +00001328 filterLastUsed, createListController(userHandle), useLayoutForBrowsables, this,
Paul McLean07425c82019-10-18 12:00:11 -06001329 isAudioCaptureDevice);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001330 }
1331
1332 @VisibleForTesting
arangelov38a6fce2019-12-02 18:21:22 +00001333 protected ResolverListController createListController(UserHandle userHandle) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001334 return new ResolverListController(
1335 this,
1336 mPm,
1337 getTargetIntent(),
1338 getReferrerPackageName(),
arangelov38a6fce2019-12-02 18:21:22 +00001339 mLaunchedFromUid,
1340 userHandle);
Adam Powell88831a22014-11-20 18:17:00 -08001341 }
1342
Adam Powell39e94eb2015-09-08 17:01:49 -07001343 /**
arangelovb0802dc2019-10-18 18:03:44 +01001344 * Sets up the content view.
arangelovbb572332019-11-13 12:50:08 +00001345 * @return <code>true</code> if the activity is finishing and creation should halt.
Adam Powell39e94eb2015-09-08 17:01:49 -07001346 */
arangelovbb572332019-11-13 12:50:08 +00001347 private boolean configureContentView() {
arangelova3912cf2019-12-13 14:34:45 +00001348 if (mMultiProfilePagerAdapter.getActiveListAdapter() == null) {
1349 throw new IllegalStateException("mMultiProfilePagerAdapter.getCurrentListAdapter() "
1350 + "cannot be null.");
arangelovb0802dc2019-10-18 18:03:44 +01001351 }
arangelov7981b122020-01-16 10:58:27 +00001352 // We partially rebuild the inactive adapter to determine if we should auto launch
arangelovb91d08f2020-03-05 21:50:14 +00001353 // isTabLoaded will be true here if the empty state screen is shown instead of the list.
1354 boolean rebuildCompleted = mMultiProfilePagerAdapter.rebuildActiveTab(true)
1355 || mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded();
arangelov2a4d0b12020-03-04 18:15:04 +00001356 if (shouldShowTabs()) {
arangelovb91d08f2020-03-05 21:50:14 +00001357 boolean rebuildInactiveCompleted = mMultiProfilePagerAdapter.rebuildInactiveTab(false)
1358 || mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded();
arangelovdb0c1f12020-02-14 17:35:12 +00001359 rebuildCompleted = rebuildCompleted && rebuildInactiveCompleted;
1360 }
arangelov7981b122020-01-16 10:58:27 +00001361
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001362 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001363 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001364 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001365 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001366 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001367 setContentView(mLayoutId);
arangelov38a6fce2019-12-02 18:21:22 +00001368 mMultiProfilePagerAdapter.setupViewPager(findViewById(R.id.profile_pager));
arangelovdb0c1f12020-02-14 17:35:12 +00001369 return postRebuildList(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001370 }
Adam Powell7d758002015-05-06 17:49:36 -07001371
arangelovb0802dc2019-10-18 18:03:44 +01001372 /**
arangelovbb572332019-11-13 12:50:08 +00001373 * Finishing procedures to be performed after the list has been rebuilt.
1374 * </p>Subclasses must call postRebuildListInternal at the end of postRebuildList.
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 protected boolean postRebuildList(boolean rebuildCompleted) {
1379 return postRebuildListInternal(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001380 }
1381
1382 /**
arangelovbb572332019-11-13 12:50:08 +00001383 * Finishing procedures to be performed after the list has been rebuilt.
1384 * @param rebuildCompleted
1385 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001386 */
arangelovbb572332019-11-13 12:50:08 +00001387 final boolean postRebuildListInternal(boolean rebuildCompleted) {
arangelova3912cf2019-12-13 14:34:45 +00001388 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001389
1390 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1391 // we're already done, we can check if we should auto-launch immediately.
arangelov7981b122020-01-16 10:58:27 +00001392 if (rebuildCompleted && maybeAutolaunchActivity()) {
1393 return true;
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001394 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001395
arangelovcf268642020-01-15 15:09:51 +00001396 setupViewVisibilities();
1397
arangelovf163a882020-02-18 17:18:47 +00001398 if (shouldShowTabs()) {
arangelovcf268642020-01-15 15:09:51 +00001399 setupProfileTabs();
1400 }
1401
arangelov38a6fce2019-12-02 18:21:22 +00001402 return false;
1403 }
1404
arangelov7981b122020-01-16 10:58:27 +00001405 private int isPermissionGranted(String permission, int uid) {
1406 return ActivityManager.checkComponentPermission(permission, uid,
1407 /* owningUid= */-1, /* exported= */ true);
1408 }
1409
1410 /**
1411 * @return {@code true} if a resolved target is autolaunched, otherwise {@code false}
1412 */
1413 private boolean maybeAutolaunchActivity() {
1414 int numberOfProfiles = mMultiProfilePagerAdapter.getItemCount();
1415 if (numberOfProfiles == 1 && maybeAutolaunchIfSingleTarget()) {
1416 return true;
arangelovf6986d42020-02-12 15:03:22 +00001417 } else if (numberOfProfiles == 2
arangelovb91d08f2020-03-05 21:50:14 +00001418 && mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded()
1419 && mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded()
arangelovf6986d42020-02-12 15:03:22 +00001420 && (maybeAutolaunchIfNoAppsOnInactiveTab()
1421 || maybeAutolaunchIfCrossProfileSupported())) {
arangelov7981b122020-01-16 10:58:27 +00001422 return true;
1423 }
1424 return false;
1425 }
1426
1427 private boolean maybeAutolaunchIfSingleTarget() {
1428 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1429 if (count != 1) {
1430 return false;
1431 }
1432
arangelovdb0c1f12020-02-14 17:35:12 +00001433 if (mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null) {
1434 return false;
1435 }
1436
arangelov7981b122020-01-16 10:58:27 +00001437 // Only one target, so we're a candidate to auto-launch!
1438 final TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
1439 .targetInfoForPosition(0, false);
1440 if (shouldAutoLaunchSingleChoice(target)) {
1441 safelyStartActivity(target);
1442 finish();
1443 return true;
1444 }
1445 return false;
1446 }
1447
arangelovf6986d42020-02-12 15:03:22 +00001448 private boolean maybeAutolaunchIfNoAppsOnInactiveTab() {
1449 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1450 if (count != 1) {
1451 return false;
1452 }
1453 ResolverListAdapter inactiveListAdapter =
1454 mMultiProfilePagerAdapter.getInactiveListAdapter();
1455 if (inactiveListAdapter.getUnfilteredCount() != 0) {
1456 return false;
1457 }
1458 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
1459 .targetInfoForPosition(0, false);
1460 safelyStartActivity(target);
1461 finish();
1462 return true;
1463 }
1464
arangelov7981b122020-01-16 10:58:27 +00001465 /**
1466 * When we have a personal and a work profile, we auto launch in the following scenario:
1467 * - There is 1 resolved target on each profile
1468 * - That target is the same app on both profiles
1469 * - The target app has permission to communicate cross profiles
1470 * - The target app has declared it supports cross-profile communication via manifest metadata
1471 */
1472 private boolean maybeAutolaunchIfCrossProfileSupported() {
arangelov4bf17472020-02-17 20:21:46 +00001473 ResolverListAdapter activeListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
1474 int count = activeListAdapter.getUnfilteredCount();
arangelov7981b122020-01-16 10:58:27 +00001475 if (count != 1) {
1476 return false;
1477 }
1478 ResolverListAdapter inactiveListAdapter =
1479 mMultiProfilePagerAdapter.getInactiveListAdapter();
1480 if (inactiveListAdapter.getUnfilteredCount() != 1) {
1481 return false;
1482 }
arangelov4bf17472020-02-17 20:21:46 +00001483 TargetInfo activeProfileTarget = activeListAdapter
arangelov7981b122020-01-16 10:58:27 +00001484 .targetInfoForPosition(0, false);
1485 TargetInfo inactiveProfileTarget = inactiveListAdapter.targetInfoForPosition(0, false);
1486 if (!Objects.equals(activeProfileTarget.getResolvedComponentName(),
1487 inactiveProfileTarget.getResolvedComponentName())) {
1488 return false;
1489 }
1490 if (!shouldAutoLaunchSingleChoice(activeProfileTarget)) {
1491 return false;
1492 }
1493 String packageName = activeProfileTarget.getResolvedComponentName().getPackageName();
1494 if (!canAppInteractCrossProfiles(packageName)) {
1495 return false;
1496 }
1497
arangelov4bf17472020-02-17 20:21:46 +00001498 DevicePolicyEventLogger
1499 .createEvent(DevicePolicyEnums.RESOLVER_AUTOLAUNCH_CROSS_PROFILE_TARGET)
arangelove5b369c2020-03-12 17:36:05 +00001500 .setBoolean(activeListAdapter.getUserHandle()
1501 .equals(getPersonalProfileUserHandle()))
arangelov4bf17472020-02-17 20:21:46 +00001502 .setStrings(getMetricsCategory())
1503 .write();
arangelov7981b122020-01-16 10:58:27 +00001504 safelyStartActivity(activeProfileTarget);
1505 finish();
1506 return true;
1507 }
1508
1509 /**
1510 * Returns whether the package has the necessary permissions to interact across profiles on
1511 * behalf of a given user.
1512 *
1513 * <p>This means meeting the following condition:
1514 * <ul>
1515 * <li>The app's {@link ApplicationInfo#crossProfile} flag must be true, and at least
1516 * one of the following conditions must be fulfilled</li>
1517 * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS_FULL} granted.</li>
1518 * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS} granted.</li>
1519 * <li>{@code Manifest.permission.INTERACT_ACROSS_PROFILES} granted, or the corresponding
1520 * AppOps {@code android:interact_across_profiles} is set to "allow".</li>
1521 * </ul>
1522 *
1523 */
1524 private boolean canAppInteractCrossProfiles(String packageName) {
1525 ApplicationInfo applicationInfo;
1526 try {
1527 applicationInfo = getPackageManager().getApplicationInfo(packageName, 0);
1528 } catch (NameNotFoundException e) {
1529 Log.e(TAG, "Package " + packageName + " does not exist on current user.");
1530 return false;
1531 }
1532 if (!applicationInfo.crossProfile) {
1533 return false;
1534 }
1535
1536 int packageUid = applicationInfo.uid;
1537
1538 if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1539 packageUid) == PackageManager.PERMISSION_GRANTED) {
1540 return true;
1541 }
1542 if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS, packageUid)
1543 == PackageManager.PERMISSION_GRANTED) {
1544 return true;
1545 }
1546 if (PermissionChecker.checkPermissionForPreflight(this, INTERACT_ACROSS_PROFILES,
1547 PID_UNKNOWN, packageUid, packageName) == PackageManager.PERMISSION_GRANTED) {
1548 return true;
1549 }
1550 return false;
1551 }
1552
1553 private boolean isAutolaunching() {
1554 return !mRegistered && isFinishing();
1555 }
1556
arangelovcf268642020-01-15 15:09:51 +00001557 private void setupProfileTabs() {
1558 TabHost tabHost = findViewById(R.id.profile_tabhost);
1559 tabHost.setup();
1560 ViewPager viewPager = findViewById(R.id.profile_pager);
1561 TabHost.TabSpec tabSpec = tabHost.newTabSpec(TAB_TAG_PERSONAL)
1562 .setContent(R.id.profile_pager)
1563 .setIndicator(getString(R.string.resolver_personal_tab));
1564 tabHost.addTab(tabSpec);
1565
1566 tabSpec = tabHost.newTabSpec(TAB_TAG_WORK)
1567 .setContent(R.id.profile_pager)
1568 .setIndicator(getString(R.string.resolver_work_tab));
1569 tabHost.addTab(tabSpec);
1570
1571 TabWidget tabWidget = tabHost.getTabWidget();
1572 tabWidget.setVisibility(View.VISIBLE);
1573 resetTabsHeaderStyle(tabWidget);
1574 updateActiveTabStyle(tabHost);
1575
1576 tabHost.setOnTabChangedListener(tabId -> {
1577 resetTabsHeaderStyle(tabWidget);
1578 updateActiveTabStyle(tabHost);
1579 if (TAB_TAG_PERSONAL.equals(tabId)) {
1580 viewPager.setCurrentItem(0);
1581 } else {
1582 viewPager.setCurrentItem(1);
1583 }
1584 setupViewVisibilities();
Susi Kharraz-Postdde9c3a2020-03-03 07:55:40 -05001585 maybeLogProfileChange();
arangelov4bf17472020-02-17 20:21:46 +00001586 DevicePolicyEventLogger
1587 .createEvent(DevicePolicyEnums.RESOLVER_SWITCH_TABS)
1588 .setInt(viewPager.getCurrentItem())
1589 .setStrings(getMetricsCategory())
1590 .write();
arangelovcf268642020-01-15 15:09:51 +00001591 });
1592
1593 viewPager.setVisibility(View.VISIBLE);
1594 tabHost.setCurrentTab(mMultiProfilePagerAdapter.getCurrentPage());
arangelov349af6a2020-03-05 11:53:40 +00001595 mMultiProfilePagerAdapter.setOnProfileSelectedListener(
1596 index -> {
1597 tabHost.setCurrentTab(index);
1598 resetButtonBar();
1599 resetCheckedItem();
1600 });
arangelov934c64c2020-02-14 13:48:23 +00001601 findViewById(R.id.resolver_tab_divider).setVisibility(View.VISIBLE);
arangelovcf268642020-01-15 15:09:51 +00001602 }
1603
arangelov349af6a2020-03-05 11:53:40 +00001604 private void resetCheckedItem() {
1605 if (!isIntentPicker()) {
1606 return;
1607 }
1608 mLastSelected = ListView.INVALID_POSITION;
1609 ListView inactiveListView = (ListView) mMultiProfilePagerAdapter.getInactiveAdapterView();
1610 if (inactiveListView.getCheckedItemCount() > 0) {
1611 inactiveListView.setItemChecked(inactiveListView.getCheckedItemPosition(), false);
1612 }
1613 }
1614
arangelovcf268642020-01-15 15:09:51 +00001615 private void resetTabsHeaderStyle(TabWidget tabWidget) {
arangelov791509d2020-03-20 13:01:53 +00001616 String workContentDescription = getString(R.string.resolver_work_tab_accessibility);
1617 String personalContentDescription = getString(R.string.resolver_personal_tab_accessibility);
arangelovcf268642020-01-15 15:09:51 +00001618 for (int i = 0; i < tabWidget.getChildCount(); i++) {
arangelov78fd3602020-02-28 15:21:20 +00001619 View tabView = tabWidget.getChildAt(i);
1620 TextView title = tabView.findViewById(android.R.id.title);
arangelov4b8ad342020-03-17 13:56:00 +00001621 title.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_DialogWindowTitle);
1622 title.setTextColor(getAttrColor(this, android.R.attr.textColorTertiary));
1623 title.setTextSize(TypedValue.COMPLEX_UNIT_PX,
1624 getResources().getDimension(R.dimen.resolver_tab_text_size));
arangelov791509d2020-03-20 13:01:53 +00001625 if (title.getText().equals(getString(R.string.resolver_personal_tab))) {
1626 tabView.setContentDescription(personalContentDescription);
1627 } else if (title.getText().equals(getString(R.string.resolver_work_tab))) {
1628 tabView.setContentDescription(workContentDescription);
1629 }
arangelovcf268642020-01-15 15:09:51 +00001630 }
1631 }
1632
arangelov4b8ad342020-03-17 13:56:00 +00001633 private static int getAttrColor(Context context, int attr) {
1634 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
1635 int colorAccent = ta.getColor(0, 0);
1636 ta.recycle();
1637 return colorAccent;
1638 }
1639
arangelovcf268642020-01-15 15:09:51 +00001640 private void updateActiveTabStyle(TabHost tabHost) {
1641 TextView title = tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
1642 .findViewById(android.R.id.title);
arangelov4b8ad342020-03-17 13:56:00 +00001643 title.setTextColor(getAttrColor(this, android.R.attr.colorAccent));
arangelovcf268642020-01-15 15:09:51 +00001644 }
1645
1646 private void setupViewVisibilities() {
arangelov590fba32020-02-11 18:05:42 +00001647 ResolverListAdapter activeListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
arangelovce7d4a62020-04-01 13:26:51 +01001648 if (!mMultiProfilePagerAdapter.shouldShowEmptyStateScreen(activeListAdapter)) {
arangelov590fba32020-02-11 18:05:42 +00001649 addUseDifferentAppLabelIfNecessary(activeListAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001650 }
1651 }
1652
Zhen Zhangbde7b462019-11-11 11:49:33 -08001653 /**
arangelovcf268642020-01-15 15:09:51 +00001654 * Add a label to signify that the user can pick a different app.
Zhen Zhangbde7b462019-11-11 11:49:33 -08001655 * @param adapter The adapter used to provide data to item views.
Zhen Zhangbde7b462019-11-11 11:49:33 -08001656 */
arangelovcf268642020-01-15 15:09:51 +00001657 public void addUseDifferentAppLabelIfNecessary(ResolverListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001658 final boolean useHeader = adapter.hasFilteredItem();
arangelovcf268642020-01-15 15:09:51 +00001659 if (useHeader) {
1660 FrameLayout stub = findViewById(R.id.stub);
1661 stub.setVisibility(View.VISIBLE);
1662 TextView textView = (TextView) LayoutInflater.from(this).inflate(
1663 R.layout.resolver_different_item_header, null, false);
arangelovf163a882020-02-18 17:18:47 +00001664 if (shouldShowTabs()) {
arangelovcf268642020-01-15 15:09:51 +00001665 textView.setGravity(Gravity.CENTER);
1666 }
1667 stub.addView(textView);
1668 }
1669 }
1670
1671 private void setupAdapterListView(ListView listView, ItemClickListener listener) {
arangelov38a6fce2019-12-02 18:21:22 +00001672 listView.setOnItemClickListener(listener);
1673 listView.setOnItemLongClickListener(listener);
Adam Powell7d758002015-05-06 17:49:36 -07001674
shafik69df96322018-12-18 15:41:19 +00001675 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001676 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1677 }
Adam Powell24428412015-04-01 17:19:56 -07001678 }
1679
Matt Pietal26038402019-01-08 07:29:34 -05001680 /**
1681 * Configure the area above the app selection list (title, content preview, etc).
1682 */
1683 public void setHeader() {
arangelova3912cf2019-12-13 14:34:45 +00001684 if (mMultiProfilePagerAdapter.getActiveListAdapter().getCount() == 0
1685 && mMultiProfilePagerAdapter.getActiveListAdapter().getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001686 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001687 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001688 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001689 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001690 }
1691
1692 CharSequence title = mTitle != null
1693 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001694 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001695
1696 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001697 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001698 if (titleView != null) {
1699 titleView.setText(title);
1700 }
1701 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001702 }
1703
Alan Viverette51efddb2017-04-05 10:00:01 -04001704 final ImageView iconView = findViewById(R.id.icon);
arangelovb0802dc2019-10-18 18:03:44 +01001705 if (iconView != null) {
arangelova3912cf2019-12-13 14:34:45 +00001706 mMultiProfilePagerAdapter.getActiveListAdapter().loadFilteredItemIconTaskAsync(iconView);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001707 }
1708 }
1709
arangelov38a6fce2019-12-02 18:21:22 +00001710 protected void resetButtonBar() {
shafik69df96322018-12-18 15:41:19 +00001711 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1712 return;
1713 }
1714 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1715 if (buttonLayout != null) {
1716 buttonLayout.setVisibility(View.VISIBLE);
Matt Pietal800136a2019-05-08 07:46:39 -04001717
Matt Pietald5071202019-10-10 11:21:28 -04001718 if (!useLayoutWithDefault()) {
1719 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1720 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
1721 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1722 R.dimen.resolver_button_bar_spacing) + inset);
1723 }
shafik69df96322018-12-18 15:41:19 +00001724 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
shafik69df96322018-12-18 15:41:19 +00001725 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1726
Matt Pietal0d6834a2019-06-27 13:27:52 -04001727 resetAlwaysOrOnceButtonBar();
shafik69df96322018-12-18 15:41:19 +00001728 } else {
1729 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1730 }
1731 }
1732
shafik69df96322018-12-18 15:41:19 +00001733 private void resetAlwaysOrOnceButtonBar() {
arangelov349af6a2020-03-05 11:53:40 +00001734 // Disable both buttons initially
1735 setAlwaysButtonEnabled(false, ListView.INVALID_POSITION, false);
1736 mOnceButton.setEnabled(false);
1737
arangelova3912cf2019-12-13 14:34:45 +00001738 int filteredPosition = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001739 .getFilteredPosition();
1740 if (useLayoutWithDefault() && filteredPosition != ListView.INVALID_POSITION) {
1741 setAlwaysButtonEnabled(true, filteredPosition, false);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001742 mOnceButton.setEnabled(true);
Sergey Troshin40e979e2019-12-30 17:29:50 +01001743 // Focus the button if we already have the default option
1744 mOnceButton.requestFocus();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001745 return;
1746 }
1747
1748 // When the items load in, if an item was already selected, enable the buttons
arangelovcf268642020-01-15 15:09:51 +00001749 ListView currentAdapterView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelov38a6fce2019-12-02 18:21:22 +00001750 if (currentAdapterView != null
1751 && currentAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1752 setAlwaysButtonEnabled(true, currentAdapterView.getCheckedItemPosition(), true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001753 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001754 }
1755 }
1756
arangelovb0802dc2019-10-18 18:03:44 +01001757 @Override // ResolverListCommunicator
1758 public boolean useLayoutWithDefault() {
arangelovcf268642020-01-15 15:09:51 +00001759 // We only use the default app layout when the profile of the active user has a
1760 // filtered item. We always show the same default app even in the inactive user profile.
1761 boolean currentUserAdapterHasFilteredItem;
1762 if (mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier()
1763 == UserHandle.myUserId()) {
1764 currentUserAdapterHasFilteredItem =
1765 mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem();
1766 } else {
1767 currentUserAdapterHasFilteredItem =
1768 mMultiProfilePagerAdapter.getInactiveListAdapter().hasFilteredItem();
1769 }
1770 return mSupportsAlwaysUseOption && currentUserAdapterHasFilteredItem;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001771 }
1772
Adam Powellc412be62015-06-24 13:54:10 -07001773 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001774 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1775 * called and we are launched in a new task.
1776 */
1777 protected void setRetainInOnStop(boolean retainInOnStop) {
1778 mRetainInOnStop = retainInOnStop;
1779 }
1780
1781 /**
Adam Powellc412be62015-06-24 13:54:10 -07001782 * Check a simple match for the component of two ResolveInfos.
1783 */
arangelovb0802dc2019-10-18 18:03:44 +01001784 @Override // ResolverListCommunicator
1785 public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
Adam Powellc412be62015-06-24 13:54:10 -07001786 return lhs == null ? rhs == null
1787 : lhs.activityInfo == null ? rhs.activityInfo == null
1788 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1789 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1790 }
1791
arangelov4bf17472020-02-17 20:21:46 +00001792 protected String getMetricsCategory() {
1793 return METRICS_CATEGORY_RESOLVER;
1794 }
1795
arangelovb0802dc2019-10-18 18:03:44 +01001796 @Override // ResolverListCommunicator
arangelov7981b122020-01-16 10:58:27 +00001797 public void onHandlePackagesChanged(ResolverListAdapter listAdapter) {
1798 if (listAdapter == mMultiProfilePagerAdapter.getActiveListAdapter()) {
arangelove5b369c2020-03-12 17:36:05 +00001799 if (listAdapter.getUserHandle().equals(getWorkProfileUserHandle())
arangelov2dd9f7b2020-02-27 10:54:24 +00001800 && mMultiProfilePagerAdapter.isWaitingToEnableWorkProfile()) {
1801 // We have just turned on the work profile and entered the pass code to start it,
1802 // now we are waiting to receive the ACTION_USER_UNLOCKED broadcast. There is no
1803 // point in reloading the list now, since the work profile user is still
1804 // turning on.
1805 return;
1806 }
arangelov7981b122020-01-16 10:58:27 +00001807 boolean listRebuilt = mMultiProfilePagerAdapter.rebuildActiveTab(true);
1808 if (listRebuilt) {
1809 ResolverListAdapter activeListAdapter =
1810 mMultiProfilePagerAdapter.getActiveListAdapter();
1811 activeListAdapter.notifyDataSetChanged();
1812 if (activeListAdapter.getCount() == 0) {
1813 // We no longer have any items... just finish the activity.
1814 finish();
1815 }
1816 }
1817 } else {
1818 mMultiProfilePagerAdapter.clearInactiveProfileCache();
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001819 }
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001820 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001821
arangelov7f8743d2020-02-13 20:34:30 +00001822 private BroadcastReceiver createWorkProfileStateReceiver() {
1823 return new BroadcastReceiver() {
1824 @Override
1825 public void onReceive(Context context, Intent intent) {
1826 String action = intent.getAction();
arangelov2dd9f7b2020-02-27 10:54:24 +00001827 if (!TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)
arangelov7f8743d2020-02-13 20:34:30 +00001828 && !TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) {
1829 return;
1830 }
arangelov2dd9f7b2020-02-27 10:54:24 +00001831 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1832 if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)
1833 && userHandle != getWorkProfileUserHandle().getIdentifier()) {
1834 return;
1835 }
1836 if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)) {
1837 mMultiProfilePagerAdapter.markWorkProfileEnabledBroadcastReceived();
1838 }
arangelov7f8743d2020-02-13 20:34:30 +00001839 if (mMultiProfilePagerAdapter.getCurrentUserHandle()
arangelove5b369c2020-03-12 17:36:05 +00001840 .equals(getWorkProfileUserHandle())) {
arangelov7f8743d2020-02-13 20:34:30 +00001841 mMultiProfilePagerAdapter.rebuildActiveTab(true);
1842 } else {
1843 mMultiProfilePagerAdapter.clearInactiveProfileCache();
1844 }
1845 }
1846 };
1847 }
1848
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001849 @VisibleForTesting
1850 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001851 public final ComponentName name;
1852 private final List<Intent> mIntents = new ArrayList<>();
1853 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001854 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001855
1856 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1857 this.name = name;
1858 add(intent, info);
1859 }
1860
1861 public void add(Intent intent, ResolveInfo info) {
1862 mIntents.add(intent);
1863 mResolveInfos.add(info);
1864 }
1865
1866 public int getCount() {
1867 return mIntents.size();
1868 }
1869
1870 public Intent getIntentAt(int index) {
1871 return index >= 0 ? mIntents.get(index) : null;
1872 }
1873
1874 public ResolveInfo getResolveInfoAt(int index) {
1875 return index >= 0 ? mResolveInfos.get(index) : null;
1876 }
1877
1878 public int findIntent(Intent intent) {
1879 for (int i = 0, N = mIntents.size(); i < N; i++) {
1880 if (intent.equals(mIntents.get(i))) {
1881 return i;
1882 }
1883 }
1884 return -1;
1885 }
1886
1887 public int findResolveInfo(ResolveInfo info) {
1888 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1889 if (info.equals(mResolveInfos.get(i))) {
1890 return i;
1891 }
1892 }
1893 return -1;
1894 }
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001895
1896 public boolean isPinned() {
1897 return mPinned;
1898 }
1899
1900 public void setPinned(boolean pinned) {
1901 mPinned = pinned;
1902 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001903 }
1904
Adam Powell7d758002015-05-06 17:49:36 -07001905 class ItemClickListener implements AdapterView.OnItemClickListener,
1906 AdapterView.OnItemLongClickListener {
1907 @Override
1908 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1909 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1910 if (listView != null) {
1911 position -= listView.getHeaderViewsCount();
1912 }
1913 if (position < 0) {
1914 // Header views don't count.
1915 return;
1916 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001917 // If we're still loading, we can't yet enable the buttons.
arangelova3912cf2019-12-13 14:34:45 +00001918 if (mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001919 .resolveInfoForPosition(position, true) == null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001920 return;
1921 }
arangelov38a6fce2019-12-02 18:21:22 +00001922 ListView currentAdapterView =
arangelovcf268642020-01-15 15:09:51 +00001923 (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelov38a6fce2019-12-02 18:21:22 +00001924 final int checkedPos = currentAdapterView.getCheckedItemPosition();
Adam Powell7d758002015-05-06 17:49:36 -07001925 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001926 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07001927 && (!hasValidSelection || mLastSelected != checkedPos)
Matt Pietal0d6834a2019-06-27 13:27:52 -04001928 && mAlwaysButton != null) {
1929 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Adam Powell7d758002015-05-06 17:49:36 -07001930 mOnceButton.setEnabled(hasValidSelection);
1931 if (hasValidSelection) {
arangelov38a6fce2019-12-02 18:21:22 +00001932 currentAdapterView.smoothScrollToPosition(checkedPos);
Sergey Troshin40e979e2019-12-30 17:29:50 +01001933 mOnceButton.requestFocus();
Adam Powell7d758002015-05-06 17:49:36 -07001934 }
1935 mLastSelected = checkedPos;
1936 } else {
1937 startSelected(position, false, true);
1938 }
1939 }
Adam Powell2d809622012-03-22 15:24:43 -07001940
1941 @Override
1942 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001943 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1944 if (listView != null) {
1945 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001946 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001947 if (position < 0) {
1948 // Header views don't count.
1949 return false;
1950 }
arangelova3912cf2019-12-13 14:34:45 +00001951 ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001952 .resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001953 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001954 return true;
1955 }
1956
1957 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001958
Dianne Hackbornec452d92014-11-11 17:16:56 -08001959 static final boolean isSpecificUriMatch(int match) {
1960 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1961 return match >= IntentFilter.MATCH_CATEGORY_HOST
1962 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1963 }
1964
Adam Powell4c470d62015-06-19 17:46:17 -07001965 static class PickTargetOptionRequest extends PickOptionRequest {
1966 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1967 @Nullable Bundle extras) {
1968 super(prompt, options, extras);
1969 }
1970
1971 @Override
1972 public void onCancel() {
1973 super.onCancel();
1974 final ResolverActivity ra = (ResolverActivity) getActivity();
1975 if (ra != null) {
1976 ra.mPickOptionRequest = null;
1977 ra.finish();
1978 }
1979 }
1980
1981 @Override
1982 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1983 super.onPickOptionResult(finished, selections, result);
1984 if (selections.length != 1) {
1985 // TODO In a better world we would filter the UI presented here and let the
1986 // user refine. Maybe later.
1987 return;
1988 }
1989
1990 final ResolverActivity ra = (ResolverActivity) getActivity();
1991 if (ra != null) {
arangelova3912cf2019-12-13 14:34:45 +00001992 final TargetInfo ti = ra.mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001993 .getItem(selections[0].getIndex());
Adam Powell4c470d62015-06-19 17:46:17 -07001994 if (ra.onTargetSelected(ti, false)) {
1995 ra.mPickOptionRequest = null;
1996 ra.finish();
1997 }
1998 }
1999 }
2000 }
Susi Kharraz-Postdde9c3a2020-03-03 07:55:40 -05002001
2002 protected void maybeLogProfileChange() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003}