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