blob: 9573ac0899f5d7a7d46ec1f3f2bf76d3f3a40198 [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
Matt Pietal78fdbd42019-06-05 07:08:08 -0400401 resetButtonBar();
402
Matt Pietal800136a2019-05-08 07:46:39 -0400403 return insets.consumeSystemWindowInsets();
404 }
405
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900406 @Override
407 public void onConfigurationChanged(Configuration newConfig) {
408 super.onConfigurationChanged(newConfig);
409 mAdapter.handlePackagesChanged();
Matt Pietal800136a2019-05-08 07:46:39 -0400410
411 if (mSystemWindowInsets != null) {
412 mResolverDrawerLayout.setPadding(mSystemWindowInsets.left, mSystemWindowInsets.top,
413 mSystemWindowInsets.right, 0);
414 }
Jiongxuan Gao3365b162016-11-24 20:20:27 +0900415 }
416
Matt Pietala4b30072019-04-04 13:44:36 -0400417 private void initSuspendedColorMatrix() {
418 int grayValue = 127;
419 float scale = 0.5f; // half bright
420
421 ColorMatrix tempBrightnessMatrix = new ColorMatrix();
422 float[] mat = tempBrightnessMatrix.getArray();
423 mat[0] = scale;
424 mat[6] = scale;
425 mat[12] = scale;
426 mat[4] = grayValue;
427 mat[9] = grayValue;
428 mat[14] = grayValue;
429
430 ColorMatrix matrix = new ColorMatrix();
431 matrix.setSaturation(0.0f);
432 matrix.preConcat(tempBrightnessMatrix);
433 mSuspendedMatrixColorFilter = new ColorMatrixColorFilter(matrix);
434 }
435
Adam Powell4c470d62015-06-19 17:46:17 -0700436 /**
437 * Perform any initialization needed for voice interaction.
438 */
Adam Powell23882512016-01-29 10:21:00 -0800439 public void onSetupVoiceInteraction() {
Adam Powell4c470d62015-06-19 17:46:17 -0700440 // Do it right now. Subclasses may delay this and send it later.
441 sendVoiceChoicesIfNeeded();
442 }
443
Adam Powell23882512016-01-29 10:21:00 -0800444 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700445 if (!isVoiceInteraction()) {
446 // Clearly not needed.
447 return;
448 }
449
450
451 final Option[] options = new Option[mAdapter.getCount()];
452 for (int i = 0, N = options.length; i < N; i++) {
453 options[i] = optionForChooserTarget(mAdapter.getItem(i), i);
454 }
455
456 mPickOptionRequest = new PickTargetOptionRequest(
457 new Prompt(getTitle()), options, null);
458 getVoiceInteractor().submitRequest(mPickOptionRequest);
459 }
460
461 Option optionForChooserTarget(TargetInfo target, int index) {
462 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800463 }
464
Adam Powell2ed547e2015-04-29 18:45:04 -0700465 protected final void setAdditionalTargets(Intent[] intents) {
466 if (intents != null) {
467 for (Intent intent : intents) {
468 mIntents.add(intent);
469 }
470 }
471 }
472
Adam Powell0ccc0e92015-04-23 17:19:37 -0700473 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700474 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700475 }
476
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800477 protected String getReferrerPackageName() {
Adam Powell24428412015-04-01 17:19:56 -0700478 final Uri referrer = getReferrer();
479 if (referrer != null && "android-app".equals(referrer.getScheme())) {
480 return referrer.getHost();
481 }
482 return null;
483 }
484
Adam Powell23882512016-01-29 10:21:00 -0800485 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700486 return R.layout.resolver_list;
487 }
488
Matt Pietal74c6ed02019-04-18 13:38:46 -0400489 protected void bindProfileView() {
490 if (mProfileView == null) {
491 return;
492 }
493
Adam Powell88831a22014-11-20 18:17:00 -0800494 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
495 if (dri != null) {
496 mProfileView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800497 View text = mProfileView.findViewById(R.id.profile_button);
498 if (!(text instanceof TextView)) {
499 text = mProfileView.findViewById(R.id.text1);
500 }
501 ((TextView) text).setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800502 } else {
503 mProfileView.setVisibility(View.GONE);
504 }
Adam Powell278902c2014-07-12 18:33:22 -0700505 }
506
Sander Alewijnsef6545332014-10-31 12:39:02 +0000507 private void setProfileSwitchMessageId(int contentUserHint) {
508 if (contentUserHint != UserHandle.USER_CURRENT &&
509 contentUserHint != UserHandle.myUserId()) {
510 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
511 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
512 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
513 : false;
514 boolean targetIsManaged = userManager.isManagedProfile();
515 if (originIsManaged && !targetIsManaged) {
516 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
517 } else if (!originIsManaged && targetIsManaged) {
518 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
519 }
520 }
521 }
522
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700523 /**
524 * Turn on launch mode that is safe to use when forwarding intents received from
525 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
526 * instead of the normal Activity.startActivity for launching the activity selected
527 * by the user.
528 *
529 * <p>This mode is set to true by default if the activity is initialized through
530 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
531 * methods, it is set to false by default. You must set it before calling one of the
532 * more detailed onCreate methods, so that it will be set correctly in the case where
533 * there is only one intent to resolve and it is thus started immediately.</p>
534 */
535 public void setSafeForwardingMode(boolean safeForwarding) {
536 mSafeForwardingMode = safeForwarding;
537 }
538
shafik69df96322018-12-18 15:41:19 +0000539 protected CharSequence getTitleForAction(Intent intent, int defaultTitleRes) {
540 final ActionTitle title = mResolvingHome
541 ? ActionTitle.HOME
542 : ActionTitle.forAction(intent.getAction());
543
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800544 // While there may already be a filtered item, we can only use it in the title if the list
545 // is already sorted and all information relevant to it is already in the list.
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800546 final boolean named = mAdapter.getFilteredPosition() >= 0;
Adam Powell278902c2014-07-12 18:33:22 -0700547 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
548 return getString(defaultTitleRes);
shafik0c7c5d52019-02-27 12:13:25 +0000549 } else if (isHttpSchemeAndViewAction(intent)) {
550 // If the Intent's scheme is http(s) then we need to warn the user that
shafik69df96322018-12-18 15:41:19 +0000551 // they're giving access for the activity to open URLs from this specific host
552 return named
553 ? getString(ActionTitle.BROWSABLE_NAMED_TITLE_RES, intent.getData().getHost(),
554 mAdapter.getFilteredItem().getDisplayLabel())
555 : getString(ActionTitle.BROWSABLE_TITLE_RES, intent.getData().getHost());
Adam Powell278902c2014-07-12 18:33:22 -0700556 } else {
Adam Powell24428412015-04-01 17:19:56 -0700557 return named
558 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
559 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700560 }
Adam Powellc5878612012-05-04 18:42:38 -0700561 }
562
Adam Powelle9414d92014-07-05 17:44:18 -0700563 void dismiss() {
564 if (!isFinishing()) {
565 finish();
566 }
567 }
568
Adam Powellc5878612012-05-04 18:42:38 -0700569
Mike Digman220587e2019-03-13 11:21:42 -0700570 /**
Mike Digmanba232682019-03-27 14:55:26 -0700571 * Loads the icon and label for the provided ApplicationInfo. Defaults to using the application
572 * icon and label over any IntentFilter or Activity icon to increase user understanding, with an
573 * exception for applications that hold the right permission. Always attempts to use available
574 * resources over PackageManager loading mechanisms so badging can be done by iconloader. Uses
575 * Strings to strip creative formatting.
Mike Digman220587e2019-03-13 11:21:42 -0700576 */
Mike Digmanba232682019-03-27 14:55:26 -0700577 private abstract static class TargetPresentationGetter {
578 @Nullable abstract Drawable getIconSubstituteInternal();
579 @Nullable abstract String getAppSubLabelInternal();
Mike Digman220587e2019-03-13 11:21:42 -0700580
Mike Digmanba232682019-03-27 14:55:26 -0700581 private Context mCtx;
Mike Digmanba232682019-03-27 14:55:26 -0700582 private final int mIconDpi;
Mike Digman9c4ae502019-03-19 17:02:25 -0700583 private final boolean mHasSubstitutePermission;
Mike Digmanc0ef5542019-04-01 15:36:25 -0700584 private final ApplicationInfo mAi;
585
586 protected PackageManager mPm;
Mike Digman9c4ae502019-03-19 17:02:25 -0700587
Mike Digmanba232682019-03-27 14:55:26 -0700588 TargetPresentationGetter(Context ctx, int iconDpi, ApplicationInfo ai) {
589 mCtx = ctx;
590 mPm = ctx.getPackageManager();
Mike Digman9c4ae502019-03-19 17:02:25 -0700591 mAi = ai;
Mike Digmanba232682019-03-27 14:55:26 -0700592 mIconDpi = iconDpi;
Mike Digman9c4ae502019-03-19 17:02:25 -0700593 mHasSubstitutePermission = PackageManager.PERMISSION_GRANTED == mPm.checkPermission(
594 android.Manifest.permission.SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON,
595 mAi.packageName);
596 }
597
Mike Digmanb2e5e712019-04-19 15:49:10 -0700598 public Drawable getIcon(UserHandle userHandle) {
599 return new BitmapDrawable(mCtx.getResources(), getIconBitmap(userHandle));
Mike Digman9c4ae502019-03-19 17:02:25 -0700600 }
601
Mike Digmanb2e5e712019-04-19 15:49:10 -0700602 public Bitmap getIconBitmap(UserHandle userHandle) {
Mike Digman9c4ae502019-03-19 17:02:25 -0700603 Drawable dr = null;
604 if (mHasSubstitutePermission) {
Mike Digmanba232682019-03-27 14:55:26 -0700605 dr = getIconSubstituteInternal();
Mike Digman9c4ae502019-03-19 17:02:25 -0700606 }
607
608 if (dr == null) {
609 try {
610 if (mAi.icon != 0) {
611 dr = loadIconFromResource(mPm.getResourcesForApplication(mAi), mAi.icon);
Mike Digman220587e2019-03-13 11:21:42 -0700612 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700613 } catch (NameNotFoundException ignore) {
614 }
615 }
616
617 // Fall back to ApplicationInfo#loadIcon if nothing has been loaded
618 if (dr == null) {
619 dr = mAi.loadIcon(mPm);
620 }
621
Mike Digmanba232682019-03-27 14:55:26 -0700622 SimpleIconFactory sif = SimpleIconFactory.obtain(mCtx);
Mike Digmanb2e5e712019-04-19 15:49:10 -0700623 Bitmap icon = sif.createUserBadgedIconBitmap(dr, userHandle);
Mike Digman9c4ae502019-03-19 17:02:25 -0700624 sif.recycle();
625
626 return icon;
627 }
628
Mike Digmanba232682019-03-27 14:55:26 -0700629 public String getLabel() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700630 String label = null;
631 // Apps with the substitute permission will always show the sublabel as their label
632 if (mHasSubstitutePermission) {
Mike Digmanba232682019-03-27 14:55:26 -0700633 label = getAppSubLabelInternal();
Mike Digman9c4ae502019-03-19 17:02:25 -0700634 }
635
636 if (label == null) {
637 label = (String) mAi.loadLabel(mPm);
638 }
639
640 return label;
641 }
642
Mike Digmanba232682019-03-27 14:55:26 -0700643 public String getSubLabel() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700644 // Apps with the substitute permission will never have a sublabel
645 if (mHasSubstitutePermission) return null;
Mike Digmanba232682019-03-27 14:55:26 -0700646 return getAppSubLabelInternal();
647 }
648
649 protected String loadLabelFromResource(Resources res, int resId) {
650 return res.getString(resId);
Mike Digman9c4ae502019-03-19 17:02:25 -0700651 }
652
653 @Nullable
654 protected Drawable loadIconFromResource(Resources res, int resId) {
655 return res.getDrawableForDensity(resId, mIconDpi);
656 }
657
658 }
659
Mike Digmanba232682019-03-27 14:55:26 -0700660 /**
661 * Loads the icon and label for the provided ResolveInfo.
662 */
663 @VisibleForTesting
Mike Digmanc0ef5542019-04-01 15:36:25 -0700664 public static class ResolveInfoPresentationGetter extends ActivityInfoPresentationGetter {
Mike Digman9c4ae502019-03-19 17:02:25 -0700665 private final ResolveInfo mRi;
Mike Digmanba232682019-03-27 14:55:26 -0700666 public ResolveInfoPresentationGetter(Context ctx, int iconDpi, ResolveInfo ri) {
Mike Digmanc0ef5542019-04-01 15:36:25 -0700667 super(ctx, iconDpi, ri.activityInfo);
Mike Digman9c4ae502019-03-19 17:02:25 -0700668 mRi = ri;
669 }
670
671 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700672 Drawable getIconSubstituteInternal() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700673 Drawable dr = null;
674 try {
675 // Do not use ResolveInfo#getIconResource() as it defaults to the app
676 if (mRi.resolvePackageName != null && mRi.icon != 0) {
677 dr = loadIconFromResource(
678 mPm.getResourcesForApplication(mRi.resolvePackageName), mRi.icon);
Adam Powellc5878612012-05-04 18:42:38 -0700679 }
Mike Digman220587e2019-03-13 11:21:42 -0700680 } catch (NameNotFoundException e) {
681 Log.e(TAG, "SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON permission granted but "
682 + "couldn't find resources for package", e);
Adam Powellc5878612012-05-04 18:42:38 -0700683 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700684
Mike Digmanc0ef5542019-04-01 15:36:25 -0700685 // Fall back to ActivityInfo if no icon is found via ResolveInfo
686 if (dr == null) dr = super.getIconSubstituteInternal();
687
Mike Digman9c4ae502019-03-19 17:02:25 -0700688 return dr;
Adam Powellc5878612012-05-04 18:42:38 -0700689 }
Mike Digman220587e2019-03-13 11:21:42 -0700690
Mike Digman9c4ae502019-03-19 17:02:25 -0700691 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700692 String getAppSubLabelInternal() {
693 // Will default to app name if no intent filter or activity label set, make sure to
694 // check if subLabel matches label before final display
Mike Digman9c4ae502019-03-19 17:02:25 -0700695 return (String) mRi.loadLabel(mPm);
696 }
697 }
698
Mike Digmanba232682019-03-27 14:55:26 -0700699 ResolveInfoPresentationGetter makePresentationGetter(ResolveInfo ri) {
700 return new ResolveInfoPresentationGetter(this, mIconDpi, ri);
701 }
702
703 /**
704 * Loads the icon and label for the provided ActivityInfo.
705 */
706 @VisibleForTesting
707 public static class ActivityInfoPresentationGetter extends TargetPresentationGetter {
Mike Digman9c4ae502019-03-19 17:02:25 -0700708 private final ActivityInfo mActivityInfo;
Mike Digmanba232682019-03-27 14:55:26 -0700709 public ActivityInfoPresentationGetter(Context ctx, int iconDpi,
710 ActivityInfo activityInfo) {
711 super(ctx, iconDpi, activityInfo.applicationInfo);
Mike Digman9c4ae502019-03-19 17:02:25 -0700712 mActivityInfo = activityInfo;
713 }
714
715 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700716 Drawable getIconSubstituteInternal() {
Mike Digman9c4ae502019-03-19 17:02:25 -0700717 Drawable dr = null;
Mike Digman220587e2019-03-13 11:21:42 -0700718 try {
Mike Digman9c4ae502019-03-19 17:02:25 -0700719 // Do not use ActivityInfo#getIconResource() as it defaults to the app
720 if (mActivityInfo.icon != 0) {
721 dr = loadIconFromResource(
722 mPm.getResourcesForApplication(mActivityInfo.applicationInfo),
723 mActivityInfo.icon);
724 }
725 } catch (NameNotFoundException e) {
726 Log.e(TAG, "SUBSTITUTE_SHARE_TARGET_APP_NAME_AND_ICON permission granted but "
727 + "couldn't find resources for package", e);
Mike Digman220587e2019-03-13 11:21:42 -0700728 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700729
730 return dr;
Mike Digman220587e2019-03-13 11:21:42 -0700731 }
732
Mike Digman9c4ae502019-03-19 17:02:25 -0700733 @Override
Mike Digmanba232682019-03-27 14:55:26 -0700734 String getAppSubLabelInternal() {
735 // Will default to app name if no activity label set, make sure to check if subLabel
736 // matches label before final display
Mike Digman9c4ae502019-03-19 17:02:25 -0700737 return (String) mActivityInfo.loadLabel(mPm);
Mike Digman220587e2019-03-13 11:21:42 -0700738 }
Mike Digman9c4ae502019-03-19 17:02:25 -0700739 }
Mike Digman220587e2019-03-13 11:21:42 -0700740
Mike Digmanba232682019-03-27 14:55:26 -0700741 protected ActivityInfoPresentationGetter makePresentationGetter(ActivityInfo ai) {
742 return new ActivityInfoPresentationGetter(this, mIconDpi, ai);
743 }
744
Mike Digman9c4ae502019-03-19 17:02:25 -0700745 Drawable loadIconForResolveInfo(ResolveInfo ri) {
Mike Digmanb2e5e712019-04-19 15:49:10 -0700746 // Load icons based on the current process. If in work profile icons should be badged.
747 return makePresentationGetter(ri).getIcon(Process.myUserHandle());
Adam Powellc5878612012-05-04 18:42:38 -0700748 }
749
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800750 @Override
751 protected void onRestart() {
752 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700753 if (!mRegistered) {
754 mPackageMonitor.register(this, getMainLooper(), false);
755 mRegistered = true;
756 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800757 mAdapter.handlePackagesChanged();
Matt Pietal74c6ed02019-04-18 13:38:46 -0400758 bindProfileView();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800759 }
760
761 @Override
762 protected void onStop() {
763 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700764 if (mRegistered) {
765 mPackageMonitor.unregister();
766 mRegistered = false;
767 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700768 final Intent intent = getIntent();
769 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
Jorim Jaggif631ef72017-02-24 13:49:47 +0100770 && !mResolvingHome && !mRetainInOnStop) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700771 // This resolver is in the unusual situation where it has been
772 // launched at the top of a new task. We don't let it be added
773 // to the recent tasks shown to the user, and we need to make sure
774 // that each time we are launched we get the correct launching
775 // uid (not re-using the same resolver from an old launching uid),
776 // so we will now finish ourself since being no longer visible,
777 // the user probably can't get back to us.
778 if (!isChangingConfigurations()) {
779 finish();
780 }
781 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800782 }
783
Adam Powellc5878612012-05-04 18:42:38 -0700784 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700785 protected void onDestroy() {
786 super.onDestroy();
787 if (!isChangingConfigurations() && mPickOptionRequest != null) {
788 mPickOptionRequest.cancel();
789 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800790 if (mPostListReadyRunnable != null) {
791 getMainThreadHandler().removeCallbacks(mPostListReadyRunnable);
792 mPostListReadyRunnable = null;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800793 }
Kang Li38a6da642017-04-05 12:30:55 -0700794 if (mAdapter != null && mAdapter.mResolverListController != null) {
795 mAdapter.mResolverListController.destroy();
796 }
Adam Powell4c470d62015-06-19 17:46:17 -0700797 }
798
799 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700800 protected void onRestoreInstanceState(Bundle savedInstanceState) {
801 super.onRestoreInstanceState(savedInstanceState);
shafik69df96322018-12-18 15:41:19 +0000802 resetButtonBar();
Adam Powell9bee4662012-05-08 11:07:23 -0700803 }
804
shafik0c7c5d52019-02-27 12:13:25 +0000805 private boolean isHttpSchemeAndViewAction(Intent intent) {
806 return (IntentFilter.SCHEME_HTTP.equals(intent.getScheme())
807 || IntentFilter.SCHEME_HTTPS.equals(intent.getScheme()))
808 && Intent.ACTION_VIEW.equals(intent.getAction());
809 }
810
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100811 private boolean hasManagedProfile() {
812 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
813 if (userManager == null) {
814 return false;
815 }
816
817 try {
818 List<UserInfo> profiles = userManager.getProfiles(getUserId());
819 for (UserInfo userInfo : profiles) {
820 if (userInfo != null && userInfo.isManagedProfile()) {
821 return true;
822 }
823 }
824 } catch (SecurityException e) {
825 return false;
826 }
827 return false;
828 }
829
830 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
831 try {
832 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
833 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700834 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100835 } catch (NameNotFoundException e) {
836 return false;
837 }
838 }
839
Adam Powell278902c2014-07-12 18:33:22 -0700840 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
841 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100842 boolean enabled = false;
843 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700844 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Hakan Seyalioglu5dbc8192017-02-24 16:16:37 -0800845 if (ri == null) {
846 Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled");
847 return;
848 } else if (ri.targetUserId != UserHandle.USER_CURRENT) {
849 Log.e(TAG, "Attempted to set selection to resolve info for another user");
850 return;
851 } else {
Nicolas Prevot50449882014-06-23 12:42:37 +0100852 enabled = true;
853 }
854 }
855 mAlwaysButton.setEnabled(enabled);
856 }
857
Adam Powellc5878612012-05-04 18:42:38 -0700858 public void onButtonClick(View v) {
859 final int id = v.getId();
shafik69df96322018-12-18 15:41:19 +0000860 int which = mAdapter.hasFilteredItem()
861 ? mAdapter.getFilteredPosition()
862 : mAdapterView.getCheckedItemPosition();
863 boolean hasIndexBeenFiltered = !mAdapter.hasFilteredItem();
864 if (id == R.id.button_app_settings) {
865 showSettingsForSelected(which, hasIndexBeenFiltered);
866 } else {
867 startSelected(which, id == R.id.button_always, hasIndexBeenFiltered);
868 }
869 }
870
871 private void showSettingsForSelected(int which, boolean hasIndexBeenFiltered) {
872 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
shafik34064c32019-03-06 15:42:52 +0000873 Intent intent = new Intent();
874 // For browsers, we open the Default Browser page
875 // For regular apps, we open the Open by Default page
876 if (ri.handleAllWebDataURI) {
877 intent.setAction(Intent.ACTION_MANAGE_DEFAULT_APP)
878 .putExtra(Intent.EXTRA_ROLE_NAME, RoleManager.ROLE_BROWSER);
879 } else {
880 intent.setAction(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS)
881 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
882 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
883 }
884 startActivity(intent);
Adam Powellc5878612012-05-04 18:42:38 -0700885 }
886
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800887 public void startSelected(int which, boolean always, boolean hasIndexBeenFiltered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700888 if (isFinishing()) {
889 return;
890 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800891 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, hasIndexBeenFiltered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000892 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
893 Toast.makeText(this, String.format(getResources().getString(
894 com.android.internal.R.string.activity_resolver_work_profiles_support),
895 ri.activityInfo.loadLabel(getPackageManager()).toString()),
896 Toast.LENGTH_LONG).show();
897 return;
898 }
899
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800900 TargetInfo target = mAdapter.targetInfoForPosition(which, hasIndexBeenFiltered);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -0800901 if (target == null) {
902 return;
903 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700904 if (onTargetSelected(target, always)) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800905 if (always && mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100906 MetricsLogger.action(
907 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800908 } else if (mSupportsAlwaysUseOption) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100909 MetricsLogger.action(
910 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
911 } else {
912 MetricsLogger.action(
913 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
914 }
915 MetricsLogger.action(this, mAdapter.hasFilteredItem()
916 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
917 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700918 finish();
919 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921
Adam Powelle49d9392014-07-17 18:45:19 -0700922 /**
923 * Replace me in subclasses!
924 */
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000925 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700926 return defIntent;
927 }
928
Adam Powell2ed547e2015-04-29 18:45:04 -0700929 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700930 final ResolveInfo ri = target.getResolveInfo();
931 final Intent intent = target != null ? target.getResolvedIntent() : null;
932
Hakan Seyalioglu13405c52017-01-31 19:01:31 -0800933 if (intent != null && (mSupportsAlwaysUseOption || mAdapter.hasFilteredItem())
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -0800934 && mAdapter.mUnfilteredResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700935 // Build a reasonable intent filter, based on what matched.
936 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200937 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938
Henrik Engström3277cf12014-07-17 12:18:29 +0200939 if (intent.getSelector() != null) {
940 filterIntent = intent.getSelector();
941 } else {
942 filterIntent = intent;
943 }
944
945 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800946 if (action != null) {
947 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200949 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700950 if (categories != null) {
951 for (String cat : categories) {
952 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700955 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956
Adam Powell24428412015-04-01 17:19:56 -0700957 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200958 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700959 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200960 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700961 if (mimeType != null) {
962 try {
963 filter.addDataType(mimeType);
964 } catch (IntentFilter.MalformedMimeTypeException e) {
965 Log.w("ResolverActivity", e);
966 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700969 }
970 if (data != null && data.getScheme() != null) {
971 // We need the data specification if there was no type,
972 // OR if the scheme is not one of our magical "file:"
973 // or "content:" schemes (see IntentFilter for the reason).
974 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
975 || (!"file".equals(data.getScheme())
976 && !"content".equals(data.getScheme()))) {
977 filter.addDataScheme(data.getScheme());
978
979 // Look through the resolved filter to determine which part
980 // of it matched the original Intent.
981 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
982 if (pIt != null) {
983 String ssp = data.getSchemeSpecificPart();
984 while (ssp != null && pIt.hasNext()) {
985 PatternMatcher p = pIt.next();
986 if (p.match(ssp)) {
987 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
988 break;
989 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700990 }
991 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700992 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
993 if (aIt != null) {
994 while (aIt.hasNext()) {
995 IntentFilter.AuthorityEntry a = aIt.next();
996 if (a.match(data) >= 0) {
997 int port = a.getPort();
998 filter.addDataAuthority(a.getHost(),
999 port >= 0 ? Integer.toString(port) : null);
1000 break;
1001 }
1002 }
1003 }
1004 pIt = ri.filter.pathsIterator();
1005 if (pIt != null) {
1006 String path = data.getPath();
1007 while (path != null && pIt.hasNext()) {
1008 PatternMatcher p = pIt.next();
1009 if (p.match(path)) {
1010 filter.addDataPath(p.getPath(), p.getType());
1011 break;
1012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
1014 }
1015 }
1016 }
1017
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001018 if (filter != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001019 final int N = mAdapter.mUnfilteredResolveList.size();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001020 ComponentName[] set;
1021 // If we don't add back in the component for forwarding the intent to a managed
1022 // profile, the preferred activity may not be updated correctly (as the set of
1023 // components we tell it we knew about will have changed).
1024 final boolean needToAddBackProfileForwardingComponent
1025 = mAdapter.mOtherProfile != null;
1026 if (!needToAddBackProfileForwardingComponent) {
1027 set = new ComponentName[N];
1028 } else {
1029 set = new ComponentName[N + 1];
1030 }
1031
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001032 int bestMatch = 0;
1033 for (int i=0; i<N; i++) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001034 ResolveInfo r = mAdapter.mUnfilteredResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001035 set[i] = new ComponentName(r.activityInfo.packageName,
1036 r.activityInfo.name);
1037 if (r.match > bestMatch) bestMatch = r.match;
1038 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001039
1040 if (needToAddBackProfileForwardingComponent) {
1041 set[N] = mAdapter.mOtherProfile.getResolvedComponentName();
1042 final int otherProfileMatch = mAdapter.mOtherProfile.getResolveInfo().match;
1043 if (otherProfileMatch > bestMatch) bestMatch = otherProfileMatch;
1044 }
1045
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001046 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001047 final int userId = getUserId();
1048 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001049
1050 // Set the preferred Activity
1051 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
1052
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001053 if (ri.handleAllWebDataURI) {
1054 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001055 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001056 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001057 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001058 }
1059 } else {
1060 // Update Domain Verification status
1061 ComponentName cn = intent.getComponent();
1062 String packageName = cn.getPackageName();
1063 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001064
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001065 boolean isHttpOrHttps = (dataScheme != null) &&
1066 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
1067 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001068
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001069 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
1070 boolean hasCategoryBrowsable = (categories != null) &&
1071 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001072
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001073 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001074 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -07001075 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
1076 userId);
1077 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001078 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001079 } else {
1080 try {
Hakan Seyalioglu9149dca2017-01-17 12:20:01 -08001081 mAdapter.mResolverListController.setLastChosen(intent, filter, bestMatch);
Amith Yamasani588dd2a2013-09-03 12:30:44 -07001082 } catch (RemoteException re) {
1083 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1084 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087 }
1088
Adam Powell24428412015-04-01 17:19:56 -07001089 if (target != null) {
1090 safelyStartActivity(target);
Matt Pietala4b30072019-04-04 13:44:36 -04001091
1092 // Rely on the ActivityManager to pop up a dialog regarding app suspension
1093 // and return false
1094 if (target.isSuspended()) {
1095 return false;
1096 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001097 }
Matt Pietala4b30072019-04-04 13:44:36 -04001098
Adam Powell2ed547e2015-04-29 18:45:04 -07001099 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001100 }
1101
Adam Powell23882512016-01-29 10:21:00 -08001102 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -07001103 // We're dispatching intents that might be coming from legacy apps, so
1104 // don't kill ourselves.
1105 StrictMode.disableDeathOnFileUriExposure();
1106 try {
1107 safelyStartActivityInternal(cti);
1108 } finally {
1109 StrictMode.enableDeathOnFileUriExposure();
1110 }
1111 }
1112
1113 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +00001114 // If needed, show that intent is forwarded
1115 // from managed profile to owner or other way around.
1116 if (mProfileSwitchMessageId != -1) {
1117 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
1118 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001119 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -07001120 if (cti.start(this, null)) {
1121 onActivityStarted(cti);
1122 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001123 return;
1124 }
1125 try {
Adam Powell24428412015-04-01 17:19:56 -07001126 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
1127 onActivityStarted(cti);
1128 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001129 } catch (RuntimeException e) {
1130 String launchedFromPackage;
1131 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -07001132 launchedFromPackage = ActivityTaskManager.getService().getLaunchedFromPackage(
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001133 getActivityToken());
1134 } catch (RemoteException e2) {
1135 launchedFromPackage = "??";
1136 }
1137 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
1138 + " package " + launchedFromPackage + ", while running in "
1139 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
1142
Alison Cichowlas3e340502018-08-07 17:15:01 -04001143
1144 boolean startAsCallerImpl(Intent intent, Bundle options, boolean ignoreTargetSecurity,
1145 int userId) {
1146 // Pass intent to delegate chooser activity with permission token.
1147 // TODO: This should move to a trampoline Activity in the system when the ChooserActivity
1148 // moves into systemui
1149 try {
1150 // TODO: Once this is a small springboard activity, it can move off the UI process
1151 // and we can move the request method to ActivityManagerInternal.
1152 IBinder permissionToken = ActivityTaskManager.getService()
1153 .requestStartActivityPermissionToken(getActivityToken());
1154 final Intent chooserIntent = new Intent();
1155 final ComponentName delegateActivity = ComponentName.unflattenFromString(
1156 Resources.getSystem().getString(R.string.config_chooserActivity));
1157 chooserIntent.setClassName(delegateActivity.getPackageName(),
1158 delegateActivity.getClassName());
1159 chooserIntent.putExtra(ActivityTaskManager.EXTRA_PERMISSION_TOKEN, permissionToken);
1160
1161 // TODO: These extras will change as chooser activity moves into systemui
1162 chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
1163 chooserIntent.putExtra(ActivityTaskManager.EXTRA_OPTIONS, options);
1164 chooserIntent.putExtra(ActivityTaskManager.EXTRA_IGNORE_TARGET_SECURITY,
1165 ignoreTargetSecurity);
1166 chooserIntent.putExtra(Intent.EXTRA_USER_ID, userId);
Alison Cichowlas7b6f3b62018-12-07 09:06:57 -05001167 chooserIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1168 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
Alison Cichowlas3e340502018-08-07 17:15:01 -04001169 startActivity(chooserIntent);
1170 } catch (RemoteException e) {
1171 Log.e(TAG, e.toString());
1172 }
1173 return true;
1174 }
1175
Adam Powell23882512016-01-29 10:21:00 -08001176 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -07001177 // Do nothing
1178 }
1179
Adam Powell23882512016-01-29 10:21:00 -08001180 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -07001181 return false;
1182 }
1183
Adam Powell23882512016-01-29 10:21:00 -08001184 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Matt Pietala4b30072019-04-04 13:44:36 -04001185 return !target.isSuspended();
Adam Powell39e94eb2015-09-08 17:01:49 -07001186 }
1187
Adam Powell23882512016-01-29 10:21:00 -08001188 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -07001189 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -07001190 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -07001191 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -07001192 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -07001193 }
1194
Adam Powell23882512016-01-29 10:21:00 -08001195 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents,
Adam Powell7d758002015-05-06 17:49:36 -07001196 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
1197 boolean filterLastUsed) {
1198 return new ResolveListAdapter(context, payloadIntents, initialIntents, rList,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001199 launchedFromUid, filterLastUsed, createListController());
1200 }
1201
1202 @VisibleForTesting
1203 protected ResolverListController createListController() {
1204 return new ResolverListController(
1205 this,
1206 mPm,
1207 getTargetIntent(),
1208 getReferrerPackageName(),
1209 mLaunchedFromUid);
Adam Powell88831a22014-11-20 18:17:00 -08001210 }
1211
Adam Powell39e94eb2015-09-08 17:01:49 -07001212 /**
1213 * Returns true if the activity is finishing and creation should halt
1214 */
Adam Powell23882512016-01-29 10:21:00 -08001215 public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents,
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001216 List<ResolveInfo> rList) {
Dianne Hackborn57dd7372015-07-27 18:11:14 -07001217 // The last argument of createAdapter is whether to do special handling
1218 // of the last used choice to highlight it in the list. We need to always
1219 // turn this off when running under voice interaction, since it results in
1220 // a more complicated UI that the current voice interaction flow is not able
1221 // to handle.
Adam Powell7d758002015-05-06 17:49:36 -07001222 mAdapter = createAdapter(this, payloadIntents, initialIntents, rList,
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001223 mLaunchedFromUid, mSupportsAlwaysUseOption && !isVoiceInteraction());
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001224 boolean rebuildCompleted = mAdapter.rebuildList();
Adam Powell7d758002015-05-06 17:49:36 -07001225
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001226 if (useLayoutWithDefault()) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001227 mLayoutId = R.layout.resolver_list_with_default;
Adam Powell7d758002015-05-06 17:49:36 -07001228 } else {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001229 mLayoutId = getLayoutResource();
Adam Powell7d758002015-05-06 17:49:36 -07001230 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001231 setContentView(mLayoutId);
Adam Powell7d758002015-05-06 17:49:36 -07001232
Adam Powell50077352015-05-26 18:01:55 -07001233 int count = mAdapter.getUnfilteredCount();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001234
1235 // We only rebuild asynchronously when we have multiple elements to sort. In the case where
1236 // we're already done, we can check if we should auto-launch immediately.
1237 if (rebuildCompleted) {
1238 if (count == 1 && mAdapter.getOtherProfile() == null) {
1239 // Only one target, so we're a candidate to auto-launch!
1240 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
1241 if (shouldAutoLaunchSingleChoice(target)) {
1242 safelyStartActivity(target);
1243 mPackageMonitor.unregister();
1244 mRegistered = false;
1245 finish();
1246 return true;
1247 }
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -07001248 }
1249 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001250
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001251
Alan Viverette51efddb2017-04-05 10:00:01 -04001252 mAdapterView = findViewById(R.id.resolver_list);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001253
1254 if (count == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001255 final TextView emptyView = findViewById(R.id.empty);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001256 emptyView.setVisibility(View.VISIBLE);
Adam Powell7d758002015-05-06 17:49:36 -07001257 mAdapterView.setVisibility(View.GONE);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001258 } else {
1259 mAdapterView.setVisibility(View.VISIBLE);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001260 onPrepareAdapterView(mAdapterView, mAdapter);
Adam Powell7d758002015-05-06 17:49:36 -07001261 }
Adam Powell39e94eb2015-09-08 17:01:49 -07001262 return false;
Adam Powell7d758002015-05-06 17:49:36 -07001263 }
1264
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001265 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter) {
Adam Powell7d758002015-05-06 17:49:36 -07001266 final boolean useHeader = adapter.hasFilteredItem();
1267 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
1268
1269 adapterView.setAdapter(mAdapter);
1270
1271 final ItemClickListener listener = new ItemClickListener();
1272 adapterView.setOnItemClickListener(listener);
1273 adapterView.setOnItemLongClickListener(listener);
1274
shafik69df96322018-12-18 15:41:19 +00001275 if (mSupportsAlwaysUseOption || mUseLayoutForBrowsables) {
Adam Powell7d758002015-05-06 17:49:36 -07001276 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
1277 }
1278
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001279 // In case this method is called again (due to activity recreation), avoid adding a new
1280 // header if one is already present.
1281 if (useHeader && listView != null && listView.getHeaderViewsCount() == 0) {
Adam Powell7d758002015-05-06 17:49:36 -07001282 listView.addHeaderView(LayoutInflater.from(this).inflate(
1283 R.layout.resolver_different_item_header, listView, false));
1284 }
Adam Powell24428412015-04-01 17:19:56 -07001285 }
1286
Matt Pietal26038402019-01-08 07:29:34 -05001287 /**
1288 * Configure the area above the app selection list (title, content preview, etc).
1289 */
1290 public void setHeader() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001291 if (mAdapter.getCount() == 0 && mAdapter.mPlaceholderCount == 0) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001292 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001293 if (titleView != null) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001294 titleView.setVisibility(View.GONE);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001295 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001296 }
1297
1298 CharSequence title = mTitle != null
1299 ? mTitle
shafik69df96322018-12-18 15:41:19 +00001300 : getTitleForAction(getTargetIntent(), mDefaultTitleResId);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001301
1302 if (!TextUtils.isEmpty(title)) {
Alan Viverette51efddb2017-04-05 10:00:01 -04001303 final TextView titleView = findViewById(R.id.title);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001304 if (titleView != null) {
1305 titleView.setText(title);
1306 }
1307 setTitle(title);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001308 }
1309
Alan Viverette51efddb2017-04-05 10:00:01 -04001310 final ImageView iconView = findViewById(R.id.icon);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001311 final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem();
1312 if (iconView != null && iconInfo != null) {
Matt Pietalaf044ae2019-03-29 06:53:53 -04001313 new LoadIconTask(iconInfo, iconView).execute();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001314 }
1315 }
1316
shafik69df96322018-12-18 15:41:19 +00001317 private void resetButtonBar() {
1318 if (!mSupportsAlwaysUseOption && !mUseLayoutForBrowsables) {
1319 return;
1320 }
1321 final ViewGroup buttonLayout = findViewById(R.id.button_bar);
1322 if (buttonLayout != null) {
1323 buttonLayout.setVisibility(View.VISIBLE);
Matt Pietal800136a2019-05-08 07:46:39 -04001324 int inset = mSystemWindowInsets != null ? mSystemWindowInsets.bottom : 0;
1325 buttonLayout.setPadding(buttonLayout.getPaddingLeft(), buttonLayout.getPaddingTop(),
Matt Pietal33b0ac12019-05-24 13:47:18 -04001326 buttonLayout.getPaddingRight(), getResources().getDimensionPixelSize(
1327 R.dimen.resolver_button_bar_spacing) + inset);
Matt Pietal800136a2019-05-08 07:46:39 -04001328
shafik69df96322018-12-18 15:41:19 +00001329 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
1330 mSettingsButton = (Button) buttonLayout.findViewById(R.id.button_app_settings);
1331 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
1332
1333 if (mUseLayoutForBrowsables) {
1334 resetSettingsOrOnceButtonBar();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001335 } else {
shafik69df96322018-12-18 15:41:19 +00001336 resetAlwaysOrOnceButtonBar();
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001337 }
shafik69df96322018-12-18 15:41:19 +00001338 } else {
1339 Log.e(TAG, "Layout unexpectedly does not have a button bar");
1340 }
1341 }
1342
1343 private void resetSettingsOrOnceButtonBar() {
1344 //unsetting always button
1345 mAlwaysButton.setVisibility(View.GONE);
1346
1347 // When the items load in, if an item was already selected,
1348 // enable the buttons
1349 if (mAdapterView != null
1350 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1351 mSettingsButton.setEnabled(true);
1352 mOnceButton.setEnabled(true);
1353 }
1354 }
1355
1356 private void resetAlwaysOrOnceButtonBar() {
1357 // This check needs to be made because layout with default
1358 // doesn't have a settings button
1359 if (mSettingsButton != null) {
1360 //unsetting always button
1361 mSettingsButton.setVisibility(View.GONE);
1362 mSettingsButton = null;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001363 }
1364
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001365 if (useLayoutWithDefault()
1366 && mAdapter.getFilteredPosition() != ListView.INVALID_POSITION) {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001367 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
1368 mOnceButton.setEnabled(true);
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001369 return;
1370 }
1371
1372 // When the items load in, if an item was already selected, enable the buttons
1373 if (mAdapterView != null
1374 && mAdapterView.getCheckedItemPosition() != ListView.INVALID_POSITION) {
1375 setAlwaysButtonEnabled(true, mAdapterView.getCheckedItemPosition(), true);
1376 mOnceButton.setEnabled(true);
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001377 }
1378 }
1379
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001380 private boolean useLayoutWithDefault() {
1381 return mSupportsAlwaysUseOption && mAdapter.hasFilteredItem();
1382 }
1383
Adam Powellc412be62015-06-24 13:54:10 -07001384 /**
Jorim Jaggif631ef72017-02-24 13:49:47 +01001385 * If {@code retainInOnStop} is set to true, we will not finish ourselves when onStop gets
1386 * called and we are launched in a new task.
1387 */
1388 protected void setRetainInOnStop(boolean retainInOnStop) {
1389 mRetainInOnStop = retainInOnStop;
1390 }
1391
1392 /**
Adam Powellc412be62015-06-24 13:54:10 -07001393 * Check a simple match for the component of two ResolveInfos.
1394 */
1395 static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
1396 return lhs == null ? rhs == null
1397 : lhs.activityInfo == null ? rhs.activityInfo == null
1398 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
1399 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
1400 }
1401
Adam Powell23882512016-01-29 10:21:00 -08001402 public final class DisplayResolveInfo implements TargetInfo {
Adam Powell24428412015-04-01 17:19:56 -07001403 private final ResolveInfo mResolveInfo;
1404 private final CharSequence mDisplayLabel;
1405 private Drawable mDisplayIcon;
Adam Powell00f4aad2015-09-17 13:38:16 -07001406 private Drawable mBadge;
Adam Powell24428412015-04-01 17:19:56 -07001407 private final CharSequence mExtendedInfo;
1408 private final Intent mResolvedIntent;
Adam Powell2ed547e2015-04-29 18:45:04 -07001409 private final List<Intent> mSourceIntents = new ArrayList<>();
Matt Pietala4b30072019-04-04 13:44:36 -04001410 private boolean mIsSuspended;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411
Adam Powell23882512016-01-29 10:21:00 -08001412 public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel,
Dianne Hackborneb034652009-09-07 00:49:58 -07001413 CharSequence pInfo, Intent pOrigIntent) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001414 mSourceIntents.add(originalIntent);
Adam Powell24428412015-04-01 17:19:56 -07001415 mResolveInfo = pri;
1416 mDisplayLabel = pLabel;
1417 mExtendedInfo = pInfo;
1418
1419 final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent :
Adam Powell2ed547e2015-04-29 18:45:04 -07001420 getReplacementIntent(pri.activityInfo, getTargetIntent()));
Adam Powell24428412015-04-01 17:19:56 -07001421 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1422 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
1423 final ActivityInfo ai = mResolveInfo.activityInfo;
1424 intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));
1425
Matt Pietala4b30072019-04-04 13:44:36 -04001426 mIsSuspended = (ai.applicationInfo.flags & ApplicationInfo.FLAG_SUSPENDED) != 0;
1427
Adam Powell24428412015-04-01 17:19:56 -07001428 mResolvedIntent = intent;
1429 }
1430
Adam Powell2ed547e2015-04-29 18:45:04 -07001431 private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) {
1432 mSourceIntents.addAll(other.getAllSourceIntents());
1433 mResolveInfo = other.mResolveInfo;
1434 mDisplayLabel = other.mDisplayLabel;
1435 mDisplayIcon = other.mDisplayIcon;
1436 mExtendedInfo = other.mExtendedInfo;
1437 mResolvedIntent = new Intent(other.mResolvedIntent);
1438 mResolvedIntent.fillIn(fillInIntent, flags);
1439 }
1440
Adam Powell24428412015-04-01 17:19:56 -07001441 public ResolveInfo getResolveInfo() {
1442 return mResolveInfo;
1443 }
1444
1445 public CharSequence getDisplayLabel() {
1446 return mDisplayLabel;
1447 }
1448
1449 public Drawable getDisplayIcon() {
1450 return mDisplayIcon;
1451 }
1452
Alan Viverettece5d92c2015-07-31 16:46:56 -04001453 @Override
Adam Powell2ed547e2015-04-29 18:45:04 -07001454 public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) {
1455 return new DisplayResolveInfo(this, fillInIntent, flags);
1456 }
1457
1458 @Override
1459 public List<Intent> getAllSourceIntents() {
1460 return mSourceIntents;
1461 }
1462
1463 public void addAlternateSourceIntent(Intent alt) {
1464 mSourceIntents.add(alt);
1465 }
1466
Adam Powell24428412015-04-01 17:19:56 -07001467 public void setDisplayIcon(Drawable icon) {
1468 mDisplayIcon = icon;
1469 }
1470
1471 public boolean hasDisplayIcon() {
1472 return mDisplayIcon != null;
1473 }
1474
1475 public CharSequence getExtendedInfo() {
1476 return mExtendedInfo;
1477 }
1478
1479 public Intent getResolvedIntent() {
1480 return mResolvedIntent;
1481 }
1482
1483 @Override
1484 public ComponentName getResolvedComponentName() {
1485 return new ComponentName(mResolveInfo.activityInfo.packageName,
1486 mResolveInfo.activityInfo.name);
1487 }
1488
1489 @Override
1490 public boolean start(Activity activity, Bundle options) {
1491 activity.startActivity(mResolvedIntent, options);
1492 return true;
1493 }
1494
1495 @Override
Alison Cichowlas3e340502018-08-07 17:15:01 -04001496 public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) {
Alison Cichowlas4691ed42018-11-13 15:59:55 -05001497 if (mEnableChooserDelegate) {
1498 return activity.startAsCallerImpl(mResolvedIntent, options, false, userId);
1499 } else {
1500 activity.startActivityAsCaller(mResolvedIntent, options, null, false, userId);
1501 return true;
1502 }
Adam Powell24428412015-04-01 17:19:56 -07001503 }
1504
1505 @Override
1506 public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
1507 activity.startActivityAsUser(mResolvedIntent, options, user);
1508 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001510
Matt Pietala4b30072019-04-04 13:44:36 -04001511 public boolean isSuspended() {
1512 return mIsSuspended;
1513 }
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001514 }
1515
1516 List<DisplayResolveInfo> getDisplayList() {
1517 return mAdapter.mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 }
1519
Adam Powell24428412015-04-01 17:19:56 -07001520 /**
1521 * A single target as represented in the chooser.
1522 */
1523 public interface TargetInfo {
1524 /**
1525 * Get the resolved intent that represents this target. Note that this may not be the
1526 * intent that will be launched by calling one of the <code>start</code> methods provided;
1527 * this is the intent that will be credited with the launch.
1528 *
1529 * @return the resolved intent for this target
1530 */
Adam Powell23882512016-01-29 10:21:00 -08001531 Intent getResolvedIntent();
Adam Powell24428412015-04-01 17:19:56 -07001532
1533 /**
1534 * Get the resolved component name that represents this target. Note that this may not
1535 * be the component that will be directly launched by calling one of the <code>start</code>
1536 * methods provided; this is the component that will be credited with the launch.
1537 *
1538 * @return the resolved ComponentName for this target
1539 */
Adam Powell23882512016-01-29 10:21:00 -08001540 ComponentName getResolvedComponentName();
Adam Powell24428412015-04-01 17:19:56 -07001541
1542 /**
1543 * Start the activity referenced by this target.
1544 *
1545 * @param activity calling Activity performing the launch
1546 * @param options ActivityOptions bundle
1547 * @return true if the start completed successfully
1548 */
Adam Powell23882512016-01-29 10:21:00 -08001549 boolean start(Activity activity, Bundle options);
Adam Powell24428412015-04-01 17:19:56 -07001550
1551 /**
1552 * Start the activity referenced by this target as if the ResolverActivity's caller
1553 * was performing the start operation.
1554 *
1555 * @param activity calling Activity (actually) performing the launch
1556 * @param options ActivityOptions bundle
1557 * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller
1558 * @return true if the start completed successfully
1559 */
Alison Cichowlas3e340502018-08-07 17:15:01 -04001560 boolean startAsCaller(ResolverActivity activity, Bundle options, int userId);
Adam Powell24428412015-04-01 17:19:56 -07001561
1562 /**
1563 * Start the activity referenced by this target as a given user.
1564 *
1565 * @param activity calling activity performing the launch
1566 * @param options ActivityOptions bundle
1567 * @param user handle for the user to start the activity as
1568 * @return true if the start completed successfully
1569 */
Adam Powell23882512016-01-29 10:21:00 -08001570 boolean startAsUser(Activity activity, Bundle options, UserHandle user);
Adam Powell24428412015-04-01 17:19:56 -07001571
1572 /**
1573 * Return the ResolveInfo about how and why this target matched the original query
1574 * for available targets.
1575 *
1576 * @return ResolveInfo representing this target's match
1577 */
Adam Powell23882512016-01-29 10:21:00 -08001578 ResolveInfo getResolveInfo();
Adam Powell24428412015-04-01 17:19:56 -07001579
1580 /**
1581 * Return the human-readable text label for this target.
1582 *
1583 * @return user-visible target label
1584 */
Adam Powell23882512016-01-29 10:21:00 -08001585 CharSequence getDisplayLabel();
Adam Powell24428412015-04-01 17:19:56 -07001586
1587 /**
1588 * Return any extended info for this target. This may be used to disambiguate
1589 * otherwise identical targets.
1590 *
1591 * @return human-readable disambig string or null if none present
1592 */
Adam Powell23882512016-01-29 10:21:00 -08001593 CharSequence getExtendedInfo();
Adam Powell24428412015-04-01 17:19:56 -07001594
1595 /**
Mike Digman9c4ae502019-03-19 17:02:25 -07001596 * @return The drawable that should be used to represent this target including badge
Adam Powell24428412015-04-01 17:19:56 -07001597 */
Adam Powell23882512016-01-29 10:21:00 -08001598 Drawable getDisplayIcon();
Adam Powell2ed547e2015-04-29 18:45:04 -07001599
1600 /**
1601 * Clone this target with the given fill-in information.
1602 */
Adam Powell23882512016-01-29 10:21:00 -08001603 TargetInfo cloneFilledIn(Intent fillInIntent, int flags);
Adam Powell2ed547e2015-04-29 18:45:04 -07001604
1605 /**
1606 * @return the list of supported source intents deduped against this single target
1607 */
Adam Powell23882512016-01-29 10:21:00 -08001608 List<Intent> getAllSourceIntents();
Matt Pietala4b30072019-04-04 13:44:36 -04001609
1610 /**
1611 * @return true if this target can be selected by the user
1612 */
1613 boolean isSuspended();
Adam Powell24428412015-04-01 17:19:56 -07001614 }
1615
Adam Powell23882512016-01-29 10:21:00 -08001616 public class ResolveListAdapter extends BaseAdapter {
Adam Powell7d758002015-05-06 17:49:36 -07001617 private final List<Intent> mIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001618 private final Intent[] mInitialIntents;
1619 private final List<ResolveInfo> mBaseResolveList;
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001620 protected ResolveInfo mLastChosen;
Adam Powell88831a22014-11-20 18:17:00 -08001621 private DisplayResolveInfo mOtherProfile;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001622 private ResolverListController mResolverListController;
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001623 private int mPlaceholderCount;
Adam Powell24428412015-04-01 17:19:56 -07001624
1625 protected final LayoutInflater mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001627 // This one is the list that the Adapter will actually present.
Adam Powell2ed547e2015-04-29 18:45:04 -07001628 List<DisplayResolveInfo> mDisplayList;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001629 List<ResolvedComponentInfo> mUnfilteredResolveList;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001630
Adam Powell278902c2014-07-12 18:33:22 -07001631 private int mLastChosenPosition = -1;
1632 private boolean mFilterLastUsed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633
Adam Powell7d758002015-05-06 17:49:36 -07001634 public ResolveListAdapter(Context context, List<Intent> payloadIntents,
1635 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001636 boolean filterLastUsed,
1637 ResolverListController resolverListController) {
Adam Powell7d758002015-05-06 17:49:36 -07001638 mIntents = payloadIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001639 mInitialIntents = initialIntents;
1640 mBaseResolveList = rList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001641 mLaunchedFromUid = launchedFromUid;
Adam Powelle9414d92014-07-05 17:44:18 -07001642 mInflater = LayoutInflater.from(context);
Adam Powell2ed547e2015-04-29 18:45:04 -07001643 mDisplayList = new ArrayList<>();
Adam Powell278902c2014-07-12 18:33:22 -07001644 mFilterLastUsed = filterLastUsed;
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001645 mResolverListController = resolverListController;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001648 public void handlePackagesChanged() {
1649 rebuildList();
Esteban Talavera6de72662014-12-11 17:54:07 +00001650 if (getCount() == 0) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001651 // We no longer have any items... just finish the activity.
1652 finish();
Adam Powellc5878612012-05-04 18:42:38 -07001653 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001654 }
1655
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001656 public void setPlaceholderCount(int count) {
1657 mPlaceholderCount = count;
1658 }
1659
Sumir Katariadb688af2017-05-10 17:33:47 -07001660 public int getPlaceholderCount() { return mPlaceholderCount; }
1661
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001662 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001663 public DisplayResolveInfo getFilteredItem() {
1664 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1665 // Not using getItem since it offsets to dodge this position for the list
Adam Powell2ed547e2015-04-29 18:45:04 -07001666 return mDisplayList.get(mLastChosenPosition);
Adam Powell278902c2014-07-12 18:33:22 -07001667 }
1668 return null;
1669 }
1670
Adam Powell88831a22014-11-20 18:17:00 -08001671 public DisplayResolveInfo getOtherProfile() {
1672 return mOtherProfile;
1673 }
1674
Adam Powell278902c2014-07-12 18:33:22 -07001675 public int getFilteredPosition() {
1676 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1677 return mLastChosenPosition;
1678 }
1679 return AbsListView.INVALID_POSITION;
1680 }
1681
1682 public boolean hasFilteredItem() {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001683 return mFilterLastUsed && mLastChosen != null;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001684 }
1685
Adam Powella182e452015-07-06 16:57:56 -07001686 public float getScore(DisplayResolveInfo target) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001687 return mResolverListController.getScore(target);
Adam Powella182e452015-07-06 16:57:56 -07001688 }
1689
Kang Li0cef910d2017-01-05 09:14:36 -08001690 public void updateModel(ComponentName componentName) {
1691 mResolverListController.updateModel(componentName);
1692 }
1693
Kang Li9fa2a2c2017-01-06 13:33:24 -08001694 public void updateChooserCounts(String packageName, int userId, String action) {
1695 mResolverListController.updateChooserCounts(packageName, userId, action);
1696 }
1697
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001698 /**
1699 * Rebuild the list of resolvers. In some cases some parts will need some asynchronous work
1700 * to complete.
1701 *
1702 * @return Whether or not the list building is completed.
1703 */
1704 protected boolean rebuildList() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001705 List<ResolvedComponentInfo> currentResolveList = null;
Sudheer Shanka7e64e102015-01-23 10:37:45 +00001706 // Clear the value of mOtherProfile from previous call.
1707 mOtherProfile = null;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001708 mLastChosen = null;
1709 mLastChosenPosition = -1;
Adam Powell2ed547e2015-04-29 18:45:04 -07001710 mDisplayList.clear();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001711 if (mBaseResolveList != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001712 currentResolveList = mUnfilteredResolveList = new ArrayList<>();
1713 mResolverListController.addResolveListDedupe(currentResolveList,
1714 getTargetIntent(),
1715 mBaseResolveList);
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001716 } else {
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001717 currentResolveList = mUnfilteredResolveList =
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001718 mResolverListController.getResolversForIntent(shouldGetResolvedFilter(),
1719 shouldGetActivityMetadata(),
1720 mIntents);
1721 if (currentResolveList == null) {
1722 processSortedList(currentResolveList);
1723 return true;
Adam Powell2ed547e2015-04-29 18:45:04 -07001724 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001725 List<ResolvedComponentInfo> originalList =
1726 mResolverListController.filterIneligibleActivities(currentResolveList,
1727 true);
1728 if (originalList != null) {
1729 mUnfilteredResolveList = originalList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001730 }
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001731 }
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001732
1733 // So far we only support a single other profile at a time.
1734 // The first one we see gets special treatment.
1735 for (ResolvedComponentInfo info : currentResolveList) {
1736 if (info.getResolveInfoAt(0).targetUserId != UserHandle.USER_CURRENT) {
1737 mOtherProfile = new DisplayResolveInfo(info.getIntentAt(0),
1738 info.getResolveInfoAt(0),
1739 info.getResolveInfoAt(0).loadLabel(mPm),
1740 info.getResolveInfoAt(0).loadLabel(mPm),
1741 getReplacementIntent(info.getResolveInfoAt(0).activityInfo,
1742 info.getIntentAt(0)));
1743 currentResolveList.remove(info);
1744 break;
1745 }
1746 }
1747
1748 if (mOtherProfile == null) {
1749 try {
1750 mLastChosen = mResolverListController.getLastChosen();
1751 } catch (RemoteException re) {
1752 Log.d(TAG, "Error calling getLastChosenActivity\n" + re);
1753 }
1754 }
1755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 int N;
You Kim43a5070e2012-11-21 23:23:45 +09001757 if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001758 // We only care about fixing the unfilteredList if the current resolve list and
1759 // current resolve list are currently the same.
1760 List<ResolvedComponentInfo> originalList =
1761 mResolverListController.filterLowPriority(currentResolveList,
1762 mUnfilteredResolveList == currentResolveList);
1763 if (originalList != null) {
1764 mUnfilteredResolveList = originalList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001766
Hakan Seyalioglu873cbfd2017-02-21 19:23:43 -08001767 if (currentResolveList.size() > 1) {
Sumir Katariadb688af2017-05-10 17:33:47 -07001768 int placeholderCount = currentResolveList.size();
1769 if (useLayoutWithDefault()) {
1770 --placeholderCount;
1771 }
1772 setPlaceholderCount(placeholderCount);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001773 AsyncTask<List<ResolvedComponentInfo>,
1774 Void,
1775 List<ResolvedComponentInfo>> sortingTask =
1776 new AsyncTask<List<ResolvedComponentInfo>,
1777 Void,
1778 List<ResolvedComponentInfo>>() {
1779 @Override
1780 protected List<ResolvedComponentInfo> doInBackground(
1781 List<ResolvedComponentInfo>... params) {
1782 mResolverListController.sort(params[0]);
1783 return params[0];
1784 }
1785
1786 @Override
1787 protected void onPostExecute(List<ResolvedComponentInfo> sortedComponents) {
1788 processSortedList(sortedComponents);
Matt Pietal74c6ed02019-04-18 13:38:46 -04001789 bindProfileView();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001790 notifyDataSetChanged();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001791 }
1792 };
1793 sortingTask.execute(currentResolveList);
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001794 postListReadyRunnable();
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001795 return false;
1796 } else {
1797 processSortedList(currentResolveList);
1798 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001800 } else {
1801 processSortedList(currentResolveList);
1802 return true;
1803 }
1804 }
1805
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001806
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001807 private void processSortedList(List<ResolvedComponentInfo> sortedComponents) {
1808 int N;
1809 if (sortedComponents != null && (N = sortedComponents.size()) != 0) {
Dianne Hackborneb034652009-09-07 00:49:58 -07001810 // First put the initial items at the top.
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001811 if (mInitialIntents != null) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001812 for (int i = 0; i < mInitialIntents.length; i++) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001813 Intent ii = mInitialIntents[i];
Dianne Hackborneb034652009-09-07 00:49:58 -07001814 if (ii == null) {
1815 continue;
1816 }
1817 ActivityInfo ai = ii.resolveActivityInfo(
1818 getPackageManager(), 0);
1819 if (ai == null) {
Adam Powell09a65602014-07-20 16:23:14 -07001820 Log.w(TAG, "No activity found for " + ii);
Dianne Hackborneb034652009-09-07 00:49:58 -07001821 continue;
1822 }
1823 ResolveInfo ri = new ResolveInfo();
1824 ri.activityInfo = ai;
Nicolas Prevot1a815922014-10-10 16:22:38 +01001825 UserManager userManager =
1826 (UserManager) getSystemService(Context.USER_SERVICE);
Dianne Hackborneb034652009-09-07 00:49:58 -07001827 if (ii instanceof LabeledIntent) {
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001828 LabeledIntent li = (LabeledIntent) ii;
Dianne Hackborneb034652009-09-07 00:49:58 -07001829 ri.resolvePackageName = li.getSourcePackage();
1830 ri.labelRes = li.getLabelResource();
1831 ri.nonLocalizedLabel = li.getNonLocalizedLabel();
1832 ri.icon = li.getIconResource();
Sudheer Shanka9ded7602015-05-19 21:17:25 +01001833 ri.iconResourceId = ri.icon;
1834 }
1835 if (userManager.isManagedProfile()) {
1836 ri.noResourceId = true;
1837 ri.icon = 0;
Dianne Hackborneb034652009-09-07 00:49:58 -07001838 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001839 addResolveInfo(new DisplayResolveInfo(ii, ri,
Dianne Hackborneb034652009-09-07 00:49:58 -07001840 ri.loadLabel(getPackageManager()), null, ii));
1841 }
1842 }
You Kim43a5070e2012-11-21 23:23:45 +09001843
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001844
Mike Digmanba232682019-03-27 14:55:26 -07001845 for (ResolvedComponentInfo rci : sortedComponents) {
1846 final ResolveInfo ri = rci.getResolveInfoAt(0);
1847 if (ri != null) {
1848 ResolveInfoPresentationGetter pg = makePresentationGetter(ri);
1849 addResolveInfoWithAlternates(rci, pg.getSubLabel(), pg.getLabel());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
Hakan Seyalioglu33550122017-01-06 19:54:43 -08001853
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001854
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001855 postListReadyRunnable();
1856 }
1857
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04001858
1859
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001860 /**
1861 * Some necessary methods for creating the list are initiated in onCreate and will also
1862 * determine the layout known. We therefore can't update the UI inline and post to the
1863 * handler thread to update after the current task is finished.
1864 */
1865 private void postListReadyRunnable() {
1866 if (mPostListReadyRunnable == null) {
1867 mPostListReadyRunnable = new Runnable() {
1868 @Override
1869 public void run() {
Matt Pietal26038402019-01-08 07:29:34 -05001870 setHeader();
shafik69df96322018-12-18 15:41:19 +00001871 resetButtonBar();
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001872 onListRebuilt();
1873 mPostListReadyRunnable = null;
1874 }
1875 };
1876 getMainThreadHandler().post(mPostListReadyRunnable);
1877 }
Adam Powell24428412015-04-01 17:19:56 -07001878 }
1879
1880 public void onListRebuilt() {
Xiaohui Chen393c8012017-02-14 14:55:07 -08001881 int count = getUnfilteredCount();
1882 if (count == 1 && getOtherProfile() == null) {
1883 // Only one target, so we're a candidate to auto-launch!
1884 final TargetInfo target = targetInfoForPosition(0, false);
1885 if (shouldAutoLaunchSingleChoice(target)) {
1886 safelyStartActivity(target);
1887 finish();
1888 }
1889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891
Adam Powellc6d5e3a2015-04-23 12:22:20 -07001892 public boolean shouldGetResolvedFilter() {
1893 return mFilterLastUsed;
1894 }
1895
Adam Powell2ed547e2015-04-29 18:45:04 -07001896 private void addResolveInfoWithAlternates(ResolvedComponentInfo rci,
1897 CharSequence extraInfo, CharSequence roLabel) {
1898 final int count = rci.getCount();
1899 final Intent intent = rci.getIntentAt(0);
1900 final ResolveInfo add = rci.getResolveInfoAt(0);
1901 final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent);
1902 final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel,
1903 extraInfo, replaceIntent);
1904 addResolveInfo(dri);
1905 if (replaceIntent == intent) {
1906 // Only add alternates if we didn't get a specific replacement from
1907 // the caller. If we have one it trumps potential alternates.
1908 for (int i = 1, N = count; i < N; i++) {
1909 final Intent altIntent = rci.getIntentAt(i);
1910 dri.addAlternateSourceIntent(altIntent);
1911 }
1912 }
1913 updateLastChosenPosition(add);
1914 }
1915
Esteban Talavera6de72662014-12-11 17:54:07 +00001916 private void updateLastChosenPosition(ResolveInfo info) {
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001917 // If another profile is present, ignore the last chosen entry.
1918 if (mOtherProfile != null) {
1919 mLastChosenPosition = -1;
1920 return;
1921 }
Esteban Talavera6de72662014-12-11 17:54:07 +00001922 if (mLastChosen != null
1923 && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName)
1924 && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001925 mLastChosenPosition = mDisplayList.size() - 1;
Esteban Talavera6de72662014-12-11 17:54:07 +00001926 }
1927 }
1928
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08001929 // We assume that at this point we've already filtered out the only intent for a different
1930 // targetUserId which we're going to use.
Adam Powell88831a22014-11-20 18:17:00 -08001931 private void addResolveInfo(DisplayResolveInfo dri) {
Kang Li6afa4f22017-06-23 12:54:38 -07001932 if (dri != null && dri.mResolveInfo != null
1933 && dri.mResolveInfo.targetUserId == UserHandle.USER_CURRENT) {
1934 // Checks if this info is already listed in display.
1935 for (DisplayResolveInfo existingInfo : mDisplayList) {
1936 if (resolveInfoMatch(dri.mResolveInfo, existingInfo.mResolveInfo)) {
1937 return;
1938 }
1939 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001940 mDisplayList.add(dri);
Adam Powell88831a22014-11-20 18:17:00 -08001941 }
1942 }
1943
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001944 @Nullable
Adam Powell278902c2014-07-12 18:33:22 -07001945 public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001946 TargetInfo target = targetInfoForPosition(position, filtered);
1947 if (target != null) {
1948 return target.getResolveInfo();
1949 }
1950 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001953 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001954 public TargetInfo targetInfoForPosition(int position, boolean filtered) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001955 if (filtered) {
1956 return getItem(position);
1957 }
1958 if (mDisplayList.size() > position) {
1959 return mDisplayList.get(position);
1960 }
1961 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
1963
1964 public int getCount() {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001965 int totalSize = mDisplayList == null || mDisplayList.isEmpty() ? mPlaceholderCount :
1966 mDisplayList.size();
Adam Powell278902c2014-07-12 18:33:22 -07001967 if (mFilterLastUsed && mLastChosenPosition >= 0) {
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001968 totalSize--;
Adam Powell278902c2014-07-12 18:33:22 -07001969 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001970 return totalSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972
Adam Powell50077352015-05-26 18:01:55 -07001973 public int getUnfilteredCount() {
1974 return mDisplayList.size();
1975 }
1976
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001977 @Nullable
Adam Powell24428412015-04-01 17:19:56 -07001978 public TargetInfo getItem(int position) {
Adam Powell278902c2014-07-12 18:33:22 -07001979 if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) {
1980 position++;
1981 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08001982 if (mDisplayList.size() > position) {
1983 return mDisplayList.get(position);
1984 } else {
1985 return null;
1986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 }
1988
1989 public long getItemId(int position) {
1990 return position;
1991 }
1992
Adam Powell23882512016-01-29 10:21:00 -08001993 public int getDisplayResolveInfoCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001994 return mDisplayList.size();
Adam Powell24428412015-04-01 17:19:56 -07001995 }
1996
Adam Powell23882512016-01-29 10:21:00 -08001997 public DisplayResolveInfo getDisplayResolveInfo(int index) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001998 // Used to query services. We only query services for primary targets, not alternates.
1999 return mDisplayList.get(index);
Adam Powell24428412015-04-01 17:19:56 -07002000 }
2001
2002 public final View getView(int position, View convertView, ViewGroup parent) {
Adam Powellfd1e93d2014-09-07 16:52:22 -07002003 View view = convertView;
2004 if (view == null) {
Adam Powell24428412015-04-01 17:19:56 -07002005 view = createView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 }
Adam Powell7d758002015-05-06 17:49:36 -07002007 onBindView(view, getItem(position));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 return view;
2009 }
2010
Adam Powell7d758002015-05-06 17:49:36 -07002011 public final View createView(ViewGroup parent) {
2012 final View view = onCreateView(parent);
2013 final ViewHolder holder = new ViewHolder(view);
2014 view.setTag(holder);
2015 return view;
2016 }
2017
2018 public View onCreateView(ViewGroup parent) {
Adam Powell24428412015-04-01 17:19:56 -07002019 return mInflater.inflate(
2020 com.android.internal.R.layout.resolve_list_item, parent, false);
2021 }
2022
Adam Powell7d758002015-05-06 17:49:36 -07002023 public final void bindView(int position, View view) {
2024 onBindView(view, getItem(position));
2025 }
2026
Mike Digmanba232682019-03-27 14:55:26 -07002027 protected void onBindView(View view, TargetInfo info) {
Adam Powell0256c6f2013-05-29 16:42:33 -07002028 final ViewHolder holder = (ViewHolder) view.getTag();
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002029 if (info == null) {
2030 holder.icon.setImageDrawable(
2031 getDrawable(R.drawable.resolver_icon_placeholder));
2032 return;
2033 }
Mike Digmanba232682019-03-27 14:55:26 -07002034
Adam Powell63b31692015-09-28 10:45:00 -07002035 final CharSequence label = info.getDisplayLabel();
2036 if (!TextUtils.equals(holder.text.getText(), label)) {
2037 holder.text.setText(info.getDisplayLabel());
2038 }
Mike Digmanba232682019-03-27 14:55:26 -07002039
2040 // Always show a subLabel for visual consistency across list items. Show an empty
2041 // subLabel if the subLabel is the same as the label
2042 CharSequence subLabel = info.getExtendedInfo();
2043 if (TextUtils.equals(label, subLabel)) subLabel = null;
2044
2045 if (!TextUtils.equals(holder.text2.getText(), subLabel)) {
2046 holder.text2.setText(subLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
Mike Digmanba232682019-03-27 14:55:26 -07002048
Matt Pietala4b30072019-04-04 13:44:36 -04002049 if (info.isSuspended()) {
2050 holder.icon.setColorFilter(mSuspendedMatrixColorFilter);
2051 } else {
2052 holder.icon.setColorFilter(null);
2053 }
2054
Adam Powell24428412015-04-01 17:19:56 -07002055 if (info instanceof DisplayResolveInfo
2056 && !((DisplayResolveInfo) info).hasDisplayIcon()) {
Matt Pietalaf044ae2019-03-29 06:53:53 -04002057 new LoadIconTask((DisplayResolveInfo) info, holder.icon).execute();
2058 } else {
2059 holder.icon.setImageDrawable(info.getDisplayIcon());
Dianne Hackborneb034652009-09-07 00:49:58 -07002060 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002061 }
2062 }
2063
Alison Cichowlas1c8816c2019-04-03 17:43:22 -04002064
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08002065 @VisibleForTesting
2066 public static final class ResolvedComponentInfo {
Adam Powell2ed547e2015-04-29 18:45:04 -07002067 public final ComponentName name;
2068 private final List<Intent> mIntents = new ArrayList<>();
2069 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
2070
2071 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
2072 this.name = name;
2073 add(intent, info);
2074 }
2075
2076 public void add(Intent intent, ResolveInfo info) {
2077 mIntents.add(intent);
2078 mResolveInfos.add(info);
2079 }
2080
2081 public int getCount() {
2082 return mIntents.size();
2083 }
2084
2085 public Intent getIntentAt(int index) {
2086 return index >= 0 ? mIntents.get(index) : null;
2087 }
2088
2089 public ResolveInfo getResolveInfoAt(int index) {
2090 return index >= 0 ? mResolveInfos.get(index) : null;
2091 }
2092
2093 public int findIntent(Intent intent) {
2094 for (int i = 0, N = mIntents.size(); i < N; i++) {
2095 if (intent.equals(mIntents.get(i))) {
2096 return i;
2097 }
2098 }
2099 return -1;
2100 }
2101
2102 public int findResolveInfo(ResolveInfo info) {
2103 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
2104 if (info.equals(mResolveInfos.get(i))) {
2105 return i;
2106 }
2107 }
2108 return -1;
2109 }
2110 }
2111
Adam Powell0256c6f2013-05-29 16:42:33 -07002112 static class ViewHolder {
Mike Digman4b83c212019-05-03 10:17:35 -07002113 public View itemView;
2114 public Drawable defaultItemViewBackground;
2115
Adam Powell0256c6f2013-05-29 16:42:33 -07002116 public TextView text;
2117 public TextView text2;
2118 public ImageView icon;
2119
2120 public ViewHolder(View view) {
Mike Digman4b83c212019-05-03 10:17:35 -07002121 itemView = view;
2122 defaultItemViewBackground = view.getBackground();
Adam Powell0256c6f2013-05-29 16:42:33 -07002123 text = (TextView) view.findViewById(com.android.internal.R.id.text1);
2124 text2 = (TextView) view.findViewById(com.android.internal.R.id.text2);
2125 icon = (ImageView) view.findViewById(R.id.icon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127 }
2128
Adam Powell7d758002015-05-06 17:49:36 -07002129 class ItemClickListener implements AdapterView.OnItemClickListener,
2130 AdapterView.OnItemLongClickListener {
2131 @Override
2132 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
2133 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2134 if (listView != null) {
2135 position -= listView.getHeaderViewsCount();
2136 }
2137 if (position < 0) {
2138 // Header views don't count.
2139 return;
2140 }
Hakan Seyalioglu23f34652017-02-03 09:38:35 -08002141 // If we're still loading, we can't yet enable the buttons.
2142 if (mAdapter.resolveInfoForPosition(position, true) == null) {
2143 return;
2144 }
2145
Adam Powell7d758002015-05-06 17:49:36 -07002146 final int checkedPos = mAdapterView.getCheckedItemPosition();
2147 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Hakan Seyalioglu13405c52017-01-31 19:01:31 -08002148 if (!useLayoutWithDefault()
Ruchi Kandoi65d76c12017-08-18 13:05:39 -07002149 && (!hasValidSelection || mLastSelected != checkedPos)
shafik69df96322018-12-18 15:41:19 +00002150 && (mAlwaysButton != null || mSettingsButton != null)) {
2151 if (mSettingsButton != null) {
2152 // this implies that the layout for browsables is being used
2153 mSettingsButton.setEnabled(true);
2154 } else {
2155 // this implies that mAlwaysButton != null
2156 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
2157 }
Adam Powell7d758002015-05-06 17:49:36 -07002158 mOnceButton.setEnabled(hasValidSelection);
2159 if (hasValidSelection) {
2160 mAdapterView.smoothScrollToPosition(checkedPos);
2161 }
2162 mLastSelected = checkedPos;
2163 } else {
2164 startSelected(position, false, true);
2165 }
2166 }
Adam Powell2d809622012-03-22 15:24:43 -07002167
2168 @Override
2169 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07002170 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
2171 if (listView != null) {
2172 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07002173 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07002174 if (position < 0) {
2175 // Header views don't count.
2176 return false;
2177 }
Adam Powell278902c2014-07-12 18:33:22 -07002178 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08002179 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07002180 return true;
2181 }
2182
2183 }
Adam Powell0256c6f2013-05-29 16:42:33 -07002184
Matt Pietalaf044ae2019-03-29 06:53:53 -04002185 class LoadIconTask extends AsyncTask<Void, Void, Drawable> {
Adam Powell24428412015-04-01 17:19:56 -07002186 protected final DisplayResolveInfo mDisplayResolveInfo;
2187 private final ResolveInfo mResolveInfo;
Matt Pietalaf044ae2019-03-29 06:53:53 -04002188 private final ImageView mTargetView;
Adam Powell24428412015-04-01 17:19:56 -07002189
Matt Pietalaf044ae2019-03-29 06:53:53 -04002190 LoadIconTask(DisplayResolveInfo dri, ImageView target) {
Adam Powell24428412015-04-01 17:19:56 -07002191 mDisplayResolveInfo = dri;
2192 mResolveInfo = dri.getResolveInfo();
Matt Pietalaf044ae2019-03-29 06:53:53 -04002193 mTargetView = target;
Adam Powell0256c6f2013-05-29 16:42:33 -07002194 }
2195
2196 @Override
Adam Powell24428412015-04-01 17:19:56 -07002197 protected Drawable doInBackground(Void... params) {
2198 return loadIconForResolveInfo(mResolveInfo);
2199 }
2200
2201 @Override
2202 protected void onPostExecute(Drawable d) {
Matt Pietal74c6ed02019-04-18 13:38:46 -04002203 if (mAdapter.getOtherProfile() == mDisplayResolveInfo) {
Adam Powell88831a22014-11-20 18:17:00 -08002204 bindProfileView();
Matt Pietalaf044ae2019-03-29 06:53:53 -04002205 } else {
2206 mDisplayResolveInfo.setDisplayIcon(d);
2207 mTargetView.setImageDrawable(d);
Adam Powell88831a22014-11-20 18:17:00 -08002208 }
Adam Powell278902c2014-07-12 18:33:22 -07002209 }
2210 }
Adam Powell09a65602014-07-20 16:23:14 -07002211
Dianne Hackbornec452d92014-11-11 17:16:56 -08002212 static final boolean isSpecificUriMatch(int match) {
2213 match = match&IntentFilter.MATCH_CATEGORY_MASK;
2214 return match >= IntentFilter.MATCH_CATEGORY_HOST
2215 && match <= IntentFilter.MATCH_CATEGORY_PATH;
2216 }
2217
Adam Powell4c470d62015-06-19 17:46:17 -07002218 static class PickTargetOptionRequest extends PickOptionRequest {
2219 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
2220 @Nullable Bundle extras) {
2221 super(prompt, options, extras);
2222 }
2223
2224 @Override
2225 public void onCancel() {
2226 super.onCancel();
2227 final ResolverActivity ra = (ResolverActivity) getActivity();
2228 if (ra != null) {
2229 ra.mPickOptionRequest = null;
2230 ra.finish();
2231 }
2232 }
2233
2234 @Override
2235 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
2236 super.onPickOptionResult(finished, selections, result);
2237 if (selections.length != 1) {
2238 // TODO In a better world we would filter the UI presented here and let the
2239 // user refine. Maybe later.
2240 return;
2241 }
2242
2243 final ResolverActivity ra = (ResolverActivity) getActivity();
2244 if (ra != null) {
2245 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
2246 if (ra.onTargetSelected(ti, false)) {
2247 ra.mPickOptionRequest = null;
2248 ra.finish();
2249 }
2250 }
2251 }
2252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253}