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; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 27 | import android.app.ActivityTaskManager; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 28 | import android.app.ActivityThread; |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 29 | import android.app.VoiceInteractor.PickOptionRequest; |
| 30 | import android.app.VoiceInteractor.PickOptionRequest.Option; |
| 31 | import android.app.VoiceInteractor.Prompt; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.ComponentName; |
| 33 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
| 36 | import android.content.pm.ActivityInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 37 | import android.content.pm.ApplicationInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 38 | import android.content.pm.LabeledIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 40 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.pm.ResolveInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 42 | import android.content.pm.UserInfo; |
Jiongxuan Gao | 3365b16 | 2016-11-24 20:20:27 +0900 | [diff] [blame] | 43 | import android.content.res.Configuration; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 44 | import android.content.res.Resources; |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 45 | import android.graphics.Bitmap; |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 46 | import android.graphics.ColorMatrix; |
| 47 | import android.graphics.ColorMatrixColorFilter; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 48 | import android.graphics.Insets; |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 49 | import android.graphics.drawable.BitmapDrawable; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 50 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.net.Uri; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 52 | import android.os.AsyncTask; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 53 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.Bundle; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 55 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.os.PatternMatcher; |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 57 | import android.os.Process; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 58 | import android.os.RemoteException; |
Jeff Sharkey | 37355a9 | 2016-02-05 16:19:10 -0700 | [diff] [blame] | 59 | import android.os.StrictMode; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 60 | import android.os.UserHandle; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 61 | import android.os.UserManager; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 62 | import android.provider.MediaStore; |
| 63 | import android.provider.Settings; |
| 64 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.util.Log; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 66 | import android.util.Slog; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 67 | import android.view.LayoutInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.view.View; |
| 69 | import android.view.ViewGroup; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 70 | import android.view.ViewGroup.LayoutParams; |
| 71 | import android.view.WindowInsets; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 72 | import android.widget.AbsListView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 73 | import android.widget.AdapterView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import android.widget.BaseAdapter; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 75 | import android.widget.Button; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.widget.ImageView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 77 | import android.widget.ListView; |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 78 | import android.widget.Space; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import android.widget.TextView; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 80 | import android.widget.Toast; |
Hyunyoung Song | 16b7af4 | 2018-10-05 14:00:42 -0700 | [diff] [blame] | 81 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 82 | import com.android.internal.R; |
| 83 | import com.android.internal.annotations.VisibleForTesting; |
| 84 | import com.android.internal.content.PackageMonitor; |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 85 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 86 | import com.android.internal.logging.nano.MetricsProto; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 87 | import com.android.internal.widget.ResolverDrawerLayout; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import java.util.ArrayList; |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 90 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import java.util.Iterator; |
| 92 | import java.util.List; |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 93 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import java.util.Set; |
| 95 | |
| 96 | /** |
| 97 | * This activity is displayed when the system attempts to start an Intent for |
| 98 | * which there is more than one matching activity, allowing the user to decide |
| 99 | * which to go to. It is not normally used directly by application developers. |
| 100 | */ |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 101 | @UiThread |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 102 | public class ResolverActivity extends Activity { |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 103 | |
Alison Cichowlas | 4691ed4 | 2018-11-13 15:59:55 -0500 | [diff] [blame] | 104 | // Temporary flag for new chooser delegate behavior. |
Alison Cichowlas | 7b6f3b6 | 2018-12-07 09:06:57 -0500 | [diff] [blame] | 105 | boolean mEnableChooserDelegate = true; |
Alison Cichowlas | 4691ed4 | 2018-11-13 15:59:55 -0500 | [diff] [blame] | 106 | |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 107 | @UnsupportedAppUsage |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 108 | protected ResolveListAdapter mAdapter; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 109 | private boolean mSafeForwardingMode; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 110 | protected AbsListView mAdapterView; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 111 | private Button mAlwaysButton; |
| 112 | private Button mOnceButton; |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 113 | protected View mProfileView; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 114 | private int mIconDpi; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 115 | private int mLastSelected = AbsListView.INVALID_POSITION; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 116 | private boolean mResolvingHome = false; |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 117 | private int mProfileSwitchMessageId = -1; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 118 | private int mLayoutId; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 119 | private final ArrayList<Intent> mIntents = new ArrayList<>(); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 120 | private PickTargetOptionRequest mPickOptionRequest; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 121 | private String mReferrerPackage; |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 122 | private CharSequence mTitle; |
| 123 | private int mDefaultTitleResId; |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 124 | private boolean mUseLayoutForBrowsables; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 125 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 126 | // Whether or not this activity supports choosing a default handler for the intent. |
| 127 | private boolean mSupportsAlwaysUseOption; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 128 | protected ResolverDrawerLayout mResolverDrawerLayout; |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 129 | @UnsupportedAppUsage |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 130 | protected PackageManager mPm; |
| 131 | protected int mLaunchedFromUid; |
| 132 | |
| 133 | private static final String TAG = "ResolverActivity"; |
| 134 | private static final boolean DEBUG = false; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 135 | private Runnable mPostListReadyRunnable; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 136 | |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 137 | private boolean mRegistered; |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 138 | |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 139 | private ColorMatrixColorFilter mSuspendedMatrixColorFilter; |
| 140 | |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 141 | protected Insets mSystemWindowInsets = null; |
| 142 | private Space mFooterSpacer = null; |
| 143 | |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 144 | /** See {@link #setRetainInOnStop}. */ |
| 145 | private boolean mRetainInOnStop; |
| 146 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 147 | private static final String EXTRA_SHOW_FRAGMENT_ARGS = ":settings:show_fragment_args"; |
| 148 | private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key"; |
| 149 | private static final String OPEN_LINKS_COMPONENT_KEY = "app_link_state"; |
| 150 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 151 | private final PackageMonitor mPackageMonitor = createPackageMonitor(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 152 | |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Get the string resource to be used as a label for the link to the resolver activity for an |
| 155 | * action. |
| 156 | * |
| 157 | * @param action The action to resolve |
| 158 | * |
| 159 | * @return The string resource to be used as a label |
| 160 | */ |
| 161 | public static @StringRes int getLabelRes(String action) { |
| 162 | return ActionTitle.forAction(action).labelRes; |
| 163 | } |
| 164 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 165 | private enum ActionTitle { |
| 166 | VIEW(Intent.ACTION_VIEW, |
| 167 | com.android.internal.R.string.whichViewApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 168 | com.android.internal.R.string.whichViewApplicationNamed, |
| 169 | com.android.internal.R.string.whichViewApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 170 | EDIT(Intent.ACTION_EDIT, |
| 171 | com.android.internal.R.string.whichEditApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 172 | com.android.internal.R.string.whichEditApplicationNamed, |
| 173 | com.android.internal.R.string.whichEditApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 174 | SEND(Intent.ACTION_SEND, |
| 175 | com.android.internal.R.string.whichSendApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 176 | com.android.internal.R.string.whichSendApplicationNamed, |
| 177 | com.android.internal.R.string.whichSendApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 178 | SENDTO(Intent.ACTION_SENDTO, |
Adam Powell | 13ea8f4 | 2016-03-18 09:39:41 -0700 | [diff] [blame] | 179 | com.android.internal.R.string.whichSendToApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 180 | com.android.internal.R.string.whichSendToApplicationNamed, |
| 181 | com.android.internal.R.string.whichSendToApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 182 | SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE, |
| 183 | com.android.internal.R.string.whichSendApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 184 | com.android.internal.R.string.whichSendApplicationNamed, |
| 185 | com.android.internal.R.string.whichSendApplicationLabel), |
| 186 | CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE, |
| 187 | com.android.internal.R.string.whichImageCaptureApplication, |
| 188 | com.android.internal.R.string.whichImageCaptureApplicationNamed, |
| 189 | com.android.internal.R.string.whichImageCaptureApplicationLabel), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 190 | DEFAULT(null, |
| 191 | com.android.internal.R.string.whichApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 192 | com.android.internal.R.string.whichApplicationNamed, |
| 193 | com.android.internal.R.string.whichApplicationLabel), |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 194 | HOME(Intent.ACTION_MAIN, |
| 195 | com.android.internal.R.string.whichHomeApplication, |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 196 | com.android.internal.R.string.whichHomeApplicationNamed, |
| 197 | com.android.internal.R.string.whichHomeApplicationLabel); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 198 | |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 199 | // titles for layout that deals with http(s) intents |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 200 | public static final int BROWSABLE_TITLE_RES = |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 201 | com.android.internal.R.string.whichOpenLinksWith; |
| 202 | public static final int BROWSABLE_HOST_TITLE_RES = |
| 203 | com.android.internal.R.string.whichOpenHostLinksWith; |
| 204 | public static final int BROWSABLE_HOST_APP_TITLE_RES = |
| 205 | com.android.internal.R.string.whichOpenHostLinksWithApp; |
| 206 | public static final int BROWSABLE_APP_TITLE_RES = |
| 207 | com.android.internal.R.string.whichOpenLinksWithApp; |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 208 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 209 | public final String action; |
| 210 | public final int titleRes; |
| 211 | public final int namedTitleRes; |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 212 | public final @StringRes int labelRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 213 | |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 214 | ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 215 | this.action = action; |
| 216 | this.titleRes = titleRes; |
| 217 | this.namedTitleRes = namedTitleRes; |
Philip P. Moltmann | f8173ca | 2016-04-12 15:11:23 -0700 | [diff] [blame] | 218 | this.labelRes = labelRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | public static ActionTitle forAction(String action) { |
| 222 | for (ActionTitle title : values()) { |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 223 | if (title != HOME && action != null && action.equals(title.action)) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 224 | return title; |
| 225 | } |
| 226 | } |
| 227 | return DEFAULT; |
| 228 | } |
| 229 | } |
| 230 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 231 | protected PackageMonitor createPackageMonitor() { |
| 232 | return new PackageMonitor() { |
| 233 | @Override |
| 234 | public void onSomePackagesChanged() { |
| 235 | mAdapter.handlePackagesChanged(); |
| 236 | bindProfileView(); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public boolean onPackageChanged(String packageName, int uid, String[] components) { |
| 241 | // We care about all package changes, not just the whole package itself which is |
| 242 | // default behavior. |
| 243 | return true; |
| 244 | } |
| 245 | }; |
| 246 | } |
| 247 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 248 | private Intent makeMyIntent() { |
| 249 | Intent intent = new Intent(getIntent()); |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 250 | intent.setComponent(null); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 251 | // The resolver activity is set to be hidden from recent tasks. |
| 252 | // we don't want this attribute to be propagated to the next activity |
| 253 | // being launched. Note that if the original Intent also had this |
| 254 | // flag set, we are now losing it. That should be a very rare case |
| 255 | // and we can live with this. |
| 256 | intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 257 | return intent; |
| 258 | } |
| 259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | @Override |
| 261 | protected void onCreate(Bundle savedInstanceState) { |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 262 | // Use a specialized prompt when we're handling the 'Home' app startActivity() |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 263 | final Intent intent = makeMyIntent(); |
| 264 | final Set<String> categories = intent.getCategories(); |
| 265 | if (Intent.ACTION_MAIN.equals(intent.getAction()) |
| 266 | && categories != null |
| 267 | && categories.size() == 1 |
| 268 | && categories.contains(Intent.CATEGORY_HOME)) { |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 269 | // Note: this field is not set to true in the compatibility version. |
| 270 | mResolvingHome = true; |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 273 | setSafeForwardingMode(true); |
| 274 | |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 275 | onCreate(savedInstanceState, intent, null, 0, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 278 | /** |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 279 | * Compatibility version for other bundled services that use this overload without |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 280 | * a default title resource |
| 281 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 282 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 284 | CharSequence title, Intent[] initialIntents, |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 285 | List<ResolveInfo> rList, boolean supportsAlwaysUseOption) { |
| 286 | onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, |
| 287 | supportsAlwaysUseOption); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
| 291 | CharSequence title, int defaultTitleRes, Intent[] initialIntents, |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 292 | List<ResolveInfo> rList, boolean supportsAlwaysUseOption) { |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 293 | setTheme(R.style.Theme_DeviceDefault_Resolver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | super.onCreate(savedInstanceState); |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 295 | |
| 296 | // Determine whether we should show that intent is forwarded |
| 297 | // from managed profile to owner or other way around. |
| 298 | setProfileSwitchMessageId(intent.getContentUserHint()); |
| 299 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 300 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 301 | mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid( |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 302 | getActivityToken()); |
| 303 | } catch (RemoteException e) { |
| 304 | mLaunchedFromUid = -1; |
| 305 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 306 | |
| 307 | if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) { |
| 308 | // Gulp! |
| 309 | finish(); |
| 310 | return; |
| 311 | } |
| 312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | mPm = getPackageManager(); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 314 | |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 315 | mPackageMonitor.register(this, getMainLooper(), false); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 316 | mRegistered = true; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 317 | mReferrerPackage = getReferrerPackageName(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 318 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 319 | final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); |
| 320 | mIconDpi = am.getLauncherLargeIconDensity(); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 321 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 322 | // 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] | 323 | mIntents.add(0, new Intent(intent)); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 324 | mTitle = title; |
| 325 | mDefaultTitleResId = defaultTitleRes; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 326 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 327 | mUseLayoutForBrowsables = getTargetIntent() == null |
| 328 | ? false |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 329 | : isHttpSchemeAndViewAction(getTargetIntent()); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 330 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 331 | mSupportsAlwaysUseOption = supportsAlwaysUseOption; |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 332 | |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 333 | if (configureContentView(mIntents, initialIntents, rList)) { |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 334 | return; |
| 335 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 336 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 337 | final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel); |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 338 | if (rdl != null) { |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 339 | rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 340 | @Override |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 341 | public void onDismissed() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 342 | finish(); |
| 343 | } |
| 344 | }); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 345 | if (isVoiceInteraction()) { |
| 346 | rdl.setCollapsed(false); |
| 347 | } |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 348 | |
| 349 | rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 350 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); |
| 351 | rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets); |
| 352 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 353 | mResolverDrawerLayout = rdl; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 356 | mProfileView = findViewById(R.id.profile_button); |
| 357 | if (mProfileView != null) { |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 358 | mProfileView.setOnClickListener(this::onProfileClick); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 359 | bindProfileView(); |
| 360 | } |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 361 | |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 362 | initSuspendedColorMatrix(); |
| 363 | |
Clara Bayarri | 9550f5d | 2016-05-11 11:20:13 +0100 | [diff] [blame] | 364 | final Set<String> categories = intent.getCategories(); |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 365 | MetricsLogger.action(this, mAdapter.hasFilteredItem() |
| 366 | ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED |
| 367 | : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED, |
| 368 | intent.getAction() + ":" + intent.getType() + ":" |
Clara Bayarri | 9550f5d | 2016-05-11 11:20:13 +0100 | [diff] [blame] | 369 | + (categories != null ? Arrays.toString(categories.toArray()) : "")); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 372 | protected void onProfileClick(View v) { |
| 373 | final DisplayResolveInfo dri = mAdapter.getOtherProfile(); |
| 374 | if (dri == null) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | // Do not show the profile switch message anymore. |
| 379 | mProfileSwitchMessageId = -1; |
| 380 | |
| 381 | onTargetSelected(dri, false); |
| 382 | finish(); |
| 383 | } |
| 384 | |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 385 | protected WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { |
| 386 | mSystemWindowInsets = insets.getSystemWindowInsets(); |
| 387 | |
| 388 | mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top, |
| 389 | mSystemWindowInsets.right, 0); |
| 390 | |
| 391 | View emptyView = findViewById(R.id.empty); |
Matt Pietal | 32922ca | 2019-05-29 07:39:21 -0400 | [diff] [blame] | 392 | if (emptyView != null) { |
| 393 | emptyView.setPadding(0, 0, 0, mSystemWindowInsets.bottom |
| 394 | + getResources().getDimensionPixelSize( |
| 395 | R.dimen.chooser_edge_margin_normal) * 2); |
| 396 | } |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 397 | |
| 398 | if (mFooterSpacer == null) { |
| 399 | mFooterSpacer = new Space(getApplicationContext()); |
| 400 | } else { |
| 401 | ((ListView) mAdapterView).removeFooterView(mFooterSpacer); |
| 402 | } |
| 403 | mFooterSpacer.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, |
| 404 | mSystemWindowInsets.bottom)); |
| 405 | ((ListView) mAdapterView).addFooterView(mFooterSpacer); |
| 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 | |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 423 | private void initSuspendedColorMatrix() { |
| 424 | int grayValue = 127; |
| 425 | float scale = 0.5f; // half bright |
| 426 | |
| 427 | ColorMatrix tempBrightnessMatrix = new ColorMatrix(); |
| 428 | float[] mat = tempBrightnessMatrix.getArray(); |
| 429 | mat[0] = scale; |
| 430 | mat[6] = scale; |
| 431 | mat[12] = scale; |
| 432 | mat[4] = grayValue; |
| 433 | mat[9] = grayValue; |
| 434 | mat[14] = grayValue; |
| 435 | |
| 436 | ColorMatrix matrix = new ColorMatrix(); |
| 437 | matrix.setSaturation(0.0f); |
| 438 | matrix.preConcat(tempBrightnessMatrix); |
| 439 | mSuspendedMatrixColorFilter = new ColorMatrixColorFilter(matrix); |
| 440 | } |
| 441 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 442 | public void sendVoiceChoicesIfNeeded() { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 443 | if (!isVoiceInteraction()) { |
| 444 | // Clearly not needed. |
| 445 | return; |
| 446 | } |
| 447 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 448 | final Option[] options = new Option[mAdapter.getCount()]; |
| 449 | for (int i = 0, N = options.length; i < N; i++) { |
Matt Pietal | 553d8a4 | 2019-09-10 09:33:11 -0400 | [diff] [blame] | 450 | TargetInfo target = mAdapter.getItem(i); |
| 451 | if (target == null) { |
| 452 | // If this occurs, a new set of targets is being loaded. Let that complete, |
| 453 | // and have the next call to send voice choices proceed instead. |
| 454 | return; |
| 455 | } |
| 456 | options[i] = optionForChooserTarget(target, i); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | mPickOptionRequest = new PickTargetOptionRequest( |
| 460 | new Prompt(getTitle()), options, null); |
| 461 | getVoiceInteractor().submitRequest(mPickOptionRequest); |
| 462 | } |
| 463 | |
| 464 | Option optionForChooserTarget(TargetInfo target, int index) { |
| 465 | return new Option(target.getDisplayLabel(), index); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 468 | protected final void setAdditionalTargets(Intent[] intents) { |
| 469 | if (intents != null) { |
| 470 | for (Intent intent : intents) { |
| 471 | mIntents.add(intent); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 476 | public Intent getTargetIntent() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 477 | return mIntents.isEmpty() ? null : mIntents.get(0); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 480 | protected String getReferrerPackageName() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 481 | final Uri referrer = getReferrer(); |
| 482 | if (referrer != null && "android-app".equals(referrer.getScheme())) { |
| 483 | return referrer.getHost(); |
| 484 | } |
| 485 | return null; |
| 486 | } |
| 487 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 488 | public int getLayoutResource() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 489 | return R.layout.resolver_list; |
| 490 | } |
| 491 | |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 492 | protected void bindProfileView() { |
| 493 | if (mProfileView == null) { |
| 494 | return; |
| 495 | } |
| 496 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 497 | final DisplayResolveInfo dri = mAdapter.getOtherProfile(); |
| 498 | if (dri != null) { |
| 499 | mProfileView.setVisibility(View.VISIBLE); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 500 | View text = mProfileView.findViewById(R.id.profile_button); |
| 501 | if (!(text instanceof TextView)) { |
| 502 | text = mProfileView.findViewById(R.id.text1); |
| 503 | } |
| 504 | ((TextView) text).setText(dri.getDisplayLabel()); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 505 | } else { |
| 506 | mProfileView.setVisibility(View.GONE); |
| 507 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 510 | private void setProfileSwitchMessageId(int contentUserHint) { |
| 511 | if (contentUserHint != UserHandle.USER_CURRENT && |
| 512 | contentUserHint != UserHandle.myUserId()) { |
| 513 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 514 | UserInfo originUserInfo = userManager.getUserInfo(contentUserHint); |
| 515 | boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile() |
| 516 | : false; |
| 517 | boolean targetIsManaged = userManager.isManagedProfile(); |
| 518 | if (originIsManaged && !targetIsManaged) { |
| 519 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner; |
| 520 | } else if (!originIsManaged && targetIsManaged) { |
| 521 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 526 | /** |
| 527 | * Turn on launch mode that is safe to use when forwarding intents received from |
| 528 | * applications and running in system processes. This mode uses Activity.startActivityAsCaller |
| 529 | * instead of the normal Activity.startActivity for launching the activity selected |
| 530 | * by the user. |
| 531 | * |
| 532 | * <p>This mode is set to true by default if the activity is initialized through |
| 533 | * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate |
| 534 | * methods, it is set to false by default. You must set it before calling one of the |
| 535 | * more detailed onCreate methods, so that it will be set correctly in the case where |
| 536 | * there is only one intent to resolve and it is thus started immediately.</p> |
| 537 | */ |
| 538 | public void setSafeForwardingMode(boolean safeForwarding) { |
| 539 | mSafeForwardingMode = safeForwarding; |
| 540 | } |
| 541 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 542 | protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) { |
| 543 | final ActionTitle title = mResolvingHome |
| 544 | ? ActionTitle.HOME |
| 545 | : ActionTitle.forAction(intent.getAction()); |
| 546 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 547 | // While there may already be a filtered item, we can only use it in the title if the list |
| 548 | // 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] | 549 | final boolean named = mAdapter.getFilteredPosition() >= 0; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 550 | if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) { |
| 551 | return getString(defaultTitleRes); |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 552 | } else if (isHttpSchemeAndViewAction(intent)) { |
| 553 | // 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] | 554 | // 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] | 555 | String dialogTitle = null; |
| 556 | if (named && !mUseLayoutForBrowsables) { |
| 557 | dialogTitle = getString(ActionTitle.BROWSABLE_APP_TITLE_RES, |
| 558 | mAdapter.getFilteredItem().getDisplayLabel()); |
| 559 | } else if (named && mUseLayoutForBrowsables) { |
| 560 | dialogTitle = getString(ActionTitle.BROWSABLE_HOST_APP_TITLE_RES, |
| 561 | intent.getData().getHost(), |
| 562 | mAdapter.getFilteredItem().getDisplayLabel()); |
| 563 | } else if (mAdapter.areAllTargetsBrowsers()) { |
| 564 | dialogTitle = getString(ActionTitle.BROWSABLE_TITLE_RES); |
| 565 | } else { |
| 566 | dialogTitle = getString(ActionTitle.BROWSABLE_HOST_TITLE_RES, |
| 567 | intent.getData().getHost()); |
| 568 | } |
| 569 | return dialogTitle; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 570 | } else { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 571 | return named |
| 572 | ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel()) |
| 573 | : getString(title.titleRes); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 574 | } |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 577 | void dismiss() { |
| 578 | if (!isFinishing()) { |
| 579 | finish(); |
| 580 | } |
| 581 | } |
| 582 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 583 | |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 584 | /** |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 585 | * Loads the icon and label for the provided ApplicationInfo. Defaults to using the application |
| 586 | * icon and label over any IntentFilter or Activity icon to increase user understanding, with an |
| 587 | * exception for applications that hold the right permission. Always attempts to use available |
| 588 | * resources over PackageManager loading mechanisms so badging can be done by iconloader. Uses |
| 589 | * Strings to strip creative formatting. |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 590 | */ |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 591 | private abstract static class TargetPresentationGetter { |
| 592 | @Nullable abstract Drawable getIconSubstituteInternal(); |
| 593 | @Nullable abstract String getAppSubLabelInternal(); |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 594 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 595 | private Context mCtx; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 596 | private final int mIconDpi; |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 597 | private final boolean mHasSubstitutePermission; |
Mike Digman | c0ef554 | 2019-04-01 15:36:25 -0700 | [diff] [blame] | 598 | private final ApplicationInfo mAi; |
| 599 | |
| 600 | protected PackageManager mPm; |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 601 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 602 | TargetPresentationGetter(Context ctx, int iconDpi, ApplicationInfo ai) { |
| 603 | mCtx = ctx; |
| 604 | mPm = ctx.getPackageManager(); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 605 | mAi = ai; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 606 | mIconDpi = iconDpi; |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 607 | mHasSubstitutePermission = PackageManager.PERMISSION_GRANTED == mPm.checkPermission( |
| 608 | android.Manifest.permission.SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON, |
| 609 | mAi.packageName); |
| 610 | } |
| 611 | |
Mike Digman | b2e5e71 | 2019-04-19 15:49:10 -0700 | [diff] [blame] | 612 | public Drawable getIcon(UserHandle userHandle) { |
| 613 | return new BitmapDrawable(mCtx.getResources(), getIconBitmap(userHandle)); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Mike Digman | b2e5e71 | 2019-04-19 15:49:10 -0700 | [diff] [blame] | 616 | public Bitmap getIconBitmap(UserHandle userHandle) { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 617 | Drawable dr = null; |
| 618 | if (mHasSubstitutePermission) { |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 619 | dr = getIconSubstituteInternal(); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | if (dr == null) { |
| 623 | try { |
| 624 | if (mAi.icon != 0) { |
| 625 | dr = loadIconFromResource(mPm.getResourcesForApplication(mAi), mAi.icon); |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 626 | } |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 627 | } catch (NameNotFoundException ignore) { |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | // Fall back to ApplicationInfo#loadIcon if nothing has been loaded |
| 632 | if (dr == null) { |
| 633 | dr = mAi.loadIcon(mPm); |
| 634 | } |
| 635 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 636 | SimpleIconFactory sif = SimpleIconFactory.obtain(mCtx); |
Mike Digman | b2e5e71 | 2019-04-19 15:49:10 -0700 | [diff] [blame] | 637 | Bitmap icon = sif.createUserBadgedIconBitmap(dr, userHandle); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 638 | sif.recycle(); |
| 639 | |
| 640 | return icon; |
| 641 | } |
| 642 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 643 | public String getLabel() { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 644 | String label = null; |
| 645 | // Apps with the substitute permission will always show the sublabel as their label |
| 646 | if (mHasSubstitutePermission) { |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 647 | label = getAppSubLabelInternal(); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | if (label == null) { |
| 651 | label = (String) mAi.loadLabel(mPm); |
| 652 | } |
| 653 | |
| 654 | return label; |
| 655 | } |
| 656 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 657 | public String getSubLabel() { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 658 | // Apps with the substitute permission will never have a sublabel |
| 659 | if (mHasSubstitutePermission) return null; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 660 | return getAppSubLabelInternal(); |
| 661 | } |
| 662 | |
| 663 | protected String loadLabelFromResource(Resources res, int resId) { |
| 664 | return res.getString(resId); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | @Nullable |
| 668 | protected Drawable loadIconFromResource(Resources res, int resId) { |
| 669 | return res.getDrawableForDensity(resId, mIconDpi); |
| 670 | } |
| 671 | |
| 672 | } |
| 673 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 674 | /** |
| 675 | * Loads the icon and label for the provided ResolveInfo. |
| 676 | */ |
| 677 | @VisibleForTesting |
Mike Digman | c0ef554 | 2019-04-01 15:36:25 -0700 | [diff] [blame] | 678 | public static class ResolveInfoPresentationGetter extends ActivityInfoPresentationGetter { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 679 | private final ResolveInfo mRi; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 680 | public ResolveInfoPresentationGetter(Context ctx, int iconDpi, ResolveInfo ri) { |
Mike Digman | c0ef554 | 2019-04-01 15:36:25 -0700 | [diff] [blame] | 681 | super(ctx, iconDpi, ri.activityInfo); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 682 | mRi = ri; |
| 683 | } |
| 684 | |
| 685 | @Override |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 686 | Drawable getIconSubstituteInternal() { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 687 | Drawable dr = null; |
| 688 | try { |
| 689 | // Do not use ResolveInfo#getIconResource() as it defaults to the app |
| 690 | if (mRi.resolvePackageName != null && mRi.icon != 0) { |
| 691 | dr = loadIconFromResource( |
| 692 | mPm.getResourcesForApplication(mRi.resolvePackageName), mRi.icon); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 693 | } |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 694 | } catch (NameNotFoundException e) { |
| 695 | Log.e(TAG, "SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON permission granted but " |
| 696 | + "couldn't find resources for package", e); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 697 | } |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 698 | |
Mike Digman | c0ef554 | 2019-04-01 15:36:25 -0700 | [diff] [blame] | 699 | // Fall back to ActivityInfo if no icon is found via ResolveInfo |
| 700 | if (dr == null) dr = super.getIconSubstituteInternal(); |
| 701 | |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 702 | return dr; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 703 | } |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 704 | |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 705 | @Override |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 706 | String getAppSubLabelInternal() { |
| 707 | // Will default to app name if no intent filter or activity label set, make sure to |
| 708 | // check if subLabel matches label before final display |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 709 | return (String) mRi.loadLabel(mPm); |
| 710 | } |
| 711 | } |
| 712 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 713 | ResolveInfoPresentationGetter makePresentationGetter(ResolveInfo ri) { |
| 714 | return new ResolveInfoPresentationGetter(this, mIconDpi, ri); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Loads the icon and label for the provided ActivityInfo. |
| 719 | */ |
| 720 | @VisibleForTesting |
| 721 | public static class ActivityInfoPresentationGetter extends TargetPresentationGetter { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 722 | private final ActivityInfo mActivityInfo; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 723 | public ActivityInfoPresentationGetter(Context ctx, int iconDpi, |
| 724 | ActivityInfo activityInfo) { |
| 725 | super(ctx, iconDpi, activityInfo.applicationInfo); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 726 | mActivityInfo = activityInfo; |
| 727 | } |
| 728 | |
| 729 | @Override |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 730 | Drawable getIconSubstituteInternal() { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 731 | Drawable dr = null; |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 732 | try { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 733 | // Do not use ActivityInfo#getIconResource() as it defaults to the app |
| 734 | if (mActivityInfo.icon != 0) { |
| 735 | dr = loadIconFromResource( |
| 736 | mPm.getResourcesForApplication(mActivityInfo.applicationInfo), |
| 737 | mActivityInfo.icon); |
| 738 | } |
| 739 | } catch (NameNotFoundException e) { |
| 740 | Log.e(TAG, "SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON permission granted but " |
| 741 | + "couldn't find resources for package", e); |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 742 | } |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 743 | |
| 744 | return dr; |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 747 | @Override |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 748 | String getAppSubLabelInternal() { |
| 749 | // Will default to app name if no activity label set, make sure to check if subLabel |
| 750 | // matches label before final display |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 751 | return (String) mActivityInfo.loadLabel(mPm); |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 752 | } |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 753 | } |
Mike Digman | 220587e | 2019-03-13 11:21:42 -0700 | [diff] [blame] | 754 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 755 | protected ActivityInfoPresentationGetter makePresentationGetter(ActivityInfo ai) { |
| 756 | return new ActivityInfoPresentationGetter(this, mIconDpi, ai); |
| 757 | } |
| 758 | |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 759 | Drawable loadIconForResolveInfo(ResolveInfo ri) { |
Mike Digman | b2e5e71 | 2019-04-19 15:49:10 -0700 | [diff] [blame] | 760 | // Load icons based on the current process. If in work profile icons should be badged. |
| 761 | return makePresentationGetter(ri).getIcon(Process.myUserHandle()); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 764 | @Override |
| 765 | protected void onRestart() { |
| 766 | super.onRestart(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 767 | if (!mRegistered) { |
| 768 | mPackageMonitor.register(this, getMainLooper(), false); |
| 769 | mRegistered = true; |
| 770 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 771 | mAdapter.handlePackagesChanged(); |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 772 | bindProfileView(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | @Override |
| 776 | protected void onStop() { |
| 777 | super.onStop(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 778 | if (mRegistered) { |
| 779 | mPackageMonitor.unregister(); |
| 780 | mRegistered = false; |
| 781 | } |
Wale Ogunwale | 9014e66 | 2016-03-19 14:55:46 -0700 | [diff] [blame] | 782 | final Intent intent = getIntent(); |
| 783 | if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction() |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 784 | && !mResolvingHome && !mRetainInOnStop) { |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 785 | // This resolver is in the unusual situation where it has been |
| 786 | // launched at the top of a new task. We don't let it be added |
| 787 | // to the recent tasks shown to the user, and we need to make sure |
| 788 | // that each time we are launched we get the correct launching |
| 789 | // uid (not re-using the same resolver from an old launching uid), |
| 790 | // so we will now finish ourself since being no longer visible, |
| 791 | // the user probably can't get back to us. |
| 792 | if (!isChangingConfigurations()) { |
| 793 | finish(); |
| 794 | } |
| 795 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 798 | @Override |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 799 | protected void onDestroy() { |
| 800 | super.onDestroy(); |
| 801 | if (!isChangingConfigurations() && mPickOptionRequest != null) { |
| 802 | mPickOptionRequest.cancel(); |
| 803 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 804 | if (mPostListReadyRunnable != null) { |
| 805 | getMainThreadHandler().removeCallbacks(mPostListReadyRunnable); |
| 806 | mPostListReadyRunnable = null; |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 807 | } |
Kang Li | 38a6da64 | 2017-04-05 12:30:55 -0700 | [diff] [blame] | 808 | if (mAdapter != null && mAdapter.mResolverListController != null) { |
| 809 | mAdapter.mResolverListController.destroy(); |
| 810 | } |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | @Override |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 814 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 815 | super.onRestoreInstanceState(savedInstanceState); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 816 | resetButtonBar(); |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 817 | } |
| 818 | |
shafik | 0c7c5d5 | 2019-02-27 12:13:25 +0000 | [diff] [blame] | 819 | private boolean isHttpSchemeAndViewAction(Intent intent) { |
| 820 | return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme()) |
| 821 | || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme())) |
| 822 | && Intent.ACTION_VIEW.equals(intent.getAction()); |
| 823 | } |
| 824 | |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 825 | private boolean hasManagedProfile() { |
| 826 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 827 | if (userManager == null) { |
| 828 | return false; |
| 829 | } |
| 830 | |
| 831 | try { |
| 832 | List<UserInfo> profiles = userManager.getProfiles(getUserId()); |
| 833 | for (UserInfo userInfo : profiles) { |
| 834 | if (userInfo != null && userInfo.isManagedProfile()) { |
| 835 | return true; |
| 836 | } |
| 837 | } |
| 838 | } catch (SecurityException e) { |
| 839 | return false; |
| 840 | } |
| 841 | return false; |
| 842 | } |
| 843 | |
| 844 | private boolean supportsManagedProfiles(ResolveInfo resolveInfo) { |
| 845 | try { |
| 846 | ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| 847 | resolveInfo.activityInfo.packageName, 0 /* default flags */); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 848 | return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 849 | } catch (NameNotFoundException e) { |
| 850 | return false; |
| 851 | } |
| 852 | } |
| 853 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 854 | private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos, |
| 855 | boolean filtered) { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 856 | boolean enabled = false; |
| 857 | if (hasValidSelection) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 858 | ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered); |
Hakan Seyalioglu | 5dbc819 | 2017-02-24 16:16:37 -0800 | [diff] [blame] | 859 | if (ri == null) { |
| 860 | Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled"); |
| 861 | return; |
| 862 | } else if (ri.targetUserId != UserHandle.USER_CURRENT) { |
| 863 | Log.e(TAG, "Attempted to set selection to resolve info for another user"); |
| 864 | return; |
| 865 | } else { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 866 | enabled = true; |
| 867 | } |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 868 | if (mUseLayoutForBrowsables && !ri.handleAllWebDataURI) { |
| 869 | mAlwaysButton.setText(getResources() |
| 870 | .getString(R.string.activity_resolver_set_always)); |
| 871 | } else { |
| 872 | mAlwaysButton.setText(getResources() |
| 873 | .getString(R.string.activity_resolver_use_always)); |
| 874 | } |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 875 | } |
| 876 | mAlwaysButton.setEnabled(enabled); |
| 877 | } |
| 878 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 879 | public void onButtonClick(View v) { |
| 880 | final int id = v.getId(); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 881 | int which = mAdapter.hasFilteredItem() |
| 882 | ? mAdapter.getFilteredPosition() |
| 883 | : mAdapterView.getCheckedItemPosition(); |
| 884 | boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem(); |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 885 | ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered); |
Matt Pietal | a310c1f | 2019-07-03 09:12:56 -0400 | [diff] [blame] | 886 | if (mUseLayoutForBrowsables |
| 887 | && !ri.handleAllWebDataURI && id == R.id.button_always) { |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 888 | showSettingsForSelected(ri); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 889 | } else { |
| 890 | startSelected(which, id == R.id.button_always, hasIndexBeenFiltered); |
| 891 | } |
| 892 | } |
| 893 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 894 | private void showSettingsForSelected(ResolveInfo ri) { |
shafik | 34064c3 | 2019-03-06 15:42:52 +0000 | [diff] [blame] | 895 | Intent intent = new Intent(); |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 896 | |
| 897 | final String packageName = ri.activityInfo.packageName; |
| 898 | Bundle showFragmentArgs = new Bundle(); |
| 899 | showFragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY); |
| 900 | showFragmentArgs.putString("package", packageName); |
| 901 | |
shafik | 34064c3 | 2019-03-06 15:42:52 +0000 | [diff] [blame] | 902 | // For regular apps, we open the Open by Default page |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 903 | intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS) |
| 904 | .setData(Uri.fromParts("package", packageName, null)) |
| 905 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT) |
| 906 | .putExtra(EXTRA_FRAGMENT_ARG_KEY, OPEN_LINKS_COMPONENT_KEY) |
| 907 | .putExtra(EXTRA_SHOW_FRAGMENT_ARGS, showFragmentArgs); |
| 908 | |
shafik | 34064c3 | 2019-03-06 15:42:52 +0000 | [diff] [blame] | 909 | startActivity(intent); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 912 | public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) { |
Amith Yamasani | 07cd351 | 2013-09-18 13:16:00 -0700 | [diff] [blame] | 913 | if (isFinishing()) { |
| 914 | return; |
| 915 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 916 | ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered); |
Sander Alewijnse | 86d35ba | 2015-02-04 15:14:53 +0000 | [diff] [blame] | 917 | if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) { |
| 918 | Toast.makeText(this, String.format(getResources().getString( |
| 919 | com.android.internal.R.string.activity_resolver_work_profiles_support), |
| 920 | ri.activityInfo.loadLabel(getPackageManager()).toString()), |
| 921 | Toast.LENGTH_LONG).show(); |
| 922 | return; |
| 923 | } |
| 924 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 925 | TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 926 | if (target == null) { |
| 927 | return; |
| 928 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 929 | if (onTargetSelected(target, always)) { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 930 | if (always && mSupportsAlwaysUseOption) { |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 931 | MetricsLogger.action( |
| 932 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 933 | } else if (mSupportsAlwaysUseOption) { |
Clara Bayarri | fa902aa | 2016-04-13 14:45:08 +0100 | [diff] [blame] | 934 | MetricsLogger.action( |
| 935 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE); |
| 936 | } else { |
| 937 | MetricsLogger.action( |
| 938 | this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP); |
| 939 | } |
| 940 | MetricsLogger.action(this, mAdapter.hasFilteredItem() |
| 941 | ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED |
| 942 | : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 943 | finish(); |
| 944 | } |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 945 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 947 | /** |
| 948 | * Replace me in subclasses! |
| 949 | */ |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 950 | public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) { |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 951 | return defIntent; |
| 952 | } |
| 953 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 954 | protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 955 | final ResolveInfo ri = target.getResolveInfo(); |
| 956 | final Intent intent = target != null ? target.getResolvedIntent() : null; |
| 957 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 958 | if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem()) |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 959 | && mAdapter.mUnfilteredResolveList != null) { |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 960 | // Build a reasonable intent filter, based on what matched. |
| 961 | IntentFilter filter = new IntentFilter(); |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 962 | Intent filterIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 964 | if (intent.getSelector() != null) { |
| 965 | filterIntent = intent.getSelector(); |
| 966 | } else { |
| 967 | filterIntent = intent; |
| 968 | } |
| 969 | |
| 970 | String action = filterIntent.getAction(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 971 | if (action != null) { |
| 972 | filter.addAction(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | } |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 974 | Set<String> categories = filterIntent.getCategories(); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 975 | if (categories != null) { |
| 976 | for (String cat : categories) { |
| 977 | filter.addCategory(cat); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | } |
| 979 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 980 | filter.addCategory(Intent.CATEGORY_DEFAULT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 982 | int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK; |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 983 | Uri data = filterIntent.getData(); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 984 | if (cat == IntentFilter.MATCH_CATEGORY_TYPE) { |
Henrik Engström | 3277cf1 | 2014-07-17 12:18:29 +0200 | [diff] [blame] | 985 | String mimeType = filterIntent.resolveType(this); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 986 | if (mimeType != null) { |
| 987 | try { |
| 988 | filter.addDataType(mimeType); |
| 989 | } catch (IntentFilter.MalformedMimeTypeException e) { |
| 990 | Log.w("ResolverActivity", e); |
| 991 | filter = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 994 | } |
| 995 | if (data != null && data.getScheme() != null) { |
| 996 | // We need the data specification if there was no type, |
| 997 | // OR if the scheme is not one of our magical "file:" |
| 998 | // or "content:" schemes (see IntentFilter for the reason). |
| 999 | if (cat != IntentFilter.MATCH_CATEGORY_TYPE |
| 1000 | || (!"file".equals(data.getScheme()) |
| 1001 | && !"content".equals(data.getScheme()))) { |
| 1002 | filter.addDataScheme(data.getScheme()); |
| 1003 | |
| 1004 | // Look through the resolved filter to determine which part |
| 1005 | // of it matched the original Intent. |
| 1006 | Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator(); |
| 1007 | if (pIt != null) { |
| 1008 | String ssp = data.getSchemeSpecificPart(); |
| 1009 | while (ssp != null && pIt.hasNext()) { |
| 1010 | PatternMatcher p = pIt.next(); |
| 1011 | if (p.match(ssp)) { |
| 1012 | filter.addDataSchemeSpecificPart(p.getPath(), p.getType()); |
| 1013 | break; |
| 1014 | } |
Dianne Hackborn | df1c0bf | 2013-06-12 16:21:38 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1017 | Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator(); |
| 1018 | if (aIt != null) { |
| 1019 | while (aIt.hasNext()) { |
| 1020 | IntentFilter.AuthorityEntry a = aIt.next(); |
| 1021 | if (a.match(data) >= 0) { |
| 1022 | int port = a.getPort(); |
| 1023 | filter.addDataAuthority(a.getHost(), |
| 1024 | port >= 0 ? Integer.toString(port) : null); |
| 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | pIt = ri.filter.pathsIterator(); |
| 1030 | if (pIt != null) { |
| 1031 | String path = data.getPath(); |
| 1032 | while (path != null && pIt.hasNext()) { |
| 1033 | PatternMatcher p = pIt.next(); |
| 1034 | if (p.match(path)) { |
| 1035 | filter.addDataPath(p.getPath(), p.getType()); |
| 1036 | break; |
| 1037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1043 | if (filter != null) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1044 | final int N = mAdapter.mUnfilteredResolveList.size(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1045 | ComponentName[] set; |
| 1046 | // If we don't add back in the component for forwarding the intent to a managed |
| 1047 | // profile, the preferred activity may not be updated correctly (as the set of |
| 1048 | // components we tell it we knew about will have changed). |
| 1049 | final boolean needToAddBackProfileForwardingComponent |
| 1050 | = mAdapter.mOtherProfile != null; |
| 1051 | if (!needToAddBackProfileForwardingComponent) { |
| 1052 | set = new ComponentName[N]; |
| 1053 | } else { |
| 1054 | set = new ComponentName[N + 1]; |
| 1055 | } |
| 1056 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1057 | int bestMatch = 0; |
| 1058 | for (int i=0; i<N; i++) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1059 | ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1060 | set[i] = new ComponentName(r.activityInfo.packageName, |
| 1061 | r.activityInfo.name); |
| 1062 | if (r.match > bestMatch) bestMatch = r.match; |
| 1063 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1064 | |
| 1065 | if (needToAddBackProfileForwardingComponent) { |
| 1066 | set[N] = mAdapter.mOtherProfile.getResolvedComponentName(); |
| 1067 | final int otherProfileMatch = mAdapter.mOtherProfile.getResolveInfo().match; |
| 1068 | if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch; |
| 1069 | } |
| 1070 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1071 | if (alwaysCheck) { |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1072 | final int userId = getUserId(); |
| 1073 | final PackageManager pm = getPackageManager(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1074 | |
| 1075 | // Set the preferred Activity |
| 1076 | pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent()); |
| 1077 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1078 | if (ri.handleAllWebDataURI) { |
| 1079 | // Set default Browser if needed |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 1080 | final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId); |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1081 | if (TextUtils.isEmpty(packageName)) { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 1082 | pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId); |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1083 | } |
| 1084 | } else { |
| 1085 | // Update Domain Verification status |
| 1086 | ComponentName cn = intent.getComponent(); |
| 1087 | String packageName = cn.getPackageName(); |
| 1088 | String dataScheme = (data != null) ? data.getScheme() : null; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1089 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1090 | boolean isHttpOrHttps = (dataScheme != null) && |
| 1091 | (dataScheme.equals(IntentFilter.SCHEME_HTTP) || |
| 1092 | dataScheme.equals(IntentFilter.SCHEME_HTTPS)); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1093 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1094 | boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW); |
| 1095 | boolean hasCategoryBrowsable = (categories != null) && |
| 1096 | categories.contains(Intent.CATEGORY_BROWSABLE); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1097 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1098 | if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 1099 | pm.updateIntentVerificationStatusAsUser(packageName, |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 1100 | PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS, |
| 1101 | userId); |
| 1102 | } |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 1103 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1104 | } else { |
| 1105 | try { |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 1106 | mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 1107 | } catch (RemoteException re) { |
| 1108 | Log.d(TAG, "Error calling setLastChosenActivity\n" + re); |
| 1109 | } |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1110 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1114 | if (target != null) { |
| 1115 | safelyStartActivity(target); |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1116 | |
| 1117 | // Rely on the ActivityManager to pop up a dialog regarding app suspension |
| 1118 | // and return false |
| 1119 | if (target.isSuspended()) { |
| 1120 | return false; |
| 1121 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1122 | } |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1123 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1124 | return true; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1127 | public void safelyStartActivity(TargetInfo cti) { |
Jeff Sharkey | 2b9eb89 | 2016-02-16 09:21:51 -0700 | [diff] [blame] | 1128 | // We're dispatching intents that might be coming from legacy apps, so |
| 1129 | // don't kill ourselves. |
| 1130 | StrictMode.disableDeathOnFileUriExposure(); |
| 1131 | try { |
| 1132 | safelyStartActivityInternal(cti); |
| 1133 | } finally { |
| 1134 | StrictMode.enableDeathOnFileUriExposure(); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | private void safelyStartActivityInternal(TargetInfo cti) { |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 1139 | // If needed, show that intent is forwarded |
| 1140 | // from managed profile to owner or other way around. |
| 1141 | if (mProfileSwitchMessageId != -1) { |
| 1142 | Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show(); |
| 1143 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1144 | if (!mSafeForwardingMode) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1145 | if (cti.start(this, null)) { |
| 1146 | onActivityStarted(cti); |
| 1147 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1148 | return; |
| 1149 | } |
| 1150 | try { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1151 | if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) { |
| 1152 | onActivityStarted(cti); |
| 1153 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1154 | } catch (RuntimeException e) { |
| 1155 | String launchedFromPackage; |
| 1156 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 1157 | launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage( |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1158 | getActivityToken()); |
| 1159 | } catch (RemoteException e2) { |
| 1160 | launchedFromPackage = "??"; |
| 1161 | } |
| 1162 | Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid |
| 1163 | + " package " + launchedFromPackage + ", while running in " |
| 1164 | + ActivityThread.currentProcessName(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1168 | |
| 1169 | boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity, |
| 1170 | int userId) { |
| 1171 | // Pass intent to delegate chooser activity with permission token. |
| 1172 | // TODO: This should move to a trampoline Activity in the system when the ChooserActivity |
| 1173 | // moves into systemui |
| 1174 | try { |
| 1175 | // TODO: Once this is a small springboard activity, it can move off the UI process |
| 1176 | // and we can move the request method to ActivityManagerInternal. |
| 1177 | IBinder permissionToken = ActivityTaskManager.getService() |
| 1178 | .requestStartActivityPermissionToken(getActivityToken()); |
| 1179 | final Intent chooserIntent = new Intent(); |
| 1180 | final ComponentName delegateActivity = ComponentName.unflattenFromString( |
| 1181 | Resources.getSystem().getString(R.string.config_chooserActivity)); |
| 1182 | chooserIntent.setClassName(delegateActivity.getPackageName(), |
| 1183 | delegateActivity.getClassName()); |
| 1184 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken); |
| 1185 | |
| 1186 | // TODO: These extras will change as chooser activity moves into systemui |
| 1187 | chooserIntent.putExtra(Intent.EXTRA_INTENT, intent); |
| 1188 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options); |
| 1189 | chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY, |
| 1190 | ignoreTargetSecurity); |
| 1191 | chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId); |
Alison Cichowlas | 7b6f3b6 | 2018-12-07 09:06:57 -0500 | [diff] [blame] | 1192 | chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| 1193 | | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1194 | startActivity(chooserIntent); |
| 1195 | } catch (RemoteException e) { |
| 1196 | Log.e(TAG, e.toString()); |
| 1197 | } |
| 1198 | return true; |
| 1199 | } |
| 1200 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1201 | public void onActivityStarted(TargetInfo cti) { |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 1202 | // Do nothing |
| 1203 | } |
| 1204 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1205 | public boolean shouldGetActivityMetadata() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1206 | return false; |
| 1207 | } |
| 1208 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1209 | public boolean shouldAutoLaunchSingleChoice(TargetInfo target) { |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1210 | return !target.isSuspended(); |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1213 | public void showTargetDetails(ResolveInfo ri) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 1214 | Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 1215 | .setData(Uri.fromParts("package", ri.activityInfo.packageName, null)) |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1216 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
Amith Yamasani | 203a2f4 | 2012-10-03 20:16:40 -0700 | [diff] [blame] | 1217 | startActivity(in); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1220 | public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents, |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1221 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
| 1222 | boolean filterLastUsed) { |
| 1223 | return new ResolveListAdapter(context, payloadIntents, initialIntents, rList, |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1224 | launchedFromUid, filterLastUsed, createListController()); |
| 1225 | } |
| 1226 | |
| 1227 | @VisibleForTesting |
| 1228 | protected ResolverListController createListController() { |
| 1229 | return new ResolverListController( |
| 1230 | this, |
| 1231 | mPm, |
| 1232 | getTargetIntent(), |
| 1233 | getReferrerPackageName(), |
| 1234 | mLaunchedFromUid); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1237 | /** |
| 1238 | * Returns true if the activity is finishing and creation should halt |
| 1239 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1240 | public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents, |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1241 | List<ResolveInfo> rList) { |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 1242 | // The last argument of createAdapter is whether to do special handling |
| 1243 | // of the last used choice to highlight it in the list. We need to always |
| 1244 | // turn this off when running under voice interaction, since it results in |
| 1245 | // a more complicated UI that the current voice interaction flow is not able |
| 1246 | // to handle. |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1247 | mAdapter = createAdapter(this, payloadIntents, initialIntents, rList, |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1248 | mLaunchedFromUid, mSupportsAlwaysUseOption && !isVoiceInteraction()); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1249 | boolean rebuildCompleted = mAdapter.rebuildList(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1250 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1251 | if (useLayoutWithDefault()) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1252 | mLayoutId = R.layout.resolver_list_with_default; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1253 | } else { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1254 | mLayoutId = getLayoutResource(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1255 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1256 | setContentView(mLayoutId); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1257 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1258 | int count = mAdapter.getUnfilteredCount(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1259 | |
| 1260 | // We only rebuild asynchronously when we have multiple elements to sort. In the case where |
| 1261 | // we're already done, we can check if we should auto-launch immediately. |
| 1262 | if (rebuildCompleted) { |
| 1263 | if (count == 1 && mAdapter.getOtherProfile() == null) { |
| 1264 | // Only one target, so we're a candidate to auto-launch! |
| 1265 | final TargetInfo target = mAdapter.targetInfoForPosition(0, false); |
| 1266 | if (shouldAutoLaunchSingleChoice(target)) { |
| 1267 | safelyStartActivity(target); |
| 1268 | mPackageMonitor.unregister(); |
| 1269 | mRegistered = false; |
| 1270 | finish(); |
| 1271 | return true; |
| 1272 | } |
Jeff Sharkey | cc2ae6b4 | 2015-09-29 13:04:46 -0700 | [diff] [blame] | 1273 | } |
| 1274 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1275 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1276 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1277 | mAdapterView = findViewById(R.id.resolver_list); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1278 | |
| 1279 | if (count == 0 && mAdapter.mPlaceholderCount == 0) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1280 | final TextView emptyView = findViewById(R.id.empty); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1281 | emptyView.setVisibility(View.VISIBLE); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1282 | mAdapterView.setVisibility(View.GONE); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1283 | } else { |
| 1284 | mAdapterView.setVisibility(View.VISIBLE); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1285 | onPrepareAdapterView(mAdapterView, mAdapter); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1286 | } |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1287 | return false; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1290 | public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1291 | final boolean useHeader = adapter.hasFilteredItem(); |
| 1292 | final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null; |
| 1293 | |
| 1294 | adapterView.setAdapter(mAdapter); |
| 1295 | |
| 1296 | final ItemClickListener listener = new ItemClickListener(); |
| 1297 | adapterView.setOnItemClickListener(listener); |
| 1298 | adapterView.setOnItemLongClickListener(listener); |
| 1299 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1300 | if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1301 | listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); |
| 1302 | } |
| 1303 | |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1304 | // In case this method is called again (due to activity recreation), avoid adding a new |
| 1305 | // header if one is already present. |
| 1306 | if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1307 | listView.addHeaderView(LayoutInflater.from(this).inflate( |
| 1308 | R.layout.resolver_different_item_header, listView, false)); |
| 1309 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1312 | /** |
| 1313 | * Configure the area above the app selection list (title, content preview, etc). |
| 1314 | */ |
| 1315 | public void setHeader() { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1316 | if (mAdapter.getCount() == 0 && mAdapter.mPlaceholderCount == 0) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1317 | final TextView titleView = findViewById(R.id.title); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1318 | if (titleView != null) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1319 | titleView.setVisibility(View.GONE); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1320 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | CharSequence title = mTitle != null |
| 1324 | ? mTitle |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1325 | : getTitleForAction(getTargetIntent(), mDefaultTitleResId); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1326 | |
| 1327 | if (!TextUtils.isEmpty(title)) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1328 | final TextView titleView = findViewById(R.id.title); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1329 | if (titleView != null) { |
| 1330 | titleView.setText(title); |
| 1331 | } |
| 1332 | setTitle(title); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1335 | final ImageView iconView = findViewById(R.id.icon); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1336 | final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem(); |
| 1337 | if (iconView != null && iconInfo != null) { |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 1338 | new LoadIconTask(iconInfo, iconView).execute(); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1342 | private void resetButtonBar() { |
| 1343 | if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) { |
| 1344 | return; |
| 1345 | } |
| 1346 | final ViewGroup buttonLayout = findViewById(R.id.button_bar); |
| 1347 | if (buttonLayout != null) { |
| 1348 | buttonLayout.setVisibility(View.VISIBLE); |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 1349 | int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0; |
| 1350 | buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(), |
Matt Pietal | 33b0ac1 | 2019-05-24 13:47:18 -0400 | [diff] [blame] | 1351 | buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize( |
| 1352 | R.dimen.resolver_button_bar_spacing) + inset); |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 1353 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1354 | mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1355 | mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always); |
| 1356 | |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1357 | resetAlwaysOrOnceButtonBar(); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1358 | } else { |
| 1359 | Log.e(TAG, "Layout unexpectedly does not have a button bar"); |
| 1360 | } |
| 1361 | } |
| 1362 | |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1363 | private void resetAlwaysOrOnceButtonBar() { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1364 | if (useLayoutWithDefault() |
| 1365 | && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) { |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1366 | setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false); |
| 1367 | mOnceButton.setEnabled(true); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | // When the items load in, if an item was already selected, enable the buttons |
| 1372 | if (mAdapterView != null |
| 1373 | && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) { |
| 1374 | setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true); |
| 1375 | mOnceButton.setEnabled(true); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1379 | private boolean useLayoutWithDefault() { |
| 1380 | return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem(); |
| 1381 | } |
| 1382 | |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1383 | /** |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 1384 | * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets |
| 1385 | * called and we are launched in a new task. |
| 1386 | */ |
| 1387 | protected void setRetainInOnStop(boolean retainInOnStop) { |
| 1388 | mRetainInOnStop = retainInOnStop; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1392 | * Check a simple match for the component of two ResolveInfos. |
| 1393 | */ |
| 1394 | static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) { |
| 1395 | return lhs == null ? rhs == null |
| 1396 | : lhs.activityInfo == null ? rhs.activityInfo == null |
| 1397 | : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name) |
| 1398 | && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName); |
| 1399 | } |
| 1400 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1401 | public final class DisplayResolveInfo implements TargetInfo { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1402 | private final ResolveInfo mResolveInfo; |
| 1403 | private final CharSequence mDisplayLabel; |
| 1404 | private Drawable mDisplayIcon; |
Adam Powell | 00f4aad | 2015-09-17 13:38:16 -0700 | [diff] [blame] | 1405 | private Drawable mBadge; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1406 | private final CharSequence mExtendedInfo; |
| 1407 | private final Intent mResolvedIntent; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1408 | private final List<Intent> mSourceIntents = new ArrayList<>(); |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1409 | private boolean mIsSuspended; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1411 | public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1412 | CharSequence pInfo, Intent pOrigIntent) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1413 | mSourceIntents.add(originalIntent); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1414 | mResolveInfo = pri; |
| 1415 | mDisplayLabel = pLabel; |
| 1416 | mExtendedInfo = pInfo; |
| 1417 | |
| 1418 | final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent : |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1419 | getReplacementIntent(pri.activityInfo, getTargetIntent())); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1420 | intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| 1421 | | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
| 1422 | final ActivityInfo ai = mResolveInfo.activityInfo; |
| 1423 | intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name)); |
| 1424 | |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1425 | mIsSuspended = (ai.applicationInfo.flags & ApplicationInfo.FLAG_SUSPENDED) != 0; |
| 1426 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1427 | mResolvedIntent = intent; |
| 1428 | } |
| 1429 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1430 | private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) { |
| 1431 | mSourceIntents.addAll(other.getAllSourceIntents()); |
| 1432 | mResolveInfo = other.mResolveInfo; |
| 1433 | mDisplayLabel = other.mDisplayLabel; |
| 1434 | mDisplayIcon = other.mDisplayIcon; |
| 1435 | mExtendedInfo = other.mExtendedInfo; |
| 1436 | mResolvedIntent = new Intent(other.mResolvedIntent); |
| 1437 | mResolvedIntent.fillIn(fillInIntent, flags); |
| 1438 | } |
| 1439 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1440 | public ResolveInfo getResolveInfo() { |
| 1441 | return mResolveInfo; |
| 1442 | } |
| 1443 | |
| 1444 | public CharSequence getDisplayLabel() { |
| 1445 | return mDisplayLabel; |
| 1446 | } |
| 1447 | |
| 1448 | public Drawable getDisplayIcon() { |
| 1449 | return mDisplayIcon; |
| 1450 | } |
| 1451 | |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1452 | @Override |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1453 | public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) { |
| 1454 | return new DisplayResolveInfo(this, fillInIntent, flags); |
| 1455 | } |
| 1456 | |
| 1457 | @Override |
| 1458 | public List<Intent> getAllSourceIntents() { |
| 1459 | return mSourceIntents; |
| 1460 | } |
| 1461 | |
| 1462 | public void addAlternateSourceIntent(Intent alt) { |
| 1463 | mSourceIntents.add(alt); |
| 1464 | } |
| 1465 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1466 | public void setDisplayIcon(Drawable icon) { |
| 1467 | mDisplayIcon = icon; |
| 1468 | } |
| 1469 | |
| 1470 | public boolean hasDisplayIcon() { |
| 1471 | return mDisplayIcon != null; |
| 1472 | } |
| 1473 | |
| 1474 | public CharSequence getExtendedInfo() { |
| 1475 | return mExtendedInfo; |
| 1476 | } |
| 1477 | |
| 1478 | public Intent getResolvedIntent() { |
| 1479 | return mResolvedIntent; |
| 1480 | } |
| 1481 | |
| 1482 | @Override |
| 1483 | public ComponentName getResolvedComponentName() { |
| 1484 | return new ComponentName(mResolveInfo.activityInfo.packageName, |
| 1485 | mResolveInfo.activityInfo.name); |
| 1486 | } |
| 1487 | |
| 1488 | @Override |
| 1489 | public boolean start(Activity activity, Bundle options) { |
| 1490 | activity.startActivity(mResolvedIntent, options); |
| 1491 | return true; |
| 1492 | } |
| 1493 | |
| 1494 | @Override |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1495 | public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) { |
Alison Cichowlas | 4691ed4 | 2018-11-13 15:59:55 -0500 | [diff] [blame] | 1496 | if (mEnableChooserDelegate) { |
| 1497 | return activity.startAsCallerImpl(mResolvedIntent, options, false, userId); |
| 1498 | } else { |
| 1499 | activity.startActivityAsCaller(mResolvedIntent, options, null, false, userId); |
| 1500 | return true; |
| 1501 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | @Override |
| 1505 | public boolean startAsUser(Activity activity, Bundle options, UserHandle user) { |
| 1506 | activity.startActivityAsUser(mResolvedIntent, options, user); |
| 1507 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | } |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1509 | |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1510 | public boolean isSuspended() { |
| 1511 | return mIsSuspended; |
| 1512 | } |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | List<DisplayResolveInfo> getDisplayList() { |
| 1516 | return mAdapter.mDisplayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1517 | } |
| 1518 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1519 | /** |
| 1520 | * A single target as represented in the chooser. |
| 1521 | */ |
| 1522 | public interface TargetInfo { |
| 1523 | /** |
| 1524 | * Get the resolved intent that represents this target. Note that this may not be the |
| 1525 | * intent that will be launched by calling one of the <code>start</code> methods provided; |
| 1526 | * this is the intent that will be credited with the launch. |
| 1527 | * |
| 1528 | * @return the resolved intent for this target |
| 1529 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1530 | Intent getResolvedIntent(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1531 | |
| 1532 | /** |
| 1533 | * Get the resolved component name that represents this target. Note that this may not |
| 1534 | * be the component that will be directly launched by calling one of the <code>start</code> |
| 1535 | * methods provided; this is the component that will be credited with the launch. |
| 1536 | * |
| 1537 | * @return the resolved ComponentName for this target |
| 1538 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1539 | ComponentName getResolvedComponentName(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1540 | |
| 1541 | /** |
| 1542 | * Start the activity referenced by this target. |
| 1543 | * |
| 1544 | * @param activity calling Activity performing the launch |
| 1545 | * @param options ActivityOptions bundle |
| 1546 | * @return true if the start completed successfully |
| 1547 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1548 | boolean start(Activity activity, Bundle options); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1549 | |
| 1550 | /** |
| 1551 | * Start the activity referenced by this target as if the ResolverActivity's caller |
| 1552 | * was performing the start operation. |
| 1553 | * |
| 1554 | * @param activity calling Activity (actually) performing the launch |
| 1555 | * @param options ActivityOptions bundle |
| 1556 | * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller |
| 1557 | * @return true if the start completed successfully |
| 1558 | */ |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1559 | boolean startAsCaller(ResolverActivity activity, Bundle options, int userId); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1560 | |
| 1561 | /** |
| 1562 | * Start the activity referenced by this target as a given user. |
| 1563 | * |
| 1564 | * @param activity calling activity performing the launch |
| 1565 | * @param options ActivityOptions bundle |
| 1566 | * @param user handle for the user to start the activity as |
| 1567 | * @return true if the start completed successfully |
| 1568 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1569 | boolean startAsUser(Activity activity, Bundle options, UserHandle user); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1570 | |
| 1571 | /** |
| 1572 | * Return the ResolveInfo about how and why this target matched the original query |
| 1573 | * for available targets. |
| 1574 | * |
| 1575 | * @return ResolveInfo representing this target's match |
| 1576 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1577 | ResolveInfo getResolveInfo(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1578 | |
| 1579 | /** |
| 1580 | * Return the human-readable text label for this target. |
| 1581 | * |
| 1582 | * @return user-visible target label |
| 1583 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1584 | CharSequence getDisplayLabel(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1585 | |
| 1586 | /** |
| 1587 | * Return any extended info for this target. This may be used to disambiguate |
| 1588 | * otherwise identical targets. |
| 1589 | * |
| 1590 | * @return human-readable disambig string or null if none present |
| 1591 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1592 | CharSequence getExtendedInfo(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1593 | |
| 1594 | /** |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 1595 | * @return The drawable that should be used to represent this target including badge |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1596 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1597 | Drawable getDisplayIcon(); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1598 | |
| 1599 | /** |
| 1600 | * Clone this target with the given fill-in information. |
| 1601 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1602 | TargetInfo cloneFilledIn(Intent fillInIntent, int flags); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1603 | |
| 1604 | /** |
| 1605 | * @return the list of supported source intents deduped against this single target |
| 1606 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1607 | List<Intent> getAllSourceIntents(); |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1608 | |
| 1609 | /** |
| 1610 | * @return true if this target can be selected by the user |
| 1611 | */ |
| 1612 | boolean isSuspended(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1615 | public class ResolveListAdapter extends BaseAdapter { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1616 | private final List<Intent> mIntents; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1617 | private final Intent[] mInitialIntents; |
| 1618 | private final List<ResolveInfo> mBaseResolveList; |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1619 | protected ResolveInfo mLastChosen; |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1620 | private DisplayResolveInfo mOtherProfile; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1621 | private ResolverListController mResolverListController; |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1622 | private int mPlaceholderCount; |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1623 | private boolean mAllTargetsAreBrowsers = false; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1624 | |
| 1625 | protected final LayoutInflater mInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1627 | // This one is the list that the Adapter will actually present. |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1628 | List<DisplayResolveInfo> mDisplayList; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1629 | List<ResolvedComponentInfo> mUnfilteredResolveList; |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 1630 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1631 | private int mLastChosenPosition = -1; |
| 1632 | private boolean mFilterLastUsed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1634 | public ResolveListAdapter(Context context, List<Intent> payloadIntents, |
| 1635 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1636 | boolean filterLastUsed, |
| 1637 | ResolverListController resolverListController) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1638 | mIntents = payloadIntents; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1639 | mInitialIntents = initialIntents; |
| 1640 | mBaseResolveList = rList; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1641 | mLaunchedFromUid = launchedFromUid; |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 1642 | mInflater = LayoutInflater.from(context); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1643 | mDisplayList = new ArrayList<>(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1644 | mFilterLastUsed = filterLastUsed; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1645 | mResolverListController = resolverListController; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1646 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1647 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1648 | public void handlePackagesChanged() { |
| 1649 | rebuildList(); |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1650 | if (getCount() == 0) { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1651 | // We no longer have any items... just finish the activity. |
| 1652 | finish(); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 1653 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1656 | public void setPlaceholderCount(int count) { |
| 1657 | mPlaceholderCount = count; |
| 1658 | } |
| 1659 | |
Sumir Kataria | db688af | 2017-05-10 17:33:47 -0700 | [diff] [blame] | 1660 | public int getPlaceholderCount() { return mPlaceholderCount; } |
| 1661 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1662 | @Nullable |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1663 | public DisplayResolveInfo getFilteredItem() { |
| 1664 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 1665 | // Not using getItem since it offsets to dodge this position for the list |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1666 | return mDisplayList.get(mLastChosenPosition); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1667 | } |
| 1668 | return null; |
| 1669 | } |
| 1670 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1671 | public DisplayResolveInfo getOtherProfile() { |
| 1672 | return mOtherProfile; |
| 1673 | } |
| 1674 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1675 | public int getFilteredPosition() { |
| 1676 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 1677 | return mLastChosenPosition; |
| 1678 | } |
| 1679 | return AbsListView.INVALID_POSITION; |
| 1680 | } |
| 1681 | |
| 1682 | public boolean hasFilteredItem() { |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1683 | return mFilterLastUsed && mLastChosen != null; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1686 | public float getScore(DisplayResolveInfo target) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1687 | return mResolverListController.getScore(target); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
Kang Li | 0cef910d | 2017-01-05 09:14:36 -0800 | [diff] [blame] | 1690 | public void updateModel(ComponentName componentName) { |
| 1691 | mResolverListController.updateModel(componentName); |
| 1692 | } |
| 1693 | |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1694 | public void updateChooserCounts(String packageName, int userId, String action) { |
| 1695 | mResolverListController.updateChooserCounts(packageName, userId, action); |
| 1696 | } |
| 1697 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1698 | /** |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1699 | * @return true if all items in the display list are defined as browsers by |
| 1700 | * ResolveInfo.handleAllWebDataURI |
| 1701 | */ |
| 1702 | public boolean areAllTargetsBrowsers() { |
| 1703 | return mAllTargetsAreBrowsers; |
| 1704 | } |
| 1705 | |
| 1706 | /** |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1707 | * Rebuild the list of resolvers. In some cases some parts will need some asynchronous work |
| 1708 | * to complete. |
| 1709 | * |
| 1710 | * @return Whether or not the list building is completed. |
| 1711 | */ |
| 1712 | protected boolean rebuildList() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1713 | List<ResolvedComponentInfo> currentResolveList = null; |
Sudheer Shanka | 7e64e10 | 2015-01-23 10:37:45 +0000 | [diff] [blame] | 1714 | // Clear the value of mOtherProfile from previous call. |
| 1715 | mOtherProfile = null; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1716 | mLastChosen = null; |
| 1717 | mLastChosenPosition = -1; |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1718 | mAllTargetsAreBrowsers = false; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1719 | mDisplayList.clear(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1720 | if (mBaseResolveList != null) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1721 | currentResolveList = mUnfilteredResolveList = new ArrayList<>(); |
| 1722 | mResolverListController.addResolveListDedupe(currentResolveList, |
| 1723 | getTargetIntent(), |
| 1724 | mBaseResolveList); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1725 | } else { |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1726 | currentResolveList = mUnfilteredResolveList = |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1727 | mResolverListController.getResolversForIntent(shouldGetResolvedFilter(), |
| 1728 | shouldGetActivityMetadata(), |
| 1729 | mIntents); |
| 1730 | if (currentResolveList == null) { |
| 1731 | processSortedList(currentResolveList); |
| 1732 | return true; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1733 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1734 | List<ResolvedComponentInfo> originalList = |
| 1735 | mResolverListController.filterIneligibleActivities(currentResolveList, |
| 1736 | true); |
| 1737 | if (originalList != null) { |
| 1738 | mUnfilteredResolveList = originalList; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1739 | } |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 1740 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1741 | |
| 1742 | // So far we only support a single other profile at a time. |
| 1743 | // The first one we see gets special treatment. |
| 1744 | for (ResolvedComponentInfo info : currentResolveList) { |
| 1745 | if (info.getResolveInfoAt(0).targetUserId != UserHandle.USER_CURRENT) { |
| 1746 | mOtherProfile = new DisplayResolveInfo(info.getIntentAt(0), |
| 1747 | info.getResolveInfoAt(0), |
| 1748 | info.getResolveInfoAt(0).loadLabel(mPm), |
| 1749 | info.getResolveInfoAt(0).loadLabel(mPm), |
| 1750 | getReplacementIntent(info.getResolveInfoAt(0).activityInfo, |
| 1751 | info.getIntentAt(0))); |
| 1752 | currentResolveList.remove(info); |
| 1753 | break; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | if (mOtherProfile == null) { |
| 1758 | try { |
| 1759 | mLastChosen = mResolverListController.getLastChosen(); |
| 1760 | } catch (RemoteException re) { |
| 1761 | Log.d(TAG, "Error calling getLastChosenActivity\n" + re); |
| 1762 | } |
| 1763 | } |
| 1764 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1765 | int N; |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1766 | if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1767 | // We only care about fixing the unfilteredList if the current resolve list and |
| 1768 | // current resolve list are currently the same. |
| 1769 | List<ResolvedComponentInfo> originalList = |
| 1770 | mResolverListController.filterLowPriority(currentResolveList, |
| 1771 | mUnfilteredResolveList == currentResolveList); |
| 1772 | if (originalList != null) { |
| 1773 | mUnfilteredResolveList = originalList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1774 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1775 | |
Hakan Seyalioglu | 873cbfd | 2017-02-21 19:23:43 -0800 | [diff] [blame] | 1776 | if (currentResolveList.size() > 1) { |
Sumir Kataria | db688af | 2017-05-10 17:33:47 -0700 | [diff] [blame] | 1777 | int placeholderCount = currentResolveList.size(); |
| 1778 | if (useLayoutWithDefault()) { |
| 1779 | --placeholderCount; |
| 1780 | } |
| 1781 | setPlaceholderCount(placeholderCount); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1782 | AsyncTask<List<ResolvedComponentInfo>, |
| 1783 | Void, |
| 1784 | List<ResolvedComponentInfo>> sortingTask = |
| 1785 | new AsyncTask<List<ResolvedComponentInfo>, |
| 1786 | Void, |
| 1787 | List<ResolvedComponentInfo>>() { |
| 1788 | @Override |
| 1789 | protected List<ResolvedComponentInfo> doInBackground( |
| 1790 | List<ResolvedComponentInfo>... params) { |
| 1791 | mResolverListController.sort(params[0]); |
| 1792 | return params[0]; |
| 1793 | } |
| 1794 | |
| 1795 | @Override |
| 1796 | protected void onPostExecute(List<ResolvedComponentInfo> sortedComponents) { |
| 1797 | processSortedList(sortedComponents); |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 1798 | bindProfileView(); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1799 | notifyDataSetChanged(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1800 | } |
| 1801 | }; |
| 1802 | sortingTask.execute(currentResolveList); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1803 | postListReadyRunnable(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1804 | return false; |
| 1805 | } else { |
| 1806 | processSortedList(currentResolveList); |
| 1807 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1808 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1809 | } else { |
| 1810 | processSortedList(currentResolveList); |
| 1811 | return true; |
| 1812 | } |
| 1813 | } |
| 1814 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1815 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1816 | private void processSortedList(List<ResolvedComponentInfo> sortedComponents) { |
| 1817 | int N; |
| 1818 | if (sortedComponents != null && (N = sortedComponents.size()) != 0) { |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1819 | mAllTargetsAreBrowsers = mUseLayoutForBrowsables; |
| 1820 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1821 | // First put the initial items at the top. |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1822 | if (mInitialIntents != null) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1823 | for (int i = 0; i < mInitialIntents.length; i++) { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1824 | Intent ii = mInitialIntents[i]; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1825 | if (ii == null) { |
| 1826 | continue; |
| 1827 | } |
| 1828 | ActivityInfo ai = ii.resolveActivityInfo( |
| 1829 | getPackageManager(), 0); |
| 1830 | if (ai == null) { |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1831 | Log.w(TAG, "No activity found for " + ii); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1832 | continue; |
| 1833 | } |
| 1834 | ResolveInfo ri = new ResolveInfo(); |
| 1835 | ri.activityInfo = ai; |
Nicolas Prevot | 1a81592 | 2014-10-10 16:22:38 +0100 | [diff] [blame] | 1836 | UserManager userManager = |
| 1837 | (UserManager) getSystemService(Context.USER_SERVICE); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1838 | if (ii instanceof LabeledIntent) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1839 | LabeledIntent li = (LabeledIntent) ii; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1840 | ri.resolvePackageName = li.getSourcePackage(); |
| 1841 | ri.labelRes = li.getLabelResource(); |
| 1842 | ri.nonLocalizedLabel = li.getNonLocalizedLabel(); |
| 1843 | ri.icon = li.getIconResource(); |
Sudheer Shanka | 9ded760 | 2015-05-19 21:17:25 +0100 | [diff] [blame] | 1844 | ri.iconResourceId = ri.icon; |
| 1845 | } |
| 1846 | if (userManager.isManagedProfile()) { |
| 1847 | ri.noResourceId = true; |
| 1848 | ri.icon = 0; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1849 | } |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1850 | mAllTargetsAreBrowsers &= ri.handleAllWebDataURI; |
| 1851 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1852 | addResolveInfo(new DisplayResolveInfo(ii, ri, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1853 | ri.loadLabel(getPackageManager()), null, ii)); |
| 1854 | } |
| 1855 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1856 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1857 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 1858 | for (ResolvedComponentInfo rci : sortedComponents) { |
| 1859 | final ResolveInfo ri = rci.getResolveInfoAt(0); |
| 1860 | if (ri != null) { |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 1861 | mAllTargetsAreBrowsers &= ri.handleAllWebDataURI; |
| 1862 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 1863 | ResolveInfoPresentationGetter pg = makePresentationGetter(ri); |
| 1864 | addResolveInfoWithAlternates(rci, pg.getSubLabel(), pg.getLabel()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1865 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1866 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1867 | } |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1868 | |
Matt Pietal | 553d8a4 | 2019-09-10 09:33:11 -0400 | [diff] [blame] | 1869 | sendVoiceChoicesIfNeeded(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1870 | postListReadyRunnable(); |
| 1871 | } |
| 1872 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1873 | |
| 1874 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1875 | /** |
| 1876 | * Some necessary methods for creating the list are initiated in onCreate and will also |
| 1877 | * determine the layout known. We therefore can't update the UI inline and post to the |
| 1878 | * handler thread to update after the current task is finished. |
| 1879 | */ |
| 1880 | private void postListReadyRunnable() { |
| 1881 | if (mPostListReadyRunnable == null) { |
| 1882 | mPostListReadyRunnable = new Runnable() { |
| 1883 | @Override |
| 1884 | public void run() { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1885 | setHeader(); |
shafik | 69df9632 | 2018-12-18 15:41:19 +0000 | [diff] [blame] | 1886 | resetButtonBar(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1887 | onListRebuilt(); |
| 1888 | mPostListReadyRunnable = null; |
| 1889 | } |
| 1890 | }; |
| 1891 | getMainThreadHandler().post(mPostListReadyRunnable); |
| 1892 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | public void onListRebuilt() { |
Xiaohui Chen | 393c801 | 2017-02-14 14:55:07 -0800 | [diff] [blame] | 1896 | int count = getUnfilteredCount(); |
| 1897 | if (count == 1 && getOtherProfile() == null) { |
| 1898 | // Only one target, so we're a candidate to auto-launch! |
| 1899 | final TargetInfo target = targetInfoForPosition(0, false); |
| 1900 | if (shouldAutoLaunchSingleChoice(target)) { |
| 1901 | safelyStartActivity(target); |
| 1902 | finish(); |
| 1903 | } |
| 1904 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
Adam Powell | c6d5e3a | 2015-04-23 12:22:20 -0700 | [diff] [blame] | 1907 | public boolean shouldGetResolvedFilter() { |
| 1908 | return mFilterLastUsed; |
| 1909 | } |
| 1910 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1911 | private void addResolveInfoWithAlternates(ResolvedComponentInfo rci, |
| 1912 | CharSequence extraInfo, CharSequence roLabel) { |
| 1913 | final int count = rci.getCount(); |
| 1914 | final Intent intent = rci.getIntentAt(0); |
| 1915 | final ResolveInfo add = rci.getResolveInfoAt(0); |
| 1916 | final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent); |
| 1917 | final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel, |
| 1918 | extraInfo, replaceIntent); |
| 1919 | addResolveInfo(dri); |
| 1920 | if (replaceIntent == intent) { |
| 1921 | // Only add alternates if we didn't get a specific replacement from |
| 1922 | // the caller. If we have one it trumps potential alternates. |
| 1923 | for (int i = 1, N = count; i < N; i++) { |
| 1924 | final Intent altIntent = rci.getIntentAt(i); |
| 1925 | dri.addAlternateSourceIntent(altIntent); |
| 1926 | } |
| 1927 | } |
| 1928 | updateLastChosenPosition(add); |
| 1929 | } |
| 1930 | |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1931 | private void updateLastChosenPosition(ResolveInfo info) { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1932 | // If another profile is present, ignore the last chosen entry. |
| 1933 | if (mOtherProfile != null) { |
| 1934 | mLastChosenPosition = -1; |
| 1935 | return; |
| 1936 | } |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1937 | if (mLastChosen != null |
| 1938 | && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName) |
| 1939 | && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1940 | mLastChosenPosition = mDisplayList.size() - 1; |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1944 | // We assume that at this point we've already filtered out the only intent for a different |
| 1945 | // targetUserId which we're going to use. |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1946 | private void addResolveInfo(DisplayResolveInfo dri) { |
Kang Li | 6afa4f2 | 2017-06-23 12:54:38 -0700 | [diff] [blame] | 1947 | if (dri != null && dri.mResolveInfo != null |
| 1948 | && dri.mResolveInfo.targetUserId == UserHandle.USER_CURRENT) { |
| 1949 | // Checks if this info is already listed in display. |
| 1950 | for (DisplayResolveInfo existingInfo : mDisplayList) { |
| 1951 | if (resolveInfoMatch(dri.mResolveInfo, existingInfo.mResolveInfo)) { |
| 1952 | return; |
| 1953 | } |
| 1954 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1955 | mDisplayList.add(dri); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1959 | @Nullable |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1960 | public ResolveInfo resolveInfoForPosition(int position, boolean filtered) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1961 | TargetInfo target = targetInfoForPosition(position, filtered); |
| 1962 | if (target != null) { |
| 1963 | return target.getResolveInfo(); |
| 1964 | } |
| 1965 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1966 | } |
| 1967 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1968 | @Nullable |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1969 | public TargetInfo targetInfoForPosition(int position, boolean filtered) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1970 | if (filtered) { |
| 1971 | return getItem(position); |
| 1972 | } |
| 1973 | if (mDisplayList.size() > position) { |
| 1974 | return mDisplayList.get(position); |
| 1975 | } |
| 1976 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | public int getCount() { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1980 | int totalSize = mDisplayList == null || mDisplayList.isEmpty() ? mPlaceholderCount : |
| 1981 | mDisplayList.size(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1982 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1983 | totalSize--; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1984 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1985 | return totalSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1986 | } |
| 1987 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1988 | public int getUnfilteredCount() { |
| 1989 | return mDisplayList.size(); |
| 1990 | } |
| 1991 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1992 | @Nullable |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1993 | public TargetInfo getItem(int position) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1994 | if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) { |
| 1995 | position++; |
| 1996 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1997 | if (mDisplayList.size() > position) { |
| 1998 | return mDisplayList.get(position); |
| 1999 | } else { |
| 2000 | return null; |
| 2001 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | public long getItemId(int position) { |
| 2005 | return position; |
| 2006 | } |
| 2007 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 2008 | public int getDisplayResolveInfoCount() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2009 | return mDisplayList.size(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 2012 | public DisplayResolveInfo getDisplayResolveInfo(int index) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2013 | // Used to query services. We only query services for primary targets, not alternates. |
| 2014 | return mDisplayList.get(index); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | public final View getView(int position, View convertView, ViewGroup parent) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 2018 | View view = convertView; |
| 2019 | if (view == null) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2020 | view = createView(parent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2021 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2022 | onBindView(view, getItem(position)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2023 | return view; |
| 2024 | } |
| 2025 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2026 | public final View createView(ViewGroup parent) { |
| 2027 | final View view = onCreateView(parent); |
| 2028 | final ViewHolder holder = new ViewHolder(view); |
| 2029 | view.setTag(holder); |
| 2030 | return view; |
| 2031 | } |
| 2032 | |
| 2033 | public View onCreateView(ViewGroup parent) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2034 | return mInflater.inflate( |
| 2035 | com.android.internal.R.layout.resolve_list_item, parent, false); |
| 2036 | } |
| 2037 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2038 | public final void bindView(int position, View view) { |
| 2039 | onBindView(view, getItem(position)); |
| 2040 | } |
| 2041 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 2042 | protected void onBindView(View view, TargetInfo info) { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 2043 | final ViewHolder holder = (ViewHolder) view.getTag(); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 2044 | if (info == null) { |
| 2045 | holder.icon.setImageDrawable( |
| 2046 | getDrawable(R.drawable.resolver_icon_placeholder)); |
| 2047 | return; |
| 2048 | } |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 2049 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2050 | final CharSequence label = info.getDisplayLabel(); |
| 2051 | if (!TextUtils.equals(holder.text.getText(), label)) { |
| 2052 | holder.text.setText(info.getDisplayLabel()); |
| 2053 | } |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 2054 | |
| 2055 | // Always show a subLabel for visual consistency across list items. Show an empty |
| 2056 | // subLabel if the subLabel is the same as the label |
| 2057 | CharSequence subLabel = info.getExtendedInfo(); |
| 2058 | if (TextUtils.equals(label, subLabel)) subLabel = null; |
| 2059 | |
| 2060 | if (!TextUtils.equals(holder.text2.getText(), subLabel)) { |
| 2061 | holder.text2.setText(subLabel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2062 | } |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 2063 | |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 2064 | if (info.isSuspended()) { |
| 2065 | holder.icon.setColorFilter(mSuspendedMatrixColorFilter); |
| 2066 | } else { |
| 2067 | holder.icon.setColorFilter(null); |
| 2068 | } |
| 2069 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2070 | if (info instanceof DisplayResolveInfo |
| 2071 | && !((DisplayResolveInfo) info).hasDisplayIcon()) { |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2072 | new LoadIconTask((DisplayResolveInfo) info, holder.icon).execute(); |
| 2073 | } else { |
| 2074 | holder.icon.setImageDrawable(info.getDisplayIcon()); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2075 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2079 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 2080 | @VisibleForTesting |
| 2081 | public static final class ResolvedComponentInfo { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2082 | public final ComponentName name; |
| 2083 | private final List<Intent> mIntents = new ArrayList<>(); |
| 2084 | private final List<ResolveInfo> mResolveInfos = new ArrayList<>(); |
| 2085 | |
| 2086 | public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) { |
| 2087 | this.name = name; |
| 2088 | add(intent, info); |
| 2089 | } |
| 2090 | |
| 2091 | public void add(Intent intent, ResolveInfo info) { |
| 2092 | mIntents.add(intent); |
| 2093 | mResolveInfos.add(info); |
| 2094 | } |
| 2095 | |
| 2096 | public int getCount() { |
| 2097 | return mIntents.size(); |
| 2098 | } |
| 2099 | |
| 2100 | public Intent getIntentAt(int index) { |
| 2101 | return index >= 0 ? mIntents.get(index) : null; |
| 2102 | } |
| 2103 | |
| 2104 | public ResolveInfo getResolveInfoAt(int index) { |
| 2105 | return index >= 0 ? mResolveInfos.get(index) : null; |
| 2106 | } |
| 2107 | |
| 2108 | public int findIntent(Intent intent) { |
| 2109 | for (int i = 0, N = mIntents.size(); i < N; i++) { |
| 2110 | if (intent.equals(mIntents.get(i))) { |
| 2111 | return i; |
| 2112 | } |
| 2113 | } |
| 2114 | return -1; |
| 2115 | } |
| 2116 | |
| 2117 | public int findResolveInfo(ResolveInfo info) { |
| 2118 | for (int i = 0, N = mResolveInfos.size(); i < N; i++) { |
| 2119 | if (info.equals(mResolveInfos.get(i))) { |
| 2120 | return i; |
| 2121 | } |
| 2122 | } |
| 2123 | return -1; |
| 2124 | } |
| 2125 | } |
| 2126 | |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 2127 | static class ViewHolder { |
Mike Digman | 4b83c21 | 2019-05-03 10:17:35 -0700 | [diff] [blame] | 2128 | public View itemView; |
| 2129 | public Drawable defaultItemViewBackground; |
| 2130 | |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 2131 | public TextView text; |
| 2132 | public TextView text2; |
| 2133 | public ImageView icon; |
| 2134 | |
| 2135 | public ViewHolder(View view) { |
Mike Digman | 4b83c21 | 2019-05-03 10:17:35 -0700 | [diff] [blame] | 2136 | itemView = view; |
| 2137 | defaultItemViewBackground = view.getBackground(); |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 2138 | text = (TextView) view.findViewById(com.android.internal.R.id.text1); |
| 2139 | text2 = (TextView) view.findViewById(com.android.internal.R.id.text2); |
| 2140 | icon = (ImageView) view.findViewById(R.id.icon); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2144 | class ItemClickListener implements AdapterView.OnItemClickListener, |
| 2145 | AdapterView.OnItemLongClickListener { |
| 2146 | @Override |
| 2147 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 2148 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 2149 | if (listView != null) { |
| 2150 | position -= listView.getHeaderViewsCount(); |
| 2151 | } |
| 2152 | if (position < 0) { |
| 2153 | // Header views don't count. |
| 2154 | return; |
| 2155 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 2156 | // If we're still loading, we can't yet enable the buttons. |
| 2157 | if (mAdapter.resolveInfoForPosition(position, true) == null) { |
| 2158 | return; |
| 2159 | } |
| 2160 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2161 | final int checkedPos = mAdapterView.getCheckedItemPosition(); |
| 2162 | final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 2163 | if (!useLayoutWithDefault() |
Ruchi Kandoi | 65d76c1 | 2017-08-18 13:05:39 -0700 | [diff] [blame] | 2164 | && (!hasValidSelection || mLastSelected != checkedPos) |
Matt Pietal | 0d6834a | 2019-06-27 13:27:52 -0400 | [diff] [blame] | 2165 | && mAlwaysButton != null) { |
| 2166 | setAlwaysButtonEnabled(hasValidSelection, checkedPos, true); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2167 | mOnceButton.setEnabled(hasValidSelection); |
| 2168 | if (hasValidSelection) { |
| 2169 | mAdapterView.smoothScrollToPosition(checkedPos); |
| 2170 | } |
| 2171 | mLastSelected = checkedPos; |
| 2172 | } else { |
| 2173 | startSelected(position, false, true); |
| 2174 | } |
| 2175 | } |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 2176 | |
| 2177 | @Override |
| 2178 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2179 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 2180 | if (listView != null) { |
| 2181 | position -= listView.getHeaderViewsCount(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2182 | } |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 2183 | if (position < 0) { |
| 2184 | // Header views don't count. |
| 2185 | return false; |
| 2186 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 2187 | ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 2188 | showTargetDetails(ri); |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 2189 | return true; |
| 2190 | } |
| 2191 | |
| 2192 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 2193 | |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2194 | class LoadIconTask extends AsyncTask<Void, Void, Drawable> { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2195 | protected final DisplayResolveInfo mDisplayResolveInfo; |
| 2196 | private final ResolveInfo mResolveInfo; |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2197 | private final ImageView mTargetView; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2198 | |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2199 | LoadIconTask(DisplayResolveInfo dri, ImageView target) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2200 | mDisplayResolveInfo = dri; |
| 2201 | mResolveInfo = dri.getResolveInfo(); |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2202 | mTargetView = target; |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | @Override |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2206 | protected Drawable doInBackground(Void... params) { |
| 2207 | return loadIconForResolveInfo(mResolveInfo); |
| 2208 | } |
| 2209 | |
| 2210 | @Override |
| 2211 | protected void onPostExecute(Drawable d) { |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 2212 | if (mAdapter.getOtherProfile() == mDisplayResolveInfo) { |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 2213 | bindProfileView(); |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2214 | } else { |
| 2215 | mDisplayResolveInfo.setDisplayIcon(d); |
| 2216 | mTargetView.setImageDrawable(d); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 2217 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 2218 | } |
| 2219 | } |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 2220 | |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 2221 | static final boolean isSpecificUriMatch(int match) { |
| 2222 | match = match&IntentFilter.MATCH_CATEGORY_MASK; |
| 2223 | return match >= IntentFilter.MATCH_CATEGORY_HOST |
| 2224 | && match <= IntentFilter.MATCH_CATEGORY_PATH; |
| 2225 | } |
| 2226 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 2227 | static class PickTargetOptionRequest extends PickOptionRequest { |
| 2228 | public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options, |
| 2229 | @Nullable Bundle extras) { |
| 2230 | super(prompt, options, extras); |
| 2231 | } |
| 2232 | |
| 2233 | @Override |
| 2234 | public void onCancel() { |
| 2235 | super.onCancel(); |
| 2236 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 2237 | if (ra != null) { |
| 2238 | ra.mPickOptionRequest = null; |
| 2239 | ra.finish(); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | @Override |
| 2244 | public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) { |
| 2245 | super.onPickOptionResult(finished, selections, result); |
| 2246 | if (selections.length != 1) { |
| 2247 | // TODO In a better world we would filter the UI presented here and let the |
| 2248 | // user refine. Maybe later. |
| 2249 | return; |
| 2250 | } |
| 2251 | |
| 2252 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 2253 | if (ra != null) { |
| 2254 | final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex()); |
| 2255 | if (ra.onTargetSelected(ti, false)) { |
| 2256 | ra.mPickOptionRequest = null; |
| 2257 | ra.finish(); |
| 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2262 | } |