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