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