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