blob: 3084f2a84945f87629e73c2f47f2aa767f997c94 [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;
arangelov9d894142020-04-21 21:08:21 +0100149 private static final String LAST_SHOWN_TAB_KEY = "last_shown_tab_key";
Adam Powell63b31692015-09-28 10:45:00 -0700150
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700151 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100152
Matt Pietal800136a2019-05-08 07:46:39 -0400153 protected Insets mSystemWindowInsets = null;
154 private Space mFooterSpacer = null;
155
Jorim Jaggif631ef72017-02-24 13:49:47 +0100156 /** See {@link #setRetainInOnStop}. */
157 private boolean mRetainInOnStop;
158
Matt Pietal0d6834a2019-06-27 13:27:52 -0400159 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
160 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
161 private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state";
arangelov4bf17472020-02-17 20:21:46 +0000162 protected static final String METRICS_CATEGORY_RESOLVER = "intent_resolver";
163 protected static final String METRICS_CATEGORY_CHOOSER = "intent_chooser";
Matt Pietal0d6834a2019-06-27 13:27:52 -0400164
arangelov38a6fce2019-12-02 18:21:22 +0000165 /**
166 * TODO(arangelov): Remove a couple of weeks after work/personal tabs are finalized.
167 */
arangelovcf268642020-01-15 15:09:51 +0000168 @VisibleForTesting
arangelov85974ed2020-03-06 15:48:19 +0000169 public static boolean ENABLE_TABBED_VIEW = true;
arangelovcf268642020-01-15 15:09:51 +0000170 private static final String TAB_TAG_PERSONAL = "personal";
171 private static final String TAB_TAG_WORK = "work";
arangelov38a6fce2019-12-02 18:21:22 +0000172
arangelov7981b122020-01-16 10:58:27 +0000173 private PackageMonitor mPersonalPackageMonitor;
arangelovbdb40052020-01-09 18:01:58 +0000174 private PackageMonitor mWorkPackageMonitor;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800175
arangelov38a6fce2019-12-02 18:21:22 +0000176 @VisibleForTesting
177 protected AbstractMultiProfilePagerAdapter mMultiProfilePagerAdapter;
178
Paul McLean07425c82019-10-18 12:00:11 -0600179 // Intent extra for connected audio devices
180 public static final String EXTRA_IS_AUDIO_CAPTURE_DEVICE = "is_audio_capture_device";
181
arangelov7f8743d2020-02-13 20:34:30 +0000182 private BroadcastReceiver mWorkProfileStateReceiver;
arangelov4e2951b2020-04-23 18:59:09 +0100183 private UserHandle mHeaderCreatorUser;
arangelov7f8743d2020-02-13 20:34:30 +0000184
arangelov706316d2020-05-06 14:25:06 +0100185 private UserHandle mWorkProfileUserHandle;
186
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700187 /**
188 * Get the string resource to be used as a label for the link to the resolver activity for an
189 * action.
190 *
191 * @param action The action to resolve
192 *
193 * @return The string resource to be used as a label
194 */
195 public static @StringRes int getLabelRes(String action) {
196 return ActionTitle.forAction(action).labelRes;
197 }
198
Adam Powell278902c2014-07-12 18:33:22 -0700199 private enum ActionTitle {
200 VIEW(Intent.ACTION_VIEW,
201 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700202 com.android.internal.R.string.whichViewApplicationNamed,
203 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700204 EDIT(Intent.ACTION_EDIT,
205 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700206 com.android.internal.R.string.whichEditApplicationNamed,
207 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700208 SEND(Intent.ACTION_SEND,
209 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700210 com.android.internal.R.string.whichSendApplicationNamed,
211 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700212 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700213 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700214 com.android.internal.R.string.whichSendToApplicationNamed,
215 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700216 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
217 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700218 com.android.internal.R.string.whichSendApplicationNamed,
219 com.android.internal.R.string.whichSendApplicationLabel),
220 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
221 com.android.internal.R.string.whichImageCaptureApplication,
222 com.android.internal.R.string.whichImageCaptureApplicationNamed,
223 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700224 DEFAULT(null,
225 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700226 com.android.internal.R.string.whichApplicationNamed,
227 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700228 HOME(Intent.ACTION_MAIN,
229 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700230 com.android.internal.R.string.whichHomeApplicationNamed,
231 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700232
shafik0c7c5d52019-02-27 12:13:25 +0000233 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000234 public static final int BROWSABLE_TITLE_RES =
Matt Pietal0d6834a2019-06-27 13:27:52 -0400235 com.android.internal.R.string.whichOpenLinksWith;
236 public static final int BROWSABLE_HOST_TITLE_RES =
237 com.android.internal.R.string.whichOpenHostLinksWith;
238 public static final int BROWSABLE_HOST_APP_TITLE_RES =
239 com.android.internal.R.string.whichOpenHostLinksWithApp;
240 public static final int BROWSABLE_APP_TITLE_RES =
241 com.android.internal.R.string.whichOpenLinksWithApp;
shafik69df96322018-12-18 15:41:19 +0000242
Adam Powell278902c2014-07-12 18:33:22 -0700243 public final String action;
244 public final int titleRes;
245 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700246 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700247
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700248 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700249 this.action = action;
250 this.titleRes = titleRes;
251 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700252 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700253 }
254
255 public static ActionTitle forAction(String action) {
256 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700257 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700258 return title;
259 }
260 }
261 return DEFAULT;
262 }
263 }
264
arangelov7981b122020-01-16 10:58:27 +0000265 protected PackageMonitor createPackageMonitor(ResolverListAdapter listAdapter) {
Matt Pietalab73a882019-06-05 07:04:55 -0400266 return new PackageMonitor() {
267 @Override
268 public void onSomePackagesChanged() {
arangelov7981b122020-01-16 10:58:27 +0000269 listAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100270 updateProfileViewButton();
Matt Pietalab73a882019-06-05 07:04:55 -0400271 }
272
273 @Override
274 public boolean onPackageChanged(String packageName, int uid, String[] components) {
275 // We care about all package changes, not just the whole package itself which is
276 // default behavior.
277 return true;
278 }
279 };
280 }
281
Dianne Hackborn905577f2011-09-07 18:31:28 -0700282 private Intent makeMyIntent() {
283 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700284 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700285 // The resolver activity is set to be hidden from recent tasks.
286 // we don't want this attribute to be propagated to the next activity
287 // being launched. Note that if the original Intent also had this
288 // flag set, we are now losing it. That should be a very rare case
289 // and we can live with this.
290 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
291 return intent;
292 }
293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 @Override
295 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700296 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700297 final Intent intent = makeMyIntent();
298 final Set<String> categories = intent.getCategories();
299 if (Intent.ACTION_MAIN.equals(intent.getAction())
300 && categories != null
301 && categories.size() == 1
302 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100303 // Note: this field is not set to true in the compatibility version.
304 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700305 }
306
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700307 setSafeForwardingMode(true);
308
Adam Powella35c77a2014-09-25 16:46:36 -0700309 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
311
Adam Powell278902c2014-07-12 18:33:22 -0700312 /**
Adam Powell24428412015-04-01 17:19:56 -0700313 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700314 * a default title resource
315 */
Andrei Onea15884392019-03-22 17:28:11 +0000316 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700318 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800319 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
320 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
321 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700322 }
323
324 protected void onCreate(Bundle savedInstanceState, Intent intent,
325 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800326 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700327 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000329
330 // Determine whether we should show that intent is forwarded
331 // from managed profile to owner or other way around.
332 setProfileSwitchMessageId(intent.getContentUserHint());
333
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700334 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700335 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700336 getActivityToken());
337 } catch (RemoteException e) {
338 mLaunchedFromUid = -1;
339 }
Adam Powell7d758002015-05-06 17:49:36 -0700340
341 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
342 // Gulp!
343 finish();
344 return;
345 }
346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700348
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800349 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800350
Adam Powell7d758002015-05-06 17:49:36 -0700351 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700352 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800353 mTitle = title;
354 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700355
shafik69df96322018-12-18 15:41:19 +0000356 mUseLayoutForBrowsables = getTargetIntent() == null
357 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000358 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000359
Matt Pietal0d6834a2019-06-27 13:27:52 -0400360 mSupportsAlwaysUseOption = supportsAlwaysUseOption;
arangelov85192182020-05-13 13:19:19 +0100361 mWorkProfileUserHandle = fetchWorkProfileUserProfile();
shafik69df96322018-12-18 15:41:19 +0000362
arangelov38a6fce2019-12-02 18:21:22 +0000363 // The last argument of createResolverListAdapter is whether to do special handling
arangelovb0802dc2019-10-18 18:03:44 +0100364 // of the last used choice to highlight it in the list. We need to always
365 // turn this off when running under voice interaction, since it results in
366 // a more complicated UI that the current voice interaction flow is not able
arangelovaa307e22020-05-07 16:14:32 +0100367 // to handle. We also turn it off when the work tab is shown to simplify the UX.
368 boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction()
369 && !shouldShowTabs();
arangelov38a6fce2019-12-02 18:21:22 +0000370 mMultiProfilePagerAdapter = createMultiProfilePagerAdapter(initialIntents, rList, filterLastUsed);
arangelovbb572332019-11-13 12:50:08 +0000371 if (configureContentView()) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700372 return;
373 }
Adam Powell278902c2014-07-12 18:33:22 -0700374
arangelov7981b122020-01-16 10:58:27 +0000375 mPersonalPackageMonitor = createPackageMonitor(
376 mMultiProfilePagerAdapter.getPersonalListAdapter());
377 mPersonalPackageMonitor.register(
378 this, getMainLooper(), getPersonalProfileUserHandle(), false);
arangelovf163a882020-02-18 17:18:47 +0000379 if (shouldShowTabs()) {
arangelov7981b122020-01-16 10:58:27 +0000380 mWorkPackageMonitor = createPackageMonitor(
381 mMultiProfilePagerAdapter.getWorkListAdapter());
382 mWorkPackageMonitor.register(this, getMainLooper(), getWorkProfileUserHandle(), false);
383 }
384
385 mRegistered = true;
386
Alan Viverette51efddb2017-04-05 10:00:01 -0400387 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700388 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700389 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700390 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700391 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700392 finish();
393 }
394 });
Sergey Troshin40e979e2019-12-30 17:29:50 +0100395
396 boolean hasTouchScreen = getPackageManager()
397 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
398
399 if (isVoiceInteraction() || !hasTouchScreen) {
Adam Powell4c470d62015-06-19 17:46:17 -0700400 rdl.setCollapsed(false);
401 }
Matt Pietal800136a2019-05-08 07:46:39 -0400402
403 rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
404 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
405 rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets);
406
Adam Powell63b31692015-09-28 10:45:00 -0700407 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700408 }
409
Adam Powell88831a22014-11-20 18:17:00 -0800410 mProfileView = findViewById(R.id.profile_button);
411 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400412 mProfileView.setOnClickListener(this::onProfileClick);
arangelovb0802dc2019-10-18 18:03:44 +0100413 updateProfileViewButton();
Adam Powell88831a22014-11-20 18:17:00 -0800414 }
Adam Powell4c470d62015-06-19 17:46:17 -0700415
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100416 final Set<String> categories = intent.getCategories();
arangelova3912cf2019-12-13 14:34:45 +0000417 MetricsLogger.action(this, mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem()
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100418 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
419 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
420 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100421 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700422 }
423
arangelovc4dbdbd2020-02-18 20:54:16 +0000424 private boolean isIntentPicker() {
425 return getClass().equals(ResolverActivity.class);
426 }
427
arangelov38a6fce2019-12-02 18:21:22 +0000428 protected AbstractMultiProfilePagerAdapter createMultiProfilePagerAdapter(
429 Intent[] initialIntents,
430 List<ResolveInfo> rList,
431 boolean filterLastUsed) {
432 AbstractMultiProfilePagerAdapter resolverMultiProfilePagerAdapter = null;
arangelovf163a882020-02-18 17:18:47 +0000433 if (shouldShowTabs()) {
arangelov38a6fce2019-12-02 18:21:22 +0000434 resolverMultiProfilePagerAdapter =
435 createResolverMultiProfilePagerAdapterForTwoProfiles(
436 initialIntents, rList, filterLastUsed);
437 } else {
438 resolverMultiProfilePagerAdapter = createResolverMultiProfilePagerAdapterForOneProfile(
439 initialIntents, rList, filterLastUsed);
440 }
441 return resolverMultiProfilePagerAdapter;
442 }
443
444 private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForOneProfile(
445 Intent[] initialIntents,
446 List<ResolveInfo> rList, boolean filterLastUsed) {
447 ResolverListAdapter adapter = createResolverListAdapter(
448 /* context */ this,
449 /* payloadIntents */ mIntents,
450 initialIntents,
451 rList,
452 filterLastUsed,
453 mUseLayoutForBrowsables,
454 /* userHandle */ UserHandle.of(UserHandle.myUserId()));
455 return new ResolverMultiProfilePagerAdapter(
456 /* context */ this,
arangelov7981b122020-01-16 10:58:27 +0000457 adapter,
458 getPersonalProfileUserHandle(),
459 /* workProfileUserHandle= */ null);
arangelov38a6fce2019-12-02 18:21:22 +0000460 }
461
462 private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForTwoProfiles(
463 Intent[] initialIntents,
464 List<ResolveInfo> rList,
465 boolean filterLastUsed) {
arangelov2c6115a2020-04-08 13:26:13 +0100466 // In the edge case when we have 0 apps in the current profile and >1 apps in the other,
467 // the intent resolver is started in the other profile. Since this is the only case when
468 // this happens, we check for it here and set the current profile's tab.
469 int selectedProfile = getCurrentProfile();
470 UserHandle intentUser = UserHandle.of(getLaunchingUserId());
471 if (!getUser().equals(intentUser)) {
472 if (getPersonalProfileUserHandle().equals(intentUser)) {
473 selectedProfile = PROFILE_PERSONAL;
474 } else if (getWorkProfileUserHandle().equals(intentUser)) {
475 selectedProfile = PROFILE_WORK;
476 }
477 }
arangelovacc82432020-04-28 12:55:55 +0100478 // We only show the default app for the profile of the current user. The filterLastUsed
479 // flag determines whether to show a default app and that app is not shown in the
480 // resolver list. So filterLastUsed should be false for the other profile.
481 ResolverListAdapter personalAdapter = createResolverListAdapter(
482 /* context */ this,
483 /* payloadIntents */ mIntents,
484 selectedProfile == PROFILE_PERSONAL ? initialIntents : null,
485 rList,
486 (filterLastUsed && UserHandle.myUserId()
487 == getPersonalProfileUserHandle().getIdentifier()),
488 mUseLayoutForBrowsables,
489 /* userHandle */ getPersonalProfileUserHandle());
490 UserHandle workProfileUserHandle = getWorkProfileUserHandle();
491 ResolverListAdapter workAdapter = createResolverListAdapter(
492 /* context */ this,
493 /* payloadIntents */ mIntents,
494 selectedProfile == PROFILE_WORK ? initialIntents : null,
495 rList,
496 (filterLastUsed && UserHandle.myUserId()
497 == workProfileUserHandle.getIdentifier()),
498 mUseLayoutForBrowsables,
499 /* userHandle */ workProfileUserHandle);
arangelov38a6fce2019-12-02 18:21:22 +0000500 return new ResolverMultiProfilePagerAdapter(
501 /* context */ this,
502 personalAdapter,
503 workAdapter,
arangelov2c6115a2020-04-08 13:26:13 +0100504 selectedProfile,
arangelov7981b122020-01-16 10:58:27 +0000505 getPersonalProfileUserHandle(),
arangelov2c6115a2020-04-08 13:26:13 +0100506 getWorkProfileUserHandle(),
507 /* shouldShowNoCrossProfileIntentsEmptyState= */ getUser().equals(intentUser));
508 }
509
510 /**
511 * Returns the user id of the user that the starting intent originated from.
512 * <p>This is not necessarily equal to {@link #getUserId()} or {@link UserHandle#myUserId()},
513 * as there are edge cases when the intent resolver is launched in the other profile.
514 * For example, when we have 0 resolved apps in current profile and multiple resolved apps
515 * in the other profile, opening a link from the current profile launches the intent resolver
516 * in the other one. b/148536209 for more info.
517 */
518 private int getLaunchingUserId() {
519 int contentUserHint = getIntent().getContentUserHint();
520 if (contentUserHint == UserHandle.USER_CURRENT) {
521 return UserHandle.myUserId();
522 }
523 return contentUserHint;
arangelov38a6fce2019-12-02 18:21:22 +0000524 }
525
526 protected @Profile int getCurrentProfile() {
527 return (UserHandle.myUserId() == UserHandle.USER_SYSTEM ? PROFILE_PERSONAL : PROFILE_WORK);
528 }
529
530 protected UserHandle getPersonalProfileUserHandle() {
531 return UserHandle.of(ActivityManager.getCurrentUser());
532 }
533 protected @Nullable UserHandle getWorkProfileUserHandle() {
arangelov706316d2020-05-06 14:25:06 +0100534 return mWorkProfileUserHandle;
535 }
536
537 protected @Nullable UserHandle fetchWorkProfileUserProfile() {
538 mWorkProfileUserHandle = null;
arangelov38a6fce2019-12-02 18:21:22 +0000539 UserManager userManager = getSystemService(UserManager.class);
540 for (final UserInfo userInfo : userManager.getProfiles(ActivityManager.getCurrentUser())) {
541 if (userInfo.isManagedProfile()) {
arangelov706316d2020-05-06 14:25:06 +0100542 mWorkProfileUserHandle = userInfo.getUserHandle();
arangelov38a6fce2019-12-02 18:21:22 +0000543 }
544 }
arangelov706316d2020-05-06 14:25:06 +0100545 return mWorkProfileUserHandle;
arangelov38a6fce2019-12-02 18:21:22 +0000546 }
547
arangelovf163a882020-02-18 17:18:47 +0000548 private boolean hasWorkProfile() {
arangelov38a6fce2019-12-02 18:21:22 +0000549 return getWorkProfileUserHandle() != null;
550 }
551
arangelovf163a882020-02-18 17:18:47 +0000552 protected boolean shouldShowTabs() {
553 return hasWorkProfile() && ENABLE_TABBED_VIEW;
554 }
555
Matt Pietal74c6ed02019-04-18 13:38:46 -0400556 protected void onProfileClick(View v) {
arangelov38a6fce2019-12-02 18:21:22 +0000557 final DisplayResolveInfo dri =
arangelova3912cf2019-12-13 14:34:45 +0000558 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400559 if (dri == null) {
560 return;
561 }
562
563 // Do not show the profile switch message anymore.
564 mProfileSwitchMessageId = -1;
565
566 onTargetSelected(dri, false);
567 finish();
568 }
569
Matt Pietal9236adc2019-12-12 09:24:23 -0500570 /**
571 * Numerous layouts are supported, each with optional ViewGroups.
572 * Make sure the inset gets added to the correct View, using
573 * a footer for Lists so it can properly scroll under the navbar.
574 */
575 protected boolean shouldAddFooterView() {
576 if (useLayoutWithDefault()) return true;
577
578 View buttonBar = findViewById(R.id.button_bar);
579 if (buttonBar == null || buttonBar.getVisibility() == View.GONE) return true;
580
581 return false;
582 }
583
584 protected void applyFooterView(int height) {
585 if (mFooterSpacer == null) {
586 mFooterSpacer = new Space(getApplicationContext());
587 } else {
588 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
arangelovcf268642020-01-15 15:09:51 +0000589 .getActiveAdapterView().removeFooterView(mFooterSpacer);
Matt Pietal9236adc2019-12-12 09:24:23 -0500590 }
591 mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
592 mSystemWindowInsets.bottom));
593 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
arangelovcf268642020-01-15 15:09:51 +0000594 .getActiveAdapterView().addFooterView(mFooterSpacer);
Matt Pietal9236adc2019-12-12 09:24:23 -0500595 }
596
Matt Pietal800136a2019-05-08 07:46:39 -0400597 protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
598 mSystemWindowInsets = insets.getSystemWindowInsets();
599
600 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
601 mSystemWindowInsets.right, 0);
602
Matt Pietal9236adc2019-12-12 09:24:23 -0500603 resetButtonBar();
604
Matt Pietald5071202019-10-10 11:21:28 -0400605 // Need extra padding so the list can fully scroll up
Matt Pietal9236adc2019-12-12 09:24:23 -0500606 if (shouldAddFooterView()) {
607 applyFooterView(mSystemWindowInsets.bottom);
Matt Pietal800136a2019-05-08 07:46:39 -0400608 }
Matt Pietal800136a2019-05-08 07:46:39 -0400609
610 return insets.consumeSystemWindowInsets();
611 }
612
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900613 @Override
614 public void onConfigurationChanged(Configuration newConfig) {
615 super.onConfigurationChanged(newConfig);
arangelova3912cf2019-12-13 14:34:45 +0000616 mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400617
618 if (mSystemWindowInsets != null) {
619 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
620 mSystemWindowInsets.right, 0);
621 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900622 }
623
arangelovb0802dc2019-10-18 18:03:44 +0100624 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -0800625 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700626 if (!isVoiceInteraction()) {
627 // Clearly not needed.
628 return;
629 }
630
arangelova3912cf2019-12-13 14:34:45 +0000631 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getCount();
arangelov38a6fce2019-12-02 18:21:22 +0000632 final Option[] options = new Option[count];
Adam Powell4c470d62015-06-19 17:46:17 -0700633 for (int i = 0, N = options.length; i < N; i++) {
arangelova3912cf2019-12-13 14:34:45 +0000634 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter().getItem(i);
Matt Pietal2cf4c6c2019-09-10 09:33:11 -0400635 if (target == null) {
636 // If this occurs, a new set of targets is being loaded. Let that complete,
637 // and have the next call to send voice choices proceed instead.
638 return;
639 }
640 options[i] = optionForChooserTarget(target, i);
Adam Powell4c470d62015-06-19 17:46:17 -0700641 }
642
643 mPickOptionRequest = new PickTargetOptionRequest(
644 new Prompt(getTitle()), options, null);
645 getVoiceInteractor().submitRequest(mPickOptionRequest);
646 }
647
648 Option optionForChooserTarget(TargetInfo target, int index) {
649 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800650 }
651
Adam Powell2ed547e2015-04-29 18:45:04 -0700652 protected final void setAdditionalTargets(Intent[] intents) {
653 if (intents != null) {
654 for (Intent intent : intents) {
655 mIntents.add(intent);
656 }
657 }
658 }
659
arangelovb0802dc2019-10-18 18:03:44 +0100660 @Override // SelectableTargetInfoCommunicator ResolverListCommunicator
Adam Powell0ccc0e92015-04-23 17:19:37 -0700661 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700662 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700663 }
664
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800665 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700666 final Uri referrer = getReferrer();
667 if (referrer != null && "android-app".equals(referrer.getScheme())) {
668 return referrer.getHost();
669 }
670 return null;
671 }
672
Adam Powell23882512016-01-29 10:21:00 -0800673 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700674 return R.layout.resolver_list;
675 }
676
arangelovb0802dc2019-10-18 18:03:44 +0100677 @Override // ResolverListCommunicator
678 public void updateProfileViewButton() {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400679 if (mProfileView == null) {
680 return;
681 }
682
arangelov38a6fce2019-12-02 18:21:22 +0000683 final DisplayResolveInfo dri =
arangelova3912cf2019-12-13 14:34:45 +0000684 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile();
arangelov2a4d0b12020-03-04 18:15:04 +0000685 if (dri != null && !shouldShowTabs()) {
Adam Powell88831a22014-11-20 18:17:00 -0800686 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800687 View text = mProfileView.findViewById(R.id.profile_button);
688 if (!(text instanceof TextView)) {
689 text = mProfileView.findViewById(R.id.text1);
690 }
691 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800692 } else {
693 mProfileView.setVisibility(View.GONE);
694 }
Adam Powell278902c2014-07-12 18:33:22 -0700695 }
696
Sander Alewijnsef6545332014-10-31 12:39:02 +0000697 private void setProfileSwitchMessageId(int contentUserHint) {
698 if (contentUserHint != UserHandle.USER_CURRENT &&
699 contentUserHint != UserHandle.myUserId()) {
700 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
701 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
702 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
703 : false;
704 boolean targetIsManaged = userManager.isManagedProfile();
705 if (originIsManaged && !targetIsManaged) {
706 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
707 } else if (!originIsManaged && targetIsManaged) {
708 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
709 }
710 }
711 }
712
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700713 /**
714 * Turn on launch mode that is safe to use when forwarding intents received from
715 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
716 * instead of the normal Activity.startActivity for launching the activity selected
717 * by the user.
718 *
719 * <p>This mode is set to true by default if the activity is initialized through
720 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
721 * methods, it is set to false by default. You must set it before calling one of the
722 * more detailed onCreate methods, so that it will be set correctly in the case where
723 * there is only one intent to resolve and it is thus started immediately.</p>
724 */
725 public void setSafeForwardingMode(boolean safeForwarding) {
726 mSafeForwardingMode = safeForwarding;
727 }
728
shafik69df96322018-12-18 15:41:19 +0000729 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
730 final ActionTitle title = mResolvingHome
731 ? ActionTitle.HOME
732 : ActionTitle.forAction(intent.getAction());
733
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800734 // While there may already be a filtered item, we can only use it in the title if the list
735 // is already sorted and all information relevant to it is already in the list.
arangelov38a6fce2019-12-02 18:21:22 +0000736 final boolean named =
arangelova3912cf2019-12-13 14:34:45 +0000737 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700738 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
739 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000740 } else if (isHttpSchemeAndViewAction(intent)) {
741 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000742 // they're giving access for the activity to open URLs from this specific host
Matt Pietal0d6834a2019-06-27 13:27:52 -0400743 String dialogTitle = null;
744 if (named && !mUseLayoutForBrowsables) {
745 dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES,
arangelova3912cf2019-12-13 14:34:45 +0000746 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredItem()
arangelov38a6fce2019-12-02 18:21:22 +0000747 .getDisplayLabel());
Matt Pietal0d6834a2019-06-27 13:27:52 -0400748 } else if (named && mUseLayoutForBrowsables) {
749 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES,
750 intent.getData().getHost(),
arangelova3912cf2019-12-13 14:34:45 +0000751 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredItem()
arangelov38a6fce2019-12-02 18:21:22 +0000752 .getDisplayLabel());
arangelova3912cf2019-12-13 14:34:45 +0000753 } else if (mMultiProfilePagerAdapter.getActiveListAdapter().areAllTargetsBrowsers()) {
Matt Pietald5071202019-10-10 11:21:28 -0400754 dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES);
Matt Pietal0d6834a2019-06-27 13:27:52 -0400755 } else {
756 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES,
757 intent.getData().getHost());
758 }
759 return dialogTitle;
Adam Powell278902c2014-07-12 18:33:22 -0700760 } else {
Adam Powell24428412015-04-01 17:19:56 -0700761 return named
arangelov38a6fce2019-12-02 18:21:22 +0000762 ? getString(title.namedTitleRes, mMultiProfilePagerAdapter
arangelova3912cf2019-12-13 14:34:45 +0000763 .getActiveListAdapter().getFilteredItem().getDisplayLabel())
Adam Powell24428412015-04-01 17:19:56 -0700764 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700765 }
Adam Powellc5878612012-05-04 18:42:38 -0700766 }
767
Adam Powelle9414d92014-07-05 17:44:18 -0700768 void dismiss() {
769 if (!isFinishing()) {
770 finish();
771 }
772 }
773
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800774 @Override
775 protected void onRestart() {
776 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700777 if (!mRegistered) {
arangelovbdb40052020-01-09 18:01:58 +0000778 mPersonalPackageMonitor.register(this, getMainLooper(),
779 getPersonalProfileUserHandle(), false);
arangelovf163a882020-02-18 17:18:47 +0000780 if (shouldShowTabs()) {
arangelovbdb40052020-01-09 18:01:58 +0000781 if (mWorkPackageMonitor == null) {
arangelov7981b122020-01-16 10:58:27 +0000782 mWorkPackageMonitor = createPackageMonitor(
783 mMultiProfilePagerAdapter.getWorkListAdapter());
arangelovbdb40052020-01-09 18:01:58 +0000784 }
785 mWorkPackageMonitor.register(this, getMainLooper(),
786 getWorkProfileUserHandle(), false);
787 }
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700788 mRegistered = true;
789 }
arangelovafec9d02020-03-17 12:41:01 +0000790 if (shouldShowTabs() && mMultiProfilePagerAdapter.isWaitingToEnableWorkProfile()) {
791 if (mMultiProfilePagerAdapter.isQuietModeEnabled(getWorkProfileUserHandle())) {
792 mMultiProfilePagerAdapter.markWorkProfileEnabledBroadcastReceived();
793 }
794 }
arangelova3912cf2019-12-13 14:34:45 +0000795 mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100796 updateProfileViewButton();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800797 }
798
799 @Override
arangelov7f8743d2020-02-13 20:34:30 +0000800 protected void onStart() {
801 super.onStart();
arangelovf163a882020-02-18 17:18:47 +0000802 if (shouldShowTabs()) {
arangelov7f8743d2020-02-13 20:34:30 +0000803 mWorkProfileStateReceiver = createWorkProfileStateReceiver();
804 registerWorkProfileStateReceiver();
805 }
806 }
807
808 private void registerWorkProfileStateReceiver() {
809 IntentFilter filter = new IntentFilter();
arangelov2dd9f7b2020-02-27 10:54:24 +0000810 filter.addAction(Intent.ACTION_USER_UNLOCKED);
arangelov7f8743d2020-02-13 20:34:30 +0000811 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
812 registerReceiverAsUser(mWorkProfileStateReceiver, UserHandle.ALL, filter, null, null);
813 }
814
815 @Override
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800816 protected void onStop() {
817 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700818 if (mRegistered) {
arangelovbdb40052020-01-09 18:01:58 +0000819 mPersonalPackageMonitor.unregister();
820 if (mWorkPackageMonitor != null) {
821 mWorkPackageMonitor.unregister();
822 }
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700823 mRegistered = false;
824 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700825 final Intent intent = getIntent();
826 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100827 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700828 // This resolver is in the unusual situation where it has been
829 // launched at the top of a new task. We don't let it be added
830 // to the recent tasks shown to the user, and we need to make sure
831 // that each time we are launched we get the correct launching
832 // uid (not re-using the same resolver from an old launching uid),
833 // so we will now finish ourself since being no longer visible,
834 // the user probably can't get back to us.
835 if (!isChangingConfigurations()) {
836 finish();
837 }
838 }
arangelov7f8743d2020-02-13 20:34:30 +0000839 if (mWorkPackageMonitor != null) {
840 unregisterReceiver(mWorkProfileStateReceiver);
841 mWorkPackageMonitor = null;
842 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800843 }
844
Adam Powellc5878612012-05-04 18:42:38 -0700845 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700846 protected void onDestroy() {
847 super.onDestroy();
848 if (!isChangingConfigurations() && mPickOptionRequest != null) {
849 mPickOptionRequest.cancel();
850 }
arangelova3912cf2019-12-13 14:34:45 +0000851 if (mMultiProfilePagerAdapter.getActiveListAdapter() != null) {
852 mMultiProfilePagerAdapter.getActiveListAdapter().onDestroy();
Kang Li38a6da642017-04-05 12:30:55 -0700853 }
Adam Powell4c470d62015-06-19 17:46:17 -0700854 }
855
856 @Override
arangelov9d894142020-04-21 21:08:21 +0100857 protected void onSaveInstanceState(Bundle outState) {
858 super.onSaveInstanceState(outState);
859 ViewPager viewPager = findViewById(R.id.profile_pager);
860 outState.putInt(LAST_SHOWN_TAB_KEY, viewPager.getCurrentItem());
861 }
862
863 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700864 protected void onRestoreInstanceState(Bundle savedInstanceState) {
865 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000866 resetButtonBar();
arangelov9d894142020-04-21 21:08:21 +0100867 ViewPager viewPager = findViewById(R.id.profile_pager);
868 viewPager.setCurrentItem(savedInstanceState.getInt(LAST_SHOWN_TAB_KEY));
869 mMultiProfilePagerAdapter.clearInactiveProfileCache();
Adam Powell9bee4662012-05-08 11:07:23 -0700870 }
871
shafik0c7c5d52019-02-27 12:13:25 +0000872 private boolean isHttpSchemeAndViewAction(Intent intent) {
873 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
874 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
875 && Intent.ACTION_VIEW.equals(intent.getAction());
876 }
877
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100878 private boolean hasManagedProfile() {
879 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
880 if (userManager == null) {
881 return false;
882 }
883
884 try {
885 List<UserInfo> profiles = userManager.getProfiles(getUserId());
886 for (UserInfo userInfo : profiles) {
887 if (userInfo != null && userInfo.isManagedProfile()) {
888 return true;
889 }
890 }
891 } catch (SecurityException e) {
892 return false;
893 }
894 return false;
895 }
896
897 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
898 try {
899 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
900 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700901 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100902 } catch (NameNotFoundException e) {
903 return false;
904 }
905 }
906
Adam Powell278902c2014-07-12 18:33:22 -0700907 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
908 boolean filtered) {
arangelov2c6115a2020-04-08 13:26:13 +0100909 if (!mMultiProfilePagerAdapter.getCurrentUserHandle().equals(getUser())) {
arangelov349af6a2020-03-05 11:53:40 +0000910 // Never allow the inactive profile to always open an app.
911 mAlwaysButton.setEnabled(false);
912 return;
913 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100914 boolean enabled = false;
Paul McLean3a30e552019-12-07 14:47:29 -0700915 ResolveInfo ri = null;
Nicolas Prevot50449882014-06-23 12:42:37 +0100916 if (hasValidSelection) {
arangelova3912cf2019-12-13 14:34:45 +0000917 ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +0000918 .resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800919 if (ri == null) {
920 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
921 return;
922 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
923 Log.e(TAG, "Attempted to set selection to resolve info for another user");
924 return;
925 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100926 enabled = true;
927 }
Matt Pietal0d6834a2019-06-27 13:27:52 -0400928 if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) {
929 mAlwaysButton.setText(getResources()
930 .getString(R.string.activity_resolver_set_always));
931 } else {
932 mAlwaysButton.setText(getResources()
933 .getString(R.string.activity_resolver_use_always));
934 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100935 }
Paul McLean3a30e552019-12-07 14:47:29 -0700936
arangelov349af6a2020-03-05 11:53:40 +0000937 if (ri != null) {
938 ActivityInfo activityInfo = ri.activityInfo;
Paul McLean3a30e552019-12-07 14:47:29 -0700939
arangelov349af6a2020-03-05 11:53:40 +0000940 boolean hasRecordPermission =
941 mPm.checkPermission(android.Manifest.permission.RECORD_AUDIO,
942 activityInfo.packageName)
943 == android.content.pm.PackageManager.PERMISSION_GRANTED;
Paul McLean3a30e552019-12-07 14:47:29 -0700944
arangelov349af6a2020-03-05 11:53:40 +0000945 if (!hasRecordPermission) {
946 // OK, we know the record permission, is this a capture device
947 boolean hasAudioCapture =
948 getIntent().getBooleanExtra(
949 ResolverActivity.EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
950 enabled = !hasAudioCapture;
951 }
Paul McLean3a30e552019-12-07 14:47:29 -0700952 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100953 mAlwaysButton.setEnabled(enabled);
954 }
955
Adam Powellc5878612012-05-04 18:42:38 -0700956 public void onButtonClick(View v) {
957 final int id = v.getId();
arangelovcf268642020-01-15 15:09:51 +0000958 ListView listView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelova3912cf2019-12-13 14:34:45 +0000959 ResolverListAdapter currentListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
arangelov38a6fce2019-12-02 18:21:22 +0000960 int which = currentListAdapter.hasFilteredItem()
961 ? currentListAdapter.getFilteredPosition()
962 : listView.getCheckedItemPosition();
963 boolean hasIndexBeenFiltered = !currentListAdapter.hasFilteredItem();
964 ResolveInfo ri = currentListAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Matt Pietala310c1f2019-07-03 09:12:56 -0400965 if (mUseLayoutForBrowsables
966 && !ri.handleAllWebDataURI && id == R.id.button_always) {
Matt Pietal0d6834a2019-06-27 13:27:52 -0400967 showSettingsForSelected(ri);
shafik69df96322018-12-18 15:41:19 +0000968 } else {
969 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
970 }
971 }
972
Matt Pietal0d6834a2019-06-27 13:27:52 -0400973 private void showSettingsForSelected(ResolveInfo ri) {
shafik34064c32019-03-06 15:42:52 +0000974 Intent intent = new Intent();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400975
976 final String packageName = ri.activityInfo.packageName;
977 Bundle showFragmentArgs = new Bundle();
978 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY);
979 showFragmentArgs.putString("package", packageName);
980
shafik34064c32019-03-06 15:42:52 +0000981 // For regular apps, we open the Open by Default page
Matt Pietal0d6834a2019-06-27 13:27:52 -0400982 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
983 .setData(Uri.fromParts("package", packageName, null))
984 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
985 .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY)
986 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
987
shafik34064c32019-03-06 15:42:52 +0000988 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700989 }
990
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800991 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700992 if (isFinishing()) {
993 return;
994 }
arangelova3912cf2019-12-13 14:34:45 +0000995 ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +0000996 .resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000997 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
998 Toast.makeText(this, String.format(getResources().getString(
999 com.android.internal.R.string.activity_resolver_work_profiles_support),
1000 ri.activityInfo.loadLabel(getPackageManager()).toString()),
1001 Toast.LENGTH_LONG).show();
1002 return;
1003 }
1004
arangelova3912cf2019-12-13 14:34:45 +00001005 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001006 .targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001007 if (target == null) {
1008 return;
1009 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001010 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001011 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +01001012 MetricsLogger.action(
1013 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001014 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +01001015 MetricsLogger.action(
1016 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
1017 } else {
1018 MetricsLogger.action(
1019 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
1020 }
arangelov38a6fce2019-12-02 18:21:22 +00001021 MetricsLogger.action(this,
arangelova3912cf2019-12-13 14:34:45 +00001022 mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem()
Clara Bayarrifa902aa2016-04-13 14:45:08 +01001023 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
1024 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -07001025 finish();
1026 }
Mike Lockwood02eb8742011-02-27 09:10:37 -08001027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028
Adam Powelle49d9392014-07-17 18:45:19 -07001029 /**
1030 * Replace me in subclasses!
1031 */
arangelovb0802dc2019-10-18 18:03:44 +01001032 @Override // ResolverListCommunicator
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +00001033 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -07001034 return defIntent;
1035 }
1036
arangelovb0802dc2019-10-18 18:03:44 +01001037 @Override // ResolverListCommunicator
arangelov1b11cc12020-04-27 17:23:04 +01001038 public final void onPostListReady(ResolverListAdapter listAdapter, boolean doPostProcessing,
1039 boolean rebuildCompleted) {
arangelov4d3be0b2020-04-23 18:54:15 +01001040 if (isAutolaunching()) {
arangelov7981b122020-01-16 10:58:27 +00001041 return;
arangelovcf268642020-01-15 15:09:51 +00001042 }
arangelov27b99742020-04-23 18:59:09 +01001043 if (isIntentPicker()) {
1044 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
1045 .setUseLayoutWithDefault(useLayoutWithDefault());
1046 }
arangelovce7d4a62020-04-01 13:26:51 +01001047 if (mMultiProfilePagerAdapter.shouldShowEmptyStateScreen(listAdapter)) {
arangelov27608552020-03-27 16:57:50 +00001048 mMultiProfilePagerAdapter.showEmptyResolverListEmptyState(listAdapter);
arangelov4c0513d2020-03-05 22:24:13 +00001049 } else {
1050 mMultiProfilePagerAdapter.showListView(listAdapter);
arangelov590fba32020-02-11 18:05:42 +00001051 }
arangelov4d3be0b2020-04-23 18:54:15 +01001052 // showEmptyResolverListEmptyState can mark the tab as loaded,
1053 // which is a precondition for auto launching
arangelov1b11cc12020-04-27 17:23:04 +01001054 if (rebuildCompleted && maybeAutolaunchActivity()) {
arangelov4d3be0b2020-04-23 18:54:15 +01001055 return;
1056 }
arangelov7981b122020-01-16 10:58:27 +00001057 if (doPostProcessing) {
arangelov2c6115a2020-04-08 13:26:13 +01001058 maybeCreateHeader(listAdapter);
arangelov7981b122020-01-16 10:58:27 +00001059 resetButtonBar();
1060 onListRebuilt(listAdapter);
1061 }
arangelovb0802dc2019-10-18 18:03:44 +01001062 }
1063
arangelova3912cf2019-12-13 14:34:45 +00001064 protected void onListRebuilt(ResolverListAdapter listAdapter) {
arangelovcf268642020-01-15 15:09:51 +00001065 final ItemClickListener listener = new ItemClickListener();
1066 setupAdapterListView((ListView) mMultiProfilePagerAdapter.getActiveAdapterView(), listener);
arangelovbf806752020-04-09 18:31:50 +01001067 if (shouldShowTabs() && isIntentPicker()) {
1068 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
1069 if (rdl != null) {
1070 rdl.setMaxCollapsedHeight(getResources()
1071 .getDimensionPixelSize(useLayoutWithDefault()
1072 ? R.dimen.resolver_max_collapsed_height_with_default_with_tabs
1073 : R.dimen.resolver_max_collapsed_height_with_tabs));
1074 }
1075 }
arangelovb0802dc2019-10-18 18:03:44 +01001076 }
1077
Adam Powell2ed547e2015-04-29 18:45:04 -07001078 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -07001079 final ResolveInfo ri = target.getResolveInfo();
1080 final Intent intent = target != null ? target.getResolvedIntent() : null;
1081
arangelov38a6fce2019-12-02 18:21:22 +00001082 if (intent != null && (mSupportsAlwaysUseOption
arangelova3912cf2019-12-13 14:34:45 +00001083 || mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem())
1084 && mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredResolveList() != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001085 // Build a reasonable intent filter, based on what matched.
1086 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +02001087 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088
Henrik Engström3277cf12014-07-17 12:18:29 +02001089 if (intent.getSelector() != null) {
1090 filterIntent = intent.getSelector();
1091 } else {
1092 filterIntent = intent;
1093 }
1094
1095 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001096 if (action != null) {
1097 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 }
Henrik Engström3277cf12014-07-17 12:18:29 +02001099 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001100 if (categories != null) {
1101 for (String cat : categories) {
1102 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 }
1104 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001105 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106
Adam Powell24428412015-04-01 17:19:56 -07001107 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +02001108 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001109 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +02001110 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001111 if (mimeType != null) {
1112 try {
1113 filter.addDataType(mimeType);
1114 } catch (IntentFilter.MalformedMimeTypeException e) {
1115 Log.w("ResolverActivity", e);
1116 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001119 }
1120 if (data != null && data.getScheme() != null) {
1121 // We need the data specification if there was no type,
1122 // OR if the scheme is not one of our magical "file:"
1123 // or "content:" schemes (see IntentFilter for the reason).
1124 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
1125 || (!"file".equals(data.getScheme())
1126 && !"content".equals(data.getScheme()))) {
1127 filter.addDataScheme(data.getScheme());
1128
1129 // Look through the resolved filter to determine which part
1130 // of it matched the original Intent.
1131 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
1132 if (pIt != null) {
1133 String ssp = data.getSchemeSpecificPart();
1134 while (ssp != null && pIt.hasNext()) {
1135 PatternMatcher p = pIt.next();
1136 if (p.match(ssp)) {
1137 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
1138 break;
1139 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001140 }
1141 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001142 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
1143 if (aIt != null) {
1144 while (aIt.hasNext()) {
1145 IntentFilter.AuthorityEntry a = aIt.next();
1146 if (a.match(data) >= 0) {
1147 int port = a.getPort();
1148 filter.addDataAuthority(a.getHost(),
1149 port >= 0 ? Integer.toString(port) : null);
1150 break;
1151 }
1152 }
1153 }
1154 pIt = ri.filter.pathsIterator();
1155 if (pIt != null) {
1156 String path = data.getPath();
1157 while (path != null && pIt.hasNext()) {
1158 PatternMatcher p = pIt.next();
1159 if (p.match(path)) {
1160 filter.addDataPath(p.getPath(), p.getType());
1161 break;
1162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164 }
1165 }
1166 }
1167
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001168 if (filter != null) {
arangelova3912cf2019-12-13 14:34:45 +00001169 final int N = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001170 .getUnfilteredResolveList().size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001171 ComponentName[] set;
1172 // If we don't add back in the component for forwarding the intent to a managed
1173 // profile, the preferred activity may not be updated correctly (as the set of
1174 // components we tell it we knew about will have changed).
arangelovb0802dc2019-10-18 18:03:44 +01001175 final boolean needToAddBackProfileForwardingComponent =
arangelova3912cf2019-12-13 14:34:45 +00001176 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001177 if (!needToAddBackProfileForwardingComponent) {
1178 set = new ComponentName[N];
1179 } else {
1180 set = new ComponentName[N + 1];
1181 }
1182
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001183 int bestMatch = 0;
1184 for (int i=0; i<N; i++) {
arangelova3912cf2019-12-13 14:34:45 +00001185 ResolveInfo r = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001186 .getUnfilteredResolveList().get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001187 set[i] = new ComponentName(r.activityInfo.packageName,
1188 r.activityInfo.name);
1189 if (r.match > bestMatch) bestMatch = r.match;
1190 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001191
1192 if (needToAddBackProfileForwardingComponent) {
arangelova3912cf2019-12-13 14:34:45 +00001193 set[N] = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001194 .getOtherProfile().getResolvedComponentName();
arangelova3912cf2019-12-13 14:34:45 +00001195 final int otherProfileMatch = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001196 .getOtherProfile().getResolveInfo().match;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001197 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
1198 }
1199
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001200 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001201 final int userId = getUserId();
1202 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001203
1204 // Set the preferred Activity
1205 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
1206
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001207 if (ri.handleAllWebDataURI) {
1208 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001209 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001210 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001211 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001212 }
1213 } else {
1214 // Update Domain Verification status
1215 ComponentName cn = intent.getComponent();
1216 String packageName = cn.getPackageName();
1217 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001218
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001219 boolean isHttpOrHttps = (dataScheme != null) &&
1220 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
1221 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001222
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001223 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
1224 boolean hasCategoryBrowsable = (categories != null) &&
1225 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001226
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001227 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001228 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001229 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
1230 userId);
1231 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001232 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001233 } else {
1234 try {
arangelova3912cf2019-12-13 14:34:45 +00001235 mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001236 .mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001237 } catch (RemoteException re) {
1238 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1239 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 }
1242 }
1243
Adam Powell24428412015-04-01 17:19:56 -07001244 if (target != null) {
arangelov9e148522020-01-15 21:11:51 +00001245 if (intent != null) {
1246 intent.fixUris(UserHandle.myUserId());
1247 }
Adam Powell24428412015-04-01 17:19:56 -07001248 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -04001249
1250 // Rely on the ActivityManager to pop up a dialog regarding app suspension
1251 // and return false
1252 if (target.isSuspended()) {
1253 return false;
1254 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001255 }
Matt Pietala4b30072019-04-04 13:44:36 -04001256
Adam Powell2ed547e2015-04-29 18:45:04 -07001257 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001258 }
1259
arangelovcf268642020-01-15 15:09:51 +00001260 @VisibleForTesting
Adam Powell23882512016-01-29 10:21:00 -08001261 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -07001262 // We're dispatching intents that might be coming from legacy apps, so
1263 // don't kill ourselves.
1264 StrictMode.disableDeathOnFileUriExposure();
1265 try {
1266 safelyStartActivityInternal(cti);
1267 } finally {
1268 StrictMode.enableDeathOnFileUriExposure();
1269 }
1270 }
1271
1272 private void safelyStartActivityInternal(TargetInfo cti) {
arangelov96dec882020-01-31 20:23:40 +00001273 if (mPersonalPackageMonitor != null) {
1274 mPersonalPackageMonitor.unregister();
1275 }
arangelov7981b122020-01-16 10:58:27 +00001276 if (mWorkPackageMonitor != null) {
1277 mWorkPackageMonitor.unregister();
1278 }
1279 mRegistered = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +00001280 // If needed, show that intent is forwarded
1281 // from managed profile to owner or other way around.
1282 if (mProfileSwitchMessageId != -1) {
1283 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
1284 }
arangelov38a6fce2019-12-02 18:21:22 +00001285 UserHandle currentUserHandle = mMultiProfilePagerAdapter.getCurrentUserHandle();
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001286 if (!mSafeForwardingMode) {
arangelov38a6fce2019-12-02 18:21:22 +00001287 if (cti.startAsUser(this, null, currentUserHandle)) {
Adam Powell24428412015-04-01 17:19:56 -07001288 onActivityStarted(cti);
arangelov4bf17472020-02-17 20:21:46 +00001289 maybeLogCrossProfileTargetLaunch(cti, currentUserHandle);
Adam Powell24428412015-04-01 17:19:56 -07001290 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001291 return;
1292 }
1293 try {
arangelov38a6fce2019-12-02 18:21:22 +00001294 if (cti.startAsCaller(this, null, currentUserHandle.getIdentifier())) {
Adam Powell24428412015-04-01 17:19:56 -07001295 onActivityStarted(cti);
arangelov4bf17472020-02-17 20:21:46 +00001296 maybeLogCrossProfileTargetLaunch(cti, currentUserHandle);
Adam Powell24428412015-04-01 17:19:56 -07001297 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001298 } catch (RuntimeException e) {
1299 String launchedFromPackage;
1300 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001301 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001302 getActivityToken());
1303 } catch (RemoteException e2) {
1304 launchedFromPackage = "??";
1305 }
1306 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
1307 + " package " + launchedFromPackage + ", while running in "
1308 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 }
1311
arangelov4bf17472020-02-17 20:21:46 +00001312 private void maybeLogCrossProfileTargetLaunch(TargetInfo cti, UserHandle currentUserHandle) {
arangelove5b369c2020-03-12 17:36:05 +00001313 if (!hasWorkProfile() || currentUserHandle.equals(getUser())) {
arangelov4bf17472020-02-17 20:21:46 +00001314 return;
1315 }
1316 DevicePolicyEventLogger
1317 .createEvent(DevicePolicyEnums.RESOLVER_CROSS_PROFILE_TARGET_OPENED)
arangelove5b369c2020-03-12 17:36:05 +00001318 .setBoolean(currentUserHandle.equals(getPersonalProfileUserHandle()))
arangelov4bf17472020-02-17 20:21:46 +00001319 .setStrings(getMetricsCategory(),
1320 cti instanceof ChooserTargetInfo ? "direct_share" : "other_target")
1321 .write();
1322 }
1323
Alison Cichowlas3e340502018-08-07 17:15:01 -04001324
arangelovb0802dc2019-10-18 18:03:44 +01001325 public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
Alison Cichowlas3e340502018-08-07 17:15:01 -04001326 int userId) {
1327 // Pass intent to delegate chooser activity with permission token.
1328 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
1329 // moves into systemui
1330 try {
1331 // TODO: Once this is a small springboard activity, it can move off the UI process
1332 // and we can move the request method to ActivityManagerInternal.
1333 IBinder permissionToken = ActivityTaskManager.getService()
1334 .requestStartActivityPermissionToken(getActivityToken());
1335 final Intent chooserIntent = new Intent();
1336 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1337 Resources.getSystem().getString(R.string.config_chooserActivity));
1338 chooserIntent.setClassName(delegateActivity.getPackageName(),
1339 delegateActivity.getClassName());
1340 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1341
1342 // TODO: These extras will change as chooser activity moves into systemui
1343 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1344 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1345 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1346 ignoreTargetSecurity);
1347 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001348 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1349 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001350 startActivity(chooserIntent);
1351 } catch (RemoteException e) {
1352 Log.e(TAG, e.toString());
1353 }
1354 return true;
1355 }
1356
Adam Powell23882512016-01-29 10:21:00 -08001357 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001358 // Do nothing
1359 }
1360
arangelovb0802dc2019-10-18 18:03:44 +01001361 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -08001362 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001363 return false;
1364 }
1365
Adam Powell23882512016-01-29 10:21:00 -08001366 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001367 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001368 }
1369
Alison Cichowlas19ee2922019-12-16 19:43:12 -05001370 void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001371 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001372 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001373 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
arangelovebf3c052020-03-17 13:11:37 +00001374 startActivityAsUser(in, mMultiProfilePagerAdapter.getCurrentUserHandle());
Adam Powellc5878612012-05-04 18:42:38 -07001375 }
1376
arangelov38a6fce2019-12-02 18:21:22 +00001377 @VisibleForTesting
1378 protected ResolverListAdapter createResolverListAdapter(Context context,
1379 List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList,
1380 boolean filterLastUsed, boolean useLayoutForBrowsables, UserHandle userHandle) {
Paul McLean07425c82019-10-18 12:00:11 -06001381 Intent startIntent = getIntent();
1382 boolean isAudioCaptureDevice =
1383 startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
arangelovb0802dc2019-10-18 18:03:44 +01001384 return new ResolverListAdapter(context, payloadIntents, initialIntents, rList,
arangelov38a6fce2019-12-02 18:21:22 +00001385 filterLastUsed, createListController(userHandle), useLayoutForBrowsables, this,
Paul McLean07425c82019-10-18 12:00:11 -06001386 isAudioCaptureDevice);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001387 }
1388
1389 @VisibleForTesting
arangelov38a6fce2019-12-02 18:21:22 +00001390 protected ResolverListController createListController(UserHandle userHandle) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001391 return new ResolverListController(
1392 this,
1393 mPm,
1394 getTargetIntent(),
1395 getReferrerPackageName(),
arangelov38a6fce2019-12-02 18:21:22 +00001396 mLaunchedFromUid,
1397 userHandle);
Adam Powell88831a22014-11-20 18:17:00 -08001398 }
1399
Adam Powell39e94eb2015-09-08 17:01:49 -07001400 /**
arangelovb0802dc2019-10-18 18:03:44 +01001401 * Sets up the content view.
arangelovbb572332019-11-13 12:50:08 +00001402 * @return <code>true</code> if the activity is finishing and creation should halt.
Adam Powell39e94eb2015-09-08 17:01:49 -07001403 */
arangelovbb572332019-11-13 12:50:08 +00001404 private boolean configureContentView() {
arangelova3912cf2019-12-13 14:34:45 +00001405 if (mMultiProfilePagerAdapter.getActiveListAdapter() == null) {
1406 throw new IllegalStateException("mMultiProfilePagerAdapter.getCurrentListAdapter() "
1407 + "cannot be null.");
arangelovb0802dc2019-10-18 18:03:44 +01001408 }
arangelov7981b122020-01-16 10:58:27 +00001409 // We partially rebuild the inactive adapter to determine if we should auto launch
arangelovb91d08f2020-03-05 21:50:14 +00001410 // isTabLoaded will be true here if the empty state screen is shown instead of the list.
1411 boolean rebuildCompleted = mMultiProfilePagerAdapter.rebuildActiveTab(true)
1412 || mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded();
arangelov2a4d0b12020-03-04 18:15:04 +00001413 if (shouldShowTabs()) {
arangelovb91d08f2020-03-05 21:50:14 +00001414 boolean rebuildInactiveCompleted = mMultiProfilePagerAdapter.rebuildInactiveTab(false)
1415 || mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded();
arangelovdb0c1f12020-02-14 17:35:12 +00001416 rebuildCompleted = rebuildCompleted && rebuildInactiveCompleted;
1417 }
arangelov7981b122020-01-16 10:58:27 +00001418
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001419 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001420 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001421 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001422 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001423 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001424 setContentView(mLayoutId);
arangelov38a6fce2019-12-02 18:21:22 +00001425 mMultiProfilePagerAdapter.setupViewPager(findViewById(R.id.profile_pager));
arangelovdb0c1f12020-02-14 17:35:12 +00001426 return postRebuildList(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001427 }
Adam Powell7d758002015-05-06 17:49:36 -07001428
arangelovb0802dc2019-10-18 18:03:44 +01001429 /**
arangelovbb572332019-11-13 12:50:08 +00001430 * Finishing procedures to be performed after the list has been rebuilt.
1431 * </p>Subclasses must call postRebuildListInternal at the end of postRebuildList.
1432 * @param rebuildCompleted
1433 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001434 */
arangelovbb572332019-11-13 12:50:08 +00001435 protected boolean postRebuildList(boolean rebuildCompleted) {
1436 return postRebuildListInternal(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001437 }
1438
1439 /**
arangelovbb572332019-11-13 12:50:08 +00001440 * Finishing procedures to be performed after the list has been rebuilt.
1441 * @param rebuildCompleted
1442 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001443 */
arangelovbb572332019-11-13 12:50:08 +00001444 final boolean postRebuildListInternal(boolean rebuildCompleted) {
arangelova3912cf2019-12-13 14:34:45 +00001445 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001446
1447 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1448 // we're already done, we can check if we should auto-launch immediately.
arangelov7981b122020-01-16 10:58:27 +00001449 if (rebuildCompleted && maybeAutolaunchActivity()) {
1450 return true;
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001451 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001452
arangelovcf268642020-01-15 15:09:51 +00001453 setupViewVisibilities();
1454
arangelovf163a882020-02-18 17:18:47 +00001455 if (shouldShowTabs()) {
arangelovcf268642020-01-15 15:09:51 +00001456 setupProfileTabs();
1457 }
1458
arangelov38a6fce2019-12-02 18:21:22 +00001459 return false;
1460 }
1461
arangelov7981b122020-01-16 10:58:27 +00001462 private int isPermissionGranted(String permission, int uid) {
1463 return ActivityManager.checkComponentPermission(permission, uid,
1464 /* owningUid= */-1, /* exported= */ true);
1465 }
1466
1467 /**
1468 * @return {@code true} if a resolved target is autolaunched, otherwise {@code false}
1469 */
1470 private boolean maybeAutolaunchActivity() {
1471 int numberOfProfiles = mMultiProfilePagerAdapter.getItemCount();
1472 if (numberOfProfiles == 1 && maybeAutolaunchIfSingleTarget()) {
1473 return true;
arangelovf6986d42020-02-12 15:03:22 +00001474 } else if (numberOfProfiles == 2
arangelovb91d08f2020-03-05 21:50:14 +00001475 && mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded()
1476 && mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded()
arangelovf6986d42020-02-12 15:03:22 +00001477 && (maybeAutolaunchIfNoAppsOnInactiveTab()
1478 || maybeAutolaunchIfCrossProfileSupported())) {
arangelov7981b122020-01-16 10:58:27 +00001479 return true;
1480 }
1481 return false;
1482 }
1483
1484 private boolean maybeAutolaunchIfSingleTarget() {
1485 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1486 if (count != 1) {
1487 return false;
1488 }
1489
arangelovdb0c1f12020-02-14 17:35:12 +00001490 if (mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null) {
1491 return false;
1492 }
1493
arangelov7981b122020-01-16 10:58:27 +00001494 // Only one target, so we're a candidate to auto-launch!
1495 final TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
1496 .targetInfoForPosition(0, false);
1497 if (shouldAutoLaunchSingleChoice(target)) {
1498 safelyStartActivity(target);
1499 finish();
1500 return true;
1501 }
1502 return false;
1503 }
1504
arangelovf6986d42020-02-12 15:03:22 +00001505 private boolean maybeAutolaunchIfNoAppsOnInactiveTab() {
1506 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1507 if (count != 1) {
1508 return false;
1509 }
1510 ResolverListAdapter inactiveListAdapter =
1511 mMultiProfilePagerAdapter.getInactiveListAdapter();
1512 if (inactiveListAdapter.getUnfilteredCount() != 0) {
1513 return false;
1514 }
1515 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
1516 .targetInfoForPosition(0, false);
1517 safelyStartActivity(target);
1518 finish();
1519 return true;
1520 }
1521
arangelov7981b122020-01-16 10:58:27 +00001522 /**
1523 * When we have a personal and a work profile, we auto launch in the following scenario:
1524 * - There is 1 resolved target on each profile
1525 * - That target is the same app on both profiles
1526 * - The target app has permission to communicate cross profiles
1527 * - The target app has declared it supports cross-profile communication via manifest metadata
1528 */
1529 private boolean maybeAutolaunchIfCrossProfileSupported() {
arangelov4bf17472020-02-17 20:21:46 +00001530 ResolverListAdapter activeListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
1531 int count = activeListAdapter.getUnfilteredCount();
arangelov7981b122020-01-16 10:58:27 +00001532 if (count != 1) {
1533 return false;
1534 }
1535 ResolverListAdapter inactiveListAdapter =
1536 mMultiProfilePagerAdapter.getInactiveListAdapter();
1537 if (inactiveListAdapter.getUnfilteredCount() != 1) {
1538 return false;
1539 }
arangelov4bf17472020-02-17 20:21:46 +00001540 TargetInfo activeProfileTarget = activeListAdapter
arangelov7981b122020-01-16 10:58:27 +00001541 .targetInfoForPosition(0, false);
1542 TargetInfo inactiveProfileTarget = inactiveListAdapter.targetInfoForPosition(0, false);
1543 if (!Objects.equals(activeProfileTarget.getResolvedComponentName(),
1544 inactiveProfileTarget.getResolvedComponentName())) {
1545 return false;
1546 }
1547 if (!shouldAutoLaunchSingleChoice(activeProfileTarget)) {
1548 return false;
1549 }
1550 String packageName = activeProfileTarget.getResolvedComponentName().getPackageName();
1551 if (!canAppInteractCrossProfiles(packageName)) {
1552 return false;
1553 }
1554
arangelov4bf17472020-02-17 20:21:46 +00001555 DevicePolicyEventLogger
1556 .createEvent(DevicePolicyEnums.RESOLVER_AUTOLAUNCH_CROSS_PROFILE_TARGET)
arangelove5b369c2020-03-12 17:36:05 +00001557 .setBoolean(activeListAdapter.getUserHandle()
1558 .equals(getPersonalProfileUserHandle()))
arangelov4bf17472020-02-17 20:21:46 +00001559 .setStrings(getMetricsCategory())
1560 .write();
arangelov7981b122020-01-16 10:58:27 +00001561 safelyStartActivity(activeProfileTarget);
1562 finish();
1563 return true;
1564 }
1565
1566 /**
1567 * Returns whether the package has the necessary permissions to interact across profiles on
1568 * behalf of a given user.
1569 *
1570 * <p>This means meeting the following condition:
1571 * <ul>
1572 * <li>The app's {@link ApplicationInfo#crossProfile} flag must be true, and at least
1573 * one of the following conditions must be fulfilled</li>
1574 * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS_FULL} granted.</li>
1575 * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS} granted.</li>
1576 * <li>{@code Manifest.permission.INTERACT_ACROSS_PROFILES} granted, or the corresponding
1577 * AppOps {@code android:interact_across_profiles} is set to "allow".</li>
1578 * </ul>
1579 *
1580 */
1581 private boolean canAppInteractCrossProfiles(String packageName) {
1582 ApplicationInfo applicationInfo;
1583 try {
1584 applicationInfo = getPackageManager().getApplicationInfo(packageName, 0);
1585 } catch (NameNotFoundException e) {
1586 Log.e(TAG, "Package " + packageName + " does not exist on current user.");
1587 return false;
1588 }
1589 if (!applicationInfo.crossProfile) {
1590 return false;
1591 }
1592
1593 int packageUid = applicationInfo.uid;
1594
1595 if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1596 packageUid) == PackageManager.PERMISSION_GRANTED) {
1597 return true;
1598 }
1599 if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS, packageUid)
1600 == PackageManager.PERMISSION_GRANTED) {
1601 return true;
1602 }
1603 if (PermissionChecker.checkPermissionForPreflight(this, INTERACT_ACROSS_PROFILES,
1604 PID_UNKNOWN, packageUid, packageName) == PackageManager.PERMISSION_GRANTED) {
1605 return true;
1606 }
1607 return false;
1608 }
1609
1610 private boolean isAutolaunching() {
1611 return !mRegistered && isFinishing();
1612 }
1613
arangelovcf268642020-01-15 15:09:51 +00001614 private void setupProfileTabs() {
Alex Kershaw760754e2020-05-12 11:32:23 +01001615 maybeHideDivider();
arangelovcf268642020-01-15 15:09:51 +00001616 TabHost tabHost = findViewById(R.id.profile_tabhost);
1617 tabHost.setup();
1618 ViewPager viewPager = findViewById(R.id.profile_pager);
arangelov9d894142020-04-21 21:08:21 +01001619 viewPager.setSaveEnabled(false);
arangelovcf268642020-01-15 15:09:51 +00001620 TabHost.TabSpec tabSpec = tabHost.newTabSpec(TAB_TAG_PERSONAL)
1621 .setContent(R.id.profile_pager)
1622 .setIndicator(getString(R.string.resolver_personal_tab));
1623 tabHost.addTab(tabSpec);
1624
1625 tabSpec = tabHost.newTabSpec(TAB_TAG_WORK)
1626 .setContent(R.id.profile_pager)
1627 .setIndicator(getString(R.string.resolver_work_tab));
1628 tabHost.addTab(tabSpec);
1629
1630 TabWidget tabWidget = tabHost.getTabWidget();
1631 tabWidget.setVisibility(View.VISIBLE);
1632 resetTabsHeaderStyle(tabWidget);
1633 updateActiveTabStyle(tabHost);
1634
1635 tabHost.setOnTabChangedListener(tabId -> {
1636 resetTabsHeaderStyle(tabWidget);
1637 updateActiveTabStyle(tabHost);
1638 if (TAB_TAG_PERSONAL.equals(tabId)) {
1639 viewPager.setCurrentItem(0);
1640 } else {
1641 viewPager.setCurrentItem(1);
1642 }
1643 setupViewVisibilities();
Susi Kharraz-Postdde9c3a2020-03-03 07:55:40 -05001644 maybeLogProfileChange();
arangelov2b15d712020-04-22 14:54:31 +01001645 onProfileTabSelected();
arangelov4bf17472020-02-17 20:21:46 +00001646 DevicePolicyEventLogger
1647 .createEvent(DevicePolicyEnums.RESOLVER_SWITCH_TABS)
1648 .setInt(viewPager.getCurrentItem())
1649 .setStrings(getMetricsCategory())
1650 .write();
arangelovcf268642020-01-15 15:09:51 +00001651 });
1652
1653 viewPager.setVisibility(View.VISIBLE);
1654 tabHost.setCurrentTab(mMultiProfilePagerAdapter.getCurrentPage());
arangelov349af6a2020-03-05 11:53:40 +00001655 mMultiProfilePagerAdapter.setOnProfileSelectedListener(
1656 index -> {
1657 tabHost.setCurrentTab(index);
1658 resetButtonBar();
1659 resetCheckedItem();
1660 });
arangelov934c64c2020-02-14 13:48:23 +00001661 findViewById(R.id.resolver_tab_divider).setVisibility(View.VISIBLE);
arangelovcf268642020-01-15 15:09:51 +00001662 }
1663
Alex Kershaw760754e2020-05-12 11:32:23 +01001664 private void maybeHideDivider() {
1665 if (!isIntentPicker()) {
1666 return;
1667 }
1668 final View divider = findViewById(R.id.divider);
1669 if (divider == null) {
1670 return;
1671 }
1672 divider.setVisibility(View.GONE);
1673 }
1674
arangelov2b15d712020-04-22 14:54:31 +01001675 /**
1676 * Callback called when user changes the profile tab.
1677 * <p>This method is intended to be overridden by subclasses.
1678 */
1679 protected void onProfileTabSelected() { }
1680
arangelov349af6a2020-03-05 11:53:40 +00001681 private void resetCheckedItem() {
1682 if (!isIntentPicker()) {
1683 return;
1684 }
1685 mLastSelected = ListView.INVALID_POSITION;
1686 ListView inactiveListView = (ListView) mMultiProfilePagerAdapter.getInactiveAdapterView();
1687 if (inactiveListView.getCheckedItemCount() > 0) {
1688 inactiveListView.setItemChecked(inactiveListView.getCheckedItemPosition(), false);
1689 }
1690 }
1691
arangelovcf268642020-01-15 15:09:51 +00001692 private void resetTabsHeaderStyle(TabWidget tabWidget) {
arangelov791509d2020-03-20 13:01:53 +00001693 String workContentDescription = getString(R.string.resolver_work_tab_accessibility);
1694 String personalContentDescription = getString(R.string.resolver_personal_tab_accessibility);
arangelovcf268642020-01-15 15:09:51 +00001695 for (int i = 0; i < tabWidget.getChildCount(); i++) {
arangelov78fd3602020-02-28 15:21:20 +00001696 View tabView = tabWidget.getChildAt(i);
1697 TextView title = tabView.findViewById(android.R.id.title);
arangelov4b8ad342020-03-17 13:56:00 +00001698 title.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_DialogWindowTitle);
1699 title.setTextColor(getAttrColor(this, android.R.attr.textColorTertiary));
1700 title.setTextSize(TypedValue.COMPLEX_UNIT_PX,
1701 getResources().getDimension(R.dimen.resolver_tab_text_size));
arangelov791509d2020-03-20 13:01:53 +00001702 if (title.getText().equals(getString(R.string.resolver_personal_tab))) {
1703 tabView.setContentDescription(personalContentDescription);
1704 } else if (title.getText().equals(getString(R.string.resolver_work_tab))) {
1705 tabView.setContentDescription(workContentDescription);
1706 }
arangelovcf268642020-01-15 15:09:51 +00001707 }
1708 }
1709
arangelov4b8ad342020-03-17 13:56:00 +00001710 private static int getAttrColor(Context context, int attr) {
1711 TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
1712 int colorAccent = ta.getColor(0, 0);
1713 ta.recycle();
1714 return colorAccent;
1715 }
1716
arangelovcf268642020-01-15 15:09:51 +00001717 private void updateActiveTabStyle(TabHost tabHost) {
1718 TextView title = tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
1719 .findViewById(android.R.id.title);
arangelov4b8ad342020-03-17 13:56:00 +00001720 title.setTextColor(getAttrColor(this, android.R.attr.colorAccent));
arangelovcf268642020-01-15 15:09:51 +00001721 }
1722
1723 private void setupViewVisibilities() {
arangelov590fba32020-02-11 18:05:42 +00001724 ResolverListAdapter activeListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
arangelovce7d4a62020-04-01 13:26:51 +01001725 if (!mMultiProfilePagerAdapter.shouldShowEmptyStateScreen(activeListAdapter)) {
arangelov590fba32020-02-11 18:05:42 +00001726 addUseDifferentAppLabelIfNecessary(activeListAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001727 }
1728 }
1729
Zhen Zhangbde7b462019-11-11 11:49:33 -08001730 /**
arangelovcf268642020-01-15 15:09:51 +00001731 * Add a label to signify that the user can pick a different app.
Zhen Zhangbde7b462019-11-11 11:49:33 -08001732 * @param adapter The adapter used to provide data to item views.
Zhen Zhangbde7b462019-11-11 11:49:33 -08001733 */
arangelovcf268642020-01-15 15:09:51 +00001734 public void addUseDifferentAppLabelIfNecessary(ResolverListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001735 final boolean useHeader = adapter.hasFilteredItem();
arangelovcf268642020-01-15 15:09:51 +00001736 if (useHeader) {
1737 FrameLayout stub = findViewById(R.id.stub);
1738 stub.setVisibility(View.VISIBLE);
1739 TextView textView = (TextView) LayoutInflater.from(this).inflate(
1740 R.layout.resolver_different_item_header, null, false);
arangelovf163a882020-02-18 17:18:47 +00001741 if (shouldShowTabs()) {
arangelovcf268642020-01-15 15:09:51 +00001742 textView.setGravity(Gravity.CENTER);
1743 }
1744 stub.addView(textView);
1745 }
1746 }
1747
1748 private void setupAdapterListView(ListView listView, ItemClickListener listener) {
arangelov38a6fce2019-12-02 18:21:22 +00001749 listView.setOnItemClickListener(listener);
1750 listView.setOnItemLongClickListener(listener);
Adam Powell7d758002015-05-06 17:49:36 -07001751
shafik69df96322018-12-18 15:41:19 +00001752 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001753 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1754 }
Adam Powell24428412015-04-01 17:19:56 -07001755 }
1756
Matt Pietal26038402019-01-08 07:29:34 -05001757 /**
1758 * Configure the area above the app selection list (title, content preview, etc).
1759 */
arangelov2c6115a2020-04-08 13:26:13 +01001760 private void maybeCreateHeader(ResolverListAdapter listAdapter) {
arangelov4e2951b2020-04-23 18:59:09 +01001761 if (mHeaderCreatorUser != null
1762 && !listAdapter.getUserHandle().equals(mHeaderCreatorUser)) {
arangelov2c6115a2020-04-08 13:26:13 +01001763 return;
1764 }
1765 if (!shouldShowTabs()
1766 && listAdapter.getCount() == 0 && listAdapter.getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001767 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001768 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001769 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001770 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001771 }
1772
1773 CharSequence title = mTitle != null
1774 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001775 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001776
1777 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001778 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001779 if (titleView != null) {
1780 titleView.setText(title);
1781 }
1782 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001783 }
1784
Alan Viverette51efddb2017-04-05 10:00:01 -04001785 final ImageView iconView = findViewById(R.id.icon);
arangelovb0802dc2019-10-18 18:03:44 +01001786 if (iconView != null) {
arangelov2c6115a2020-04-08 13:26:13 +01001787 listAdapter.loadFilteredItemIconTaskAsync(iconView);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001788 }
arangelov4e2951b2020-04-23 18:59:09 +01001789 mHeaderCreatorUser = listAdapter.getUserHandle();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001790 }
1791
arangelov38a6fce2019-12-02 18:21:22 +00001792 protected void resetButtonBar() {
shafik69df96322018-12-18 15:41:19 +00001793 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1794 return;
1795 }
1796 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
arangelov5ecac7a92020-04-20 21:22:25 +01001797 if (buttonLayout == null) {
shafik69df96322018-12-18 15:41:19 +00001798 Log.e(TAG, "Layout unexpectedly does not have a button bar");
arangelov5ecac7a92020-04-20 21:22:25 +01001799 return;
shafik69df96322018-12-18 15:41:19 +00001800 }
arangelov5ecac7a92020-04-20 21:22:25 +01001801 ResolverListAdapter activeListAdapter =
1802 mMultiProfilePagerAdapter.getActiveListAdapter();
1803 View buttonBarDivider = findViewById(R.id.resolver_button_bar_divider);
1804 if (activeListAdapter.isTabLoaded()
arangelov4e2951b2020-04-23 18:59:09 +01001805 && mMultiProfilePagerAdapter.shouldShowEmptyStateScreen(activeListAdapter)
1806 && !useLayoutWithDefault()) {
arangelov5ecac7a92020-04-20 21:22:25 +01001807 buttonLayout.setVisibility(View.INVISIBLE);
arangelov4e2951b2020-04-23 18:59:09 +01001808 if (buttonBarDivider != null) {
1809 buttonBarDivider.setVisibility(View.INVISIBLE);
1810 }
arangelov27b99742020-04-23 18:59:09 +01001811 setButtonBarIgnoreOffset(/* ignoreOffset */ false);
arangelov5ecac7a92020-04-20 21:22:25 +01001812 return;
1813 }
arangelov4e2951b2020-04-23 18:59:09 +01001814 if (buttonBarDivider != null) {
1815 buttonBarDivider.setVisibility(View.VISIBLE);
1816 }
arangelov5ecac7a92020-04-20 21:22:25 +01001817 buttonLayout.setVisibility(View.VISIBLE);
arangelov27b99742020-04-23 18:59:09 +01001818 setButtonBarIgnoreOffset(/* ignoreOffset */ true);
arangelov5ecac7a92020-04-20 21:22:25 +01001819
1820 if (!useLayoutWithDefault()) {
1821 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1822 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
1823 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1824 R.dimen.resolver_button_bar_spacing) + inset);
1825 }
1826 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
1827 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1828
1829 resetAlwaysOrOnceButtonBar();
shafik69df96322018-12-18 15:41:19 +00001830 }
1831
arangelov27b99742020-04-23 18:59:09 +01001832 /**
1833 * Updates the button bar container {@code ignoreOffset} layout param.
1834 * <p>Setting this to {@code true} means that the button bar will be glued to the bottom of
1835 * the screen.
1836 */
1837 private void setButtonBarIgnoreOffset(boolean ignoreOffset) {
1838 View buttonBarContainer = findViewById(R.id.button_bar_container);
1839 if (buttonBarContainer != null) {
1840 ResolverDrawerLayout.LayoutParams layoutParams =
1841 (ResolverDrawerLayout.LayoutParams) buttonBarContainer.getLayoutParams();
1842 layoutParams.ignoreOffset = ignoreOffset;
1843 buttonBarContainer.setLayoutParams(layoutParams);
1844 }
1845 }
1846
shafik69df96322018-12-18 15:41:19 +00001847 private void resetAlwaysOrOnceButtonBar() {
arangelov349af6a2020-03-05 11:53:40 +00001848 // Disable both buttons initially
1849 setAlwaysButtonEnabled(false, ListView.INVALID_POSITION, false);
1850 mOnceButton.setEnabled(false);
1851
arangelova3912cf2019-12-13 14:34:45 +00001852 int filteredPosition = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001853 .getFilteredPosition();
1854 if (useLayoutWithDefault() && filteredPosition != ListView.INVALID_POSITION) {
1855 setAlwaysButtonEnabled(true, filteredPosition, false);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001856 mOnceButton.setEnabled(true);
Sergey Troshin40e979e2019-12-30 17:29:50 +01001857 // Focus the button if we already have the default option
1858 mOnceButton.requestFocus();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001859 return;
1860 }
1861
1862 // When the items load in, if an item was already selected, enable the buttons
arangelovcf268642020-01-15 15:09:51 +00001863 ListView currentAdapterView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelov38a6fce2019-12-02 18:21:22 +00001864 if (currentAdapterView != null
1865 && currentAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1866 setAlwaysButtonEnabled(true, currentAdapterView.getCheckedItemPosition(), true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001867 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001868 }
1869 }
1870
arangelovb0802dc2019-10-18 18:03:44 +01001871 @Override // ResolverListCommunicator
1872 public boolean useLayoutWithDefault() {
arangelovcf268642020-01-15 15:09:51 +00001873 // We only use the default app layout when the profile of the active user has a
1874 // filtered item. We always show the same default app even in the inactive user profile.
1875 boolean currentUserAdapterHasFilteredItem;
1876 if (mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier()
1877 == UserHandle.myUserId()) {
1878 currentUserAdapterHasFilteredItem =
1879 mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem();
1880 } else {
1881 currentUserAdapterHasFilteredItem =
1882 mMultiProfilePagerAdapter.getInactiveListAdapter().hasFilteredItem();
1883 }
1884 return mSupportsAlwaysUseOption && currentUserAdapterHasFilteredItem;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001885 }
1886
Adam Powellc412be62015-06-24 13:54:10 -07001887 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001888 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1889 * called and we are launched in a new task.
1890 */
1891 protected void setRetainInOnStop(boolean retainInOnStop) {
1892 mRetainInOnStop = retainInOnStop;
1893 }
1894
1895 /**
Adam Powellc412be62015-06-24 13:54:10 -07001896 * Check a simple match for the component of two ResolveInfos.
1897 */
arangelovb0802dc2019-10-18 18:03:44 +01001898 @Override // ResolverListCommunicator
1899 public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
Adam Powellc412be62015-06-24 13:54:10 -07001900 return lhs == null ? rhs == null
1901 : lhs.activityInfo == null ? rhs.activityInfo == null
1902 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1903 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1904 }
1905
arangelov4bf17472020-02-17 20:21:46 +00001906 protected String getMetricsCategory() {
1907 return METRICS_CATEGORY_RESOLVER;
1908 }
1909
arangelovb0802dc2019-10-18 18:03:44 +01001910 @Override // ResolverListCommunicator
arangelov7981b122020-01-16 10:58:27 +00001911 public void onHandlePackagesChanged(ResolverListAdapter listAdapter) {
1912 if (listAdapter == mMultiProfilePagerAdapter.getActiveListAdapter()) {
arangelove5b369c2020-03-12 17:36:05 +00001913 if (listAdapter.getUserHandle().equals(getWorkProfileUserHandle())
arangelov2dd9f7b2020-02-27 10:54:24 +00001914 && mMultiProfilePagerAdapter.isWaitingToEnableWorkProfile()) {
1915 // We have just turned on the work profile and entered the pass code to start it,
1916 // now we are waiting to receive the ACTION_USER_UNLOCKED broadcast. There is no
1917 // point in reloading the list now, since the work profile user is still
1918 // turning on.
1919 return;
1920 }
arangelov7981b122020-01-16 10:58:27 +00001921 boolean listRebuilt = mMultiProfilePagerAdapter.rebuildActiveTab(true);
1922 if (listRebuilt) {
1923 ResolverListAdapter activeListAdapter =
1924 mMultiProfilePagerAdapter.getActiveListAdapter();
1925 activeListAdapter.notifyDataSetChanged();
1926 if (activeListAdapter.getCount() == 0) {
1927 // We no longer have any items... just finish the activity.
1928 finish();
1929 }
1930 }
1931 } else {
1932 mMultiProfilePagerAdapter.clearInactiveProfileCache();
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001933 }
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001934 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001935
arangelov7f8743d2020-02-13 20:34:30 +00001936 private BroadcastReceiver createWorkProfileStateReceiver() {
1937 return new BroadcastReceiver() {
1938 @Override
1939 public void onReceive(Context context, Intent intent) {
1940 String action = intent.getAction();
arangelov2dd9f7b2020-02-27 10:54:24 +00001941 if (!TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)
arangelov7f8743d2020-02-13 20:34:30 +00001942 && !TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) {
1943 return;
1944 }
arangelov2dd9f7b2020-02-27 10:54:24 +00001945 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1946 if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)
1947 && userHandle != getWorkProfileUserHandle().getIdentifier()) {
1948 return;
1949 }
1950 if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)) {
1951 mMultiProfilePagerAdapter.markWorkProfileEnabledBroadcastReceived();
1952 }
arangelov7f8743d2020-02-13 20:34:30 +00001953 if (mMultiProfilePagerAdapter.getCurrentUserHandle()
arangelove5b369c2020-03-12 17:36:05 +00001954 .equals(getWorkProfileUserHandle())) {
arangelov7f8743d2020-02-13 20:34:30 +00001955 mMultiProfilePagerAdapter.rebuildActiveTab(true);
1956 } else {
1957 mMultiProfilePagerAdapter.clearInactiveProfileCache();
1958 }
1959 }
1960 };
1961 }
1962
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001963 @VisibleForTesting
1964 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001965 public final ComponentName name;
1966 private final List<Intent> mIntents = new ArrayList<>();
1967 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001968 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001969
1970 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1971 this.name = name;
1972 add(intent, info);
1973 }
1974
1975 public void add(Intent intent, ResolveInfo info) {
1976 mIntents.add(intent);
1977 mResolveInfos.add(info);
1978 }
1979
1980 public int getCount() {
1981 return mIntents.size();
1982 }
1983
1984 public Intent getIntentAt(int index) {
1985 return index >= 0 ? mIntents.get(index) : null;
1986 }
1987
1988 public ResolveInfo getResolveInfoAt(int index) {
1989 return index >= 0 ? mResolveInfos.get(index) : null;
1990 }
1991
1992 public int findIntent(Intent intent) {
1993 for (int i = 0, N = mIntents.size(); i < N; i++) {
1994 if (intent.equals(mIntents.get(i))) {
1995 return i;
1996 }
1997 }
1998 return -1;
1999 }
2000
2001 public int findResolveInfo(ResolveInfo info) {
2002 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
2003 if (info.equals(mResolveInfos.get(i))) {
2004 return i;
2005 }
2006 }
2007 return -1;
2008 }
Alison Cichowlas1fd47152019-11-14 19:50:55 -05002009
2010 public boolean isPinned() {
2011 return mPinned;
2012 }
2013
2014 public void setPinned(boolean pinned) {
2015 mPinned = pinned;
2016 }
Adam Powell2ed547e2015-04-29 18:45:04 -07002017 }
2018
Adam Powell7d758002015-05-06 17:49:36 -07002019 class ItemClickListener implements AdapterView.OnItemClickListener,
2020 AdapterView.OnItemLongClickListener {
2021 @Override
2022 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
2023 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2024 if (listView != null) {
2025 position -= listView.getHeaderViewsCount();
2026 }
2027 if (position < 0) {
2028 // Header views don't count.
2029 return;
2030 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002031 // If we're still loading, we can't yet enable the buttons.
arangelova3912cf2019-12-13 14:34:45 +00002032 if (mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00002033 .resolveInfoForPosition(position, true) == null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002034 return;
2035 }
arangelov38a6fce2019-12-02 18:21:22 +00002036 ListView currentAdapterView =
arangelovcf268642020-01-15 15:09:51 +00002037 (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelov38a6fce2019-12-02 18:21:22 +00002038 final int checkedPos = currentAdapterView.getCheckedItemPosition();
Adam Powell7d758002015-05-06 17:49:36 -07002039 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08002040 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07002041 && (!hasValidSelection || mLastSelected != checkedPos)
Matt Pietal0d6834a2019-06-27 13:27:52 -04002042 && mAlwaysButton != null) {
2043 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Adam Powell7d758002015-05-06 17:49:36 -07002044 mOnceButton.setEnabled(hasValidSelection);
2045 if (hasValidSelection) {
arangelov38a6fce2019-12-02 18:21:22 +00002046 currentAdapterView.smoothScrollToPosition(checkedPos);
Sergey Troshin40e979e2019-12-30 17:29:50 +01002047 mOnceButton.requestFocus();
Adam Powell7d758002015-05-06 17:49:36 -07002048 }
2049 mLastSelected = checkedPos;
2050 } else {
2051 startSelected(position, false, true);
2052 }
2053 }
Adam Powell2d809622012-03-22 15:24:43 -07002054
2055 @Override
2056 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07002057 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2058 if (listView != null) {
2059 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07002060 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07002061 if (position < 0) {
2062 // Header views don't count.
2063 return false;
2064 }
arangelova3912cf2019-12-13 14:34:45 +00002065 ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00002066 .resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08002067 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07002068 return true;
2069 }
2070
2071 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002072
Dianne Hackbornec452d92014-11-11 17:16:56 -08002073 static final boolean isSpecificUriMatch(int match) {
2074 match = match&IntentFilter.MATCH_CATEGORY_MASK;
2075 return match >= IntentFilter.MATCH_CATEGORY_HOST
2076 && match <= IntentFilter.MATCH_CATEGORY_PATH;
2077 }
2078
Adam Powell4c470d62015-06-19 17:46:17 -07002079 static class PickTargetOptionRequest extends PickOptionRequest {
2080 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
2081 @Nullable Bundle extras) {
2082 super(prompt, options, extras);
2083 }
2084
2085 @Override
2086 public void onCancel() {
2087 super.onCancel();
2088 final ResolverActivity ra = (ResolverActivity) getActivity();
2089 if (ra != null) {
2090 ra.mPickOptionRequest = null;
2091 ra.finish();
2092 }
2093 }
2094
2095 @Override
2096 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
2097 super.onPickOptionResult(finished, selections, result);
2098 if (selections.length != 1) {
2099 // TODO In a better world we would filter the UI presented here and let the
2100 // user refine. Maybe later.
2101 return;
2102 }
2103
2104 final ResolverActivity ra = (ResolverActivity) getActivity();
2105 if (ra != null) {
arangelova3912cf2019-12-13 14:34:45 +00002106 final TargetInfo ti = ra.mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00002107 .getItem(selections[0].getIndex());
Adam Powell4c470d62015-06-19 17:46:17 -07002108 if (ra.onTargetSelected(ti, false)) {
2109 ra.mPickOptionRequest = null;
2110 ra.finish();
2111 }
2112 }
2113 }
2114 }
Susi Kharraz-Postdde9c3a2020-03-03 07:55:40 -05002115
2116 protected void maybeLogProfileChange() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117}