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 | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 20 | import android.app.ActivityThread; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 21 | import android.app.usage.UsageStats; |
| 22 | import android.app.usage.UsageStatsManager; |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 23 | import android.os.AsyncTask; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 24 | import android.provider.Settings; |
Adam Powell | 11f59a0 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 26 | import android.util.Slog; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 27 | import android.widget.AbsListView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import com.android.internal.R; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 29 | import com.android.internal.content.PackageMonitor; |
| 30 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 31 | import android.app.ActivityManager; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 32 | import android.app.ActivityManagerNative; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 33 | import android.app.AppGlobals; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.content.ComponentName; |
| 35 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
| 38 | import android.content.pm.ActivityInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 39 | import android.content.pm.ApplicationInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 40 | import android.content.pm.LabeledIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageManager; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 42 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.content.pm.ResolveInfo; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 44 | import android.content.pm.UserInfo; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 45 | import android.content.res.Resources; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 46 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.net.Uri; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 48 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.os.Bundle; |
| 50 | import android.os.PatternMatcher; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 51 | import android.os.RemoteException; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 52 | import android.os.UserHandle; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 53 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.util.Log; |
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; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 58 | import android.widget.AdapterView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.widget.BaseAdapter; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 60 | import android.widget.Button; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.widget.ImageView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 62 | import android.widget.ListView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.widget.TextView; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 64 | import android.widget.Toast; |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 65 | import com.android.internal.widget.ResolverDrawerLayout; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 66 | |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 67 | import java.text.Collator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import java.util.ArrayList; |
| 69 | import java.util.Collections; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 70 | import java.util.Comparator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import java.util.HashSet; |
| 72 | import java.util.Iterator; |
| 73 | import java.util.List; |
Adam Lesinski | cc562a8 | 2014-08-27 11:52:52 -0700 | [diff] [blame] | 74 | import java.util.Map; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import java.util.Set; |
| 76 | |
Adrian Roos | 27c3ab6 | 2014-10-15 17:21:31 +0200 | [diff] [blame] | 77 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; |
| 78 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; |
| 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | /** |
| 81 | * This activity is displayed when the system attempts to start an Intent for |
| 82 | * which there is more than one matching activity, allowing the user to decide |
| 83 | * which to go to. It is not normally used directly by application developers. |
| 84 | */ |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 85 | public class ResolverActivity extends Activity implements AdapterView.OnItemClickListener { |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 86 | private static final String TAG = "ResolverActivity"; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 87 | private static final boolean DEBUG = false; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 88 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 89 | private int mLaunchedFromUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | private ResolveListAdapter mAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | private PackageManager mPm; |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 92 | private boolean mSafeForwardingMode; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 93 | private boolean mAlwaysUseOption; |
| 94 | private boolean mShowExtended; |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 95 | private ListView mListView; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 96 | private Button mAlwaysButton; |
| 97 | private Button mOnceButton; |
| 98 | private int mIconDpi; |
| 99 | private int mIconSize; |
Adam Powell | 589e6f9 | 2012-05-06 20:52:38 -0700 | [diff] [blame] | 100 | private int mMaxColumns; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 101 | private int mLastSelected = ListView.INVALID_POSITION; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 102 | private boolean mResolvingHome = false; |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 103 | private int mProfileSwitchMessageId = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 105 | private UsageStatsManager mUsm; |
Adam Lesinski | cc562a8 | 2014-08-27 11:52:52 -0700 | [diff] [blame] | 106 | private Map<String, UsageStats> mStats; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 107 | private static final long USAGE_STATS_PERIOD = 1000 * 60 * 60 * 24 * 14; |
| 108 | |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 109 | private boolean mRegistered; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 110 | private final PackageMonitor mPackageMonitor = new PackageMonitor() { |
| 111 | @Override public void onSomePackagesChanged() { |
| 112 | mAdapter.handlePackagesChanged(); |
| 113 | } |
| 114 | }; |
| 115 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 116 | private enum ActionTitle { |
| 117 | VIEW(Intent.ACTION_VIEW, |
| 118 | com.android.internal.R.string.whichViewApplication, |
| 119 | com.android.internal.R.string.whichViewApplicationNamed), |
| 120 | EDIT(Intent.ACTION_EDIT, |
| 121 | com.android.internal.R.string.whichEditApplication, |
| 122 | com.android.internal.R.string.whichEditApplicationNamed), |
| 123 | SEND(Intent.ACTION_SEND, |
| 124 | com.android.internal.R.string.whichSendApplication, |
| 125 | com.android.internal.R.string.whichSendApplicationNamed), |
| 126 | SENDTO(Intent.ACTION_SENDTO, |
| 127 | com.android.internal.R.string.whichSendApplication, |
| 128 | com.android.internal.R.string.whichSendApplicationNamed), |
| 129 | SEND_MULTIPLE(Intent.ACTION_SEND_MULTIPLE, |
| 130 | com.android.internal.R.string.whichSendApplication, |
| 131 | com.android.internal.R.string.whichSendApplicationNamed), |
| 132 | DEFAULT(null, |
| 133 | com.android.internal.R.string.whichApplication, |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 134 | com.android.internal.R.string.whichApplicationNamed), |
| 135 | HOME(Intent.ACTION_MAIN, |
| 136 | com.android.internal.R.string.whichHomeApplication, |
| 137 | com.android.internal.R.string.whichHomeApplicationNamed); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 138 | |
| 139 | public final String action; |
| 140 | public final int titleRes; |
| 141 | public final int namedTitleRes; |
| 142 | |
| 143 | ActionTitle(String action, int titleRes, int namedTitleRes) { |
| 144 | this.action = action; |
| 145 | this.titleRes = titleRes; |
| 146 | this.namedTitleRes = namedTitleRes; |
| 147 | } |
| 148 | |
| 149 | public static ActionTitle forAction(String action) { |
| 150 | for (ActionTitle title : values()) { |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 151 | if (title != HOME && action != null && action.equals(title.action)) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 152 | return title; |
| 153 | } |
| 154 | } |
| 155 | return DEFAULT; |
| 156 | } |
| 157 | } |
| 158 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 159 | private Intent makeMyIntent() { |
| 160 | Intent intent = new Intent(getIntent()); |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 161 | intent.setComponent(null); |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 162 | // The resolver activity is set to be hidden from recent tasks. |
| 163 | // we don't want this attribute to be propagated to the next activity |
| 164 | // being launched. Note that if the original Intent also had this |
| 165 | // flag set, we are now losing it. That should be a very rare case |
| 166 | // and we can live with this. |
| 167 | intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 168 | return intent; |
| 169 | } |
| 170 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | @Override |
| 172 | protected void onCreate(Bundle savedInstanceState) { |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 173 | // Use a specialized prompt when we're handling the 'Home' app startActivity() |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 174 | final Intent intent = makeMyIntent(); |
| 175 | final Set<String> categories = intent.getCategories(); |
| 176 | if (Intent.ACTION_MAIN.equals(intent.getAction()) |
| 177 | && categories != null |
| 178 | && categories.size() == 1 |
| 179 | && categories.contains(Intent.CATEGORY_HOME)) { |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 180 | // Note: this field is not set to true in the compatibility version. |
| 181 | mResolvingHome = true; |
Christopher Tate | b72b363 | 2013-09-30 17:50:32 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 184 | setSafeForwardingMode(true); |
| 185 | |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 186 | onCreate(savedInstanceState, intent, null, 0, null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Compatibility version for other bundled services that use this ocerload without |
| 191 | * a default title resource |
| 192 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 194 | CharSequence title, Intent[] initialIntents, |
| 195 | List<ResolveInfo> rList, boolean alwaysUseOption) { |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 196 | onCreate(savedInstanceState, intent, title, 0, initialIntents, rList, alwaysUseOption); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
| 200 | CharSequence title, int defaultTitleRes, Intent[] initialIntents, |
| 201 | List<ResolveInfo> rList, boolean alwaysUseOption) { |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 202 | setTheme(R.style.Theme_DeviceDefault_Resolver); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | super.onCreate(savedInstanceState); |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 204 | |
| 205 | // Determine whether we should show that intent is forwarded |
| 206 | // from managed profile to owner or other way around. |
| 207 | setProfileSwitchMessageId(intent.getContentUserHint()); |
| 208 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 209 | try { |
| 210 | mLaunchedFromUid = ActivityManagerNative.getDefault().getLaunchedFromUid( |
| 211 | getActivityToken()); |
| 212 | } catch (RemoteException e) { |
| 213 | mLaunchedFromUid = -1; |
| 214 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | mPm = getPackageManager(); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 216 | mUsm = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE); |
| 217 | |
| 218 | final long sinceTime = System.currentTimeMillis() - USAGE_STATS_PERIOD; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 219 | mStats = mUsm.queryAndAggregateUsageStats(sinceTime, System.currentTimeMillis()); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 220 | Log.d(TAG, "sinceTime=" + sinceTime); |
| 221 | |
Adam Powell | 589e6f9 | 2012-05-06 20:52:38 -0700 | [diff] [blame] | 222 | mMaxColumns = getResources().getInteger(R.integer.config_maxResolverActivityColumns); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 224 | mPackageMonitor.register(this, getMainLooper(), false); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 225 | mRegistered = true; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 226 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 227 | final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); |
| 228 | mIconDpi = am.getLauncherLargeIconDensity(); |
| 229 | mIconSize = am.getLauncherLargeIconSize(); |
| 230 | |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 231 | mAdapter = new ResolveListAdapter(this, intent, initialIntents, rList, |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 232 | mLaunchedFromUid, alwaysUseOption); |
| 233 | |
| 234 | final int layoutId; |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 235 | final boolean useHeader; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 236 | if (mAdapter.hasFilteredItem()) { |
| 237 | layoutId = R.layout.resolver_list_with_default; |
| 238 | alwaysUseOption = false; |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 239 | useHeader = true; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 240 | } else { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 241 | useHeader = false; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 242 | layoutId = R.layout.resolver_list; |
| 243 | } |
| 244 | mAlwaysUseOption = alwaysUseOption; |
| 245 | |
Craig Mautner | 1c8eb5e3 | 2014-08-04 17:08:25 -0700 | [diff] [blame] | 246 | int count = mAdapter.mList.size(); |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 247 | if (mLaunchedFromUid < 0 || UserHandle.isIsolated(mLaunchedFromUid)) { |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 248 | // Gulp! |
| 249 | finish(); |
| 250 | return; |
| 251 | } else if (count > 1) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 252 | setContentView(layoutId); |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 253 | mListView = (ListView) findViewById(R.id.resolver_list); |
| 254 | mListView.setAdapter(mAdapter); |
| 255 | mListView.setOnItemClickListener(this); |
| 256 | mListView.setOnItemLongClickListener(new ItemLongClickListener()); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 257 | |
| 258 | if (alwaysUseOption) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 259 | mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 260 | } |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 261 | |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 262 | if (useHeader) { |
| 263 | mListView.addHeaderView(LayoutInflater.from(this).inflate( |
| 264 | R.layout.resolver_different_item_header, mListView, false)); |
| 265 | } |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 266 | } else if (count == 1) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 267 | safelyStartActivity(mAdapter.intentForPosition(0, false)); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 268 | mPackageMonitor.unregister(); |
| 269 | mRegistered = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | finish(); |
| 271 | return; |
| 272 | } else { |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 273 | setContentView(R.layout.resolver_list); |
| 274 | |
| 275 | final TextView empty = (TextView) findViewById(R.id.empty); |
| 276 | empty.setVisibility(View.VISIBLE); |
| 277 | |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 278 | mListView = (ListView) findViewById(R.id.resolver_list); |
| 279 | mListView.setVisibility(View.GONE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | } |
Adrian Roos | 27c3ab6 | 2014-10-15 17:21:31 +0200 | [diff] [blame] | 281 | // Prevent the Resolver window from becoming the top fullscreen window and thus from taking |
| 282 | // control of the system bars. |
| 283 | getWindow().clearFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 285 | final ResolverDrawerLayout rdl = (ResolverDrawerLayout) findViewById(R.id.contentPanel); |
| 286 | if (rdl != null) { |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 287 | rdl.setOnDismissedListener(new ResolverDrawerLayout.OnDismissedListener() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 288 | @Override |
Adam Powell | 5dd072d | 2014-10-30 19:51:41 -0700 | [diff] [blame] | 289 | public void onDismissed() { |
Adam Powell | 4f6c205 | 2014-07-07 18:49:10 -0700 | [diff] [blame] | 290 | finish(); |
| 291 | } |
| 292 | }); |
| 293 | } |
| 294 | |
Adam Powell | 11f59a0 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 295 | if (title == null) { |
| 296 | title = getTitleForAction(intent.getAction(), defaultTitleRes); |
| 297 | } |
| 298 | if (!TextUtils.isEmpty(title)) { |
| 299 | final TextView titleView = (TextView) findViewById(R.id.title); |
| 300 | if (titleView != null) { |
| 301 | titleView.setText(title); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 302 | } |
Adam Powell | 11f59a0 | 2014-08-20 13:22:16 -0700 | [diff] [blame] | 303 | setTitle(title); |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 304 | } |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 305 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 306 | final ImageView iconView = (ImageView) findViewById(R.id.icon); |
| 307 | final DisplayResolveInfo iconInfo = mAdapter.getFilteredItem(); |
| 308 | if (iconView != null && iconInfo != null) { |
| 309 | new LoadIconIntoViewTask(iconView).execute(iconInfo); |
| 310 | } |
| 311 | |
| 312 | if (alwaysUseOption || mAdapter.hasFilteredItem()) { |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 313 | final ViewGroup buttonLayout = (ViewGroup) findViewById(R.id.button_bar); |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 314 | if (buttonLayout != null) { |
| 315 | buttonLayout.setVisibility(View.VISIBLE); |
| 316 | mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always); |
| 317 | mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once); |
| 318 | } else { |
| 319 | mAlwaysUseOption = false; |
| 320 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (mAdapter.hasFilteredItem()) { |
| 324 | setAlwaysButtonEnabled(true, mAdapter.getFilteredPosition(), false); |
| 325 | mOnceButton.setEnabled(true); |
| 326 | } |
| 327 | } |
| 328 | |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 329 | private void setProfileSwitchMessageId(int contentUserHint) { |
| 330 | if (contentUserHint != UserHandle.USER_CURRENT && |
| 331 | contentUserHint != UserHandle.myUserId()) { |
| 332 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 333 | UserInfo originUserInfo = userManager.getUserInfo(contentUserHint); |
| 334 | boolean originIsManaged = originUserInfo != null ? originUserInfo.isManagedProfile() |
| 335 | : false; |
| 336 | boolean targetIsManaged = userManager.isManagedProfile(); |
| 337 | if (originIsManaged && !targetIsManaged) { |
| 338 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_owner; |
| 339 | } else if (!originIsManaged && targetIsManaged) { |
| 340 | mProfileSwitchMessageId = com.android.internal.R.string.forward_intent_to_work; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 345 | /** |
| 346 | * Turn on launch mode that is safe to use when forwarding intents received from |
| 347 | * applications and running in system processes. This mode uses Activity.startActivityAsCaller |
| 348 | * instead of the normal Activity.startActivity for launching the activity selected |
| 349 | * by the user. |
| 350 | * |
| 351 | * <p>This mode is set to true by default if the activity is initialized through |
| 352 | * {@link #onCreate(android.os.Bundle)}. If a subclass calls one of the other onCreate |
| 353 | * methods, it is set to false by default. You must set it before calling one of the |
| 354 | * more detailed onCreate methods, so that it will be set correctly in the case where |
| 355 | * there is only one intent to resolve and it is thus started immediately.</p> |
| 356 | */ |
| 357 | public void setSafeForwardingMode(boolean safeForwarding) { |
| 358 | mSafeForwardingMode = safeForwarding; |
| 359 | } |
| 360 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 361 | protected CharSequence getTitleForAction(String action, int defaultTitleRes) { |
Adam Powell | a35c77a | 2014-09-25 16:46:36 -0700 | [diff] [blame] | 362 | final ActionTitle title = mResolvingHome ? ActionTitle.HOME : ActionTitle.forAction(action); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 363 | final boolean named = mAdapter.hasFilteredItem(); |
| 364 | if (title == ActionTitle.DEFAULT && defaultTitleRes != 0) { |
| 365 | return getString(defaultTitleRes); |
| 366 | } else { |
| 367 | return named ? getString(title.namedTitleRes, mAdapter.getFilteredItem().displayLabel) : |
| 368 | getString(title.titleRes); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 369 | } |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 372 | void dismiss() { |
| 373 | if (!isFinishing()) { |
| 374 | finish(); |
| 375 | } |
| 376 | } |
| 377 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 378 | Drawable getIcon(Resources res, int resId) { |
| 379 | Drawable result; |
| 380 | try { |
| 381 | result = res.getDrawableForDensity(resId, mIconDpi); |
| 382 | } catch (Resources.NotFoundException e) { |
| 383 | result = null; |
| 384 | } |
| 385 | |
| 386 | return result; |
| 387 | } |
| 388 | |
| 389 | Drawable loadIconForResolveInfo(ResolveInfo ri) { |
| 390 | Drawable dr; |
| 391 | try { |
| 392 | if (ri.resolvePackageName != null && ri.icon != 0) { |
| 393 | dr = getIcon(mPm.getResourcesForApplication(ri.resolvePackageName), ri.icon); |
| 394 | if (dr != null) { |
| 395 | return dr; |
| 396 | } |
| 397 | } |
| 398 | final int iconRes = ri.getIconResource(); |
| 399 | if (iconRes != 0) { |
| 400 | dr = getIcon(mPm.getResourcesForApplication(ri.activityInfo.packageName), iconRes); |
| 401 | if (dr != null) { |
| 402 | return dr; |
| 403 | } |
| 404 | } |
| 405 | } catch (NameNotFoundException e) { |
| 406 | Log.e(TAG, "Couldn't find resources for package", e); |
| 407 | } |
| 408 | return ri.loadIcon(mPm); |
| 409 | } |
| 410 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 411 | @Override |
| 412 | protected void onRestart() { |
| 413 | super.onRestart(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 414 | if (!mRegistered) { |
| 415 | mPackageMonitor.register(this, getMainLooper(), false); |
| 416 | mRegistered = true; |
| 417 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 418 | mAdapter.handlePackagesChanged(); |
| 419 | } |
| 420 | |
| 421 | @Override |
| 422 | protected void onStop() { |
| 423 | super.onStop(); |
Dianne Hackborn | d44713a | 2012-04-30 16:34:46 -0700 | [diff] [blame] | 424 | if (mRegistered) { |
| 425 | mPackageMonitor.unregister(); |
| 426 | mRegistered = false; |
| 427 | } |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 428 | if ((getIntent().getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) { |
| 429 | // This resolver is in the unusual situation where it has been |
| 430 | // launched at the top of a new task. We don't let it be added |
| 431 | // to the recent tasks shown to the user, and we need to make sure |
| 432 | // that each time we are launched we get the correct launching |
| 433 | // uid (not re-using the same resolver from an old launching uid), |
| 434 | // so we will now finish ourself since being no longer visible, |
| 435 | // the user probably can't get back to us. |
| 436 | if (!isChangingConfigurations()) { |
| 437 | finish(); |
| 438 | } |
| 439 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 442 | @Override |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 443 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 444 | super.onRestoreInstanceState(savedInstanceState); |
| 445 | if (mAlwaysUseOption) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 446 | final int checkedPos = mListView.getCheckedItemPosition(); |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 447 | final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION; |
Adam Powell | d81cc4a | 2012-07-19 13:51:39 -0700 | [diff] [blame] | 448 | mLastSelected = checkedPos; |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 449 | setAlwaysButtonEnabled(hasValidSelection, checkedPos, true); |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 450 | mOnceButton.setEnabled(hasValidSelection); |
| 451 | if (hasValidSelection) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 452 | mListView.setSelection(checkedPos); |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | @Override |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 458 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 459 | position -= mListView.getHeaderViewsCount(); |
| 460 | if (position < 0) { |
| 461 | // Header views don't count. |
| 462 | return; |
| 463 | } |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 464 | ResolveInfo resolveInfo = mAdapter.resolveInfoForPosition(position, true); |
| 465 | if (mResolvingHome && hasManagedProfile() |
| 466 | && !supportsManagedProfiles(resolveInfo)) { |
| 467 | Toast.makeText(this, String.format(getResources().getString( |
| 468 | com.android.internal.R.string.activity_resolver_work_profiles_support), |
| 469 | resolveInfo.activityInfo.loadLabel(getPackageManager()).toString()), |
| 470 | Toast.LENGTH_LONG).show(); |
| 471 | return; |
| 472 | } |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 473 | final int checkedPos = mListView.getCheckedItemPosition(); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 474 | final boolean hasValidSelection = checkedPos != ListView.INVALID_POSITION; |
Adam Powell | bdda4e7 | 2012-07-20 11:22:03 -0700 | [diff] [blame] | 475 | if (mAlwaysUseOption && (!hasValidSelection || mLastSelected != checkedPos)) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 476 | setAlwaysButtonEnabled(hasValidSelection, checkedPos, true); |
Adam Powell | d81cc4a | 2012-07-19 13:51:39 -0700 | [diff] [blame] | 477 | mOnceButton.setEnabled(hasValidSelection); |
| 478 | if (hasValidSelection) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 479 | mListView.smoothScrollToPosition(checkedPos); |
Adam Powell | 9bee466 | 2012-05-08 11:07:23 -0700 | [diff] [blame] | 480 | } |
Adam Powell | d81cc4a | 2012-07-19 13:51:39 -0700 | [diff] [blame] | 481 | mLastSelected = checkedPos; |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 482 | } else { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 483 | startSelected(position, false, true); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 487 | private boolean hasManagedProfile() { |
| 488 | UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 489 | if (userManager == null) { |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | try { |
| 494 | List<UserInfo> profiles = userManager.getProfiles(getUserId()); |
| 495 | for (UserInfo userInfo : profiles) { |
| 496 | if (userInfo != null && userInfo.isManagedProfile()) { |
| 497 | return true; |
| 498 | } |
| 499 | } |
| 500 | } catch (SecurityException e) { |
| 501 | return false; |
| 502 | } |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | private boolean supportsManagedProfiles(ResolveInfo resolveInfo) { |
| 507 | try { |
| 508 | ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| 509 | resolveInfo.activityInfo.packageName, 0 /* default flags */); |
| 510 | return versionNumberAtLeastL(appInfo.targetSdkVersion); |
| 511 | } catch (NameNotFoundException e) { |
| 512 | return false; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | private boolean versionNumberAtLeastL(int versionNumber) { |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 517 | return versionNumber >= Build.VERSION_CODES.LOLLIPOP; |
Sander Alewijnse | 6c9eee8 | 2014-07-17 10:03:31 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 520 | private void setAlwaysButtonEnabled(boolean hasValidSelection, int checkedPos, |
| 521 | boolean filtered) { |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 522 | boolean enabled = false; |
| 523 | if (hasValidSelection) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 524 | ResolveInfo ri = mAdapter.resolveInfoForPosition(checkedPos, filtered); |
Nicolas Prevot | 5044988 | 2014-06-23 12:42:37 +0100 | [diff] [blame] | 525 | if (ri.targetUserId == UserHandle.USER_CURRENT) { |
| 526 | enabled = true; |
| 527 | } |
| 528 | } |
| 529 | mAlwaysButton.setEnabled(enabled); |
| 530 | } |
| 531 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 532 | public void onButtonClick(View v) { |
| 533 | final int id = v.getId(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 534 | startSelected(mAlwaysUseOption ? |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 535 | mListView.getCheckedItemPosition() : mAdapter.getFilteredPosition(), |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 536 | id == R.id.button_always, |
| 537 | mAlwaysUseOption); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 538 | dismiss(); |
| 539 | } |
| 540 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 541 | void startSelected(int which, boolean always, boolean filtered) { |
Amith Yamasani | 07cd351 | 2013-09-18 13:16:00 -0700 | [diff] [blame] | 542 | if (isFinishing()) { |
| 543 | return; |
| 544 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 545 | ResolveInfo ri = mAdapter.resolveInfoForPosition(which, filtered); |
| 546 | Intent intent = mAdapter.intentForPosition(which, filtered); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 547 | onIntentSelected(ri, intent, always); |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 548 | finish(); |
| 549 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 551 | /** |
| 552 | * Replace me in subclasses! |
| 553 | */ |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 554 | public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) { |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 555 | return defIntent; |
| 556 | } |
| 557 | |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 558 | protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 559 | if ((mAlwaysUseOption || mAdapter.hasFilteredItem()) && mAdapter.mOrigResolveList != null) { |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 560 | // Build a reasonable intent filter, based on what matched. |
| 561 | IntentFilter filter = new IntentFilter(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 563 | if (intent.getAction() != null) { |
| 564 | filter.addAction(intent.getAction()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 566 | Set<String> categories = intent.getCategories(); |
| 567 | if (categories != null) { |
| 568 | for (String cat : categories) { |
| 569 | filter.addCategory(cat); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | } |
| 571 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 572 | filter.addCategory(Intent.CATEGORY_DEFAULT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 574 | int cat = ri.match&IntentFilter.MATCH_CATEGORY_MASK; |
| 575 | Uri data = intent.getData(); |
| 576 | if (cat == IntentFilter.MATCH_CATEGORY_TYPE) { |
| 577 | String mimeType = intent.resolveType(this); |
| 578 | if (mimeType != null) { |
| 579 | try { |
| 580 | filter.addDataType(mimeType); |
| 581 | } catch (IntentFilter.MalformedMimeTypeException e) { |
| 582 | Log.w("ResolverActivity", e); |
| 583 | filter = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | } |
| 585 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 586 | } |
| 587 | if (data != null && data.getScheme() != null) { |
| 588 | // We need the data specification if there was no type, |
| 589 | // OR if the scheme is not one of our magical "file:" |
| 590 | // or "content:" schemes (see IntentFilter for the reason). |
| 591 | if (cat != IntentFilter.MATCH_CATEGORY_TYPE |
| 592 | || (!"file".equals(data.getScheme()) |
| 593 | && !"content".equals(data.getScheme()))) { |
| 594 | filter.addDataScheme(data.getScheme()); |
| 595 | |
| 596 | // Look through the resolved filter to determine which part |
| 597 | // of it matched the original Intent. |
| 598 | Iterator<PatternMatcher> pIt = ri.filter.schemeSpecificPartsIterator(); |
| 599 | if (pIt != null) { |
| 600 | String ssp = data.getSchemeSpecificPart(); |
| 601 | while (ssp != null && pIt.hasNext()) { |
| 602 | PatternMatcher p = pIt.next(); |
| 603 | if (p.match(ssp)) { |
| 604 | filter.addDataSchemeSpecificPart(p.getPath(), p.getType()); |
| 605 | break; |
| 606 | } |
Dianne Hackborn | df1c0bf | 2013-06-12 16:21:38 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 609 | Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator(); |
| 610 | if (aIt != null) { |
| 611 | while (aIt.hasNext()) { |
| 612 | IntentFilter.AuthorityEntry a = aIt.next(); |
| 613 | if (a.match(data) >= 0) { |
| 614 | int port = a.getPort(); |
| 615 | filter.addDataAuthority(a.getHost(), |
| 616 | port >= 0 ? Integer.toString(port) : null); |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | pIt = ri.filter.pathsIterator(); |
| 622 | if (pIt != null) { |
| 623 | String path = data.getPath(); |
| 624 | while (path != null && pIt.hasNext()) { |
| 625 | PatternMatcher p = pIt.next(); |
| 626 | if (p.match(path)) { |
| 627 | filter.addDataPath(p.getPath(), p.getType()); |
| 628 | break; |
| 629 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 635 | if (filter != null) { |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 636 | final int N = mAdapter.mOrigResolveList.size(); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 637 | ComponentName[] set = new ComponentName[N]; |
| 638 | int bestMatch = 0; |
| 639 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 640 | ResolveInfo r = mAdapter.mOrigResolveList.get(i); |
Amith Yamasani | 588dd2a | 2013-09-03 12:30:44 -0700 | [diff] [blame] | 641 | set[i] = new ComponentName(r.activityInfo.packageName, |
| 642 | r.activityInfo.name); |
| 643 | if (r.match > bestMatch) bestMatch = r.match; |
| 644 | } |
| 645 | if (alwaysCheck) { |
| 646 | getPackageManager().addPreferredActivity(filter, bestMatch, set, |
| 647 | intent.getComponent()); |
| 648 | } else { |
| 649 | try { |
| 650 | AppGlobals.getPackageManager().setLastChosenActivity(intent, |
| 651 | intent.resolveTypeIfNeeded(getContentResolver()), |
| 652 | PackageManager.MATCH_DEFAULT_ONLY, |
| 653 | filter, bestMatch, intent.getComponent()); |
| 654 | } catch (RemoteException re) { |
| 655 | Log.d(TAG, "Error calling setLastChosenActivity\n" + re); |
| 656 | } |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 657 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | if (intent != null) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 662 | safelyStartActivity(intent); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | public void safelyStartActivity(Intent intent) { |
Sander Alewijnse | f654533 | 2014-10-31 12:39:02 +0000 | [diff] [blame] | 667 | // If needed, show that intent is forwarded |
| 668 | // from managed profile to owner or other way around. |
| 669 | if (mProfileSwitchMessageId != -1) { |
| 670 | Toast.makeText(this, getString(mProfileSwitchMessageId), Toast.LENGTH_LONG).show(); |
| 671 | } |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 672 | if (!mSafeForwardingMode) { |
Amith Yamasani | 203a2f4 | 2012-10-03 20:16:40 -0700 | [diff] [blame] | 673 | startActivity(intent); |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 674 | onActivityStarted(intent); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 675 | return; |
| 676 | } |
| 677 | try { |
Jeff Sharkey | 9797880 | 2014-10-14 10:48:18 -0700 | [diff] [blame] | 678 | startActivityAsCaller(intent, null, UserHandle.USER_NULL); |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 679 | onActivityStarted(intent); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 680 | } catch (RuntimeException e) { |
| 681 | String launchedFromPackage; |
| 682 | try { |
| 683 | launchedFromPackage = ActivityManagerNative.getDefault().getLaunchedFromPackage( |
| 684 | getActivityToken()); |
| 685 | } catch (RemoteException e2) { |
| 686 | launchedFromPackage = "??"; |
| 687 | } |
| 688 | Slog.wtf(TAG, "Unable to launch as uid " + mLaunchedFromUid |
| 689 | + " package " + launchedFromPackage + ", while running in " |
| 690 | + ActivityThread.currentProcessName(), e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 694 | public void onActivityStarted(Intent intent) { |
| 695 | // Do nothing |
| 696 | } |
| 697 | |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 698 | void showAppDetails(ResolveInfo ri) { |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 699 | Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) |
Adam Powell | 0fc5b2b | 2012-07-18 18:20:29 -0700 | [diff] [blame] | 700 | .setData(Uri.fromParts("package", ri.activityInfo.packageName, null)) |
| 701 | .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
Amith Yamasani | 203a2f4 | 2012-10-03 20:16:40 -0700 | [diff] [blame] | 702 | startActivity(in); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 703 | } |
| 704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | private final class DisplayResolveInfo { |
| 706 | ResolveInfo ri; |
| 707 | CharSequence displayLabel; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 708 | Drawable displayIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | CharSequence extendedInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 710 | Intent origIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 712 | DisplayResolveInfo(ResolveInfo pri, CharSequence pLabel, |
| 713 | CharSequence pInfo, Intent pOrigIntent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | ri = pri; |
| 715 | displayLabel = pLabel; |
| 716 | extendedInfo = pInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 717 | origIntent = pOrigIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | private final class ResolveListAdapter extends BaseAdapter { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 722 | private final Intent[] mInitialIntents; |
| 723 | private final List<ResolveInfo> mBaseResolveList; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 724 | private ResolveInfo mLastChosen; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | private final Intent mIntent; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 726 | private final int mLaunchedFromUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | private final LayoutInflater mInflater; |
| 728 | |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 729 | List<DisplayResolveInfo> mList; |
| 730 | List<ResolveInfo> mOrigResolveList; |
| 731 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 732 | private int mLastChosenPosition = -1; |
| 733 | private boolean mFilterLastUsed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 735 | public ResolveListAdapter(Context context, Intent intent, |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 736 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
| 737 | boolean filterLastUsed) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | mIntent = new Intent(intent); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 739 | mInitialIntents = initialIntents; |
| 740 | mBaseResolveList = rList; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 741 | mLaunchedFromUid = launchedFromUid; |
Adam Powell | e9414d9 | 2014-07-05 17:44:18 -0700 | [diff] [blame] | 742 | mInflater = LayoutInflater.from(context); |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 743 | mList = new ArrayList<DisplayResolveInfo>(); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 744 | mFilterLastUsed = filterLastUsed; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 745 | rebuildList(); |
| 746 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 748 | public void handlePackagesChanged() { |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 749 | final int oldItemCount = getCount(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 750 | rebuildList(); |
| 751 | notifyDataSetChanged(); |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 752 | final int newItemCount = getCount(); |
| 753 | if (newItemCount == 0) { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 754 | // We no longer have any items... just finish the activity. |
| 755 | finish(); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 756 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 759 | public DisplayResolveInfo getFilteredItem() { |
| 760 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 761 | // Not using getItem since it offsets to dodge this position for the list |
| 762 | return mList.get(mLastChosenPosition); |
| 763 | } |
| 764 | return null; |
| 765 | } |
| 766 | |
| 767 | public int getFilteredPosition() { |
| 768 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 769 | return mLastChosenPosition; |
| 770 | } |
| 771 | return AbsListView.INVALID_POSITION; |
| 772 | } |
| 773 | |
| 774 | public boolean hasFilteredItem() { |
| 775 | return mFilterLastUsed && mLastChosenPosition >= 0; |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 778 | private void rebuildList() { |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 779 | List<ResolveInfo> currentResolveList; |
| 780 | |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 781 | try { |
| 782 | mLastChosen = AppGlobals.getPackageManager().getLastChosenActivity( |
| 783 | mIntent, mIntent.resolveTypeIfNeeded(getContentResolver()), |
| 784 | PackageManager.MATCH_DEFAULT_ONLY); |
| 785 | } catch (RemoteException re) { |
| 786 | Log.d(TAG, "Error calling setLastChosenActivity\n" + re); |
| 787 | } |
| 788 | |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 789 | mList.clear(); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 790 | if (mBaseResolveList != null) { |
Xiong Li | e88b042 | 2014-04-10 15:25:45 +0800 | [diff] [blame] | 791 | currentResolveList = mOrigResolveList = mBaseResolveList; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 792 | } else { |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 793 | currentResolveList = mOrigResolveList = mPm.queryIntentActivities( |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 794 | mIntent, PackageManager.MATCH_DEFAULT_ONLY |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 795 | | (mFilterLastUsed ? PackageManager.GET_RESOLVED_FILTER : 0)); |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 796 | // Filter out any activities that the launched uid does not |
| 797 | // have permission for. We don't do this when we have an explicit |
| 798 | // list of resolved activities, because that only happens when |
| 799 | // we are being subclassed, so we can safely launch whatever |
| 800 | // they gave us. |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 801 | if (currentResolveList != null) { |
| 802 | for (int i=currentResolveList.size()-1; i >= 0; i--) { |
| 803 | ActivityInfo ai = currentResolveList.get(i).activityInfo; |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 804 | int granted = ActivityManager.checkComponentPermission( |
| 805 | ai.permission, mLaunchedFromUid, |
| 806 | ai.applicationInfo.uid, ai.exported); |
| 807 | if (granted != PackageManager.PERMISSION_GRANTED) { |
| 808 | // Access not allowed! |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 809 | if (mOrigResolveList == currentResolveList) { |
| 810 | mOrigResolveList = new ArrayList<ResolveInfo>(mOrigResolveList); |
| 811 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 812 | currentResolveList.remove(i); |
Dianne Hackborn | 5320eb8 | 2012-05-18 12:05:04 -0700 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | } |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 816 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | int N; |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 818 | if ((currentResolveList != null) && ((N = currentResolveList.size()) > 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | // Only display the first matches that are either of equal |
| 820 | // priority or have asked to be default options. |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 821 | ResolveInfo r0 = currentResolveList.get(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | for (int i=1; i<N; i++) { |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 823 | ResolveInfo ri = currentResolveList.get(i); |
| 824 | if (DEBUG) Log.v( |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 825 | TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | r0.activityInfo.name + "=" + |
| 827 | r0.priority + "/" + r0.isDefault + " vs " + |
| 828 | ri.activityInfo.name + "=" + |
| 829 | ri.priority + "/" + ri.isDefault); |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 830 | if (r0.priority != ri.priority || |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | r0.isDefault != ri.isDefault) { |
| 832 | while (i < N) { |
Dianne Hackborn | 6d8dfbd | 2013-09-23 17:38:51 -0700 | [diff] [blame] | 833 | if (mOrigResolveList == currentResolveList) { |
| 834 | mOrigResolveList = new ArrayList<ResolveInfo>(mOrigResolveList); |
| 835 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 836 | currentResolveList.remove(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | N--; |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | if (N > 1) { |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 842 | Comparator<ResolveInfo> rComparator = |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 843 | new ResolverComparator(ResolverActivity.this, mIntent); |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 844 | Collections.sort(currentResolveList, rComparator); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 846 | // First put the initial items at the top. |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 847 | if (mInitialIntents != null) { |
| 848 | for (int i=0; i<mInitialIntents.length; i++) { |
| 849 | Intent ii = mInitialIntents[i]; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 850 | if (ii == null) { |
| 851 | continue; |
| 852 | } |
| 853 | ActivityInfo ai = ii.resolveActivityInfo( |
| 854 | getPackageManager(), 0); |
| 855 | if (ai == null) { |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 856 | Log.w(TAG, "No activity found for " + ii); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 857 | continue; |
| 858 | } |
| 859 | ResolveInfo ri = new ResolveInfo(); |
| 860 | ri.activityInfo = ai; |
Nicolas Prevot | 1a81592 | 2014-10-10 16:22:38 +0100 | [diff] [blame] | 861 | UserManager userManager = |
| 862 | (UserManager) getSystemService(Context.USER_SERVICE); |
| 863 | if (userManager.isManagedProfile()) { |
| 864 | ri.noResourceId = true; |
| 865 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 866 | if (ii instanceof LabeledIntent) { |
| 867 | LabeledIntent li = (LabeledIntent)ii; |
| 868 | ri.resolvePackageName = li.getSourcePackage(); |
| 869 | ri.labelRes = li.getLabelResource(); |
| 870 | ri.nonLocalizedLabel = li.getNonLocalizedLabel(); |
| 871 | ri.icon = li.getIconResource(); |
| 872 | } |
| 873 | mList.add(new DisplayResolveInfo(ri, |
| 874 | ri.loadLabel(getPackageManager()), null, ii)); |
| 875 | } |
| 876 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 877 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | // Check for applications with same name and use application name or |
| 879 | // package name if necessary |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 880 | r0 = currentResolveList.get(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | int start = 0; |
| 882 | CharSequence r0Label = r0.loadLabel(mPm); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 883 | mShowExtended = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | for (int i = 1; i < N; i++) { |
| 885 | if (r0Label == null) { |
| 886 | r0Label = r0.activityInfo.packageName; |
| 887 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 888 | ResolveInfo ri = currentResolveList.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | CharSequence riLabel = ri.loadLabel(mPm); |
| 890 | if (riLabel == null) { |
| 891 | riLabel = ri.activityInfo.packageName; |
| 892 | } |
| 893 | if (riLabel.equals(r0Label)) { |
| 894 | continue; |
| 895 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 896 | processGroup(currentResolveList, start, (i-1), r0, r0Label); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | r0 = ri; |
| 898 | r0Label = riLabel; |
| 899 | start = i; |
| 900 | } |
| 901 | // Process last group |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 902 | processGroup(currentResolveList, start, (N-1), r0, r0Label); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
| 906 | private void processGroup(List<ResolveInfo> rList, int start, int end, ResolveInfo ro, |
| 907 | CharSequence roLabel) { |
| 908 | // Process labels from start to i |
| 909 | int num = end - start+1; |
| 910 | if (num == 1) { |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 911 | if (mLastChosen != null |
| 912 | && mLastChosen.activityInfo.packageName.equals( |
| 913 | ro.activityInfo.packageName) |
| 914 | && mLastChosen.activityInfo.name.equals(ro.activityInfo.name)) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 915 | mLastChosenPosition = mList.size(); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 916 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | // No duplicate labels. Use label for entry at start |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 918 | mList.add(new DisplayResolveInfo(ro, roLabel, null, null)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | } else { |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 920 | mShowExtended = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | boolean usePkg = false; |
| 922 | CharSequence startApp = ro.activityInfo.applicationInfo.loadLabel(mPm); |
| 923 | if (startApp == null) { |
| 924 | usePkg = true; |
| 925 | } |
| 926 | if (!usePkg) { |
| 927 | // Use HashSet to track duplicates |
| 928 | HashSet<CharSequence> duplicates = |
| 929 | new HashSet<CharSequence>(); |
| 930 | duplicates.add(startApp); |
| 931 | for (int j = start+1; j <= end ; j++) { |
| 932 | ResolveInfo jRi = rList.get(j); |
| 933 | CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm); |
| 934 | if ( (jApp == null) || (duplicates.contains(jApp))) { |
| 935 | usePkg = true; |
| 936 | break; |
| 937 | } else { |
| 938 | duplicates.add(jApp); |
| 939 | } |
| 940 | } |
| 941 | // Clear HashSet for later use |
| 942 | duplicates.clear(); |
| 943 | } |
| 944 | for (int k = start; k <= end; k++) { |
| 945 | ResolveInfo add = rList.get(k); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 946 | if (mLastChosen != null |
| 947 | && mLastChosen.activityInfo.packageName.equals( |
| 948 | add.activityInfo.packageName) |
| 949 | && mLastChosen.activityInfo.name.equals(add.activityInfo.name)) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 950 | mLastChosenPosition = mList.size(); |
Amith Yamasani | e9ecc8b | 2013-08-22 11:16:27 -0700 | [diff] [blame] | 951 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | if (usePkg) { |
| 953 | // Use application name for all entries from start to end-1 |
| 954 | mList.add(new DisplayResolveInfo(add, roLabel, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 955 | add.activityInfo.packageName, null)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | } else { |
| 957 | // Use package name for all entries from start to end-1 |
| 958 | mList.add(new DisplayResolveInfo(add, roLabel, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 959 | add.activityInfo.applicationInfo.loadLabel(mPm), null)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 965 | public ResolveInfo resolveInfoForPosition(int position, boolean filtered) { |
| 966 | return (filtered ? getItem(position) : mList.get(position)).ri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | } |
| 968 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 969 | public Intent intentForPosition(int position, boolean filtered) { |
| 970 | DisplayResolveInfo dri = filtered ? getItem(position) : mList.get(position); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 971 | |
| 972 | Intent intent = new Intent(dri.origIntent != null ? dri.origIntent : |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 973 | getReplacementIntent(dri.ri.activityInfo, mIntent)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| 975 | |Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 976 | ActivityInfo ai = dri.ri.activityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | intent.setComponent(new ComponentName( |
| 978 | ai.applicationInfo.packageName, ai.name)); |
| 979 | return intent; |
| 980 | } |
| 981 | |
| 982 | public int getCount() { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 983 | int result = mList.size(); |
| 984 | if (mFilterLastUsed && mLastChosenPosition >= 0) { |
| 985 | result--; |
| 986 | } |
| 987 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 990 | public DisplayResolveInfo getItem(int position) { |
| 991 | if (mFilterLastUsed && mLastChosenPosition >= 0 && position >= mLastChosenPosition) { |
| 992 | position++; |
| 993 | } |
You Kim | 43a5070e | 2012-11-21 23:23:45 +0900 | [diff] [blame] | 994 | return mList.get(position); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | public long getItemId(int position) { |
| 998 | return position; |
| 999 | } |
| 1000 | |
| 1001 | public View getView(int position, View convertView, ViewGroup parent) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 1002 | View view = convertView; |
| 1003 | if (view == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | view = mInflater.inflate( |
| 1005 | com.android.internal.R.layout.resolve_list_item, parent, false); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 1006 | |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1007 | final ViewHolder holder = new ViewHolder(view); |
| 1008 | view.setTag(holder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1010 | bindView(view, getItem(position)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | return view; |
| 1012 | } |
| 1013 | |
| 1014 | private final void bindView(View view, DisplayResolveInfo info) { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1015 | final ViewHolder holder = (ViewHolder) view.getTag(); |
| 1016 | holder.text.setText(info.displayLabel); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 1017 | if (mShowExtended) { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1018 | holder.text2.setVisibility(View.VISIBLE); |
| 1019 | holder.text2.setText(info.extendedInfo); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } else { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1021 | holder.text2.setVisibility(View.GONE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1023 | if (info.displayIcon == null) { |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1024 | new LoadIconTask().execute(info); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1025 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1026 | holder.icon.setImageDrawable(info.displayIcon); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | static class ViewHolder { |
| 1031 | public TextView text; |
| 1032 | public TextView text2; |
| 1033 | public ImageView icon; |
| 1034 | |
| 1035 | public ViewHolder(View view) { |
| 1036 | text = (TextView) view.findViewById(com.android.internal.R.id.text1); |
| 1037 | text2 = (TextView) view.findViewById(com.android.internal.R.id.text2); |
| 1038 | icon = (ImageView) view.findViewById(R.id.icon); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 1042 | class ItemLongClickListener implements AdapterView.OnItemLongClickListener { |
| 1043 | |
| 1044 | @Override |
| 1045 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
Adam Powell | fd1e93d | 2014-09-07 16:52:22 -0700 | [diff] [blame] | 1046 | position -= mListView.getHeaderViewsCount(); |
| 1047 | if (position < 0) { |
| 1048 | // Header views don't count. |
| 1049 | return false; |
| 1050 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1051 | ResolveInfo ri = mAdapter.resolveInfoForPosition(position, true); |
Adam Powell | c587861 | 2012-05-04 18:42:38 -0700 | [diff] [blame] | 1052 | showAppDetails(ri); |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 1053 | return true; |
| 1054 | } |
| 1055 | |
| 1056 | } |
Adam Powell | 0256c6f | 2013-05-29 16:42:33 -0700 | [diff] [blame] | 1057 | |
| 1058 | class LoadIconTask extends AsyncTask<DisplayResolveInfo, Void, DisplayResolveInfo> { |
| 1059 | @Override |
| 1060 | protected DisplayResolveInfo doInBackground(DisplayResolveInfo... params) { |
| 1061 | final DisplayResolveInfo info = params[0]; |
| 1062 | if (info.displayIcon == null) { |
| 1063 | info.displayIcon = loadIconForResolveInfo(info.ri); |
| 1064 | } |
| 1065 | return info; |
| 1066 | } |
| 1067 | |
| 1068 | @Override |
| 1069 | protected void onPostExecute(DisplayResolveInfo info) { |
| 1070 | mAdapter.notifyDataSetChanged(); |
| 1071 | } |
| 1072 | } |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 1073 | |
| 1074 | class LoadIconIntoViewTask extends AsyncTask<DisplayResolveInfo, Void, DisplayResolveInfo> { |
| 1075 | final ImageView mTargetView; |
| 1076 | |
| 1077 | public LoadIconIntoViewTask(ImageView target) { |
| 1078 | mTargetView = target; |
| 1079 | } |
| 1080 | |
| 1081 | @Override |
| 1082 | protected DisplayResolveInfo doInBackground(DisplayResolveInfo... params) { |
| 1083 | final DisplayResolveInfo info = params[0]; |
| 1084 | if (info.displayIcon == null) { |
| 1085 | info.displayIcon = loadIconForResolveInfo(info.ri); |
| 1086 | } |
| 1087 | return info; |
| 1088 | } |
| 1089 | |
| 1090 | @Override |
| 1091 | protected void onPostExecute(DisplayResolveInfo info) { |
| 1092 | mTargetView.setImageDrawable(info.displayIcon); |
| 1093 | } |
| 1094 | } |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1095 | |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 1096 | static final boolean isSpecificUriMatch(int match) { |
| 1097 | match = match&IntentFilter.MATCH_CATEGORY_MASK; |
| 1098 | return match >= IntentFilter.MATCH_CATEGORY_HOST |
| 1099 | && match <= IntentFilter.MATCH_CATEGORY_PATH; |
| 1100 | } |
| 1101 | |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1102 | class ResolverComparator implements Comparator<ResolveInfo> { |
| 1103 | private final Collator mCollator; |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 1104 | private final boolean mHttp; |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1105 | |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 1106 | public ResolverComparator(Context context, Intent intent) { |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1107 | mCollator = Collator.getInstance(context.getResources().getConfiguration().locale); |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 1108 | String scheme = intent.getScheme(); |
| 1109 | mHttp = "http".equals(scheme) || "https".equals(scheme); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | @Override |
| 1113 | public int compare(ResolveInfo lhs, ResolveInfo rhs) { |
| 1114 | // We want to put the one targeted to another user at the end of the dialog. |
| 1115 | if (lhs.targetUserId != UserHandle.USER_CURRENT) { |
| 1116 | return 1; |
| 1117 | } |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1118 | |
Dianne Hackborn | ec452d9 | 2014-11-11 17:16:56 -0800 | [diff] [blame] | 1119 | if (mHttp) { |
| 1120 | // Special case: we want filters that match URI paths/schemes to be |
| 1121 | // ordered before others. This is for the case when opening URIs, |
| 1122 | // to make native apps go above browsers. |
| 1123 | final boolean lhsSpecific = isSpecificUriMatch(lhs.match); |
| 1124 | final boolean rhsSpecific = isSpecificUriMatch(rhs.match); |
| 1125 | if (lhsSpecific != rhsSpecific) { |
| 1126 | return lhsSpecific ? -1 : 1; |
| 1127 | } |
| 1128 | } |
| 1129 | |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1130 | if (mStats != null) { |
| 1131 | final long timeDiff = |
| 1132 | getPackageTimeSpent(rhs.activityInfo.packageName) - |
| 1133 | getPackageTimeSpent(lhs.activityInfo.packageName); |
| 1134 | |
| 1135 | if (timeDiff != 0) { |
| 1136 | return timeDiff > 0 ? 1 : -1; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | CharSequence sa = lhs.loadLabel(mPm); |
| 1141 | if (sa == null) sa = lhs.activityInfo.name; |
| 1142 | CharSequence sb = rhs.loadLabel(mPm); |
| 1143 | if (sb == null) sb = rhs.activityInfo.name; |
| 1144 | |
| 1145 | return mCollator.compare(sa.toString(), sb.toString()); |
| 1146 | } |
| 1147 | |
| 1148 | private long getPackageTimeSpent(String packageName) { |
| 1149 | if (mStats != null) { |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 1150 | final UsageStats stats = mStats.get(packageName); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1151 | if (stats != null) { |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 1152 | return stats.getTotalTimeInForeground(); |
Adam Powell | 09a6560 | 2014-07-20 16:23:14 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | } |
| 1156 | return 0; |
| 1157 | } |
| 1158 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |