blob: 0a01beba659fd5d3f30ddb8a677fdda9a943de3d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.internal.app;
18
Hyunyoung Song16b7af42018-10-05 14:00:42 -070019import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
20
Adam Powell4c470d62015-06-19 17:46:17 -070021import android.annotation.Nullable;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -070022import android.annotation.StringRes;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080023import android.annotation.UiThread;
Andrei Onea15884392019-03-22 17:28:11 +000024import android.annotation.UnsupportedAppUsage;
Adam Powelle9414d92014-07-05 17:44:18 -070025import android.app.Activity;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080026import android.app.ActivityManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070027import android.app.ActivityTaskManager;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070028import android.app.ActivityThread;
Adam Powell4c470d62015-06-19 17:46:17 -070029import android.app.VoiceInteractor.PickOptionRequest;
30import android.app.VoiceInteractor.PickOptionRequest.Option;
31import android.app.VoiceInteractor.Prompt;
shafik34064c32019-03-06 15:42:52 +000032import android.app.role.RoleManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.ComponentName;
34import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.Intent;
36import android.content.IntentFilter;
37import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010038import android.content.pm.ApplicationInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -070039import android.content.pm.LabeledIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070041import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010043import android.content.pm.UserInfo;
Jiongxuan Gao3365b162016-11-24 20:20:27 +090044import android.content.res.Configuration;
Adam Powellc5878612012-05-04 18:42:38 -070045import android.content.res.Resources;
Mike Digman9c4ae502019-03-19 17:02:25 -070046import android.graphics.Bitmap;
Matt Pietala4b30072019-04-04 13:44:36 -040047import android.graphics.ColorMatrix;
48import android.graphics.ColorMatrixColorFilter;
Matt Pietal800136a2019-05-08 07:46:39 -040049import android.graphics.Insets;
Mike Digman220587e2019-03-13 11:21:42 -070050import android.graphics.drawable.BitmapDrawable;
Dianne Hackborneb034652009-09-07 00:49:58 -070051import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.net.Uri;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080053import android.os.AsyncTask;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010054import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040056import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.PatternMatcher;
Mike Digman220587e2019-03-13 11:21:42 -070058import android.os.Process;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080059import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070060import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070061import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010062import android.os.UserManager;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080063import android.provider.MediaStore;
64import android.provider.Settings;
65import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.Log;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080067import android.util.Slog;
Adam Powell2d809622012-03-22 15:24:43 -070068import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.view.View;
70import android.view.ViewGroup;
Matt Pietal800136a2019-05-08 07:46:39 -040071import android.view.ViewGroup.LayoutParams;
72import android.view.WindowInsets;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080073import android.widget.AbsListView;
Adam Powell2d809622012-03-22 15:24:43 -070074import android.widget.AdapterView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.widget.BaseAdapter;
Adam Powellc5878612012-05-04 18:42:38 -070076import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070078import android.widget.ListView;
Matt Pietal800136a2019-05-08 07:46:39 -040079import android.widget.Space;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010081import android.widget.Toast;
Hyunyoung Song16b7af42018-10-05 14:00:42 -070082
Hakan Seyalioglu13405c52017-01-31 19:01:31 -080083import com.android.internal.R;
84import com.android.internal.annotations.VisibleForTesting;
85import com.android.internal.content.PackageMonitor;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010086import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010087import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070088import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010091import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.util.Iterator;
93import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070094import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import 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 Seyalioglue1276bf2016-12-07 16:38:57 -0800102@UiThread
Adam Powell7d758002015-05-06 17:49:36 -0700103public class ResolverActivity extends Activity {
Adam Powellc5878612012-05-04 18:42:38 -0700104
Alison Cichowlas4691ed42018-11-13 15:59:55 -0500105 // Temporary flag for new chooser delegate behavior.
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -0500106 boolean mEnableChooserDelegate = true;
Alison Cichowlas4691ed42018-11-13 15:59:55 -0500107
Andrei Onea15884392019-03-22 17:28:11 +0000108 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800109 protected ResolveListAdapter mAdapter;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700110 private boolean mSafeForwardingMode;
Matt Pietal5b648562019-03-12 07:40:26 -0400111 protected AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -0700112 private Button mAlwaysButton;
113 private Button mOnceButton;
shafik69df96322018-12-18 15:41:19 +0000114 private Button mSettingsButton;
Matt Pietal74c6ed02019-04-18 13:38:46 -0400115 protected View mProfileView;
Adam Powellc5878612012-05-04 18:42:38 -0700116 private int mIconDpi;
Adam Powell24428412015-04-01 17:19:56 -0700117 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100118 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000119 private int mProfileSwitchMessageId = -1;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800120 private int mLayoutId;
Adam Powell2ed547e2015-04-29 18:45:04 -0700121 private final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powell4c470d62015-06-19 17:46:17 -0700122 private PickTargetOptionRequest mPickOptionRequest;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800123 private String mReferrerPackage;
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800124 private CharSequence mTitle;
125 private int mDefaultTitleResId;
shafik69df96322018-12-18 15:41:19 +0000126 private boolean mUseLayoutForBrowsables;
Adam Powell09a65602014-07-20 16:23:14 -0700127
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800128 // Whether or not this activity supports choosing a default handler for the intent.
129 private boolean mSupportsAlwaysUseOption;
Adam Powell63b31692015-09-28 10:45:00 -0700130 protected ResolverDrawerLayout mResolverDrawerLayout;
Andrei Onea15884392019-03-22 17:28:11 +0000131 @UnsupportedAppUsage
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800132 protected PackageManager mPm;
133 protected int mLaunchedFromUid;
134
135 private static final String TAG = "ResolverActivity";
136 private static final boolean DEBUG = false;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800137 private Runnable mPostListReadyRunnable;
Adam Powell63b31692015-09-28 10:45:00 -0700138
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700139 private boolean mRegistered;
Jorim Jaggif631ef72017-02-24 13:49:47 +0100140
Matt Pietala4b30072019-04-04 13:44:36 -0400141 private ColorMatrixColorFilter mSuspendedMatrixColorFilter;
142
Matt Pietal800136a2019-05-08 07:46:39 -0400143 protected Insets mSystemWindowInsets = null;
144 private Space mFooterSpacer = null;
145
Jorim Jaggif631ef72017-02-24 13:49:47 +0100146 /** See {@link #setRetainInOnStop}. */
147 private boolean mRetainInOnStop;
148
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800149 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
150 @Override public void onSomePackagesChanged() {
151 mAdapter.handlePackagesChanged();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400152 bindProfileView();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800153 }
Xiaohui Chen393c8012017-02-14 14:55:07 -0800154
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 Hackborne8f2c7f2012-03-01 17:30:53 -0800161 };
162
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700163 /**
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 Powell278902c2014-07-12 18:33:22 -0700175 private enum ActionTitle {
176 VIEW(Intent.ACTION_VIEW,
177 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700178 com.android.internal.R.string.whichViewApplicationNamed,
179 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700180 EDIT(Intent.ACTION_EDIT,
181 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700182 com.android.internal.R.string.whichEditApplicationNamed,
183 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700184 SEND(Intent.ACTION_SEND,
185 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700186 com.android.internal.R.string.whichSendApplicationNamed,
187 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700188 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700189 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700190 com.android.internal.R.string.whichSendToApplicationNamed,
191 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700192 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
193 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700194 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 Powell278902c2014-07-12 18:33:22 -0700200 DEFAULT(null,
201 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700202 com.android.internal.R.string.whichApplicationNamed,
203 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700204 HOME(Intent.ACTION_MAIN,
205 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700206 com.android.internal.R.string.whichHomeApplicationNamed,
207 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700208
shafik0c7c5d52019-02-27 12:13:25 +0000209 // titles for layout that deals with http(s) intents
shafik69df96322018-12-18 15:41:19 +0000210 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 Powell278902c2014-07-12 18:33:22 -0700215 public final String action;
216 public final int titleRes;
217 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700218 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700219
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700220 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700221 this.action = action;
222 this.titleRes = titleRes;
223 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700224 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700225 }
226
227 public static ActionTitle forAction(String action) {
228 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700229 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700230 return title;
231 }
232 }
233 return DEFAULT;
234 }
235 }
236
Dianne Hackborn905577f2011-09-07 18:31:28 -0700237 private Intent makeMyIntent() {
238 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700239 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700240 // 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 Project9066cfe2009-03-03 19:31:44 -0800249 @Override
250 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700251 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700252 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 Alewijnse6c9eee82014-07-17 10:03:31 +0100258 // Note: this field is not set to true in the compatibility version.
259 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700260 }
261
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700262 setSafeForwardingMode(true);
263
Adam Powella35c77a2014-09-25 16:46:36 -0700264 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
266
Adam Powell278902c2014-07-12 18:33:22 -0700267 /**
Adam Powell24428412015-04-01 17:19:56 -0700268 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700269 * a default title resource
270 */
Andrei Onea15884392019-03-22 17:28:11 +0000271 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700273 CharSequence title, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800274 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
275 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList,
276 supportsAlwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700277 }
278
279 protected void onCreate(Bundle savedInstanceState, Intent intent,
280 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800281 List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700282 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000284
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 Hackborn5320eb82012-05-18 12:05:04 -0700289 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -0700290 mLaunchedFromUid = ActivityTaskManager.getService().getLaunchedFromUid(
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700291 getActivityToken());
292 } catch (RemoteException e) {
293 mLaunchedFromUid = -1;
294 }
Adam Powell7d758002015-05-06 17:49:36 -0700295
296 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
297 // Gulp!
298 finish();
299 return;
300 }
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700303
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700304 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700305 mRegistered = true;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800306 mReferrerPackage = getReferrerPackageName();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800307
Adam Powellc5878612012-05-04 18:42:38 -0700308 final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
309 mIconDpi = am.getLauncherLargeIconDensity();
Adam Powellc5878612012-05-04 18:42:38 -0700310
Adam Powell7d758002015-05-06 17:49:36 -0700311 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700312 mIntents.add(0, new Intent(intent));
Hakan Seyalioglu33550122017-01-06 19:54:43 -0800313 mTitle = title;
314 mDefaultTitleResId = defaultTitleRes;
Adam Powell278902c2014-07-12 18:33:22 -0700315
shafik69df96322018-12-18 15:41:19 +0000316 mUseLayoutForBrowsables = getTargetIntent() == null
317 ? false
shafik0c7c5d52019-02-27 12:13:25 +0000318 : isHttpSchemeAndViewAction(getTargetIntent());
shafik69df96322018-12-18 15:41:19 +0000319
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 Seyalioglu33550122017-01-06 19:54:43 -0800324 if (configureContentView(mIntents, initialIntents, rList)) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700325 return;
326 }
Adam Powell278902c2014-07-12 18:33:22 -0700327
Alan Viverette51efddb2017-04-05 10:00:01 -0400328 final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel);
Adam Powell4f6c2052014-07-07 18:49:10 -0700329 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700330 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700331 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700332 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700333 finish();
334 }
335 });
Adam Powell4c470d62015-06-19 17:46:17 -0700336 if (isVoiceInteraction()) {
337 rdl.setCollapsed(false);
338 }
Matt Pietal800136a2019-05-08 07:46:39 -0400339
340 rdl.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
341 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
342 rdl.setOnApplyWindowInsetsListener(this::onApplyWindowInsets);
343
Adam Powell63b31692015-09-28 10:45:00 -0700344 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700345 }
346
Adam Powell88831a22014-11-20 18:17:00 -0800347 mProfileView = findViewById(R.id.profile_button);
348 if (mProfileView != null) {
Matt Pietal74c6ed02019-04-18 13:38:46 -0400349 mProfileView.setOnClickListener(this::onProfileClick);
Adam Powell88831a22014-11-20 18:17:00 -0800350 bindProfileView();
351 }
Adam Powell4c470d62015-06-19 17:46:17 -0700352
Matt Pietala4b30072019-04-04 13:44:36 -0400353 initSuspendedColorMatrix();
354
Adam Powell4c470d62015-06-19 17:46:17 -0700355 if (isVoiceInteraction()) {
356 onSetupVoiceInteraction();
357 }
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100358 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100359 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 Bayarri9550f5d2016-05-11 11:20:13 +0100363 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700364 }
365
Matt Pietal74c6ed02019-04-18 13:38:46 -0400366 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 Pietal800136a2019-05-08 07:46:39 -0400379 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 Pietal32922ca2019-05-29 07:39:21 -0400386 if (emptyView != null) {
387 emptyView.setPadding(0, 0, 0, mSystemWindowInsets.bottom
388 + getResources().getDimensionPixelSize(
389 R.dimen.chooser_edge_margin_normal) * 2);
390 }
Matt Pietal800136a2019-05-08 07:46:39 -0400391
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 Gao3365b162016-11-24 20:20:27 +0900404 @Override
405 public void onConfigurationChanged(Configuration newConfig) {
406 super.onConfigurationChanged(newConfig);
407 mAdapter.handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400408
409 if (mSystemWindowInsets != null) {
410 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
411 mSystemWindowInsets.right, 0);
412 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900413 }
414
Matt Pietala4b30072019-04-04 13:44:36 -0400415 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 Powell4c470d62015-06-19 17:46:17 -0700434 /**
435 * Perform any initialization needed for voice interaction.
436 */
Adam Powell23882512016-01-29 10:21:00 -0800437 public void onSetupVoiceInteraction() {
Adam Powell4c470d62015-06-19 17:46:17 -0700438 // Do it right now. Subclasses may delay this and send it later.
439 sendVoiceChoicesIfNeeded();
440 }
441
Adam Powell23882512016-01-29 10:21:00 -0800442 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700443 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 Powell88831a22014-11-20 18:17:00 -0800461 }
462
Adam Powell2ed547e2015-04-29 18:45:04 -0700463 protected final void setAdditionalTargets(Intent[] intents) {
464 if (intents != null) {
465 for (Intent intent : intents) {
466 mIntents.add(intent);
467 }
468 }
469 }
470
Adam Powell0ccc0e92015-04-23 17:19:37 -0700471 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700472 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700473 }
474
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800475 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700476 final Uri referrer = getReferrer();
477 if (referrer != null && "android-app".equals(referrer.getScheme())) {
478 return referrer.getHost();
479 }
480 return null;
481 }
482
Adam Powell23882512016-01-29 10:21:00 -0800483 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700484 return R.layout.resolver_list;
485 }
486
Matt Pietal74c6ed02019-04-18 13:38:46 -0400487 protected void bindProfileView() {
488 if (mProfileView == null) {
489 return;
490 }
491
Adam Powell88831a22014-11-20 18:17:00 -0800492 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
493 if (dri != null) {
494 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800495 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 Powell88831a22014-11-20 18:17:00 -0800500 } else {
501 mProfileView.setVisibility(View.GONE);
502 }
Adam Powell278902c2014-07-12 18:33:22 -0700503 }
504
Sander Alewijnsef6545332014-10-31 12:39:02 +0000505 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 Hackborn028ceeb2014-08-17 17:45:48 -0700521 /**
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
shafik69df96322018-12-18 15:41:19 +0000537 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
538 final ActionTitle title = mResolvingHome
539 ? ActionTitle.HOME
540 : ActionTitle.forAction(intent.getAction());
541
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800542 // 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 Seyalioglu13405c52017-01-31 19:01:31 -0800544 final boolean named = mAdapter.getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700545 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
546 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000547 } else if (isHttpSchemeAndViewAction(intent)) {
548 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000549 // 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 Powell278902c2014-07-12 18:33:22 -0700554 } else {
Adam Powell24428412015-04-01 17:19:56 -0700555 return named
556 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
557 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700558 }
Adam Powellc5878612012-05-04 18:42:38 -0700559 }
560
Adam Powelle9414d92014-07-05 17:44:18 -0700561 void dismiss() {
562 if (!isFinishing()) {
563 finish();
564 }
565 }
566
Adam Powellc5878612012-05-04 18:42:38 -0700567
Mike Digman220587e2019-03-13 11:21:42 -0700568 /**
Mike Digmanba232682019-03-27 14:55:26 -0700569 * 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 Digman220587e2019-03-13 11:21:42 -0700574 */
Mike Digmanba232682019-03-27 14:55:26 -0700575 private abstract static class TargetPresentationGetter {
576 @Nullable abstract Drawable getIconSubstituteInternal();
577 @Nullable abstract String getAppSubLabelInternal();
Mike Digman220587e2019-03-13 11:21:42 -0700578
Mike Digmanba232682019-03-27 14:55:26 -0700579 private Context mCtx;
Mike Digmanba232682019-03-27 14:55:26 -0700580 private final int mIconDpi;
Mike Digman9c4ae502019-03-19 17:02:25 -0700581 private final boolean mHasSubstitutePermission;
Mike Digmanc0ef5542019-04-01 15:36:25 -0700582 private final ApplicationInfo mAi;
583
584 protected PackageManager mPm;
Mike Digman9c4ae502019-03-19 17:02:25 -0700585
Mike Digmanba232682019-03-27 14:55:26 -0700586 TargetPresentationGetter(Context ctx, int iconDpi, ApplicationInfo ai) {
587 mCtx = ctx;
588 mPm = ctx.getPackageManager();
Mike Digman9c4ae502019-03-19 17:02:25 -0700589 mAi = ai;
Mike Digmanba232682019-03-27 14:55:26 -0700590 mIconDpi = iconDpi;
Mike Digman9c4ae502019-03-19 17:02:25 -0700591 mHasSubstitutePermission = PackageManager.PERMISSION_GRANTED == mPm.checkPermission(
592 android.Manifest.permission.SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON,
593 mAi.packageName);
594 }
595
Mike Digmanb2e5e712019-04-19 15:49:10 -0700596 public Drawable getIcon(UserHandle userHandle) {
597 return new BitmapDrawable(mCtx.getResources(), getIconBitmap(userHandle));
Mike Digman9c4ae502019-03-19 17:02:25 -0700598 }
599
Mike Digmanb2e5e712019-04-19 15:49:10 -0700600 public Bitmap getIconBitmap(UserHandle userHandle) {
Mike Digman9c4ae502019-03-19 17:02:25 -0700601 Drawable dr = null;
602 if (mHasSubstitutePermission) {
Mike Digmanba232682019-03-27 14:55:26 -0700603 dr = getIconSubstituteInternal();
Mike Digman9c4ae502019-03-19 17:02:25 -0700604 }
605
606 if (dr == null) {
607 try {
608 if (mAi.icon != 0) {
609 dr = loadIconFromResource(mPm.getResourcesForApplication(mAi), mAi.icon);
Mike Digman220587e2019-03-13 11:21:42 -0700610 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700611 } 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 Digmanba232682019-03-27 14:55:26 -0700620 SimpleIconFactory sif = SimpleIconFactory.obtain(mCtx);
Mike Digmanb2e5e712019-04-19 15:49:10 -0700621 Bitmap icon = sif.createUserBadgedIconBitmap(dr, userHandle);
Mike Digman9c4ae502019-03-19 17:02:25 -0700622 sif.recycle();
623
624 return icon;
625 }
626
Mike Digmanba232682019-03-27 14:55:26 -0700627 public String getLabel() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700628 String label = null;
629 // Apps with the substitute permission will always show the sublabel as their label
630 if (mHasSubstitutePermission) {
Mike Digmanba232682019-03-27 14:55:26 -0700631 label = getAppSubLabelInternal();
Mike Digman9c4ae502019-03-19 17:02:25 -0700632 }
633
634 if (label == null) {
635 label = (String) mAi.loadLabel(mPm);
636 }
637
638 return label;
639 }
640
Mike Digmanba232682019-03-27 14:55:26 -0700641 public String getSubLabel() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700642 // Apps with the substitute permission will never have a sublabel
643 if (mHasSubstitutePermission) return null;
Mike Digmanba232682019-03-27 14:55:26 -0700644 return getAppSubLabelInternal();
645 }
646
647 protected String loadLabelFromResource(Resources res, int resId) {
648 return res.getString(resId);
Mike Digman9c4ae502019-03-19 17:02:25 -0700649 }
650
651 @Nullable
652 protected Drawable loadIconFromResource(Resources res, int resId) {
653 return res.getDrawableForDensity(resId, mIconDpi);
654 }
655
656 }
657
Mike Digmanba232682019-03-27 14:55:26 -0700658 /**
659 * Loads the icon and label for the provided ResolveInfo.
660 */
661 @VisibleForTesting
Mike Digmanc0ef5542019-04-01 15:36:25 -0700662 public static class ResolveInfoPresentationGetter extends ActivityInfoPresentationGetter {
Mike Digman9c4ae502019-03-19 17:02:25 -0700663 private final ResolveInfo mRi;
Mike Digmanba232682019-03-27 14:55:26 -0700664 public ResolveInfoPresentationGetter(Context ctx, int iconDpi, ResolveInfo ri) {
Mike Digmanc0ef5542019-04-01 15:36:25 -0700665 super(ctx, iconDpi, ri.activityInfo);
Mike Digman9c4ae502019-03-19 17:02:25 -0700666 mRi = ri;
667 }
668
669 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700670 Drawable getIconSubstituteInternal() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700671 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 Powellc5878612012-05-04 18:42:38 -0700677 }
Mike Digman220587e2019-03-13 11:21:42 -0700678 } 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 Powellc5878612012-05-04 18:42:38 -0700681 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700682
Mike Digmanc0ef5542019-04-01 15:36:25 -0700683 // Fall back to ActivityInfo if no icon is found via ResolveInfo
684 if (dr == null) dr = super.getIconSubstituteInternal();
685
Mike Digman9c4ae502019-03-19 17:02:25 -0700686 return dr;
Adam Powellc5878612012-05-04 18:42:38 -0700687 }
Mike Digman220587e2019-03-13 11:21:42 -0700688
Mike Digman9c4ae502019-03-19 17:02:25 -0700689 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700690 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 Digman9c4ae502019-03-19 17:02:25 -0700693 return (String) mRi.loadLabel(mPm);
694 }
695 }
696
Mike Digmanba232682019-03-27 14:55:26 -0700697 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 Digman9c4ae502019-03-19 17:02:25 -0700706 private final ActivityInfo mActivityInfo;
Mike Digmanba232682019-03-27 14:55:26 -0700707 public ActivityInfoPresentationGetter(Context ctx, int iconDpi,
708 ActivityInfo activityInfo) {
709 super(ctx, iconDpi, activityInfo.applicationInfo);
Mike Digman9c4ae502019-03-19 17:02:25 -0700710 mActivityInfo = activityInfo;
711 }
712
713 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700714 Drawable getIconSubstituteInternal() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700715 Drawable dr = null;
Mike Digman220587e2019-03-13 11:21:42 -0700716 try {
Mike Digman9c4ae502019-03-19 17:02:25 -0700717 // 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 Digman220587e2019-03-13 11:21:42 -0700726 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700727
728 return dr;
Mike Digman220587e2019-03-13 11:21:42 -0700729 }
730
Mike Digman9c4ae502019-03-19 17:02:25 -0700731 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700732 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 Digman9c4ae502019-03-19 17:02:25 -0700735 return (String) mActivityInfo.loadLabel(mPm);
Mike Digman220587e2019-03-13 11:21:42 -0700736 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700737 }
Mike Digman220587e2019-03-13 11:21:42 -0700738
Mike Digmanba232682019-03-27 14:55:26 -0700739 protected ActivityInfoPresentationGetter makePresentationGetter(ActivityInfo ai) {
740 return new ActivityInfoPresentationGetter(this, mIconDpi, ai);
741 }
742
Mike Digman9c4ae502019-03-19 17:02:25 -0700743 Drawable loadIconForResolveInfo(ResolveInfo ri) {
Mike Digmanb2e5e712019-04-19 15:49:10 -0700744 // Load icons based on the current process. If in work profile icons should be badged.
745 return makePresentationGetter(ri).getIcon(Process.myUserHandle());
Adam Powellc5878612012-05-04 18:42:38 -0700746 }
747
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800748 @Override
749 protected void onRestart() {
750 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700751 if (!mRegistered) {
752 mPackageMonitor.register(this, getMainLooper(), false);
753 mRegistered = true;
754 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800755 mAdapter.handlePackagesChanged();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400756 bindProfileView();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800757 }
758
759 @Override
760 protected void onStop() {
761 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700762 if (mRegistered) {
763 mPackageMonitor.unregister();
764 mRegistered = false;
765 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700766 final Intent intent = getIntent();
767 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100768 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700769 // 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 Hackborne8f2c7f2012-03-01 17:30:53 -0800780 }
781
Adam Powellc5878612012-05-04 18:42:38 -0700782 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700783 protected void onDestroy() {
784 super.onDestroy();
785 if (!isChangingConfigurations() && mPickOptionRequest != null) {
786 mPickOptionRequest.cancel();
787 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800788 if (mPostListReadyRunnable != null) {
789 getMainThreadHandler().removeCallbacks(mPostListReadyRunnable);
790 mPostListReadyRunnable = null;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800791 }
Kang Li38a6da642017-04-05 12:30:55 -0700792 if (mAdapter != null && mAdapter.mResolverListController != null) {
793 mAdapter.mResolverListController.destroy();
794 }
Adam Powell4c470d62015-06-19 17:46:17 -0700795 }
796
797 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700798 protected void onRestoreInstanceState(Bundle savedInstanceState) {
799 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000800 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700801 }
802
shafik0c7c5d52019-02-27 12:13:25 +0000803 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 Alewijnse6c9eee82014-07-17 10:03:31 +0100809 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 Powell4c470d62015-06-19 17:46:17 -0700832 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100833 } catch (NameNotFoundException e) {
834 return false;
835 }
836 }
837
Adam Powell278902c2014-07-12 18:33:22 -0700838 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
839 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100840 boolean enabled = false;
841 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700842 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800843 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 Prevot50449882014-06-23 12:42:37 +0100850 enabled = true;
851 }
852 }
853 mAlwaysButton.setEnabled(enabled);
854 }
855
Adam Powellc5878612012-05-04 18:42:38 -0700856 public void onButtonClick(View v) {
857 final int id = v.getId();
shafik69df96322018-12-18 15:41:19 +0000858 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);
shafik34064c32019-03-06 15:42:52 +0000871 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 Powellc5878612012-05-04 18:42:38 -0700883 }
884
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800885 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700886 if (isFinishing()) {
887 return;
888 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800889 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000890 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 Seyalioglu13405c52017-01-31 19:01:31 -0800898 TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800899 if (target == null) {
900 return;
901 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700902 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800903 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100904 MetricsLogger.action(
905 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800906 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100907 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 Powell2ed547e2015-04-29 18:45:04 -0700916 finish();
917 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919
Adam Powelle49d9392014-07-17 18:45:19 -0700920 /**
921 * Replace me in subclasses!
922 */
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000923 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700924 return defIntent;
925 }
926
Adam Powell2ed547e2015-04-29 18:45:04 -0700927 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700928 final ResolveInfo ri = target.getResolveInfo();
929 final Intent intent = target != null ? target.getResolvedIntent() : null;
930
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800931 if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800932 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700933 // Build a reasonable intent filter, based on what matched.
934 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200935 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
Henrik Engström3277cf12014-07-17 12:18:29 +0200937 if (intent.getSelector() != null) {
938 filterIntent = intent.getSelector();
939 } else {
940 filterIntent = intent;
941 }
942
943 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800944 if (action != null) {
945 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200947 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700948 if (categories != null) {
949 for (String cat : categories) {
950 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700953 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954
Adam Powell24428412015-04-01 17:19:56 -0700955 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200956 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700957 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200958 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700959 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 Project9066cfe2009-03-03 19:31:44 -0800965 }
966 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700967 }
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 Hackborndf1c0bf2013-06-12 16:21:38 -0700988 }
989 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700990 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 Project9066cfe2009-03-03 19:31:44 -08001011 }
1012 }
1013 }
1014 }
1015
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001016 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001017 final int N = mAdapter.mUnfilteredResolveList.size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001018 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 Yamasani588dd2a2013-09-03 12:30:44 -07001030 int bestMatch = 0;
1031 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001032 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001033 set[i] = new ComponentName(r.activityInfo.packageName,
1034 r.activityInfo.name);
1035 if (r.match > bestMatch) bestMatch = r.match;
1036 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001037
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 Yamasani588dd2a2013-09-03 12:30:44 -07001044 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001045 final int userId = getUserId();
1046 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001047
1048 // Set the preferred Activity
1049 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
1050
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001051 if (ri.handleAllWebDataURI) {
1052 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001053 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001054 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001055 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001056 }
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 Meglio1c1b4712014-11-19 17:12:32 -08001062
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001063 boolean isHttpOrHttps = (dataScheme != null) &&
1064 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
1065 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001066
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001067 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
1068 boolean hasCategoryBrowsable = (categories != null) &&
1069 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001070
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001071 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001072 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001073 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
1074 userId);
1075 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001076 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001077 } else {
1078 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -08001079 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001080 } catch (RemoteException re) {
1081 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1082 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
1085 }
1086
Adam Powell24428412015-04-01 17:19:56 -07001087 if (target != null) {
1088 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -04001089
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 Hackborn028ceeb2014-08-17 17:45:48 -07001095 }
Matt Pietala4b30072019-04-04 13:44:36 -04001096
Adam Powell2ed547e2015-04-29 18:45:04 -07001097 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001098 }
1099
Adam Powell23882512016-01-29 10:21:00 -08001100 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -07001101 // 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 Alewijnsef6545332014-10-31 12:39:02 +00001112 // 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 Hackborn028ceeb2014-08-17 17:45:48 -07001117 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -07001118 if (cti.start(this, null)) {
1119 onActivityStarted(cti);
1120 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001121 return;
1122 }
1123 try {
Adam Powell24428412015-04-01 17:19:56 -07001124 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
1125 onActivityStarted(cti);
1126 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001127 } catch (RuntimeException e) {
1128 String launchedFromPackage;
1129 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001130 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001131 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 Project9066cfe2009-03-03 19:31:44 -08001138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
1140
Alison Cichowlas3e340502018-08-07 17:15:01 -04001141
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 Cichowlas7b6f3b62018-12-07 09:06:57 -05001165 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1166 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001167 startActivity(chooserIntent);
1168 } catch (RemoteException e) {
1169 Log.e(TAG, e.toString());
1170 }
1171 return true;
1172 }
1173
Adam Powell23882512016-01-29 10:21:00 -08001174 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001175 // Do nothing
1176 }
1177
Adam Powell23882512016-01-29 10:21:00 -08001178 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001179 return false;
1180 }
1181
Adam Powell23882512016-01-29 10:21:00 -08001182 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001183 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001184 }
1185
Adam Powell23882512016-01-29 10:21:00 -08001186 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001187 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001188 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001189 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001190 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001191 }
1192
Adam Powell23882512016-01-29 10:21:00 -08001193 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents,
Adam Powell7d758002015-05-06 17:49:36 -07001194 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
1195 boolean filterLastUsed) {
1196 return new ResolveListAdapter(context, payloadIntents, initialIntents, rList,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001197 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 Powell88831a22014-11-20 18:17:00 -08001208 }
1209
Adam Powell39e94eb2015-09-08 17:01:49 -07001210 /**
1211 * Returns true if the activity is finishing and creation should halt
1212 */
Adam Powell23882512016-01-29 10:21:00 -08001213 public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents,
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001214 List<ResolveInfo> rList) {
Dianne Hackborn57dd7372015-07-27 18:11:14 -07001215 // 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 Powell7d758002015-05-06 17:49:36 -07001220 mAdapter = createAdapter(this, payloadIntents, initialIntents, rList,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001221 mLaunchedFromUid, mSupportsAlwaysUseOption && !isVoiceInteraction());
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001222 boolean rebuildCompleted = mAdapter.rebuildList();
Adam Powell7d758002015-05-06 17:49:36 -07001223
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001224 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001225 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001226 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001227 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001228 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001229 setContentView(mLayoutId);
Adam Powell7d758002015-05-06 17:49:36 -07001230
Adam Powell50077352015-05-26 18:01:55 -07001231 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001232
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 Sharkeycc2ae6b42015-09-29 13:04:46 -07001246 }
1247 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001248
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001249
Alan Viverette51efddb2017-04-05 10:00:01 -04001250 mAdapterView = findViewById(R.id.resolver_list);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001251
1252 if (count == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001253 final TextView emptyView = findViewById(R.id.empty);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001254 emptyView.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -07001255 mAdapterView.setVisibility(View.GONE);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001256 } else {
1257 mAdapterView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001258 onPrepareAdapterView(mAdapterView, mAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001259 }
Adam Powell39e94eb2015-09-08 17:01:49 -07001260 return false;
Adam Powell7d758002015-05-06 17:49:36 -07001261 }
1262
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001263 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001264 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
shafik69df96322018-12-18 15:41:19 +00001273 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001274 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1275 }
1276
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001277 // 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 Powell7d758002015-05-06 17:49:36 -07001280 listView.addHeaderView(LayoutInflater.from(this).inflate(
1281 R.layout.resolver_different_item_header, listView, false));
1282 }
Adam Powell24428412015-04-01 17:19:56 -07001283 }
1284
Matt Pietal26038402019-01-08 07:29:34 -05001285 /**
1286 * Configure the area above the app selection list (title, content preview, etc).
1287 */
1288 public void setHeader() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001289 if (mAdapter.getCount() == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001290 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001291 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001292 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001293 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001294 }
1295
1296 CharSequence title = mTitle != null
1297 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001298 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001299
1300 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001301 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001302 if (titleView != null) {
1303 titleView.setText(title);
1304 }
1305 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001306 }
1307
Alan Viverette51efddb2017-04-05 10:00:01 -04001308 final ImageView iconView = findViewById(R.id.icon);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001309 final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem();
1310 if (iconView != null && iconInfo != null) {
Matt Pietalaf044ae2019-03-29 06:53:53 -04001311 new LoadIconTask(iconInfo, iconView).execute();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001312 }
1313 }
1314
shafik69df96322018-12-18 15:41:19 +00001315 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 Pietal800136a2019-05-08 07:46:39 -04001322 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1323 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
Matt Pietal33b0ac12019-05-24 13:47:18 -04001324 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1325 R.dimen.resolver_button_bar_spacing) + inset);
Matt Pietal800136a2019-05-08 07:46:39 -04001326
shafik69df96322018-12-18 15:41:19 +00001327 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 Seyalioglu13405c52017-01-31 19:01:31 -08001333 } else {
shafik69df96322018-12-18 15:41:19 +00001334 resetAlwaysOrOnceButtonBar();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001335 }
shafik69df96322018-12-18 15:41:19 +00001336 } 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 Seyalioglu33550122017-01-06 19:54:43 -08001361 }
1362
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001363 if (useLayoutWithDefault()
1364 && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001365 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
1366 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001367 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 Seyalioglu33550122017-01-06 19:54:43 -08001375 }
1376 }
1377
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001378 private boolean useLayoutWithDefault() {
1379 return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem();
1380 }
1381
Adam Powellc412be62015-06-24 13:54:10 -07001382 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001383 * 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 Powellc412be62015-06-24 13:54:10 -07001391 * 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 Powell23882512016-01-29 10:21:00 -08001400 public final class DisplayResolveInfo implements TargetInfo {
Adam Powell24428412015-04-01 17:19:56 -07001401 private final ResolveInfo mResolveInfo;
1402 private final CharSequence mDisplayLabel;
1403 private Drawable mDisplayIcon;
Adam Powell00f4aad2015-09-17 13:38:16 -07001404 private Drawable mBadge;
Adam Powell24428412015-04-01 17:19:56 -07001405 private final CharSequence mExtendedInfo;
1406 private final Intent mResolvedIntent;
Adam Powell2ed547e2015-04-29 18:45:04 -07001407 private final List<Intent> mSourceIntents = new ArrayList<>();
Matt Pietala4b30072019-04-04 13:44:36 -04001408 private boolean mIsSuspended;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409
Adam Powell23882512016-01-29 10:21:00 -08001410 public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel,
Dianne Hackborneb034652009-09-07 00:49:58 -07001411 CharSequence pInfo, Intent pOrigIntent) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001412 mSourceIntents.add(originalIntent);
Adam Powell24428412015-04-01 17:19:56 -07001413 mResolveInfo = pri;
1414 mDisplayLabel = pLabel;
1415 mExtendedInfo = pInfo;
1416
1417 final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent :
Adam Powell2ed547e2015-04-29 18:45:04 -07001418 getReplacementIntent(pri.activityInfo, getTargetIntent()));
Adam Powell24428412015-04-01 17:19:56 -07001419 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 Pietala4b30072019-04-04 13:44:36 -04001424 mIsSuspended = (ai.applicationInfo.flags & ApplicationInfo.FLAG_SUSPENDED) != 0;
1425
Adam Powell24428412015-04-01 17:19:56 -07001426 mResolvedIntent = intent;
1427 }
1428
Adam Powell2ed547e2015-04-29 18:45:04 -07001429 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 Powell24428412015-04-01 17:19:56 -07001439 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 Viverettece5d92c2015-07-31 16:46:56 -04001451 @Override
Adam Powell2ed547e2015-04-29 18:45:04 -07001452 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 Powell24428412015-04-01 17:19:56 -07001465 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 Cichowlas3e340502018-08-07 17:15:01 -04001494 public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) {
Alison Cichowlas4691ed42018-11-13 15:59:55 -05001495 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 Powell24428412015-04-01 17:19:56 -07001501 }
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 Project9066cfe2009-03-03 19:31:44 -08001507 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001508
Matt Pietala4b30072019-04-04 13:44:36 -04001509 public boolean isSuspended() {
1510 return mIsSuspended;
1511 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001512 }
1513
1514 List<DisplayResolveInfo> getDisplayList() {
1515 return mAdapter.mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
1517
Adam Powell24428412015-04-01 17:19:56 -07001518 /**
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 Powell23882512016-01-29 10:21:00 -08001529 Intent getResolvedIntent();
Adam Powell24428412015-04-01 17:19:56 -07001530
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 Powell23882512016-01-29 10:21:00 -08001538 ComponentName getResolvedComponentName();
Adam Powell24428412015-04-01 17:19:56 -07001539
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 Powell23882512016-01-29 10:21:00 -08001547 boolean start(Activity activity, Bundle options);
Adam Powell24428412015-04-01 17:19:56 -07001548
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 Cichowlas3e340502018-08-07 17:15:01 -04001558 boolean startAsCaller(ResolverActivity activity, Bundle options, int userId);
Adam Powell24428412015-04-01 17:19:56 -07001559
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 Powell23882512016-01-29 10:21:00 -08001568 boolean startAsUser(Activity activity, Bundle options, UserHandle user);
Adam Powell24428412015-04-01 17:19:56 -07001569
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 Powell23882512016-01-29 10:21:00 -08001576 ResolveInfo getResolveInfo();
Adam Powell24428412015-04-01 17:19:56 -07001577
1578 /**
1579 * Return the human-readable text label for this target.
1580 *
1581 * @return user-visible target label
1582 */
Adam Powell23882512016-01-29 10:21:00 -08001583 CharSequence getDisplayLabel();
Adam Powell24428412015-04-01 17:19:56 -07001584
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 Powell23882512016-01-29 10:21:00 -08001591 CharSequence getExtendedInfo();
Adam Powell24428412015-04-01 17:19:56 -07001592
1593 /**
Mike Digman9c4ae502019-03-19 17:02:25 -07001594 * @return The drawable that should be used to represent this target including badge
Adam Powell24428412015-04-01 17:19:56 -07001595 */
Adam Powell23882512016-01-29 10:21:00 -08001596 Drawable getDisplayIcon();
Adam Powell2ed547e2015-04-29 18:45:04 -07001597
1598 /**
1599 * Clone this target with the given fill-in information.
1600 */
Adam Powell23882512016-01-29 10:21:00 -08001601 TargetInfo cloneFilledIn(Intent fillInIntent, int flags);
Adam Powell2ed547e2015-04-29 18:45:04 -07001602
1603 /**
1604 * @return the list of supported source intents deduped against this single target
1605 */
Adam Powell23882512016-01-29 10:21:00 -08001606 List<Intent> getAllSourceIntents();
Matt Pietala4b30072019-04-04 13:44:36 -04001607
1608 /**
1609 * @return true if this target can be selected by the user
1610 */
1611 boolean isSuspended();
Adam Powell24428412015-04-01 17:19:56 -07001612 }
1613
Adam Powell23882512016-01-29 10:21:00 -08001614 public class ResolveListAdapter extends BaseAdapter {
Adam Powell7d758002015-05-06 17:49:36 -07001615 private final List<Intent> mIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001616 private final Intent[] mInitialIntents;
1617 private final List<ResolveInfo> mBaseResolveList;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001618 protected ResolveInfo mLastChosen;
Adam Powell88831a22014-11-20 18:17:00 -08001619 private DisplayResolveInfo mOtherProfile;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001620 private ResolverListController mResolverListController;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001621 private int mPlaceholderCount;
Adam Powell24428412015-04-01 17:19:56 -07001622
1623 protected final LayoutInflater mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001625 // This one is the list that the Adapter will actually present.
Adam Powell2ed547e2015-04-29 18:45:04 -07001626 List<DisplayResolveInfo> mDisplayList;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001627 List<ResolvedComponentInfo> mUnfilteredResolveList;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001628
Adam Powell278902c2014-07-12 18:33:22 -07001629 private int mLastChosenPosition = -1;
1630 private boolean mFilterLastUsed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
Adam Powell7d758002015-05-06 17:49:36 -07001632 public ResolveListAdapter(Context context, List<Intent> payloadIntents,
1633 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001634 boolean filterLastUsed,
1635 ResolverListController resolverListController) {
Adam Powell7d758002015-05-06 17:49:36 -07001636 mIntents = payloadIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001637 mInitialIntents = initialIntents;
1638 mBaseResolveList = rList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001639 mLaunchedFromUid = launchedFromUid;
Adam Powelle9414d92014-07-05 17:44:18 -07001640 mInflater = LayoutInflater.from(context);
Adam Powell2ed547e2015-04-29 18:45:04 -07001641 mDisplayList = new ArrayList<>();
Adam Powell278902c2014-07-12 18:33:22 -07001642 mFilterLastUsed = filterLastUsed;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001643 mResolverListController = resolverListController;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001646 public void handlePackagesChanged() {
1647 rebuildList();
Esteban Talavera6de72662014-12-11 17:54:07 +00001648 if (getCount() == 0) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001649 // We no longer have any items... just finish the activity.
1650 finish();
Adam Powellc5878612012-05-04 18:42:38 -07001651 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001652 }
1653
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001654 public void setPlaceholderCount(int count) {
1655 mPlaceholderCount = count;
1656 }
1657
Sumir Katariadb688af2017-05-10 17:33:47 -07001658 public int getPlaceholderCount() { return mPlaceholderCount; }
1659
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001660 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001661 public DisplayResolveInfo getFilteredItem() {
1662 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1663 // Not using getItem since it offsets to dodge this position for the list
Adam Powell2ed547e2015-04-29 18:45:04 -07001664 return mDisplayList.get(mLastChosenPosition);
Adam Powell278902c2014-07-12 18:33:22 -07001665 }
1666 return null;
1667 }
1668
Adam Powell88831a22014-11-20 18:17:00 -08001669 public DisplayResolveInfo getOtherProfile() {
1670 return mOtherProfile;
1671 }
1672
Adam Powell278902c2014-07-12 18:33:22 -07001673 public int getFilteredPosition() {
1674 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1675 return mLastChosenPosition;
1676 }
1677 return AbsListView.INVALID_POSITION;
1678 }
1679
1680 public boolean hasFilteredItem() {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001681 return mFilterLastUsed && mLastChosen != null;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001682 }
1683
Adam Powella182e452015-07-06 16:57:56 -07001684 public float getScore(DisplayResolveInfo target) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001685 return mResolverListController.getScore(target);
Adam Powella182e452015-07-06 16:57:56 -07001686 }
1687
Kang Li0cef910d2017-01-05 09:14:36 -08001688 public void updateModel(ComponentName componentName) {
1689 mResolverListController.updateModel(componentName);
1690 }
1691
Kang Li9fa2a2c2017-01-06 13:33:24 -08001692 public void updateChooserCounts(String packageName, int userId, String action) {
1693 mResolverListController.updateChooserCounts(packageName, userId, action);
1694 }
1695
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001696 /**
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 Powell2ed547e2015-04-29 18:45:04 -07001703 List<ResolvedComponentInfo> currentResolveList = null;
Sudheer Shanka7e64e102015-01-23 10:37:45 +00001704 // Clear the value of mOtherProfile from previous call.
1705 mOtherProfile = null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001706 mLastChosen = null;
1707 mLastChosenPosition = -1;
Adam Powell2ed547e2015-04-29 18:45:04 -07001708 mDisplayList.clear();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001709 if (mBaseResolveList != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001710 currentResolveList = mUnfilteredResolveList = new ArrayList<>();
1711 mResolverListController.addResolveListDedupe(currentResolveList,
1712 getTargetIntent(),
1713 mBaseResolveList);
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001714 } else {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001715 currentResolveList = mUnfilteredResolveList =
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001716 mResolverListController.getResolversForIntent(shouldGetResolvedFilter(),
1717 shouldGetActivityMetadata(),
1718 mIntents);
1719 if (currentResolveList == null) {
1720 processSortedList(currentResolveList);
1721 return true;
Adam Powell2ed547e2015-04-29 18:45:04 -07001722 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001723 List<ResolvedComponentInfo> originalList =
1724 mResolverListController.filterIneligibleActivities(currentResolveList,
1725 true);
1726 if (originalList != null) {
1727 mUnfilteredResolveList = originalList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001728 }
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001729 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001730
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 Project9066cfe2009-03-03 19:31:44 -08001754 int N;
You Kim43a5070e2012-11-21 23:23:45 +09001755 if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001756 // 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 Project9066cfe2009-03-03 19:31:44 -08001763 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001764
Hakan Seyalioglu873cbfd2017-02-21 19:23:43 -08001765 if (currentResolveList.size() > 1) {
Sumir Katariadb688af2017-05-10 17:33:47 -07001766 int placeholderCount = currentResolveList.size();
1767 if (useLayoutWithDefault()) {
1768 --placeholderCount;
1769 }
1770 setPlaceholderCount(placeholderCount);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001771 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 Pietal74c6ed02019-04-18 13:38:46 -04001787 bindProfileView();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001788 notifyDataSetChanged();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001789 }
1790 };
1791 sortingTask.execute(currentResolveList);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001792 postListReadyRunnable();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001793 return false;
1794 } else {
1795 processSortedList(currentResolveList);
1796 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001798 } else {
1799 processSortedList(currentResolveList);
1800 return true;
1801 }
1802 }
1803
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001804
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001805 private void processSortedList(List<ResolvedComponentInfo> sortedComponents) {
1806 int N;
1807 if (sortedComponents != null && (N = sortedComponents.size()) != 0) {
Dianne Hackborneb034652009-09-07 00:49:58 -07001808 // First put the initial items at the top.
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001809 if (mInitialIntents != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001810 for (int i = 0; i < mInitialIntents.length; i++) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001811 Intent ii = mInitialIntents[i];
Dianne Hackborneb034652009-09-07 00:49:58 -07001812 if (ii == null) {
1813 continue;
1814 }
1815 ActivityInfo ai = ii.resolveActivityInfo(
1816 getPackageManager(), 0);
1817 if (ai == null) {
Adam Powell09a65602014-07-20 16:23:14 -07001818 Log.w(TAG, "No activity found for " + ii);
Dianne Hackborneb034652009-09-07 00:49:58 -07001819 continue;
1820 }
1821 ResolveInfo ri = new ResolveInfo();
1822 ri.activityInfo = ai;
Nicolas Prevot1a815922014-10-10 16:22:38 +01001823 UserManager userManager =
1824 (UserManager) getSystemService(Context.USER_SERVICE);
Dianne Hackborneb034652009-09-07 00:49:58 -07001825 if (ii instanceof LabeledIntent) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001826 LabeledIntent li = (LabeledIntent) ii;
Dianne Hackborneb034652009-09-07 00:49:58 -07001827 ri.resolvePackageName = li.getSourcePackage();
1828 ri.labelRes = li.getLabelResource();
1829 ri.nonLocalizedLabel = li.getNonLocalizedLabel();
1830 ri.icon = li.getIconResource();
Sudheer Shanka9ded7602015-05-19 21:17:25 +01001831 ri.iconResourceId = ri.icon;
1832 }
1833 if (userManager.isManagedProfile()) {
1834 ri.noResourceId = true;
1835 ri.icon = 0;
Dianne Hackborneb034652009-09-07 00:49:58 -07001836 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001837 addResolveInfo(new DisplayResolveInfo(ii, ri,
Dianne Hackborneb034652009-09-07 00:49:58 -07001838 ri.loadLabel(getPackageManager()), null, ii));
1839 }
1840 }
You Kim43a5070e2012-11-21 23:23:45 +09001841
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001842
Mike Digmanba232682019-03-27 14:55:26 -07001843 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 Project9066cfe2009-03-03 19:31:44 -08001848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001851
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001852
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001853 postListReadyRunnable();
1854 }
1855
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001856
1857
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001858 /**
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 Pietal26038402019-01-08 07:29:34 -05001868 setHeader();
shafik69df96322018-12-18 15:41:19 +00001869 resetButtonBar();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001870 onListRebuilt();
1871 mPostListReadyRunnable = null;
1872 }
1873 };
1874 getMainThreadHandler().post(mPostListReadyRunnable);
1875 }
Adam Powell24428412015-04-01 17:19:56 -07001876 }
1877
1878 public void onListRebuilt() {
Xiaohui Chen393c8012017-02-14 14:55:07 -08001879 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 Project9066cfe2009-03-03 19:31:44 -08001888 }
1889
Adam Powellc6d5e3a2015-04-23 12:22:20 -07001890 public boolean shouldGetResolvedFilter() {
1891 return mFilterLastUsed;
1892 }
1893
Adam Powell2ed547e2015-04-29 18:45:04 -07001894 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 Talavera6de72662014-12-11 17:54:07 +00001914 private void updateLastChosenPosition(ResolveInfo info) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001915 // If another profile is present, ignore the last chosen entry.
1916 if (mOtherProfile != null) {
1917 mLastChosenPosition = -1;
1918 return;
1919 }
Esteban Talavera6de72662014-12-11 17:54:07 +00001920 if (mLastChosen != null
1921 && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName)
1922 && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001923 mLastChosenPosition = mDisplayList.size() - 1;
Esteban Talavera6de72662014-12-11 17:54:07 +00001924 }
1925 }
1926
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001927 // 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 Powell88831a22014-11-20 18:17:00 -08001929 private void addResolveInfo(DisplayResolveInfo dri) {
Kang Li6afa4f22017-06-23 12:54:38 -07001930 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 Powell2ed547e2015-04-29 18:45:04 -07001938 mDisplayList.add(dri);
Adam Powell88831a22014-11-20 18:17:00 -08001939 }
1940 }
1941
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001942 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001943 public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001944 TargetInfo target = targetInfoForPosition(position, filtered);
1945 if (target != null) {
1946 return target.getResolveInfo();
1947 }
1948 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
1950
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001951 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001952 public TargetInfo targetInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001953 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 Project9066cfe2009-03-03 19:31:44 -08001960 }
1961
1962 public int getCount() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001963 int totalSize = mDisplayList == null || mDisplayList.isEmpty() ? mPlaceholderCount :
1964 mDisplayList.size();
Adam Powell278902c2014-07-12 18:33:22 -07001965 if (mFilterLastUsed && mLastChosenPosition >= 0) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001966 totalSize--;
Adam Powell278902c2014-07-12 18:33:22 -07001967 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001968 return totalSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 }
1970
Adam Powell50077352015-05-26 18:01:55 -07001971 public int getUnfilteredCount() {
1972 return mDisplayList.size();
1973 }
1974
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001975 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001976 public TargetInfo getItem(int position) {
Adam Powell278902c2014-07-12 18:33:22 -07001977 if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) {
1978 position++;
1979 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001980 if (mDisplayList.size() > position) {
1981 return mDisplayList.get(position);
1982 } else {
1983 return null;
1984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986
1987 public long getItemId(int position) {
1988 return position;
1989 }
1990
Adam Powell23882512016-01-29 10:21:00 -08001991 public int getDisplayResolveInfoCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001992 return mDisplayList.size();
Adam Powell24428412015-04-01 17:19:56 -07001993 }
1994
Adam Powell23882512016-01-29 10:21:00 -08001995 public DisplayResolveInfo getDisplayResolveInfo(int index) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001996 // Used to query services. We only query services for primary targets, not alternates.
1997 return mDisplayList.get(index);
Adam Powell24428412015-04-01 17:19:56 -07001998 }
1999
2000 public final View getView(int position, View convertView, ViewGroup parent) {
Adam Powellfd1e93d2014-09-07 16:52:22 -07002001 View view = convertView;
2002 if (view == null) {
Adam Powell24428412015-04-01 17:19:56 -07002003 view = createView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
Adam Powell7d758002015-05-06 17:49:36 -07002005 onBindView(view, getItem(position));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 return view;
2007 }
2008
Adam Powell7d758002015-05-06 17:49:36 -07002009 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 Powell24428412015-04-01 17:19:56 -07002017 return mInflater.inflate(
2018 com.android.internal.R.layout.resolve_list_item, parent, false);
2019 }
2020
Adam Powell7d758002015-05-06 17:49:36 -07002021 public final void bindView(int position, View view) {
2022 onBindView(view, getItem(position));
2023 }
2024
Mike Digmanba232682019-03-27 14:55:26 -07002025 protected void onBindView(View view, TargetInfo info) {
Adam Powell0256c6f2013-05-29 16:42:33 -07002026 final ViewHolder holder = (ViewHolder) view.getTag();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002027 if (info == null) {
2028 holder.icon.setImageDrawable(
2029 getDrawable(R.drawable.resolver_icon_placeholder));
2030 return;
2031 }
Mike Digmanba232682019-03-27 14:55:26 -07002032
Adam Powell63b31692015-09-28 10:45:00 -07002033 final CharSequence label = info.getDisplayLabel();
2034 if (!TextUtils.equals(holder.text.getText(), label)) {
2035 holder.text.setText(info.getDisplayLabel());
2036 }
Mike Digmanba232682019-03-27 14:55:26 -07002037
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 Project9066cfe2009-03-03 19:31:44 -08002045 }
Mike Digmanba232682019-03-27 14:55:26 -07002046
Matt Pietala4b30072019-04-04 13:44:36 -04002047 if (info.isSuspended()) {
2048 holder.icon.setColorFilter(mSuspendedMatrixColorFilter);
2049 } else {
2050 holder.icon.setColorFilter(null);
2051 }
2052
Adam Powell24428412015-04-01 17:19:56 -07002053 if (info instanceof DisplayResolveInfo
2054 && !((DisplayResolveInfo) info).hasDisplayIcon()) {
Matt Pietalaf044ae2019-03-29 06:53:53 -04002055 new LoadIconTask((DisplayResolveInfo) info, holder.icon).execute();
2056 } else {
2057 holder.icon.setImageDrawable(info.getDisplayIcon());
Dianne Hackborneb034652009-09-07 00:49:58 -07002058 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002059 }
2060 }
2061
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04002062
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08002063 @VisibleForTesting
2064 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07002065 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 Powell0256c6f2013-05-29 16:42:33 -07002110 static class ViewHolder {
Mike Digman4b83c212019-05-03 10:17:35 -07002111 public View itemView;
2112 public Drawable defaultItemViewBackground;
2113
Adam Powell0256c6f2013-05-29 16:42:33 -07002114 public TextView text;
2115 public TextView text2;
2116 public ImageView icon;
2117
2118 public ViewHolder(View view) {
Mike Digman4b83c212019-05-03 10:17:35 -07002119 itemView = view;
2120 defaultItemViewBackground = view.getBackground();
Adam Powell0256c6f2013-05-29 16:42:33 -07002121 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 Project9066cfe2009-03-03 19:31:44 -08002124 }
2125 }
2126
Adam Powell7d758002015-05-06 17:49:36 -07002127 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 Seyalioglu23f34652017-02-03 09:38:35 -08002139 // If we're still loading, we can't yet enable the buttons.
2140 if (mAdapter.resolveInfoForPosition(position, true) == null) {
2141 return;
2142 }
2143
Adam Powell7d758002015-05-06 17:49:36 -07002144 final int checkedPos = mAdapterView.getCheckedItemPosition();
2145 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08002146 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07002147 && (!hasValidSelection || mLastSelected != checkedPos)
shafik69df96322018-12-18 15:41:19 +00002148 && (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 Powell7d758002015-05-06 17:49:36 -07002156 mOnceButton.setEnabled(hasValidSelection);
2157 if (hasValidSelection) {
2158 mAdapterView.smoothScrollToPosition(checkedPos);
2159 }
2160 mLastSelected = checkedPos;
2161 } else {
2162 startSelected(position, false, true);
2163 }
2164 }
Adam Powell2d809622012-03-22 15:24:43 -07002165
2166 @Override
2167 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07002168 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2169 if (listView != null) {
2170 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07002171 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07002172 if (position < 0) {
2173 // Header views don't count.
2174 return false;
2175 }
Adam Powell278902c2014-07-12 18:33:22 -07002176 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08002177 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07002178 return true;
2179 }
2180
2181 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002182
Matt Pietalaf044ae2019-03-29 06:53:53 -04002183 class LoadIconTask extends AsyncTask<Void, Void, Drawable> {
Adam Powell24428412015-04-01 17:19:56 -07002184 protected final DisplayResolveInfo mDisplayResolveInfo;
2185 private final ResolveInfo mResolveInfo;
Matt Pietalaf044ae2019-03-29 06:53:53 -04002186 private final ImageView mTargetView;
Adam Powell24428412015-04-01 17:19:56 -07002187
Matt Pietalaf044ae2019-03-29 06:53:53 -04002188 LoadIconTask(DisplayResolveInfo dri, ImageView target) {
Adam Powell24428412015-04-01 17:19:56 -07002189 mDisplayResolveInfo = dri;
2190 mResolveInfo = dri.getResolveInfo();
Matt Pietalaf044ae2019-03-29 06:53:53 -04002191 mTargetView = target;
Adam Powell0256c6f2013-05-29 16:42:33 -07002192 }
2193
2194 @Override
Adam Powell24428412015-04-01 17:19:56 -07002195 protected Drawable doInBackground(Void... params) {
2196 return loadIconForResolveInfo(mResolveInfo);
2197 }
2198
2199 @Override
2200 protected void onPostExecute(Drawable d) {
Matt Pietal74c6ed02019-04-18 13:38:46 -04002201 if (mAdapter.getOtherProfile() == mDisplayResolveInfo) {
Adam Powell88831a22014-11-20 18:17:00 -08002202 bindProfileView();
Matt Pietalaf044ae2019-03-29 06:53:53 -04002203 } else {
2204 mDisplayResolveInfo.setDisplayIcon(d);
2205 mTargetView.setImageDrawable(d);
Adam Powell88831a22014-11-20 18:17:00 -08002206 }
Adam Powell278902c2014-07-12 18:33:22 -07002207 }
2208 }
Adam Powell09a65602014-07-20 16:23:14 -07002209
Dianne Hackbornec452d92014-11-11 17:16:56 -08002210 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 Powell4c470d62015-06-19 17:46:17 -07002216 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 Project9066cfe2009-03-03 19:31:44 -08002251}