The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.app; |
| 18 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 21 | import android.app.ActivityThread; |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 22 | import android.app.VoiceInteractor.PickOptionRequest; |
| 23 | import android.app.VoiceInteractor.PickOptionRequest.Option; |
| 24 | import android.app.VoiceInteractor.Prompt; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 25 | import android.content.pm.ComponentInfo; |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 26 | import android.os.AsyncTask; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 27 | import android.provider.Settings; |
Adam Powell | 11f59a0 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 29 | import android.util.Slog; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 30 | import android.widget.AbsListView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import com.android.internal.R; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 32 | import com.android.internal.content.PackageMonitor; |
| 33 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 34 | import android.app.ActivityManager; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 35 | import android.app.ActivityManagerNative; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 36 | import android.app.AppGlobals; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.ComponentName; |
| 38 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.content.Intent; |
| 40 | import android.content.IntentFilter; |
| 41 | import android.content.pm.ActivityInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 42 | import android.content.pm.ApplicationInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 43 | import android.content.pm.LabeledIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.content.pm.PackageManager; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 45 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.content.pm.ResolveInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 47 | import android.content.pm.UserInfo; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 48 | import android.content.res.Resources; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 49 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.net.Uri; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 51 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.Bundle; |
| 53 | import android.os.PatternMatcher; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 54 | import android.os.RemoteException; |
Jeff Sharkey | 37355a9 | 2016-02-05 16:19:10 -0700 | [diff] [blame] | 55 | import android.os.StrictMode; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 56 | import android.os.UserHandle; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 57 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.util.Log; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 59 | import android.view.LayoutInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.view.View; |
| 61 | import android.view.ViewGroup; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 62 | import android.widget.AdapterView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.widget.BaseAdapter; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 64 | import android.widget.Button; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.widget.ImageView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 66 | import android.widget.ListView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.widget.TextView; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 68 | import android.widget.Toast; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 69 | import com.android.internal.widget.ResolverDrawerLayout; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import java.util.ArrayList; |
| 72 | import java.util.Collections; |
| 73 | import java.util.HashSet; |
| 74 | import java.util.Iterator; |
| 75 | import java.util.List; |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 76 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import java.util.Set; |
| 78 | |
Wale Ogunwale | 9014e66 | 2016-03-19 14:55:46 -0700 | [diff] [blame] | 79 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
Adrian Roos | 27c3ab6 | 2014-10-15 17:21:31 +0200 | [diff] [blame] | 80 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; |
| 81 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; |
| 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | /** |
| 84 | * This activity is displayed when the system attempts to start an Intent for |
| 85 | * which there is more than one matching activity, allowing the user to decide |
| 86 | * which to go to. It is not normally used directly by application developers. |
| 87 | */ |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 88 | public class ResolverActivity extends Activity { |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 89 | private static final String TAG = "ResolverActivity"; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 90 | private static final boolean DEBUG = false; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 91 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 92 | private int mLaunchedFromUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | private ResolveListAdapter mAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | private PackageManager mPm; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 95 | private boolean mSafeForwardingMode; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 96 | private boolean mAlwaysUseOption; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 97 | private AbsListView mAdapterView; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 98 | private Button mAlwaysButton; |
| 99 | private Button mOnceButton; |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 100 | private View mProfileView; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 101 | private int mIconDpi; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 102 | private int mLastSelected = AbsListView.INVALID_POSITION; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 103 | private boolean mResolvingHome = false; |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 104 | private int mProfileSwitchMessageId = -1; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 105 | private final ArrayList<Intent> mIntents = new ArrayList<>(); |
Adam Powell | d25267c | 2015-06-05 18:02:21 -0700 | [diff] [blame] | 106 | private ResolverComparator mResolverComparator; |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 107 | private PickTargetOptionRequest mPickOptionRequest; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 108 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 109 | protected ResolverDrawerLayout mResolverDrawerLayout; |
| 110 | |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 111 | private boolean mRegistered; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 112 | private final PackageMonitor mPackageMonitor = new PackageMonitor() { |
| 113 | @Override public void onSomePackagesChanged() { |
| 114 | mAdapter.handlePackagesChanged(); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 115 | if (mProfileView != null) { |
| 116 | bindProfileView(); |
| 117 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 118 | } |
| 119 | }; |
| 120 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 121 | private enum ActionTitle { |
| 122 | VIEW(Intent.ACTION_VIEW, |
| 123 | com.android.internal.R.string.whichViewApplication, |
| 124 | com.android.internal.R.string.whichViewApplicationNamed), |
| 125 | EDIT(Intent.ACTION_EDIT, |
| 126 | com.android.internal.R.string.whichEditApplication, |
| 127 | com.android.internal.R.string.whichEditApplicationNamed), |
| 128 | SEND(Intent.ACTION_SEND, |
| 129 | com.android.internal.R.string.whichSendApplication, |
| 130 | com.android.internal.R.string.whichSendApplicationNamed), |
| 131 | SENDTO(Intent.ACTION_SENDTO, |
Adam Powell | 13ea8f4 | 2016-03-18 09:39:41 -0700 | [diff] [blame] | 132 | com.android.internal.R.string.whichSendToApplication, |
| 133 | com.android.internal.R.string.whichSendToApplicationNamed), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 134 | SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE, |
| 135 | com.android.internal.R.string.whichSendApplication, |
| 136 | com.android.internal.R.string.whichSendApplicationNamed), |
| 137 | DEFAULT(null, |
| 138 | com.android.internal.R.string.whichApplication, |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 139 | com.android.internal.R.string.whichApplicationNamed), |
| 140 | HOME(Intent.ACTION_MAIN, |
| 141 | com.android.internal.R.string.whichHomeApplication, |
| 142 | com.android.internal.R.string.whichHomeApplicationNamed); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 143 | |
| 144 | public final String action; |
| 145 | public final int titleRes; |
| 146 | public final int namedTitleRes; |
| 147 | |
| 148 | ActionTitle(String action, int titleRes, int namedTitleRes) { |
| 149 | this.action = action; |
| 150 | this.titleRes = titleRes; |
| 151 | this.namedTitleRes = namedTitleRes; |
| 152 | } |
| 153 | |
| 154 | public static ActionTitle forAction(String action) { |
| 155 | for (ActionTitle title : values()) { |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 156 | if (title != HOME && action != null && action.equals(title.action)) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 157 | return title; |
| 158 | } |
| 159 | } |
| 160 | return DEFAULT; |
| 161 | } |
| 162 | } |
| 163 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 164 | private Intent makeMyIntent() { |
| 165 | Intent intent = new Intent(getIntent()); |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 166 | intent.setComponent(null); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 167 | // The resolver activity is set to be hidden from recent tasks. |
| 168 | // we don't want this attribute to be propagated to the next activity |
| 169 | // being launched. Note that if the original Intent also had this |
| 170 | // flag set, we are now losing it. That should be a very rare case |
| 171 | // and we can live with this. |
| 172 | intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 173 | return intent; |
| 174 | } |
| 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | @Override |
| 177 | protected void onCreate(Bundle savedInstanceState) { |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 178 | // Use a specialized prompt when we're handling the 'Home' app startActivity() |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 179 | final Intent intent = makeMyIntent(); |
| 180 | final Set<String> categories = intent.getCategories(); |
| 181 | if (Intent.ACTION_MAIN.equals(intent.getAction()) |
| 182 | && categories != null |
| 183 | && categories.size() == 1 |
| 184 | && categories.contains(Intent.CATEGORY_HOME)) { |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 185 | // Note: this field is not set to true in the compatibility version. |
| 186 | mResolvingHome = true; |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 189 | setSafeForwardingMode(true); |
| 190 | |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 191 | onCreate(savedInstanceState, intent, null, 0, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 194 | /** |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 195 | * Compatibility version for other bundled services that use this overload without |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 196 | * a default title resource |
| 197 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 199 | CharSequence title, Intent[] initialIntents, |
| 200 | List<ResolveInfo> rList, boolean alwaysUseOption) { |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 201 | onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, alwaysUseOption); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
| 205 | CharSequence title, int defaultTitleRes, Intent[] initialIntents, |
| 206 | List<ResolveInfo> rList, boolean alwaysUseOption) { |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 207 | setTheme(R.style.Theme_DeviceDefault_Resolver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | super.onCreate(savedInstanceState); |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 209 | |
| 210 | // Determine whether we should show that intent is forwarded |
| 211 | // from managed profile to owner or other way around. |
| 212 | setProfileSwitchMessageId(intent.getContentUserHint()); |
| 213 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 214 | try { |
| 215 | mLaunchedFromUid = ActivityManagerNative.getDefault().getLaunchedFromUid( |
| 216 | getActivityToken()); |
| 217 | } catch (RemoteException e) { |
| 218 | mLaunchedFromUid = -1; |
| 219 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 220 | |
| 221 | if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) { |
| 222 | // Gulp! |
| 223 | finish(); |
| 224 | return; |
| 225 | } |
| 226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | mPm = getPackageManager(); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 228 | |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 229 | mPackageMonitor.register(this, getMainLooper(), false); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 230 | mRegistered = true; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 231 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 232 | final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); |
| 233 | mIconDpi = am.getLauncherLargeIconDensity(); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 234 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 235 | // Add our initial intent as the first item, regardless of what else has already been added. |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 236 | mIntents.add(0, new Intent(intent)); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 237 | |
Adam Powell | d25267c | 2015-06-05 18:02:21 -0700 | [diff] [blame] | 238 | final String referrerPackage = getReferrerPackageName(); |
| 239 | |
| 240 | mResolverComparator = new ResolverComparator(this, getTargetIntent(), referrerPackage); |
| 241 | |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 242 | if (configureContentView(mIntents, initialIntents, rList, alwaysUseOption)) { |
| 243 | return; |
| 244 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 245 | |
Adrian Roos | 27c3ab6 | 2014-10-15 17:21:31 +0200 | [diff] [blame] | 246 | // Prevent the Resolver window from becoming the top fullscreen window and thus from taking |
| 247 | // control of the system bars. |
| 248 | getWindow().clearFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 250 | final ResolverDrawerLayout rdl = (ResolverDrawerLayout) findViewById(R.id.contentPanel); |
| 251 | if (rdl != null) { |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 252 | rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 253 | @Override |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 254 | public void onDismissed() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 255 | finish(); |
| 256 | } |
| 257 | }); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 258 | if (isVoiceInteraction()) { |
| 259 | rdl.setCollapsed(false); |
| 260 | } |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 261 | mResolverDrawerLayout = rdl; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Adam Powell | 11f59a0 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 264 | if (title == null) { |
| 265 | title = getTitleForAction(intent.getAction(), defaultTitleRes); |
| 266 | } |
| 267 | if (!TextUtils.isEmpty(title)) { |
| 268 | final TextView titleView = (TextView) findViewById(R.id.title); |
| 269 | if (titleView != null) { |
| 270 | titleView.setText(title); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 271 | } |
Adam Powell | 11f59a0 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 272 | setTitle(title); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 273 | |
| 274 | // Try to initialize the title icon if we have a view for it and a title to match |
| 275 | final ImageView titleIcon = (ImageView) findViewById(R.id.title_icon); |
| 276 | if (titleIcon != null) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 277 | ApplicationInfo ai = null; |
| 278 | try { |
| 279 | if (!TextUtils.isEmpty(referrerPackage)) { |
| 280 | ai = mPm.getApplicationInfo(referrerPackage, 0); |
| 281 | } |
| 282 | } catch (NameNotFoundException e) { |
| 283 | Log.e(TAG, "Could not find referrer package " + referrerPackage); |
| 284 | } |
| 285 | |
| 286 | if (ai != null) { |
| 287 | titleIcon.setImageDrawable(ai.loadIcon(mPm)); |
| 288 | } |
| 289 | } |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 290 | } |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 291 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 292 | final ImageView iconView = (ImageView) findViewById(R.id.icon); |
| 293 | final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem(); |
| 294 | if (iconView != null && iconInfo != null) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 295 | new LoadIconIntoViewTask(iconInfo, iconView).execute(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if (alwaysUseOption || mAdapter.hasFilteredItem()) { |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 299 | final ViewGroup buttonLayout = (ViewGroup) findViewById(R.id.button_bar); |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 300 | if (buttonLayout != null) { |
| 301 | buttonLayout.setVisibility(View.VISIBLE); |
| 302 | mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always); |
| 303 | mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once); |
| 304 | } else { |
| 305 | mAlwaysUseOption = false; |
| 306 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (mAdapter.hasFilteredItem()) { |
| 310 | setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false); |
| 311 | mOnceButton.setEnabled(true); |
| 312 | } |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 313 | |
| 314 | mProfileView = findViewById(R.id.profile_button); |
| 315 | if (mProfileView != null) { |
| 316 | mProfileView.setOnClickListener(new View.OnClickListener() { |
| 317 | @Override |
| 318 | public void onClick(View v) { |
| 319 | final DisplayResolveInfo dri = mAdapter.getOtherProfile(); |
| 320 | if (dri == null) { |
| 321 | return; |
| 322 | } |
| 323 | |
Sander Alewijnse | 053d3dd | 2015-03-09 15:31:10 +0000 | [diff] [blame] | 324 | // Do not show the profile switch message anymore. |
| 325 | mProfileSwitchMessageId = -1; |
| 326 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 327 | onTargetSelected(dri, false); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 328 | finish(); |
| 329 | } |
| 330 | }); |
| 331 | bindProfileView(); |
| 332 | } |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 333 | |
| 334 | if (isVoiceInteraction()) { |
| 335 | onSetupVoiceInteraction(); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Perform any initialization needed for voice interaction. |
| 341 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 342 | public void onSetupVoiceInteraction() { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 343 | // Do it right now. Subclasses may delay this and send it later. |
| 344 | sendVoiceChoicesIfNeeded(); |
| 345 | } |
| 346 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 347 | public void sendVoiceChoicesIfNeeded() { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 348 | if (!isVoiceInteraction()) { |
| 349 | // Clearly not needed. |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | final Option[] options = new Option[mAdapter.getCount()]; |
| 355 | for (int i = 0, N = options.length; i < N; i++) { |
| 356 | options[i] = optionForChooserTarget(mAdapter.getItem(i), i); |
| 357 | } |
| 358 | |
| 359 | mPickOptionRequest = new PickTargetOptionRequest( |
| 360 | new Prompt(getTitle()), options, null); |
| 361 | getVoiceInteractor().submitRequest(mPickOptionRequest); |
| 362 | } |
| 363 | |
| 364 | Option optionForChooserTarget(TargetInfo target, int index) { |
| 365 | return new Option(target.getDisplayLabel(), index); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 368 | protected final void setAdditionalTargets(Intent[] intents) { |
| 369 | if (intents != null) { |
| 370 | for (Intent intent : intents) { |
| 371 | mIntents.add(intent); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 376 | public Intent getTargetIntent() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 377 | return mIntents.isEmpty() ? null : mIntents.get(0); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 380 | private String getReferrerPackageName() { |
| 381 | final Uri referrer = getReferrer(); |
| 382 | if (referrer != null && "android-app".equals(referrer.getScheme())) { |
| 383 | return referrer.getHost(); |
| 384 | } |
| 385 | return null; |
| 386 | } |
| 387 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 388 | public int getLayoutResource() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 389 | return R.layout.resolver_list; |
| 390 | } |
| 391 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 392 | void bindProfileView() { |
| 393 | final DisplayResolveInfo dri = mAdapter.getOtherProfile(); |
| 394 | if (dri != null) { |
| 395 | mProfileView.setVisibility(View.VISIBLE); |
Nicolas Prevot | 256423f | 2016-02-09 16:33:57 +0000 | [diff] [blame] | 396 | final TextView text = (TextView) mProfileView.findViewById(R.id.profile_button); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 397 | text.setText(dri.getDisplayLabel()); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 398 | } else { |
| 399 | mProfileView.setVisibility(View.GONE); |
| 400 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 403 | private void setProfileSwitchMessageId(int contentUserHint) { |
| 404 | if (contentUserHint != UserHandle.USER_CURRENT && |
| 405 | contentUserHint != UserHandle.myUserId()) { |
| 406 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 407 | UserInfo originUserInfo = userManager.getUserInfo(contentUserHint); |
| 408 | boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile() |
| 409 | : false; |
| 410 | boolean targetIsManaged = userManager.isManagedProfile(); |
| 411 | if (originIsManaged && !targetIsManaged) { |
| 412 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner; |
| 413 | } else if (!originIsManaged && targetIsManaged) { |
| 414 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 419 | /** |
| 420 | * Turn on launch mode that is safe to use when forwarding intents received from |
| 421 | * applications and running in system processes. This mode uses Activity.startActivityAsCaller |
| 422 | * instead of the normal Activity.startActivity for launching the activity selected |
| 423 | * by the user. |
| 424 | * |
| 425 | * <p>This mode is set to true by default if the activity is initialized through |
| 426 | * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate |
| 427 | * methods, it is set to false by default. You must set it before calling one of the |
| 428 | * more detailed onCreate methods, so that it will be set correctly in the case where |
| 429 | * there is only one intent to resolve and it is thus started immediately.</p> |
| 430 | */ |
| 431 | public void setSafeForwardingMode(boolean safeForwarding) { |
| 432 | mSafeForwardingMode = safeForwarding; |
| 433 | } |
| 434 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 435 | protected CharSequence getTitleForAction(String action, int defaultTitleRes) { |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 436 | final ActionTitle title = mResolvingHome ? ActionTitle.HOME : ActionTitle.forAction(action); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 437 | final boolean named = mAdapter.hasFilteredItem(); |
| 438 | if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) { |
| 439 | return getString(defaultTitleRes); |
| 440 | } else { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 441 | return named |
| 442 | ? getString(title.namedTitleRes, mAdapter.getFilteredItem().getDisplayLabel()) |
| 443 | : getString(title.titleRes); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 444 | } |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 447 | void dismiss() { |
| 448 | if (!isFinishing()) { |
| 449 | finish(); |
| 450 | } |
| 451 | } |
| 452 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 453 | Drawable getIcon(Resources res, int resId) { |
| 454 | Drawable result; |
| 455 | try { |
| 456 | result = res.getDrawableForDensity(resId, mIconDpi); |
| 457 | } catch (Resources.NotFoundException e) { |
| 458 | result = null; |
| 459 | } |
| 460 | |
| 461 | return result; |
| 462 | } |
| 463 | |
| 464 | Drawable loadIconForResolveInfo(ResolveInfo ri) { |
| 465 | Drawable dr; |
| 466 | try { |
| 467 | if (ri.resolvePackageName != null && ri.icon != 0) { |
| 468 | dr = getIcon(mPm.getResourcesForApplication(ri.resolvePackageName), ri.icon); |
| 469 | if (dr != null) { |
| 470 | return dr; |
| 471 | } |
| 472 | } |
| 473 | final int iconRes = ri.getIconResource(); |
| 474 | if (iconRes != 0) { |
| 475 | dr = getIcon(mPm.getResourcesForApplication(ri.activityInfo.packageName), iconRes); |
| 476 | if (dr != null) { |
| 477 | return dr; |
| 478 | } |
| 479 | } |
| 480 | } catch (NameNotFoundException e) { |
| 481 | Log.e(TAG, "Couldn't find resources for package", e); |
| 482 | } |
| 483 | return ri.loadIcon(mPm); |
| 484 | } |
| 485 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 486 | @Override |
| 487 | protected void onRestart() { |
| 488 | super.onRestart(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 489 | if (!mRegistered) { |
| 490 | mPackageMonitor.register(this, getMainLooper(), false); |
| 491 | mRegistered = true; |
| 492 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 493 | mAdapter.handlePackagesChanged(); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 494 | if (mProfileView != null) { |
| 495 | bindProfileView(); |
| 496 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | @Override |
| 500 | protected void onStop() { |
| 501 | super.onStop(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 502 | if (mRegistered) { |
| 503 | mPackageMonitor.unregister(); |
| 504 | mRegistered = false; |
| 505 | } |
Wale Ogunwale | 9014e66 | 2016-03-19 14:55:46 -0700 | [diff] [blame] | 506 | final Intent intent = getIntent(); |
| 507 | if ((intent.getFlags() & FLAG_ACTIVITY_NEW_TASK) != 0 && !isVoiceInteraction() |
| 508 | && !mResolvingHome) { |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 509 | // This resolver is in the unusual situation where it has been |
| 510 | // launched at the top of a new task. We don't let it be added |
| 511 | // to the recent tasks shown to the user, and we need to make sure |
| 512 | // that each time we are launched we get the correct launching |
| 513 | // uid (not re-using the same resolver from an old launching uid), |
| 514 | // so we will now finish ourself since being no longer visible, |
| 515 | // the user probably can't get back to us. |
| 516 | if (!isChangingConfigurations()) { |
| 517 | finish(); |
| 518 | } |
| 519 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 522 | @Override |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 523 | protected void onDestroy() { |
| 524 | super.onDestroy(); |
| 525 | if (!isChangingConfigurations() && mPickOptionRequest != null) { |
| 526 | mPickOptionRequest.cancel(); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | @Override |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 531 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 532 | super.onRestoreInstanceState(savedInstanceState); |
| 533 | if (mAlwaysUseOption) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 534 | final int checkedPos = mAdapterView.getCheckedItemPosition(); |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 535 | final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION; |
Adam Powell | d81cc4a | 2012-07-19 13:51:39 -0700 | [diff] [blame] | 536 | mLastSelected = checkedPos; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 537 | setAlwaysButtonEnabled(hasValidSelection, checkedPos, true); |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 538 | mOnceButton.setEnabled(hasValidSelection); |
| 539 | if (hasValidSelection) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 540 | mAdapterView.setSelection(checkedPos); |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 545 | private boolean hasManagedProfile() { |
| 546 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 547 | if (userManager == null) { |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | try { |
| 552 | List<UserInfo> profiles = userManager.getProfiles(getUserId()); |
| 553 | for (UserInfo userInfo : profiles) { |
| 554 | if (userInfo != null && userInfo.isManagedProfile()) { |
| 555 | return true; |
| 556 | } |
| 557 | } |
| 558 | } catch (SecurityException e) { |
| 559 | return false; |
| 560 | } |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | private boolean supportsManagedProfiles(ResolveInfo resolveInfo) { |
| 565 | try { |
| 566 | ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| 567 | resolveInfo.activityInfo.packageName, 0 /* default flags */); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 568 | return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 569 | } catch (NameNotFoundException e) { |
| 570 | return false; |
| 571 | } |
| 572 | } |
| 573 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 574 | private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos, |
| 575 | boolean filtered) { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 576 | boolean enabled = false; |
| 577 | if (hasValidSelection) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 578 | ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered); |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 579 | if (ri.targetUserId == UserHandle.USER_CURRENT) { |
| 580 | enabled = true; |
| 581 | } |
| 582 | } |
| 583 | mAlwaysButton.setEnabled(enabled); |
| 584 | } |
| 585 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 586 | public void onButtonClick(View v) { |
| 587 | final int id = v.getId(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 588 | startSelected(mAlwaysUseOption ? |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 589 | mAdapterView.getCheckedItemPosition() : mAdapter.getFilteredPosition(), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 590 | id == R.id.button_always, |
| 591 | mAlwaysUseOption); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 594 | public void startSelected(int which, boolean always, boolean filtered) { |
Amith Yamasani | 07cd351 | 2013-09-18 13:16:00 -0700 | [diff] [blame] | 595 | if (isFinishing()) { |
| 596 | return; |
| 597 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 598 | ResolveInfo ri = mAdapter.resolveInfoForPosition(which, filtered); |
Sander Alewijnse | 86d35ba | 2015-02-04 15:14:53 +0000 | [diff] [blame] | 599 | if (mResolvingHome && hasManagedProfile() && !supportsManagedProfiles(ri)) { |
| 600 | Toast.makeText(this, String.format(getResources().getString( |
| 601 | com.android.internal.R.string.activity_resolver_work_profiles_support), |
| 602 | ri.activityInfo.loadLabel(getPackageManager()).toString()), |
| 603 | Toast.LENGTH_LONG).show(); |
| 604 | return; |
| 605 | } |
| 606 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 607 | TargetInfo target = mAdapter.targetInfoForPosition(which, filtered); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 608 | if (onTargetSelected(target, always)) { |
| 609 | finish(); |
| 610 | } |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 611 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 613 | /** |
| 614 | * Replace me in subclasses! |
| 615 | */ |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 616 | public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) { |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 617 | return defIntent; |
| 618 | } |
| 619 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 620 | protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 621 | final ResolveInfo ri = target.getResolveInfo(); |
| 622 | final Intent intent = target != null ? target.getResolvedIntent() : null; |
| 623 | |
| 624 | if (intent != null && (mAlwaysUseOption || mAdapter.hasFilteredItem()) |
| 625 | && mAdapter.mOrigResolveList != null) { |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 626 | // Build a reasonable intent filter, based on what matched. |
| 627 | IntentFilter filter = new IntentFilter(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 628 | String action = intent.getAction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 630 | if (action != null) { |
| 631 | filter.addAction(action); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 633 | Set<String> categories = intent.getCategories(); |
| 634 | if (categories != null) { |
| 635 | for (String cat : categories) { |
| 636 | filter.addCategory(cat); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 639 | filter.addCategory(Intent.CATEGORY_DEFAULT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 641 | int cat = ri.match & IntentFilter.MATCH_CATEGORY_MASK; |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 642 | Uri data = intent.getData(); |
| 643 | if (cat == IntentFilter.MATCH_CATEGORY_TYPE) { |
| 644 | String mimeType = intent.resolveType(this); |
| 645 | if (mimeType != null) { |
| 646 | try { |
| 647 | filter.addDataType(mimeType); |
| 648 | } catch (IntentFilter.MalformedMimeTypeException e) { |
| 649 | Log.w("ResolverActivity", e); |
| 650 | filter = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | } |
| 652 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 653 | } |
| 654 | if (data != null && data.getScheme() != null) { |
| 655 | // We need the data specification if there was no type, |
| 656 | // OR if the scheme is not one of our magical "file:" |
| 657 | // or "content:" schemes (see IntentFilter for the reason). |
| 658 | if (cat != IntentFilter.MATCH_CATEGORY_TYPE |
| 659 | || (!"file".equals(data.getScheme()) |
| 660 | && !"content".equals(data.getScheme()))) { |
| 661 | filter.addDataScheme(data.getScheme()); |
| 662 | |
| 663 | // Look through the resolved filter to determine which part |
| 664 | // of it matched the original Intent. |
| 665 | Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator(); |
| 666 | if (pIt != null) { |
| 667 | String ssp = data.getSchemeSpecificPart(); |
| 668 | while (ssp != null && pIt.hasNext()) { |
| 669 | PatternMatcher p = pIt.next(); |
| 670 | if (p.match(ssp)) { |
| 671 | filter.addDataSchemeSpecificPart(p.getPath(), p.getType()); |
| 672 | break; |
| 673 | } |
Dianne Hackborn | df1c0bf | 2013-06-12 16:21:38 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 676 | Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator(); |
| 677 | if (aIt != null) { |
| 678 | while (aIt.hasNext()) { |
| 679 | IntentFilter.AuthorityEntry a = aIt.next(); |
| 680 | if (a.match(data) >= 0) { |
| 681 | int port = a.getPort(); |
| 682 | filter.addDataAuthority(a.getHost(), |
| 683 | port >= 0 ? Integer.toString(port) : null); |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | pIt = ri.filter.pathsIterator(); |
| 689 | if (pIt != null) { |
| 690 | String path = data.getPath(); |
| 691 | while (path != null && pIt.hasNext()) { |
| 692 | PatternMatcher p = pIt.next(); |
| 693 | if (p.match(path)) { |
| 694 | filter.addDataPath(p.getPath(), p.getType()); |
| 695 | break; |
| 696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 702 | if (filter != null) { |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 703 | final int N = mAdapter.mOrigResolveList.size(); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 704 | ComponentName[] set = new ComponentName[N]; |
| 705 | int bestMatch = 0; |
| 706 | for (int i=0; i<N; i++) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 707 | ResolveInfo r = mAdapter.mOrigResolveList.get(i).getResolveInfoAt(0); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 708 | set[i] = new ComponentName(r.activityInfo.packageName, |
| 709 | r.activityInfo.name); |
| 710 | if (r.match > bestMatch) bestMatch = r.match; |
| 711 | } |
| 712 | if (alwaysCheck) { |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 713 | final int userId = getUserId(); |
| 714 | final PackageManager pm = getPackageManager(); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 715 | |
| 716 | // Set the preferred Activity |
| 717 | pm.addPreferredActivity(filter, bestMatch, set, intent.getComponent()); |
| 718 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 719 | if (ri.handleAllWebDataURI) { |
| 720 | // Set default Browser if needed |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 721 | final String packageName = pm.getDefaultBrowserPackageNameAsUser(userId); |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 722 | if (TextUtils.isEmpty(packageName)) { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 723 | pm.setDefaultBrowserPackageNameAsUser(ri.activityInfo.packageName, userId); |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 724 | } |
| 725 | } else { |
| 726 | // Update Domain Verification status |
| 727 | ComponentName cn = intent.getComponent(); |
| 728 | String packageName = cn.getPackageName(); |
| 729 | String dataScheme = (data != null) ? data.getScheme() : null; |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 730 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 731 | boolean isHttpOrHttps = (dataScheme != null) && |
| 732 | (dataScheme.equals(IntentFilter.SCHEME_HTTP) || |
| 733 | dataScheme.equals(IntentFilter.SCHEME_HTTPS)); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 734 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 735 | boolean isViewAction = (action != null) && action.equals(Intent.ACTION_VIEW); |
| 736 | boolean hasCategoryBrowsable = (categories != null) && |
| 737 | categories.contains(Intent.CATEGORY_BROWSABLE); |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 738 | |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 739 | if (isHttpOrHttps && isViewAction && hasCategoryBrowsable) { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 740 | pm.updateIntentVerificationStatusAsUser(packageName, |
Fabrice Di Meglio | 3453e08 | 2015-05-11 17:46:23 -0700 | [diff] [blame] | 741 | PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS, |
| 742 | userId); |
| 743 | } |
Fabrice Di Meglio | 1c1b471 | 2014-11-19 17:12:32 -0800 | [diff] [blame] | 744 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 745 | } else { |
| 746 | try { |
| 747 | AppGlobals.getPackageManager().setLastChosenActivity(intent, |
| 748 | intent.resolveTypeIfNeeded(getContentResolver()), |
| 749 | PackageManager.MATCH_DEFAULT_ONLY, |
| 750 | filter, bestMatch, intent.getComponent()); |
| 751 | } catch (RemoteException re) { |
| 752 | Log.d(TAG, "Error calling setLastChosenActivity\n" + re); |
| 753 | } |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 754 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 758 | if (target != null) { |
| 759 | safelyStartActivity(target); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 760 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 761 | return true; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 764 | public void safelyStartActivity(TargetInfo cti) { |
Jeff Sharkey | 2b9eb89 | 2016-02-16 09:21:51 -0700 | [diff] [blame] | 765 | // We're dispatching intents that might be coming from legacy apps, so |
| 766 | // don't kill ourselves. |
| 767 | StrictMode.disableDeathOnFileUriExposure(); |
| 768 | try { |
| 769 | safelyStartActivityInternal(cti); |
| 770 | } finally { |
| 771 | StrictMode.enableDeathOnFileUriExposure(); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | private void safelyStartActivityInternal(TargetInfo cti) { |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 776 | // If needed, show that intent is forwarded |
| 777 | // from managed profile to owner or other way around. |
| 778 | if (mProfileSwitchMessageId != -1) { |
| 779 | Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show(); |
| 780 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 781 | if (!mSafeForwardingMode) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 782 | if (cti.start(this, null)) { |
| 783 | onActivityStarted(cti); |
| 784 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 785 | return; |
| 786 | } |
| 787 | try { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 788 | if (cti.startAsCaller(this, null, UserHandle.USER_NULL)) { |
| 789 | onActivityStarted(cti); |
| 790 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 791 | } catch (RuntimeException e) { |
| 792 | String launchedFromPackage; |
| 793 | try { |
| 794 | launchedFromPackage = ActivityManagerNative.getDefault().getLaunchedFromPackage( |
| 795 | getActivityToken()); |
| 796 | } catch (RemoteException e2) { |
| 797 | launchedFromPackage = "??"; |
| 798 | } |
| 799 | Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid |
| 800 | + " package " + launchedFromPackage + ", while running in " |
| 801 | + ActivityThread.currentProcessName(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 805 | public void onActivityStarted(TargetInfo cti) { |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 806 | // Do nothing |
| 807 | } |
| 808 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 809 | public boolean shouldGetActivityMetadata() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 810 | return false; |
| 811 | } |
| 812 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 813 | public boolean shouldAutoLaunchSingleChoice(TargetInfo target) { |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 814 | return true; |
| 815 | } |
| 816 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 817 | public void showTargetDetails(ResolveInfo ri) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 818 | Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 819 | .setData(Uri.fromParts("package", ri.activityInfo.packageName, null)) |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 820 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
Amith Yamasani | 203a2f4 | 2012-10-03 20:16:40 -0700 | [diff] [blame] | 821 | startActivity(in); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 824 | public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents, |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 825 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
| 826 | boolean filterLastUsed) { |
| 827 | return new ResolveListAdapter(context, payloadIntents, initialIntents, rList, |
| 828 | launchedFromUid, filterLastUsed); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 831 | /** |
| 832 | * Returns true if the activity is finishing and creation should halt |
| 833 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 834 | public boolean configureContentView(List<Intent> payloadIntents, Intent[] initialIntents, |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 835 | List<ResolveInfo> rList, boolean alwaysUseOption) { |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 836 | // The last argument of createAdapter is whether to do special handling |
| 837 | // of the last used choice to highlight it in the list. We need to always |
| 838 | // turn this off when running under voice interaction, since it results in |
| 839 | // a more complicated UI that the current voice interaction flow is not able |
| 840 | // to handle. |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 841 | mAdapter = createAdapter(this, payloadIntents, initialIntents, rList, |
Dianne Hackborn | 57dd737 | 2015-07-27 18:11:14 -0700 | [diff] [blame] | 842 | mLaunchedFromUid, alwaysUseOption && !isVoiceInteraction()); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 843 | |
| 844 | final int layoutId; |
| 845 | if (mAdapter.hasFilteredItem()) { |
| 846 | layoutId = R.layout.resolver_list_with_default; |
| 847 | alwaysUseOption = false; |
| 848 | } else { |
| 849 | layoutId = getLayoutResource(); |
| 850 | } |
| 851 | mAlwaysUseOption = alwaysUseOption; |
| 852 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 853 | int count = mAdapter.getUnfilteredCount(); |
Jeff Sharkey | cc2ae6b4 | 2015-09-29 13:04:46 -0700 | [diff] [blame] | 854 | if (count == 1 && mAdapter.getOtherProfile() == null) { |
| 855 | // Only one target, so we're a candidate to auto-launch! |
| 856 | final TargetInfo target = mAdapter.targetInfoForPosition(0, false); |
| 857 | if (shouldAutoLaunchSingleChoice(target)) { |
| 858 | safelyStartActivity(target); |
| 859 | mPackageMonitor.unregister(); |
| 860 | mRegistered = false; |
| 861 | finish(); |
| 862 | return true; |
| 863 | } |
| 864 | } |
| 865 | if (count > 0) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 866 | setContentView(layoutId); |
| 867 | mAdapterView = (AbsListView) findViewById(R.id.resolver_list); |
| 868 | onPrepareAdapterView(mAdapterView, mAdapter, alwaysUseOption); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 869 | } else { |
| 870 | setContentView(R.layout.resolver_list); |
| 871 | |
| 872 | final TextView empty = (TextView) findViewById(R.id.empty); |
| 873 | empty.setVisibility(View.VISIBLE); |
| 874 | |
| 875 | mAdapterView = (AbsListView) findViewById(R.id.resolver_list); |
| 876 | mAdapterView.setVisibility(View.GONE); |
| 877 | } |
Adam Powell | 39e94eb | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 878 | return false; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 881 | public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter, |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 882 | boolean alwaysUseOption) { |
| 883 | final boolean useHeader = adapter.hasFilteredItem(); |
| 884 | final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null; |
| 885 | |
| 886 | adapterView.setAdapter(mAdapter); |
| 887 | |
| 888 | final ItemClickListener listener = new ItemClickListener(); |
| 889 | adapterView.setOnItemClickListener(listener); |
| 890 | adapterView.setOnItemLongClickListener(listener); |
| 891 | |
| 892 | if (alwaysUseOption) { |
| 893 | listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); |
| 894 | } |
| 895 | |
| 896 | if (useHeader && listView != null) { |
| 897 | listView.addHeaderView(LayoutInflater.from(this).inflate( |
| 898 | R.layout.resolver_different_item_header, listView, false)); |
| 899 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 902 | /** |
| 903 | * Check a simple match for the component of two ResolveInfos. |
| 904 | */ |
| 905 | static boolean resolveInfoMatch(ResolveInfo lhs, ResolveInfo rhs) { |
| 906 | return lhs == null ? rhs == null |
| 907 | : lhs.activityInfo == null ? rhs.activityInfo == null |
| 908 | : Objects.equals(lhs.activityInfo.name, rhs.activityInfo.name) |
| 909 | && Objects.equals(lhs.activityInfo.packageName, rhs.activityInfo.packageName); |
| 910 | } |
| 911 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 912 | public final class DisplayResolveInfo implements TargetInfo { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 913 | private final ResolveInfo mResolveInfo; |
| 914 | private final CharSequence mDisplayLabel; |
| 915 | private Drawable mDisplayIcon; |
Adam Powell | 00f4aad | 2015-09-17 13:38:16 -0700 | [diff] [blame] | 916 | private Drawable mBadge; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 917 | private final CharSequence mExtendedInfo; |
| 918 | private final Intent mResolvedIntent; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 919 | private final List<Intent> mSourceIntents = new ArrayList<>(); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 920 | private boolean mPinned; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 922 | public DisplayResolveInfo(Intent originalIntent, ResolveInfo pri, CharSequence pLabel, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 923 | CharSequence pInfo, Intent pOrigIntent) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 924 | mSourceIntents.add(originalIntent); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 925 | mResolveInfo = pri; |
| 926 | mDisplayLabel = pLabel; |
| 927 | mExtendedInfo = pInfo; |
| 928 | |
| 929 | final Intent intent = new Intent(pOrigIntent != null ? pOrigIntent : |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 930 | getReplacementIntent(pri.activityInfo, getTargetIntent())); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 931 | intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| 932 | | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
| 933 | final ActivityInfo ai = mResolveInfo.activityInfo; |
| 934 | intent.setComponent(new ComponentName(ai.applicationInfo.packageName, ai.name)); |
| 935 | |
| 936 | mResolvedIntent = intent; |
| 937 | } |
| 938 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 939 | private DisplayResolveInfo(DisplayResolveInfo other, Intent fillInIntent, int flags) { |
| 940 | mSourceIntents.addAll(other.getAllSourceIntents()); |
| 941 | mResolveInfo = other.mResolveInfo; |
| 942 | mDisplayLabel = other.mDisplayLabel; |
| 943 | mDisplayIcon = other.mDisplayIcon; |
| 944 | mExtendedInfo = other.mExtendedInfo; |
| 945 | mResolvedIntent = new Intent(other.mResolvedIntent); |
| 946 | mResolvedIntent.fillIn(fillInIntent, flags); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 947 | mPinned = other.mPinned; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 950 | public ResolveInfo getResolveInfo() { |
| 951 | return mResolveInfo; |
| 952 | } |
| 953 | |
| 954 | public CharSequence getDisplayLabel() { |
| 955 | return mDisplayLabel; |
| 956 | } |
| 957 | |
| 958 | public Drawable getDisplayIcon() { |
| 959 | return mDisplayIcon; |
| 960 | } |
| 961 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 962 | public Drawable getBadgeIcon() { |
Adam Powell | 00f4aad | 2015-09-17 13:38:16 -0700 | [diff] [blame] | 963 | // We only expose a badge if we have extended info. |
| 964 | // The badge is a higher-priority disambiguation signal |
| 965 | // but we don't need one if we wouldn't show extended info at all. |
| 966 | if (TextUtils.isEmpty(getExtendedInfo())) { |
| 967 | return null; |
| 968 | } |
| 969 | |
| 970 | if (mBadge == null && mResolveInfo != null && mResolveInfo.activityInfo != null |
| 971 | && mResolveInfo.activityInfo.applicationInfo != null) { |
| 972 | if (mResolveInfo.activityInfo.icon == 0 || mResolveInfo.activityInfo.icon |
| 973 | == mResolveInfo.activityInfo.applicationInfo.icon) { |
| 974 | // Badging an icon with exactly the same icon is silly. |
| 975 | // If the activityInfo icon resid is 0 it will fall back |
| 976 | // to the application's icon, making it a match. |
| 977 | return null; |
| 978 | } |
| 979 | mBadge = mResolveInfo.activityInfo.applicationInfo.loadIcon(mPm); |
| 980 | } |
| 981 | return mBadge; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 984 | @Override |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 985 | public CharSequence getBadgeContentDescription() { |
| 986 | return null; |
| 987 | } |
| 988 | |
| 989 | @Override |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 990 | public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) { |
| 991 | return new DisplayResolveInfo(this, fillInIntent, flags); |
| 992 | } |
| 993 | |
| 994 | @Override |
| 995 | public List<Intent> getAllSourceIntents() { |
| 996 | return mSourceIntents; |
| 997 | } |
| 998 | |
| 999 | public void addAlternateSourceIntent(Intent alt) { |
| 1000 | mSourceIntents.add(alt); |
| 1001 | } |
| 1002 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1003 | public void setDisplayIcon(Drawable icon) { |
| 1004 | mDisplayIcon = icon; |
| 1005 | } |
| 1006 | |
| 1007 | public boolean hasDisplayIcon() { |
| 1008 | return mDisplayIcon != null; |
| 1009 | } |
| 1010 | |
| 1011 | public CharSequence getExtendedInfo() { |
| 1012 | return mExtendedInfo; |
| 1013 | } |
| 1014 | |
| 1015 | public Intent getResolvedIntent() { |
| 1016 | return mResolvedIntent; |
| 1017 | } |
| 1018 | |
| 1019 | @Override |
| 1020 | public ComponentName getResolvedComponentName() { |
| 1021 | return new ComponentName(mResolveInfo.activityInfo.packageName, |
| 1022 | mResolveInfo.activityInfo.name); |
| 1023 | } |
| 1024 | |
| 1025 | @Override |
| 1026 | public boolean start(Activity activity, Bundle options) { |
| 1027 | activity.startActivity(mResolvedIntent, options); |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
| 1031 | @Override |
| 1032 | public boolean startAsCaller(Activity activity, Bundle options, int userId) { |
Dianne Hackborn | a7cfbe0 | 2015-07-16 10:52:52 -0700 | [diff] [blame] | 1033 | activity.startActivityAsCaller(mResolvedIntent, options, false, userId); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | @Override |
| 1038 | public boolean startAsUser(Activity activity, Bundle options, UserHandle user) { |
| 1039 | activity.startActivityAsUser(mResolvedIntent, options, user); |
| 1040 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | } |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1042 | |
| 1043 | @Override |
| 1044 | public boolean isPinned() { |
| 1045 | return mPinned; |
| 1046 | } |
| 1047 | |
| 1048 | public void setPinned(boolean pinned) { |
| 1049 | mPinned = pinned; |
| 1050 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1053 | /** |
| 1054 | * A single target as represented in the chooser. |
| 1055 | */ |
| 1056 | public interface TargetInfo { |
| 1057 | /** |
| 1058 | * Get the resolved intent that represents this target. Note that this may not be the |
| 1059 | * intent that will be launched by calling one of the <code>start</code> methods provided; |
| 1060 | * this is the intent that will be credited with the launch. |
| 1061 | * |
| 1062 | * @return the resolved intent for this target |
| 1063 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1064 | Intent getResolvedIntent(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1065 | |
| 1066 | /** |
| 1067 | * Get the resolved component name that represents this target. Note that this may not |
| 1068 | * be the component that will be directly launched by calling one of the <code>start</code> |
| 1069 | * methods provided; this is the component that will be credited with the launch. |
| 1070 | * |
| 1071 | * @return the resolved ComponentName for this target |
| 1072 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1073 | ComponentName getResolvedComponentName(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1074 | |
| 1075 | /** |
| 1076 | * Start the activity referenced by this target. |
| 1077 | * |
| 1078 | * @param activity calling Activity performing the launch |
| 1079 | * @param options ActivityOptions bundle |
| 1080 | * @return true if the start completed successfully |
| 1081 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1082 | boolean start(Activity activity, Bundle options); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1083 | |
| 1084 | /** |
| 1085 | * Start the activity referenced by this target as if the ResolverActivity's caller |
| 1086 | * was performing the start operation. |
| 1087 | * |
| 1088 | * @param activity calling Activity (actually) performing the launch |
| 1089 | * @param options ActivityOptions bundle |
| 1090 | * @param userId userId to start as or {@link UserHandle#USER_NULL} for activity's caller |
| 1091 | * @return true if the start completed successfully |
| 1092 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1093 | boolean startAsCaller(Activity activity, Bundle options, int userId); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1094 | |
| 1095 | /** |
| 1096 | * Start the activity referenced by this target as a given user. |
| 1097 | * |
| 1098 | * @param activity calling activity performing the launch |
| 1099 | * @param options ActivityOptions bundle |
| 1100 | * @param user handle for the user to start the activity as |
| 1101 | * @return true if the start completed successfully |
| 1102 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1103 | boolean startAsUser(Activity activity, Bundle options, UserHandle user); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1104 | |
| 1105 | /** |
| 1106 | * Return the ResolveInfo about how and why this target matched the original query |
| 1107 | * for available targets. |
| 1108 | * |
| 1109 | * @return ResolveInfo representing this target's match |
| 1110 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1111 | ResolveInfo getResolveInfo(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1112 | |
| 1113 | /** |
| 1114 | * Return the human-readable text label for this target. |
| 1115 | * |
| 1116 | * @return user-visible target label |
| 1117 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1118 | CharSequence getDisplayLabel(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1119 | |
| 1120 | /** |
| 1121 | * Return any extended info for this target. This may be used to disambiguate |
| 1122 | * otherwise identical targets. |
| 1123 | * |
| 1124 | * @return human-readable disambig string or null if none present |
| 1125 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1126 | CharSequence getExtendedInfo(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1127 | |
| 1128 | /** |
| 1129 | * @return The drawable that should be used to represent this target |
| 1130 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1131 | Drawable getDisplayIcon(); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1132 | |
| 1133 | /** |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1134 | * @return The (small) icon to badge the target with |
| 1135 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1136 | Drawable getBadgeIcon(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1137 | |
| 1138 | /** |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1139 | * @return The content description for the badge icon |
| 1140 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1141 | CharSequence getBadgeContentDescription(); |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1142 | |
| 1143 | /** |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1144 | * Clone this target with the given fill-in information. |
| 1145 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1146 | TargetInfo cloneFilledIn(Intent fillInIntent, int flags); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1147 | |
| 1148 | /** |
| 1149 | * @return the list of supported source intents deduped against this single target |
| 1150 | */ |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1151 | List<Intent> getAllSourceIntents(); |
| 1152 | |
| 1153 | /** |
| 1154 | * @return true if this target should be pinned to the front by the request of the user |
| 1155 | */ |
| 1156 | boolean isPinned(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1159 | public class ResolveListAdapter extends BaseAdapter { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1160 | private final List<Intent> mIntents; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1161 | private final Intent[] mInitialIntents; |
| 1162 | private final List<ResolveInfo> mBaseResolveList; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1163 | private ResolveInfo mLastChosen; |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1164 | private DisplayResolveInfo mOtherProfile; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1165 | private final int mLaunchedFromUid; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1166 | private boolean mHasExtendedInfo; |
| 1167 | |
| 1168 | protected final LayoutInflater mInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1170 | List<DisplayResolveInfo> mDisplayList; |
| 1171 | List<ResolvedComponentInfo> mOrigResolveList; |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 1172 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1173 | private int mLastChosenPosition = -1; |
| 1174 | private boolean mFilterLastUsed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1176 | public ResolveListAdapter(Context context, List<Intent> payloadIntents, |
| 1177 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
| 1178 | boolean filterLastUsed) { |
| 1179 | mIntents = payloadIntents; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1180 | mInitialIntents = initialIntents; |
| 1181 | mBaseResolveList = rList; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1182 | mLaunchedFromUid = launchedFromUid; |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 1183 | mInflater = LayoutInflater.from(context); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1184 | mDisplayList = new ArrayList<>(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1185 | mFilterLastUsed = filterLastUsed; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1186 | rebuildList(); |
| 1187 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1189 | public void handlePackagesChanged() { |
| 1190 | rebuildList(); |
| 1191 | notifyDataSetChanged(); |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1192 | if (getCount() == 0) { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1193 | // We no longer have any items... just finish the activity. |
| 1194 | finish(); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 1195 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1198 | public DisplayResolveInfo getFilteredItem() { |
| 1199 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 1200 | // Not using getItem since it offsets to dodge this position for the list |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1201 | return mDisplayList.get(mLastChosenPosition); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return null; |
| 1204 | } |
| 1205 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1206 | public DisplayResolveInfo getOtherProfile() { |
| 1207 | return mOtherProfile; |
| 1208 | } |
| 1209 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1210 | public int getFilteredPosition() { |
| 1211 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 1212 | return mLastChosenPosition; |
| 1213 | } |
| 1214 | return AbsListView.INVALID_POSITION; |
| 1215 | } |
| 1216 | |
| 1217 | public boolean hasFilteredItem() { |
| 1218 | return mFilterLastUsed && mLastChosenPosition >= 0; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1221 | public float getScore(DisplayResolveInfo target) { |
| 1222 | return mResolverComparator.getScore(target.getResolvedComponentName()); |
| 1223 | } |
| 1224 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1225 | private void rebuildList() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1226 | List<ResolvedComponentInfo> currentResolveList = null; |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1227 | |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1228 | try { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1229 | final Intent primaryIntent = getTargetIntent(); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1230 | mLastChosen = AppGlobals.getPackageManager().getLastChosenActivity( |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1231 | primaryIntent, primaryIntent.resolveTypeIfNeeded(getContentResolver()), |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1232 | PackageManager.MATCH_DEFAULT_ONLY); |
| 1233 | } catch (RemoteException re) { |
| 1234 | Log.d(TAG, "Error calling setLastChosenActivity\n" + re); |
| 1235 | } |
| 1236 | |
Sudheer Shanka | 7e64e10 | 2015-01-23 10:37:45 +0000 | [diff] [blame] | 1237 | // Clear the value of mOtherProfile from previous call. |
| 1238 | mOtherProfile = null; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1239 | mDisplayList.clear(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1240 | if (mBaseResolveList != null) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1241 | currentResolveList = mOrigResolveList = new ArrayList<>(); |
| 1242 | addResolveListDedupe(currentResolveList, getTargetIntent(), mBaseResolveList); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1243 | } else { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1244 | final boolean shouldGetResolvedFilter = shouldGetResolvedFilter(); |
| 1245 | final boolean shouldGetActivityMetadata = shouldGetActivityMetadata(); |
| 1246 | for (int i = 0, N = mIntents.size(); i < N; i++) { |
| 1247 | final Intent intent = mIntents.get(i); |
| 1248 | final List<ResolveInfo> infos = mPm.queryIntentActivities(intent, |
| 1249 | PackageManager.MATCH_DEFAULT_ONLY |
| 1250 | | (shouldGetResolvedFilter ? PackageManager.GET_RESOLVED_FILTER : 0) |
| 1251 | | (shouldGetActivityMetadata ? PackageManager.GET_META_DATA : 0)); |
| 1252 | if (infos != null) { |
| 1253 | if (currentResolveList == null) { |
| 1254 | currentResolveList = mOrigResolveList = new ArrayList<>(); |
| 1255 | } |
| 1256 | addResolveListDedupe(currentResolveList, intent, infos); |
| 1257 | } |
| 1258 | } |
| 1259 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1260 | // Filter out any activities that the launched uid does not |
Nicolas Prevot | 697397a | 2016-01-11 11:14:32 +0000 | [diff] [blame] | 1261 | // have permission for. |
| 1262 | // Also filter out those that are suspended because they couldn't |
| 1263 | // be started. We don't do this when we have an explicit |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1264 | // list of resolved activities, because that only happens when |
| 1265 | // we are being subclassed, so we can safely launch whatever |
| 1266 | // they gave us. |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1267 | if (currentResolveList != null) { |
| 1268 | for (int i=currentResolveList.size()-1; i >= 0; i--) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1269 | ActivityInfo ai = currentResolveList.get(i) |
| 1270 | .getResolveInfoAt(0).activityInfo; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1271 | int granted = ActivityManager.checkComponentPermission( |
| 1272 | ai.permission, mLaunchedFromUid, |
| 1273 | ai.applicationInfo.uid, ai.exported); |
Nicolas Prevot | 697397a | 2016-01-11 11:14:32 +0000 | [diff] [blame] | 1274 | boolean suspended = (ai.applicationInfo.flags |
| 1275 | & ApplicationInfo.FLAG_SUSPENDED) != 0; |
| 1276 | if (granted != PackageManager.PERMISSION_GRANTED || suspended) { |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1277 | // Access not allowed! |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 1278 | if (mOrigResolveList == currentResolveList) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1279 | mOrigResolveList = new ArrayList<>(mOrigResolveList); |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 1280 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1281 | currentResolveList.remove(i); |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | } |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 1285 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | int N; |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1287 | if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | // Only display the first matches that are either of equal |
| 1289 | // priority or have asked to be default options. |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1290 | ResolvedComponentInfo rci0 = currentResolveList.get(0); |
| 1291 | ResolveInfo r0 = rci0.getResolveInfoAt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | for (int i=1; i<N; i++) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1293 | ResolveInfo ri = currentResolveList.get(i).getResolveInfoAt(0); |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1294 | if (DEBUG) Log.v( |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1295 | TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | r0.activityInfo.name + "=" + |
| 1297 | r0.priority + "/" + r0.isDefault + " vs " + |
| 1298 | ri.activityInfo.name + "=" + |
| 1299 | ri.priority + "/" + ri.isDefault); |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1300 | if (r0.priority != ri.priority || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | r0.isDefault != ri.isDefault) { |
| 1302 | while (i < N) { |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 1303 | if (mOrigResolveList == currentResolveList) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1304 | mOrigResolveList = new ArrayList<>(mOrigResolveList); |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 1305 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1306 | currentResolveList.remove(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | N--; |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | if (N > 1) { |
Adam Powell | d25267c | 2015-06-05 18:02:21 -0700 | [diff] [blame] | 1312 | mResolverComparator.compute(currentResolveList); |
| 1313 | Collections.sort(currentResolveList, mResolverComparator); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1315 | // First put the initial items at the top. |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1316 | if (mInitialIntents != null) { |
| 1317 | for (int i=0; i<mInitialIntents.length; i++) { |
| 1318 | Intent ii = mInitialIntents[i]; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1319 | if (ii == null) { |
| 1320 | continue; |
| 1321 | } |
| 1322 | ActivityInfo ai = ii.resolveActivityInfo( |
| 1323 | getPackageManager(), 0); |
| 1324 | if (ai == null) { |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1325 | Log.w(TAG, "No activity found for " + ii); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1326 | continue; |
| 1327 | } |
| 1328 | ResolveInfo ri = new ResolveInfo(); |
| 1329 | ri.activityInfo = ai; |
Nicolas Prevot | 1a81592 | 2014-10-10 16:22:38 +0100 | [diff] [blame] | 1330 | UserManager userManager = |
| 1331 | (UserManager) getSystemService(Context.USER_SERVICE); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1332 | if (ii instanceof LabeledIntent) { |
| 1333 | LabeledIntent li = (LabeledIntent)ii; |
| 1334 | ri.resolvePackageName = li.getSourcePackage(); |
| 1335 | ri.labelRes = li.getLabelResource(); |
| 1336 | ri.nonLocalizedLabel = li.getNonLocalizedLabel(); |
| 1337 | ri.icon = li.getIconResource(); |
Sudheer Shanka | 9ded760 | 2015-05-19 21:17:25 +0100 | [diff] [blame] | 1338 | ri.iconResourceId = ri.icon; |
| 1339 | } |
| 1340 | if (userManager.isManagedProfile()) { |
| 1341 | ri.noResourceId = true; |
| 1342 | ri.icon = 0; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1343 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1344 | addResolveInfo(new DisplayResolveInfo(ii, ri, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1345 | ri.loadLabel(getPackageManager()), null, ii)); |
| 1346 | } |
| 1347 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1348 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | // Check for applications with same name and use application name or |
| 1350 | // package name if necessary |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1351 | rci0 = currentResolveList.get(0); |
| 1352 | r0 = rci0.getResolveInfoAt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | int start = 0; |
| 1354 | CharSequence r0Label = r0.loadLabel(mPm); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1355 | mHasExtendedInfo = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | for (int i = 1; i < N; i++) { |
| 1357 | if (r0Label == null) { |
| 1358 | r0Label = r0.activityInfo.packageName; |
| 1359 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1360 | ResolvedComponentInfo rci = currentResolveList.get(i); |
| 1361 | ResolveInfo ri = rci.getResolveInfoAt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1362 | CharSequence riLabel = ri.loadLabel(mPm); |
| 1363 | if (riLabel == null) { |
| 1364 | riLabel = ri.activityInfo.packageName; |
| 1365 | } |
| 1366 | if (riLabel.equals(r0Label)) { |
| 1367 | continue; |
| 1368 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1369 | processGroup(currentResolveList, start, (i-1), rci0, r0Label); |
| 1370 | rci0 = rci; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1371 | r0 = ri; |
| 1372 | r0Label = riLabel; |
| 1373 | start = i; |
| 1374 | } |
| 1375 | // Process last group |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1376 | processGroup(currentResolveList, start, (N-1), rci0, r0Label); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | } |
Kenny Guy | b8a2df1 | 2014-12-12 16:56:08 +0000 | [diff] [blame] | 1378 | |
| 1379 | // Layout doesn't handle both profile button and last chosen |
| 1380 | // so disable last chosen if profile button is present. |
| 1381 | if (mOtherProfile != null && mLastChosenPosition >= 0) { |
| 1382 | mLastChosenPosition = -1; |
| 1383 | mFilterLastUsed = false; |
| 1384 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1385 | |
| 1386 | onListRebuilt(); |
| 1387 | } |
| 1388 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1389 | private void addResolveListDedupe(List<ResolvedComponentInfo> into, Intent intent, |
| 1390 | List<ResolveInfo> from) { |
| 1391 | final int fromCount = from.size(); |
| 1392 | final int intoCount = into.size(); |
| 1393 | for (int i = 0; i < fromCount; i++) { |
| 1394 | final ResolveInfo newInfo = from.get(i); |
| 1395 | boolean found = false; |
| 1396 | // Only loop to the end of into as it was before we started; no dupes in from. |
| 1397 | for (int j = 0; j < intoCount; j++) { |
| 1398 | final ResolvedComponentInfo rci = into.get(i); |
| 1399 | if (isSameResolvedComponent(newInfo, rci)) { |
| 1400 | found = true; |
| 1401 | rci.add(intent, newInfo); |
| 1402 | break; |
| 1403 | } |
| 1404 | } |
| 1405 | if (!found) { |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1406 | final ComponentName name = new ComponentName( |
| 1407 | newInfo.activityInfo.packageName, newInfo.activityInfo.name); |
| 1408 | final ResolvedComponentInfo rci = new ResolvedComponentInfo(name, |
| 1409 | intent, newInfo); |
| 1410 | rci.setPinned(isComponentPinned(name)); |
| 1411 | into.add(rci); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | private boolean isSameResolvedComponent(ResolveInfo a, ResolvedComponentInfo b) { |
| 1417 | final ActivityInfo ai = a.activityInfo; |
| 1418 | return ai.packageName.equals(b.name.getPackageName()) |
| 1419 | && ai.name.equals(b.name.getClassName()); |
| 1420 | } |
| 1421 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1422 | public void onListRebuilt() { |
| 1423 | // This space for rent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
Adam Powell | c6d5e3a | 2015-04-23 12:22:20 -0700 | [diff] [blame] | 1426 | public boolean shouldGetResolvedFilter() { |
| 1427 | return mFilterLastUsed; |
| 1428 | } |
| 1429 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1430 | private void processGroup(List<ResolvedComponentInfo> rList, int start, int end, |
| 1431 | ResolvedComponentInfo ro, CharSequence roLabel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | // Process labels from start to i |
| 1433 | int num = end - start+1; |
| 1434 | if (num == 1) { |
| 1435 | // No duplicate labels. Use label for entry at start |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1436 | addResolveInfoWithAlternates(ro, null, roLabel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1437 | } else { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1438 | mHasExtendedInfo = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | boolean usePkg = false; |
Adam Powell | 00f4aad | 2015-09-17 13:38:16 -0700 | [diff] [blame] | 1440 | final ApplicationInfo ai = ro.getResolveInfoAt(0).activityInfo.applicationInfo; |
| 1441 | final CharSequence startApp = ai.loadLabel(mPm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | if (startApp == null) { |
| 1443 | usePkg = true; |
| 1444 | } |
| 1445 | if (!usePkg) { |
| 1446 | // Use HashSet to track duplicates |
| 1447 | HashSet<CharSequence> duplicates = |
| 1448 | new HashSet<CharSequence>(); |
| 1449 | duplicates.add(startApp); |
| 1450 | for (int j = start+1; j <= end ; j++) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1451 | ResolveInfo jRi = rList.get(j).getResolveInfoAt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm); |
| 1453 | if ( (jApp == null) || (duplicates.contains(jApp))) { |
| 1454 | usePkg = true; |
| 1455 | break; |
| 1456 | } else { |
| 1457 | duplicates.add(jApp); |
| 1458 | } |
| 1459 | } |
| 1460 | // Clear HashSet for later use |
| 1461 | duplicates.clear(); |
| 1462 | } |
| 1463 | for (int k = start; k <= end; k++) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1464 | final ResolvedComponentInfo rci = rList.get(k); |
| 1465 | final ResolveInfo add = rci.getResolveInfoAt(0); |
| 1466 | final CharSequence extraInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | if (usePkg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | // Use package name for all entries from start to end-1 |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1469 | extraInfo = add.activityInfo.packageName; |
| 1470 | } else { |
| 1471 | // Use application name for all entries from start to end-1 |
| 1472 | extraInfo = add.activityInfo.applicationInfo.loadLabel(mPm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1474 | addResolveInfoWithAlternates(rci, extraInfo, roLabel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1479 | private void addResolveInfoWithAlternates(ResolvedComponentInfo rci, |
| 1480 | CharSequence extraInfo, CharSequence roLabel) { |
| 1481 | final int count = rci.getCount(); |
| 1482 | final Intent intent = rci.getIntentAt(0); |
| 1483 | final ResolveInfo add = rci.getResolveInfoAt(0); |
| 1484 | final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent); |
| 1485 | final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel, |
| 1486 | extraInfo, replaceIntent); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1487 | dri.setPinned(rci.isPinned()); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1488 | addResolveInfo(dri); |
| 1489 | if (replaceIntent == intent) { |
| 1490 | // Only add alternates if we didn't get a specific replacement from |
| 1491 | // the caller. If we have one it trumps potential alternates. |
| 1492 | for (int i = 1, N = count; i < N; i++) { |
| 1493 | final Intent altIntent = rci.getIntentAt(i); |
| 1494 | dri.addAlternateSourceIntent(altIntent); |
| 1495 | } |
| 1496 | } |
| 1497 | updateLastChosenPosition(add); |
| 1498 | } |
| 1499 | |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1500 | private void updateLastChosenPosition(ResolveInfo info) { |
| 1501 | if (mLastChosen != null |
| 1502 | && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName) |
| 1503 | && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1504 | mLastChosenPosition = mDisplayList.size() - 1; |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1508 | private void addResolveInfo(DisplayResolveInfo dri) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1509 | if (dri.mResolveInfo.targetUserId != UserHandle.USER_CURRENT && mOtherProfile == null) { |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1510 | // So far we only support a single other profile at a time. |
| 1511 | // The first one we see gets special treatment. |
| 1512 | mOtherProfile = dri; |
| 1513 | } else { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1514 | mDisplayList.add(dri); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1518 | public ResolveInfo resolveInfoForPosition(int position, boolean filtered) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1519 | return (filtered ? getItem(position) : mDisplayList.get(position)) |
| 1520 | .getResolveInfo(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1523 | public TargetInfo targetInfoForPosition(int position, boolean filtered) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1524 | return filtered ? getItem(position) : mDisplayList.get(position); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | public int getCount() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1528 | int result = mDisplayList.size(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1529 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 1530 | result--; |
| 1531 | } |
| 1532 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1533 | } |
| 1534 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1535 | public int getUnfilteredCount() { |
| 1536 | return mDisplayList.size(); |
| 1537 | } |
| 1538 | |
| 1539 | public int getDisplayInfoCount() { |
| 1540 | return mDisplayList.size(); |
| 1541 | } |
| 1542 | |
| 1543 | public DisplayResolveInfo getDisplayInfoAt(int index) { |
| 1544 | return mDisplayList.get(index); |
| 1545 | } |
| 1546 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1547 | public TargetInfo getItem(int position) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1548 | if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) { |
| 1549 | position++; |
| 1550 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1551 | return mDisplayList.get(position); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | public long getItemId(int position) { |
| 1555 | return position; |
| 1556 | } |
| 1557 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1558 | public boolean hasExtendedInfo() { |
| 1559 | return mHasExtendedInfo; |
| 1560 | } |
| 1561 | |
| 1562 | public boolean hasResolvedTarget(ResolveInfo info) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1563 | for (int i = 0, N = mDisplayList.size(); i < N; i++) { |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1564 | if (resolveInfoMatch(info, mDisplayList.get(i).getResolveInfo())) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1565 | return true; |
| 1566 | } |
| 1567 | } |
| 1568 | return false; |
| 1569 | } |
| 1570 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1571 | public int getDisplayResolveInfoCount() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1572 | return mDisplayList.size(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1575 | public DisplayResolveInfo getDisplayResolveInfo(int index) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1576 | // Used to query services. We only query services for primary targets, not alternates. |
| 1577 | return mDisplayList.get(index); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | public final View getView(int position, View convertView, ViewGroup parent) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 1581 | View view = convertView; |
| 1582 | if (view == null) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1583 | view = createView(parent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1585 | onBindView(view, getItem(position)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | return view; |
| 1587 | } |
| 1588 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1589 | public final View createView(ViewGroup parent) { |
| 1590 | final View view = onCreateView(parent); |
| 1591 | final ViewHolder holder = new ViewHolder(view); |
| 1592 | view.setTag(holder); |
| 1593 | return view; |
| 1594 | } |
| 1595 | |
| 1596 | public View onCreateView(ViewGroup parent) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1597 | return mInflater.inflate( |
| 1598 | com.android.internal.R.layout.resolve_list_item, parent, false); |
| 1599 | } |
| 1600 | |
| 1601 | public boolean showsExtendedInfo(TargetInfo info) { |
| 1602 | return !TextUtils.isEmpty(info.getExtendedInfo()); |
| 1603 | } |
| 1604 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1605 | public boolean isComponentPinned(ComponentName name) { |
| 1606 | return false; |
| 1607 | } |
| 1608 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1609 | public final void bindView(int position, View view) { |
| 1610 | onBindView(view, getItem(position)); |
| 1611 | } |
| 1612 | |
| 1613 | private void onBindView(View view, TargetInfo info) { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1614 | final ViewHolder holder = (ViewHolder) view.getTag(); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 1615 | final CharSequence label = info.getDisplayLabel(); |
| 1616 | if (!TextUtils.equals(holder.text.getText(), label)) { |
| 1617 | holder.text.setText(info.getDisplayLabel()); |
| 1618 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1619 | if (showsExtendedInfo(info)) { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1620 | holder.text2.setVisibility(View.VISIBLE); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1621 | holder.text2.setText(info.getExtendedInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | } else { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1623 | holder.text2.setVisibility(View.GONE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1625 | if (info instanceof DisplayResolveInfo |
| 1626 | && !((DisplayResolveInfo) info).hasDisplayIcon()) { |
| 1627 | new LoadAdapterIconTask((DisplayResolveInfo) info).execute(); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1628 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1629 | holder.icon.setImageDrawable(info.getDisplayIcon()); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1630 | if (holder.badge != null) { |
| 1631 | final Drawable badge = info.getBadgeIcon(); |
| 1632 | if (badge != null) { |
| 1633 | holder.badge.setImageDrawable(badge); |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1634 | holder.badge.setContentDescription(info.getBadgeContentDescription()); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1635 | holder.badge.setVisibility(View.VISIBLE); |
| 1636 | } else { |
| 1637 | holder.badge.setVisibility(View.GONE); |
| 1638 | } |
| 1639 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1643 | static final class ResolvedComponentInfo { |
| 1644 | public final ComponentName name; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1645 | private boolean mPinned; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1646 | private final List<Intent> mIntents = new ArrayList<>(); |
| 1647 | private final List<ResolveInfo> mResolveInfos = new ArrayList<>(); |
| 1648 | |
| 1649 | public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) { |
| 1650 | this.name = name; |
| 1651 | add(intent, info); |
| 1652 | } |
| 1653 | |
| 1654 | public void add(Intent intent, ResolveInfo info) { |
| 1655 | mIntents.add(intent); |
| 1656 | mResolveInfos.add(info); |
| 1657 | } |
| 1658 | |
| 1659 | public int getCount() { |
| 1660 | return mIntents.size(); |
| 1661 | } |
| 1662 | |
| 1663 | public Intent getIntentAt(int index) { |
| 1664 | return index >= 0 ? mIntents.get(index) : null; |
| 1665 | } |
| 1666 | |
| 1667 | public ResolveInfo getResolveInfoAt(int index) { |
| 1668 | return index >= 0 ? mResolveInfos.get(index) : null; |
| 1669 | } |
| 1670 | |
| 1671 | public int findIntent(Intent intent) { |
| 1672 | for (int i = 0, N = mIntents.size(); i < N; i++) { |
| 1673 | if (intent.equals(mIntents.get(i))) { |
| 1674 | return i; |
| 1675 | } |
| 1676 | } |
| 1677 | return -1; |
| 1678 | } |
| 1679 | |
| 1680 | public int findResolveInfo(ResolveInfo info) { |
| 1681 | for (int i = 0, N = mResolveInfos.size(); i < N; i++) { |
| 1682 | if (info.equals(mResolveInfos.get(i))) { |
| 1683 | return i; |
| 1684 | } |
| 1685 | } |
| 1686 | return -1; |
| 1687 | } |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1688 | |
| 1689 | public boolean isPinned() { |
| 1690 | return mPinned; |
| 1691 | } |
| 1692 | |
| 1693 | public void setPinned(boolean pinned) { |
| 1694 | mPinned = pinned; |
| 1695 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1698 | static class ViewHolder { |
| 1699 | public TextView text; |
| 1700 | public TextView text2; |
| 1701 | public ImageView icon; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1702 | public ImageView badge; |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1703 | |
| 1704 | public ViewHolder(View view) { |
| 1705 | text = (TextView) view.findViewById(com.android.internal.R.id.text1); |
| 1706 | text2 = (TextView) view.findViewById(com.android.internal.R.id.text2); |
| 1707 | icon = (ImageView) view.findViewById(R.id.icon); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1708 | badge = (ImageView) view.findViewById(R.id.target_badge); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1712 | class ItemClickListener implements AdapterView.OnItemClickListener, |
| 1713 | AdapterView.OnItemLongClickListener { |
| 1714 | @Override |
| 1715 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 1716 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 1717 | if (listView != null) { |
| 1718 | position -= listView.getHeaderViewsCount(); |
| 1719 | } |
| 1720 | if (position < 0) { |
| 1721 | // Header views don't count. |
| 1722 | return; |
| 1723 | } |
| 1724 | final int checkedPos = mAdapterView.getCheckedItemPosition(); |
| 1725 | final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION; |
| 1726 | if (mAlwaysUseOption && (!hasValidSelection || mLastSelected != checkedPos)) { |
| 1727 | setAlwaysButtonEnabled(hasValidSelection, checkedPos, true); |
| 1728 | mOnceButton.setEnabled(hasValidSelection); |
| 1729 | if (hasValidSelection) { |
| 1730 | mAdapterView.smoothScrollToPosition(checkedPos); |
| 1731 | } |
| 1732 | mLastSelected = checkedPos; |
| 1733 | } else { |
| 1734 | startSelected(position, false, true); |
| 1735 | } |
| 1736 | } |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 1737 | |
| 1738 | @Override |
| 1739 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1740 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 1741 | if (listView != null) { |
| 1742 | position -= listView.getHeaderViewsCount(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1743 | } |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 1744 | if (position < 0) { |
| 1745 | // Header views don't count. |
| 1746 | return false; |
| 1747 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1748 | ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1749 | showTargetDetails(ri); |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1754 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1755 | abstract class LoadIconTask extends AsyncTask<Void, Void, Drawable> { |
| 1756 | protected final DisplayResolveInfo mDisplayResolveInfo; |
| 1757 | private final ResolveInfo mResolveInfo; |
| 1758 | |
| 1759 | public LoadIconTask(DisplayResolveInfo dri) { |
| 1760 | mDisplayResolveInfo = dri; |
| 1761 | mResolveInfo = dri.getResolveInfo(); |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | @Override |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1765 | protected Drawable doInBackground(Void... params) { |
| 1766 | return loadIconForResolveInfo(mResolveInfo); |
| 1767 | } |
| 1768 | |
| 1769 | @Override |
| 1770 | protected void onPostExecute(Drawable d) { |
| 1771 | mDisplayResolveInfo.setDisplayIcon(d); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | class LoadAdapterIconTask extends LoadIconTask { |
| 1776 | public LoadAdapterIconTask(DisplayResolveInfo dri) { |
| 1777 | super(dri); |
| 1778 | } |
| 1779 | |
| 1780 | @Override |
| 1781 | protected void onPostExecute(Drawable d) { |
| 1782 | super.onPostExecute(d); |
| 1783 | if (mProfileView != null && mAdapter.getOtherProfile() == mDisplayResolveInfo) { |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1784 | bindProfileView(); |
| 1785 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1786 | mAdapter.notifyDataSetChanged(); |
| 1787 | } |
| 1788 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1789 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1790 | class LoadIconIntoViewTask extends LoadIconTask { |
| 1791 | private final ImageView mTargetView; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1792 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1793 | public LoadIconIntoViewTask(DisplayResolveInfo dri, ImageView target) { |
| 1794 | super(dri); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1795 | mTargetView = target; |
| 1796 | } |
| 1797 | |
| 1798 | @Override |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1799 | protected void onPostExecute(Drawable d) { |
| 1800 | super.onPostExecute(d); |
| 1801 | mTargetView.setImageDrawable(d); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1802 | } |
| 1803 | } |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1804 | |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 1805 | static final boolean isSpecificUriMatch(int match) { |
| 1806 | match = match&IntentFilter.MATCH_CATEGORY_MASK; |
| 1807 | return match >= IntentFilter.MATCH_CATEGORY_HOST |
| 1808 | && match <= IntentFilter.MATCH_CATEGORY_PATH; |
| 1809 | } |
| 1810 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 1811 | static class PickTargetOptionRequest extends PickOptionRequest { |
| 1812 | public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options, |
| 1813 | @Nullable Bundle extras) { |
| 1814 | super(prompt, options, extras); |
| 1815 | } |
| 1816 | |
| 1817 | @Override |
| 1818 | public void onCancel() { |
| 1819 | super.onCancel(); |
| 1820 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 1821 | if (ra != null) { |
| 1822 | ra.mPickOptionRequest = null; |
| 1823 | ra.finish(); |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | @Override |
| 1828 | public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) { |
| 1829 | super.onPickOptionResult(finished, selections, result); |
| 1830 | if (selections.length != 1) { |
| 1831 | // TODO In a better world we would filter the UI presented here and let the |
| 1832 | // user refine. Maybe later. |
| 1833 | return; |
| 1834 | } |
| 1835 | |
| 1836 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 1837 | if (ra != null) { |
| 1838 | final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex()); |
| 1839 | if (ra.onTargetSelected(ti, false)) { |
| 1840 | ra.mPickOptionRequest = null; |
| 1841 | ra.finish(); |
| 1842 | } |
| 1843 | } |
| 1844 | } |
| 1845 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1846 | } |