blob: 051534cc3eb10503780acf8ebeecc69bc70bdfb1 [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;
Artur Satayeved5a6ae2019-12-10 17:47:54 +000036import android.compat.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.ComponentName;
38import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.Intent;
40import android.content.IntentFilter;
arangelov7981b122020-01-16 10:58:27 +000041import android.content.PermissionChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010043import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070045import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010047import android.content.pm.UserInfo;
Jiongxuan Gao3365b162016-11-24 20:20:27 +090048import android.content.res.Configuration;
Adam Powellc5878612012-05-04 18:42:38 -070049import android.content.res.Resources;
Matt Pietal800136a2019-05-08 07:46:39 -040050import android.graphics.Insets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.net.Uri;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010052import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040054import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.PatternMatcher;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080056import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070057import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070058import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010059import android.os.UserManager;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080060import android.provider.MediaStore;
61import android.provider.Settings;
62import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.util.Log;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080064import android.util.Slog;
arangelovcf268642020-01-15 15:09:51 +000065import android.view.Gravity;
Adam Powell2d809622012-03-22 15:24:43 -070066import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.View;
68import android.view.ViewGroup;
Matt Pietal800136a2019-05-08 07:46:39 -040069import android.view.ViewGroup.LayoutParams;
70import android.view.WindowInsets;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080071import android.widget.AbsListView;
Adam Powell2d809622012-03-22 15:24:43 -070072import android.widget.AdapterView;
Adam Powellc5878612012-05-04 18:42:38 -070073import android.widget.Button;
arangelovcf268642020-01-15 15:09:51 +000074import android.widget.FrameLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070076import android.widget.ListView;
Matt Pietal800136a2019-05-08 07:46:39 -040077import android.widget.Space;
arangelovcf268642020-01-15 15:09:51 +000078import android.widget.TabHost;
79import android.widget.TabWidget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010081import android.widget.Toast;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070082
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080083import com.android.internal.R;
84import com.android.internal.annotations.VisibleForTesting;
arangelov38a6fce2019-12-02 18:21:22 +000085import com.android.internal.app.AbstractMultiProfilePagerAdapter.Profile;
arangelovb0802dc2019-10-18 18:03:44 +010086import com.android.internal.app.chooser.DisplayResolveInfo;
87import com.android.internal.app.chooser.TargetInfo;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080088import com.android.internal.content.PackageMonitor;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010089import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010090import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070091import com.android.internal.widget.ResolverDrawerLayout;
arangelovcf268642020-01-15 15:09:51 +000092import com.android.internal.widget.ViewPager;
Adam Powell2d809622012-03-22 15:24:43 -070093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010095import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import java.util.Iterator;
97import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070098import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import java.util.Set;
100
101/**
102 * This activity is displayed when the system attempts to start an Intent for
103 * which there is more than one matching activity, allowing the user to decide
104 * which to go to. It is not normally used directly by application developers.
105 */
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800106@UiThread
arangelovb0802dc2019-10-18 18:03:44 +0100107public class ResolverActivity extends Activity implements
108 ResolverListAdapter.ResolverListCommunicator {
Alison Cichowlas4691ed42018-11-13 15:59:55 -0500109
Andrei Onea15884392019-03-22 17:28:11 +0000110 @UnsupportedAppUsage
Artur Satayev751e5512019-11-15 19:12:49 +0000111 public ResolverActivity() {
112 }
113
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700114 private boolean mSafeForwardingMode;
Adam Powellc5878612012-05-04 18:42:38 -0700115 private Button mAlwaysButton;
116 private Button mOnceButton;
Matt Pietal74c6ed02019-04-18 13:38:46 -0400117 protected View mProfileView;
Adam Powell24428412015-04-01 17:19:56 -0700118 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100119 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000120 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800121 private int mLayoutId;
arangelovb0802dc2019-10-18 18:03:44 +0100122 @VisibleForTesting
123 protected final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700124 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800125 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800126 private CharSequence mTitle;
127 private int mDefaultTitleResId;
arangelovb0802dc2019-10-18 18:03:44 +0100128
129 @VisibleForTesting
130 protected boolean mUseLayoutForBrowsables;
Adam Powell09a65602014-07-20 16:23:14 -0700131
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800132 // Whether or not this activity supports choosing a default handler for the intent.
arangelovb0802dc2019-10-18 18:03:44 +0100133 @VisibleForTesting
134 protected boolean mSupportsAlwaysUseOption;
Adam Powell63b31692015-09-28 10:45:00 -0700135 protected ResolverDrawerLayout mResolverDrawerLayout;
Andrei Onea15884392019-03-22 17:28:11 +0000136 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800137 protected PackageManager mPm;
138 protected int mLaunchedFromUid;
139
140 private static final String TAG = "ResolverActivity";
141 private static final boolean DEBUG = false;
Adam Powell63b31692015-09-28 10:45:00 -0700142
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700143 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100144
Matt Pietal800136a2019-05-08 07:46:39 -0400145 protected Insets mSystemWindowInsets = null;
146 private Space mFooterSpacer = null;
147
Jorim Jaggif631ef72017-02-24 13:49:47 +0100148 /** See {@link #setRetainInOnStop}. */
149 private boolean mRetainInOnStop;
150
Matt Pietal0d6834a2019-06-27 13:27:52 -0400151 private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args";
152 private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key";
153 private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state";
154
arangelov38a6fce2019-12-02 18:21:22 +0000155 /**
156 * TODO(arangelov): Remove a couple of weeks after work/personal tabs are finalized.
157 */
arangelovcf268642020-01-15 15:09:51 +0000158 @VisibleForTesting
159 public static boolean ENABLE_TABBED_VIEW = false;
160 private static final String TAB_TAG_PERSONAL = "personal";
161 private static final String TAB_TAG_WORK = "work";
arangelov38a6fce2019-12-02 18:21:22 +0000162
arangelov7981b122020-01-16 10:58:27 +0000163 private PackageMonitor mPersonalPackageMonitor;
arangelovbdb40052020-01-09 18:01:58 +0000164 private PackageMonitor mWorkPackageMonitor;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800165
arangelov38a6fce2019-12-02 18:21:22 +0000166 @VisibleForTesting
167 protected AbstractMultiProfilePagerAdapter mMultiProfilePagerAdapter;
168
Paul McLean07425c82019-10-18 12:00:11 -0600169 // Intent extra for connected audio devices
170 public static final String EXTRA_IS_AUDIO_CAPTURE_DEVICE = "is_audio_capture_device";
171
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700172 /**
173 * Get the string resource to be used as a label for the link to the resolver activity for an
174 * action.
175 *
176 * @param action The action to resolve
177 *
178 * @return The string resource to be used as a label
179 */
180 public static @StringRes int getLabelRes(String action) {
181 return ActionTitle.forAction(action).labelRes;
182 }
183
Adam Powell278902c2014-07-12 18:33:22 -0700184 private enum ActionTitle {
185 VIEW(Intent.ACTION_VIEW,
186 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700187 com.android.internal.R.string.whichViewApplicationNamed,
188 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700189 EDIT(Intent.ACTION_EDIT,
190 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700191 com.android.internal.R.string.whichEditApplicationNamed,
192 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700193 SEND(Intent.ACTION_SEND,
194 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700195 com.android.internal.R.string.whichSendApplicationNamed,
196 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700197 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700198 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700199 com.android.internal.R.string.whichSendToApplicationNamed,
200 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700201 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
202 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700203 com.android.internal.R.string.whichSendApplicationNamed,
204 com.android.internal.R.string.whichSendApplicationLabel),
205 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
206 com.android.internal.R.string.whichImageCaptureApplication,
207 com.android.internal.R.string.whichImageCaptureApplicationNamed,
208 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700209 DEFAULT(null,
210 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700211 com.android.internal.R.string.whichApplicationNamed,
212 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700213 HOME(Intent.ACTION_MAIN,
214 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700215 com.android.internal.R.string.whichHomeApplicationNamed,
216 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700217
shafik0c7c5d52019-02-27 12:13:25 +0000218 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000219 public static final int BROWSABLE_TITLE_RES =
Matt Pietal0d6834a2019-06-27 13:27:52 -0400220 com.android.internal.R.string.whichOpenLinksWith;
221 public static final int BROWSABLE_HOST_TITLE_RES =
222 com.android.internal.R.string.whichOpenHostLinksWith;
223 public static final int BROWSABLE_HOST_APP_TITLE_RES =
224 com.android.internal.R.string.whichOpenHostLinksWithApp;
225 public static final int BROWSABLE_APP_TITLE_RES =
226 com.android.internal.R.string.whichOpenLinksWithApp;
shafik69df96322018-12-18 15:41:19 +0000227
Adam Powell278902c2014-07-12 18:33:22 -0700228 public final String action;
229 public final int titleRes;
230 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700231 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700232
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700233 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700234 this.action = action;
235 this.titleRes = titleRes;
236 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700237 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700238 }
239
240 public static ActionTitle forAction(String action) {
241 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700242 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700243 return title;
244 }
245 }
246 return DEFAULT;
247 }
248 }
249
arangelov7981b122020-01-16 10:58:27 +0000250 protected PackageMonitor createPackageMonitor(ResolverListAdapter listAdapter) {
Matt Pietalab73a882019-06-05 07:04:55 -0400251 return new PackageMonitor() {
252 @Override
253 public void onSomePackagesChanged() {
arangelov7981b122020-01-16 10:58:27 +0000254 listAdapter.handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100255 updateProfileViewButton();
Matt Pietalab73a882019-06-05 07:04:55 -0400256 }
257
258 @Override
259 public boolean onPackageChanged(String packageName, int uid, String[] components) {
260 // We care about all package changes, not just the whole package itself which is
261 // default behavior.
262 return true;
263 }
264 };
265 }
266
Dianne Hackborn905577f2011-09-07 18:31:28 -0700267 private Intent makeMyIntent() {
268 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700269 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700270 // The resolver activity is set to be hidden from recent tasks.
271 // we don't want this attribute to be propagated to the next activity
272 // being launched. Note that if the original Intent also had this
273 // flag set, we are now losing it. That should be a very rare case
274 // and we can live with this.
275 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
276 return intent;
277 }
278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 @Override
280 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700281 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700282 final Intent intent = makeMyIntent();
283 final Set<String> categories = intent.getCategories();
284 if (Intent.ACTION_MAIN.equals(intent.getAction())
285 && categories != null
286 && categories.size() == 1
287 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100288 // Note: this field is not set to true in the compatibility version.
289 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700290 }
291
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700292 setSafeForwardingMode(true);
293
Adam Powella35c77a2014-09-25 16:46:36 -0700294 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296
Adam Powell278902c2014-07-12 18:33:22 -0700297 /**
Adam Powell24428412015-04-01 17:19:56 -0700298 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700299 * a default title resource
300 */
Andrei Onea15884392019-03-22 17:28:11 +0000301 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700303 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800304 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
305 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
306 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700307 }
308
309 protected void onCreate(Bundle savedInstanceState, Intent intent,
310 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800311 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700312 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000314
315 // Determine whether we should show that intent is forwarded
316 // from managed profile to owner or other way around.
317 setProfileSwitchMessageId(intent.getContentUserHint());
318
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700319 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700320 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700321 getActivityToken());
322 } catch (RemoteException e) {
323 mLaunchedFromUid = -1;
324 }
Adam Powell7d758002015-05-06 17:49:36 -0700325
326 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
327 // Gulp!
328 finish();
329 return;
330 }
331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700333
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800334 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800335
Adam Powell7d758002015-05-06 17:49:36 -0700336 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700337 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800338 mTitle = title;
339 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700340
shafik69df96322018-12-18 15:41:19 +0000341 mUseLayoutForBrowsables = getTargetIntent() == null
342 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000343 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000344
Matt Pietal0d6834a2019-06-27 13:27:52 -0400345 mSupportsAlwaysUseOption = supportsAlwaysUseOption;
shafik69df96322018-12-18 15:41:19 +0000346
arangelov38a6fce2019-12-02 18:21:22 +0000347 // The last argument of createResolverListAdapter is whether to do special handling
arangelovb0802dc2019-10-18 18:03:44 +0100348 // of the last used choice to highlight it in the list. We need to always
349 // turn this off when running under voice interaction, since it results in
350 // a more complicated UI that the current voice interaction flow is not able
351 // to handle.
352 boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction();
arangelov38a6fce2019-12-02 18:21:22 +0000353 mMultiProfilePagerAdapter = createMultiProfilePagerAdapter(initialIntents, rList, filterLastUsed);
arangelovbb572332019-11-13 12:50:08 +0000354 if (configureContentView()) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700355 return;
356 }
Adam Powell278902c2014-07-12 18:33:22 -0700357
arangelov7981b122020-01-16 10:58:27 +0000358 mPersonalPackageMonitor = createPackageMonitor(
359 mMultiProfilePagerAdapter.getPersonalListAdapter());
360 mPersonalPackageMonitor.register(
361 this, getMainLooper(), getPersonalProfileUserHandle(), false);
362 if (hasWorkProfile()) {
363 mWorkPackageMonitor = createPackageMonitor(
364 mMultiProfilePagerAdapter.getWorkListAdapter());
365 mWorkPackageMonitor.register(this, getMainLooper(), getWorkProfileUserHandle(), false);
366 }
367
368 mRegistered = true;
369
Alan Viverette51efddb2017-04-05 10:00:01 -0400370 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700371 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700372 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700373 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700374 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700375 finish();
376 }
377 });
Sergey Troshin40e979e2019-12-30 17:29:50 +0100378
379 boolean hasTouchScreen = getPackageManager()
380 .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN);
381
382 if (isVoiceInteraction() || !hasTouchScreen) {
Adam Powell4c470d62015-06-19 17:46:17 -0700383 rdl.setCollapsed(false);
384 }
Matt Pietal800136a2019-05-08 07:46:39 -0400385
386 rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
387 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
388 rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets);
389
Adam Powell63b31692015-09-28 10:45:00 -0700390 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700391 }
392
Adam Powell88831a22014-11-20 18:17:00 -0800393 mProfileView = findViewById(R.id.profile_button);
394 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400395 mProfileView.setOnClickListener(this::onProfileClick);
arangelovb0802dc2019-10-18 18:03:44 +0100396 updateProfileViewButton();
Adam Powell88831a22014-11-20 18:17:00 -0800397 }
Adam Powell4c470d62015-06-19 17:46:17 -0700398
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100399 final Set<String> categories = intent.getCategories();
arangelova3912cf2019-12-13 14:34:45 +0000400 MetricsLogger.action(this, mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem()
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100401 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
402 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
403 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100404 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700405 }
406
arangelov38a6fce2019-12-02 18:21:22 +0000407 protected AbstractMultiProfilePagerAdapter createMultiProfilePagerAdapter(
408 Intent[] initialIntents,
409 List<ResolveInfo> rList,
410 boolean filterLastUsed) {
411 AbstractMultiProfilePagerAdapter resolverMultiProfilePagerAdapter = null;
412 if (hasWorkProfile() && ENABLE_TABBED_VIEW) {
413 resolverMultiProfilePagerAdapter =
414 createResolverMultiProfilePagerAdapterForTwoProfiles(
415 initialIntents, rList, filterLastUsed);
416 } else {
417 resolverMultiProfilePagerAdapter = createResolverMultiProfilePagerAdapterForOneProfile(
418 initialIntents, rList, filterLastUsed);
419 }
420 return resolverMultiProfilePagerAdapter;
421 }
422
423 private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForOneProfile(
424 Intent[] initialIntents,
425 List<ResolveInfo> rList, boolean filterLastUsed) {
426 ResolverListAdapter adapter = createResolverListAdapter(
427 /* context */ this,
428 /* payloadIntents */ mIntents,
429 initialIntents,
430 rList,
431 filterLastUsed,
432 mUseLayoutForBrowsables,
433 /* userHandle */ UserHandle.of(UserHandle.myUserId()));
434 return new ResolverMultiProfilePagerAdapter(
435 /* context */ this,
arangelov7981b122020-01-16 10:58:27 +0000436 adapter,
437 getPersonalProfileUserHandle(),
438 /* workProfileUserHandle= */ null);
arangelov38a6fce2019-12-02 18:21:22 +0000439 }
440
441 private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForTwoProfiles(
442 Intent[] initialIntents,
443 List<ResolveInfo> rList,
444 boolean filterLastUsed) {
arangelovcf268642020-01-15 15:09:51 +0000445 // We only show the default app for the profile of the current user. The filterLastUsed
446 // flag determines whether to show a default app and that app is not shown in the
447 // resolver list. So filterLastUsed should be false for the other profile.
arangelov38a6fce2019-12-02 18:21:22 +0000448 ResolverListAdapter personalAdapter = createResolverListAdapter(
449 /* context */ this,
450 /* payloadIntents */ mIntents,
451 initialIntents,
452 rList,
arangelovcf268642020-01-15 15:09:51 +0000453 (filterLastUsed && UserHandle.myUserId()
454 == getPersonalProfileUserHandle().getIdentifier()),
arangelov38a6fce2019-12-02 18:21:22 +0000455 mUseLayoutForBrowsables,
456 /* userHandle */ getPersonalProfileUserHandle());
arangelov7981b122020-01-16 10:58:27 +0000457 UserHandle workProfileUserHandle = getWorkProfileUserHandle();
arangelov38a6fce2019-12-02 18:21:22 +0000458 ResolverListAdapter workAdapter = createResolverListAdapter(
459 /* context */ this,
460 /* payloadIntents */ mIntents,
461 initialIntents,
462 rList,
arangelovcf268642020-01-15 15:09:51 +0000463 (filterLastUsed && UserHandle.myUserId()
arangelov7981b122020-01-16 10:58:27 +0000464 == workProfileUserHandle.getIdentifier()),
arangelov38a6fce2019-12-02 18:21:22 +0000465 mUseLayoutForBrowsables,
arangelov7981b122020-01-16 10:58:27 +0000466 /* userHandle */ workProfileUserHandle);
arangelov38a6fce2019-12-02 18:21:22 +0000467 return new ResolverMultiProfilePagerAdapter(
468 /* context */ this,
469 personalAdapter,
470 workAdapter,
arangelov7981b122020-01-16 10:58:27 +0000471 /* defaultProfile */ getCurrentProfile(),
472 getPersonalProfileUserHandle(),
473 getWorkProfileUserHandle());
arangelov38a6fce2019-12-02 18:21:22 +0000474 }
475
476 protected @Profile int getCurrentProfile() {
477 return (UserHandle.myUserId() == UserHandle.USER_SYSTEM ? PROFILE_PERSONAL : PROFILE_WORK);
478 }
479
480 protected UserHandle getPersonalProfileUserHandle() {
481 return UserHandle.of(ActivityManager.getCurrentUser());
482 }
483 protected @Nullable UserHandle getWorkProfileUserHandle() {
484 UserManager userManager = getSystemService(UserManager.class);
485 for (final UserInfo userInfo : userManager.getProfiles(ActivityManager.getCurrentUser())) {
486 if (userInfo.isManagedProfile()) {
487 return userInfo.getUserHandle();
488 }
489 }
490 return null;
491 }
492
493 protected boolean hasWorkProfile() {
494 return getWorkProfileUserHandle() != null;
495 }
496
Matt Pietal74c6ed02019-04-18 13:38:46 -0400497 protected void onProfileClick(View v) {
arangelov38a6fce2019-12-02 18:21:22 +0000498 final DisplayResolveInfo dri =
arangelova3912cf2019-12-13 14:34:45 +0000499 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400500 if (dri == null) {
501 return;
502 }
503
504 // Do not show the profile switch message anymore.
505 mProfileSwitchMessageId = -1;
506
507 onTargetSelected(dri, false);
508 finish();
509 }
510
Matt Pietal9236adc2019-12-12 09:24:23 -0500511 /**
512 * Numerous layouts are supported, each with optional ViewGroups.
513 * Make sure the inset gets added to the correct View, using
514 * a footer for Lists so it can properly scroll under the navbar.
515 */
516 protected boolean shouldAddFooterView() {
517 if (useLayoutWithDefault()) return true;
518
519 View buttonBar = findViewById(R.id.button_bar);
520 if (buttonBar == null || buttonBar.getVisibility() == View.GONE) return true;
521
522 return false;
523 }
524
525 protected void applyFooterView(int height) {
526 if (mFooterSpacer == null) {
527 mFooterSpacer = new Space(getApplicationContext());
528 } else {
529 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
arangelovcf268642020-01-15 15:09:51 +0000530 .getActiveAdapterView().removeFooterView(mFooterSpacer);
Matt Pietal9236adc2019-12-12 09:24:23 -0500531 }
532 mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
533 mSystemWindowInsets.bottom));
534 ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter)
arangelovcf268642020-01-15 15:09:51 +0000535 .getActiveAdapterView().addFooterView(mFooterSpacer);
Matt Pietal9236adc2019-12-12 09:24:23 -0500536 }
537
Matt Pietal800136a2019-05-08 07:46:39 -0400538 protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
539 mSystemWindowInsets = insets.getSystemWindowInsets();
540
541 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
542 mSystemWindowInsets.right, 0);
543
Matt Pietal9236adc2019-12-12 09:24:23 -0500544 resetButtonBar();
545
Matt Pietald5071202019-10-10 11:21:28 -0400546 // Need extra padding so the list can fully scroll up
Matt Pietal9236adc2019-12-12 09:24:23 -0500547 if (shouldAddFooterView()) {
548 applyFooterView(mSystemWindowInsets.bottom);
Matt Pietal800136a2019-05-08 07:46:39 -0400549 }
Matt Pietal800136a2019-05-08 07:46:39 -0400550
Matt Pietal9236adc2019-12-12 09:24:23 -0500551 View emptyView = findViewById(R.id.empty);
552 if (emptyView != null) {
553 emptyView.setPadding(0, 0, 0, mSystemWindowInsets.bottom
554 + getResources().getDimensionPixelSize(
555 R.dimen.chooser_edge_margin_normal) * 2);
556 }
Matt Pietal78fdbd42019-06-05 07:08:08 -0400557
Matt Pietal800136a2019-05-08 07:46:39 -0400558 return insets.consumeSystemWindowInsets();
559 }
560
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900561 @Override
562 public void onConfigurationChanged(Configuration newConfig) {
563 super.onConfigurationChanged(newConfig);
arangelova3912cf2019-12-13 14:34:45 +0000564 mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400565
566 if (mSystemWindowInsets != null) {
567 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
568 mSystemWindowInsets.right, 0);
569 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900570 }
571
arangelovb0802dc2019-10-18 18:03:44 +0100572 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -0800573 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700574 if (!isVoiceInteraction()) {
575 // Clearly not needed.
576 return;
577 }
578
arangelova3912cf2019-12-13 14:34:45 +0000579 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getCount();
arangelov38a6fce2019-12-02 18:21:22 +0000580 final Option[] options = new Option[count];
Adam Powell4c470d62015-06-19 17:46:17 -0700581 for (int i = 0, N = options.length; i < N; i++) {
arangelova3912cf2019-12-13 14:34:45 +0000582 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter().getItem(i);
Matt Pietal2cf4c6c2019-09-10 09:33:11 -0400583 if (target == null) {
584 // If this occurs, a new set of targets is being loaded. Let that complete,
585 // and have the next call to send voice choices proceed instead.
586 return;
587 }
588 options[i] = optionForChooserTarget(target, i);
Adam Powell4c470d62015-06-19 17:46:17 -0700589 }
590
591 mPickOptionRequest = new PickTargetOptionRequest(
592 new Prompt(getTitle()), options, null);
593 getVoiceInteractor().submitRequest(mPickOptionRequest);
594 }
595
596 Option optionForChooserTarget(TargetInfo target, int index) {
597 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800598 }
599
Adam Powell2ed547e2015-04-29 18:45:04 -0700600 protected final void setAdditionalTargets(Intent[] intents) {
601 if (intents != null) {
602 for (Intent intent : intents) {
603 mIntents.add(intent);
604 }
605 }
606 }
607
arangelovb0802dc2019-10-18 18:03:44 +0100608 @Override // SelectableTargetInfoCommunicator ResolverListCommunicator
Adam Powell0ccc0e92015-04-23 17:19:37 -0700609 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700610 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700611 }
612
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800613 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700614 final Uri referrer = getReferrer();
615 if (referrer != null && "android-app".equals(referrer.getScheme())) {
616 return referrer.getHost();
617 }
618 return null;
619 }
620
Adam Powell23882512016-01-29 10:21:00 -0800621 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700622 return R.layout.resolver_list;
623 }
624
arangelovb0802dc2019-10-18 18:03:44 +0100625 @Override // ResolverListCommunicator
626 public void updateProfileViewButton() {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400627 if (mProfileView == null) {
628 return;
629 }
630
arangelov38a6fce2019-12-02 18:21:22 +0000631 final DisplayResolveInfo dri =
arangelova3912cf2019-12-13 14:34:45 +0000632 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile();
arangelov38a6fce2019-12-02 18:21:22 +0000633 if (dri != null && !ENABLE_TABBED_VIEW) {
Adam Powell88831a22014-11-20 18:17:00 -0800634 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800635 View text = mProfileView.findViewById(R.id.profile_button);
636 if (!(text instanceof TextView)) {
637 text = mProfileView.findViewById(R.id.text1);
638 }
639 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800640 } else {
641 mProfileView.setVisibility(View.GONE);
642 }
Adam Powell278902c2014-07-12 18:33:22 -0700643 }
644
Sander Alewijnsef6545332014-10-31 12:39:02 +0000645 private void setProfileSwitchMessageId(int contentUserHint) {
646 if (contentUserHint != UserHandle.USER_CURRENT &&
647 contentUserHint != UserHandle.myUserId()) {
648 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
649 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
650 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
651 : false;
652 boolean targetIsManaged = userManager.isManagedProfile();
653 if (originIsManaged && !targetIsManaged) {
654 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
655 } else if (!originIsManaged && targetIsManaged) {
656 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
657 }
658 }
659 }
660
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700661 /**
662 * Turn on launch mode that is safe to use when forwarding intents received from
663 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
664 * instead of the normal Activity.startActivity for launching the activity selected
665 * by the user.
666 *
667 * <p>This mode is set to true by default if the activity is initialized through
668 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
669 * methods, it is set to false by default. You must set it before calling one of the
670 * more detailed onCreate methods, so that it will be set correctly in the case where
671 * there is only one intent to resolve and it is thus started immediately.</p>
672 */
673 public void setSafeForwardingMode(boolean safeForwarding) {
674 mSafeForwardingMode = safeForwarding;
675 }
676
shafik69df96322018-12-18 15:41:19 +0000677 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
678 final ActionTitle title = mResolvingHome
679 ? ActionTitle.HOME
680 : ActionTitle.forAction(intent.getAction());
681
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800682 // While there may already be a filtered item, we can only use it in the title if the list
683 // is already sorted and all information relevant to it is already in the list.
arangelov38a6fce2019-12-02 18:21:22 +0000684 final boolean named =
arangelova3912cf2019-12-13 14:34:45 +0000685 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700686 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
687 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000688 } else if (isHttpSchemeAndViewAction(intent)) {
689 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000690 // they're giving access for the activity to open URLs from this specific host
Matt Pietal0d6834a2019-06-27 13:27:52 -0400691 String dialogTitle = null;
692 if (named && !mUseLayoutForBrowsables) {
693 dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES,
arangelova3912cf2019-12-13 14:34:45 +0000694 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredItem()
arangelov38a6fce2019-12-02 18:21:22 +0000695 .getDisplayLabel());
Matt Pietal0d6834a2019-06-27 13:27:52 -0400696 } else if (named && mUseLayoutForBrowsables) {
697 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES,
698 intent.getData().getHost(),
arangelova3912cf2019-12-13 14:34:45 +0000699 mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredItem()
arangelov38a6fce2019-12-02 18:21:22 +0000700 .getDisplayLabel());
arangelova3912cf2019-12-13 14:34:45 +0000701 } else if (mMultiProfilePagerAdapter.getActiveListAdapter().areAllTargetsBrowsers()) {
Matt Pietald5071202019-10-10 11:21:28 -0400702 dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES);
Matt Pietal0d6834a2019-06-27 13:27:52 -0400703 } else {
704 dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES,
705 intent.getData().getHost());
706 }
707 return dialogTitle;
Adam Powell278902c2014-07-12 18:33:22 -0700708 } else {
Adam Powell24428412015-04-01 17:19:56 -0700709 return named
arangelov38a6fce2019-12-02 18:21:22 +0000710 ? getString(title.namedTitleRes, mMultiProfilePagerAdapter
arangelova3912cf2019-12-13 14:34:45 +0000711 .getActiveListAdapter().getFilteredItem().getDisplayLabel())
Adam Powell24428412015-04-01 17:19:56 -0700712 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700713 }
Adam Powellc5878612012-05-04 18:42:38 -0700714 }
715
Adam Powelle9414d92014-07-05 17:44:18 -0700716 void dismiss() {
717 if (!isFinishing()) {
718 finish();
719 }
720 }
721
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800722 @Override
723 protected void onRestart() {
724 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700725 if (!mRegistered) {
arangelovbdb40052020-01-09 18:01:58 +0000726 mPersonalPackageMonitor.register(this, getMainLooper(),
727 getPersonalProfileUserHandle(), false);
728 if (hasWorkProfile()) {
729 if (mWorkPackageMonitor == null) {
arangelov7981b122020-01-16 10:58:27 +0000730 mWorkPackageMonitor = createPackageMonitor(
731 mMultiProfilePagerAdapter.getWorkListAdapter());
arangelovbdb40052020-01-09 18:01:58 +0000732 }
733 mWorkPackageMonitor.register(this, getMainLooper(),
734 getWorkProfileUserHandle(), false);
735 }
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700736 mRegistered = true;
737 }
arangelova3912cf2019-12-13 14:34:45 +0000738 mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged();
arangelovb0802dc2019-10-18 18:03:44 +0100739 updateProfileViewButton();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800740 }
741
742 @Override
743 protected void onStop() {
744 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700745 if (mRegistered) {
arangelovbdb40052020-01-09 18:01:58 +0000746 mPersonalPackageMonitor.unregister();
747 if (mWorkPackageMonitor != null) {
748 mWorkPackageMonitor.unregister();
749 }
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700750 mRegistered = false;
751 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700752 final Intent intent = getIntent();
753 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100754 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700755 // This resolver is in the unusual situation where it has been
756 // launched at the top of a new task. We don't let it be added
757 // to the recent tasks shown to the user, and we need to make sure
758 // that each time we are launched we get the correct launching
759 // uid (not re-using the same resolver from an old launching uid),
760 // so we will now finish ourself since being no longer visible,
761 // the user probably can't get back to us.
762 if (!isChangingConfigurations()) {
763 finish();
764 }
765 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800766 }
767
Adam Powellc5878612012-05-04 18:42:38 -0700768 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700769 protected void onDestroy() {
770 super.onDestroy();
771 if (!isChangingConfigurations() && mPickOptionRequest != null) {
772 mPickOptionRequest.cancel();
773 }
arangelova3912cf2019-12-13 14:34:45 +0000774 if (mMultiProfilePagerAdapter.getActiveListAdapter() != null) {
775 mMultiProfilePagerAdapter.getActiveListAdapter().onDestroy();
Kang Li38a6da642017-04-05 12:30:55 -0700776 }
Adam Powell4c470d62015-06-19 17:46:17 -0700777 }
778
779 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700780 protected void onRestoreInstanceState(Bundle savedInstanceState) {
781 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000782 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700783 }
784
shafik0c7c5d52019-02-27 12:13:25 +0000785 private boolean isHttpSchemeAndViewAction(Intent intent) {
786 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
787 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
788 && Intent.ACTION_VIEW.equals(intent.getAction());
789 }
790
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100791 private boolean hasManagedProfile() {
792 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
793 if (userManager == null) {
794 return false;
795 }
796
797 try {
798 List<UserInfo> profiles = userManager.getProfiles(getUserId());
799 for (UserInfo userInfo : profiles) {
800 if (userInfo != null && userInfo.isManagedProfile()) {
801 return true;
802 }
803 }
804 } catch (SecurityException e) {
805 return false;
806 }
807 return false;
808 }
809
810 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
811 try {
812 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
813 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700814 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100815 } catch (NameNotFoundException e) {
816 return false;
817 }
818 }
819
Adam Powell278902c2014-07-12 18:33:22 -0700820 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
821 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100822 boolean enabled = false;
Paul McLean3a30e552019-12-07 14:47:29 -0700823 ResolveInfo ri = null;
Nicolas Prevot50449882014-06-23 12:42:37 +0100824 if (hasValidSelection) {
arangelova3912cf2019-12-13 14:34:45 +0000825 ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +0000826 .resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800827 if (ri == null) {
828 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
829 return;
830 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
831 Log.e(TAG, "Attempted to set selection to resolve info for another user");
832 return;
833 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100834 enabled = true;
835 }
Matt Pietal0d6834a2019-06-27 13:27:52 -0400836 if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) {
837 mAlwaysButton.setText(getResources()
838 .getString(R.string.activity_resolver_set_always));
839 } else {
840 mAlwaysButton.setText(getResources()
841 .getString(R.string.activity_resolver_use_always));
842 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100843 }
Paul McLean3a30e552019-12-07 14:47:29 -0700844
845 ActivityInfo activityInfo = ri.activityInfo;
846
847 boolean hasRecordPermission =
Paul McLeanc613c772019-12-13 11:36:16 -0700848 mPm.checkPermission(android.Manifest.permission.RECORD_AUDIO,
Paul McLean3a30e552019-12-07 14:47:29 -0700849 activityInfo.packageName)
Paul McLeanc613c772019-12-13 11:36:16 -0700850 == android.content.pm.PackageManager.PERMISSION_GRANTED;
Paul McLean3a30e552019-12-07 14:47:29 -0700851
852 if (!hasRecordPermission) {
853 // OK, we know the record permission, is this a capture device
854 boolean hasAudioCapture =
855 getIntent().getBooleanExtra(
856 ResolverActivity.EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
857 enabled = !hasAudioCapture;
858 }
Nicolas Prevot50449882014-06-23 12:42:37 +0100859 mAlwaysButton.setEnabled(enabled);
860 }
861
Adam Powellc5878612012-05-04 18:42:38 -0700862 public void onButtonClick(View v) {
863 final int id = v.getId();
arangelovcf268642020-01-15 15:09:51 +0000864 ListView listView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelova3912cf2019-12-13 14:34:45 +0000865 ResolverListAdapter currentListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter();
arangelov38a6fce2019-12-02 18:21:22 +0000866 int which = currentListAdapter.hasFilteredItem()
867 ? currentListAdapter.getFilteredPosition()
868 : listView.getCheckedItemPosition();
869 boolean hasIndexBeenFiltered = !currentListAdapter.hasFilteredItem();
870 ResolveInfo ri = currentListAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Matt Pietala310c1f2019-07-03 09:12:56 -0400871 if (mUseLayoutForBrowsables
872 && !ri.handleAllWebDataURI && id == R.id.button_always) {
Matt Pietal0d6834a2019-06-27 13:27:52 -0400873 showSettingsForSelected(ri);
shafik69df96322018-12-18 15:41:19 +0000874 } else {
875 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
876 }
877 }
878
Matt Pietal0d6834a2019-06-27 13:27:52 -0400879 private void showSettingsForSelected(ResolveInfo ri) {
shafik34064c32019-03-06 15:42:52 +0000880 Intent intent = new Intent();
Matt Pietal0d6834a2019-06-27 13:27:52 -0400881
882 final String packageName = ri.activityInfo.packageName;
883 Bundle showFragmentArgs = new Bundle();
884 showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY);
885 showFragmentArgs.putString("package", packageName);
886
shafik34064c32019-03-06 15:42:52 +0000887 // For regular apps, we open the Open by Default page
Matt Pietal0d6834a2019-06-27 13:27:52 -0400888 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
889 .setData(Uri.fromParts("package", packageName, null))
890 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
891 .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY)
892 .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs);
893
shafik34064c32019-03-06 15:42:52 +0000894 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700895 }
896
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800897 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700898 if (isFinishing()) {
899 return;
900 }
arangelova3912cf2019-12-13 14:34:45 +0000901 ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +0000902 .resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000903 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
904 Toast.makeText(this, String.format(getResources().getString(
905 com.android.internal.R.string.activity_resolver_work_profiles_support),
906 ri.activityInfo.loadLabel(getPackageManager()).toString()),
907 Toast.LENGTH_LONG).show();
908 return;
909 }
910
arangelova3912cf2019-12-13 14:34:45 +0000911 TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +0000912 .targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800913 if (target == null) {
914 return;
915 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700916 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800917 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100918 MetricsLogger.action(
919 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800920 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100921 MetricsLogger.action(
922 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
923 } else {
924 MetricsLogger.action(
925 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
926 }
arangelov38a6fce2019-12-02 18:21:22 +0000927 MetricsLogger.action(this,
arangelova3912cf2019-12-13 14:34:45 +0000928 mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem()
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100929 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
930 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700931 finish();
932 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934
Adam Powelle49d9392014-07-17 18:45:19 -0700935 /**
936 * Replace me in subclasses!
937 */
arangelovb0802dc2019-10-18 18:03:44 +0100938 @Override // ResolverListCommunicator
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000939 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700940 return defIntent;
941 }
942
arangelovb0802dc2019-10-18 18:03:44 +0100943 @Override // ResolverListCommunicator
arangelov7981b122020-01-16 10:58:27 +0000944 public void onPostListReady(ResolverListAdapter listAdapter, boolean doPostProcessing) {
945 if (isAutolaunching() || maybeAutolaunchActivity()) {
946 return;
arangelovcf268642020-01-15 15:09:51 +0000947 }
arangelov7981b122020-01-16 10:58:27 +0000948 if (doPostProcessing) {
949 if (mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier()
950 == UserHandle.myUserId()) {
951 setHeader();
952 }
953 resetButtonBar();
954 onListRebuilt(listAdapter);
955 }
arangelovb0802dc2019-10-18 18:03:44 +0100956 }
957
arangelova3912cf2019-12-13 14:34:45 +0000958 protected void onListRebuilt(ResolverListAdapter listAdapter) {
arangelovcf268642020-01-15 15:09:51 +0000959 final ItemClickListener listener = new ItemClickListener();
960 setupAdapterListView((ListView) mMultiProfilePagerAdapter.getActiveAdapterView(), listener);
arangelovb0802dc2019-10-18 18:03:44 +0100961 }
962
Adam Powell2ed547e2015-04-29 18:45:04 -0700963 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700964 final ResolveInfo ri = target.getResolveInfo();
965 final Intent intent = target != null ? target.getResolvedIntent() : null;
966
arangelov38a6fce2019-12-02 18:21:22 +0000967 if (intent != null && (mSupportsAlwaysUseOption
arangelova3912cf2019-12-13 14:34:45 +0000968 || mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem())
969 && mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredResolveList() != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700970 // Build a reasonable intent filter, based on what matched.
971 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200972 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973
Henrik Engström3277cf12014-07-17 12:18:29 +0200974 if (intent.getSelector() != null) {
975 filterIntent = intent.getSelector();
976 } else {
977 filterIntent = intent;
978 }
979
980 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800981 if (action != null) {
982 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200984 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700985 if (categories != null) {
986 for (String cat : categories) {
987 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700990 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991
Adam Powell24428412015-04-01 17:19:56 -0700992 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200993 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700994 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200995 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700996 if (mimeType != null) {
997 try {
998 filter.addDataType(mimeType);
999 } catch (IntentFilter.MalformedMimeTypeException e) {
1000 Log.w("ResolverActivity", e);
1001 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001004 }
1005 if (data != null && data.getScheme() != null) {
1006 // We need the data specification if there was no type,
1007 // OR if the scheme is not one of our magical "file:"
1008 // or "content:" schemes (see IntentFilter for the reason).
1009 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
1010 || (!"file".equals(data.getScheme())
1011 && !"content".equals(data.getScheme()))) {
1012 filter.addDataScheme(data.getScheme());
1013
1014 // Look through the resolved filter to determine which part
1015 // of it matched the original Intent.
1016 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
1017 if (pIt != null) {
1018 String ssp = data.getSchemeSpecificPart();
1019 while (ssp != null && pIt.hasNext()) {
1020 PatternMatcher p = pIt.next();
1021 if (p.match(ssp)) {
1022 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
1023 break;
1024 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07001025 }
1026 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001027 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
1028 if (aIt != null) {
1029 while (aIt.hasNext()) {
1030 IntentFilter.AuthorityEntry a = aIt.next();
1031 if (a.match(data) >= 0) {
1032 int port = a.getPort();
1033 filter.addDataAuthority(a.getHost(),
1034 port >= 0 ? Integer.toString(port) : null);
1035 break;
1036 }
1037 }
1038 }
1039 pIt = ri.filter.pathsIterator();
1040 if (pIt != null) {
1041 String path = data.getPath();
1042 while (path != null && pIt.hasNext()) {
1043 PatternMatcher p = pIt.next();
1044 if (p.match(path)) {
1045 filter.addDataPath(p.getPath(), p.getType());
1046 break;
1047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 }
1050 }
1051 }
1052
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001053 if (filter != null) {
arangelova3912cf2019-12-13 14:34:45 +00001054 final int N = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001055 .getUnfilteredResolveList().size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001056 ComponentName[] set;
1057 // If we don't add back in the component for forwarding the intent to a managed
1058 // profile, the preferred activity may not be updated correctly (as the set of
1059 // components we tell it we knew about will have changed).
arangelovb0802dc2019-10-18 18:03:44 +01001060 final boolean needToAddBackProfileForwardingComponent =
arangelova3912cf2019-12-13 14:34:45 +00001061 mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001062 if (!needToAddBackProfileForwardingComponent) {
1063 set = new ComponentName[N];
1064 } else {
1065 set = new ComponentName[N + 1];
1066 }
1067
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001068 int bestMatch = 0;
1069 for (int i=0; i<N; i++) {
arangelova3912cf2019-12-13 14:34:45 +00001070 ResolveInfo r = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001071 .getUnfilteredResolveList().get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001072 set[i] = new ComponentName(r.activityInfo.packageName,
1073 r.activityInfo.name);
1074 if (r.match > bestMatch) bestMatch = r.match;
1075 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001076
1077 if (needToAddBackProfileForwardingComponent) {
arangelova3912cf2019-12-13 14:34:45 +00001078 set[N] = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001079 .getOtherProfile().getResolvedComponentName();
arangelova3912cf2019-12-13 14:34:45 +00001080 final int otherProfileMatch = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001081 .getOtherProfile().getResolveInfo().match;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001082 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
1083 }
1084
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001085 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001086 final int userId = getUserId();
1087 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001088
1089 // Set the preferred Activity
1090 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
1091
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001092 if (ri.handleAllWebDataURI) {
1093 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001094 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001095 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001096 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001097 }
1098 } else {
1099 // Update Domain Verification status
1100 ComponentName cn = intent.getComponent();
1101 String packageName = cn.getPackageName();
1102 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001103
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001104 boolean isHttpOrHttps = (dataScheme != null) &&
1105 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
1106 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001107
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001108 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
1109 boolean hasCategoryBrowsable = (categories != null) &&
1110 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001111
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001112 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001113 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001114 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
1115 userId);
1116 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001117 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001118 } else {
1119 try {
arangelova3912cf2019-12-13 14:34:45 +00001120 mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001121 .mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001122 } catch (RemoteException re) {
1123 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1124 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127 }
1128
Adam Powell24428412015-04-01 17:19:56 -07001129 if (target != null) {
arangelov9e148522020-01-15 21:11:51 +00001130 if (intent != null) {
1131 intent.fixUris(UserHandle.myUserId());
1132 }
Adam Powell24428412015-04-01 17:19:56 -07001133 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -04001134
1135 // Rely on the ActivityManager to pop up a dialog regarding app suspension
1136 // and return false
1137 if (target.isSuspended()) {
1138 return false;
1139 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001140 }
Matt Pietala4b30072019-04-04 13:44:36 -04001141
Adam Powell2ed547e2015-04-29 18:45:04 -07001142 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001143 }
1144
arangelovcf268642020-01-15 15:09:51 +00001145 @VisibleForTesting
Adam Powell23882512016-01-29 10:21:00 -08001146 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -07001147 // We're dispatching intents that might be coming from legacy apps, so
1148 // don't kill ourselves.
1149 StrictMode.disableDeathOnFileUriExposure();
1150 try {
1151 safelyStartActivityInternal(cti);
1152 } finally {
1153 StrictMode.enableDeathOnFileUriExposure();
1154 }
1155 }
1156
1157 private void safelyStartActivityInternal(TargetInfo cti) {
arangelov7981b122020-01-16 10:58:27 +00001158 mPersonalPackageMonitor.unregister();
1159 if (mWorkPackageMonitor != null) {
1160 mWorkPackageMonitor.unregister();
1161 }
1162 mRegistered = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +00001163 // If needed, show that intent is forwarded
1164 // from managed profile to owner or other way around.
1165 if (mProfileSwitchMessageId != -1) {
1166 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
1167 }
arangelov38a6fce2019-12-02 18:21:22 +00001168 UserHandle currentUserHandle = mMultiProfilePagerAdapter.getCurrentUserHandle();
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001169 if (!mSafeForwardingMode) {
arangelov38a6fce2019-12-02 18:21:22 +00001170 if (cti.startAsUser(this, null, currentUserHandle)) {
Adam Powell24428412015-04-01 17:19:56 -07001171 onActivityStarted(cti);
1172 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001173 return;
1174 }
1175 try {
arangelov38a6fce2019-12-02 18:21:22 +00001176 if (cti.startAsCaller(this, null, currentUserHandle.getIdentifier())) {
Adam Powell24428412015-04-01 17:19:56 -07001177 onActivityStarted(cti);
1178 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001179 } catch (RuntimeException e) {
1180 String launchedFromPackage;
1181 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001182 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001183 getActivityToken());
1184 } catch (RemoteException e2) {
1185 launchedFromPackage = "??";
1186 }
1187 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
1188 + " package " + launchedFromPackage + ", while running in "
1189 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192
Alison Cichowlas3e340502018-08-07 17:15:01 -04001193
arangelovb0802dc2019-10-18 18:03:44 +01001194 public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
Alison Cichowlas3e340502018-08-07 17:15:01 -04001195 int userId) {
1196 // Pass intent to delegate chooser activity with permission token.
1197 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
1198 // moves into systemui
1199 try {
1200 // TODO: Once this is a small springboard activity, it can move off the UI process
1201 // and we can move the request method to ActivityManagerInternal.
1202 IBinder permissionToken = ActivityTaskManager.getService()
1203 .requestStartActivityPermissionToken(getActivityToken());
1204 final Intent chooserIntent = new Intent();
1205 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1206 Resources.getSystem().getString(R.string.config_chooserActivity));
1207 chooserIntent.setClassName(delegateActivity.getPackageName(),
1208 delegateActivity.getClassName());
1209 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1210
1211 // TODO: These extras will change as chooser activity moves into systemui
1212 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1213 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1214 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1215 ignoreTargetSecurity);
1216 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001217 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1218 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001219 startActivity(chooserIntent);
1220 } catch (RemoteException e) {
1221 Log.e(TAG, e.toString());
1222 }
1223 return true;
1224 }
1225
Adam Powell23882512016-01-29 10:21:00 -08001226 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001227 // Do nothing
1228 }
1229
arangelovb0802dc2019-10-18 18:03:44 +01001230 @Override // ResolverListCommunicator
Adam Powell23882512016-01-29 10:21:00 -08001231 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001232 return false;
1233 }
1234
Adam Powell23882512016-01-29 10:21:00 -08001235 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001236 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001237 }
1238
Alison Cichowlas19ee2922019-12-16 19:43:12 -05001239 void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001240 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001241 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001242 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001243 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001244 }
1245
arangelov38a6fce2019-12-02 18:21:22 +00001246 @VisibleForTesting
1247 protected ResolverListAdapter createResolverListAdapter(Context context,
1248 List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList,
1249 boolean filterLastUsed, boolean useLayoutForBrowsables, UserHandle userHandle) {
Paul McLean07425c82019-10-18 12:00:11 -06001250 Intent startIntent = getIntent();
1251 boolean isAudioCaptureDevice =
1252 startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false);
arangelovb0802dc2019-10-18 18:03:44 +01001253 return new ResolverListAdapter(context, payloadIntents, initialIntents, rList,
arangelov38a6fce2019-12-02 18:21:22 +00001254 filterLastUsed, createListController(userHandle), useLayoutForBrowsables, this,
Paul McLean07425c82019-10-18 12:00:11 -06001255 isAudioCaptureDevice);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001256 }
1257
1258 @VisibleForTesting
arangelov38a6fce2019-12-02 18:21:22 +00001259 protected ResolverListController createListController(UserHandle userHandle) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001260 return new ResolverListController(
1261 this,
1262 mPm,
1263 getTargetIntent(),
1264 getReferrerPackageName(),
arangelov38a6fce2019-12-02 18:21:22 +00001265 mLaunchedFromUid,
1266 userHandle);
Adam Powell88831a22014-11-20 18:17:00 -08001267 }
1268
Adam Powell39e94eb2015-09-08 17:01:49 -07001269 /**
arangelovb0802dc2019-10-18 18:03:44 +01001270 * Sets up the content view.
arangelovbb572332019-11-13 12:50:08 +00001271 * @return <code>true</code> if the activity is finishing and creation should halt.
Adam Powell39e94eb2015-09-08 17:01:49 -07001272 */
arangelovbb572332019-11-13 12:50:08 +00001273 private boolean configureContentView() {
arangelova3912cf2019-12-13 14:34:45 +00001274 if (mMultiProfilePagerAdapter.getActiveListAdapter() == null) {
1275 throw new IllegalStateException("mMultiProfilePagerAdapter.getCurrentListAdapter() "
1276 + "cannot be null.");
arangelovb0802dc2019-10-18 18:03:44 +01001277 }
arangelov7981b122020-01-16 10:58:27 +00001278 boolean rebuildCompleted = mMultiProfilePagerAdapter.rebuildActiveTab(true);
1279
1280 // We partially rebuild the inactive adapter to determine if we should auto launch
1281 mMultiProfilePagerAdapter.rebuildInactiveTab(false);
1282
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001283 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001284 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001285 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001286 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001287 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001288 setContentView(mLayoutId);
arangelov38a6fce2019-12-02 18:21:22 +00001289 mMultiProfilePagerAdapter.setupViewPager(findViewById(R.id.profile_pager));
arangelovbb572332019-11-13 12:50:08 +00001290 return postRebuildList(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001291 }
Adam Powell7d758002015-05-06 17:49:36 -07001292
arangelovb0802dc2019-10-18 18:03:44 +01001293 /**
arangelovbb572332019-11-13 12:50:08 +00001294 * Finishing procedures to be performed after the list has been rebuilt.
1295 * </p>Subclasses must call postRebuildListInternal at the end of postRebuildList.
1296 * @param rebuildCompleted
1297 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001298 */
arangelovbb572332019-11-13 12:50:08 +00001299 protected boolean postRebuildList(boolean rebuildCompleted) {
1300 return postRebuildListInternal(rebuildCompleted);
arangelovb0802dc2019-10-18 18:03:44 +01001301 }
1302
1303 /**
arangelovbb572332019-11-13 12:50:08 +00001304 * Finishing procedures to be performed after the list has been rebuilt.
1305 * @param rebuildCompleted
1306 * @return <code>true</code> if the activity is finishing and creation should halt.
arangelovb0802dc2019-10-18 18:03:44 +01001307 */
arangelovbb572332019-11-13 12:50:08 +00001308 final boolean postRebuildListInternal(boolean rebuildCompleted) {
arangelova3912cf2019-12-13 14:34:45 +00001309 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001310
1311 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1312 // we're already done, we can check if we should auto-launch immediately.
arangelov7981b122020-01-16 10:58:27 +00001313 if (rebuildCompleted && maybeAutolaunchActivity()) {
1314 return true;
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001315 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001316
arangelovcf268642020-01-15 15:09:51 +00001317 setupViewVisibilities();
1318
1319 if (hasWorkProfile() && ENABLE_TABBED_VIEW) {
1320 setupProfileTabs();
1321 }
1322
arangelov38a6fce2019-12-02 18:21:22 +00001323 return false;
1324 }
1325
arangelov7981b122020-01-16 10:58:27 +00001326 private int isPermissionGranted(String permission, int uid) {
1327 return ActivityManager.checkComponentPermission(permission, uid,
1328 /* owningUid= */-1, /* exported= */ true);
1329 }
1330
1331 /**
1332 * @return {@code true} if a resolved target is autolaunched, otherwise {@code false}
1333 */
1334 private boolean maybeAutolaunchActivity() {
1335 int numberOfProfiles = mMultiProfilePagerAdapter.getItemCount();
1336 if (numberOfProfiles == 1 && maybeAutolaunchIfSingleTarget()) {
1337 return true;
1338 } else if (numberOfProfiles == 2 && maybeAutolaunchIfCrossProfileSupported()) {
1339 // note that autolaunching when we have 2 profiles, 1 resolved target on the active
1340 // tab and 0 resolved targets on the inactive tab, is already handled before launching
1341 // ResolverActivity
1342 return true;
1343 }
1344 return false;
1345 }
1346
1347 private boolean maybeAutolaunchIfSingleTarget() {
1348 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1349 if (count != 1) {
1350 return false;
1351 }
1352
1353 // Only one target, so we're a candidate to auto-launch!
1354 final TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter()
1355 .targetInfoForPosition(0, false);
1356 if (shouldAutoLaunchSingleChoice(target)) {
1357 safelyStartActivity(target);
1358 finish();
1359 return true;
1360 }
1361 return false;
1362 }
1363
1364 /**
1365 * When we have a personal and a work profile, we auto launch in the following scenario:
1366 * - There is 1 resolved target on each profile
1367 * - That target is the same app on both profiles
1368 * - The target app has permission to communicate cross profiles
1369 * - The target app has declared it supports cross-profile communication via manifest metadata
1370 */
1371 private boolean maybeAutolaunchIfCrossProfileSupported() {
1372 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1373 if (count != 1) {
1374 return false;
1375 }
1376 ResolverListAdapter inactiveListAdapter =
1377 mMultiProfilePagerAdapter.getInactiveListAdapter();
1378 if (inactiveListAdapter.getUnfilteredCount() != 1) {
1379 return false;
1380 }
1381 TargetInfo activeProfileTarget = mMultiProfilePagerAdapter.getActiveListAdapter()
1382 .targetInfoForPosition(0, false);
1383 TargetInfo inactiveProfileTarget = inactiveListAdapter.targetInfoForPosition(0, false);
1384 if (!Objects.equals(activeProfileTarget.getResolvedComponentName(),
1385 inactiveProfileTarget.getResolvedComponentName())) {
1386 return false;
1387 }
1388 if (!shouldAutoLaunchSingleChoice(activeProfileTarget)) {
1389 return false;
1390 }
1391 String packageName = activeProfileTarget.getResolvedComponentName().getPackageName();
1392 if (!canAppInteractCrossProfiles(packageName)) {
1393 return false;
1394 }
1395
1396 safelyStartActivity(activeProfileTarget);
1397 finish();
1398 return true;
1399 }
1400
1401 /**
1402 * Returns whether the package has the necessary permissions to interact across profiles on
1403 * behalf of a given user.
1404 *
1405 * <p>This means meeting the following condition:
1406 * <ul>
1407 * <li>The app's {@link ApplicationInfo#crossProfile} flag must be true, and at least
1408 * one of the following conditions must be fulfilled</li>
1409 * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS_FULL} granted.</li>
1410 * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS} granted.</li>
1411 * <li>{@code Manifest.permission.INTERACT_ACROSS_PROFILES} granted, or the corresponding
1412 * AppOps {@code android:interact_across_profiles} is set to "allow".</li>
1413 * </ul>
1414 *
1415 */
1416 private boolean canAppInteractCrossProfiles(String packageName) {
1417 ApplicationInfo applicationInfo;
1418 try {
1419 applicationInfo = getPackageManager().getApplicationInfo(packageName, 0);
1420 } catch (NameNotFoundException e) {
1421 Log.e(TAG, "Package " + packageName + " does not exist on current user.");
1422 return false;
1423 }
1424 if (!applicationInfo.crossProfile) {
1425 return false;
1426 }
1427
1428 int packageUid = applicationInfo.uid;
1429
1430 if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1431 packageUid) == PackageManager.PERMISSION_GRANTED) {
1432 return true;
1433 }
1434 if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS, packageUid)
1435 == PackageManager.PERMISSION_GRANTED) {
1436 return true;
1437 }
1438 if (PermissionChecker.checkPermissionForPreflight(this, INTERACT_ACROSS_PROFILES,
1439 PID_UNKNOWN, packageUid, packageName) == PackageManager.PERMISSION_GRANTED) {
1440 return true;
1441 }
1442 return false;
1443 }
1444
1445 private boolean isAutolaunching() {
1446 return !mRegistered && isFinishing();
1447 }
1448
arangelovcf268642020-01-15 15:09:51 +00001449 private void setupProfileTabs() {
1450 TabHost tabHost = findViewById(R.id.profile_tabhost);
1451 tabHost.setup();
1452 ViewPager viewPager = findViewById(R.id.profile_pager);
1453 TabHost.TabSpec tabSpec = tabHost.newTabSpec(TAB_TAG_PERSONAL)
1454 .setContent(R.id.profile_pager)
1455 .setIndicator(getString(R.string.resolver_personal_tab));
1456 tabHost.addTab(tabSpec);
1457
1458 tabSpec = tabHost.newTabSpec(TAB_TAG_WORK)
1459 .setContent(R.id.profile_pager)
1460 .setIndicator(getString(R.string.resolver_work_tab));
1461 tabHost.addTab(tabSpec);
1462
1463 TabWidget tabWidget = tabHost.getTabWidget();
1464 tabWidget.setVisibility(View.VISIBLE);
1465 resetTabsHeaderStyle(tabWidget);
1466 updateActiveTabStyle(tabHost);
1467
1468 tabHost.setOnTabChangedListener(tabId -> {
1469 resetTabsHeaderStyle(tabWidget);
1470 updateActiveTabStyle(tabHost);
1471 if (TAB_TAG_PERSONAL.equals(tabId)) {
1472 viewPager.setCurrentItem(0);
1473 } else {
1474 viewPager.setCurrentItem(1);
1475 }
1476 setupViewVisibilities();
1477 });
1478
1479 viewPager.setVisibility(View.VISIBLE);
1480 tabHost.setCurrentTab(mMultiProfilePagerAdapter.getCurrentPage());
1481 mMultiProfilePagerAdapter.setOnProfileSelectedListener(tabHost::setCurrentTab);
1482 }
1483
1484 private void resetTabsHeaderStyle(TabWidget tabWidget) {
1485 for (int i = 0; i < tabWidget.getChildCount(); i++) {
1486 TextView title = tabWidget.getChildAt(i).findViewById(android.R.id.title);
1487 title.setTextColor(getColor(R.color.resolver_tabs_inactive_color));
1488 title.setAllCaps(false);
1489 }
1490 }
1491
1492 private void updateActiveTabStyle(TabHost tabHost) {
1493 TextView title = tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
1494 .findViewById(android.R.id.title);
1495 title.setTextColor(getColor(R.color.resolver_tabs_active_color));
1496 }
1497
1498 private void setupViewVisibilities() {
1499 int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount();
1500 boolean shouldShowEmptyState = count == 0
1501 && mMultiProfilePagerAdapter.getActiveListAdapter().getPlaceholderCount() == 0;
1502 //TODO(arangelov): Handle empty state
1503 if (!shouldShowEmptyState) {
1504 addUseDifferentAppLabelIfNecessary(mMultiProfilePagerAdapter.getActiveListAdapter());
Adam Powell7d758002015-05-06 17:49:36 -07001505 }
1506 }
1507
Zhen Zhangbde7b462019-11-11 11:49:33 -08001508 /**
arangelovcf268642020-01-15 15:09:51 +00001509 * Add a label to signify that the user can pick a different app.
Zhen Zhangbde7b462019-11-11 11:49:33 -08001510 * @param adapter The adapter used to provide data to item views.
Zhen Zhangbde7b462019-11-11 11:49:33 -08001511 */
arangelovcf268642020-01-15 15:09:51 +00001512 public void addUseDifferentAppLabelIfNecessary(ResolverListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001513 final boolean useHeader = adapter.hasFilteredItem();
arangelovcf268642020-01-15 15:09:51 +00001514 if (useHeader) {
1515 FrameLayout stub = findViewById(R.id.stub);
1516 stub.setVisibility(View.VISIBLE);
1517 TextView textView = (TextView) LayoutInflater.from(this).inflate(
1518 R.layout.resolver_different_item_header, null, false);
1519 if (ENABLE_TABBED_VIEW) {
1520 textView.setGravity(Gravity.CENTER);
1521 }
1522 stub.addView(textView);
1523 }
1524 }
1525
1526 private void setupAdapterListView(ListView listView, ItemClickListener listener) {
arangelov38a6fce2019-12-02 18:21:22 +00001527 listView.setOnItemClickListener(listener);
1528 listView.setOnItemLongClickListener(listener);
Adam Powell7d758002015-05-06 17:49:36 -07001529
shafik69df96322018-12-18 15:41:19 +00001530 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001531 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1532 }
Adam Powell24428412015-04-01 17:19:56 -07001533 }
1534
Matt Pietal26038402019-01-08 07:29:34 -05001535 /**
1536 * Configure the area above the app selection list (title, content preview, etc).
1537 */
1538 public void setHeader() {
arangelova3912cf2019-12-13 14:34:45 +00001539 if (mMultiProfilePagerAdapter.getActiveListAdapter().getCount() == 0
1540 && mMultiProfilePagerAdapter.getActiveListAdapter().getPlaceholderCount() == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001541 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001542 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001543 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001544 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001545 }
1546
1547 CharSequence title = mTitle != null
1548 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001549 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001550
1551 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001552 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001553 if (titleView != null) {
1554 titleView.setText(title);
1555 }
1556 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001557 }
1558
Alan Viverette51efddb2017-04-05 10:00:01 -04001559 final ImageView iconView = findViewById(R.id.icon);
arangelovb0802dc2019-10-18 18:03:44 +01001560 if (iconView != null) {
arangelova3912cf2019-12-13 14:34:45 +00001561 mMultiProfilePagerAdapter.getActiveListAdapter().loadFilteredItemIconTaskAsync(iconView);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001562 }
1563 }
1564
arangelov38a6fce2019-12-02 18:21:22 +00001565 protected void resetButtonBar() {
shafik69df96322018-12-18 15:41:19 +00001566 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1567 return;
1568 }
1569 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1570 if (buttonLayout != null) {
1571 buttonLayout.setVisibility(View.VISIBLE);
Matt Pietal800136a2019-05-08 07:46:39 -04001572
Matt Pietald5071202019-10-10 11:21:28 -04001573 if (!useLayoutWithDefault()) {
1574 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1575 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
1576 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1577 R.dimen.resolver_button_bar_spacing) + inset);
1578 }
shafik69df96322018-12-18 15:41:19 +00001579 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
shafik69df96322018-12-18 15:41:19 +00001580 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1581
Matt Pietal0d6834a2019-06-27 13:27:52 -04001582 resetAlwaysOrOnceButtonBar();
shafik69df96322018-12-18 15:41:19 +00001583 } else {
1584 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1585 }
1586 }
1587
shafik69df96322018-12-18 15:41:19 +00001588 private void resetAlwaysOrOnceButtonBar() {
arangelova3912cf2019-12-13 14:34:45 +00001589 int filteredPosition = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001590 .getFilteredPosition();
1591 if (useLayoutWithDefault() && filteredPosition != ListView.INVALID_POSITION) {
1592 setAlwaysButtonEnabled(true, filteredPosition, false);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001593 mOnceButton.setEnabled(true);
Sergey Troshin40e979e2019-12-30 17:29:50 +01001594 // Focus the button if we already have the default option
1595 mOnceButton.requestFocus();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001596 return;
1597 }
1598
1599 // When the items load in, if an item was already selected, enable the buttons
arangelovcf268642020-01-15 15:09:51 +00001600 ListView currentAdapterView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelov38a6fce2019-12-02 18:21:22 +00001601 if (currentAdapterView != null
1602 && currentAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1603 setAlwaysButtonEnabled(true, currentAdapterView.getCheckedItemPosition(), true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001604 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001605 }
1606 }
1607
arangelovb0802dc2019-10-18 18:03:44 +01001608 @Override // ResolverListCommunicator
1609 public boolean useLayoutWithDefault() {
arangelovcf268642020-01-15 15:09:51 +00001610 // We only use the default app layout when the profile of the active user has a
1611 // filtered item. We always show the same default app even in the inactive user profile.
1612 boolean currentUserAdapterHasFilteredItem;
1613 if (mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier()
1614 == UserHandle.myUserId()) {
1615 currentUserAdapterHasFilteredItem =
1616 mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem();
1617 } else {
1618 currentUserAdapterHasFilteredItem =
1619 mMultiProfilePagerAdapter.getInactiveListAdapter().hasFilteredItem();
1620 }
1621 return mSupportsAlwaysUseOption && currentUserAdapterHasFilteredItem;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001622 }
1623
Adam Powellc412be62015-06-24 13:54:10 -07001624 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001625 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1626 * called and we are launched in a new task.
1627 */
1628 protected void setRetainInOnStop(boolean retainInOnStop) {
1629 mRetainInOnStop = retainInOnStop;
1630 }
1631
1632 /**
Adam Powellc412be62015-06-24 13:54:10 -07001633 * Check a simple match for the component of two ResolveInfos.
1634 */
arangelovb0802dc2019-10-18 18:03:44 +01001635 @Override // ResolverListCommunicator
1636 public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
Adam Powellc412be62015-06-24 13:54:10 -07001637 return lhs == null ? rhs == null
1638 : lhs.activityInfo == null ? rhs.activityInfo == null
1639 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1640 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1641 }
1642
arangelovb0802dc2019-10-18 18:03:44 +01001643 @Override // ResolverListCommunicator
arangelov7981b122020-01-16 10:58:27 +00001644 public void onHandlePackagesChanged(ResolverListAdapter listAdapter) {
1645 if (listAdapter == mMultiProfilePagerAdapter.getActiveListAdapter()) {
1646 boolean listRebuilt = mMultiProfilePagerAdapter.rebuildActiveTab(true);
1647 if (listRebuilt) {
1648 ResolverListAdapter activeListAdapter =
1649 mMultiProfilePagerAdapter.getActiveListAdapter();
1650 activeListAdapter.notifyDataSetChanged();
1651 if (activeListAdapter.getCount() == 0) {
1652 // We no longer have any items... just finish the activity.
1653 finish();
1654 }
1655 }
1656 } else {
1657 mMultiProfilePagerAdapter.clearInactiveProfileCache();
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001658 }
Zhen Zhang55d66aa2019-10-14 17:11:19 -07001659 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001660
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001661 @VisibleForTesting
1662 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07001663 public final ComponentName name;
1664 private final List<Intent> mIntents = new ArrayList<>();
1665 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001666 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001667
1668 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1669 this.name = name;
1670 add(intent, info);
1671 }
1672
1673 public void add(Intent intent, ResolveInfo info) {
1674 mIntents.add(intent);
1675 mResolveInfos.add(info);
1676 }
1677
1678 public int getCount() {
1679 return mIntents.size();
1680 }
1681
1682 public Intent getIntentAt(int index) {
1683 return index >= 0 ? mIntents.get(index) : null;
1684 }
1685
1686 public ResolveInfo getResolveInfoAt(int index) {
1687 return index >= 0 ? mResolveInfos.get(index) : null;
1688 }
1689
1690 public int findIntent(Intent intent) {
1691 for (int i = 0, N = mIntents.size(); i < N; i++) {
1692 if (intent.equals(mIntents.get(i))) {
1693 return i;
1694 }
1695 }
1696 return -1;
1697 }
1698
1699 public int findResolveInfo(ResolveInfo info) {
1700 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1701 if (info.equals(mResolveInfos.get(i))) {
1702 return i;
1703 }
1704 }
1705 return -1;
1706 }
Alison Cichowlas1fd47152019-11-14 19:50:55 -05001707
1708 public boolean isPinned() {
1709 return mPinned;
1710 }
1711
1712 public void setPinned(boolean pinned) {
1713 mPinned = pinned;
1714 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001715 }
1716
Adam Powell7d758002015-05-06 17:49:36 -07001717 class ItemClickListener implements AdapterView.OnItemClickListener,
1718 AdapterView.OnItemLongClickListener {
1719 @Override
1720 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1721 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1722 if (listView != null) {
1723 position -= listView.getHeaderViewsCount();
1724 }
1725 if (position < 0) {
1726 // Header views don't count.
1727 return;
1728 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001729 // If we're still loading, we can't yet enable the buttons.
arangelova3912cf2019-12-13 14:34:45 +00001730 if (mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001731 .resolveInfoForPosition(position, true) == null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001732 return;
1733 }
arangelov38a6fce2019-12-02 18:21:22 +00001734 ListView currentAdapterView =
arangelovcf268642020-01-15 15:09:51 +00001735 (ListView) mMultiProfilePagerAdapter.getActiveAdapterView();
arangelov38a6fce2019-12-02 18:21:22 +00001736 final int checkedPos = currentAdapterView.getCheckedItemPosition();
Adam Powell7d758002015-05-06 17:49:36 -07001737 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001738 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07001739 && (!hasValidSelection || mLastSelected != checkedPos)
Matt Pietal0d6834a2019-06-27 13:27:52 -04001740 && mAlwaysButton != null) {
1741 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Adam Powell7d758002015-05-06 17:49:36 -07001742 mOnceButton.setEnabled(hasValidSelection);
1743 if (hasValidSelection) {
arangelov38a6fce2019-12-02 18:21:22 +00001744 currentAdapterView.smoothScrollToPosition(checkedPos);
Sergey Troshin40e979e2019-12-30 17:29:50 +01001745 mOnceButton.requestFocus();
Adam Powell7d758002015-05-06 17:49:36 -07001746 }
1747 mLastSelected = checkedPos;
1748 } else {
1749 startSelected(position, false, true);
1750 }
1751 }
Adam Powell2d809622012-03-22 15:24:43 -07001752
1753 @Override
1754 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001755 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1756 if (listView != null) {
1757 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001758 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001759 if (position < 0) {
1760 // Header views don't count.
1761 return false;
1762 }
arangelova3912cf2019-12-13 14:34:45 +00001763 ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001764 .resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001765 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001766 return true;
1767 }
1768
1769 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001770
Dianne Hackbornec452d92014-11-11 17:16:56 -08001771 static final boolean isSpecificUriMatch(int match) {
1772 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1773 return match >= IntentFilter.MATCH_CATEGORY_HOST
1774 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1775 }
1776
Adam Powell4c470d62015-06-19 17:46:17 -07001777 static class PickTargetOptionRequest extends PickOptionRequest {
1778 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1779 @Nullable Bundle extras) {
1780 super(prompt, options, extras);
1781 }
1782
1783 @Override
1784 public void onCancel() {
1785 super.onCancel();
1786 final ResolverActivity ra = (ResolverActivity) getActivity();
1787 if (ra != null) {
1788 ra.mPickOptionRequest = null;
1789 ra.finish();
1790 }
1791 }
1792
1793 @Override
1794 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1795 super.onPickOptionResult(finished, selections, result);
1796 if (selections.length != 1) {
1797 // TODO In a better world we would filter the UI presented here and let the
1798 // user refine. Maybe later.
1799 return;
1800 }
1801
1802 final ResolverActivity ra = (ResolverActivity) getActivity();
1803 if (ra != null) {
arangelova3912cf2019-12-13 14:34:45 +00001804 final TargetInfo ti = ra.mMultiProfilePagerAdapter.getActiveListAdapter()
arangelov38a6fce2019-12-02 18:21:22 +00001805 .getItem(selections[0].getIndex());
Adam Powell4c470d62015-06-19 17:46:17 -07001806 if (ra.onTargetSelected(ti, false)) {
1807 ra.mPickOptionRequest = null;
1808 ra.finish();
1809 }
1810 }
1811 }
1812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813}