The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 19 | import static android.Manifest.permission.INTERACT_ACROSS_PROFILES; |
Hyunyoung Song | 16b7af4 | 2018-10-05 14:00:42 -0700 | [diff] [blame] | 20 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 21 | import static android.content.PermissionChecker.PID_UNKNOWN; |
Hyunyoung Song | 16b7af4 | 2018-10-05 14:00:42 -0700 | [diff] [blame] | 22 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 23 | import static com.android.internal.app.AbstractMultiProfilePagerAdapter.PROFILE_PERSONAL; |
| 24 | import static com.android.internal.app.AbstractMultiProfilePagerAdapter.PROFILE_WORK; |
| 25 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 26 | import android.annotation.Nullable; |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 27 | import android.annotation.StringRes; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 28 | import android.annotation.UiThread; |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 29 | import android.app.Activity; |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 30 | import android.app.ActivityManager; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 31 | import android.app.ActivityTaskManager; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 32 | import android.app.ActivityThread; |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 33 | import android.app.VoiceInteractor.PickOptionRequest; |
| 34 | import android.app.VoiceInteractor.PickOptionRequest.Option; |
| 35 | import android.app.VoiceInteractor.Prompt; |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 36 | import android.app.admin.DevicePolicyEventLogger; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 37 | import android.compat.annotation.UnsupportedAppUsage; |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 38 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.content.ComponentName; |
| 40 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.Intent; |
| 42 | import android.content.IntentFilter; |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 43 | import android.content.PermissionChecker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.content.pm.ActivityInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 45 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.content.pm.PackageManager; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 47 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.content.pm.ResolveInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 49 | import android.content.pm.UserInfo; |
Jiongxuan Gao | 3365b16 | 2016-11-24 20:20:27 +0900 | [diff] [blame] | 50 | import android.content.res.Configuration; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 51 | import android.content.res.Resources; |
arangelov | 4b8ad34 | 2020-03-17 13:56:00 +0000 | [diff] [blame] | 52 | import android.content.res.TypedArray; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 53 | import android.graphics.Insets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.net.Uri; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 55 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.os.Bundle; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 57 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.os.PatternMatcher; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 59 | import android.os.RemoteException; |
Jeff Sharkey | 37355a9 | 2016-02-05 16:19:10 -0700 | [diff] [blame] | 60 | import android.os.StrictMode; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 61 | import android.os.UserHandle; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 62 | import android.os.UserManager; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 63 | import android.provider.MediaStore; |
| 64 | import android.provider.Settings; |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 65 | import android.stats.devicepolicy.DevicePolicyEnums; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 66 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.util.Log; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 68 | import android.util.Slog; |
arangelov | 4b8ad34 | 2020-03-17 13:56:00 +0000 | [diff] [blame] | 69 | import android.util.TypedValue; |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 70 | import android.view.Gravity; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 71 | import android.view.LayoutInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.view.View; |
| 73 | import android.view.ViewGroup; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 74 | import android.view.ViewGroup.LayoutParams; |
| 75 | import android.view.WindowInsets; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 76 | import android.widget.AbsListView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 77 | import android.widget.AdapterView; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 78 | import android.widget.Button; |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 79 | import android.widget.FrameLayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.widget.ImageView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 81 | import android.widget.ListView; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 82 | import android.widget.Space; |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 83 | import android.widget.TabHost; |
| 84 | import android.widget.TabWidget; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.widget.TextView; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 86 | import android.widget.Toast; |
Hyunyoung Song | 16b7af4 | 2018-10-05 14:00:42 -0700 | [diff] [blame] | 87 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 88 | import com.android.internal.R; |
| 89 | import com.android.internal.annotations.VisibleForTesting; |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 90 | import com.android.internal.app.AbstractMultiProfilePagerAdapter.Profile; |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 91 | import com.android.internal.app.chooser.ChooserTargetInfo; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 92 | import com.android.internal.app.chooser.DisplayResolveInfo; |
| 93 | import com.android.internal.app.chooser.TargetInfo; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 94 | import com.android.internal.content.PackageMonitor; |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 95 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 96 | import com.android.internal.logging.nano.MetricsProto; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 97 | import com.android.internal.widget.ResolverDrawerLayout; |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 98 | import com.android.internal.widget.ViewPager; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import java.util.ArrayList; |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 101 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import java.util.Iterator; |
| 103 | import java.util.List; |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 104 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import java.util.Set; |
| 106 | |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | /** |
| 109 | * This activity is displayed when the system attempts to start an Intent for |
| 110 | * which there is more than one matching activity, allowing the user to decide |
| 111 | * which to go to. It is not normally used directly by application developers. |
| 112 | */ |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 113 | @UiThread |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 114 | public class ResolverActivity extends Activity implements |
| 115 | ResolverListAdapter.ResolverListCommunicator { |
Alison Cichowlas | 4691ed4 | 2018-11-13 15:59:55 -0500 | [diff] [blame] | 116 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 117 | @UnsupportedAppUsage |
Artur Satayev | 751e551 | 2019-11-15 19:12:49 +0000 | [diff] [blame] | 118 | public ResolverActivity() { |
| 119 | } |
| 120 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 121 | private boolean mSafeForwardingMode; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 122 | private Button mAlwaysButton; |
| 123 | private Button mOnceButton; |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 124 | protected View mProfileView; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 125 | private int mLastSelected = AbsListView.INVALID_POSITION; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 126 | private boolean mResolvingHome = false; |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 127 | private int mProfileSwitchMessageId = -1; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 128 | private int mLayoutId; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 129 | @VisibleForTesting |
| 130 | protected final ArrayList<Intent> mIntents = new ArrayList<>(); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 131 | private PickTargetOptionRequest mPickOptionRequest; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 132 | private String mReferrerPackage; |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 133 | private CharSequence mTitle; |
| 134 | private int mDefaultTitleResId; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 135 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 136 | // Whether or not this activity supports choosing a default handler for the intent. |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 137 | @VisibleForTesting |
| 138 | protected boolean mSupportsAlwaysUseOption; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 139 | protected ResolverDrawerLayout mResolverDrawerLayout; |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 140 | @UnsupportedAppUsage |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 141 | protected PackageManager mPm; |
| 142 | protected int mLaunchedFromUid; |
| 143 | |
| 144 | private static final String TAG = "ResolverActivity"; |
| 145 | private static final boolean DEBUG = false; |
arangelov | 9d89414 | 2020-04-21 21:08:21 +0100 | [diff] [blame] | 146 | private static final String LAST_SHOWN_TAB_KEY = "last_shown_tab_key"; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 147 | |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 148 | private boolean mRegistered; |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 149 | |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 150 | protected Insets mSystemWindowInsets = null; |
| 151 | private Space mFooterSpacer = null; |
| 152 | |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 153 | /** See {@link #setRetainInOnStop}. */ |
| 154 | private boolean mRetainInOnStop; |
| 155 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 156 | private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args"; |
| 157 | private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key"; |
| 158 | private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state"; |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 159 | protected static final String METRICS_CATEGORY_RESOLVER = "intent_resolver"; |
| 160 | protected static final String METRICS_CATEGORY_CHOOSER = "intent_chooser"; |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 161 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 162 | @VisibleForTesting |
arangelov | 85974ed | 2020-03-06 15:48:19 +0000 | [diff] [blame] | 163 | public static boolean ENABLE_TABBED_VIEW = true; |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 164 | private static final String TAB_TAG_PERSONAL = "personal"; |
| 165 | private static final String TAB_TAG_WORK = "work"; |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 166 | |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 167 | private PackageMonitor mPersonalPackageMonitor; |
arangelov | bdb4005 | 2020-01-09 18:01:58 +0000 | [diff] [blame] | 168 | private PackageMonitor mWorkPackageMonitor; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 169 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 170 | @VisibleForTesting |
| 171 | protected AbstractMultiProfilePagerAdapter mMultiProfilePagerAdapter; |
| 172 | |
Paul McLean | 07425c8 | 2019-10-18 12:00:11 -0600 | [diff] [blame] | 173 | // Intent extra for connected audio devices |
| 174 | public static final String EXTRA_IS_AUDIO_CAPTURE_DEVICE = "is_audio_capture_device"; |
| 175 | |
arangelov | eb7793a | 2020-05-11 16:57:33 +0100 | [diff] [blame] | 176 | /** |
| 177 | * Integer extra to indicate which profile should be automatically selected. |
| 178 | * <p>Can only be used if there is a work profile. |
| 179 | * <p>Possible values can be either {@link #PROFILE_PERSONAL} or {@link #PROFILE_WORK}. |
| 180 | */ |
| 181 | static final String EXTRA_SELECTED_PROFILE = |
| 182 | "com.android.internal.app.ResolverActivity.EXTRA_SELECTED_PROFILE"; |
| 183 | |
arangelov | 738034b | 2020-06-02 18:18:39 +0100 | [diff] [blame] | 184 | /** |
| 185 | * {@link UserHandle} extra to indicate the user of the user that the starting intent |
| 186 | * originated from. |
| 187 | * <p>This is not necessarily the same as {@link #getUserId()} or {@link UserHandle#myUserId()}, |
| 188 | * as there are edge cases when the intent resolver is launched in the other profile. |
| 189 | * For example, when we have 0 resolved apps in current profile and multiple resolved |
| 190 | * apps in the other profile, opening a link from the current profile launches the intent |
| 191 | * resolver in the other one. b/148536209 for more info. |
| 192 | */ |
| 193 | static final String EXTRA_CALLING_USER = |
| 194 | "com.android.internal.app.ResolverActivity.EXTRA_CALLING_USER"; |
| 195 | |
arangelov | eb7793a | 2020-05-11 16:57:33 +0100 | [diff] [blame] | 196 | static final int PROFILE_PERSONAL = AbstractMultiProfilePagerAdapter.PROFILE_PERSONAL; |
| 197 | static final int PROFILE_WORK = AbstractMultiProfilePagerAdapter.PROFILE_WORK; |
| 198 | |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 199 | private BroadcastReceiver mWorkProfileStateReceiver; |
arangelov | 4e2951b | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 200 | private UserHandle mHeaderCreatorUser; |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 201 | |
arangelov | 706316d | 2020-05-06 14:25:06 +0100 | [diff] [blame] | 202 | private UserHandle mWorkProfileUserHandle; |
| 203 | |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Get the string resource to be used as a label for the link to the resolver activity for an |
| 206 | * action. |
| 207 | * |
| 208 | * @param action The action to resolve |
| 209 | * |
| 210 | * @return The string resource to be used as a label |
| 211 | */ |
| 212 | public static @StringRes int getLabelRes(String action) { |
| 213 | return ActionTitle.forAction(action).labelRes; |
| 214 | } |
| 215 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 216 | private enum ActionTitle { |
| 217 | VIEW(Intent.ACTION_VIEW, |
| 218 | com.android.internal.R.string.whichViewApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 219 | com.android.internal.R.string.whichViewApplicationNamed, |
| 220 | com.android.internal.R.string.whichViewApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 221 | EDIT(Intent.ACTION_EDIT, |
| 222 | com.android.internal.R.string.whichEditApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 223 | com.android.internal.R.string.whichEditApplicationNamed, |
| 224 | com.android.internal.R.string.whichEditApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 225 | SEND(Intent.ACTION_SEND, |
| 226 | com.android.internal.R.string.whichSendApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 227 | com.android.internal.R.string.whichSendApplicationNamed, |
| 228 | com.android.internal.R.string.whichSendApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 229 | SENDTO(Intent.ACTION_SENDTO, |
Adam Powell | 13ea8f4 | 2016-03-18 09:39:41 -0700 | [diff] [blame] | 230 | com.android.internal.R.string.whichSendToApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 231 | com.android.internal.R.string.whichSendToApplicationNamed, |
| 232 | com.android.internal.R.string.whichSendToApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 233 | SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE, |
| 234 | com.android.internal.R.string.whichSendApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 235 | com.android.internal.R.string.whichSendApplicationNamed, |
| 236 | com.android.internal.R.string.whichSendApplicationLabel), |
| 237 | CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE, |
| 238 | com.android.internal.R.string.whichImageCaptureApplication, |
| 239 | com.android.internal.R.string.whichImageCaptureApplicationNamed, |
| 240 | com.android.internal.R.string.whichImageCaptureApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 241 | DEFAULT(null, |
| 242 | com.android.internal.R.string.whichApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 243 | com.android.internal.R.string.whichApplicationNamed, |
| 244 | com.android.internal.R.string.whichApplicationLabel), |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 245 | HOME(Intent.ACTION_MAIN, |
| 246 | com.android.internal.R.string.whichHomeApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 247 | com.android.internal.R.string.whichHomeApplicationNamed, |
| 248 | com.android.internal.R.string.whichHomeApplicationLabel); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 249 | |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 250 | // titles for layout that deals with http(s) intents |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 251 | public static final int BROWSABLE_TITLE_RES = |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 252 | com.android.internal.R.string.whichOpenLinksWith; |
| 253 | public static final int BROWSABLE_HOST_TITLE_RES = |
| 254 | com.android.internal.R.string.whichOpenHostLinksWith; |
| 255 | public static final int BROWSABLE_HOST_APP_TITLE_RES = |
| 256 | com.android.internal.R.string.whichOpenHostLinksWithApp; |
| 257 | public static final int BROWSABLE_APP_TITLE_RES = |
| 258 | com.android.internal.R.string.whichOpenLinksWithApp; |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 259 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 260 | public final String action; |
| 261 | public final int titleRes; |
| 262 | public final int namedTitleRes; |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 263 | public final @StringRes int labelRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 264 | |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 265 | ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 266 | this.action = action; |
| 267 | this.titleRes = titleRes; |
| 268 | this.namedTitleRes = namedTitleRes; |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 269 | this.labelRes = labelRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | public static ActionTitle forAction(String action) { |
| 273 | for (ActionTitle title : values()) { |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 274 | if (title != HOME && action != null && action.equals(title.action)) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 275 | return title; |
| 276 | } |
| 277 | } |
| 278 | return DEFAULT; |
| 279 | } |
| 280 | } |
| 281 | |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 282 | protected PackageMonitor createPackageMonitor(ResolverListAdapter listAdapter) { |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 283 | return new PackageMonitor() { |
| 284 | @Override |
| 285 | public void onSomePackagesChanged() { |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 286 | listAdapter.handlePackagesChanged(); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 287 | updateProfileViewButton(); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
| 292 | // We care about all package changes, not just the whole package itself which is |
| 293 | // default behavior. |
| 294 | return true; |
| 295 | } |
| 296 | }; |
| 297 | } |
| 298 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 299 | private Intent makeMyIntent() { |
| 300 | Intent intent = new Intent(getIntent()); |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 301 | intent.setComponent(null); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 302 | // The resolver activity is set to be hidden from recent tasks. |
| 303 | // we don't want this attribute to be propagated to the next activity |
| 304 | // being launched. Note that if the original Intent also had this |
| 305 | // flag set, we are now losing it. That should be a very rare case |
| 306 | // and we can live with this. |
| 307 | intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 308 | return intent; |
| 309 | } |
| 310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | @Override |
| 312 | protected void onCreate(Bundle savedInstanceState) { |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 313 | // Use a specialized prompt when we're handling the 'Home' app startActivity() |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 314 | final Intent intent = makeMyIntent(); |
| 315 | final Set<String> categories = intent.getCategories(); |
| 316 | if (Intent.ACTION_MAIN.equals(intent.getAction()) |
| 317 | && categories != null |
| 318 | && categories.size() == 1 |
| 319 | && categories.contains(Intent.CATEGORY_HOME)) { |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 320 | // Note: this field is not set to true in the compatibility version. |
| 321 | mResolvingHome = true; |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 324 | setSafeForwardingMode(true); |
| 325 | |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 326 | onCreate(savedInstanceState, intent, null, 0, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 329 | /** |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 330 | * Compatibility version for other bundled services that use this overload without |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 331 | * a default title resource |
| 332 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 333 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 335 | CharSequence title, Intent[] initialIntents, |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 336 | List<ResolveInfo> rList, boolean supportsAlwaysUseOption) { |
| 337 | onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, |
| 338 | supportsAlwaysUseOption); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
| 342 | CharSequence title, int defaultTitleRes, Intent[] initialIntents, |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 343 | List<ResolveInfo> rList, boolean supportsAlwaysUseOption) { |
Mike Digman | d45a8b5 | 2020-05-13 12:32:19 -0700 | [diff] [blame] | 344 | setTheme(appliedThemeResId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | super.onCreate(savedInstanceState); |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 346 | |
| 347 | // Determine whether we should show that intent is forwarded |
| 348 | // from managed profile to owner or other way around. |
| 349 | setProfileSwitchMessageId(intent.getContentUserHint()); |
| 350 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 351 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 352 | mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid( |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 353 | getActivityToken()); |
| 354 | } catch (RemoteException e) { |
| 355 | mLaunchedFromUid = -1; |
| 356 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 357 | |
| 358 | if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) { |
| 359 | // Gulp! |
| 360 | finish(); |
| 361 | return; |
| 362 | } |
| 363 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | mPm = getPackageManager(); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 365 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 366 | mReferrerPackage = getReferrerPackageName(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 367 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 368 | // Add our initial intent as the first item, regardless of what else has already been added. |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 369 | mIntents.add(0, new Intent(intent)); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 370 | mTitle = title; |
| 371 | mDefaultTitleResId = defaultTitleRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 372 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 373 | mSupportsAlwaysUseOption = supportsAlwaysUseOption; |
arangelov | 8519218 | 2020-05-13 13:19:19 +0100 | [diff] [blame] | 374 | mWorkProfileUserHandle = fetchWorkProfileUserProfile(); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 375 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 376 | // The last argument of createResolverListAdapter is whether to do special handling |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 377 | // of the last used choice to highlight it in the list. We need to always |
| 378 | // turn this off when running under voice interaction, since it results in |
| 379 | // a more complicated UI that the current voice interaction flow is not able |
arangelov | aa307e2 | 2020-05-07 16:14:32 +0100 | [diff] [blame] | 380 | // to handle. We also turn it off when the work tab is shown to simplify the UX. |
| 381 | boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction() |
| 382 | && !shouldShowTabs(); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 383 | mMultiProfilePagerAdapter = createMultiProfilePagerAdapter(initialIntents, rList, filterLastUsed); |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 384 | if (configureContentView()) { |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 385 | return; |
| 386 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 387 | |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 388 | mPersonalPackageMonitor = createPackageMonitor( |
| 389 | mMultiProfilePagerAdapter.getPersonalListAdapter()); |
| 390 | mPersonalPackageMonitor.register( |
| 391 | this, getMainLooper(), getPersonalProfileUserHandle(), false); |
arangelov | f163a88 | 2020-02-18 17:18:47 +0000 | [diff] [blame] | 392 | if (shouldShowTabs()) { |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 393 | mWorkPackageMonitor = createPackageMonitor( |
| 394 | mMultiProfilePagerAdapter.getWorkListAdapter()); |
| 395 | mWorkPackageMonitor.register(this, getMainLooper(), getWorkProfileUserHandle(), false); |
| 396 | } |
| 397 | |
| 398 | mRegistered = true; |
| 399 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 400 | final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel); |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 401 | if (rdl != null) { |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 402 | rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 403 | @Override |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 404 | public void onDismissed() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 405 | finish(); |
| 406 | } |
| 407 | }); |
Sergey Troshin | 40e979e | 2019-12-30 17:29:50 +0100 | [diff] [blame] | 408 | |
| 409 | boolean hasTouchScreen = getPackageManager() |
| 410 | .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN); |
| 411 | |
| 412 | if (isVoiceInteraction() || !hasTouchScreen) { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 413 | rdl.setCollapsed(false); |
| 414 | } |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 415 | |
| 416 | rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 417 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 418 | rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets); |
| 419 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 420 | mResolverDrawerLayout = rdl; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 423 | mProfileView = findViewById(R.id.profile_button); |
| 424 | if (mProfileView != null) { |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 425 | mProfileView.setOnClickListener(this::onProfileClick); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 426 | updateProfileViewButton(); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 427 | } |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 428 | |
Clara Bayarri | 9550f5d | 2016-05-11 11:20:13 +0100 | [diff] [blame] | 429 | final Set<String> categories = intent.getCategories(); |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 430 | MetricsLogger.action(this, mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem() |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 431 | ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED |
| 432 | : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED, |
| 433 | intent.getAction() + ":" + intent.getType() + ":" |
Clara Bayarri | 9550f5d | 2016-05-11 11:20:13 +0100 | [diff] [blame] | 434 | + (categories != null ? Arrays.toString(categories.toArray()) : "")); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 435 | } |
| 436 | |
arangelov | c4dbdbd | 2020-02-18 20:54:16 +0000 | [diff] [blame] | 437 | private boolean isIntentPicker() { |
| 438 | return getClass().equals(ResolverActivity.class); |
| 439 | } |
| 440 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 441 | protected AbstractMultiProfilePagerAdapter createMultiProfilePagerAdapter( |
| 442 | Intent[] initialIntents, |
| 443 | List<ResolveInfo> rList, |
| 444 | boolean filterLastUsed) { |
| 445 | AbstractMultiProfilePagerAdapter resolverMultiProfilePagerAdapter = null; |
arangelov | f163a88 | 2020-02-18 17:18:47 +0000 | [diff] [blame] | 446 | if (shouldShowTabs()) { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 447 | resolverMultiProfilePagerAdapter = |
| 448 | createResolverMultiProfilePagerAdapterForTwoProfiles( |
| 449 | initialIntents, rList, filterLastUsed); |
| 450 | } else { |
| 451 | resolverMultiProfilePagerAdapter = createResolverMultiProfilePagerAdapterForOneProfile( |
| 452 | initialIntents, rList, filterLastUsed); |
| 453 | } |
| 454 | return resolverMultiProfilePagerAdapter; |
| 455 | } |
| 456 | |
| 457 | private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForOneProfile( |
| 458 | Intent[] initialIntents, |
| 459 | List<ResolveInfo> rList, boolean filterLastUsed) { |
| 460 | ResolverListAdapter adapter = createResolverListAdapter( |
| 461 | /* context */ this, |
| 462 | /* payloadIntents */ mIntents, |
| 463 | initialIntents, |
| 464 | rList, |
| 465 | filterLastUsed, |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 466 | /* userHandle */ UserHandle.of(UserHandle.myUserId())); |
| 467 | return new ResolverMultiProfilePagerAdapter( |
| 468 | /* context */ this, |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 469 | adapter, |
| 470 | getPersonalProfileUserHandle(), |
| 471 | /* workProfileUserHandle= */ null); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | private ResolverMultiProfilePagerAdapter createResolverMultiProfilePagerAdapterForTwoProfiles( |
| 475 | Intent[] initialIntents, |
| 476 | List<ResolveInfo> rList, |
| 477 | boolean filterLastUsed) { |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 478 | // In the edge case when we have 0 apps in the current profile and >1 apps in the other, |
| 479 | // the intent resolver is started in the other profile. Since this is the only case when |
| 480 | // this happens, we check for it here and set the current profile's tab. |
| 481 | int selectedProfile = getCurrentProfile(); |
arangelov | 738034b | 2020-06-02 18:18:39 +0100 | [diff] [blame] | 482 | UserHandle intentUser = getIntent().hasExtra(EXTRA_CALLING_USER) |
| 483 | ? getIntent().getParcelableExtra(EXTRA_CALLING_USER) |
| 484 | : getUser(); |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 485 | if (!getUser().equals(intentUser)) { |
| 486 | if (getPersonalProfileUserHandle().equals(intentUser)) { |
| 487 | selectedProfile = PROFILE_PERSONAL; |
| 488 | } else if (getWorkProfileUserHandle().equals(intentUser)) { |
| 489 | selectedProfile = PROFILE_WORK; |
| 490 | } |
arangelov | 738034b | 2020-06-02 18:18:39 +0100 | [diff] [blame] | 491 | } else { |
| 492 | int selectedProfileExtra = getSelectedProfileExtra(); |
| 493 | if (selectedProfileExtra != -1) { |
| 494 | selectedProfile = selectedProfileExtra; |
| 495 | } |
arangelov | eb7793a | 2020-05-11 16:57:33 +0100 | [diff] [blame] | 496 | } |
arangelov | acc8243 | 2020-04-28 12:55:55 +0100 | [diff] [blame] | 497 | // We only show the default app for the profile of the current user. The filterLastUsed |
| 498 | // flag determines whether to show a default app and that app is not shown in the |
| 499 | // resolver list. So filterLastUsed should be false for the other profile. |
| 500 | ResolverListAdapter personalAdapter = createResolverListAdapter( |
| 501 | /* context */ this, |
| 502 | /* payloadIntents */ mIntents, |
| 503 | selectedProfile == PROFILE_PERSONAL ? initialIntents : null, |
| 504 | rList, |
| 505 | (filterLastUsed && UserHandle.myUserId() |
| 506 | == getPersonalProfileUserHandle().getIdentifier()), |
arangelov | acc8243 | 2020-04-28 12:55:55 +0100 | [diff] [blame] | 507 | /* userHandle */ getPersonalProfileUserHandle()); |
| 508 | UserHandle workProfileUserHandle = getWorkProfileUserHandle(); |
| 509 | ResolverListAdapter workAdapter = createResolverListAdapter( |
| 510 | /* context */ this, |
| 511 | /* payloadIntents */ mIntents, |
| 512 | selectedProfile == PROFILE_WORK ? initialIntents : null, |
| 513 | rList, |
| 514 | (filterLastUsed && UserHandle.myUserId() |
| 515 | == workProfileUserHandle.getIdentifier()), |
arangelov | acc8243 | 2020-04-28 12:55:55 +0100 | [diff] [blame] | 516 | /* userHandle */ workProfileUserHandle); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 517 | return new ResolverMultiProfilePagerAdapter( |
| 518 | /* context */ this, |
| 519 | personalAdapter, |
| 520 | workAdapter, |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 521 | selectedProfile, |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 522 | getPersonalProfileUserHandle(), |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 523 | getWorkProfileUserHandle(), |
| 524 | /* shouldShowNoCrossProfileIntentsEmptyState= */ getUser().equals(intentUser)); |
| 525 | } |
| 526 | |
Mike Digman | d45a8b5 | 2020-05-13 12:32:19 -0700 | [diff] [blame] | 527 | protected int appliedThemeResId() { |
| 528 | return R.style.Theme_DeviceDefault_Resolver; |
| 529 | } |
| 530 | |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 531 | /** |
arangelov | eb7793a | 2020-05-11 16:57:33 +0100 | [diff] [blame] | 532 | * Returns {@link #PROFILE_PERSONAL} or {@link #PROFILE_WORK} if the {@link |
| 533 | * #EXTRA_SELECTED_PROFILE} extra was supplied, or {@code -1} if no extra was supplied. |
| 534 | * @throws IllegalArgumentException if the value passed to the {@link #EXTRA_SELECTED_PROFILE} |
| 535 | * extra is not {@link #PROFILE_PERSONAL} or {@link #PROFILE_WORK} |
| 536 | */ |
| 537 | int getSelectedProfileExtra() { |
| 538 | int selectedProfile = -1; |
| 539 | if (getIntent().hasExtra(EXTRA_SELECTED_PROFILE)) { |
| 540 | selectedProfile = getIntent().getIntExtra(EXTRA_SELECTED_PROFILE, /* defValue = */ -1); |
| 541 | if (selectedProfile != PROFILE_PERSONAL && selectedProfile != PROFILE_WORK) { |
| 542 | throw new IllegalArgumentException(EXTRA_SELECTED_PROFILE + " has invalid value " |
| 543 | + selectedProfile + ". Must be either ResolverActivity.PROFILE_PERSONAL or " |
| 544 | + "ResolverActivity.PROFILE_WORK."); |
| 545 | } |
| 546 | } |
| 547 | return selectedProfile; |
| 548 | } |
| 549 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 550 | protected @Profile int getCurrentProfile() { |
| 551 | return (UserHandle.myUserId() == UserHandle.USER_SYSTEM ? PROFILE_PERSONAL : PROFILE_WORK); |
| 552 | } |
| 553 | |
| 554 | protected UserHandle getPersonalProfileUserHandle() { |
| 555 | return UserHandle.of(ActivityManager.getCurrentUser()); |
| 556 | } |
| 557 | protected @Nullable UserHandle getWorkProfileUserHandle() { |
arangelov | 706316d | 2020-05-06 14:25:06 +0100 | [diff] [blame] | 558 | return mWorkProfileUserHandle; |
| 559 | } |
| 560 | |
| 561 | protected @Nullable UserHandle fetchWorkProfileUserProfile() { |
| 562 | mWorkProfileUserHandle = null; |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 563 | UserManager userManager = getSystemService(UserManager.class); |
| 564 | for (final UserInfo userInfo : userManager.getProfiles(ActivityManager.getCurrentUser())) { |
| 565 | if (userInfo.isManagedProfile()) { |
arangelov | 706316d | 2020-05-06 14:25:06 +0100 | [diff] [blame] | 566 | mWorkProfileUserHandle = userInfo.getUserHandle(); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 567 | } |
| 568 | } |
arangelov | 706316d | 2020-05-06 14:25:06 +0100 | [diff] [blame] | 569 | return mWorkProfileUserHandle; |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 570 | } |
| 571 | |
arangelov | f163a88 | 2020-02-18 17:18:47 +0000 | [diff] [blame] | 572 | private boolean hasWorkProfile() { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 573 | return getWorkProfileUserHandle() != null; |
| 574 | } |
| 575 | |
arangelov | f163a88 | 2020-02-18 17:18:47 +0000 | [diff] [blame] | 576 | protected boolean shouldShowTabs() { |
| 577 | return hasWorkProfile() && ENABLE_TABBED_VIEW; |
| 578 | } |
| 579 | |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 580 | protected void onProfileClick(View v) { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 581 | final DisplayResolveInfo dri = |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 582 | mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile(); |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 583 | if (dri == null) { |
| 584 | return; |
| 585 | } |
| 586 | |
| 587 | // Do not show the profile switch message anymore. |
| 588 | mProfileSwitchMessageId = -1; |
| 589 | |
| 590 | onTargetSelected(dri, false); |
| 591 | finish(); |
| 592 | } |
| 593 | |
Matt Pietal | 9236adc | 2019-12-12 09:24:23 -0500 | [diff] [blame] | 594 | /** |
| 595 | * Numerous layouts are supported, each with optional ViewGroups. |
| 596 | * Make sure the inset gets added to the correct View, using |
| 597 | * a footer for Lists so it can properly scroll under the navbar. |
| 598 | */ |
| 599 | protected boolean shouldAddFooterView() { |
| 600 | if (useLayoutWithDefault()) return true; |
| 601 | |
| 602 | View buttonBar = findViewById(R.id.button_bar); |
| 603 | if (buttonBar == null || buttonBar.getVisibility() == View.GONE) return true; |
| 604 | |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | protected void applyFooterView(int height) { |
| 609 | if (mFooterSpacer == null) { |
| 610 | mFooterSpacer = new Space(getApplicationContext()); |
| 611 | } else { |
| 612 | ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter) |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 613 | .getActiveAdapterView().removeFooterView(mFooterSpacer); |
Matt Pietal | 9236adc | 2019-12-12 09:24:23 -0500 | [diff] [blame] | 614 | } |
| 615 | mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, |
| 616 | mSystemWindowInsets.bottom)); |
| 617 | ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter) |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 618 | .getActiveAdapterView().addFooterView(mFooterSpacer); |
Matt Pietal | 9236adc | 2019-12-12 09:24:23 -0500 | [diff] [blame] | 619 | } |
| 620 | |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 621 | protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { |
| 622 | mSystemWindowInsets = insets.getSystemWindowInsets(); |
| 623 | |
| 624 | mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top, |
| 625 | mSystemWindowInsets.right, 0); |
| 626 | |
Matt Pietal | 9236adc | 2019-12-12 09:24:23 -0500 | [diff] [blame] | 627 | resetButtonBar(); |
| 628 | |
Matt Pietal | d507120 | 2019-10-10 11:21:28 -0400 | [diff] [blame] | 629 | // Need extra padding so the list can fully scroll up |
Matt Pietal | 9236adc | 2019-12-12 09:24:23 -0500 | [diff] [blame] | 630 | if (shouldAddFooterView()) { |
| 631 | applyFooterView(mSystemWindowInsets.bottom); |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 632 | } |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 633 | |
| 634 | return insets.consumeSystemWindowInsets(); |
| 635 | } |
| 636 | |
Jiongxuan Gao | 3365b16 | 2016-11-24 20:20:27 +0900 | [diff] [blame] | 637 | @Override |
| 638 | public void onConfigurationChanged(Configuration newConfig) { |
| 639 | super.onConfigurationChanged(newConfig); |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 640 | mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged(); |
arangelov | 981e414 | 2020-05-27 15:59:18 +0100 | [diff] [blame] | 641 | if (isIntentPicker() && shouldShowTabs() && !useLayoutWithDefault()) { |
| 642 | updateIntentPickerPaddings(); |
| 643 | } |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 644 | |
| 645 | if (mSystemWindowInsets != null) { |
| 646 | mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top, |
| 647 | mSystemWindowInsets.right, 0); |
| 648 | } |
Jiongxuan Gao | 3365b16 | 2016-11-24 20:20:27 +0900 | [diff] [blame] | 649 | } |
| 650 | |
arangelov | 981e414 | 2020-05-27 15:59:18 +0100 | [diff] [blame] | 651 | private void updateIntentPickerPaddings() { |
| 652 | View titleCont = findViewById(R.id.title_container); |
| 653 | titleCont.setPadding( |
| 654 | titleCont.getPaddingLeft(), |
| 655 | titleCont.getPaddingTop(), |
| 656 | titleCont.getPaddingRight(), |
| 657 | getResources().getDimensionPixelSize(R.dimen.resolver_title_padding_bottom)); |
| 658 | View buttonBar = findViewById(R.id.button_bar); |
| 659 | buttonBar.setPadding( |
| 660 | buttonBar.getPaddingLeft(), |
| 661 | getResources().getDimensionPixelSize(R.dimen.resolver_button_bar_spacing), |
| 662 | buttonBar.getPaddingRight(), |
| 663 | getResources().getDimensionPixelSize(R.dimen.resolver_button_bar_spacing)); |
| 664 | mMultiProfilePagerAdapter.updateAfterConfigChange(); |
| 665 | } |
| 666 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 667 | @Override // ResolverListCommunicator |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 668 | public void sendVoiceChoicesIfNeeded() { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 669 | if (!isVoiceInteraction()) { |
| 670 | // Clearly not needed. |
| 671 | return; |
| 672 | } |
| 673 | |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 674 | int count = mMultiProfilePagerAdapter.getActiveListAdapter().getCount(); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 675 | final Option[] options = new Option[count]; |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 676 | for (int i = 0, N = options.length; i < N; i++) { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 677 | TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter().getItem(i); |
Matt Pietal | 2cf4c6c | 2019-09-10 09:33:11 -0400 | [diff] [blame] | 678 | if (target == null) { |
| 679 | // If this occurs, a new set of targets is being loaded. Let that complete, |
| 680 | // and have the next call to send voice choices proceed instead. |
| 681 | return; |
| 682 | } |
| 683 | options[i] = optionForChooserTarget(target, i); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | mPickOptionRequest = new PickTargetOptionRequest( |
| 687 | new Prompt(getTitle()), options, null); |
| 688 | getVoiceInteractor().submitRequest(mPickOptionRequest); |
| 689 | } |
| 690 | |
| 691 | Option optionForChooserTarget(TargetInfo target, int index) { |
| 692 | return new Option(target.getDisplayLabel(), index); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 693 | } |
| 694 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 695 | protected final void setAdditionalTargets(Intent[] intents) { |
| 696 | if (intents != null) { |
| 697 | for (Intent intent : intents) { |
| 698 | mIntents.add(intent); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 703 | @Override // SelectableTargetInfoCommunicator ResolverListCommunicator |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 704 | public Intent getTargetIntent() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 705 | return mIntents.isEmpty() ? null : mIntents.get(0); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 708 | protected String getReferrerPackageName() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 709 | final Uri referrer = getReferrer(); |
| 710 | if (referrer != null && "android-app".equals(referrer.getScheme())) { |
| 711 | return referrer.getHost(); |
| 712 | } |
| 713 | return null; |
| 714 | } |
| 715 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 716 | public int getLayoutResource() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 717 | return R.layout.resolver_list; |
| 718 | } |
| 719 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 720 | @Override // ResolverListCommunicator |
| 721 | public void updateProfileViewButton() { |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 722 | if (mProfileView == null) { |
| 723 | return; |
| 724 | } |
| 725 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 726 | final DisplayResolveInfo dri = |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 727 | mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile(); |
arangelov | 2a4d0b1 | 2020-03-04 18:15:04 +0000 | [diff] [blame] | 728 | if (dri != null && !shouldShowTabs()) { |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 729 | mProfileView.setVisibility(View.VISIBLE); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 730 | View text = mProfileView.findViewById(R.id.profile_button); |
| 731 | if (!(text instanceof TextView)) { |
| 732 | text = mProfileView.findViewById(R.id.text1); |
| 733 | } |
| 734 | ((TextView) text).setText(dri.getDisplayLabel()); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 735 | } else { |
| 736 | mProfileView.setVisibility(View.GONE); |
| 737 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 740 | private void setProfileSwitchMessageId(int contentUserHint) { |
| 741 | if (contentUserHint != UserHandle.USER_CURRENT && |
| 742 | contentUserHint != UserHandle.myUserId()) { |
| 743 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 744 | UserInfo originUserInfo = userManager.getUserInfo(contentUserHint); |
| 745 | boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile() |
| 746 | : false; |
| 747 | boolean targetIsManaged = userManager.isManagedProfile(); |
| 748 | if (originIsManaged && !targetIsManaged) { |
| 749 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner; |
| 750 | } else if (!originIsManaged && targetIsManaged) { |
| 751 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 756 | /** |
| 757 | * Turn on launch mode that is safe to use when forwarding intents received from |
| 758 | * applications and running in system processes. This mode uses Activity.startActivityAsCaller |
| 759 | * instead of the normal Activity.startActivity for launching the activity selected |
| 760 | * by the user. |
| 761 | * |
| 762 | * <p>This mode is set to true by default if the activity is initialized through |
| 763 | * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate |
| 764 | * methods, it is set to false by default. You must set it before calling one of the |
| 765 | * more detailed onCreate methods, so that it will be set correctly in the case where |
| 766 | * there is only one intent to resolve and it is thus started immediately.</p> |
| 767 | */ |
| 768 | public void setSafeForwardingMode(boolean safeForwarding) { |
| 769 | mSafeForwardingMode = safeForwarding; |
| 770 | } |
| 771 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 772 | protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) { |
| 773 | final ActionTitle title = mResolvingHome |
| 774 | ? ActionTitle.HOME |
| 775 | : ActionTitle.forAction(intent.getAction()); |
| 776 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 777 | // While there may already be a filtered item, we can only use it in the title if the list |
| 778 | // is already sorted and all information relevant to it is already in the list. |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 779 | final boolean named = |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 780 | mMultiProfilePagerAdapter.getActiveListAdapter().getFilteredPosition() >= 0; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 781 | if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) { |
| 782 | return getString(defaultTitleRes); |
| 783 | } else { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 784 | return named |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 785 | ? getString(title.namedTitleRes, mMultiProfilePagerAdapter |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 786 | .getActiveListAdapter().getFilteredItem().getDisplayLabel()) |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 787 | : getString(title.titleRes); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 788 | } |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 791 | void dismiss() { |
| 792 | if (!isFinishing()) { |
| 793 | finish(); |
| 794 | } |
| 795 | } |
| 796 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 797 | @Override |
| 798 | protected void onRestart() { |
| 799 | super.onRestart(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 800 | if (!mRegistered) { |
arangelov | bdb4005 | 2020-01-09 18:01:58 +0000 | [diff] [blame] | 801 | mPersonalPackageMonitor.register(this, getMainLooper(), |
| 802 | getPersonalProfileUserHandle(), false); |
arangelov | f163a88 | 2020-02-18 17:18:47 +0000 | [diff] [blame] | 803 | if (shouldShowTabs()) { |
arangelov | bdb4005 | 2020-01-09 18:01:58 +0000 | [diff] [blame] | 804 | if (mWorkPackageMonitor == null) { |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 805 | mWorkPackageMonitor = createPackageMonitor( |
| 806 | mMultiProfilePagerAdapter.getWorkListAdapter()); |
arangelov | bdb4005 | 2020-01-09 18:01:58 +0000 | [diff] [blame] | 807 | } |
| 808 | mWorkPackageMonitor.register(this, getMainLooper(), |
| 809 | getWorkProfileUserHandle(), false); |
| 810 | } |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 811 | mRegistered = true; |
| 812 | } |
arangelov | afec9d0 | 2020-03-17 12:41:01 +0000 | [diff] [blame] | 813 | if (shouldShowTabs() && mMultiProfilePagerAdapter.isWaitingToEnableWorkProfile()) { |
| 814 | if (mMultiProfilePagerAdapter.isQuietModeEnabled(getWorkProfileUserHandle())) { |
| 815 | mMultiProfilePagerAdapter.markWorkProfileEnabledBroadcastReceived(); |
| 816 | } |
| 817 | } |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 818 | mMultiProfilePagerAdapter.getActiveListAdapter().handlePackagesChanged(); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 819 | updateProfileViewButton(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | @Override |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 823 | protected void onStart() { |
| 824 | super.onStart(); |
arangelov | f163a88 | 2020-02-18 17:18:47 +0000 | [diff] [blame] | 825 | if (shouldShowTabs()) { |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 826 | mWorkProfileStateReceiver = createWorkProfileStateReceiver(); |
| 827 | registerWorkProfileStateReceiver(); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | private void registerWorkProfileStateReceiver() { |
| 832 | IntentFilter filter = new IntentFilter(); |
arangelov | 2dd9f7b | 2020-02-27 10:54:24 +0000 | [diff] [blame] | 833 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 834 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
| 835 | registerReceiverAsUser(mWorkProfileStateReceiver, UserHandle.ALL, filter, null, null); |
| 836 | } |
| 837 | |
| 838 | @Override |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 839 | protected void onStop() { |
| 840 | super.onStop(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 841 | if (mRegistered) { |
arangelov | bdb4005 | 2020-01-09 18:01:58 +0000 | [diff] [blame] | 842 | mPersonalPackageMonitor.unregister(); |
| 843 | if (mWorkPackageMonitor != null) { |
| 844 | mWorkPackageMonitor.unregister(); |
| 845 | } |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 846 | mRegistered = false; |
| 847 | } |
Wale Ogunwale | 9014e66 | 2016-03-19 14:55:46 -0700 | [diff] [blame] | 848 | final Intent intent = getIntent(); |
| 849 | if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction() |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 850 | && !mResolvingHome && !mRetainInOnStop) { |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 851 | // This resolver is in the unusual situation where it has been |
| 852 | // launched at the top of a new task. We don't let it be added |
| 853 | // to the recent tasks shown to the user, and we need to make sure |
| 854 | // that each time we are launched we get the correct launching |
| 855 | // uid (not re-using the same resolver from an old launching uid), |
| 856 | // so we will now finish ourself since being no longer visible, |
| 857 | // the user probably can't get back to us. |
| 858 | if (!isChangingConfigurations()) { |
| 859 | finish(); |
| 860 | } |
| 861 | } |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 862 | if (mWorkPackageMonitor != null) { |
| 863 | unregisterReceiver(mWorkProfileStateReceiver); |
| 864 | mWorkPackageMonitor = null; |
| 865 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 868 | @Override |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 869 | protected void onDestroy() { |
| 870 | super.onDestroy(); |
| 871 | if (!isChangingConfigurations() && mPickOptionRequest != null) { |
| 872 | mPickOptionRequest.cancel(); |
| 873 | } |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 874 | if (mMultiProfilePagerAdapter.getActiveListAdapter() != null) { |
| 875 | mMultiProfilePagerAdapter.getActiveListAdapter().onDestroy(); |
Kang Li | 38a6da64 | 2017-04-05 12:30:55 -0700 | [diff] [blame] | 876 | } |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | @Override |
arangelov | 9d89414 | 2020-04-21 21:08:21 +0100 | [diff] [blame] | 880 | protected void onSaveInstanceState(Bundle outState) { |
| 881 | super.onSaveInstanceState(outState); |
| 882 | ViewPager viewPager = findViewById(R.id.profile_pager); |
| 883 | outState.putInt(LAST_SHOWN_TAB_KEY, viewPager.getCurrentItem()); |
| 884 | } |
| 885 | |
| 886 | @Override |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 887 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 888 | super.onRestoreInstanceState(savedInstanceState); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 889 | resetButtonBar(); |
arangelov | 9d89414 | 2020-04-21 21:08:21 +0100 | [diff] [blame] | 890 | ViewPager viewPager = findViewById(R.id.profile_pager); |
| 891 | viewPager.setCurrentItem(savedInstanceState.getInt(LAST_SHOWN_TAB_KEY)); |
| 892 | mMultiProfilePagerAdapter.clearInactiveProfileCache(); |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 895 | private boolean hasManagedProfile() { |
| 896 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 897 | if (userManager == null) { |
| 898 | return false; |
| 899 | } |
| 900 | |
| 901 | try { |
| 902 | List<UserInfo> profiles = userManager.getProfiles(getUserId()); |
| 903 | for (UserInfo userInfo : profiles) { |
| 904 | if (userInfo != null && userInfo.isManagedProfile()) { |
| 905 | return true; |
| 906 | } |
| 907 | } |
| 908 | } catch (SecurityException e) { |
| 909 | return false; |
| 910 | } |
| 911 | return false; |
| 912 | } |
| 913 | |
| 914 | private boolean supportsManagedProfiles(ResolveInfo resolveInfo) { |
| 915 | try { |
| 916 | ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| 917 | resolveInfo.activityInfo.packageName, 0 /* default flags */); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 918 | return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 919 | } catch (NameNotFoundException e) { |
| 920 | return false; |
| 921 | } |
| 922 | } |
| 923 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 924 | private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos, |
| 925 | boolean filtered) { |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 926 | if (!mMultiProfilePagerAdapter.getCurrentUserHandle().equals(getUser())) { |
arangelov | 349af6a | 2020-03-05 11:53:40 +0000 | [diff] [blame] | 927 | // Never allow the inactive profile to always open an app. |
| 928 | mAlwaysButton.setEnabled(false); |
| 929 | return; |
| 930 | } |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 931 | boolean enabled = false; |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 932 | ResolveInfo ri = null; |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 933 | if (hasValidSelection) { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 934 | ri = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 935 | .resolveInfoForPosition(checkedPos, filtered); |
Hakan Seyalioglu | 5dbc819 | 2017-02-24 16:16:37 -0800 | [diff] [blame] | 936 | if (ri == null) { |
| 937 | Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled"); |
| 938 | return; |
| 939 | } else if (ri.targetUserId != UserHandle.USER_CURRENT) { |
| 940 | Log.e(TAG, "Attempted to set selection to resolve info for another user"); |
| 941 | return; |
| 942 | } else { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 943 | enabled = true; |
| 944 | } |
Narayan Kamath | 7541868 | 2020-05-07 13:42:05 +0100 | [diff] [blame] | 945 | |
| 946 | mAlwaysButton.setText(getResources() |
| 947 | .getString(R.string.activity_resolver_use_always)); |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 948 | } |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 949 | |
arangelov | 349af6a | 2020-03-05 11:53:40 +0000 | [diff] [blame] | 950 | if (ri != null) { |
| 951 | ActivityInfo activityInfo = ri.activityInfo; |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 952 | |
arangelov | 349af6a | 2020-03-05 11:53:40 +0000 | [diff] [blame] | 953 | boolean hasRecordPermission = |
| 954 | mPm.checkPermission(android.Manifest.permission.RECORD_AUDIO, |
| 955 | activityInfo.packageName) |
| 956 | == android.content.pm.PackageManager.PERMISSION_GRANTED; |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 957 | |
arangelov | 349af6a | 2020-03-05 11:53:40 +0000 | [diff] [blame] | 958 | if (!hasRecordPermission) { |
| 959 | // OK, we know the record permission, is this a capture device |
| 960 | boolean hasAudioCapture = |
| 961 | getIntent().getBooleanExtra( |
| 962 | ResolverActivity.EXTRA_IS_AUDIO_CAPTURE_DEVICE, false); |
| 963 | enabled = !hasAudioCapture; |
| 964 | } |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 965 | } |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 966 | mAlwaysButton.setEnabled(enabled); |
| 967 | } |
| 968 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 969 | public void onButtonClick(View v) { |
| 970 | final int id = v.getId(); |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 971 | ListView listView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView(); |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 972 | ResolverListAdapter currentListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter(); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 973 | int which = currentListAdapter.hasFilteredItem() |
| 974 | ? currentListAdapter.getFilteredPosition() |
| 975 | : listView.getCheckedItemPosition(); |
| 976 | boolean hasIndexBeenFiltered = !currentListAdapter.hasFilteredItem(); |
Narayan Kamath | 7541868 | 2020-05-07 13:42:05 +0100 | [diff] [blame] | 977 | startSelected(which, id == R.id.button_always, hasIndexBeenFiltered); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 980 | public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) { |
Amith Yamasani | 07cd351 | 2013-09-18 13:16:00 -0700 | [diff] [blame] | 981 | if (isFinishing()) { |
| 982 | return; |
| 983 | } |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 984 | ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 985 | .resolveInfoForPosition(which, hasIndexBeenFiltered); |
Sander Alewijnse | 86d35ba | 2015-02-04 15:14:53 +0000 | [diff] [blame] | 986 | if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) { |
| 987 | Toast.makeText(this, String.format(getResources().getString( |
| 988 | com.android.internal.R.string.activity_resolver_work_profiles_support), |
| 989 | ri.activityInfo.loadLabel(getPackageManager()).toString()), |
| 990 | Toast.LENGTH_LONG).show(); |
| 991 | return; |
| 992 | } |
| 993 | |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 994 | TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 995 | .targetInfoForPosition(which, hasIndexBeenFiltered); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 996 | if (target == null) { |
| 997 | return; |
| 998 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 999 | if (onTargetSelected(target, always)) { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1000 | if (always && mSupportsAlwaysUseOption) { |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 1001 | MetricsLogger.action( |
| 1002 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1003 | } else if (mSupportsAlwaysUseOption) { |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 1004 | MetricsLogger.action( |
| 1005 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE); |
| 1006 | } else { |
| 1007 | MetricsLogger.action( |
| 1008 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP); |
| 1009 | } |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1010 | MetricsLogger.action(this, |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1011 | mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem() |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 1012 | ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED |
| 1013 | : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1014 | finish(); |
| 1015 | } |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 1016 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1018 | /** |
| 1019 | * Replace me in subclasses! |
| 1020 | */ |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1021 | @Override // ResolverListCommunicator |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 1022 | public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) { |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1023 | return defIntent; |
| 1024 | } |
| 1025 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1026 | @Override // ResolverListCommunicator |
arangelov | 1b11cc1 | 2020-04-27 17:23:04 +0100 | [diff] [blame] | 1027 | public final void onPostListReady(ResolverListAdapter listAdapter, boolean doPostProcessing, |
| 1028 | boolean rebuildCompleted) { |
arangelov | 4d3be0b | 2020-04-23 18:54:15 +0100 | [diff] [blame] | 1029 | if (isAutolaunching()) { |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1030 | return; |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1031 | } |
arangelov | 27b9974 | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1032 | if (isIntentPicker()) { |
| 1033 | ((ResolverMultiProfilePagerAdapter) mMultiProfilePagerAdapter) |
| 1034 | .setUseLayoutWithDefault(useLayoutWithDefault()); |
| 1035 | } |
arangelov | ce7d4a6 | 2020-04-01 13:26:51 +0100 | [diff] [blame] | 1036 | if (mMultiProfilePagerAdapter.shouldShowEmptyStateScreen(listAdapter)) { |
arangelov | 2760855 | 2020-03-27 16:57:50 +0000 | [diff] [blame] | 1037 | mMultiProfilePagerAdapter.showEmptyResolverListEmptyState(listAdapter); |
arangelov | 4c0513d | 2020-03-05 22:24:13 +0000 | [diff] [blame] | 1038 | } else { |
| 1039 | mMultiProfilePagerAdapter.showListView(listAdapter); |
arangelov | 590fba3 | 2020-02-11 18:05:42 +0000 | [diff] [blame] | 1040 | } |
arangelov | 4d3be0b | 2020-04-23 18:54:15 +0100 | [diff] [blame] | 1041 | // showEmptyResolverListEmptyState can mark the tab as loaded, |
| 1042 | // which is a precondition for auto launching |
arangelov | 1b11cc1 | 2020-04-27 17:23:04 +0100 | [diff] [blame] | 1043 | if (rebuildCompleted && maybeAutolaunchActivity()) { |
arangelov | 4d3be0b | 2020-04-23 18:54:15 +0100 | [diff] [blame] | 1044 | return; |
| 1045 | } |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1046 | if (doPostProcessing) { |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 1047 | maybeCreateHeader(listAdapter); |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1048 | resetButtonBar(); |
| 1049 | onListRebuilt(listAdapter); |
| 1050 | } |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1053 | protected void onListRebuilt(ResolverListAdapter listAdapter) { |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1054 | final ItemClickListener listener = new ItemClickListener(); |
| 1055 | setupAdapterListView((ListView) mMultiProfilePagerAdapter.getActiveAdapterView(), listener); |
arangelov | bf80675 | 2020-04-09 18:31:50 +0100 | [diff] [blame] | 1056 | if (shouldShowTabs() && isIntentPicker()) { |
| 1057 | final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel); |
| 1058 | if (rdl != null) { |
| 1059 | rdl.setMaxCollapsedHeight(getResources() |
| 1060 | .getDimensionPixelSize(useLayoutWithDefault() |
| 1061 | ? R.dimen.resolver_max_collapsed_height_with_default_with_tabs |
| 1062 | : R.dimen.resolver_max_collapsed_height_with_tabs)); |
| 1063 | } |
| 1064 | } |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1067 | protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1068 | final ResolveInfo ri = target.getResolveInfo(); |
| 1069 | final Intent intent = target != null ? target.getResolvedIntent() : null; |
| 1070 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1071 | if (intent != null && (mSupportsAlwaysUseOption |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1072 | || mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem()) |
| 1073 | && mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredResolveList() != null) { |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1074 | // Build a reasonable intent filter, based on what matched. |
| 1075 | IntentFilter filter = new IntentFilter(); |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 1076 | Intent filterIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 1078 | if (intent.getSelector() != null) { |
| 1079 | filterIntent = intent.getSelector(); |
| 1080 | } else { |
| 1081 | filterIntent = intent; |
| 1082 | } |
| 1083 | |
| 1084 | String action = filterIntent.getAction(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1085 | if (action != null) { |
| 1086 | filter.addAction(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | } |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 1088 | Set<String> categories = filterIntent.getCategories(); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1089 | if (categories != null) { |
| 1090 | for (String cat : categories) { |
| 1091 | filter.addCategory(cat); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1094 | filter.addCategory(Intent.CATEGORY_DEFAULT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1096 | int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK; |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 1097 | Uri data = filterIntent.getData(); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1098 | if (cat == IntentFilter.MATCH_CATEGORY_TYPE) { |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 1099 | String mimeType = filterIntent.resolveType(this); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1100 | if (mimeType != null) { |
| 1101 | try { |
| 1102 | filter.addDataType(mimeType); |
| 1103 | } catch (IntentFilter.MalformedMimeTypeException e) { |
| 1104 | Log.w("ResolverActivity", e); |
| 1105 | filter = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | } |
| 1107 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1108 | } |
| 1109 | if (data != null && data.getScheme() != null) { |
| 1110 | // We need the data specification if there was no type, |
| 1111 | // OR if the scheme is not one of our magical "file:" |
| 1112 | // or "content:" schemes (see IntentFilter for the reason). |
| 1113 | if (cat != IntentFilter.MATCH_CATEGORY_TYPE |
| 1114 | || (!"file".equals(data.getScheme()) |
| 1115 | && !"content".equals(data.getScheme()))) { |
| 1116 | filter.addDataScheme(data.getScheme()); |
| 1117 | |
| 1118 | // Look through the resolved filter to determine which part |
| 1119 | // of it matched the original Intent. |
| 1120 | Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator(); |
| 1121 | if (pIt != null) { |
| 1122 | String ssp = data.getSchemeSpecificPart(); |
| 1123 | while (ssp != null && pIt.hasNext()) { |
| 1124 | PatternMatcher p = pIt.next(); |
| 1125 | if (p.match(ssp)) { |
| 1126 | filter.addDataSchemeSpecificPart(p.getPath(), p.getType()); |
| 1127 | break; |
| 1128 | } |
Dianne Hackborn | df1c0bf | 2013-06-12 16:21:38 -0700 | [diff] [blame] | 1129 | } |
| 1130 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1131 | Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator(); |
| 1132 | if (aIt != null) { |
| 1133 | while (aIt.hasNext()) { |
| 1134 | IntentFilter.AuthorityEntry a = aIt.next(); |
| 1135 | if (a.match(data) >= 0) { |
| 1136 | int port = a.getPort(); |
| 1137 | filter.addDataAuthority(a.getHost(), |
| 1138 | port >= 0 ? Integer.toString(port) : null); |
| 1139 | break; |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | pIt = ri.filter.pathsIterator(); |
| 1144 | if (pIt != null) { |
| 1145 | String path = data.getPath(); |
| 1146 | while (path != null && pIt.hasNext()) { |
| 1147 | PatternMatcher p = pIt.next(); |
| 1148 | if (p.match(path)) { |
| 1149 | filter.addDataPath(p.getPath(), p.getType()); |
| 1150 | break; |
| 1151 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1157 | if (filter != null) { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1158 | final int N = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1159 | .getUnfilteredResolveList().size(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1160 | ComponentName[] set; |
| 1161 | // If we don't add back in the component for forwarding the intent to a managed |
| 1162 | // profile, the preferred activity may not be updated correctly (as the set of |
| 1163 | // components we tell it we knew about will have changed). |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1164 | final boolean needToAddBackProfileForwardingComponent = |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1165 | mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1166 | if (!needToAddBackProfileForwardingComponent) { |
| 1167 | set = new ComponentName[N]; |
| 1168 | } else { |
| 1169 | set = new ComponentName[N + 1]; |
| 1170 | } |
| 1171 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1172 | int bestMatch = 0; |
| 1173 | for (int i=0; i<N; i++) { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1174 | ResolveInfo r = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1175 | .getUnfilteredResolveList().get(i).getResolveInfoAt(0); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1176 | set[i] = new ComponentName(r.activityInfo.packageName, |
| 1177 | r.activityInfo.name); |
| 1178 | if (r.match > bestMatch) bestMatch = r.match; |
| 1179 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1180 | |
| 1181 | if (needToAddBackProfileForwardingComponent) { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1182 | set[N] = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1183 | .getOtherProfile().getResolvedComponentName(); |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1184 | final int otherProfileMatch = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1185 | .getOtherProfile().getResolveInfo().match; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1186 | if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch; |
| 1187 | } |
| 1188 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1189 | if (alwaysCheck) { |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1190 | final int userId = getUserId(); |
| 1191 | final PackageManager pm = getPackageManager(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1192 | |
| 1193 | // Set the preferred Activity |
| 1194 | pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent()); |
| 1195 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1196 | if (ri.handleAllWebDataURI) { |
| 1197 | // Set default Browser if needed |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 1198 | final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId); |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1199 | if (TextUtils.isEmpty(packageName)) { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 1200 | pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId); |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1201 | } |
| 1202 | } else { |
| 1203 | // Update Domain Verification status |
| 1204 | ComponentName cn = intent.getComponent(); |
| 1205 | String packageName = cn.getPackageName(); |
| 1206 | String dataScheme = (data != null) ? data.getScheme() : null; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1207 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1208 | boolean isHttpOrHttps = (dataScheme != null) && |
| 1209 | (dataScheme.equals(IntentFilter.SCHEME_HTTP) || |
| 1210 | dataScheme.equals(IntentFilter.SCHEME_HTTPS)); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1211 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1212 | boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW); |
| 1213 | boolean hasCategoryBrowsable = (categories != null) && |
| 1214 | categories.contains(Intent.CATEGORY_BROWSABLE); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1215 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1216 | if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 1217 | pm.updateIntentVerificationStatusAsUser(packageName, |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1218 | PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS, |
| 1219 | userId); |
| 1220 | } |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1221 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1222 | } else { |
| 1223 | try { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1224 | mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1225 | .mResolverListController.setLastChosen(intent, filter, bestMatch); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1226 | } catch (RemoteException re) { |
| 1227 | Log.d(TAG, "Error calling setLastChosenActivity\n" + re); |
| 1228 | } |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1229 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1233 | if (target != null) { |
arangelov | 9a8a674 | 2020-06-01 14:17:32 +0100 | [diff] [blame] | 1234 | if (intent != null && isLaunchingTargetInOtherProfile()) { |
| 1235 | prepareIntentForCrossProfileLaunch(intent); |
arangelov | 9e14852 | 2020-01-15 21:11:51 +0000 | [diff] [blame] | 1236 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1237 | safelyStartActivity(target); |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1238 | |
| 1239 | // Rely on the ActivityManager to pop up a dialog regarding app suspension |
| 1240 | // and return false |
| 1241 | if (target.isSuspended()) { |
| 1242 | return false; |
| 1243 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1244 | } |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1245 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1246 | return true; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
arangelov | 7c510c2 | 2020-06-03 18:24:40 +0100 | [diff] [blame] | 1249 | private void prepareIntentForCrossProfileLaunch(Intent intent) { |
| 1250 | intent.fixUris(UserHandle.myUserId()); |
| 1251 | } |
arangelov | 9a8a674 | 2020-06-01 14:17:32 +0100 | [diff] [blame] | 1252 | |
| 1253 | private boolean isLaunchingTargetInOtherProfile() { |
| 1254 | return mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier() |
| 1255 | != UserHandle.myUserId(); |
| 1256 | } |
| 1257 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1258 | @VisibleForTesting |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1259 | public void safelyStartActivity(TargetInfo cti) { |
Jeff Sharkey | 2b9eb89 | 2016-02-16 09:21:51 -0700 | [diff] [blame] | 1260 | // We're dispatching intents that might be coming from legacy apps, so |
| 1261 | // don't kill ourselves. |
| 1262 | StrictMode.disableDeathOnFileUriExposure(); |
| 1263 | try { |
| 1264 | safelyStartActivityInternal(cti); |
| 1265 | } finally { |
| 1266 | StrictMode.enableDeathOnFileUriExposure(); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | private void safelyStartActivityInternal(TargetInfo cti) { |
arangelov | 96dec88 | 2020-01-31 20:23:40 +0000 | [diff] [blame] | 1271 | if (mPersonalPackageMonitor != null) { |
| 1272 | mPersonalPackageMonitor.unregister(); |
| 1273 | } |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1274 | if (mWorkPackageMonitor != null) { |
| 1275 | mWorkPackageMonitor.unregister(); |
| 1276 | } |
| 1277 | mRegistered = false; |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 1278 | // If needed, show that intent is forwarded |
| 1279 | // from managed profile to owner or other way around. |
| 1280 | if (mProfileSwitchMessageId != -1) { |
| 1281 | Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show(); |
| 1282 | } |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1283 | UserHandle currentUserHandle = mMultiProfilePagerAdapter.getCurrentUserHandle(); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1284 | if (!mSafeForwardingMode) { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1285 | if (cti.startAsUser(this, null, currentUserHandle)) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1286 | onActivityStarted(cti); |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1287 | maybeLogCrossProfileTargetLaunch(cti, currentUserHandle); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1288 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1289 | return; |
| 1290 | } |
| 1291 | try { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1292 | if (cti.startAsCaller(this, null, currentUserHandle.getIdentifier())) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1293 | onActivityStarted(cti); |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1294 | maybeLogCrossProfileTargetLaunch(cti, currentUserHandle); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1295 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1296 | } catch (RuntimeException e) { |
| 1297 | String launchedFromPackage; |
| 1298 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 1299 | launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage( |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1300 | getActivityToken()); |
| 1301 | } catch (RemoteException e2) { |
| 1302 | launchedFromPackage = "??"; |
| 1303 | } |
| 1304 | Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid |
| 1305 | + " package " + launchedFromPackage + ", while running in " |
| 1306 | + ActivityThread.currentProcessName(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1310 | private void maybeLogCrossProfileTargetLaunch(TargetInfo cti, UserHandle currentUserHandle) { |
arangelov | e5b369c | 2020-03-12 17:36:05 +0000 | [diff] [blame] | 1311 | if (!hasWorkProfile() || currentUserHandle.equals(getUser())) { |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1312 | return; |
| 1313 | } |
| 1314 | DevicePolicyEventLogger |
| 1315 | .createEvent(DevicePolicyEnums.RESOLVER_CROSS_PROFILE_TARGET_OPENED) |
arangelov | e5b369c | 2020-03-12 17:36:05 +0000 | [diff] [blame] | 1316 | .setBoolean(currentUserHandle.equals(getPersonalProfileUserHandle())) |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1317 | .setStrings(getMetricsCategory(), |
| 1318 | cti instanceof ChooserTargetInfo ? "direct_share" : "other_target") |
| 1319 | .write(); |
| 1320 | } |
| 1321 | |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1322 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1323 | public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity, |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1324 | int userId) { |
| 1325 | // Pass intent to delegate chooser activity with permission token. |
| 1326 | // TODO: This should move to a trampoline Activity in the system when the ChooserActivity |
| 1327 | // moves into systemui |
| 1328 | try { |
| 1329 | // TODO: Once this is a small springboard activity, it can move off the UI process |
| 1330 | // and we can move the request method to ActivityManagerInternal. |
| 1331 | IBinder permissionToken = ActivityTaskManager.getService() |
| 1332 | .requestStartActivityPermissionToken(getActivityToken()); |
| 1333 | final Intent chooserIntent = new Intent(); |
| 1334 | final ComponentName delegateActivity = ComponentName.unflattenFromString( |
| 1335 | Resources.getSystem().getString(R.string.config_chooserActivity)); |
| 1336 | chooserIntent.setClassName(delegateActivity.getPackageName(), |
| 1337 | delegateActivity.getClassName()); |
| 1338 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken); |
| 1339 | |
| 1340 | // TODO: These extras will change as chooser activity moves into systemui |
| 1341 | chooserIntent.putExtra(Intent.EXTRA_INTENT, intent); |
| 1342 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options); |
| 1343 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY, |
| 1344 | ignoreTargetSecurity); |
| 1345 | chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId); |
Alison Cichowlas | 7b6f3b6 | 2018-12-07 09:06:57 -0500 | [diff] [blame] | 1346 | chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| 1347 | | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1348 | startActivity(chooserIntent); |
| 1349 | } catch (RemoteException e) { |
| 1350 | Log.e(TAG, e.toString()); |
| 1351 | } |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1355 | public void onActivityStarted(TargetInfo cti) { |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 1356 | // Do nothing |
| 1357 | } |
| 1358 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1359 | @Override // ResolverListCommunicator |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1360 | public boolean shouldGetActivityMetadata() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1361 | return false; |
| 1362 | } |
| 1363 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1364 | public boolean shouldAutoLaunchSingleChoice(TargetInfo target) { |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1365 | return !target.isSuspended(); |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Alison Cichowlas | 19ee292 | 2019-12-16 19:43:12 -0500 | [diff] [blame] | 1368 | void showTargetDetails(ResolveInfo ri) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1369 | Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 1370 | .setData(Uri.fromParts("package", ri.activityInfo.packageName, null)) |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1371 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
arangelov | ebf3c05 | 2020-03-17 13:11:37 +0000 | [diff] [blame] | 1372 | startActivityAsUser(in, mMultiProfilePagerAdapter.getCurrentUserHandle()); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1375 | @VisibleForTesting |
| 1376 | protected ResolverListAdapter createResolverListAdapter(Context context, |
| 1377 | List<Intent> payloadIntents, Intent[] initialIntents, List<ResolveInfo> rList, |
Narayan Kamath | 7541868 | 2020-05-07 13:42:05 +0100 | [diff] [blame] | 1378 | boolean filterLastUsed, UserHandle userHandle) { |
Paul McLean | 07425c8 | 2019-10-18 12:00:11 -0600 | [diff] [blame] | 1379 | Intent startIntent = getIntent(); |
| 1380 | boolean isAudioCaptureDevice = |
| 1381 | startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1382 | return new ResolverListAdapter(context, payloadIntents, initialIntents, rList, |
Narayan Kamath | 7541868 | 2020-05-07 13:42:05 +0100 | [diff] [blame] | 1383 | filterLastUsed, createListController(userHandle), this, |
Paul McLean | 07425c8 | 2019-10-18 12:00:11 -0600 | [diff] [blame] | 1384 | isAudioCaptureDevice); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | @VisibleForTesting |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1388 | protected ResolverListController createListController(UserHandle userHandle) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1389 | return new ResolverListController( |
| 1390 | this, |
| 1391 | mPm, |
| 1392 | getTargetIntent(), |
| 1393 | getReferrerPackageName(), |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1394 | mLaunchedFromUid, |
| 1395 | userHandle); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1396 | } |
| 1397 | |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1398 | /** |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1399 | * Sets up the content view. |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1400 | * @return <code>true</code> if the activity is finishing and creation should halt. |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1401 | */ |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1402 | private boolean configureContentView() { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1403 | if (mMultiProfilePagerAdapter.getActiveListAdapter() == null) { |
| 1404 | throw new IllegalStateException("mMultiProfilePagerAdapter.getCurrentListAdapter() " |
| 1405 | + "cannot be null."); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1406 | } |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1407 | // We partially rebuild the inactive adapter to determine if we should auto launch |
arangelov | b91d08f | 2020-03-05 21:50:14 +0000 | [diff] [blame] | 1408 | // isTabLoaded will be true here if the empty state screen is shown instead of the list. |
| 1409 | boolean rebuildCompleted = mMultiProfilePagerAdapter.rebuildActiveTab(true) |
| 1410 | || mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded(); |
arangelov | 2a4d0b1 | 2020-03-04 18:15:04 +0000 | [diff] [blame] | 1411 | if (shouldShowTabs()) { |
arangelov | b91d08f | 2020-03-05 21:50:14 +0000 | [diff] [blame] | 1412 | boolean rebuildInactiveCompleted = mMultiProfilePagerAdapter.rebuildInactiveTab(false) |
| 1413 | || mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded(); |
arangelov | db0c1f1 | 2020-02-14 17:35:12 +0000 | [diff] [blame] | 1414 | rebuildCompleted = rebuildCompleted && rebuildInactiveCompleted; |
| 1415 | } |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1416 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1417 | if (useLayoutWithDefault()) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1418 | mLayoutId = R.layout.resolver_list_with_default; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1419 | } else { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1420 | mLayoutId = getLayoutResource(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1421 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1422 | setContentView(mLayoutId); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1423 | mMultiProfilePagerAdapter.setupViewPager(findViewById(R.id.profile_pager)); |
arangelov | db0c1f1 | 2020-02-14 17:35:12 +0000 | [diff] [blame] | 1424 | return postRebuildList(rebuildCompleted); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1425 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1426 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1427 | /** |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1428 | * Finishing procedures to be performed after the list has been rebuilt. |
| 1429 | * </p>Subclasses must call postRebuildListInternal at the end of postRebuildList. |
| 1430 | * @param rebuildCompleted |
| 1431 | * @return <code>true</code> if the activity is finishing and creation should halt. |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1432 | */ |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1433 | protected boolean postRebuildList(boolean rebuildCompleted) { |
| 1434 | return postRebuildListInternal(rebuildCompleted); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | /** |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1438 | * Finishing procedures to be performed after the list has been rebuilt. |
| 1439 | * @param rebuildCompleted |
| 1440 | * @return <code>true</code> if the activity is finishing and creation should halt. |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1441 | */ |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1442 | final boolean postRebuildListInternal(boolean rebuildCompleted) { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1443 | int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1444 | |
| 1445 | // We only rebuild asynchronously when we have multiple elements to sort. In the case where |
| 1446 | // we're already done, we can check if we should auto-launch immediately. |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1447 | if (rebuildCompleted && maybeAutolaunchActivity()) { |
| 1448 | return true; |
Jeff Sharkey | cc2ae6b4 | 2015-09-29 13:04:46 -0700 | [diff] [blame] | 1449 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1450 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1451 | setupViewVisibilities(); |
| 1452 | |
arangelov | f163a88 | 2020-02-18 17:18:47 +0000 | [diff] [blame] | 1453 | if (shouldShowTabs()) { |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1454 | setupProfileTabs(); |
| 1455 | } |
| 1456 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1457 | return false; |
| 1458 | } |
| 1459 | |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1460 | private int isPermissionGranted(String permission, int uid) { |
| 1461 | return ActivityManager.checkComponentPermission(permission, uid, |
| 1462 | /* owningUid= */-1, /* exported= */ true); |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * @return {@code true} if a resolved target is autolaunched, otherwise {@code false} |
| 1467 | */ |
| 1468 | private boolean maybeAutolaunchActivity() { |
| 1469 | int numberOfProfiles = mMultiProfilePagerAdapter.getItemCount(); |
| 1470 | if (numberOfProfiles == 1 && maybeAutolaunchIfSingleTarget()) { |
| 1471 | return true; |
arangelov | f6986d4 | 2020-02-12 15:03:22 +0000 | [diff] [blame] | 1472 | } else if (numberOfProfiles == 2 |
arangelov | b91d08f | 2020-03-05 21:50:14 +0000 | [diff] [blame] | 1473 | && mMultiProfilePagerAdapter.getActiveListAdapter().isTabLoaded() |
| 1474 | && mMultiProfilePagerAdapter.getInactiveListAdapter().isTabLoaded() |
arangelov | f6986d4 | 2020-02-12 15:03:22 +0000 | [diff] [blame] | 1475 | && (maybeAutolaunchIfNoAppsOnInactiveTab() |
| 1476 | || maybeAutolaunchIfCrossProfileSupported())) { |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1477 | return true; |
| 1478 | } |
| 1479 | return false; |
| 1480 | } |
| 1481 | |
| 1482 | private boolean maybeAutolaunchIfSingleTarget() { |
| 1483 | int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount(); |
| 1484 | if (count != 1) { |
| 1485 | return false; |
| 1486 | } |
| 1487 | |
arangelov | db0c1f1 | 2020-02-14 17:35:12 +0000 | [diff] [blame] | 1488 | if (mMultiProfilePagerAdapter.getActiveListAdapter().getOtherProfile() != null) { |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1492 | // Only one target, so we're a candidate to auto-launch! |
| 1493 | final TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter() |
| 1494 | .targetInfoForPosition(0, false); |
| 1495 | if (shouldAutoLaunchSingleChoice(target)) { |
| 1496 | safelyStartActivity(target); |
| 1497 | finish(); |
| 1498 | return true; |
| 1499 | } |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
arangelov | f6986d4 | 2020-02-12 15:03:22 +0000 | [diff] [blame] | 1503 | private boolean maybeAutolaunchIfNoAppsOnInactiveTab() { |
| 1504 | int count = mMultiProfilePagerAdapter.getActiveListAdapter().getUnfilteredCount(); |
| 1505 | if (count != 1) { |
| 1506 | return false; |
| 1507 | } |
| 1508 | ResolverListAdapter inactiveListAdapter = |
| 1509 | mMultiProfilePagerAdapter.getInactiveListAdapter(); |
| 1510 | if (inactiveListAdapter.getUnfilteredCount() != 0) { |
| 1511 | return false; |
| 1512 | } |
| 1513 | TargetInfo target = mMultiProfilePagerAdapter.getActiveListAdapter() |
| 1514 | .targetInfoForPosition(0, false); |
| 1515 | safelyStartActivity(target); |
| 1516 | finish(); |
| 1517 | return true; |
| 1518 | } |
| 1519 | |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1520 | /** |
| 1521 | * When we have a personal and a work profile, we auto launch in the following scenario: |
| 1522 | * - There is 1 resolved target on each profile |
| 1523 | * - That target is the same app on both profiles |
| 1524 | * - The target app has permission to communicate cross profiles |
| 1525 | * - The target app has declared it supports cross-profile communication via manifest metadata |
| 1526 | */ |
| 1527 | private boolean maybeAutolaunchIfCrossProfileSupported() { |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1528 | ResolverListAdapter activeListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter(); |
| 1529 | int count = activeListAdapter.getUnfilteredCount(); |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1530 | if (count != 1) { |
| 1531 | return false; |
| 1532 | } |
| 1533 | ResolverListAdapter inactiveListAdapter = |
| 1534 | mMultiProfilePagerAdapter.getInactiveListAdapter(); |
| 1535 | if (inactiveListAdapter.getUnfilteredCount() != 1) { |
| 1536 | return false; |
| 1537 | } |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1538 | TargetInfo activeProfileTarget = activeListAdapter |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1539 | .targetInfoForPosition(0, false); |
| 1540 | TargetInfo inactiveProfileTarget = inactiveListAdapter.targetInfoForPosition(0, false); |
| 1541 | if (!Objects.equals(activeProfileTarget.getResolvedComponentName(), |
| 1542 | inactiveProfileTarget.getResolvedComponentName())) { |
| 1543 | return false; |
| 1544 | } |
| 1545 | if (!shouldAutoLaunchSingleChoice(activeProfileTarget)) { |
| 1546 | return false; |
| 1547 | } |
| 1548 | String packageName = activeProfileTarget.getResolvedComponentName().getPackageName(); |
| 1549 | if (!canAppInteractCrossProfiles(packageName)) { |
| 1550 | return false; |
| 1551 | } |
| 1552 | |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1553 | DevicePolicyEventLogger |
| 1554 | .createEvent(DevicePolicyEnums.RESOLVER_AUTOLAUNCH_CROSS_PROFILE_TARGET) |
arangelov | e5b369c | 2020-03-12 17:36:05 +0000 | [diff] [blame] | 1555 | .setBoolean(activeListAdapter.getUserHandle() |
| 1556 | .equals(getPersonalProfileUserHandle())) |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1557 | .setStrings(getMetricsCategory()) |
| 1558 | .write(); |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1559 | safelyStartActivity(activeProfileTarget); |
| 1560 | finish(); |
| 1561 | return true; |
| 1562 | } |
| 1563 | |
| 1564 | /** |
| 1565 | * Returns whether the package has the necessary permissions to interact across profiles on |
| 1566 | * behalf of a given user. |
| 1567 | * |
| 1568 | * <p>This means meeting the following condition: |
| 1569 | * <ul> |
| 1570 | * <li>The app's {@link ApplicationInfo#crossProfile} flag must be true, and at least |
| 1571 | * one of the following conditions must be fulfilled</li> |
| 1572 | * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS_FULL} granted.</li> |
| 1573 | * <li>{@code Manifest.permission.INTERACT_ACROSS_USERS} granted.</li> |
| 1574 | * <li>{@code Manifest.permission.INTERACT_ACROSS_PROFILES} granted, or the corresponding |
| 1575 | * AppOps {@code android:interact_across_profiles} is set to "allow".</li> |
| 1576 | * </ul> |
| 1577 | * |
| 1578 | */ |
| 1579 | private boolean canAppInteractCrossProfiles(String packageName) { |
| 1580 | ApplicationInfo applicationInfo; |
| 1581 | try { |
| 1582 | applicationInfo = getPackageManager().getApplicationInfo(packageName, 0); |
| 1583 | } catch (NameNotFoundException e) { |
| 1584 | Log.e(TAG, "Package " + packageName + " does not exist on current user."); |
| 1585 | return false; |
| 1586 | } |
| 1587 | if (!applicationInfo.crossProfile) { |
| 1588 | return false; |
| 1589 | } |
| 1590 | |
| 1591 | int packageUid = applicationInfo.uid; |
| 1592 | |
| 1593 | if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1594 | packageUid) == PackageManager.PERMISSION_GRANTED) { |
| 1595 | return true; |
| 1596 | } |
| 1597 | if (isPermissionGranted(android.Manifest.permission.INTERACT_ACROSS_USERS, packageUid) |
| 1598 | == PackageManager.PERMISSION_GRANTED) { |
| 1599 | return true; |
| 1600 | } |
| 1601 | if (PermissionChecker.checkPermissionForPreflight(this, INTERACT_ACROSS_PROFILES, |
| 1602 | PID_UNKNOWN, packageUid, packageName) == PackageManager.PERMISSION_GRANTED) { |
| 1603 | return true; |
| 1604 | } |
| 1605 | return false; |
| 1606 | } |
| 1607 | |
| 1608 | private boolean isAutolaunching() { |
| 1609 | return !mRegistered && isFinishing(); |
| 1610 | } |
| 1611 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1612 | private void setupProfileTabs() { |
Alex Kershaw | 760754e | 2020-05-12 11:32:23 +0100 | [diff] [blame] | 1613 | maybeHideDivider(); |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1614 | TabHost tabHost = findViewById(R.id.profile_tabhost); |
| 1615 | tabHost.setup(); |
| 1616 | ViewPager viewPager = findViewById(R.id.profile_pager); |
arangelov | 9d89414 | 2020-04-21 21:08:21 +0100 | [diff] [blame] | 1617 | viewPager.setSaveEnabled(false); |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1618 | TabHost.TabSpec tabSpec = tabHost.newTabSpec(TAB_TAG_PERSONAL) |
| 1619 | .setContent(R.id.profile_pager) |
| 1620 | .setIndicator(getString(R.string.resolver_personal_tab)); |
| 1621 | tabHost.addTab(tabSpec); |
| 1622 | |
| 1623 | tabSpec = tabHost.newTabSpec(TAB_TAG_WORK) |
| 1624 | .setContent(R.id.profile_pager) |
| 1625 | .setIndicator(getString(R.string.resolver_work_tab)); |
| 1626 | tabHost.addTab(tabSpec); |
| 1627 | |
| 1628 | TabWidget tabWidget = tabHost.getTabWidget(); |
| 1629 | tabWidget.setVisibility(View.VISIBLE); |
| 1630 | resetTabsHeaderStyle(tabWidget); |
| 1631 | updateActiveTabStyle(tabHost); |
| 1632 | |
| 1633 | tabHost.setOnTabChangedListener(tabId -> { |
| 1634 | resetTabsHeaderStyle(tabWidget); |
| 1635 | updateActiveTabStyle(tabHost); |
| 1636 | if (TAB_TAG_PERSONAL.equals(tabId)) { |
| 1637 | viewPager.setCurrentItem(0); |
| 1638 | } else { |
| 1639 | viewPager.setCurrentItem(1); |
| 1640 | } |
| 1641 | setupViewVisibilities(); |
Susi Kharraz-Post | dde9c3a | 2020-03-03 07:55:40 -0500 | [diff] [blame] | 1642 | maybeLogProfileChange(); |
arangelov | 2b15d71 | 2020-04-22 14:54:31 +0100 | [diff] [blame] | 1643 | onProfileTabSelected(); |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1644 | DevicePolicyEventLogger |
| 1645 | .createEvent(DevicePolicyEnums.RESOLVER_SWITCH_TABS) |
| 1646 | .setInt(viewPager.getCurrentItem()) |
| 1647 | .setStrings(getMetricsCategory()) |
| 1648 | .write(); |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1649 | }); |
| 1650 | |
| 1651 | viewPager.setVisibility(View.VISIBLE); |
| 1652 | tabHost.setCurrentTab(mMultiProfilePagerAdapter.getCurrentPage()); |
arangelov | 349af6a | 2020-03-05 11:53:40 +0000 | [diff] [blame] | 1653 | mMultiProfilePagerAdapter.setOnProfileSelectedListener( |
arangelov | d85a74a | 2020-06-02 11:50:19 +0100 | [diff] [blame] | 1654 | new AbstractMultiProfilePagerAdapter.OnProfileSelectedListener() { |
| 1655 | @Override |
| 1656 | public void onProfileSelected(int index) { |
| 1657 | tabHost.setCurrentTab(index); |
| 1658 | resetButtonBar(); |
| 1659 | resetCheckedItem(); |
| 1660 | } |
| 1661 | |
| 1662 | @Override |
| 1663 | public void onProfilePageStateChanged(int state) { |
| 1664 | onHorizontalSwipeStateChanged(state); |
| 1665 | } |
arangelov | 349af6a | 2020-03-05 11:53:40 +0000 | [diff] [blame] | 1666 | }); |
Kholoud Mohamed | 77bc0f1 | 2020-05-15 19:51:26 +0100 | [diff] [blame] | 1667 | mMultiProfilePagerAdapter.setOnSwitchOnWorkSelectedListener( |
| 1668 | () -> { |
| 1669 | final View workTab = tabHost.getTabWidget().getChildAt(1); |
| 1670 | workTab.setFocusable(true); |
| 1671 | workTab.setFocusableInTouchMode(true); |
| 1672 | workTab.requestFocus(); |
| 1673 | }); |
arangelov | 934c64c | 2020-02-14 13:48:23 +0000 | [diff] [blame] | 1674 | findViewById(R.id.resolver_tab_divider).setVisibility(View.VISIBLE); |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
arangelov | d85a74a | 2020-06-02 11:50:19 +0100 | [diff] [blame] | 1677 | void onHorizontalSwipeStateChanged(int state) {} |
| 1678 | |
Alex Kershaw | 760754e | 2020-05-12 11:32:23 +0100 | [diff] [blame] | 1679 | private void maybeHideDivider() { |
| 1680 | if (!isIntentPicker()) { |
| 1681 | return; |
| 1682 | } |
| 1683 | final View divider = findViewById(R.id.divider); |
| 1684 | if (divider == null) { |
| 1685 | return; |
| 1686 | } |
| 1687 | divider.setVisibility(View.GONE); |
| 1688 | } |
| 1689 | |
arangelov | 2b15d71 | 2020-04-22 14:54:31 +0100 | [diff] [blame] | 1690 | /** |
| 1691 | * Callback called when user changes the profile tab. |
| 1692 | * <p>This method is intended to be overridden by subclasses. |
| 1693 | */ |
| 1694 | protected void onProfileTabSelected() { } |
| 1695 | |
arangelov | 349af6a | 2020-03-05 11:53:40 +0000 | [diff] [blame] | 1696 | private void resetCheckedItem() { |
| 1697 | if (!isIntentPicker()) { |
| 1698 | return; |
| 1699 | } |
| 1700 | mLastSelected = ListView.INVALID_POSITION; |
| 1701 | ListView inactiveListView = (ListView) mMultiProfilePagerAdapter.getInactiveAdapterView(); |
| 1702 | if (inactiveListView.getCheckedItemCount() > 0) { |
| 1703 | inactiveListView.setItemChecked(inactiveListView.getCheckedItemPosition(), false); |
| 1704 | } |
| 1705 | } |
| 1706 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1707 | private void resetTabsHeaderStyle(TabWidget tabWidget) { |
arangelov | 791509d | 2020-03-20 13:01:53 +0000 | [diff] [blame] | 1708 | String workContentDescription = getString(R.string.resolver_work_tab_accessibility); |
| 1709 | String personalContentDescription = getString(R.string.resolver_personal_tab_accessibility); |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1710 | for (int i = 0; i < tabWidget.getChildCount(); i++) { |
arangelov | 78fd360 | 2020-02-28 15:21:20 +0000 | [diff] [blame] | 1711 | View tabView = tabWidget.getChildAt(i); |
| 1712 | TextView title = tabView.findViewById(android.R.id.title); |
arangelov | 4b8ad34 | 2020-03-17 13:56:00 +0000 | [diff] [blame] | 1713 | title.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_DialogWindowTitle); |
| 1714 | title.setTextColor(getAttrColor(this, android.R.attr.textColorTertiary)); |
| 1715 | title.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 1716 | getResources().getDimension(R.dimen.resolver_tab_text_size)); |
arangelov | 791509d | 2020-03-20 13:01:53 +0000 | [diff] [blame] | 1717 | if (title.getText().equals(getString(R.string.resolver_personal_tab))) { |
| 1718 | tabView.setContentDescription(personalContentDescription); |
| 1719 | } else if (title.getText().equals(getString(R.string.resolver_work_tab))) { |
| 1720 | tabView.setContentDescription(workContentDescription); |
| 1721 | } |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | |
arangelov | 4b8ad34 | 2020-03-17 13:56:00 +0000 | [diff] [blame] | 1725 | private static int getAttrColor(Context context, int attr) { |
| 1726 | TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); |
| 1727 | int colorAccent = ta.getColor(0, 0); |
| 1728 | ta.recycle(); |
| 1729 | return colorAccent; |
| 1730 | } |
| 1731 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1732 | private void updateActiveTabStyle(TabHost tabHost) { |
| 1733 | TextView title = tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()) |
| 1734 | .findViewById(android.R.id.title); |
arangelov | 4b8ad34 | 2020-03-17 13:56:00 +0000 | [diff] [blame] | 1735 | title.setTextColor(getAttrColor(this, android.R.attr.colorAccent)); |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | private void setupViewVisibilities() { |
arangelov | 590fba3 | 2020-02-11 18:05:42 +0000 | [diff] [blame] | 1739 | ResolverListAdapter activeListAdapter = mMultiProfilePagerAdapter.getActiveListAdapter(); |
arangelov | ce7d4a6 | 2020-04-01 13:26:51 +0100 | [diff] [blame] | 1740 | if (!mMultiProfilePagerAdapter.shouldShowEmptyStateScreen(activeListAdapter)) { |
arangelov | 590fba3 | 2020-02-11 18:05:42 +0000 | [diff] [blame] | 1741 | addUseDifferentAppLabelIfNecessary(activeListAdapter); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1745 | /** |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1746 | * Add a label to signify that the user can pick a different app. |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1747 | * @param adapter The adapter used to provide data to item views. |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1748 | */ |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1749 | public void addUseDifferentAppLabelIfNecessary(ResolverListAdapter adapter) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1750 | final boolean useHeader = adapter.hasFilteredItem(); |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1751 | if (useHeader) { |
| 1752 | FrameLayout stub = findViewById(R.id.stub); |
| 1753 | stub.setVisibility(View.VISIBLE); |
| 1754 | TextView textView = (TextView) LayoutInflater.from(this).inflate( |
| 1755 | R.layout.resolver_different_item_header, null, false); |
arangelov | f163a88 | 2020-02-18 17:18:47 +0000 | [diff] [blame] | 1756 | if (shouldShowTabs()) { |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1757 | textView.setGravity(Gravity.CENTER); |
| 1758 | } |
| 1759 | stub.addView(textView); |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | private void setupAdapterListView(ListView listView, ItemClickListener listener) { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1764 | listView.setOnItemClickListener(listener); |
| 1765 | listView.setOnItemLongClickListener(listener); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1766 | |
Narayan Kamath | 7541868 | 2020-05-07 13:42:05 +0100 | [diff] [blame] | 1767 | if (mSupportsAlwaysUseOption) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1768 | listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); |
| 1769 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1772 | /** |
| 1773 | * Configure the area above the app selection list (title, content preview, etc). |
| 1774 | */ |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 1775 | private void maybeCreateHeader(ResolverListAdapter listAdapter) { |
arangelov | 4e2951b | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1776 | if (mHeaderCreatorUser != null |
| 1777 | && !listAdapter.getUserHandle().equals(mHeaderCreatorUser)) { |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | if (!shouldShowTabs() |
| 1781 | && listAdapter.getCount() == 0 && listAdapter.getPlaceholderCount() == 0) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1782 | final TextView titleView = findViewById(R.id.title); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1783 | if (titleView != null) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1784 | titleView.setVisibility(View.GONE); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1785 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | CharSequence title = mTitle != null |
| 1789 | ? mTitle |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1790 | : getTitleForAction(getTargetIntent(), mDefaultTitleResId); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1791 | |
| 1792 | if (!TextUtils.isEmpty(title)) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1793 | final TextView titleView = findViewById(R.id.title); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1794 | if (titleView != null) { |
| 1795 | titleView.setText(title); |
| 1796 | } |
| 1797 | setTitle(title); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1798 | } |
| 1799 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1800 | final ImageView iconView = findViewById(R.id.icon); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1801 | if (iconView != null) { |
arangelov | 2c6115a | 2020-04-08 13:26:13 +0100 | [diff] [blame] | 1802 | listAdapter.loadFilteredItemIconTaskAsync(iconView); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1803 | } |
arangelov | 4e2951b | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1804 | mHeaderCreatorUser = listAdapter.getUserHandle(); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1807 | protected void resetButtonBar() { |
Narayan Kamath | 7541868 | 2020-05-07 13:42:05 +0100 | [diff] [blame] | 1808 | if (!mSupportsAlwaysUseOption) { |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1809 | return; |
| 1810 | } |
| 1811 | final ViewGroup buttonLayout = findViewById(R.id.button_bar); |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1812 | if (buttonLayout == null) { |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1813 | Log.e(TAG, "Layout unexpectedly does not have a button bar"); |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1814 | return; |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1815 | } |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1816 | ResolverListAdapter activeListAdapter = |
| 1817 | mMultiProfilePagerAdapter.getActiveListAdapter(); |
| 1818 | View buttonBarDivider = findViewById(R.id.resolver_button_bar_divider); |
arangelov | 5d9ca17 | 2020-06-04 18:18:59 +0100 | [diff] [blame] | 1819 | if (!useLayoutWithDefault()) { |
| 1820 | int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0; |
| 1821 | buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(), |
| 1822 | buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize( |
| 1823 | R.dimen.resolver_button_bar_spacing) + inset); |
| 1824 | } |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1825 | if (activeListAdapter.isTabLoaded() |
arangelov | 4e2951b | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1826 | && mMultiProfilePagerAdapter.shouldShowEmptyStateScreen(activeListAdapter) |
| 1827 | && !useLayoutWithDefault()) { |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1828 | buttonLayout.setVisibility(View.INVISIBLE); |
arangelov | 4e2951b | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1829 | if (buttonBarDivider != null) { |
| 1830 | buttonBarDivider.setVisibility(View.INVISIBLE); |
| 1831 | } |
arangelov | 27b9974 | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1832 | setButtonBarIgnoreOffset(/* ignoreOffset */ false); |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1833 | return; |
| 1834 | } |
arangelov | 4e2951b | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1835 | if (buttonBarDivider != null) { |
| 1836 | buttonBarDivider.setVisibility(View.VISIBLE); |
| 1837 | } |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1838 | buttonLayout.setVisibility(View.VISIBLE); |
arangelov | 27b9974 | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1839 | setButtonBarIgnoreOffset(/* ignoreOffset */ true); |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1840 | |
arangelov | 5ecac7a9 | 2020-04-20 21:22:25 +0100 | [diff] [blame] | 1841 | mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once); |
| 1842 | mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always); |
| 1843 | |
| 1844 | resetAlwaysOrOnceButtonBar(); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
arangelov | 27b9974 | 2020-04-23 18:59:09 +0100 | [diff] [blame] | 1847 | /** |
| 1848 | * Updates the button bar container {@code ignoreOffset} layout param. |
| 1849 | * <p>Setting this to {@code true} means that the button bar will be glued to the bottom of |
| 1850 | * the screen. |
| 1851 | */ |
| 1852 | private void setButtonBarIgnoreOffset(boolean ignoreOffset) { |
| 1853 | View buttonBarContainer = findViewById(R.id.button_bar_container); |
| 1854 | if (buttonBarContainer != null) { |
| 1855 | ResolverDrawerLayout.LayoutParams layoutParams = |
| 1856 | (ResolverDrawerLayout.LayoutParams) buttonBarContainer.getLayoutParams(); |
| 1857 | layoutParams.ignoreOffset = ignoreOffset; |
| 1858 | buttonBarContainer.setLayoutParams(layoutParams); |
| 1859 | } |
| 1860 | } |
| 1861 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1862 | private void resetAlwaysOrOnceButtonBar() { |
arangelov | 349af6a | 2020-03-05 11:53:40 +0000 | [diff] [blame] | 1863 | // Disable both buttons initially |
| 1864 | setAlwaysButtonEnabled(false, ListView.INVALID_POSITION, false); |
| 1865 | mOnceButton.setEnabled(false); |
| 1866 | |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 1867 | int filteredPosition = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1868 | .getFilteredPosition(); |
| 1869 | if (useLayoutWithDefault() && filteredPosition != ListView.INVALID_POSITION) { |
| 1870 | setAlwaysButtonEnabled(true, filteredPosition, false); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1871 | mOnceButton.setEnabled(true); |
Sergey Troshin | 40e979e | 2019-12-30 17:29:50 +0100 | [diff] [blame] | 1872 | // Focus the button if we already have the default option |
| 1873 | mOnceButton.requestFocus(); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1874 | return; |
| 1875 | } |
| 1876 | |
| 1877 | // When the items load in, if an item was already selected, enable the buttons |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1878 | ListView currentAdapterView = (ListView) mMultiProfilePagerAdapter.getActiveAdapterView(); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1879 | if (currentAdapterView != null |
| 1880 | && currentAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) { |
| 1881 | setAlwaysButtonEnabled(true, currentAdapterView.getCheckedItemPosition(), true); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1882 | mOnceButton.setEnabled(true); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1883 | } |
| 1884 | } |
| 1885 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1886 | @Override // ResolverListCommunicator |
| 1887 | public boolean useLayoutWithDefault() { |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 1888 | // We only use the default app layout when the profile of the active user has a |
| 1889 | // filtered item. We always show the same default app even in the inactive user profile. |
| 1890 | boolean currentUserAdapterHasFilteredItem; |
| 1891 | if (mMultiProfilePagerAdapter.getCurrentUserHandle().getIdentifier() |
| 1892 | == UserHandle.myUserId()) { |
| 1893 | currentUserAdapterHasFilteredItem = |
| 1894 | mMultiProfilePagerAdapter.getActiveListAdapter().hasFilteredItem(); |
| 1895 | } else { |
| 1896 | currentUserAdapterHasFilteredItem = |
| 1897 | mMultiProfilePagerAdapter.getInactiveListAdapter().hasFilteredItem(); |
| 1898 | } |
| 1899 | return mSupportsAlwaysUseOption && currentUserAdapterHasFilteredItem; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1900 | } |
| 1901 | |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1902 | /** |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 1903 | * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets |
| 1904 | * called and we are launched in a new task. |
| 1905 | */ |
| 1906 | protected void setRetainInOnStop(boolean retainInOnStop) { |
| 1907 | mRetainInOnStop = retainInOnStop; |
| 1908 | } |
| 1909 | |
| 1910 | /** |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1911 | * Check a simple match for the component of two ResolveInfos. |
| 1912 | */ |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1913 | @Override // ResolverListCommunicator |
| 1914 | public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) { |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1915 | return lhs == null ? rhs == null |
| 1916 | : lhs.activityInfo == null ? rhs.activityInfo == null |
| 1917 | : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name) |
| 1918 | && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName); |
| 1919 | } |
| 1920 | |
arangelov | 4bf1747 | 2020-02-17 20:21:46 +0000 | [diff] [blame] | 1921 | protected String getMetricsCategory() { |
| 1922 | return METRICS_CATEGORY_RESOLVER; |
| 1923 | } |
| 1924 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1925 | @Override // ResolverListCommunicator |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1926 | public void onHandlePackagesChanged(ResolverListAdapter listAdapter) { |
| 1927 | if (listAdapter == mMultiProfilePagerAdapter.getActiveListAdapter()) { |
arangelov | e5b369c | 2020-03-12 17:36:05 +0000 | [diff] [blame] | 1928 | if (listAdapter.getUserHandle().equals(getWorkProfileUserHandle()) |
arangelov | 2dd9f7b | 2020-02-27 10:54:24 +0000 | [diff] [blame] | 1929 | && mMultiProfilePagerAdapter.isWaitingToEnableWorkProfile()) { |
| 1930 | // We have just turned on the work profile and entered the pass code to start it, |
| 1931 | // now we are waiting to receive the ACTION_USER_UNLOCKED broadcast. There is no |
| 1932 | // point in reloading the list now, since the work profile user is still |
| 1933 | // turning on. |
| 1934 | return; |
| 1935 | } |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1936 | boolean listRebuilt = mMultiProfilePagerAdapter.rebuildActiveTab(true); |
| 1937 | if (listRebuilt) { |
| 1938 | ResolverListAdapter activeListAdapter = |
| 1939 | mMultiProfilePagerAdapter.getActiveListAdapter(); |
| 1940 | activeListAdapter.notifyDataSetChanged(); |
arangelov | 581a3c5 | 2020-06-17 13:43:20 +0100 | [diff] [blame] | 1941 | if (activeListAdapter.getCount() == 0 && !inactiveListAdapterHasItems()) { |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 1942 | // We no longer have any items... just finish the activity. |
| 1943 | finish(); |
| 1944 | } |
| 1945 | } |
| 1946 | } else { |
| 1947 | mMultiProfilePagerAdapter.clearInactiveProfileCache(); |
Zhen Zhang | 55d66aa | 2019-10-14 17:11:19 -0700 | [diff] [blame] | 1948 | } |
Zhen Zhang | 55d66aa | 2019-10-14 17:11:19 -0700 | [diff] [blame] | 1949 | } |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1950 | |
arangelov | 581a3c5 | 2020-06-17 13:43:20 +0100 | [diff] [blame] | 1951 | private boolean inactiveListAdapterHasItems() { |
| 1952 | if (!shouldShowTabs()) { |
| 1953 | return false; |
| 1954 | } |
| 1955 | return mMultiProfilePagerAdapter.getInactiveListAdapter().getCount() > 0; |
| 1956 | } |
| 1957 | |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 1958 | private BroadcastReceiver createWorkProfileStateReceiver() { |
| 1959 | return new BroadcastReceiver() { |
| 1960 | @Override |
| 1961 | public void onReceive(Context context, Intent intent) { |
| 1962 | String action = intent.getAction(); |
arangelov | 2dd9f7b | 2020-02-27 10:54:24 +0000 | [diff] [blame] | 1963 | if (!TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED) |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 1964 | && !TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
| 1965 | return; |
| 1966 | } |
arangelov | 2dd9f7b | 2020-02-27 10:54:24 +0000 | [diff] [blame] | 1967 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1968 | if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED) |
| 1969 | && userHandle != getWorkProfileUserHandle().getIdentifier()) { |
| 1970 | return; |
| 1971 | } |
| 1972 | if (TextUtils.equals(action, Intent.ACTION_USER_UNLOCKED)) { |
| 1973 | mMultiProfilePagerAdapter.markWorkProfileEnabledBroadcastReceived(); |
| 1974 | } |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 1975 | if (mMultiProfilePagerAdapter.getCurrentUserHandle() |
arangelov | e5b369c | 2020-03-12 17:36:05 +0000 | [diff] [blame] | 1976 | .equals(getWorkProfileUserHandle())) { |
arangelov | 7f8743d | 2020-02-13 20:34:30 +0000 | [diff] [blame] | 1977 | mMultiProfilePagerAdapter.rebuildActiveTab(true); |
| 1978 | } else { |
| 1979 | mMultiProfilePagerAdapter.clearInactiveProfileCache(); |
| 1980 | } |
| 1981 | } |
| 1982 | }; |
| 1983 | } |
| 1984 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1985 | @VisibleForTesting |
| 1986 | public static final class ResolvedComponentInfo { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1987 | public final ComponentName name; |
| 1988 | private final List<Intent> mIntents = new ArrayList<>(); |
| 1989 | private final List<ResolveInfo> mResolveInfos = new ArrayList<>(); |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 1990 | private boolean mPinned; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1991 | |
| 1992 | public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) { |
| 1993 | this.name = name; |
| 1994 | add(intent, info); |
| 1995 | } |
| 1996 | |
| 1997 | public void add(Intent intent, ResolveInfo info) { |
| 1998 | mIntents.add(intent); |
| 1999 | mResolveInfos.add(info); |
| 2000 | } |
| 2001 | |
| 2002 | public int getCount() { |
| 2003 | return mIntents.size(); |
| 2004 | } |
| 2005 | |
| 2006 | public Intent getIntentAt(int index) { |
| 2007 | return index >= 0 ? mIntents.get(index) : null; |
| 2008 | } |
| 2009 | |
| 2010 | public ResolveInfo getResolveInfoAt(int index) { |
| 2011 | return index >= 0 ? mResolveInfos.get(index) : null; |
| 2012 | } |
| 2013 | |
| 2014 | public int findIntent(Intent intent) { |
| 2015 | for (int i = 0, N = mIntents.size(); i < N; i++) { |
| 2016 | if (intent.equals(mIntents.get(i))) { |
| 2017 | return i; |
| 2018 | } |
| 2019 | } |
| 2020 | return -1; |
| 2021 | } |
| 2022 | |
| 2023 | public int findResolveInfo(ResolveInfo info) { |
| 2024 | for (int i = 0, N = mResolveInfos.size(); i < N; i++) { |
| 2025 | if (info.equals(mResolveInfos.get(i))) { |
| 2026 | return i; |
| 2027 | } |
| 2028 | } |
| 2029 | return -1; |
| 2030 | } |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 2031 | |
| 2032 | public boolean isPinned() { |
| 2033 | return mPinned; |
| 2034 | } |
| 2035 | |
| 2036 | public void setPinned(boolean pinned) { |
| 2037 | mPinned = pinned; |
| 2038 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2039 | } |
| 2040 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2041 | class ItemClickListener implements AdapterView.OnItemClickListener, |
| 2042 | AdapterView.OnItemLongClickListener { |
| 2043 | @Override |
| 2044 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 2045 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 2046 | if (listView != null) { |
| 2047 | position -= listView.getHeaderViewsCount(); |
| 2048 | } |
| 2049 | if (position < 0) { |
| 2050 | // Header views don't count. |
| 2051 | return; |
| 2052 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 2053 | // If we're still loading, we can't yet enable the buttons. |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 2054 | if (mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 2055 | .resolveInfoForPosition(position, true) == null) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 2056 | return; |
| 2057 | } |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 2058 | ListView currentAdapterView = |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 2059 | (ListView) mMultiProfilePagerAdapter.getActiveAdapterView(); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 2060 | final int checkedPos = currentAdapterView.getCheckedItemPosition(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2061 | final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 2062 | if (!useLayoutWithDefault() |
Ruchi Kandoi | 65d76c1 | 2017-08-18 13:05:39 -0700 | [diff] [blame] | 2063 | && (!hasValidSelection || mLastSelected != checkedPos) |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 2064 | && mAlwaysButton != null) { |
| 2065 | setAlwaysButtonEnabled(hasValidSelection, checkedPos, true); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2066 | mOnceButton.setEnabled(hasValidSelection); |
| 2067 | if (hasValidSelection) { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 2068 | currentAdapterView.smoothScrollToPosition(checkedPos); |
Sergey Troshin | 40e979e | 2019-12-30 17:29:50 +0100 | [diff] [blame] | 2069 | mOnceButton.requestFocus(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2070 | } |
| 2071 | mLastSelected = checkedPos; |
| 2072 | } else { |
| 2073 | startSelected(position, false, true); |
| 2074 | } |
| 2075 | } |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 2076 | |
| 2077 | @Override |
| 2078 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2079 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 2080 | if (listView != null) { |
| 2081 | position -= listView.getHeaderViewsCount(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2082 | } |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 2083 | if (position < 0) { |
| 2084 | // Header views don't count. |
| 2085 | return false; |
| 2086 | } |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 2087 | ResolveInfo ri = mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 2088 | .resolveInfoForPosition(position, true); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 2089 | showTargetDetails(ri); |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 2090 | return true; |
| 2091 | } |
| 2092 | |
| 2093 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 2094 | |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 2095 | static final boolean isSpecificUriMatch(int match) { |
| 2096 | match = match&IntentFilter.MATCH_CATEGORY_MASK; |
| 2097 | return match >= IntentFilter.MATCH_CATEGORY_HOST |
| 2098 | && match <= IntentFilter.MATCH_CATEGORY_PATH; |
| 2099 | } |
| 2100 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 2101 | static class PickTargetOptionRequest extends PickOptionRequest { |
| 2102 | public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options, |
| 2103 | @Nullable Bundle extras) { |
| 2104 | super(prompt, options, extras); |
| 2105 | } |
| 2106 | |
| 2107 | @Override |
| 2108 | public void onCancel() { |
| 2109 | super.onCancel(); |
| 2110 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 2111 | if (ra != null) { |
| 2112 | ra.mPickOptionRequest = null; |
| 2113 | ra.finish(); |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | @Override |
| 2118 | public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) { |
| 2119 | super.onPickOptionResult(finished, selections, result); |
| 2120 | if (selections.length != 1) { |
| 2121 | // TODO In a better world we would filter the UI presented here and let the |
| 2122 | // user refine. Maybe later. |
| 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 2127 | if (ra != null) { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 2128 | final TargetInfo ti = ra.mMultiProfilePagerAdapter.getActiveListAdapter() |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 2129 | .getItem(selections[0].getIndex()); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 2130 | if (ra.onTargetSelected(ti, false)) { |
| 2131 | ra.mPickOptionRequest = null; |
| 2132 | ra.finish(); |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | } |
Susi Kharraz-Post | dde9c3a | 2020-03-03 07:55:40 -0500 | [diff] [blame] | 2137 | |
| 2138 | protected void maybeLogProfileChange() {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2139 | } |