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 | |
Hyunyoung Song | 16b7af4 | 2018-10-05 14:00:42 -0700 | [diff] [blame] | 19 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
| 20 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 22 | import android.annotation.StringRes; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 23 | import android.annotation.UiThread; |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 24 | import android.annotation.UnsupportedAppUsage; |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 25 | import android.app.Activity; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 26 | import android.app.ActivityTaskManager; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 27 | import android.app.ActivityThread; |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 28 | import android.app.VoiceInteractor.PickOptionRequest; |
| 29 | import android.app.VoiceInteractor.PickOptionRequest.Option; |
| 30 | import android.app.VoiceInteractor.Prompt; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.content.ComponentName; |
| 32 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 35 | import android.content.PermissionChecker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.pm.ActivityInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 37 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.content.pm.PackageManager; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 39 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.pm.ResolveInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 41 | import android.content.pm.UserInfo; |
Jiongxuan Gao | 3365b16 | 2016-11-24 20:20:27 +0900 | [diff] [blame] | 42 | import android.content.res.Configuration; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 43 | import android.content.res.Resources; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 44 | import android.graphics.Insets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.net.Uri; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 46 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.Bundle; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 48 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.os.PatternMatcher; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 50 | import android.os.RemoteException; |
Jeff Sharkey | 37355a9 | 2016-02-05 16:19:10 -0700 | [diff] [blame] | 51 | import android.os.StrictMode; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 52 | import android.os.UserHandle; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 53 | import android.os.UserManager; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 54 | import android.provider.MediaStore; |
| 55 | import android.provider.Settings; |
| 56 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.util.Log; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 58 | import android.util.Slog; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 59 | import android.view.LayoutInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.view.View; |
| 61 | import android.view.ViewGroup; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 62 | import android.view.ViewGroup.LayoutParams; |
| 63 | import android.view.WindowInsets; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 64 | import android.widget.AbsListView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 65 | import android.widget.AdapterView; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 66 | import android.widget.Button; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.widget.ImageView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 68 | import android.widget.ListView; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 69 | import android.widget.Space; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.widget.TextView; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 71 | import android.widget.Toast; |
Hyunyoung Song | 16b7af4 | 2018-10-05 14:00:42 -0700 | [diff] [blame] | 72 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 73 | import com.android.internal.R; |
| 74 | import com.android.internal.annotations.VisibleForTesting; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 75 | import com.android.internal.app.chooser.DisplayResolveInfo; |
| 76 | import com.android.internal.app.chooser.TargetInfo; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 77 | import com.android.internal.content.PackageMonitor; |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 78 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 79 | import com.android.internal.logging.nano.MetricsProto; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 80 | import com.android.internal.widget.ResolverDrawerLayout; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 81 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import java.util.ArrayList; |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 83 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import java.util.Iterator; |
| 85 | import java.util.List; |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 86 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import java.util.Set; |
| 88 | |
| 89 | /** |
| 90 | * This activity is displayed when the system attempts to start an Intent for |
| 91 | * which there is more than one matching activity, allowing the user to decide |
| 92 | * which to go to. It is not normally used directly by application developers. |
| 93 | */ |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 94 | @UiThread |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 95 | public class ResolverActivity extends Activity implements |
| 96 | ResolverListAdapter.ResolverListCommunicator { |
Alison Cichowlas | 4691ed4 | 2018-11-13 15:59:55 -0500 | [diff] [blame] | 97 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 98 | @UnsupportedAppUsage |
Artur Satayev | 751e551 | 2019-11-15 19:12:49 +0000 | [diff] [blame] | 99 | public ResolverActivity() { |
| 100 | } |
| 101 | |
| 102 | @UnsupportedAppUsage |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 103 | protected ResolverListAdapter mAdapter; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 104 | private boolean mSafeForwardingMode; |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 105 | private AbsListView mAdapterView; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 106 | private Button mAlwaysButton; |
| 107 | private Button mOnceButton; |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 108 | protected View mProfileView; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 109 | private int mLastSelected = AbsListView.INVALID_POSITION; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 110 | private boolean mResolvingHome = false; |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 111 | private int mProfileSwitchMessageId = -1; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 112 | private int mLayoutId; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 113 | @VisibleForTesting |
| 114 | protected final ArrayList<Intent> mIntents = new ArrayList<>(); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 115 | private PickTargetOptionRequest mPickOptionRequest; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 116 | private String mReferrerPackage; |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 117 | private CharSequence mTitle; |
| 118 | private int mDefaultTitleResId; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 119 | |
| 120 | @VisibleForTesting |
| 121 | protected boolean mUseLayoutForBrowsables; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 122 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 123 | // Whether or not this activity supports choosing a default handler for the intent. |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 124 | @VisibleForTesting |
| 125 | protected boolean mSupportsAlwaysUseOption; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 126 | protected ResolverDrawerLayout mResolverDrawerLayout; |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 127 | @UnsupportedAppUsage |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 128 | protected PackageManager mPm; |
| 129 | protected int mLaunchedFromUid; |
| 130 | |
| 131 | private static final String TAG = "ResolverActivity"; |
| 132 | private static final boolean DEBUG = false; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 133 | |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 134 | private boolean mRegistered; |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 135 | |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 136 | protected Insets mSystemWindowInsets = null; |
| 137 | private Space mFooterSpacer = null; |
| 138 | |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 139 | /** See {@link #setRetainInOnStop}. */ |
| 140 | private boolean mRetainInOnStop; |
| 141 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 142 | private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args"; |
| 143 | private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key"; |
| 144 | private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state"; |
| 145 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 146 | private final PackageMonitor mPackageMonitor = createPackageMonitor(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 147 | |
Paul McLean | 07425c8 | 2019-10-18 12:00:11 -0600 | [diff] [blame] | 148 | // Intent extra for connected audio devices |
| 149 | public static final String EXTRA_IS_AUDIO_CAPTURE_DEVICE = "is_audio_capture_device"; |
| 150 | |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Get the string resource to be used as a label for the link to the resolver activity for an |
| 153 | * action. |
| 154 | * |
| 155 | * @param action The action to resolve |
| 156 | * |
| 157 | * @return The string resource to be used as a label |
| 158 | */ |
| 159 | public static @StringRes int getLabelRes(String action) { |
| 160 | return ActionTitle.forAction(action).labelRes; |
| 161 | } |
| 162 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 163 | private enum ActionTitle { |
| 164 | VIEW(Intent.ACTION_VIEW, |
| 165 | com.android.internal.R.string.whichViewApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 166 | com.android.internal.R.string.whichViewApplicationNamed, |
| 167 | com.android.internal.R.string.whichViewApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 168 | EDIT(Intent.ACTION_EDIT, |
| 169 | com.android.internal.R.string.whichEditApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 170 | com.android.internal.R.string.whichEditApplicationNamed, |
| 171 | com.android.internal.R.string.whichEditApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 172 | SEND(Intent.ACTION_SEND, |
| 173 | com.android.internal.R.string.whichSendApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 174 | com.android.internal.R.string.whichSendApplicationNamed, |
| 175 | com.android.internal.R.string.whichSendApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 176 | SENDTO(Intent.ACTION_SENDTO, |
Adam Powell | 13ea8f4 | 2016-03-18 09:39:41 -0700 | [diff] [blame] | 177 | com.android.internal.R.string.whichSendToApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 178 | com.android.internal.R.string.whichSendToApplicationNamed, |
| 179 | com.android.internal.R.string.whichSendToApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 180 | SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE, |
| 181 | com.android.internal.R.string.whichSendApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 182 | com.android.internal.R.string.whichSendApplicationNamed, |
| 183 | com.android.internal.R.string.whichSendApplicationLabel), |
| 184 | CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE, |
| 185 | com.android.internal.R.string.whichImageCaptureApplication, |
| 186 | com.android.internal.R.string.whichImageCaptureApplicationNamed, |
| 187 | com.android.internal.R.string.whichImageCaptureApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 188 | DEFAULT(null, |
| 189 | com.android.internal.R.string.whichApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 190 | com.android.internal.R.string.whichApplicationNamed, |
| 191 | com.android.internal.R.string.whichApplicationLabel), |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 192 | HOME(Intent.ACTION_MAIN, |
| 193 | com.android.internal.R.string.whichHomeApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 194 | com.android.internal.R.string.whichHomeApplicationNamed, |
| 195 | com.android.internal.R.string.whichHomeApplicationLabel); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 196 | |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 197 | // titles for layout that deals with http(s) intents |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 198 | public static final int BROWSABLE_TITLE_RES = |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 199 | com.android.internal.R.string.whichOpenLinksWith; |
| 200 | public static final int BROWSABLE_HOST_TITLE_RES = |
| 201 | com.android.internal.R.string.whichOpenHostLinksWith; |
| 202 | public static final int BROWSABLE_HOST_APP_TITLE_RES = |
| 203 | com.android.internal.R.string.whichOpenHostLinksWithApp; |
| 204 | public static final int BROWSABLE_APP_TITLE_RES = |
| 205 | com.android.internal.R.string.whichOpenLinksWithApp; |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 206 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 207 | public final String action; |
| 208 | public final int titleRes; |
| 209 | public final int namedTitleRes; |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 210 | public final @StringRes int labelRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 211 | |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 212 | ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 213 | this.action = action; |
| 214 | this.titleRes = titleRes; |
| 215 | this.namedTitleRes = namedTitleRes; |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 216 | this.labelRes = labelRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | public static ActionTitle forAction(String action) { |
| 220 | for (ActionTitle title : values()) { |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 221 | if (title != HOME && action != null && action.equals(title.action)) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 222 | return title; |
| 223 | } |
| 224 | } |
| 225 | return DEFAULT; |
| 226 | } |
| 227 | } |
| 228 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 229 | protected PackageMonitor createPackageMonitor() { |
| 230 | return new PackageMonitor() { |
| 231 | @Override |
| 232 | public void onSomePackagesChanged() { |
| 233 | mAdapter.handlePackagesChanged(); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 234 | updateProfileViewButton(); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
| 239 | // We care about all package changes, not just the whole package itself which is |
| 240 | // default behavior. |
| 241 | return true; |
| 242 | } |
| 243 | }; |
| 244 | } |
| 245 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 246 | private Intent makeMyIntent() { |
| 247 | Intent intent = new Intent(getIntent()); |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 248 | intent.setComponent(null); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 249 | // The resolver activity is set to be hidden from recent tasks. |
| 250 | // we don't want this attribute to be propagated to the next activity |
| 251 | // being launched. Note that if the original Intent also had this |
| 252 | // flag set, we are now losing it. That should be a very rare case |
| 253 | // and we can live with this. |
| 254 | intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 255 | return intent; |
| 256 | } |
| 257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | @Override |
| 259 | protected void onCreate(Bundle savedInstanceState) { |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 260 | // Use a specialized prompt when we're handling the 'Home' app startActivity() |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 261 | final Intent intent = makeMyIntent(); |
| 262 | final Set<String> categories = intent.getCategories(); |
| 263 | if (Intent.ACTION_MAIN.equals(intent.getAction()) |
| 264 | && categories != null |
| 265 | && categories.size() == 1 |
| 266 | && categories.contains(Intent.CATEGORY_HOME)) { |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 267 | // Note: this field is not set to true in the compatibility version. |
| 268 | mResolvingHome = true; |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 271 | setSafeForwardingMode(true); |
| 272 | |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 273 | onCreate(savedInstanceState, intent, null, 0, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 276 | /** |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 277 | * Compatibility version for other bundled services that use this overload without |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 278 | * a default title resource |
| 279 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 280 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 282 | CharSequence title, Intent[] initialIntents, |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 283 | List<ResolveInfo> rList, boolean supportsAlwaysUseOption) { |
| 284 | onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, |
| 285 | supportsAlwaysUseOption); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
| 289 | CharSequence title, int defaultTitleRes, Intent[] initialIntents, |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 290 | List<ResolveInfo> rList, boolean supportsAlwaysUseOption) { |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 291 | setTheme(R.style.Theme_DeviceDefault_Resolver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | super.onCreate(savedInstanceState); |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 293 | |
| 294 | // Determine whether we should show that intent is forwarded |
| 295 | // from managed profile to owner or other way around. |
| 296 | setProfileSwitchMessageId(intent.getContentUserHint()); |
| 297 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 298 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 299 | mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid( |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 300 | getActivityToken()); |
| 301 | } catch (RemoteException e) { |
| 302 | mLaunchedFromUid = -1; |
| 303 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 304 | |
| 305 | if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) { |
| 306 | // Gulp! |
| 307 | finish(); |
| 308 | return; |
| 309 | } |
| 310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | mPm = getPackageManager(); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 312 | |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 313 | mPackageMonitor.register(this, getMainLooper(), false); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 314 | mRegistered = true; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 315 | mReferrerPackage = getReferrerPackageName(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 316 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 317 | // 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] | 318 | mIntents.add(0, new Intent(intent)); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 319 | mTitle = title; |
| 320 | mDefaultTitleResId = defaultTitleRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 321 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 322 | mUseLayoutForBrowsables = getTargetIntent() == null |
| 323 | ? false |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 324 | : isHttpSchemeAndViewAction(getTargetIntent()); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 325 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 326 | mSupportsAlwaysUseOption = supportsAlwaysUseOption; |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 327 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 328 | // The last argument of createAdapter is whether to do special handling |
| 329 | // of the last used choice to highlight it in the list. We need to always |
| 330 | // turn this off when running under voice interaction, since it results in |
| 331 | // a more complicated UI that the current voice interaction flow is not able |
| 332 | // to handle. |
| 333 | boolean filterLastUsed = mSupportsAlwaysUseOption && !isVoiceInteraction(); |
| 334 | mAdapter = createAdapter(this, mIntents, initialIntents, rList, |
| 335 | filterLastUsed, mUseLayoutForBrowsables); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 336 | |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 337 | if (configureContentView()) { |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 338 | return; |
| 339 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 340 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 341 | final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel); |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 342 | if (rdl != null) { |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 343 | rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 344 | @Override |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 345 | public void onDismissed() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 346 | finish(); |
| 347 | } |
| 348 | }); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 349 | if (isVoiceInteraction()) { |
| 350 | rdl.setCollapsed(false); |
| 351 | } |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 352 | |
| 353 | rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 354 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 355 | rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets); |
| 356 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 357 | mResolverDrawerLayout = rdl; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 360 | mProfileView = findViewById(R.id.profile_button); |
| 361 | if (mProfileView != null) { |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 362 | mProfileView.setOnClickListener(this::onProfileClick); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 363 | updateProfileViewButton(); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 364 | } |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 365 | |
Clara Bayarri | 9550f5d | 2016-05-11 11:20:13 +0100 | [diff] [blame] | 366 | final Set<String> categories = intent.getCategories(); |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 367 | MetricsLogger.action(this, mAdapter.hasFilteredItem() |
| 368 | ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED |
| 369 | : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED, |
| 370 | intent.getAction() + ":" + intent.getType() + ":" |
Clara Bayarri | 9550f5d | 2016-05-11 11:20:13 +0100 | [diff] [blame] | 371 | + (categories != null ? Arrays.toString(categories.toArray()) : "")); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 374 | protected void onProfileClick(View v) { |
| 375 | final DisplayResolveInfo dri = mAdapter.getOtherProfile(); |
| 376 | if (dri == null) { |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // Do not show the profile switch message anymore. |
| 381 | mProfileSwitchMessageId = -1; |
| 382 | |
| 383 | onTargetSelected(dri, false); |
| 384 | finish(); |
| 385 | } |
| 386 | |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 387 | protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { |
| 388 | mSystemWindowInsets = insets.getSystemWindowInsets(); |
| 389 | |
| 390 | mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top, |
| 391 | mSystemWindowInsets.right, 0); |
| 392 | |
Matt Pietal | d507120 | 2019-10-10 11:21:28 -0400 | [diff] [blame] | 393 | // Need extra padding so the list can fully scroll up |
| 394 | if (useLayoutWithDefault()) { |
| 395 | if (mFooterSpacer == null) { |
| 396 | mFooterSpacer = new Space(getApplicationContext()); |
| 397 | } else { |
| 398 | ((ListView) mAdapterView).removeFooterView(mFooterSpacer); |
| 399 | } |
| 400 | mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, |
| 401 | mSystemWindowInsets.bottom)); |
| 402 | ((ListView) mAdapterView).addFooterView(mFooterSpacer); |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 403 | } else { |
Matt Pietal | d507120 | 2019-10-10 11:21:28 -0400 | [diff] [blame] | 404 | View emptyView = findViewById(R.id.empty); |
| 405 | if (emptyView != null) { |
| 406 | emptyView.setPadding(0, 0, 0, mSystemWindowInsets.bottom |
| 407 | + getResources().getDimensionPixelSize( |
| 408 | R.dimen.chooser_edge_margin_normal) * 2); |
| 409 | } |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 410 | } |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 411 | |
Matt Pietal | 78fdbd4 | 2019-06-05 07:08:08 -0400 | [diff] [blame] | 412 | resetButtonBar(); |
| 413 | |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 414 | return insets.consumeSystemWindowInsets(); |
| 415 | } |
| 416 | |
Jiongxuan Gao | 3365b16 | 2016-11-24 20:20:27 +0900 | [diff] [blame] | 417 | @Override |
| 418 | public void onConfigurationChanged(Configuration newConfig) { |
| 419 | super.onConfigurationChanged(newConfig); |
| 420 | mAdapter.handlePackagesChanged(); |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 421 | |
| 422 | if (mSystemWindowInsets != null) { |
| 423 | mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top, |
| 424 | mSystemWindowInsets.right, 0); |
| 425 | } |
Jiongxuan Gao | 3365b16 | 2016-11-24 20:20:27 +0900 | [diff] [blame] | 426 | } |
| 427 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 428 | @Override // ResolverListCommunicator |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 429 | public void sendVoiceChoicesIfNeeded() { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 430 | if (!isVoiceInteraction()) { |
| 431 | // Clearly not needed. |
| 432 | return; |
| 433 | } |
| 434 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 435 | final Option[] options = new Option[mAdapter.getCount()]; |
| 436 | for (int i = 0, N = options.length; i < N; i++) { |
Matt Pietal | 2cf4c6c | 2019-09-10 09:33:11 -0400 | [diff] [blame] | 437 | TargetInfo target = mAdapter.getItem(i); |
| 438 | if (target == null) { |
| 439 | // If this occurs, a new set of targets is being loaded. Let that complete, |
| 440 | // and have the next call to send voice choices proceed instead. |
| 441 | return; |
| 442 | } |
| 443 | options[i] = optionForChooserTarget(target, i); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | mPickOptionRequest = new PickTargetOptionRequest( |
| 447 | new Prompt(getTitle()), options, null); |
| 448 | getVoiceInteractor().submitRequest(mPickOptionRequest); |
| 449 | } |
| 450 | |
| 451 | Option optionForChooserTarget(TargetInfo target, int index) { |
| 452 | return new Option(target.getDisplayLabel(), index); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 455 | protected final void setAdditionalTargets(Intent[] intents) { |
| 456 | if (intents != null) { |
| 457 | for (Intent intent : intents) { |
| 458 | mIntents.add(intent); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 463 | @Override // SelectableTargetInfoCommunicator ResolverListCommunicator |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 464 | public Intent getTargetIntent() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 465 | return mIntents.isEmpty() ? null : mIntents.get(0); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 468 | protected String getReferrerPackageName() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 469 | final Uri referrer = getReferrer(); |
| 470 | if (referrer != null && "android-app".equals(referrer.getScheme())) { |
| 471 | return referrer.getHost(); |
| 472 | } |
| 473 | return null; |
| 474 | } |
| 475 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 476 | public int getLayoutResource() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 477 | return R.layout.resolver_list; |
| 478 | } |
| 479 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 480 | @Override // ResolverListCommunicator |
| 481 | public void updateProfileViewButton() { |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 482 | if (mProfileView == null) { |
| 483 | return; |
| 484 | } |
| 485 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 486 | final DisplayResolveInfo dri = mAdapter.getOtherProfile(); |
| 487 | if (dri != null) { |
| 488 | mProfileView.setVisibility(View.VISIBLE); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 489 | View text = mProfileView.findViewById(R.id.profile_button); |
| 490 | if (!(text instanceof TextView)) { |
| 491 | text = mProfileView.findViewById(R.id.text1); |
| 492 | } |
| 493 | ((TextView) text).setText(dri.getDisplayLabel()); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 494 | } else { |
| 495 | mProfileView.setVisibility(View.GONE); |
| 496 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 499 | private void setProfileSwitchMessageId(int contentUserHint) { |
| 500 | if (contentUserHint != UserHandle.USER_CURRENT && |
| 501 | contentUserHint != UserHandle.myUserId()) { |
| 502 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 503 | UserInfo originUserInfo = userManager.getUserInfo(contentUserHint); |
| 504 | boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile() |
| 505 | : false; |
| 506 | boolean targetIsManaged = userManager.isManagedProfile(); |
| 507 | if (originIsManaged && !targetIsManaged) { |
| 508 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner; |
| 509 | } else if (!originIsManaged && targetIsManaged) { |
| 510 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 515 | /** |
| 516 | * Turn on launch mode that is safe to use when forwarding intents received from |
| 517 | * applications and running in system processes. This mode uses Activity.startActivityAsCaller |
| 518 | * instead of the normal Activity.startActivity for launching the activity selected |
| 519 | * by the user. |
| 520 | * |
| 521 | * <p>This mode is set to true by default if the activity is initialized through |
| 522 | * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate |
| 523 | * methods, it is set to false by default. You must set it before calling one of the |
| 524 | * more detailed onCreate methods, so that it will be set correctly in the case where |
| 525 | * there is only one intent to resolve and it is thus started immediately.</p> |
| 526 | */ |
| 527 | public void setSafeForwardingMode(boolean safeForwarding) { |
| 528 | mSafeForwardingMode = safeForwarding; |
| 529 | } |
| 530 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 531 | protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) { |
| 532 | final ActionTitle title = mResolvingHome |
| 533 | ? ActionTitle.HOME |
| 534 | : ActionTitle.forAction(intent.getAction()); |
| 535 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 536 | // While there may already be a filtered item, we can only use it in the title if the list |
| 537 | // is already sorted and all information relevant to it is already in the list. |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 538 | final boolean named = mAdapter.getFilteredPosition() >= 0; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 539 | if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) { |
| 540 | return getString(defaultTitleRes); |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 541 | } else if (isHttpSchemeAndViewAction(intent)) { |
| 542 | // 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] | 543 | // 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] | 544 | String dialogTitle = null; |
| 545 | if (named && !mUseLayoutForBrowsables) { |
| 546 | dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES, |
| 547 | mAdapter.getFilteredItem().getDisplayLabel()); |
| 548 | } else if (named && mUseLayoutForBrowsables) { |
| 549 | dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES, |
| 550 | intent.getData().getHost(), |
| 551 | mAdapter.getFilteredItem().getDisplayLabel()); |
| 552 | } else if (mAdapter.areAllTargetsBrowsers()) { |
Matt Pietal | d507120 | 2019-10-10 11:21:28 -0400 | [diff] [blame] | 553 | dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES); |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 554 | } else { |
| 555 | dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES, |
| 556 | intent.getData().getHost()); |
| 557 | } |
| 558 | return dialogTitle; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 559 | } else { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 560 | return named |
| 561 | ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel()) |
| 562 | : getString(title.titleRes); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 563 | } |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 566 | void dismiss() { |
| 567 | if (!isFinishing()) { |
| 568 | finish(); |
| 569 | } |
| 570 | } |
| 571 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 572 | @Override |
| 573 | protected void onRestart() { |
| 574 | super.onRestart(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 575 | if (!mRegistered) { |
| 576 | mPackageMonitor.register(this, getMainLooper(), false); |
| 577 | mRegistered = true; |
| 578 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 579 | mAdapter.handlePackagesChanged(); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 580 | updateProfileViewButton(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | @Override |
| 584 | protected void onStop() { |
| 585 | super.onStop(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 586 | if (mRegistered) { |
| 587 | mPackageMonitor.unregister(); |
| 588 | mRegistered = false; |
| 589 | } |
Wale Ogunwale | 9014e66 | 2016-03-19 14:55:46 -0700 | [diff] [blame] | 590 | final Intent intent = getIntent(); |
| 591 | if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction() |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 592 | && !mResolvingHome && !mRetainInOnStop) { |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 593 | // This resolver is in the unusual situation where it has been |
| 594 | // launched at the top of a new task. We don't let it be added |
| 595 | // to the recent tasks shown to the user, and we need to make sure |
| 596 | // that each time we are launched we get the correct launching |
| 597 | // uid (not re-using the same resolver from an old launching uid), |
| 598 | // so we will now finish ourself since being no longer visible, |
| 599 | // the user probably can't get back to us. |
| 600 | if (!isChangingConfigurations()) { |
| 601 | finish(); |
| 602 | } |
| 603 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 606 | @Override |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 607 | protected void onDestroy() { |
| 608 | super.onDestroy(); |
| 609 | if (!isChangingConfigurations() && mPickOptionRequest != null) { |
| 610 | mPickOptionRequest.cancel(); |
| 611 | } |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 612 | if (mAdapter != null) { |
| 613 | mAdapter.onDestroy(); |
Kang Li | 38a6da64 | 2017-04-05 12:30:55 -0700 | [diff] [blame] | 614 | } |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | @Override |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 618 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 619 | super.onRestoreInstanceState(savedInstanceState); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 620 | resetButtonBar(); |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 621 | } |
| 622 | |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 623 | private boolean isHttpSchemeAndViewAction(Intent intent) { |
| 624 | return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme()) |
| 625 | || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme())) |
| 626 | && Intent.ACTION_VIEW.equals(intent.getAction()); |
| 627 | } |
| 628 | |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 629 | private boolean hasManagedProfile() { |
| 630 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 631 | if (userManager == null) { |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | try { |
| 636 | List<UserInfo> profiles = userManager.getProfiles(getUserId()); |
| 637 | for (UserInfo userInfo : profiles) { |
| 638 | if (userInfo != null && userInfo.isManagedProfile()) { |
| 639 | return true; |
| 640 | } |
| 641 | } |
| 642 | } catch (SecurityException e) { |
| 643 | return false; |
| 644 | } |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | private boolean supportsManagedProfiles(ResolveInfo resolveInfo) { |
| 649 | try { |
| 650 | ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| 651 | resolveInfo.activityInfo.packageName, 0 /* default flags */); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 652 | return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 653 | } catch (NameNotFoundException e) { |
| 654 | return false; |
| 655 | } |
| 656 | } |
| 657 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 658 | private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos, |
| 659 | boolean filtered) { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 660 | boolean enabled = false; |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 661 | ResolveInfo ri = null; |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 662 | if (hasValidSelection) { |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 663 | ri = mAdapter.resolveInfoForPosition(checkedPos, filtered); |
Hakan Seyalioglu | 5dbc819 | 2017-02-24 16:16:37 -0800 | [diff] [blame] | 664 | if (ri == null) { |
| 665 | Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled"); |
| 666 | return; |
| 667 | } else if (ri.targetUserId != UserHandle.USER_CURRENT) { |
| 668 | Log.e(TAG, "Attempted to set selection to resolve info for another user"); |
| 669 | return; |
| 670 | } else { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 671 | enabled = true; |
| 672 | } |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 673 | if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) { |
| 674 | mAlwaysButton.setText(getResources() |
| 675 | .getString(R.string.activity_resolver_set_always)); |
| 676 | } else { |
| 677 | mAlwaysButton.setText(getResources() |
| 678 | .getString(R.string.activity_resolver_use_always)); |
| 679 | } |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 680 | } |
Paul McLean | 3a30e55 | 2019-12-07 14:47:29 -0700 | [diff] [blame] | 681 | |
| 682 | ActivityInfo activityInfo = ri.activityInfo; |
| 683 | |
| 684 | boolean hasRecordPermission = |
| 685 | PermissionChecker.checkPermissionForPreflight( |
| 686 | getApplicationContext(), |
| 687 | android.Manifest.permission.RECORD_AUDIO, -1, |
| 688 | activityInfo.applicationInfo.uid, |
| 689 | activityInfo.packageName) |
| 690 | == android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 691 | |
| 692 | if (!hasRecordPermission) { |
| 693 | // OK, we know the record permission, is this a capture device |
| 694 | boolean hasAudioCapture = |
| 695 | getIntent().getBooleanExtra( |
| 696 | ResolverActivity.EXTRA_IS_AUDIO_CAPTURE_DEVICE, false); |
| 697 | enabled = !hasAudioCapture; |
| 698 | } |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 699 | mAlwaysButton.setEnabled(enabled); |
| 700 | } |
| 701 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 702 | public void onButtonClick(View v) { |
| 703 | final int id = v.getId(); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 704 | int which = mAdapter.hasFilteredItem() |
| 705 | ? mAdapter.getFilteredPosition() |
| 706 | : mAdapterView.getCheckedItemPosition(); |
| 707 | boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem(); |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 708 | ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered); |
Matt Pietal | a310c1f | 2019-07-03 09:12:56 -0400 | [diff] [blame] | 709 | if (mUseLayoutForBrowsables |
| 710 | && !ri.handleAllWebDataURI && id == R.id.button_always) { |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 711 | showSettingsForSelected(ri); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 712 | } else { |
| 713 | startSelected(which, id == R.id.button_always, hasIndexBeenFiltered); |
| 714 | } |
| 715 | } |
| 716 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 717 | private void showSettingsForSelected(ResolveInfo ri) { |
shafik | 34064c3 | 2019-03-06 15:42:52 +0000 | [diff] [blame] | 718 | Intent intent = new Intent(); |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 719 | |
| 720 | final String packageName = ri.activityInfo.packageName; |
| 721 | Bundle showFragmentArgs = new Bundle(); |
| 722 | showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY); |
| 723 | showFragmentArgs.putString("package", packageName); |
| 724 | |
shafik | 34064c3 | 2019-03-06 15:42:52 +0000 | [diff] [blame] | 725 | // For regular apps, we open the Open by Default page |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 726 | intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS) |
| 727 | .setData(Uri.fromParts("package", packageName, null)) |
| 728 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT) |
| 729 | .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY) |
| 730 | .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs); |
| 731 | |
shafik | 34064c3 | 2019-03-06 15:42:52 +0000 | [diff] [blame] | 732 | startActivity(intent); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 735 | public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) { |
Amith Yamasani | 07cd351 | 2013-09-18 13:16:00 -0700 | [diff] [blame] | 736 | if (isFinishing()) { |
| 737 | return; |
| 738 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 739 | ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered); |
Sander Alewijnse | 86d35ba | 2015-02-04 15:14:53 +0000 | [diff] [blame] | 740 | if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) { |
| 741 | Toast.makeText(this, String.format(getResources().getString( |
| 742 | com.android.internal.R.string.activity_resolver_work_profiles_support), |
| 743 | ri.activityInfo.loadLabel(getPackageManager()).toString()), |
| 744 | Toast.LENGTH_LONG).show(); |
| 745 | return; |
| 746 | } |
| 747 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 748 | TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 749 | if (target == null) { |
| 750 | return; |
| 751 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 752 | if (onTargetSelected(target, always)) { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 753 | if (always && mSupportsAlwaysUseOption) { |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 754 | MetricsLogger.action( |
| 755 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 756 | } else if (mSupportsAlwaysUseOption) { |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 757 | MetricsLogger.action( |
| 758 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE); |
| 759 | } else { |
| 760 | MetricsLogger.action( |
| 761 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP); |
| 762 | } |
| 763 | MetricsLogger.action(this, mAdapter.hasFilteredItem() |
| 764 | ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED |
| 765 | : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 766 | finish(); |
| 767 | } |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 768 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 770 | /** |
| 771 | * Replace me in subclasses! |
| 772 | */ |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 773 | @Override // ResolverListCommunicator |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 774 | public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) { |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 775 | return defIntent; |
| 776 | } |
| 777 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 778 | @Override // ResolverListCommunicator |
| 779 | public void onPostListReady() { |
| 780 | setHeader(); |
| 781 | resetButtonBar(); |
| 782 | onListRebuilt(); |
| 783 | } |
| 784 | |
| 785 | protected void onListRebuilt() { |
| 786 | int count = mAdapter.getUnfilteredCount(); |
| 787 | if (count == 1 && mAdapter.getOtherProfile() == null) { |
| 788 | // Only one target, so we're a candidate to auto-launch! |
| 789 | final TargetInfo target = mAdapter.targetInfoForPosition(0, false); |
| 790 | if (shouldAutoLaunchSingleChoice(target)) { |
| 791 | safelyStartActivity(target); |
| 792 | finish(); |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 797 | protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 798 | final ResolveInfo ri = target.getResolveInfo(); |
| 799 | final Intent intent = target != null ? target.getResolvedIntent() : null; |
| 800 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 801 | if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem()) |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 802 | && mAdapter.mUnfilteredResolveList != null) { |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 803 | // Build a reasonable intent filter, based on what matched. |
| 804 | IntentFilter filter = new IntentFilter(); |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 805 | Intent filterIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 807 | if (intent.getSelector() != null) { |
| 808 | filterIntent = intent.getSelector(); |
| 809 | } else { |
| 810 | filterIntent = intent; |
| 811 | } |
| 812 | |
| 813 | String action = filterIntent.getAction(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 814 | if (action != null) { |
| 815 | filter.addAction(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | } |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 817 | Set<String> categories = filterIntent.getCategories(); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 818 | if (categories != null) { |
| 819 | for (String cat : categories) { |
| 820 | filter.addCategory(cat); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | } |
| 822 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 823 | filter.addCategory(Intent.CATEGORY_DEFAULT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 825 | int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK; |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 826 | Uri data = filterIntent.getData(); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 827 | if (cat == IntentFilter.MATCH_CATEGORY_TYPE) { |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 828 | String mimeType = filterIntent.resolveType(this); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 829 | if (mimeType != null) { |
| 830 | try { |
| 831 | filter.addDataType(mimeType); |
| 832 | } catch (IntentFilter.MalformedMimeTypeException e) { |
| 833 | Log.w("ResolverActivity", e); |
| 834 | filter = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | } |
| 836 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 837 | } |
| 838 | if (data != null && data.getScheme() != null) { |
| 839 | // We need the data specification if there was no type, |
| 840 | // OR if the scheme is not one of our magical "file:" |
| 841 | // or "content:" schemes (see IntentFilter for the reason). |
| 842 | if (cat != IntentFilter.MATCH_CATEGORY_TYPE |
| 843 | || (!"file".equals(data.getScheme()) |
| 844 | && !"content".equals(data.getScheme()))) { |
| 845 | filter.addDataScheme(data.getScheme()); |
| 846 | |
| 847 | // Look through the resolved filter to determine which part |
| 848 | // of it matched the original Intent. |
| 849 | Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator(); |
| 850 | if (pIt != null) { |
| 851 | String ssp = data.getSchemeSpecificPart(); |
| 852 | while (ssp != null && pIt.hasNext()) { |
| 853 | PatternMatcher p = pIt.next(); |
| 854 | if (p.match(ssp)) { |
| 855 | filter.addDataSchemeSpecificPart(p.getPath(), p.getType()); |
| 856 | break; |
| 857 | } |
Dianne Hackborn | df1c0bf | 2013-06-12 16:21:38 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 860 | Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator(); |
| 861 | if (aIt != null) { |
| 862 | while (aIt.hasNext()) { |
| 863 | IntentFilter.AuthorityEntry a = aIt.next(); |
| 864 | if (a.match(data) >= 0) { |
| 865 | int port = a.getPort(); |
| 866 | filter.addDataAuthority(a.getHost(), |
| 867 | port >= 0 ? Integer.toString(port) : null); |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | pIt = ri.filter.pathsIterator(); |
| 873 | if (pIt != null) { |
| 874 | String path = data.getPath(); |
| 875 | while (path != null && pIt.hasNext()) { |
| 876 | PatternMatcher p = pIt.next(); |
| 877 | if (p.match(path)) { |
| 878 | filter.addDataPath(p.getPath(), p.getType()); |
| 879 | break; |
| 880 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 886 | if (filter != null) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 887 | final int N = mAdapter.mUnfilteredResolveList.size(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 888 | ComponentName[] set; |
| 889 | // If we don't add back in the component for forwarding the intent to a managed |
| 890 | // profile, the preferred activity may not be updated correctly (as the set of |
| 891 | // components we tell it we knew about will have changed). |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 892 | final boolean needToAddBackProfileForwardingComponent = |
| 893 | mAdapter.getOtherProfile() != null; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 894 | if (!needToAddBackProfileForwardingComponent) { |
| 895 | set = new ComponentName[N]; |
| 896 | } else { |
| 897 | set = new ComponentName[N + 1]; |
| 898 | } |
| 899 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 900 | int bestMatch = 0; |
| 901 | for (int i=0; i<N; i++) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 902 | ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 903 | set[i] = new ComponentName(r.activityInfo.packageName, |
| 904 | r.activityInfo.name); |
| 905 | if (r.match > bestMatch) bestMatch = r.match; |
| 906 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 907 | |
| 908 | if (needToAddBackProfileForwardingComponent) { |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 909 | set[N] = mAdapter.getOtherProfile().getResolvedComponentName(); |
| 910 | final int otherProfileMatch = mAdapter.getOtherProfile().getResolveInfo().match; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 911 | if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch; |
| 912 | } |
| 913 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 914 | if (alwaysCheck) { |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 915 | final int userId = getUserId(); |
| 916 | final PackageManager pm = getPackageManager(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 917 | |
| 918 | // Set the preferred Activity |
| 919 | pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent()); |
| 920 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 921 | if (ri.handleAllWebDataURI) { |
| 922 | // Set default Browser if needed |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 923 | final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId); |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 924 | if (TextUtils.isEmpty(packageName)) { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 925 | pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId); |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 926 | } |
| 927 | } else { |
| 928 | // Update Domain Verification status |
| 929 | ComponentName cn = intent.getComponent(); |
| 930 | String packageName = cn.getPackageName(); |
| 931 | String dataScheme = (data != null) ? data.getScheme() : null; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 932 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 933 | boolean isHttpOrHttps = (dataScheme != null) && |
| 934 | (dataScheme.equals(IntentFilter.SCHEME_HTTP) || |
| 935 | dataScheme.equals(IntentFilter.SCHEME_HTTPS)); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 936 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 937 | boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW); |
| 938 | boolean hasCategoryBrowsable = (categories != null) && |
| 939 | categories.contains(Intent.CATEGORY_BROWSABLE); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 940 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 941 | if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 942 | pm.updateIntentVerificationStatusAsUser(packageName, |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 943 | PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS, |
| 944 | userId); |
| 945 | } |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 946 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 947 | } else { |
| 948 | try { |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 949 | mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 950 | } catch (RemoteException re) { |
| 951 | Log.d(TAG, "Error calling setLastChosenActivity\n" + re); |
| 952 | } |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 953 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 957 | if (target != null) { |
| 958 | safelyStartActivity(target); |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 959 | |
| 960 | // Rely on the ActivityManager to pop up a dialog regarding app suspension |
| 961 | // and return false |
| 962 | if (target.isSuspended()) { |
| 963 | return false; |
| 964 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 965 | } |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 966 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 967 | return true; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 970 | public void safelyStartActivity(TargetInfo cti) { |
Jeff Sharkey | 2b9eb89 | 2016-02-16 09:21:51 -0700 | [diff] [blame] | 971 | // We're dispatching intents that might be coming from legacy apps, so |
| 972 | // don't kill ourselves. |
| 973 | StrictMode.disableDeathOnFileUriExposure(); |
| 974 | try { |
| 975 | safelyStartActivityInternal(cti); |
| 976 | } finally { |
| 977 | StrictMode.enableDeathOnFileUriExposure(); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | private void safelyStartActivityInternal(TargetInfo cti) { |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 982 | // If needed, show that intent is forwarded |
| 983 | // from managed profile to owner or other way around. |
| 984 | if (mProfileSwitchMessageId != -1) { |
| 985 | Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show(); |
| 986 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 987 | if (!mSafeForwardingMode) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 988 | if (cti.start(this, null)) { |
| 989 | onActivityStarted(cti); |
| 990 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | try { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 994 | if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) { |
| 995 | onActivityStarted(cti); |
| 996 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 997 | } catch (RuntimeException e) { |
| 998 | String launchedFromPackage; |
| 999 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 1000 | launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage( |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1001 | getActivityToken()); |
| 1002 | } catch (RemoteException e2) { |
| 1003 | launchedFromPackage = "??"; |
| 1004 | } |
| 1005 | Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid |
| 1006 | + " package " + launchedFromPackage + ", while running in " |
| 1007 | + ActivityThread.currentProcessName(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1011 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1012 | public boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity, |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1013 | int userId) { |
| 1014 | // Pass intent to delegate chooser activity with permission token. |
| 1015 | // TODO: This should move to a trampoline Activity in the system when the ChooserActivity |
| 1016 | // moves into systemui |
| 1017 | try { |
| 1018 | // TODO: Once this is a small springboard activity, it can move off the UI process |
| 1019 | // and we can move the request method to ActivityManagerInternal. |
| 1020 | IBinder permissionToken = ActivityTaskManager.getService() |
| 1021 | .requestStartActivityPermissionToken(getActivityToken()); |
| 1022 | final Intent chooserIntent = new Intent(); |
| 1023 | final ComponentName delegateActivity = ComponentName.unflattenFromString( |
| 1024 | Resources.getSystem().getString(R.string.config_chooserActivity)); |
| 1025 | chooserIntent.setClassName(delegateActivity.getPackageName(), |
| 1026 | delegateActivity.getClassName()); |
| 1027 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken); |
| 1028 | |
| 1029 | // TODO: These extras will change as chooser activity moves into systemui |
| 1030 | chooserIntent.putExtra(Intent.EXTRA_INTENT, intent); |
| 1031 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options); |
| 1032 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY, |
| 1033 | ignoreTargetSecurity); |
| 1034 | chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId); |
Alison Cichowlas | 7b6f3b6 | 2018-12-07 09:06:57 -0500 | [diff] [blame] | 1035 | chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| 1036 | | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1037 | startActivity(chooserIntent); |
| 1038 | } catch (RemoteException e) { |
| 1039 | Log.e(TAG, e.toString()); |
| 1040 | } |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1044 | public void onActivityStarted(TargetInfo cti) { |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 1045 | // Do nothing |
| 1046 | } |
| 1047 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1048 | @Override // ResolverListCommunicator |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1049 | public boolean shouldGetActivityMetadata() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1050 | return false; |
| 1051 | } |
| 1052 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1053 | public boolean shouldAutoLaunchSingleChoice(TargetInfo target) { |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1054 | return !target.isSuspended(); |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1057 | public void showTargetDetails(ResolveInfo ri) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1058 | Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 1059 | .setData(Uri.fromParts("package", ri.activityInfo.packageName, null)) |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1060 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
Amith Yamasani | 203a2f4 | 2012-10-03 20:16:40 -0700 | [diff] [blame] | 1061 | startActivity(in); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1064 | public ResolverListAdapter createAdapter(Context context, List<Intent> payloadIntents, |
| 1065 | Intent[] initialIntents, List<ResolveInfo> rList, |
| 1066 | boolean filterLastUsed, boolean useLayoutForBrowsables) { |
Paul McLean | 07425c8 | 2019-10-18 12:00:11 -0600 | [diff] [blame] | 1067 | |
| 1068 | Intent startIntent = getIntent(); |
| 1069 | boolean isAudioCaptureDevice = |
| 1070 | startIntent.getBooleanExtra(EXTRA_IS_AUDIO_CAPTURE_DEVICE, false); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1071 | return new ResolverListAdapter(context, payloadIntents, initialIntents, rList, |
Paul McLean | 07425c8 | 2019-10-18 12:00:11 -0600 | [diff] [blame] | 1072 | filterLastUsed, createListController(), useLayoutForBrowsables, this, |
| 1073 | isAudioCaptureDevice); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | @VisibleForTesting |
| 1077 | protected ResolverListController createListController() { |
| 1078 | return new ResolverListController( |
| 1079 | this, |
| 1080 | mPm, |
| 1081 | getTargetIntent(), |
| 1082 | getReferrerPackageName(), |
| 1083 | mLaunchedFromUid); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1086 | /** |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1087 | * Sets up the content view. |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1088 | * @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] | 1089 | */ |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1090 | private boolean configureContentView() { |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1091 | if (mAdapter == null) { |
| 1092 | throw new IllegalStateException("mAdapter cannot be null."); |
| 1093 | } |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1094 | boolean rebuildCompleted = mAdapter.rebuildList(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1095 | if (useLayoutWithDefault()) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1096 | mLayoutId = R.layout.resolver_list_with_default; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1097 | } else { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1098 | mLayoutId = getLayoutResource(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1099 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1100 | setContentView(mLayoutId); |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1101 | return postRebuildList(rebuildCompleted); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1102 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1103 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1104 | /** |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1105 | * Finishing procedures to be performed after the list has been rebuilt. |
| 1106 | * </p>Subclasses must call postRebuildListInternal at the end of postRebuildList. |
| 1107 | * @param rebuildCompleted |
| 1108 | * @return <code>true</code> if the activity is finishing and creation should halt. |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1109 | */ |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1110 | protected boolean postRebuildList(boolean rebuildCompleted) { |
| 1111 | return postRebuildListInternal(rebuildCompleted); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /** |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1115 | * Finishing procedures to be performed after the list has been rebuilt. |
| 1116 | * @param rebuildCompleted |
| 1117 | * @return <code>true</code> if the activity is finishing and creation should halt. |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1118 | */ |
arangelov | bb57233 | 2019-11-13 12:50:08 +0000 | [diff] [blame] | 1119 | final boolean postRebuildListInternal(boolean rebuildCompleted) { |
| 1120 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1121 | int count = mAdapter.getUnfilteredCount(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1122 | |
| 1123 | // We only rebuild asynchronously when we have multiple elements to sort. In the case where |
| 1124 | // we're already done, we can check if we should auto-launch immediately. |
| 1125 | if (rebuildCompleted) { |
| 1126 | if (count == 1 && mAdapter.getOtherProfile() == null) { |
| 1127 | // Only one target, so we're a candidate to auto-launch! |
| 1128 | final TargetInfo target = mAdapter.targetInfoForPosition(0, false); |
| 1129 | if (shouldAutoLaunchSingleChoice(target)) { |
| 1130 | safelyStartActivity(target); |
| 1131 | mPackageMonitor.unregister(); |
| 1132 | mRegistered = false; |
| 1133 | finish(); |
| 1134 | return true; |
| 1135 | } |
Jeff Sharkey | cc2ae6b4 | 2015-09-29 13:04:46 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1138 | |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1139 | boolean isAdapterViewVisible = true; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1140 | if (count == 0 && mAdapter.getPlaceholderCount() == 0) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1141 | final TextView emptyView = findViewById(R.id.empty); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1142 | emptyView.setVisibility(View.VISIBLE); |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1143 | isAdapterViewVisible = false; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1144 | } |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1145 | |
| 1146 | onPrepareAdapterView(mAdapter, isAdapterViewVisible); |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1147 | return false; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1150 | /** |
| 1151 | * Prepare the scrollable view which consumes data in the list adapter. |
| 1152 | * @param adapter The adapter used to provide data to item views. |
| 1153 | * @param isVisible True if the scrollable view should be visible; false, otherwise. |
| 1154 | */ |
| 1155 | public void onPrepareAdapterView(ResolverListAdapter adapter, boolean isVisible) { |
| 1156 | mAdapterView = findViewById(R.id.resolver_list); |
| 1157 | if (!isVisible) { |
| 1158 | mAdapterView.setVisibility(View.GONE); |
| 1159 | return; |
| 1160 | } |
| 1161 | mAdapterView.setVisibility(View.VISIBLE); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1162 | final boolean useHeader = adapter.hasFilteredItem(); |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1163 | final ListView listView = mAdapterView instanceof ListView ? (ListView) mAdapterView : null; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1164 | |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1165 | mAdapterView.setAdapter(mAdapter); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | final ItemClickListener listener = new ItemClickListener(); |
Zhen Zhang | bde7b46 | 2019-11-11 11:49:33 -0800 | [diff] [blame] | 1168 | mAdapterView.setOnItemClickListener(listener); |
| 1169 | mAdapterView.setOnItemLongClickListener(listener); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1170 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1171 | if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1172 | listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); |
| 1173 | } |
| 1174 | |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1175 | // In case this method is called again (due to activity recreation), avoid adding a new |
| 1176 | // header if one is already present. |
| 1177 | if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) { |
Matt Pietal | d507120 | 2019-10-10 11:21:28 -0400 | [diff] [blame] | 1178 | listView.setHeaderDividersEnabled(true); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1179 | listView.addHeaderView(LayoutInflater.from(this).inflate( |
| 1180 | R.layout.resolver_different_item_header, listView, false)); |
| 1181 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1184 | /** |
| 1185 | * Configure the area above the app selection list (title, content preview, etc). |
| 1186 | */ |
| 1187 | public void setHeader() { |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1188 | if (mAdapter.getCount() == 0 && mAdapter.getPlaceholderCount() == 0) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1189 | final TextView titleView = findViewById(R.id.title); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1190 | if (titleView != null) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1191 | titleView.setVisibility(View.GONE); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1192 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | CharSequence title = mTitle != null |
| 1196 | ? mTitle |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1197 | : getTitleForAction(getTargetIntent(), mDefaultTitleResId); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1198 | |
| 1199 | if (!TextUtils.isEmpty(title)) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1200 | final TextView titleView = findViewById(R.id.title); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1201 | if (titleView != null) { |
| 1202 | titleView.setText(title); |
| 1203 | } |
| 1204 | setTitle(title); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1207 | final ImageView iconView = findViewById(R.id.icon); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1208 | if (iconView != null) { |
| 1209 | mAdapter.loadFilteredItemIconTaskAsync(iconView); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1213 | private void resetButtonBar() { |
| 1214 | if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) { |
| 1215 | return; |
| 1216 | } |
| 1217 | final ViewGroup buttonLayout = findViewById(R.id.button_bar); |
| 1218 | if (buttonLayout != null) { |
| 1219 | buttonLayout.setVisibility(View.VISIBLE); |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 1220 | |
Matt Pietal | d507120 | 2019-10-10 11:21:28 -0400 | [diff] [blame] | 1221 | if (!useLayoutWithDefault()) { |
| 1222 | int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0; |
| 1223 | buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(), |
| 1224 | buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize( |
| 1225 | R.dimen.resolver_button_bar_spacing) + inset); |
| 1226 | } |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1227 | mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1228 | mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always); |
| 1229 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1230 | resetAlwaysOrOnceButtonBar(); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1231 | } else { |
| 1232 | Log.e(TAG, "Layout unexpectedly does not have a button bar"); |
| 1233 | } |
| 1234 | } |
| 1235 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1236 | private void resetAlwaysOrOnceButtonBar() { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1237 | if (useLayoutWithDefault() |
| 1238 | && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) { |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1239 | setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false); |
| 1240 | mOnceButton.setEnabled(true); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1241 | return; |
| 1242 | } |
| 1243 | |
| 1244 | // When the items load in, if an item was already selected, enable the buttons |
| 1245 | if (mAdapterView != null |
| 1246 | && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) { |
| 1247 | setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true); |
| 1248 | mOnceButton.setEnabled(true); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1252 | @Override // ResolverListCommunicator |
| 1253 | public boolean useLayoutWithDefault() { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1254 | return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem(); |
| 1255 | } |
| 1256 | |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1257 | /** |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 1258 | * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets |
| 1259 | * called and we are launched in a new task. |
| 1260 | */ |
| 1261 | protected void setRetainInOnStop(boolean retainInOnStop) { |
| 1262 | mRetainInOnStop = retainInOnStop; |
| 1263 | } |
| 1264 | |
| 1265 | /** |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1266 | * Check a simple match for the component of two ResolveInfos. |
| 1267 | */ |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1268 | @Override // ResolverListCommunicator |
| 1269 | public boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) { |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1270 | return lhs == null ? rhs == null |
| 1271 | : lhs.activityInfo == null ? rhs.activityInfo == null |
| 1272 | : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name) |
| 1273 | && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName); |
| 1274 | } |
| 1275 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1276 | @Override // ResolverListCommunicator |
| 1277 | public void onHandlePackagesChanged() { |
| 1278 | if (mAdapter.getCount() == 0) { |
| 1279 | // We no longer have any items... just finish the activity. |
| 1280 | finish(); |
Zhen Zhang | 55d66aa | 2019-10-14 17:11:19 -0700 | [diff] [blame] | 1281 | } |
Zhen Zhang | 55d66aa | 2019-10-14 17:11:19 -0700 | [diff] [blame] | 1282 | } |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1283 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1284 | @VisibleForTesting |
| 1285 | public static final class ResolvedComponentInfo { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1286 | public final ComponentName name; |
| 1287 | private final List<Intent> mIntents = new ArrayList<>(); |
| 1288 | private final List<ResolveInfo> mResolveInfos = new ArrayList<>(); |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 1289 | private boolean mPinned; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1290 | |
| 1291 | public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) { |
| 1292 | this.name = name; |
| 1293 | add(intent, info); |
| 1294 | } |
| 1295 | |
| 1296 | public void add(Intent intent, ResolveInfo info) { |
| 1297 | mIntents.add(intent); |
| 1298 | mResolveInfos.add(info); |
| 1299 | } |
| 1300 | |
| 1301 | public int getCount() { |
| 1302 | return mIntents.size(); |
| 1303 | } |
| 1304 | |
| 1305 | public Intent getIntentAt(int index) { |
| 1306 | return index >= 0 ? mIntents.get(index) : null; |
| 1307 | } |
| 1308 | |
| 1309 | public ResolveInfo getResolveInfoAt(int index) { |
| 1310 | return index >= 0 ? mResolveInfos.get(index) : null; |
| 1311 | } |
| 1312 | |
| 1313 | public int findIntent(Intent intent) { |
| 1314 | for (int i = 0, N = mIntents.size(); i < N; i++) { |
| 1315 | if (intent.equals(mIntents.get(i))) { |
| 1316 | return i; |
| 1317 | } |
| 1318 | } |
| 1319 | return -1; |
| 1320 | } |
| 1321 | |
| 1322 | public int findResolveInfo(ResolveInfo info) { |
| 1323 | for (int i = 0, N = mResolveInfos.size(); i < N; i++) { |
| 1324 | if (info.equals(mResolveInfos.get(i))) { |
| 1325 | return i; |
| 1326 | } |
| 1327 | } |
| 1328 | return -1; |
| 1329 | } |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 1330 | |
| 1331 | public boolean isPinned() { |
| 1332 | return mPinned; |
| 1333 | } |
| 1334 | |
| 1335 | public void setPinned(boolean pinned) { |
| 1336 | mPinned = pinned; |
| 1337 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1340 | class ItemClickListener implements AdapterView.OnItemClickListener, |
| 1341 | AdapterView.OnItemLongClickListener { |
| 1342 | @Override |
| 1343 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 1344 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 1345 | if (listView != null) { |
| 1346 | position -= listView.getHeaderViewsCount(); |
| 1347 | } |
| 1348 | if (position < 0) { |
| 1349 | // Header views don't count. |
| 1350 | return; |
| 1351 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1352 | // If we're still loading, we can't yet enable the buttons. |
| 1353 | if (mAdapter.resolveInfoForPosition(position, true) == null) { |
| 1354 | return; |
| 1355 | } |
| 1356 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1357 | final int checkedPos = mAdapterView.getCheckedItemPosition(); |
| 1358 | final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1359 | if (!useLayoutWithDefault() |
Ruchi Kandoi | 65d76c1 | 2017-08-18 13:05:39 -0700 | [diff] [blame] | 1360 | && (!hasValidSelection || mLastSelected != checkedPos) |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1361 | && mAlwaysButton != null) { |
| 1362 | setAlwaysButtonEnabled(hasValidSelection, checkedPos, true); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1363 | mOnceButton.setEnabled(hasValidSelection); |
| 1364 | if (hasValidSelection) { |
| 1365 | mAdapterView.smoothScrollToPosition(checkedPos); |
| 1366 | } |
| 1367 | mLastSelected = checkedPos; |
| 1368 | } else { |
| 1369 | startSelected(position, false, true); |
| 1370 | } |
| 1371 | } |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 1372 | |
| 1373 | @Override |
| 1374 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1375 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 1376 | if (listView != null) { |
| 1377 | position -= listView.getHeaderViewsCount(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1378 | } |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 1379 | if (position < 0) { |
| 1380 | // Header views don't count. |
| 1381 | return false; |
| 1382 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1383 | ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1384 | showTargetDetails(ri); |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 1385 | return true; |
| 1386 | } |
| 1387 | |
| 1388 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1389 | |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 1390 | static final boolean isSpecificUriMatch(int match) { |
| 1391 | match = match&IntentFilter.MATCH_CATEGORY_MASK; |
| 1392 | return match >= IntentFilter.MATCH_CATEGORY_HOST |
| 1393 | && match <= IntentFilter.MATCH_CATEGORY_PATH; |
| 1394 | } |
| 1395 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 1396 | static class PickTargetOptionRequest extends PickOptionRequest { |
| 1397 | public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options, |
| 1398 | @Nullable Bundle extras) { |
| 1399 | super(prompt, options, extras); |
| 1400 | } |
| 1401 | |
| 1402 | @Override |
| 1403 | public void onCancel() { |
| 1404 | super.onCancel(); |
| 1405 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 1406 | if (ra != null) { |
| 1407 | ra.mPickOptionRequest = null; |
| 1408 | ra.finish(); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | @Override |
| 1413 | public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) { |
| 1414 | super.onPickOptionResult(finished, selections, result); |
| 1415 | if (selections.length != 1) { |
| 1416 | // TODO In a better world we would filter the UI presented here and let the |
| 1417 | // user refine. Maybe later. |
| 1418 | return; |
| 1419 | } |
| 1420 | |
| 1421 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 1422 | if (ra != null) { |
| 1423 | final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex()); |
| 1424 | if (ra.onTargetSelected(ti, false)) { |
| 1425 | ra.mPickOptionRequest = null; |
| 1426 | ra.finish(); |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | } |