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