blob: 72a40b7dcb45cdff5d806dc4f48b548d66533a7c [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
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -070019import android.annotation.NonNull;
Adam Powell4c470d62015-06-19 17:46:17 -070020import android.annotation.Nullable;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -070021import android.annotation.StringRes;
Adam Powelle9414d92014-07-05 17:44:18 -070022import android.app.Activity;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070023import android.app.ActivityThread;
Adam Powell4c470d62015-06-19 17:46:17 -070024import android.app.VoiceInteractor.PickOptionRequest;
25import android.app.VoiceInteractor.PickOptionRequest.Option;
26import android.app.VoiceInteractor.Prompt;
Adam Powell23882512016-01-29 10:21:00 -080027import android.content.pm.ComponentInfo;
Adam Powell0256c6f2013-05-29 16:42:33 -070028import android.os.AsyncTask;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -070029import android.provider.MediaStore;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070030import android.provider.Settings;
Adam Powell11f59a02014-08-20 13:22:16 -070031import android.text.TextUtils;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -070032import android.util.Slog;
Adam Powell278902c2014-07-12 18:33:22 -070033import android.widget.AbsListView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import com.android.internal.R;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -080035import com.android.internal.content.PackageMonitor;
36
Adam Powellc5878612012-05-04 18:42:38 -070037import android.app.ActivityManager;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070038import android.app.ActivityManagerNative;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -070039import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.ComponentName;
41import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.Intent;
43import android.content.IntentFilter;
44import android.content.pm.ActivityInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010045import android.content.pm.ApplicationInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -070046import android.content.pm.LabeledIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.content.pm.PackageManager;
Adam Powellc5878612012-05-04 18:42:38 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.pm.ResolveInfo;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010050import android.content.pm.UserInfo;
Adam Powellc5878612012-05-04 18:42:38 -070051import android.content.res.Resources;
Dianne Hackborneb034652009-09-07 00:49:58 -070052import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.net.Uri;
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;
56import android.os.PatternMatcher;
Dianne Hackborn5320eb82012-05-18 12:05:04 -070057import android.os.RemoteException;
Jeff Sharkey37355a92016-02-05 16:19:10 -070058import android.os.StrictMode;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070059import android.os.UserHandle;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010060import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.Log;
Adam Powell2d809622012-03-22 15:24:43 -070062import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.view.View;
64import android.view.ViewGroup;
Adam Powell2d809622012-03-22 15:24:43 -070065import android.widget.AdapterView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.widget.BaseAdapter;
Adam Powellc5878612012-05-04 18:42:38 -070067import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.widget.ImageView;
Adam Powell2d809622012-03-22 15:24:43 -070069import android.widget.ListView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.TextView;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +010071import android.widget.Toast;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010072
73import com.android.internal.logging.MetricsLogger;
Tamas Berghammercbd3f0c2016-06-22 15:21:38 +010074import com.android.internal.logging.nano.MetricsProto;
Adam Powell4f6c2052014-07-07 18:49:10 -070075import com.android.internal.widget.ResolverDrawerLayout;
Adam Powell2d809622012-03-22 15:24:43 -070076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.util.ArrayList;
Clara Bayarrifa902aa2016-04-13 14:45:08 +010078import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.util.Collections;
80import java.util.HashSet;
81import java.util.Iterator;
82import java.util.List;
Adam Powellc412be62015-06-24 13:54:10 -070083import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.Set;
85
Wale Ogunwale9014e662016-03-19 14:55:46 -070086import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
Adrian Roos27c3ab62014-10-15 17:21:31 +020087import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
88import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090/**
91 * This activity is displayed when the system attempts to start an Intent for
92 * which there is more than one matching activity, allowing the user to decide
93 * which to go to. It is not normally used directly by application developers.
94 */
Adam Powell7d758002015-05-06 17:49:36 -070095public class ResolverActivity extends Activity {
Adam Powellc5878612012-05-04 18:42:38 -070096 private static final String TAG = "ResolverActivity";
Adam Powell09a65602014-07-20 16:23:14 -070097 private static final boolean DEBUG = false;
Adam Powellc5878612012-05-04 18:42:38 -070098
Dianne Hackborn5320eb82012-05-18 12:05:04 -070099 private int mLaunchedFromUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 private ResolveListAdapter mAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private PackageManager mPm;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700102 private boolean mSafeForwardingMode;
Adam Powellc5878612012-05-04 18:42:38 -0700103 private boolean mAlwaysUseOption;
Adam Powell24428412015-04-01 17:19:56 -0700104 private AbsListView mAdapterView;
Adam Powellc5878612012-05-04 18:42:38 -0700105 private Button mAlwaysButton;
106 private Button mOnceButton;
Adam Powell88831a22014-11-20 18:17:00 -0800107 private View mProfileView;
Adam Powellc5878612012-05-04 18:42:38 -0700108 private int mIconDpi;
Adam Powell24428412015-04-01 17:19:56 -0700109 private int mLastSelected = AbsListView.INVALID_POSITION;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100110 private boolean mResolvingHome = false;
Sander Alewijnsef6545332014-10-31 12:39:02 +0000111 private int mProfileSwitchMessageId = -1;
Adam Powell2ed547e2015-04-29 18:45:04 -0700112 private final ArrayList<Intent> mIntents = new ArrayList<>();
Adam Powelld25267c2015-06-05 18:02:21 -0700113 private ResolverComparator mResolverComparator;
Adam Powell4c470d62015-06-19 17:46:17 -0700114 private PickTargetOptionRequest mPickOptionRequest;
Adam Powell52c39212016-04-07 15:14:18 -0700115 private ComponentName[] mFilteredComponents;
Adam Powell09a65602014-07-20 16:23:14 -0700116
Adam Powell63b31692015-09-28 10:45:00 -0700117 protected ResolverDrawerLayout mResolverDrawerLayout;
118
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700119 private boolean mRegistered;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800120 private final PackageMonitor mPackageMonitor = new PackageMonitor() {
121 @Override public void onSomePackagesChanged() {
122 mAdapter.handlePackagesChanged();
Adam Powell88831a22014-11-20 18:17:00 -0800123 if (mProfileView != null) {
124 bindProfileView();
125 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800126 }
127 };
128
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700129 /**
130 * Get the string resource to be used as a label for the link to the resolver activity for an
131 * action.
132 *
133 * @param action The action to resolve
134 *
135 * @return The string resource to be used as a label
136 */
137 public static @StringRes int getLabelRes(String action) {
138 return ActionTitle.forAction(action).labelRes;
139 }
140
Adam Powell278902c2014-07-12 18:33:22 -0700141 private enum ActionTitle {
142 VIEW(Intent.ACTION_VIEW,
143 com.android.internal.R.string.whichViewApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700144 com.android.internal.R.string.whichViewApplicationNamed,
145 com.android.internal.R.string.whichViewApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700146 EDIT(Intent.ACTION_EDIT,
147 com.android.internal.R.string.whichEditApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700148 com.android.internal.R.string.whichEditApplicationNamed,
149 com.android.internal.R.string.whichEditApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700150 SEND(Intent.ACTION_SEND,
151 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700152 com.android.internal.R.string.whichSendApplicationNamed,
153 com.android.internal.R.string.whichSendApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700154 SENDTO(Intent.ACTION_SENDTO,
Adam Powell13ea8f42016-03-18 09:39:41 -0700155 com.android.internal.R.string.whichSendToApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700156 com.android.internal.R.string.whichSendToApplicationNamed,
157 com.android.internal.R.string.whichSendToApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700158 SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE,
159 com.android.internal.R.string.whichSendApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700160 com.android.internal.R.string.whichSendApplicationNamed,
161 com.android.internal.R.string.whichSendApplicationLabel),
162 CAPTURE_IMAGE(MediaStore.ACTION_IMAGE_CAPTURE,
163 com.android.internal.R.string.whichImageCaptureApplication,
164 com.android.internal.R.string.whichImageCaptureApplicationNamed,
165 com.android.internal.R.string.whichImageCaptureApplicationLabel),
Adam Powell278902c2014-07-12 18:33:22 -0700166 DEFAULT(null,
167 com.android.internal.R.string.whichApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700168 com.android.internal.R.string.whichApplicationNamed,
169 com.android.internal.R.string.whichApplicationLabel),
Adam Powella35c77a2014-09-25 16:46:36 -0700170 HOME(Intent.ACTION_MAIN,
171 com.android.internal.R.string.whichHomeApplication,
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700172 com.android.internal.R.string.whichHomeApplicationNamed,
173 com.android.internal.R.string.whichHomeApplicationLabel);
Adam Powell278902c2014-07-12 18:33:22 -0700174
175 public final String action;
176 public final int titleRes;
177 public final int namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700178 public final @StringRes int labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700179
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700180 ActionTitle(String action, int titleRes, int namedTitleRes, @StringRes int labelRes) {
Adam Powell278902c2014-07-12 18:33:22 -0700181 this.action = action;
182 this.titleRes = titleRes;
183 this.namedTitleRes = namedTitleRes;
Philip P. Moltmannf8173ca2016-04-12 15:11:23 -0700184 this.labelRes = labelRes;
Adam Powell278902c2014-07-12 18:33:22 -0700185 }
186
187 public static ActionTitle forAction(String action) {
188 for (ActionTitle title : values()) {
Adam Powella35c77a2014-09-25 16:46:36 -0700189 if (title != HOME && action != null && action.equals(title.action)) {
Adam Powell278902c2014-07-12 18:33:22 -0700190 return title;
191 }
192 }
193 return DEFAULT;
194 }
195 }
196
Dianne Hackborn905577f2011-09-07 18:31:28 -0700197 private Intent makeMyIntent() {
198 Intent intent = new Intent(getIntent());
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700199 intent.setComponent(null);
Dianne Hackborn905577f2011-09-07 18:31:28 -0700200 // The resolver activity is set to be hidden from recent tasks.
201 // we don't want this attribute to be propagated to the next activity
202 // being launched. Note that if the original Intent also had this
203 // flag set, we are now losing it. That should be a very rare case
204 // and we can live with this.
205 intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
206 return intent;
207 }
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 @Override
210 protected void onCreate(Bundle savedInstanceState) {
Christopher Tateb72b3632013-09-30 17:50:32 -0700211 // Use a specialized prompt when we're handling the 'Home' app startActivity()
Christopher Tateb72b3632013-09-30 17:50:32 -0700212 final Intent intent = makeMyIntent();
213 final Set<String> categories = intent.getCategories();
214 if (Intent.ACTION_MAIN.equals(intent.getAction())
215 && categories != null
216 && categories.size() == 1
217 && categories.contains(Intent.CATEGORY_HOME)) {
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100218 // Note: this field is not set to true in the compatibility version.
219 mResolvingHome = true;
Christopher Tateb72b3632013-09-30 17:50:32 -0700220 }
221
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700222 setSafeForwardingMode(true);
223
Adam Powella35c77a2014-09-25 16:46:36 -0700224 onCreate(savedInstanceState, intent, null, 0, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 }
226
Adam Powell278902c2014-07-12 18:33:22 -0700227 /**
Adam Powell24428412015-04-01 17:19:56 -0700228 * Compatibility version for other bundled services that use this overload without
Adam Powell278902c2014-07-12 18:33:22 -0700229 * a default title resource
230 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 protected void onCreate(Bundle savedInstanceState, Intent intent,
Adam Powell278902c2014-07-12 18:33:22 -0700232 CharSequence title, Intent[] initialIntents,
233 List<ResolveInfo> rList, boolean alwaysUseOption) {
Adam Powell09a65602014-07-20 16:23:14 -0700234 onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, alwaysUseOption);
Adam Powell278902c2014-07-12 18:33:22 -0700235 }
236
237 protected void onCreate(Bundle savedInstanceState, Intent intent,
238 CharSequence title, int defaultTitleRes, Intent[] initialIntents,
239 List<ResolveInfo> rList, boolean alwaysUseOption) {
Adam Powelle9414d92014-07-05 17:44:18 -0700240 setTheme(R.style.Theme_DeviceDefault_Resolver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 super.onCreate(savedInstanceState);
Sander Alewijnsef6545332014-10-31 12:39:02 +0000242
243 // Determine whether we should show that intent is forwarded
244 // from managed profile to owner or other way around.
245 setProfileSwitchMessageId(intent.getContentUserHint());
246
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700247 try {
248 mLaunchedFromUid = ActivityManagerNative.getDefault().getLaunchedFromUid(
249 getActivityToken());
250 } catch (RemoteException e) {
251 mLaunchedFromUid = -1;
252 }
Adam Powell7d758002015-05-06 17:49:36 -0700253
254 if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) {
255 // Gulp!
256 finish();
257 return;
258 }
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 mPm = getPackageManager();
Adam Powell09a65602014-07-20 16:23:14 -0700261
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700262 mPackageMonitor.register(this, getMainLooper(), false);
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700263 mRegistered = true;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800264
Adam Powellc5878612012-05-04 18:42:38 -0700265 final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
266 mIconDpi = am.getLauncherLargeIconDensity();
Adam Powellc5878612012-05-04 18:42:38 -0700267
Adam Powell7d758002015-05-06 17:49:36 -0700268 // Add our initial intent as the first item, regardless of what else has already been added.
Adam Powell2ed547e2015-04-29 18:45:04 -0700269 mIntents.add(0, new Intent(intent));
Adam Powell278902c2014-07-12 18:33:22 -0700270
Adam Powelld25267c2015-06-05 18:02:21 -0700271 final String referrerPackage = getReferrerPackageName();
272
273 mResolverComparator = new ResolverComparator(this, getTargetIntent(), referrerPackage);
274
Adam Powell39e94eb2015-09-08 17:01:49 -0700275 if (configureContentView(mIntents, initialIntents, rList, alwaysUseOption)) {
276 return;
277 }
Adam Powell278902c2014-07-12 18:33:22 -0700278
Adam Powell4f6c2052014-07-07 18:49:10 -0700279 final ResolverDrawerLayout rdl = (ResolverDrawerLayout) findViewById(R.id.contentPanel);
280 if (rdl != null) {
Adam Powell5dd072d2014-10-30 19:51:41 -0700281 rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700282 @Override
Adam Powell5dd072d2014-10-30 19:51:41 -0700283 public void onDismissed() {
Adam Powell4f6c2052014-07-07 18:49:10 -0700284 finish();
285 }
286 });
Adam Powell4c470d62015-06-19 17:46:17 -0700287 if (isVoiceInteraction()) {
288 rdl.setCollapsed(false);
289 }
Adam Powell63b31692015-09-28 10:45:00 -0700290 mResolverDrawerLayout = rdl;
Adam Powell4f6c2052014-07-07 18:49:10 -0700291 }
292
Adam Powell11f59a02014-08-20 13:22:16 -0700293 if (title == null) {
294 title = getTitleForAction(intent.getAction(), defaultTitleRes);
295 }
296 if (!TextUtils.isEmpty(title)) {
297 final TextView titleView = (TextView) findViewById(R.id.title);
298 if (titleView != null) {
299 titleView.setText(title);
Adam Powell278902c2014-07-12 18:33:22 -0700300 }
Adam Powell11f59a02014-08-20 13:22:16 -0700301 setTitle(title);
Adam Powell24428412015-04-01 17:19:56 -0700302
303 // Try to initialize the title icon if we have a view for it and a title to match
304 final ImageView titleIcon = (ImageView) findViewById(R.id.title_icon);
305 if (titleIcon != null) {
Adam Powell24428412015-04-01 17:19:56 -0700306 ApplicationInfo ai = null;
307 try {
308 if (!TextUtils.isEmpty(referrerPackage)) {
309 ai = mPm.getApplicationInfo(referrerPackage, 0);
310 }
311 } catch (NameNotFoundException e) {
312 Log.e(TAG, "Could not find referrer package " + referrerPackage);
313 }
314
315 if (ai != null) {
316 titleIcon.setImageDrawable(ai.loadIcon(mPm));
317 }
318 }
Adam Powelle9414d92014-07-05 17:44:18 -0700319 }
Adam Powell2d809622012-03-22 15:24:43 -0700320
Adam Powell278902c2014-07-12 18:33:22 -0700321 final ImageView iconView = (ImageView) findViewById(R.id.icon);
322 final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem();
323 if (iconView != null && iconInfo != null) {
Adam Powell24428412015-04-01 17:19:56 -0700324 new LoadIconIntoViewTask(iconInfo, iconView).execute();
Adam Powell278902c2014-07-12 18:33:22 -0700325 }
326
327 if (alwaysUseOption || mAdapter.hasFilteredItem()) {
Adam Powellc5878612012-05-04 18:42:38 -0700328 final ViewGroup buttonLayout = (ViewGroup) findViewById(R.id.button_bar);
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700329 if (buttonLayout != null) {
330 buttonLayout.setVisibility(View.VISIBLE);
331 mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
332 mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
333 } else {
334 mAlwaysUseOption = false;
335 }
Adam Powell278902c2014-07-12 18:33:22 -0700336 }
337
338 if (mAdapter.hasFilteredItem()) {
339 setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false);
340 mOnceButton.setEnabled(true);
341 }
Adam Powell88831a22014-11-20 18:17:00 -0800342
343 mProfileView = findViewById(R.id.profile_button);
344 if (mProfileView != null) {
345 mProfileView.setOnClickListener(new View.OnClickListener() {
346 @Override
347 public void onClick(View v) {
348 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
349 if (dri == null) {
350 return;
351 }
352
Sander Alewijnse053d3dd2015-03-09 15:31:10 +0000353 // Do not show the profile switch message anymore.
354 mProfileSwitchMessageId = -1;
355
Adam Powell24428412015-04-01 17:19:56 -0700356 onTargetSelected(dri, false);
Adam Powell88831a22014-11-20 18:17:00 -0800357 finish();
358 }
359 });
360 bindProfileView();
361 }
Adam Powell4c470d62015-06-19 17:46:17 -0700362
363 if (isVoiceInteraction()) {
364 onSetupVoiceInteraction();
365 }
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100366 final Set<String> categories = intent.getCategories();
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100367 MetricsLogger.action(this, mAdapter.hasFilteredItem()
368 ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
369 : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
370 intent.getAction() + ":" + intent.getType() + ":"
Clara Bayarri9550f5d2016-05-11 11:20:13 +0100371 + (categories != null ? Arrays.toString(categories.toArray()) : ""));
Adam Powell4c470d62015-06-19 17:46:17 -0700372 }
373
Adam Powell52c39212016-04-07 15:14:18 -0700374 public final void setFilteredComponents(ComponentName[] components) {
375 mFilteredComponents = components;
376 }
377
378 public final boolean isComponentFiltered(ComponentInfo component) {
379 if (mFilteredComponents == null) {
380 return false;
381 }
382
383 final ComponentName checkName = component.getComponentName();
384 for (ComponentName name : mFilteredComponents) {
385 if (name.equals(checkName)) {
386 return true;
387 }
388 }
389 return false;
390 }
391
Adam Powell4c470d62015-06-19 17:46:17 -0700392 /**
393 * Perform any initialization needed for voice interaction.
394 */
Adam Powell23882512016-01-29 10:21:00 -0800395 public void onSetupVoiceInteraction() {
Adam Powell4c470d62015-06-19 17:46:17 -0700396 // Do it right now. Subclasses may delay this and send it later.
397 sendVoiceChoicesIfNeeded();
398 }
399
Adam Powell23882512016-01-29 10:21:00 -0800400 public void sendVoiceChoicesIfNeeded() {
Adam Powell4c470d62015-06-19 17:46:17 -0700401 if (!isVoiceInteraction()) {
402 // Clearly not needed.
403 return;
404 }
405
406
407 final Option[] options = new Option[mAdapter.getCount()];
408 for (int i = 0, N = options.length; i < N; i++) {
409 options[i] = optionForChooserTarget(mAdapter.getItem(i), i);
410 }
411
412 mPickOptionRequest = new PickTargetOptionRequest(
413 new Prompt(getTitle()), options, null);
414 getVoiceInteractor().submitRequest(mPickOptionRequest);
415 }
416
417 Option optionForChooserTarget(TargetInfo target, int index) {
418 return new Option(target.getDisplayLabel(), index);
Adam Powell88831a22014-11-20 18:17:00 -0800419 }
420
Adam Powell2ed547e2015-04-29 18:45:04 -0700421 protected final void setAdditionalTargets(Intent[] intents) {
422 if (intents != null) {
423 for (Intent intent : intents) {
424 mIntents.add(intent);
425 }
426 }
427 }
428
Adam Powell0ccc0e92015-04-23 17:19:37 -0700429 public Intent getTargetIntent() {
Adam Powell2ed547e2015-04-29 18:45:04 -0700430 return mIntents.isEmpty() ? null : mIntents.get(0);
Adam Powell0ccc0e92015-04-23 17:19:37 -0700431 }
432
Adam Powell24428412015-04-01 17:19:56 -0700433 private String getReferrerPackageName() {
434 final Uri referrer = getReferrer();
435 if (referrer != null && "android-app".equals(referrer.getScheme())) {
436 return referrer.getHost();
437 }
438 return null;
439 }
440
Adam Powell23882512016-01-29 10:21:00 -0800441 public int getLayoutResource() {
Adam Powell24428412015-04-01 17:19:56 -0700442 return R.layout.resolver_list;
443 }
444
Adam Powell88831a22014-11-20 18:17:00 -0800445 void bindProfileView() {
446 final DisplayResolveInfo dri = mAdapter.getOtherProfile();
447 if (dri != null) {
448 mProfileView.setVisibility(View.VISIBLE);
Nicolas Prevot256423f2016-02-09 16:33:57 +0000449 final TextView text = (TextView) mProfileView.findViewById(R.id.profile_button);
Adam Powell24428412015-04-01 17:19:56 -0700450 text.setText(dri.getDisplayLabel());
Adam Powell88831a22014-11-20 18:17:00 -0800451 } else {
452 mProfileView.setVisibility(View.GONE);
453 }
Adam Powell278902c2014-07-12 18:33:22 -0700454 }
455
Sander Alewijnsef6545332014-10-31 12:39:02 +0000456 private void setProfileSwitchMessageId(int contentUserHint) {
457 if (contentUserHint != UserHandle.USER_CURRENT &&
458 contentUserHint != UserHandle.myUserId()) {
459 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
460 UserInfo originUserInfo = userManager.getUserInfo(contentUserHint);
461 boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile()
462 : false;
463 boolean targetIsManaged = userManager.isManagedProfile();
464 if (originIsManaged && !targetIsManaged) {
465 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner;
466 } else if (!originIsManaged && targetIsManaged) {
467 mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work;
468 }
469 }
470 }
471
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700472 /**
473 * Turn on launch mode that is safe to use when forwarding intents received from
474 * applications and running in system processes. This mode uses Activity.startActivityAsCaller
475 * instead of the normal Activity.startActivity for launching the activity selected
476 * by the user.
477 *
478 * <p>This mode is set to true by default if the activity is initialized through
479 * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate
480 * methods, it is set to false by default. You must set it before calling one of the
481 * more detailed onCreate methods, so that it will be set correctly in the case where
482 * there is only one intent to resolve and it is thus started immediately.</p>
483 */
484 public void setSafeForwardingMode(boolean safeForwarding) {
485 mSafeForwardingMode = safeForwarding;
486 }
487
Adam Powell278902c2014-07-12 18:33:22 -0700488 protected CharSequence getTitleForAction(String action, int defaultTitleRes) {
Adam Powella35c77a2014-09-25 16:46:36 -0700489 final ActionTitle title = mResolvingHome ? ActionTitle.HOME : ActionTitle.forAction(action);
Adam Powell278902c2014-07-12 18:33:22 -0700490 final boolean named = mAdapter.hasFilteredItem();
491 if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) {
492 return getString(defaultTitleRes);
493 } else {
Adam Powell24428412015-04-01 17:19:56 -0700494 return named
495 ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel())
496 : getString(title.titleRes);
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700497 }
Adam Powellc5878612012-05-04 18:42:38 -0700498 }
499
Adam Powelle9414d92014-07-05 17:44:18 -0700500 void dismiss() {
501 if (!isFinishing()) {
502 finish();
503 }
504 }
505
Adam Powellc5878612012-05-04 18:42:38 -0700506 Drawable getIcon(Resources res, int resId) {
507 Drawable result;
508 try {
509 result = res.getDrawableForDensity(resId, mIconDpi);
510 } catch (Resources.NotFoundException e) {
511 result = null;
512 }
513
514 return result;
515 }
516
517 Drawable loadIconForResolveInfo(ResolveInfo ri) {
518 Drawable dr;
519 try {
520 if (ri.resolvePackageName != null && ri.icon != 0) {
521 dr = getIcon(mPm.getResourcesForApplication(ri.resolvePackageName), ri.icon);
522 if (dr != null) {
523 return dr;
524 }
525 }
526 final int iconRes = ri.getIconResource();
527 if (iconRes != 0) {
528 dr = getIcon(mPm.getResourcesForApplication(ri.activityInfo.packageName), iconRes);
529 if (dr != null) {
530 return dr;
531 }
532 }
533 } catch (NameNotFoundException e) {
534 Log.e(TAG, "Couldn't find resources for package", e);
535 }
536 return ri.loadIcon(mPm);
537 }
538
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800539 @Override
540 protected void onRestart() {
541 super.onRestart();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700542 if (!mRegistered) {
543 mPackageMonitor.register(this, getMainLooper(), false);
544 mRegistered = true;
545 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800546 mAdapter.handlePackagesChanged();
Adam Powell88831a22014-11-20 18:17:00 -0800547 if (mProfileView != null) {
548 bindProfileView();
549 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800550 }
551
552 @Override
553 protected void onStop() {
554 super.onStop();
Dianne Hackbornd44713a2012-04-30 16:34:46 -0700555 if (mRegistered) {
556 mPackageMonitor.unregister();
557 mRegistered = false;
558 }
Wale Ogunwale9014e662016-03-19 14:55:46 -0700559 final Intent intent = getIntent();
560 if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction()
561 && !mResolvingHome) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -0700562 // This resolver is in the unusual situation where it has been
563 // launched at the top of a new task. We don't let it be added
564 // to the recent tasks shown to the user, and we need to make sure
565 // that each time we are launched we get the correct launching
566 // uid (not re-using the same resolver from an old launching uid),
567 // so we will now finish ourself since being no longer visible,
568 // the user probably can't get back to us.
569 if (!isChangingConfigurations()) {
570 finish();
571 }
572 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -0800573 }
574
Adam Powellc5878612012-05-04 18:42:38 -0700575 @Override
Adam Powell4c470d62015-06-19 17:46:17 -0700576 protected void onDestroy() {
577 super.onDestroy();
578 if (!isChangingConfigurations() && mPickOptionRequest != null) {
579 mPickOptionRequest.cancel();
580 }
581 }
582
583 @Override
Adam Powell9bee4662012-05-08 11:07:23 -0700584 protected void onRestoreInstanceState(Bundle savedInstanceState) {
585 super.onRestoreInstanceState(savedInstanceState);
586 if (mAlwaysUseOption) {
Adam Powell24428412015-04-01 17:19:56 -0700587 final int checkedPos = mAdapterView.getCheckedItemPosition();
Nicolas Prevot50449882014-06-23 12:42:37 +0100588 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
Adam Powelld81cc4a2012-07-19 13:51:39 -0700589 mLastSelected = checkedPos;
Adam Powell278902c2014-07-12 18:33:22 -0700590 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
Nicolas Prevot50449882014-06-23 12:42:37 +0100591 mOnceButton.setEnabled(hasValidSelection);
592 if (hasValidSelection) {
Adam Powell24428412015-04-01 17:19:56 -0700593 mAdapterView.setSelection(checkedPos);
Adam Powell9bee4662012-05-08 11:07:23 -0700594 }
595 }
596 }
597
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100598 private boolean hasManagedProfile() {
599 UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
600 if (userManager == null) {
601 return false;
602 }
603
604 try {
605 List<UserInfo> profiles = userManager.getProfiles(getUserId());
606 for (UserInfo userInfo : profiles) {
607 if (userInfo != null && userInfo.isManagedProfile()) {
608 return true;
609 }
610 }
611 } catch (SecurityException e) {
612 return false;
613 }
614 return false;
615 }
616
617 private boolean supportsManagedProfiles(ResolveInfo resolveInfo) {
618 try {
619 ApplicationInfo appInfo = getPackageManager().getApplicationInfo(
620 resolveInfo.activityInfo.packageName, 0 /* default flags */);
Adam Powell4c470d62015-06-19 17:46:17 -0700621 return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP;
Sander Alewijnse6c9eee82014-07-17 10:03:31 +0100622 } catch (NameNotFoundException e) {
623 return false;
624 }
625 }
626
Adam Powell278902c2014-07-12 18:33:22 -0700627 private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos,
628 boolean filtered) {
Nicolas Prevot50449882014-06-23 12:42:37 +0100629 boolean enabled = false;
630 if (hasValidSelection) {
Adam Powell278902c2014-07-12 18:33:22 -0700631 ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered);
Nicolas Prevot50449882014-06-23 12:42:37 +0100632 if (ri.targetUserId == UserHandle.USER_CURRENT) {
633 enabled = true;
634 }
635 }
636 mAlwaysButton.setEnabled(enabled);
637 }
638
Adam Powellc5878612012-05-04 18:42:38 -0700639 public void onButtonClick(View v) {
640 final int id = v.getId();
Adam Powell278902c2014-07-12 18:33:22 -0700641 startSelected(mAlwaysUseOption ?
Adam Powell24428412015-04-01 17:19:56 -0700642 mAdapterView.getCheckedItemPosition() : mAdapter.getFilteredPosition(),
Adam Powell278902c2014-07-12 18:33:22 -0700643 id == R.id.button_always,
644 mAlwaysUseOption);
Adam Powellc5878612012-05-04 18:42:38 -0700645 }
646
Adam Powell23882512016-01-29 10:21:00 -0800647 public void startSelected(int which, boolean always, boolean filtered) {
Amith Yamasani07cd3512013-09-18 13:16:00 -0700648 if (isFinishing()) {
649 return;
650 }
Adam Powell278902c2014-07-12 18:33:22 -0700651 ResolveInfo ri = mAdapter.resolveInfoForPosition(which, filtered);
Sander Alewijnse86d35ba2015-02-04 15:14:53 +0000652 if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) {
653 Toast.makeText(this, String.format(getResources().getString(
654 com.android.internal.R.string.activity_resolver_work_profiles_support),
655 ri.activityInfo.loadLabel(getPackageManager()).toString()),
656 Toast.LENGTH_LONG).show();
657 return;
658 }
659
Adam Powell24428412015-04-01 17:19:56 -0700660 TargetInfo target = mAdapter.targetInfoForPosition(which, filtered);
Adam Powell2ed547e2015-04-29 18:45:04 -0700661 if (onTargetSelected(target, always)) {
Clara Bayarrifa902aa2016-04-13 14:45:08 +0100662 if (always && filtered) {
663 MetricsLogger.action(
664 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
665 } else if (filtered) {
666 MetricsLogger.action(
667 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
668 } else {
669 MetricsLogger.action(
670 this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
671 }
672 MetricsLogger.action(this, mAdapter.hasFilteredItem()
673 ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
674 : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
Adam Powell2ed547e2015-04-29 18:45:04 -0700675 finish();
676 }
Mike Lockwood02eb8742011-02-27 09:10:37 -0800677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678
Adam Powelle49d9392014-07-17 18:45:19 -0700679 /**
680 * Replace me in subclasses!
681 */
Nicolas Prevot0e2b73f2014-10-27 10:06:11 +0000682 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) {
Adam Powelle49d9392014-07-17 18:45:19 -0700683 return defIntent;
684 }
685
Adam Powell2ed547e2015-04-29 18:45:04 -0700686 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) {
Adam Powell24428412015-04-01 17:19:56 -0700687 final ResolveInfo ri = target.getResolveInfo();
688 final Intent intent = target != null ? target.getResolvedIntent() : null;
689
690 if (intent != null && (mAlwaysUseOption || mAdapter.hasFilteredItem())
691 && mAdapter.mOrigResolveList != null) {
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700692 // Build a reasonable intent filter, based on what matched.
693 IntentFilter filter = new IntentFilter();
Henrik Engström3277cf12014-07-17 12:18:29 +0200694 Intent filterIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695
Henrik Engström3277cf12014-07-17 12:18:29 +0200696 if (intent.getSelector() != null) {
697 filterIntent = intent.getSelector();
698 } else {
699 filterIntent = intent;
700 }
701
702 String action = filterIntent.getAction();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800703 if (action != null) {
704 filter.addAction(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
Henrik Engström3277cf12014-07-17 12:18:29 +0200706 Set<String> categories = filterIntent.getCategories();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700707 if (categories != null) {
708 for (String cat : categories) {
709 filter.addCategory(cat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700712 filter.addCategory(Intent.CATEGORY_DEFAULT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713
Adam Powell24428412015-04-01 17:19:56 -0700714 int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK;
Henrik Engström3277cf12014-07-17 12:18:29 +0200715 Uri data = filterIntent.getData();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700716 if (cat == IntentFilter.MATCH_CATEGORY_TYPE) {
Henrik Engström3277cf12014-07-17 12:18:29 +0200717 String mimeType = filterIntent.resolveType(this);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700718 if (mimeType != null) {
719 try {
720 filter.addDataType(mimeType);
721 } catch (IntentFilter.MalformedMimeTypeException e) {
722 Log.w("ResolverActivity", e);
723 filter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 }
725 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700726 }
727 if (data != null && data.getScheme() != null) {
728 // We need the data specification if there was no type,
729 // OR if the scheme is not one of our magical "file:"
730 // or "content:" schemes (see IntentFilter for the reason).
731 if (cat != IntentFilter.MATCH_CATEGORY_TYPE
732 || (!"file".equals(data.getScheme())
733 && !"content".equals(data.getScheme()))) {
734 filter.addDataScheme(data.getScheme());
735
736 // Look through the resolved filter to determine which part
737 // of it matched the original Intent.
738 Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator();
739 if (pIt != null) {
740 String ssp = data.getSchemeSpecificPart();
741 while (ssp != null && pIt.hasNext()) {
742 PatternMatcher p = pIt.next();
743 if (p.match(ssp)) {
744 filter.addDataSchemeSpecificPart(p.getPath(), p.getType());
745 break;
746 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -0700747 }
748 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700749 Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator();
750 if (aIt != null) {
751 while (aIt.hasNext()) {
752 IntentFilter.AuthorityEntry a = aIt.next();
753 if (a.match(data) >= 0) {
754 int port = a.getPort();
755 filter.addDataAuthority(a.getHost(),
756 port >= 0 ? Integer.toString(port) : null);
757 break;
758 }
759 }
760 }
761 pIt = ri.filter.pathsIterator();
762 if (pIt != null) {
763 String path = data.getPath();
764 while (path != null && pIt.hasNext()) {
765 PatternMatcher p = pIt.next();
766 if (p.match(path)) {
767 filter.addDataPath(p.getPath(), p.getType());
768 break;
769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771 }
772 }
773 }
774
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700775 if (filter != null) {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700776 final int N = mAdapter.mOrigResolveList.size();
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700777 ComponentName[] set = new ComponentName[N];
778 int bestMatch = 0;
779 for (int i=0; i<N; i++) {
Adam Powell2ed547e2015-04-29 18:45:04 -0700780 ResolveInfo r = mAdapter.mOrigResolveList.get(i).getResolveInfoAt(0);
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700781 set[i] = new ComponentName(r.activityInfo.packageName,
782 r.activityInfo.name);
783 if (r.match > bestMatch) bestMatch = r.match;
784 }
785 if (alwaysCheck) {
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700786 final int userId = getUserId();
787 final PackageManager pm = getPackageManager();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800788
789 // Set the preferred Activity
790 pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent());
791
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700792 if (ri.handleAllWebDataURI) {
793 // Set default Browser if needed
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700794 final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700795 if (TextUtils.isEmpty(packageName)) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700796 pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId);
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700797 }
798 } else {
799 // Update Domain Verification status
800 ComponentName cn = intent.getComponent();
801 String packageName = cn.getPackageName();
802 String dataScheme = (data != null) ? data.getScheme() : null;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800803
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700804 boolean isHttpOrHttps = (dataScheme != null) &&
805 (dataScheme.equals(IntentFilter.SCHEME_HTTP) ||
806 dataScheme.equals(IntentFilter.SCHEME_HTTPS));
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800807
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700808 boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW);
809 boolean hasCategoryBrowsable = (categories != null) &&
810 categories.contains(Intent.CATEGORY_BROWSABLE);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800811
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700812 if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700813 pm.updateIntentVerificationStatusAsUser(packageName,
Fabrice Di Meglio3453e082015-05-11 17:46:23 -0700814 PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS,
815 userId);
816 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800817 }
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700818 } else {
819 try {
820 AppGlobals.getPackageManager().setLastChosenActivity(intent,
Yuichi Biwa66e495e2015-08-06 19:45:27 +0900821 intent.resolveType(getContentResolver()),
Amith Yamasani588dd2a2013-09-03 12:30:44 -0700822 PackageManager.MATCH_DEFAULT_ONLY,
823 filter, bestMatch, intent.getComponent());
824 } catch (RemoteException re) {
825 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
826 }
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -0700827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 }
829 }
830
Adam Powell24428412015-04-01 17:19:56 -0700831 if (target != null) {
832 safelyStartActivity(target);
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700833 }
Adam Powell2ed547e2015-04-29 18:45:04 -0700834 return true;
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700835 }
836
Adam Powell23882512016-01-29 10:21:00 -0800837 public void safelyStartActivity(TargetInfo cti) {
Jeff Sharkey2b9eb892016-02-16 09:21:51 -0700838 // We're dispatching intents that might be coming from legacy apps, so
839 // don't kill ourselves.
840 StrictMode.disableDeathOnFileUriExposure();
841 try {
842 safelyStartActivityInternal(cti);
843 } finally {
844 StrictMode.enableDeathOnFileUriExposure();
845 }
846 }
847
848 private void safelyStartActivityInternal(TargetInfo cti) {
Sander Alewijnsef6545332014-10-31 12:39:02 +0000849 // If needed, show that intent is forwarded
850 // from managed profile to owner or other way around.
851 if (mProfileSwitchMessageId != -1) {
852 Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show();
853 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700854 if (!mSafeForwardingMode) {
Adam Powell24428412015-04-01 17:19:56 -0700855 if (cti.start(this, null)) {
856 onActivityStarted(cti);
857 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700858 return;
859 }
860 try {
Adam Powell24428412015-04-01 17:19:56 -0700861 if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) {
862 onActivityStarted(cti);
863 }
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700864 } catch (RuntimeException e) {
865 String launchedFromPackage;
866 try {
867 launchedFromPackage = ActivityManagerNative.getDefault().getLaunchedFromPackage(
868 getActivityToken());
869 } catch (RemoteException e2) {
870 launchedFromPackage = "??";
871 }
872 Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid
873 + " package " + launchedFromPackage + ", while running in "
874 + ActivityThread.currentProcessName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
877
Adam Powell23882512016-01-29 10:21:00 -0800878 public void onActivityStarted(TargetInfo cti) {
Adam Powell0b3c1122014-10-09 12:50:14 -0700879 // Do nothing
880 }
881
Adam Powell23882512016-01-29 10:21:00 -0800882 public boolean shouldGetActivityMetadata() {
Adam Powell24428412015-04-01 17:19:56 -0700883 return false;
884 }
885
Adam Powell23882512016-01-29 10:21:00 -0800886 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) {
Adam Powell39e94eb2015-09-08 17:01:49 -0700887 return true;
888 }
889
Adam Powell23882512016-01-29 10:21:00 -0800890 public void showTargetDetails(ResolveInfo ri) {
Dianne Hackborn028ceeb2014-08-17 17:45:48 -0700891 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
Adam Powell0fc5b2b2012-07-18 18:20:29 -0700892 .setData(Uri.fromParts("package", ri.activityInfo.packageName, null))
Adam Powell24428412015-04-01 17:19:56 -0700893 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Amith Yamasani203a2f42012-10-03 20:16:40 -0700894 startActivity(in);
Adam Powellc5878612012-05-04 18:42:38 -0700895 }
896
Adam Powell23882512016-01-29 10:21:00 -0800897 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents,
Adam Powell7d758002015-05-06 17:49:36 -0700898 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
899 boolean filterLastUsed) {
900 return new ResolveListAdapter(context, payloadIntents, initialIntents, rList,
901 launchedFromUid, filterLastUsed);
Adam Powell88831a22014-11-20 18:17:00 -0800902 }
903
Adam Powell39e94eb2015-09-08 17:01:49 -0700904 /**
905 * Returns true if the activity is finishing and creation should halt
906 */
Adam Powell23882512016-01-29 10:21:00 -0800907 public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents,
Adam Powell7d758002015-05-06 17:49:36 -0700908 List<ResolveInfo> rList, boolean alwaysUseOption) {
Dianne Hackborn57dd7372015-07-27 18:11:14 -0700909 // The last argument of createAdapter is whether to do special handling
910 // of the last used choice to highlight it in the list. We need to always
911 // turn this off when running under voice interaction, since it results in
912 // a more complicated UI that the current voice interaction flow is not able
913 // to handle.
Adam Powell7d758002015-05-06 17:49:36 -0700914 mAdapter = createAdapter(this, payloadIntents, initialIntents, rList,
Dianne Hackborn57dd7372015-07-27 18:11:14 -0700915 mLaunchedFromUid, alwaysUseOption && !isVoiceInteraction());
Adam Powell7d758002015-05-06 17:49:36 -0700916
917 final int layoutId;
918 if (mAdapter.hasFilteredItem()) {
919 layoutId = R.layout.resolver_list_with_default;
920 alwaysUseOption = false;
921 } else {
922 layoutId = getLayoutResource();
923 }
924 mAlwaysUseOption = alwaysUseOption;
925
Adam Powell50077352015-05-26 18:01:55 -0700926 int count = mAdapter.getUnfilteredCount();
Jeff Sharkeycc2ae6b42015-09-29 13:04:46 -0700927 if (count == 1 && mAdapter.getOtherProfile() == null) {
928 // Only one target, so we're a candidate to auto-launch!
929 final TargetInfo target = mAdapter.targetInfoForPosition(0, false);
930 if (shouldAutoLaunchSingleChoice(target)) {
931 safelyStartActivity(target);
932 mPackageMonitor.unregister();
933 mRegistered = false;
934 finish();
935 return true;
936 }
937 }
938 if (count > 0) {
Adam Powell7d758002015-05-06 17:49:36 -0700939 setContentView(layoutId);
940 mAdapterView = (AbsListView) findViewById(R.id.resolver_list);
941 onPrepareAdapterView(mAdapterView, mAdapter, alwaysUseOption);
Adam Powell7d758002015-05-06 17:49:36 -0700942 } else {
943 setContentView(R.layout.resolver_list);
944
945 final TextView empty = (TextView) findViewById(R.id.empty);
946 empty.setVisibility(View.VISIBLE);
947
948 mAdapterView = (AbsListView) findViewById(R.id.resolver_list);
949 mAdapterView.setVisibility(View.GONE);
950 }
Adam Powell39e94eb2015-09-08 17:01:49 -0700951 return false;
Adam Powell7d758002015-05-06 17:49:36 -0700952 }
953
Adam Powell23882512016-01-29 10:21:00 -0800954 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter,
Adam Powell7d758002015-05-06 17:49:36 -0700955 boolean alwaysUseOption) {
956 final boolean useHeader = adapter.hasFilteredItem();
957 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null;
958
959 adapterView.setAdapter(mAdapter);
960
961 final ItemClickListener listener = new ItemClickListener();
962 adapterView.setOnItemClickListener(listener);
963 adapterView.setOnItemLongClickListener(listener);
964
965 if (alwaysUseOption) {
966 listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
967 }
968
969 if (useHeader && listView != null) {
970 listView.addHeaderView(LayoutInflater.from(this).inflate(
971 R.layout.resolver_different_item_header, listView, false));
972 }
Adam Powell24428412015-04-01 17:19:56 -0700973 }
974
Adam Powellc412be62015-06-24 13:54:10 -0700975 /**
976 * Check a simple match for the component of two ResolveInfos.
977 */
978 static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) {
979 return lhs == null ? rhs == null
980 : lhs.activityInfo == null ? rhs.activityInfo == null
981 : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name)
982 && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName);
983 }
984
Adam Powell23882512016-01-29 10:21:00 -0800985 public final class DisplayResolveInfo implements TargetInfo {
Adam Powell24428412015-04-01 17:19:56 -0700986 private final ResolveInfo mResolveInfo;
987 private final CharSequence mDisplayLabel;
988 private Drawable mDisplayIcon;
Adam Powell00f4aad2015-09-17 13:38:16 -0700989 private Drawable mBadge;
Adam Powell24428412015-04-01 17:19:56 -0700990 private final CharSequence mExtendedInfo;
991 private final Intent mResolvedIntent;
Adam Powell2ed547e2015-04-29 18:45:04 -0700992 private final List<Intent> mSourceIntents = new ArrayList<>();
Adam Powell23882512016-01-29 10:21:00 -0800993 private boolean mPinned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994
Adam Powell23882512016-01-29 10:21:00 -0800995 public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel,
Dianne Hackborneb034652009-09-07 00:49:58 -0700996 CharSequence pInfo, Intent pOrigIntent) {
Adam Powell2ed547e2015-04-29 18:45:04 -0700997 mSourceIntents.add(originalIntent);
Adam Powell24428412015-04-01 17:19:56 -0700998 mResolveInfo = pri;
999 mDisplayLabel = pLabel;
1000 mExtendedInfo = pInfo;
1001
1002 final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent :
Adam Powell2ed547e2015-04-29 18:45:04 -07001003 getReplacementIntent(pri.activityInfo, getTargetIntent()));
Adam Powell24428412015-04-01 17:19:56 -07001004 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT
1005 | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
1006 final ActivityInfo ai = mResolveInfo.activityInfo;
1007 intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name));
1008
1009 mResolvedIntent = intent;
1010 }
1011
Adam Powell2ed547e2015-04-29 18:45:04 -07001012 private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) {
1013 mSourceIntents.addAll(other.getAllSourceIntents());
1014 mResolveInfo = other.mResolveInfo;
1015 mDisplayLabel = other.mDisplayLabel;
1016 mDisplayIcon = other.mDisplayIcon;
1017 mExtendedInfo = other.mExtendedInfo;
1018 mResolvedIntent = new Intent(other.mResolvedIntent);
1019 mResolvedIntent.fillIn(fillInIntent, flags);
Adam Powell23882512016-01-29 10:21:00 -08001020 mPinned = other.mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001021 }
1022
Adam Powell24428412015-04-01 17:19:56 -07001023 public ResolveInfo getResolveInfo() {
1024 return mResolveInfo;
1025 }
1026
1027 public CharSequence getDisplayLabel() {
1028 return mDisplayLabel;
1029 }
1030
1031 public Drawable getDisplayIcon() {
1032 return mDisplayIcon;
1033 }
1034
Adam Powell7d758002015-05-06 17:49:36 -07001035 public Drawable getBadgeIcon() {
Adam Powell00f4aad2015-09-17 13:38:16 -07001036 // We only expose a badge if we have extended info.
1037 // The badge is a higher-priority disambiguation signal
1038 // but we don't need one if we wouldn't show extended info at all.
1039 if (TextUtils.isEmpty(getExtendedInfo())) {
1040 return null;
1041 }
1042
1043 if (mBadge == null && mResolveInfo != null && mResolveInfo.activityInfo != null
1044 && mResolveInfo.activityInfo.applicationInfo != null) {
1045 if (mResolveInfo.activityInfo.icon == 0 || mResolveInfo.activityInfo.icon
1046 == mResolveInfo.activityInfo.applicationInfo.icon) {
1047 // Badging an icon with exactly the same icon is silly.
1048 // If the activityInfo icon resid is 0 it will fall back
1049 // to the application's icon, making it a match.
1050 return null;
1051 }
1052 mBadge = mResolveInfo.activityInfo.applicationInfo.loadIcon(mPm);
1053 }
1054 return mBadge;
Adam Powell7d758002015-05-06 17:49:36 -07001055 }
1056
Adam Powell2ed547e2015-04-29 18:45:04 -07001057 @Override
Alan Viverettece5d92c2015-07-31 16:46:56 -04001058 public CharSequence getBadgeContentDescription() {
1059 return null;
1060 }
1061
1062 @Override
Adam Powell2ed547e2015-04-29 18:45:04 -07001063 public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) {
1064 return new DisplayResolveInfo(this, fillInIntent, flags);
1065 }
1066
1067 @Override
1068 public List<Intent> getAllSourceIntents() {
1069 return mSourceIntents;
1070 }
1071
1072 public void addAlternateSourceIntent(Intent alt) {
1073 mSourceIntents.add(alt);
1074 }
1075
Adam Powell24428412015-04-01 17:19:56 -07001076 public void setDisplayIcon(Drawable icon) {
1077 mDisplayIcon = icon;
1078 }
1079
1080 public boolean hasDisplayIcon() {
1081 return mDisplayIcon != null;
1082 }
1083
1084 public CharSequence getExtendedInfo() {
1085 return mExtendedInfo;
1086 }
1087
1088 public Intent getResolvedIntent() {
1089 return mResolvedIntent;
1090 }
1091
1092 @Override
1093 public ComponentName getResolvedComponentName() {
1094 return new ComponentName(mResolveInfo.activityInfo.packageName,
1095 mResolveInfo.activityInfo.name);
1096 }
1097
1098 @Override
1099 public boolean start(Activity activity, Bundle options) {
1100 activity.startActivity(mResolvedIntent, options);
1101 return true;
1102 }
1103
1104 @Override
1105 public boolean startAsCaller(Activity activity, Bundle options, int userId) {
Dianne Hackborna7cfbe02015-07-16 10:52:52 -07001106 activity.startActivityAsCaller(mResolvedIntent, options, false, userId);
Adam Powell24428412015-04-01 17:19:56 -07001107 return true;
1108 }
1109
1110 @Override
1111 public boolean startAsUser(Activity activity, Bundle options, UserHandle user) {
1112 activity.startActivityAsUser(mResolvedIntent, options, user);
1113 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
Adam Powell23882512016-01-29 10:21:00 -08001115
1116 @Override
1117 public boolean isPinned() {
1118 return mPinned;
1119 }
1120
1121 public void setPinned(boolean pinned) {
1122 mPinned = pinned;
1123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
1125
Adam Powell24428412015-04-01 17:19:56 -07001126 /**
1127 * A single target as represented in the chooser.
1128 */
1129 public interface TargetInfo {
1130 /**
1131 * Get the resolved intent that represents this target. Note that this may not be the
1132 * intent that will be launched by calling one of the <code>start</code> methods provided;
1133 * this is the intent that will be credited with the launch.
1134 *
1135 * @return the resolved intent for this target
1136 */
Adam Powell23882512016-01-29 10:21:00 -08001137 Intent getResolvedIntent();
Adam Powell24428412015-04-01 17:19:56 -07001138
1139 /**
1140 * Get the resolved component name that represents this target. Note that this may not
1141 * be the component that will be directly launched by calling one of the <code>start</code>
1142 * methods provided; this is the component that will be credited with the launch.
1143 *
1144 * @return the resolved ComponentName for this target
1145 */
Adam Powell23882512016-01-29 10:21:00 -08001146 ComponentName getResolvedComponentName();
Adam Powell24428412015-04-01 17:19:56 -07001147
1148 /**
1149 * Start the activity referenced by this target.
1150 *
1151 * @param activity calling Activity performing the launch
1152 * @param options ActivityOptions bundle
1153 * @return true if the start completed successfully
1154 */
Adam Powell23882512016-01-29 10:21:00 -08001155 boolean start(Activity activity, Bundle options);
Adam Powell24428412015-04-01 17:19:56 -07001156
1157 /**
1158 * Start the activity referenced by this target as if the ResolverActivity's caller
1159 * was performing the start operation.
1160 *
1161 * @param activity calling Activity (actually) performing the launch
1162 * @param options ActivityOptions bundle
1163 * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller
1164 * @return true if the start completed successfully
1165 */
Adam Powell23882512016-01-29 10:21:00 -08001166 boolean startAsCaller(Activity activity, Bundle options, int userId);
Adam Powell24428412015-04-01 17:19:56 -07001167
1168 /**
1169 * Start the activity referenced by this target as a given user.
1170 *
1171 * @param activity calling activity performing the launch
1172 * @param options ActivityOptions bundle
1173 * @param user handle for the user to start the activity as
1174 * @return true if the start completed successfully
1175 */
Adam Powell23882512016-01-29 10:21:00 -08001176 boolean startAsUser(Activity activity, Bundle options, UserHandle user);
Adam Powell24428412015-04-01 17:19:56 -07001177
1178 /**
1179 * Return the ResolveInfo about how and why this target matched the original query
1180 * for available targets.
1181 *
1182 * @return ResolveInfo representing this target's match
1183 */
Adam Powell23882512016-01-29 10:21:00 -08001184 ResolveInfo getResolveInfo();
Adam Powell24428412015-04-01 17:19:56 -07001185
1186 /**
1187 * Return the human-readable text label for this target.
1188 *
1189 * @return user-visible target label
1190 */
Adam Powell23882512016-01-29 10:21:00 -08001191 CharSequence getDisplayLabel();
Adam Powell24428412015-04-01 17:19:56 -07001192
1193 /**
1194 * Return any extended info for this target. This may be used to disambiguate
1195 * otherwise identical targets.
1196 *
1197 * @return human-readable disambig string or null if none present
1198 */
Adam Powell23882512016-01-29 10:21:00 -08001199 CharSequence getExtendedInfo();
Adam Powell24428412015-04-01 17:19:56 -07001200
1201 /**
1202 * @return The drawable that should be used to represent this target
1203 */
Adam Powell23882512016-01-29 10:21:00 -08001204 Drawable getDisplayIcon();
Adam Powell2ed547e2015-04-29 18:45:04 -07001205
1206 /**
Adam Powell7d758002015-05-06 17:49:36 -07001207 * @return The (small) icon to badge the target with
1208 */
Adam Powell23882512016-01-29 10:21:00 -08001209 Drawable getBadgeIcon();
Adam Powell7d758002015-05-06 17:49:36 -07001210
1211 /**
Alan Viverettece5d92c2015-07-31 16:46:56 -04001212 * @return The content description for the badge icon
1213 */
Adam Powell23882512016-01-29 10:21:00 -08001214 CharSequence getBadgeContentDescription();
Alan Viverettece5d92c2015-07-31 16:46:56 -04001215
1216 /**
Adam Powell2ed547e2015-04-29 18:45:04 -07001217 * Clone this target with the given fill-in information.
1218 */
Adam Powell23882512016-01-29 10:21:00 -08001219 TargetInfo cloneFilledIn(Intent fillInIntent, int flags);
Adam Powell2ed547e2015-04-29 18:45:04 -07001220
1221 /**
1222 * @return the list of supported source intents deduped against this single target
1223 */
Adam Powell23882512016-01-29 10:21:00 -08001224 List<Intent> getAllSourceIntents();
1225
1226 /**
1227 * @return true if this target should be pinned to the front by the request of the user
1228 */
1229 boolean isPinned();
Adam Powell24428412015-04-01 17:19:56 -07001230 }
1231
Adam Powell23882512016-01-29 10:21:00 -08001232 public class ResolveListAdapter extends BaseAdapter {
Adam Powell7d758002015-05-06 17:49:36 -07001233 private final List<Intent> mIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001234 private final Intent[] mInitialIntents;
1235 private final List<ResolveInfo> mBaseResolveList;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001236 private ResolveInfo mLastChosen;
Adam Powell88831a22014-11-20 18:17:00 -08001237 private DisplayResolveInfo mOtherProfile;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001238 private final int mLaunchedFromUid;
Adam Powell24428412015-04-01 17:19:56 -07001239 private boolean mHasExtendedInfo;
1240
1241 protected final LayoutInflater mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242
Adam Powell2ed547e2015-04-29 18:45:04 -07001243 List<DisplayResolveInfo> mDisplayList;
1244 List<ResolvedComponentInfo> mOrigResolveList;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001245
Adam Powell278902c2014-07-12 18:33:22 -07001246 private int mLastChosenPosition = -1;
1247 private boolean mFilterLastUsed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248
Adam Powell7d758002015-05-06 17:49:36 -07001249 public ResolveListAdapter(Context context, List<Intent> payloadIntents,
1250 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid,
1251 boolean filterLastUsed) {
1252 mIntents = payloadIntents;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001253 mInitialIntents = initialIntents;
1254 mBaseResolveList = rList;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001255 mLaunchedFromUid = launchedFromUid;
Adam Powelle9414d92014-07-05 17:44:18 -07001256 mInflater = LayoutInflater.from(context);
Adam Powell2ed547e2015-04-29 18:45:04 -07001257 mDisplayList = new ArrayList<>();
Adam Powell278902c2014-07-12 18:33:22 -07001258 mFilterLastUsed = filterLastUsed;
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001259 rebuildList();
1260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001262 public void handlePackagesChanged() {
1263 rebuildList();
1264 notifyDataSetChanged();
Esteban Talavera6de72662014-12-11 17:54:07 +00001265 if (getCount() == 0) {
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001266 // We no longer have any items... just finish the activity.
1267 finish();
Adam Powellc5878612012-05-04 18:42:38 -07001268 }
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001269 }
1270
Adam Powell278902c2014-07-12 18:33:22 -07001271 public DisplayResolveInfo getFilteredItem() {
1272 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1273 // Not using getItem since it offsets to dodge this position for the list
Adam Powell2ed547e2015-04-29 18:45:04 -07001274 return mDisplayList.get(mLastChosenPosition);
Adam Powell278902c2014-07-12 18:33:22 -07001275 }
1276 return null;
1277 }
1278
Adam Powell88831a22014-11-20 18:17:00 -08001279 public DisplayResolveInfo getOtherProfile() {
1280 return mOtherProfile;
1281 }
1282
Adam Powell278902c2014-07-12 18:33:22 -07001283 public int getFilteredPosition() {
1284 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1285 return mLastChosenPosition;
1286 }
1287 return AbsListView.INVALID_POSITION;
1288 }
1289
1290 public boolean hasFilteredItem() {
1291 return mFilterLastUsed && mLastChosenPosition >= 0;
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001292 }
1293
Adam Powella182e452015-07-06 16:57:56 -07001294 public float getScore(DisplayResolveInfo target) {
1295 return mResolverComparator.getScore(target.getResolvedComponentName());
1296 }
1297
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001298 private void rebuildList() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001299 List<ResolvedComponentInfo> currentResolveList = null;
You Kim43a5070e2012-11-21 23:23:45 +09001300
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001301 try {
Adam Powell2ed547e2015-04-29 18:45:04 -07001302 final Intent primaryIntent = getTargetIntent();
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001303 mLastChosen = AppGlobals.getPackageManager().getLastChosenActivity(
Adam Powell2ed547e2015-04-29 18:45:04 -07001304 primaryIntent, primaryIntent.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasanie9ecc8b2013-08-22 11:16:27 -07001305 PackageManager.MATCH_DEFAULT_ONLY);
1306 } catch (RemoteException re) {
1307 Log.d(TAG, "Error calling setLastChosenActivity\n" + re);
1308 }
1309
Sudheer Shanka7e64e102015-01-23 10:37:45 +00001310 // Clear the value of mOtherProfile from previous call.
1311 mOtherProfile = null;
Adam Powell2ed547e2015-04-29 18:45:04 -07001312 mDisplayList.clear();
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001313 if (mBaseResolveList != null) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001314 currentResolveList = mOrigResolveList = new ArrayList<>();
1315 addResolveListDedupe(currentResolveList, getTargetIntent(), mBaseResolveList);
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001316 } else {
Adam Powell2ed547e2015-04-29 18:45:04 -07001317 final boolean shouldGetResolvedFilter = shouldGetResolvedFilter();
1318 final boolean shouldGetActivityMetadata = shouldGetActivityMetadata();
1319 for (int i = 0, N = mIntents.size(); i < N; i++) {
1320 final Intent intent = mIntents.get(i);
1321 final List<ResolveInfo> infos = mPm.queryIntentActivities(intent,
1322 PackageManager.MATCH_DEFAULT_ONLY
1323 | (shouldGetResolvedFilter ? PackageManager.GET_RESOLVED_FILTER : 0)
1324 | (shouldGetActivityMetadata ? PackageManager.GET_META_DATA : 0));
1325 if (infos != null) {
1326 if (currentResolveList == null) {
1327 currentResolveList = mOrigResolveList = new ArrayList<>();
1328 }
1329 addResolveListDedupe(currentResolveList, intent, infos);
1330 }
1331 }
1332
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001333 // Filter out any activities that the launched uid does not
Nicolas Prevot697397a2016-01-11 11:14:32 +00001334 // have permission for.
1335 // Also filter out those that are suspended because they couldn't
1336 // be started. We don't do this when we have an explicit
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001337 // list of resolved activities, because that only happens when
1338 // we are being subclassed, so we can safely launch whatever
1339 // they gave us.
You Kim43a5070e2012-11-21 23:23:45 +09001340 if (currentResolveList != null) {
1341 for (int i=currentResolveList.size()-1; i >= 0; i--) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001342 ActivityInfo ai = currentResolveList.get(i)
1343 .getResolveInfoAt(0).activityInfo;
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001344 int granted = ActivityManager.checkComponentPermission(
1345 ai.permission, mLaunchedFromUid,
1346 ai.applicationInfo.uid, ai.exported);
Nicolas Prevot697397a2016-01-11 11:14:32 +00001347 boolean suspended = (ai.applicationInfo.flags
1348 & ApplicationInfo.FLAG_SUSPENDED) != 0;
Adam Powell52c39212016-04-07 15:14:18 -07001349 if (granted != PackageManager.PERMISSION_GRANTED || suspended
1350 || isComponentFiltered(ai)) {
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001351 // Access not allowed!
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001352 if (mOrigResolveList == currentResolveList) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001353 mOrigResolveList = new ArrayList<>(mOrigResolveList);
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001354 }
You Kim43a5070e2012-11-21 23:23:45 +09001355 currentResolveList.remove(i);
Dianne Hackborn5320eb82012-05-18 12:05:04 -07001356 }
1357 }
1358 }
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 int N;
You Kim43a5070e2012-11-21 23:23:45 +09001361 if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 // Only display the first matches that are either of equal
1363 // priority or have asked to be default options.
Adam Powell2ed547e2015-04-29 18:45:04 -07001364 ResolvedComponentInfo rci0 = currentResolveList.get(0);
1365 ResolveInfo r0 = rci0.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 for (int i=1; i<N; i++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001367 ResolveInfo ri = currentResolveList.get(i).getResolveInfoAt(0);
You Kim43a5070e2012-11-21 23:23:45 +09001368 if (DEBUG) Log.v(
Adam Powell09a65602014-07-20 16:23:14 -07001369 TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 r0.activityInfo.name + "=" +
1371 r0.priority + "/" + r0.isDefault + " vs " +
1372 ri.activityInfo.name + "=" +
1373 ri.priority + "/" + ri.isDefault);
You Kim43a5070e2012-11-21 23:23:45 +09001374 if (r0.priority != ri.priority ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 r0.isDefault != ri.isDefault) {
1376 while (i < N) {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001377 if (mOrigResolveList == currentResolveList) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001378 mOrigResolveList = new ArrayList<>(mOrigResolveList);
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07001379 }
You Kim43a5070e2012-11-21 23:23:45 +09001380 currentResolveList.remove(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 N--;
1382 }
1383 }
1384 }
1385 if (N > 1) {
Adam Powelld25267c2015-06-05 18:02:21 -07001386 mResolverComparator.compute(currentResolveList);
1387 Collections.sort(currentResolveList, mResolverComparator);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
Dianne Hackborneb034652009-09-07 00:49:58 -07001389 // First put the initial items at the top.
Dianne Hackborne8f2c7f2012-03-01 17:30:53 -08001390 if (mInitialIntents != null) {
1391 for (int i=0; i<mInitialIntents.length; i++) {
1392 Intent ii = mInitialIntents[i];
Dianne Hackborneb034652009-09-07 00:49:58 -07001393 if (ii == null) {
1394 continue;
1395 }
1396 ActivityInfo ai = ii.resolveActivityInfo(
1397 getPackageManager(), 0);
1398 if (ai == null) {
Adam Powell09a65602014-07-20 16:23:14 -07001399 Log.w(TAG, "No activity found for " + ii);
Dianne Hackborneb034652009-09-07 00:49:58 -07001400 continue;
1401 }
1402 ResolveInfo ri = new ResolveInfo();
1403 ri.activityInfo = ai;
Nicolas Prevot1a815922014-10-10 16:22:38 +01001404 UserManager userManager =
1405 (UserManager) getSystemService(Context.USER_SERVICE);
Dianne Hackborneb034652009-09-07 00:49:58 -07001406 if (ii instanceof LabeledIntent) {
1407 LabeledIntent li = (LabeledIntent)ii;
1408 ri.resolvePackageName = li.getSourcePackage();
1409 ri.labelRes = li.getLabelResource();
1410 ri.nonLocalizedLabel = li.getNonLocalizedLabel();
1411 ri.icon = li.getIconResource();
Sudheer Shanka9ded7602015-05-19 21:17:25 +01001412 ri.iconResourceId = ri.icon;
1413 }
1414 if (userManager.isManagedProfile()) {
1415 ri.noResourceId = true;
1416 ri.icon = 0;
Dianne Hackborneb034652009-09-07 00:49:58 -07001417 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001418 addResolveInfo(new DisplayResolveInfo(ii, ri,
Dianne Hackborneb034652009-09-07 00:49:58 -07001419 ri.loadLabel(getPackageManager()), null, ii));
1420 }
1421 }
You Kim43a5070e2012-11-21 23:23:45 +09001422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 // Check for applications with same name and use application name or
1424 // package name if necessary
Adam Powell2ed547e2015-04-29 18:45:04 -07001425 rci0 = currentResolveList.get(0);
1426 r0 = rci0.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 int start = 0;
1428 CharSequence r0Label = r0.loadLabel(mPm);
Adam Powell24428412015-04-01 17:19:56 -07001429 mHasExtendedInfo = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 for (int i = 1; i < N; i++) {
1431 if (r0Label == null) {
1432 r0Label = r0.activityInfo.packageName;
1433 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001434 ResolvedComponentInfo rci = currentResolveList.get(i);
1435 ResolveInfo ri = rci.getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 CharSequence riLabel = ri.loadLabel(mPm);
1437 if (riLabel == null) {
1438 riLabel = ri.activityInfo.packageName;
1439 }
1440 if (riLabel.equals(r0Label)) {
1441 continue;
1442 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001443 processGroup(currentResolveList, start, (i-1), rci0, r0Label);
1444 rci0 = rci;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 r0 = ri;
1446 r0Label = riLabel;
1447 start = i;
1448 }
1449 // Process last group
Adam Powell2ed547e2015-04-29 18:45:04 -07001450 processGroup(currentResolveList, start, (N-1), rci0, r0Label);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
Kenny Guyb8a2df12014-12-12 16:56:08 +00001452
1453 // Layout doesn't handle both profile button and last chosen
1454 // so disable last chosen if profile button is present.
1455 if (mOtherProfile != null && mLastChosenPosition >= 0) {
1456 mLastChosenPosition = -1;
1457 mFilterLastUsed = false;
1458 }
Adam Powell24428412015-04-01 17:19:56 -07001459
1460 onListRebuilt();
1461 }
1462
Adam Powell2ed547e2015-04-29 18:45:04 -07001463 private void addResolveListDedupe(List<ResolvedComponentInfo> into, Intent intent,
1464 List<ResolveInfo> from) {
1465 final int fromCount = from.size();
1466 final int intoCount = into.size();
1467 for (int i = 0; i < fromCount; i++) {
1468 final ResolveInfo newInfo = from.get(i);
1469 boolean found = false;
1470 // Only loop to the end of into as it was before we started; no dupes in from.
1471 for (int j = 0; j < intoCount; j++) {
Adam Powell00e58702016-09-09 10:26:35 -07001472 final ResolvedComponentInfo rci = into.get(j);
Adam Powell2ed547e2015-04-29 18:45:04 -07001473 if (isSameResolvedComponent(newInfo, rci)) {
1474 found = true;
1475 rci.add(intent, newInfo);
1476 break;
1477 }
1478 }
1479 if (!found) {
Adam Powell23882512016-01-29 10:21:00 -08001480 final ComponentName name = new ComponentName(
1481 newInfo.activityInfo.packageName, newInfo.activityInfo.name);
1482 final ResolvedComponentInfo rci = new ResolvedComponentInfo(name,
1483 intent, newInfo);
1484 rci.setPinned(isComponentPinned(name));
1485 into.add(rci);
Adam Powell2ed547e2015-04-29 18:45:04 -07001486 }
1487 }
1488 }
1489
1490 private boolean isSameResolvedComponent(ResolveInfo a, ResolvedComponentInfo b) {
1491 final ActivityInfo ai = a.activityInfo;
1492 return ai.packageName.equals(b.name.getPackageName())
1493 && ai.name.equals(b.name.getClassName());
1494 }
1495
Adam Powell24428412015-04-01 17:19:56 -07001496 public void onListRebuilt() {
1497 // This space for rent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
1499
Adam Powellc6d5e3a2015-04-23 12:22:20 -07001500 public boolean shouldGetResolvedFilter() {
1501 return mFilterLastUsed;
1502 }
1503
Adam Powell2ed547e2015-04-29 18:45:04 -07001504 private void processGroup(List<ResolvedComponentInfo> rList, int start, int end,
1505 ResolvedComponentInfo ro, CharSequence roLabel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 // Process labels from start to i
1507 int num = end - start+1;
1508 if (num == 1) {
1509 // No duplicate labels. Use label for entry at start
Adam Powell2ed547e2015-04-29 18:45:04 -07001510 addResolveInfoWithAlternates(ro, null, roLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 } else {
Adam Powell24428412015-04-01 17:19:56 -07001512 mHasExtendedInfo = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 boolean usePkg = false;
Adam Powell00f4aad2015-09-17 13:38:16 -07001514 final ApplicationInfo ai = ro.getResolveInfoAt(0).activityInfo.applicationInfo;
1515 final CharSequence startApp = ai.loadLabel(mPm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 if (startApp == null) {
1517 usePkg = true;
1518 }
1519 if (!usePkg) {
1520 // Use HashSet to track duplicates
1521 HashSet<CharSequence> duplicates =
1522 new HashSet<CharSequence>();
1523 duplicates.add(startApp);
1524 for (int j = start+1; j <= end ; j++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001525 ResolveInfo jRi = rList.get(j).getResolveInfoAt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm);
1527 if ( (jApp == null) || (duplicates.contains(jApp))) {
1528 usePkg = true;
1529 break;
1530 } else {
1531 duplicates.add(jApp);
1532 }
1533 }
1534 // Clear HashSet for later use
1535 duplicates.clear();
1536 }
1537 for (int k = start; k <= end; k++) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001538 final ResolvedComponentInfo rci = rList.get(k);
1539 final ResolveInfo add = rci.getResolveInfoAt(0);
1540 final CharSequence extraInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 if (usePkg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 // Use package name for all entries from start to end-1
Adam Powell2ed547e2015-04-29 18:45:04 -07001543 extraInfo = add.activityInfo.packageName;
1544 } else {
1545 // Use application name for all entries from start to end-1
1546 extraInfo = add.activityInfo.applicationInfo.loadLabel(mPm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001548 addResolveInfoWithAlternates(rci, extraInfo, roLabel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
1550 }
1551 }
1552
Adam Powell2ed547e2015-04-29 18:45:04 -07001553 private void addResolveInfoWithAlternates(ResolvedComponentInfo rci,
1554 CharSequence extraInfo, CharSequence roLabel) {
1555 final int count = rci.getCount();
1556 final Intent intent = rci.getIntentAt(0);
1557 final ResolveInfo add = rci.getResolveInfoAt(0);
1558 final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent);
1559 final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel,
1560 extraInfo, replaceIntent);
Adam Powell23882512016-01-29 10:21:00 -08001561 dri.setPinned(rci.isPinned());
Adam Powell2ed547e2015-04-29 18:45:04 -07001562 addResolveInfo(dri);
1563 if (replaceIntent == intent) {
1564 // Only add alternates if we didn't get a specific replacement from
1565 // the caller. If we have one it trumps potential alternates.
1566 for (int i = 1, N = count; i < N; i++) {
1567 final Intent altIntent = rci.getIntentAt(i);
1568 dri.addAlternateSourceIntent(altIntent);
1569 }
1570 }
1571 updateLastChosenPosition(add);
1572 }
1573
Esteban Talavera6de72662014-12-11 17:54:07 +00001574 private void updateLastChosenPosition(ResolveInfo info) {
1575 if (mLastChosen != null
1576 && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName)
1577 && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001578 mLastChosenPosition = mDisplayList.size() - 1;
Esteban Talavera6de72662014-12-11 17:54:07 +00001579 }
1580 }
1581
Adam Powell88831a22014-11-20 18:17:00 -08001582 private void addResolveInfo(DisplayResolveInfo dri) {
Adam Powell24428412015-04-01 17:19:56 -07001583 if (dri.mResolveInfo.targetUserId != UserHandle.USER_CURRENT && mOtherProfile == null) {
Adam Powell88831a22014-11-20 18:17:00 -08001584 // So far we only support a single other profile at a time.
1585 // The first one we see gets special treatment.
1586 mOtherProfile = dri;
1587 } else {
Adam Powell2ed547e2015-04-29 18:45:04 -07001588 mDisplayList.add(dri);
Adam Powell88831a22014-11-20 18:17:00 -08001589 }
1590 }
1591
Adam Powell278902c2014-07-12 18:33:22 -07001592 public ResolveInfo resolveInfoForPosition(int position, boolean filtered) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001593 return (filtered ? getItem(position) : mDisplayList.get(position))
1594 .getResolveInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
1596
Adam Powell24428412015-04-01 17:19:56 -07001597 public TargetInfo targetInfoForPosition(int position, boolean filtered) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001598 return filtered ? getItem(position) : mDisplayList.get(position);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 }
1600
1601 public int getCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001602 int result = mDisplayList.size();
Adam Powell278902c2014-07-12 18:33:22 -07001603 if (mFilterLastUsed && mLastChosenPosition >= 0) {
1604 result--;
1605 }
1606 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 }
1608
Adam Powell50077352015-05-26 18:01:55 -07001609 public int getUnfilteredCount() {
1610 return mDisplayList.size();
1611 }
1612
1613 public int getDisplayInfoCount() {
1614 return mDisplayList.size();
1615 }
1616
1617 public DisplayResolveInfo getDisplayInfoAt(int index) {
1618 return mDisplayList.get(index);
1619 }
1620
Adam Powell24428412015-04-01 17:19:56 -07001621 public TargetInfo getItem(int position) {
Adam Powell278902c2014-07-12 18:33:22 -07001622 if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) {
1623 position++;
1624 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001625 return mDisplayList.get(position);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
1627
1628 public long getItemId(int position) {
1629 return position;
1630 }
1631
Adam Powell24428412015-04-01 17:19:56 -07001632 public boolean hasExtendedInfo() {
1633 return mHasExtendedInfo;
1634 }
1635
1636 public boolean hasResolvedTarget(ResolveInfo info) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001637 for (int i = 0, N = mDisplayList.size(); i < N; i++) {
Adam Powellc412be62015-06-24 13:54:10 -07001638 if (resolveInfoMatch(info, mDisplayList.get(i).getResolveInfo())) {
Adam Powell24428412015-04-01 17:19:56 -07001639 return true;
1640 }
1641 }
1642 return false;
1643 }
1644
Adam Powell23882512016-01-29 10:21:00 -08001645 public int getDisplayResolveInfoCount() {
Adam Powell2ed547e2015-04-29 18:45:04 -07001646 return mDisplayList.size();
Adam Powell24428412015-04-01 17:19:56 -07001647 }
1648
Adam Powell23882512016-01-29 10:21:00 -08001649 public DisplayResolveInfo getDisplayResolveInfo(int index) {
Adam Powell2ed547e2015-04-29 18:45:04 -07001650 // Used to query services. We only query services for primary targets, not alternates.
1651 return mDisplayList.get(index);
Adam Powell24428412015-04-01 17:19:56 -07001652 }
1653
1654 public final View getView(int position, View convertView, ViewGroup parent) {
Adam Powellfd1e93d2014-09-07 16:52:22 -07001655 View view = convertView;
1656 if (view == null) {
Adam Powell24428412015-04-01 17:19:56 -07001657 view = createView(parent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
Adam Powell7d758002015-05-06 17:49:36 -07001659 onBindView(view, getItem(position));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 return view;
1661 }
1662
Adam Powell7d758002015-05-06 17:49:36 -07001663 public final View createView(ViewGroup parent) {
1664 final View view = onCreateView(parent);
1665 final ViewHolder holder = new ViewHolder(view);
1666 view.setTag(holder);
1667 return view;
1668 }
1669
1670 public View onCreateView(ViewGroup parent) {
Adam Powell24428412015-04-01 17:19:56 -07001671 return mInflater.inflate(
1672 com.android.internal.R.layout.resolve_list_item, parent, false);
1673 }
1674
1675 public boolean showsExtendedInfo(TargetInfo info) {
1676 return !TextUtils.isEmpty(info.getExtendedInfo());
1677 }
1678
Adam Powell23882512016-01-29 10:21:00 -08001679 public boolean isComponentPinned(ComponentName name) {
1680 return false;
1681 }
1682
Adam Powell7d758002015-05-06 17:49:36 -07001683 public final void bindView(int position, View view) {
1684 onBindView(view, getItem(position));
1685 }
1686
1687 private void onBindView(View view, TargetInfo info) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001688 final ViewHolder holder = (ViewHolder) view.getTag();
Adam Powell63b31692015-09-28 10:45:00 -07001689 final CharSequence label = info.getDisplayLabel();
1690 if (!TextUtils.equals(holder.text.getText(), label)) {
1691 holder.text.setText(info.getDisplayLabel());
1692 }
Adam Powell24428412015-04-01 17:19:56 -07001693 if (showsExtendedInfo(info)) {
Adam Powell0256c6f2013-05-29 16:42:33 -07001694 holder.text2.setVisibility(View.VISIBLE);
Adam Powell24428412015-04-01 17:19:56 -07001695 holder.text2.setText(info.getExtendedInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 } else {
Adam Powell0256c6f2013-05-29 16:42:33 -07001697 holder.text2.setVisibility(View.GONE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
Adam Powell24428412015-04-01 17:19:56 -07001699 if (info instanceof DisplayResolveInfo
1700 && !((DisplayResolveInfo) info).hasDisplayIcon()) {
1701 new LoadAdapterIconTask((DisplayResolveInfo) info).execute();
Dianne Hackborneb034652009-09-07 00:49:58 -07001702 }
Adam Powell24428412015-04-01 17:19:56 -07001703 holder.icon.setImageDrawable(info.getDisplayIcon());
Adam Powell7d758002015-05-06 17:49:36 -07001704 if (holder.badge != null) {
1705 final Drawable badge = info.getBadgeIcon();
1706 if (badge != null) {
1707 holder.badge.setImageDrawable(badge);
Alan Viverettece5d92c2015-07-31 16:46:56 -04001708 holder.badge.setContentDescription(info.getBadgeContentDescription());
Adam Powell7d758002015-05-06 17:49:36 -07001709 holder.badge.setVisibility(View.VISIBLE);
1710 } else {
1711 holder.badge.setVisibility(View.GONE);
1712 }
1713 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001714 }
1715 }
1716
Adam Powell2ed547e2015-04-29 18:45:04 -07001717 static final class ResolvedComponentInfo {
1718 public final ComponentName name;
Adam Powell23882512016-01-29 10:21:00 -08001719 private boolean mPinned;
Adam Powell2ed547e2015-04-29 18:45:04 -07001720 private final List<Intent> mIntents = new ArrayList<>();
1721 private final List<ResolveInfo> mResolveInfos = new ArrayList<>();
1722
1723 public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) {
1724 this.name = name;
1725 add(intent, info);
1726 }
1727
1728 public void add(Intent intent, ResolveInfo info) {
1729 mIntents.add(intent);
1730 mResolveInfos.add(info);
1731 }
1732
1733 public int getCount() {
1734 return mIntents.size();
1735 }
1736
1737 public Intent getIntentAt(int index) {
1738 return index >= 0 ? mIntents.get(index) : null;
1739 }
1740
1741 public ResolveInfo getResolveInfoAt(int index) {
1742 return index >= 0 ? mResolveInfos.get(index) : null;
1743 }
1744
1745 public int findIntent(Intent intent) {
1746 for (int i = 0, N = mIntents.size(); i < N; i++) {
1747 if (intent.equals(mIntents.get(i))) {
1748 return i;
1749 }
1750 }
1751 return -1;
1752 }
1753
1754 public int findResolveInfo(ResolveInfo info) {
1755 for (int i = 0, N = mResolveInfos.size(); i < N; i++) {
1756 if (info.equals(mResolveInfos.get(i))) {
1757 return i;
1758 }
1759 }
1760 return -1;
1761 }
Adam Powell23882512016-01-29 10:21:00 -08001762
1763 public boolean isPinned() {
1764 return mPinned;
1765 }
1766
1767 public void setPinned(boolean pinned) {
1768 mPinned = pinned;
1769 }
Adam Powell2ed547e2015-04-29 18:45:04 -07001770 }
1771
Adam Powell0256c6f2013-05-29 16:42:33 -07001772 static class ViewHolder {
1773 public TextView text;
1774 public TextView text2;
1775 public ImageView icon;
Adam Powell7d758002015-05-06 17:49:36 -07001776 public ImageView badge;
Adam Powell0256c6f2013-05-29 16:42:33 -07001777
1778 public ViewHolder(View view) {
1779 text = (TextView) view.findViewById(com.android.internal.R.id.text1);
1780 text2 = (TextView) view.findViewById(com.android.internal.R.id.text2);
1781 icon = (ImageView) view.findViewById(R.id.icon);
Adam Powell7d758002015-05-06 17:49:36 -07001782 badge = (ImageView) view.findViewById(R.id.target_badge);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 }
1784 }
1785
Adam Powell7d758002015-05-06 17:49:36 -07001786 class ItemClickListener implements AdapterView.OnItemClickListener,
1787 AdapterView.OnItemLongClickListener {
1788 @Override
1789 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1790 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1791 if (listView != null) {
1792 position -= listView.getHeaderViewsCount();
1793 }
1794 if (position < 0) {
1795 // Header views don't count.
1796 return;
1797 }
1798 final int checkedPos = mAdapterView.getCheckedItemPosition();
1799 final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION;
1800 if (mAlwaysUseOption && (!hasValidSelection || mLastSelected != checkedPos)) {
1801 setAlwaysButtonEnabled(hasValidSelection, checkedPos, true);
1802 mOnceButton.setEnabled(hasValidSelection);
1803 if (hasValidSelection) {
1804 mAdapterView.smoothScrollToPosition(checkedPos);
1805 }
1806 mLastSelected = checkedPos;
1807 } else {
1808 startSelected(position, false, true);
1809 }
1810 }
Adam Powell2d809622012-03-22 15:24:43 -07001811
1812 @Override
1813 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Adam Powell7d758002015-05-06 17:49:36 -07001814 final ListView listView = parent instanceof ListView ? (ListView) parent : null;
1815 if (listView != null) {
1816 position -= listView.getHeaderViewsCount();
Adam Powell24428412015-04-01 17:19:56 -07001817 }
Adam Powellfd1e93d2014-09-07 16:52:22 -07001818 if (position < 0) {
1819 // Header views don't count.
1820 return false;
1821 }
Adam Powell278902c2014-07-12 18:33:22 -07001822 ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true);
Adam Powell23882512016-01-29 10:21:00 -08001823 showTargetDetails(ri);
Adam Powell2d809622012-03-22 15:24:43 -07001824 return true;
1825 }
1826
1827 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001828
Adam Powell24428412015-04-01 17:19:56 -07001829 abstract class LoadIconTask extends AsyncTask<Void, Void, Drawable> {
1830 protected final DisplayResolveInfo mDisplayResolveInfo;
1831 private final ResolveInfo mResolveInfo;
1832
1833 public LoadIconTask(DisplayResolveInfo dri) {
1834 mDisplayResolveInfo = dri;
1835 mResolveInfo = dri.getResolveInfo();
Adam Powell0256c6f2013-05-29 16:42:33 -07001836 }
1837
1838 @Override
Adam Powell24428412015-04-01 17:19:56 -07001839 protected Drawable doInBackground(Void... params) {
1840 return loadIconForResolveInfo(mResolveInfo);
1841 }
1842
1843 @Override
1844 protected void onPostExecute(Drawable d) {
1845 mDisplayResolveInfo.setDisplayIcon(d);
1846 }
1847 }
1848
1849 class LoadAdapterIconTask extends LoadIconTask {
1850 public LoadAdapterIconTask(DisplayResolveInfo dri) {
1851 super(dri);
1852 }
1853
1854 @Override
1855 protected void onPostExecute(Drawable d) {
1856 super.onPostExecute(d);
1857 if (mProfileView != null && mAdapter.getOtherProfile() == mDisplayResolveInfo) {
Adam Powell88831a22014-11-20 18:17:00 -08001858 bindProfileView();
1859 }
Adam Powell0256c6f2013-05-29 16:42:33 -07001860 mAdapter.notifyDataSetChanged();
1861 }
1862 }
Adam Powell278902c2014-07-12 18:33:22 -07001863
Adam Powell24428412015-04-01 17:19:56 -07001864 class LoadIconIntoViewTask extends LoadIconTask {
1865 private final ImageView mTargetView;
Adam Powell278902c2014-07-12 18:33:22 -07001866
Adam Powell24428412015-04-01 17:19:56 -07001867 public LoadIconIntoViewTask(DisplayResolveInfo dri, ImageView target) {
1868 super(dri);
Adam Powell278902c2014-07-12 18:33:22 -07001869 mTargetView = target;
1870 }
1871
1872 @Override
Adam Powell24428412015-04-01 17:19:56 -07001873 protected void onPostExecute(Drawable d) {
1874 super.onPostExecute(d);
1875 mTargetView.setImageDrawable(d);
Adam Powell278902c2014-07-12 18:33:22 -07001876 }
1877 }
Adam Powell09a65602014-07-20 16:23:14 -07001878
Dianne Hackbornec452d92014-11-11 17:16:56 -08001879 static final boolean isSpecificUriMatch(int match) {
1880 match = match&IntentFilter.MATCH_CATEGORY_MASK;
1881 return match >= IntentFilter.MATCH_CATEGORY_HOST
1882 && match <= IntentFilter.MATCH_CATEGORY_PATH;
1883 }
1884
Adam Powell4c470d62015-06-19 17:46:17 -07001885 static class PickTargetOptionRequest extends PickOptionRequest {
1886 public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options,
1887 @Nullable Bundle extras) {
1888 super(prompt, options, extras);
1889 }
1890
1891 @Override
1892 public void onCancel() {
1893 super.onCancel();
1894 final ResolverActivity ra = (ResolverActivity) getActivity();
1895 if (ra != null) {
1896 ra.mPickOptionRequest = null;
1897 ra.finish();
1898 }
1899 }
1900
1901 @Override
1902 public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
1903 super.onPickOptionResult(finished, selections, result);
1904 if (selections.length != 1) {
1905 // TODO In a better world we would filter the UI presented here and let the
1906 // user refine. Maybe later.
1907 return;
1908 }
1909
1910 final ResolverActivity ra = (ResolverActivity) getActivity();
1911 if (ra != null) {
1912 final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex());
1913 if (ra.onTargetSelected(ti, false)) {
1914 ra.mPickOptionRequest = null;
1915 ra.finish();
1916 }
1917 }
1918 }
1919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920}