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