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 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 290 | final ResolverDrawerLayout rdl = findViewById(R.id.contentPanel); |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 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 | } |
Kang Li | 38a6da64 | 2017-04-05 12:30:55 -0700 | [diff] [blame] | 533 | if (mAdapter != null && mAdapter.mResolverListController != null) { |
| 534 | mAdapter.mResolverListController.destroy(); |
| 535 | } |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | @Override |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 539 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 540 | super.onRestoreInstanceState(savedInstanceState); |
Hakan Seyalioglu | 5dbc819 | 2017-02-24 16:16:37 -0800 | [diff] [blame] | 541 | resetAlwaysOrOnceButtonBar(); |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 544 | private boolean hasManagedProfile() { |
| 545 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 546 | if (userManager == null) { |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | try { |
| 551 | List<UserInfo> profiles = userManager.getProfiles(getUserId()); |
| 552 | for (UserInfo userInfo : profiles) { |
| 553 | if (userInfo != null && userInfo.isManagedProfile()) { |
| 554 | return true; |
| 555 | } |
| 556 | } |
| 557 | } catch (SecurityException e) { |
| 558 | return false; |
| 559 | } |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | private boolean supportsManagedProfiles(ResolveInfo resolveInfo) { |
| 564 | try { |
| 565 | ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| 566 | resolveInfo.activityInfo.packageName, 0 /* default flags */); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 567 | return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 568 | } catch (NameNotFoundException e) { |
| 569 | return false; |
| 570 | } |
| 571 | } |
| 572 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 573 | private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos, |
| 574 | boolean filtered) { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 575 | boolean enabled = false; |
| 576 | if (hasValidSelection) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 577 | ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered); |
Hakan Seyalioglu | 5dbc819 | 2017-02-24 16:16:37 -0800 | [diff] [blame] | 578 | if (ri == null) { |
| 579 | Log.e(TAG, "Invalid position supplied to setAlwaysButtonEnabled"); |
| 580 | return; |
| 581 | } else if (ri.targetUserId != UserHandle.USER_CURRENT) { |
| 582 | Log.e(TAG, "Attempted to set selection to resolve info for another user"); |
| 583 | return; |
| 584 | } else { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 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 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 925 | mAdapterView = findViewById(R.id.resolver_list); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 926 | |
| 927 | if (count == 0 && mAdapter.mPlaceholderCount == 0) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 928 | final TextView emptyView = findViewById(R.id.empty); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 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) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 962 | final TextView titleView = findViewById(R.id.title); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 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)) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 973 | final TextView titleView = findViewById(R.id.title); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 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 |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 980 | final ImageView titleIcon = findViewById(R.id.title_icon); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 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 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 997 | final ImageView iconView = findViewById(R.id.icon); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 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) { |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 1006 | final ViewGroup buttonLayout = findViewById(R.id.button_bar); |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 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 | |
Sumir Kataria | db688af | 2017-05-10 17:33:47 -0700 | [diff] [blame] | 1344 | public int getPlaceholderCount() { return mPlaceholderCount; } |
| 1345 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1346 | @Nullable |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1347 | public DisplayResolveInfo getFilteredItem() { |
| 1348 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 1349 | // 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] | 1350 | return mDisplayList.get(mLastChosenPosition); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1351 | } |
| 1352 | return null; |
| 1353 | } |
| 1354 | |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1355 | public DisplayResolveInfo getOtherProfile() { |
| 1356 | return mOtherProfile; |
| 1357 | } |
| 1358 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1359 | public int getFilteredPosition() { |
| 1360 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 1361 | return mLastChosenPosition; |
| 1362 | } |
| 1363 | return AbsListView.INVALID_POSITION; |
| 1364 | } |
| 1365 | |
| 1366 | public boolean hasFilteredItem() { |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1367 | return mFilterLastUsed && mLastChosen != null; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1370 | public float getScore(DisplayResolveInfo target) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1371 | return mResolverListController.getScore(target); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Kang Li | 0cef910d | 2017-01-05 09:14:36 -0800 | [diff] [blame] | 1374 | public void updateModel(ComponentName componentName) { |
| 1375 | mResolverListController.updateModel(componentName); |
| 1376 | } |
| 1377 | |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1378 | public void updateChooserCounts(String packageName, int userId, String action) { |
| 1379 | mResolverListController.updateChooserCounts(packageName, userId, action); |
| 1380 | } |
| 1381 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1382 | /** |
| 1383 | * Rebuild the list of resolvers. In some cases some parts will need some asynchronous work |
| 1384 | * to complete. |
| 1385 | * |
| 1386 | * @return Whether or not the list building is completed. |
| 1387 | */ |
| 1388 | protected boolean rebuildList() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1389 | List<ResolvedComponentInfo> currentResolveList = null; |
Sudheer Shanka | 7e64e10 | 2015-01-23 10:37:45 +0000 | [diff] [blame] | 1390 | // Clear the value of mOtherProfile from previous call. |
| 1391 | mOtherProfile = null; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1392 | mLastChosen = null; |
| 1393 | mLastChosenPosition = -1; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1394 | mDisplayList.clear(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1395 | if (mBaseResolveList != null) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1396 | currentResolveList = mUnfilteredResolveList = new ArrayList<>(); |
| 1397 | mResolverListController.addResolveListDedupe(currentResolveList, |
| 1398 | getTargetIntent(), |
| 1399 | mBaseResolveList); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1400 | } else { |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1401 | currentResolveList = mUnfilteredResolveList = |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1402 | mResolverListController.getResolversForIntent(shouldGetResolvedFilter(), |
| 1403 | shouldGetActivityMetadata(), |
| 1404 | mIntents); |
| 1405 | if (currentResolveList == null) { |
| 1406 | processSortedList(currentResolveList); |
| 1407 | return true; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1408 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1409 | List<ResolvedComponentInfo> originalList = |
| 1410 | mResolverListController.filterIneligibleActivities(currentResolveList, |
| 1411 | true); |
| 1412 | if (originalList != null) { |
| 1413 | mUnfilteredResolveList = originalList; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 1414 | } |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 1415 | } |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1416 | |
| 1417 | // So far we only support a single other profile at a time. |
| 1418 | // The first one we see gets special treatment. |
| 1419 | for (ResolvedComponentInfo info : currentResolveList) { |
| 1420 | if (info.getResolveInfoAt(0).targetUserId != UserHandle.USER_CURRENT) { |
| 1421 | mOtherProfile = new DisplayResolveInfo(info.getIntentAt(0), |
| 1422 | info.getResolveInfoAt(0), |
| 1423 | info.getResolveInfoAt(0).loadLabel(mPm), |
| 1424 | info.getResolveInfoAt(0).loadLabel(mPm), |
| 1425 | getReplacementIntent(info.getResolveInfoAt(0).activityInfo, |
| 1426 | info.getIntentAt(0))); |
| 1427 | currentResolveList.remove(info); |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | if (mOtherProfile == null) { |
| 1433 | try { |
| 1434 | mLastChosen = mResolverListController.getLastChosen(); |
| 1435 | } catch (RemoteException re) { |
| 1436 | Log.d(TAG, "Error calling getLastChosenActivity\n" + re); |
| 1437 | } |
| 1438 | } |
| 1439 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | int N; |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1441 | if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1442 | // We only care about fixing the unfilteredList if the current resolve list and |
| 1443 | // current resolve list are currently the same. |
| 1444 | List<ResolvedComponentInfo> originalList = |
| 1445 | mResolverListController.filterLowPriority(currentResolveList, |
| 1446 | mUnfilteredResolveList == currentResolveList); |
| 1447 | if (originalList != null) { |
| 1448 | mUnfilteredResolveList = originalList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1449 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1450 | |
Hakan Seyalioglu | 873cbfd | 2017-02-21 19:23:43 -0800 | [diff] [blame] | 1451 | if (currentResolveList.size() > 1) { |
Sumir Kataria | db688af | 2017-05-10 17:33:47 -0700 | [diff] [blame] | 1452 | int placeholderCount = currentResolveList.size(); |
| 1453 | if (useLayoutWithDefault()) { |
| 1454 | --placeholderCount; |
| 1455 | } |
| 1456 | setPlaceholderCount(placeholderCount); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1457 | AsyncTask<List<ResolvedComponentInfo>, |
| 1458 | Void, |
| 1459 | List<ResolvedComponentInfo>> sortingTask = |
| 1460 | new AsyncTask<List<ResolvedComponentInfo>, |
| 1461 | Void, |
| 1462 | List<ResolvedComponentInfo>>() { |
| 1463 | @Override |
| 1464 | protected List<ResolvedComponentInfo> doInBackground( |
| 1465 | List<ResolvedComponentInfo>... params) { |
| 1466 | mResolverListController.sort(params[0]); |
| 1467 | return params[0]; |
| 1468 | } |
| 1469 | |
| 1470 | @Override |
| 1471 | protected void onPostExecute(List<ResolvedComponentInfo> sortedComponents) { |
| 1472 | processSortedList(sortedComponents); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1473 | if (mProfileView != null) { |
| 1474 | bindProfileView(); |
| 1475 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1476 | notifyDataSetChanged(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1477 | } |
| 1478 | }; |
| 1479 | sortingTask.execute(currentResolveList); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1480 | postListReadyRunnable(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1481 | return false; |
| 1482 | } else { |
| 1483 | processSortedList(currentResolveList); |
| 1484 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1486 | } else { |
| 1487 | processSortedList(currentResolveList); |
| 1488 | return true; |
| 1489 | } |
| 1490 | } |
| 1491 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1492 | private void processSortedList(List<ResolvedComponentInfo> sortedComponents) { |
| 1493 | int N; |
| 1494 | if (sortedComponents != null && (N = sortedComponents.size()) != 0) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1495 | // First put the initial items at the top. |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1496 | if (mInitialIntents != null) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1497 | for (int i = 0; i < mInitialIntents.length; i++) { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 1498 | Intent ii = mInitialIntents[i]; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1499 | if (ii == null) { |
| 1500 | continue; |
| 1501 | } |
| 1502 | ActivityInfo ai = ii.resolveActivityInfo( |
| 1503 | getPackageManager(), 0); |
| 1504 | if (ai == null) { |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1505 | Log.w(TAG, "No activity found for " + ii); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1506 | continue; |
| 1507 | } |
| 1508 | ResolveInfo ri = new ResolveInfo(); |
| 1509 | ri.activityInfo = ai; |
Nicolas Prevot | 1a81592 | 2014-10-10 16:22:38 +0100 | [diff] [blame] | 1510 | UserManager userManager = |
| 1511 | (UserManager) getSystemService(Context.USER_SERVICE); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1512 | if (ii instanceof LabeledIntent) { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1513 | LabeledIntent li = (LabeledIntent) ii; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1514 | ri.resolvePackageName = li.getSourcePackage(); |
| 1515 | ri.labelRes = li.getLabelResource(); |
| 1516 | ri.nonLocalizedLabel = li.getNonLocalizedLabel(); |
| 1517 | ri.icon = li.getIconResource(); |
Sudheer Shanka | 9ded760 | 2015-05-19 21:17:25 +0100 | [diff] [blame] | 1518 | ri.iconResourceId = ri.icon; |
| 1519 | } |
| 1520 | if (userManager.isManagedProfile()) { |
| 1521 | ri.noResourceId = true; |
| 1522 | ri.icon = 0; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1523 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1524 | addResolveInfo(new DisplayResolveInfo(ii, ri, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1525 | ri.loadLabel(getPackageManager()), null, ii)); |
| 1526 | } |
| 1527 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 1528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | // Check for applications with same name and use application name or |
| 1530 | // package name if necessary |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1531 | ResolvedComponentInfo rci0 = sortedComponents.get(0); |
| 1532 | ResolveInfo r0 = rci0.getResolveInfoAt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1533 | int start = 0; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1534 | CharSequence r0Label = r0.loadLabel(mPm); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1535 | mHasExtendedInfo = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | for (int i = 1; i < N; i++) { |
| 1537 | if (r0Label == null) { |
| 1538 | r0Label = r0.activityInfo.packageName; |
| 1539 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1540 | ResolvedComponentInfo rci = sortedComponents.get(i); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1541 | ResolveInfo ri = rci.getResolveInfoAt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1542 | CharSequence riLabel = ri.loadLabel(mPm); |
| 1543 | if (riLabel == null) { |
| 1544 | riLabel = ri.activityInfo.packageName; |
| 1545 | } |
| 1546 | if (riLabel.equals(r0Label)) { |
| 1547 | continue; |
| 1548 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1549 | processGroup(sortedComponents, start, (i - 1), rci0, r0Label); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1550 | rci0 = rci; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | r0 = ri; |
| 1552 | r0Label = riLabel; |
| 1553 | start = i; |
| 1554 | } |
| 1555 | // Process last group |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1556 | processGroup(sortedComponents, start, (N - 1), rci0, r0Label); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1557 | } |
Hakan Seyalioglu | 3355012 | 2017-01-06 19:54:43 -0800 | [diff] [blame] | 1558 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1559 | postListReadyRunnable(); |
| 1560 | } |
| 1561 | |
| 1562 | /** |
| 1563 | * Some necessary methods for creating the list are initiated in onCreate and will also |
| 1564 | * determine the layout known. We therefore can't update the UI inline and post to the |
| 1565 | * handler thread to update after the current task is finished. |
| 1566 | */ |
| 1567 | private void postListReadyRunnable() { |
| 1568 | if (mPostListReadyRunnable == null) { |
| 1569 | mPostListReadyRunnable = new Runnable() { |
| 1570 | @Override |
| 1571 | public void run() { |
| 1572 | setTitleAndIcon(); |
| 1573 | resetAlwaysOrOnceButtonBar(); |
| 1574 | onListRebuilt(); |
| 1575 | mPostListReadyRunnable = null; |
| 1576 | } |
| 1577 | }; |
| 1578 | getMainThreadHandler().post(mPostListReadyRunnable); |
| 1579 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | public void onListRebuilt() { |
Xiaohui Chen | 393c801 | 2017-02-14 14:55:07 -0800 | [diff] [blame] | 1583 | int count = getUnfilteredCount(); |
| 1584 | if (count == 1 && getOtherProfile() == null) { |
| 1585 | // Only one target, so we're a candidate to auto-launch! |
| 1586 | final TargetInfo target = targetInfoForPosition(0, false); |
| 1587 | if (shouldAutoLaunchSingleChoice(target)) { |
| 1588 | safelyStartActivity(target); |
| 1589 | finish(); |
| 1590 | } |
| 1591 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | } |
| 1593 | |
Adam Powell | c6d5e3a | 2015-04-23 12:22:20 -0700 | [diff] [blame] | 1594 | public boolean shouldGetResolvedFilter() { |
| 1595 | return mFilterLastUsed; |
| 1596 | } |
| 1597 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1598 | private void processGroup(List<ResolvedComponentInfo> rList, int start, int end, |
| 1599 | ResolvedComponentInfo ro, CharSequence roLabel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | // Process labels from start to i |
| 1601 | int num = end - start+1; |
| 1602 | if (num == 1) { |
| 1603 | // No duplicate labels. Use label for entry at start |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1604 | addResolveInfoWithAlternates(ro, null, roLabel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1605 | } else { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1606 | mHasExtendedInfo = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | boolean usePkg = false; |
Adam Powell | 00f4aad | 2015-09-17 13:38:16 -0700 | [diff] [blame] | 1608 | final ApplicationInfo ai = ro.getResolveInfoAt(0).activityInfo.applicationInfo; |
| 1609 | final CharSequence startApp = ai.loadLabel(mPm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | if (startApp == null) { |
| 1611 | usePkg = true; |
| 1612 | } |
| 1613 | if (!usePkg) { |
| 1614 | // Use HashSet to track duplicates |
| 1615 | HashSet<CharSequence> duplicates = |
| 1616 | new HashSet<CharSequence>(); |
| 1617 | duplicates.add(startApp); |
| 1618 | for (int j = start+1; j <= end ; j++) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1619 | ResolveInfo jRi = rList.get(j).getResolveInfoAt(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm); |
| 1621 | if ( (jApp == null) || (duplicates.contains(jApp))) { |
| 1622 | usePkg = true; |
| 1623 | break; |
| 1624 | } else { |
| 1625 | duplicates.add(jApp); |
| 1626 | } |
| 1627 | } |
| 1628 | // Clear HashSet for later use |
| 1629 | duplicates.clear(); |
| 1630 | } |
| 1631 | for (int k = start; k <= end; k++) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1632 | final ResolvedComponentInfo rci = rList.get(k); |
| 1633 | final ResolveInfo add = rci.getResolveInfoAt(0); |
| 1634 | final CharSequence extraInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1635 | if (usePkg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | // Use package name for all entries from start to end-1 |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1637 | extraInfo = add.activityInfo.packageName; |
| 1638 | } else { |
| 1639 | // Use application name for all entries from start to end-1 |
| 1640 | extraInfo = add.activityInfo.applicationInfo.loadLabel(mPm); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1642 | addResolveInfoWithAlternates(rci, extraInfo, roLabel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1643 | } |
| 1644 | } |
| 1645 | } |
| 1646 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1647 | private void addResolveInfoWithAlternates(ResolvedComponentInfo rci, |
| 1648 | CharSequence extraInfo, CharSequence roLabel) { |
| 1649 | final int count = rci.getCount(); |
| 1650 | final Intent intent = rci.getIntentAt(0); |
| 1651 | final ResolveInfo add = rci.getResolveInfoAt(0); |
| 1652 | final Intent replaceIntent = getReplacementIntent(add.activityInfo, intent); |
| 1653 | final DisplayResolveInfo dri = new DisplayResolveInfo(intent, add, roLabel, |
| 1654 | extraInfo, replaceIntent); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1655 | dri.setPinned(rci.isPinned()); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1656 | addResolveInfo(dri); |
| 1657 | if (replaceIntent == intent) { |
| 1658 | // Only add alternates if we didn't get a specific replacement from |
| 1659 | // the caller. If we have one it trumps potential alternates. |
| 1660 | for (int i = 1, N = count; i < N; i++) { |
| 1661 | final Intent altIntent = rci.getIntentAt(i); |
| 1662 | dri.addAlternateSourceIntent(altIntent); |
| 1663 | } |
| 1664 | } |
| 1665 | updateLastChosenPosition(add); |
| 1666 | } |
| 1667 | |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1668 | private void updateLastChosenPosition(ResolveInfo info) { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1669 | // If another profile is present, ignore the last chosen entry. |
| 1670 | if (mOtherProfile != null) { |
| 1671 | mLastChosenPosition = -1; |
| 1672 | return; |
| 1673 | } |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1674 | if (mLastChosen != null |
| 1675 | && mLastChosen.activityInfo.packageName.equals(info.activityInfo.packageName) |
| 1676 | && mLastChosen.activityInfo.name.equals(info.activityInfo.name)) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1677 | mLastChosenPosition = mDisplayList.size() - 1; |
Esteban Talavera | 6de7266 | 2014-12-11 17:54:07 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1681 | // We assume that at this point we've already filtered out the only intent for a different |
| 1682 | // targetUserId which we're going to use. |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1683 | private void addResolveInfo(DisplayResolveInfo dri) { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1684 | if (dri.mResolveInfo.targetUserId == UserHandle.USER_CURRENT) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1685 | mDisplayList.add(dri); |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1686 | } |
| 1687 | } |
| 1688 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1689 | @Nullable |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1690 | public ResolveInfo resolveInfoForPosition(int position, boolean filtered) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1691 | TargetInfo target = targetInfoForPosition(position, filtered); |
| 1692 | if (target != null) { |
| 1693 | return target.getResolveInfo(); |
| 1694 | } |
| 1695 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | } |
| 1697 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1698 | @Nullable |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1699 | public TargetInfo targetInfoForPosition(int position, boolean filtered) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1700 | if (filtered) { |
| 1701 | return getItem(position); |
| 1702 | } |
| 1703 | if (mDisplayList.size() > position) { |
| 1704 | return mDisplayList.get(position); |
| 1705 | } |
| 1706 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | public int getCount() { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1710 | int totalSize = mDisplayList == null || mDisplayList.isEmpty() ? mPlaceholderCount : |
| 1711 | mDisplayList.size(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1712 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1713 | totalSize--; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1714 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1715 | return totalSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1718 | public int getUnfilteredCount() { |
| 1719 | return mDisplayList.size(); |
| 1720 | } |
| 1721 | |
| 1722 | public int getDisplayInfoCount() { |
| 1723 | return mDisplayList.size(); |
| 1724 | } |
| 1725 | |
| 1726 | public DisplayResolveInfo getDisplayInfoAt(int index) { |
| 1727 | return mDisplayList.get(index); |
| 1728 | } |
| 1729 | |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1730 | @Nullable |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1731 | public TargetInfo getItem(int position) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1732 | if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) { |
| 1733 | position++; |
| 1734 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1735 | if (mDisplayList.size() > position) { |
| 1736 | return mDisplayList.get(position); |
| 1737 | } else { |
| 1738 | return null; |
| 1739 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | public long getItemId(int position) { |
| 1743 | return position; |
| 1744 | } |
| 1745 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1746 | public boolean hasExtendedInfo() { |
| 1747 | return mHasExtendedInfo; |
| 1748 | } |
| 1749 | |
| 1750 | public boolean hasResolvedTarget(ResolveInfo info) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1751 | for (int i = 0, N = mDisplayList.size(); i < N; i++) { |
Adam Powell | c412be6 | 2015-06-24 13:54:10 -0700 | [diff] [blame] | 1752 | if (resolveInfoMatch(info, mDisplayList.get(i).getResolveInfo())) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1753 | return true; |
| 1754 | } |
| 1755 | } |
| 1756 | return false; |
| 1757 | } |
| 1758 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1759 | public int getDisplayResolveInfoCount() { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1760 | return mDisplayList.size(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1763 | public DisplayResolveInfo getDisplayResolveInfo(int index) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1764 | // Used to query services. We only query services for primary targets, not alternates. |
| 1765 | return mDisplayList.get(index); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | public final View getView(int position, View convertView, ViewGroup parent) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 1769 | View view = convertView; |
| 1770 | if (view == null) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1771 | view = createView(parent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1772 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1773 | onBindView(view, getItem(position)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1774 | return view; |
| 1775 | } |
| 1776 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1777 | public final View createView(ViewGroup parent) { |
| 1778 | final View view = onCreateView(parent); |
| 1779 | final ViewHolder holder = new ViewHolder(view); |
| 1780 | view.setTag(holder); |
| 1781 | return view; |
| 1782 | } |
| 1783 | |
| 1784 | public View onCreateView(ViewGroup parent) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1785 | return mInflater.inflate( |
| 1786 | com.android.internal.R.layout.resolve_list_item, parent, false); |
| 1787 | } |
| 1788 | |
| 1789 | public boolean showsExtendedInfo(TargetInfo info) { |
| 1790 | return !TextUtils.isEmpty(info.getExtendedInfo()); |
| 1791 | } |
| 1792 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1793 | public boolean isComponentPinned(ComponentName name) { |
| 1794 | return false; |
| 1795 | } |
| 1796 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1797 | public final void bindView(int position, View view) { |
| 1798 | onBindView(view, getItem(position)); |
| 1799 | } |
| 1800 | |
| 1801 | private void onBindView(View view, TargetInfo info) { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1802 | final ViewHolder holder = (ViewHolder) view.getTag(); |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1803 | if (info == null) { |
| 1804 | holder.icon.setImageDrawable( |
| 1805 | getDrawable(R.drawable.resolver_icon_placeholder)); |
| 1806 | return; |
| 1807 | } |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 1808 | final CharSequence label = info.getDisplayLabel(); |
| 1809 | if (!TextUtils.equals(holder.text.getText(), label)) { |
| 1810 | holder.text.setText(info.getDisplayLabel()); |
| 1811 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1812 | if (showsExtendedInfo(info)) { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1813 | holder.text2.setVisibility(View.VISIBLE); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1814 | holder.text2.setText(info.getExtendedInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1815 | } else { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1816 | holder.text2.setVisibility(View.GONE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1817 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1818 | if (info instanceof DisplayResolveInfo |
| 1819 | && !((DisplayResolveInfo) info).hasDisplayIcon()) { |
| 1820 | new LoadAdapterIconTask((DisplayResolveInfo) info).execute(); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1821 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1822 | holder.icon.setImageDrawable(info.getDisplayIcon()); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1823 | if (holder.badge != null) { |
| 1824 | final Drawable badge = info.getBadgeIcon(); |
| 1825 | if (badge != null) { |
| 1826 | holder.badge.setImageDrawable(badge); |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1827 | holder.badge.setContentDescription(info.getBadgeContentDescription()); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1828 | holder.badge.setVisibility(View.VISIBLE); |
| 1829 | } else { |
| 1830 | holder.badge.setVisibility(View.GONE); |
| 1831 | } |
| 1832 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1836 | @VisibleForTesting |
| 1837 | public static final class ResolvedComponentInfo { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1838 | public final ComponentName name; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1839 | private boolean mPinned; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1840 | private final List<Intent> mIntents = new ArrayList<>(); |
| 1841 | private final List<ResolveInfo> mResolveInfos = new ArrayList<>(); |
| 1842 | |
| 1843 | public ResolvedComponentInfo(ComponentName name, Intent intent, ResolveInfo info) { |
| 1844 | this.name = name; |
| 1845 | add(intent, info); |
| 1846 | } |
| 1847 | |
| 1848 | public void add(Intent intent, ResolveInfo info) { |
| 1849 | mIntents.add(intent); |
| 1850 | mResolveInfos.add(info); |
| 1851 | } |
| 1852 | |
| 1853 | public int getCount() { |
| 1854 | return mIntents.size(); |
| 1855 | } |
| 1856 | |
| 1857 | public Intent getIntentAt(int index) { |
| 1858 | return index >= 0 ? mIntents.get(index) : null; |
| 1859 | } |
| 1860 | |
| 1861 | public ResolveInfo getResolveInfoAt(int index) { |
| 1862 | return index >= 0 ? mResolveInfos.get(index) : null; |
| 1863 | } |
| 1864 | |
| 1865 | public int findIntent(Intent intent) { |
| 1866 | for (int i = 0, N = mIntents.size(); i < N; i++) { |
| 1867 | if (intent.equals(mIntents.get(i))) { |
| 1868 | return i; |
| 1869 | } |
| 1870 | } |
| 1871 | return -1; |
| 1872 | } |
| 1873 | |
| 1874 | public int findResolveInfo(ResolveInfo info) { |
| 1875 | for (int i = 0, N = mResolveInfos.size(); i < N; i++) { |
| 1876 | if (info.equals(mResolveInfos.get(i))) { |
| 1877 | return i; |
| 1878 | } |
| 1879 | } |
| 1880 | return -1; |
| 1881 | } |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1882 | |
| 1883 | public boolean isPinned() { |
| 1884 | return mPinned; |
| 1885 | } |
| 1886 | |
| 1887 | public void setPinned(boolean pinned) { |
| 1888 | mPinned = pinned; |
| 1889 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1892 | static class ViewHolder { |
| 1893 | public TextView text; |
| 1894 | public TextView text2; |
| 1895 | public ImageView icon; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1896 | public ImageView badge; |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1897 | |
| 1898 | public ViewHolder(View view) { |
| 1899 | text = (TextView) view.findViewById(com.android.internal.R.id.text1); |
| 1900 | text2 = (TextView) view.findViewById(com.android.internal.R.id.text2); |
| 1901 | icon = (ImageView) view.findViewById(R.id.icon); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1902 | badge = (ImageView) view.findViewById(R.id.target_badge); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1906 | class ItemClickListener implements AdapterView.OnItemClickListener, |
| 1907 | AdapterView.OnItemLongClickListener { |
| 1908 | @Override |
| 1909 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 1910 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 1911 | if (listView != null) { |
| 1912 | position -= listView.getHeaderViewsCount(); |
| 1913 | } |
| 1914 | if (position < 0) { |
| 1915 | // Header views don't count. |
| 1916 | return; |
| 1917 | } |
Hakan Seyalioglu | 23f3465 | 2017-02-03 09:38:35 -0800 | [diff] [blame] | 1918 | // If we're still loading, we can't yet enable the buttons. |
| 1919 | if (mAdapter.resolveInfoForPosition(position, true) == null) { |
| 1920 | return; |
| 1921 | } |
| 1922 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1923 | final int checkedPos = mAdapterView.getCheckedItemPosition(); |
| 1924 | final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION; |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1925 | if (!useLayoutWithDefault() |
| 1926 | && (!hasValidSelection || mLastSelected != checkedPos)) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1927 | setAlwaysButtonEnabled(hasValidSelection, checkedPos, true); |
| 1928 | mOnceButton.setEnabled(hasValidSelection); |
| 1929 | if (hasValidSelection) { |
| 1930 | mAdapterView.smoothScrollToPosition(checkedPos); |
| 1931 | } |
| 1932 | mLastSelected = checkedPos; |
| 1933 | } else { |
| 1934 | startSelected(position, false, true); |
| 1935 | } |
| 1936 | } |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 1937 | |
| 1938 | @Override |
| 1939 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1940 | final ListView listView = parent instanceof ListView ? (ListView) parent : null; |
| 1941 | if (listView != null) { |
| 1942 | position -= listView.getHeaderViewsCount(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1943 | } |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 1944 | if (position < 0) { |
| 1945 | // Header views don't count. |
| 1946 | return false; |
| 1947 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1948 | ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1949 | showTargetDetails(ri); |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 1950 | return true; |
| 1951 | } |
| 1952 | |
| 1953 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1954 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1955 | abstract class LoadIconTask extends AsyncTask<Void, Void, Drawable> { |
| 1956 | protected final DisplayResolveInfo mDisplayResolveInfo; |
| 1957 | private final ResolveInfo mResolveInfo; |
| 1958 | |
| 1959 | public LoadIconTask(DisplayResolveInfo dri) { |
| 1960 | mDisplayResolveInfo = dri; |
| 1961 | mResolveInfo = dri.getResolveInfo(); |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | @Override |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1965 | protected Drawable doInBackground(Void... params) { |
| 1966 | return loadIconForResolveInfo(mResolveInfo); |
| 1967 | } |
| 1968 | |
| 1969 | @Override |
| 1970 | protected void onPostExecute(Drawable d) { |
| 1971 | mDisplayResolveInfo.setDisplayIcon(d); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | class LoadAdapterIconTask extends LoadIconTask { |
| 1976 | public LoadAdapterIconTask(DisplayResolveInfo dri) { |
| 1977 | super(dri); |
| 1978 | } |
| 1979 | |
| 1980 | @Override |
| 1981 | protected void onPostExecute(Drawable d) { |
| 1982 | super.onPostExecute(d); |
| 1983 | if (mProfileView != null && mAdapter.getOtherProfile() == mDisplayResolveInfo) { |
Adam Powell | 88831a2 | 2014-11-20 18:17:00 -0800 | [diff] [blame] | 1984 | bindProfileView(); |
| 1985 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1986 | mAdapter.notifyDataSetChanged(); |
| 1987 | } |
| 1988 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1989 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1990 | class LoadIconIntoViewTask extends LoadIconTask { |
| 1991 | private final ImageView mTargetView; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1992 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1993 | public LoadIconIntoViewTask(DisplayResolveInfo dri, ImageView target) { |
| 1994 | super(dri); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1995 | mTargetView = target; |
| 1996 | } |
| 1997 | |
| 1998 | @Override |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1999 | protected void onPostExecute(Drawable d) { |
| 2000 | super.onPostExecute(d); |
| 2001 | mTargetView.setImageDrawable(d); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 2002 | } |
| 2003 | } |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 2004 | |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 2005 | static final boolean isSpecificUriMatch(int match) { |
| 2006 | match = match&IntentFilter.MATCH_CATEGORY_MASK; |
| 2007 | return match >= IntentFilter.MATCH_CATEGORY_HOST |
| 2008 | && match <= IntentFilter.MATCH_CATEGORY_PATH; |
| 2009 | } |
| 2010 | |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 2011 | static class PickTargetOptionRequest extends PickOptionRequest { |
| 2012 | public PickTargetOptionRequest(@Nullable Prompt prompt, Option[] options, |
| 2013 | @Nullable Bundle extras) { |
| 2014 | super(prompt, options, extras); |
| 2015 | } |
| 2016 | |
| 2017 | @Override |
| 2018 | public void onCancel() { |
| 2019 | super.onCancel(); |
| 2020 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 2021 | if (ra != null) { |
| 2022 | ra.mPickOptionRequest = null; |
| 2023 | ra.finish(); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | @Override |
| 2028 | public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) { |
| 2029 | super.onPickOptionResult(finished, selections, result); |
| 2030 | if (selections.length != 1) { |
| 2031 | // TODO In a better world we would filter the UI presented here and let the |
| 2032 | // user refine. Maybe later. |
| 2033 | return; |
| 2034 | } |
| 2035 | |
| 2036 | final ResolverActivity ra = (ResolverActivity) getActivity(); |
| 2037 | if (ra != null) { |
| 2038 | final TargetInfo ti = ra.mAdapter.getItem(selections[0].getIndex()); |
| 2039 | if (ra.onTargetSelected(ti, false)) { |
| 2040 | ra.mPickOptionRequest = null; |
| 2041 | ra.finish(); |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2046 | } |