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 | |
| 19 | import com.android.internal.R; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 20 | import com.android.internal.content.PackageMonitor; |
| 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 28 | import android.content.pm.LabeledIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.net.Uri; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.PatternMatcher; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.util.Log; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 36 | import android.view.LayoutInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.view.View; |
| 38 | import android.view.ViewGroup; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 39 | import android.widget.AdapterView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.widget.BaseAdapter; |
| 41 | import android.widget.CheckBox; |
| 42 | import android.widget.CompoundButton; |
| 43 | import android.widget.ImageView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 44 | import android.widget.ListView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.widget.TextView; |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.Collections; |
| 49 | import java.util.HashSet; |
| 50 | import java.util.Iterator; |
| 51 | import java.util.List; |
| 52 | import java.util.Set; |
| 53 | |
| 54 | /** |
| 55 | * This activity is displayed when the system attempts to start an Intent for |
| 56 | * which there is more than one matching activity, allowing the user to decide |
| 57 | * which to go to. It is not normally used directly by application developers. |
| 58 | */ |
| 59 | public class ResolverActivity extends AlertActivity implements |
| 60 | DialogInterface.OnClickListener, CheckBox.OnCheckedChangeListener { |
| 61 | private ResolveListAdapter mAdapter; |
| 62 | private CheckBox mAlwaysCheck; |
| 63 | private TextView mClearDefaultHint; |
| 64 | private PackageManager mPm; |
| 65 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 66 | private final PackageMonitor mPackageMonitor = new PackageMonitor() { |
| 67 | @Override public void onSomePackagesChanged() { |
| 68 | mAdapter.handlePackagesChanged(); |
| 69 | } |
| 70 | }; |
| 71 | |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 72 | private Intent makeMyIntent() { |
| 73 | Intent intent = new Intent(getIntent()); |
| 74 | // The resolver activity is set to be hidden from recent tasks. |
| 75 | // we don't want this attribute to be propagated to the next activity |
| 76 | // being launched. Note that if the original Intent also had this |
| 77 | // flag set, we are now losing it. That should be a very rare case |
| 78 | // and we can live with this. |
| 79 | intent.setFlags(intent.getFlags()&~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 80 | return intent; |
| 81 | } |
| 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | @Override |
| 84 | protected void onCreate(Bundle savedInstanceState) { |
Dianne Hackborn | 905577f | 2011-09-07 18:31:28 -0700 | [diff] [blame] | 85 | onCreate(savedInstanceState, makeMyIntent(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | getResources().getText(com.android.internal.R.string.whichApplication), |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 87 | null, null, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | protected void onCreate(Bundle savedInstanceState, Intent intent, |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 91 | CharSequence title, Intent[] initialIntents, List<ResolveInfo> rList, |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 92 | boolean alwaysUseOption) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | super.onCreate(savedInstanceState); |
| 94 | mPm = getPackageManager(); |
| 95 | intent.setComponent(null); |
| 96 | |
| 97 | AlertController.AlertParams ap = mAlertParams; |
| 98 | |
| 99 | ap.mTitle = title; |
| 100 | ap.mOnClickListener = this; |
| 101 | |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame^] | 102 | mPackageMonitor.register(this, getMainLooper(), false); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 103 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | if (alwaysUseOption) { |
| 105 | LayoutInflater inflater = (LayoutInflater) getSystemService( |
| 106 | Context.LAYOUT_INFLATER_SERVICE); |
| 107 | ap.mView = inflater.inflate(R.layout.always_use_checkbox, null); |
| 108 | mAlwaysCheck = (CheckBox)ap.mView.findViewById(com.android.internal.R.id.alwaysUse); |
| 109 | mAlwaysCheck.setText(R.string.alwaysUse); |
| 110 | mAlwaysCheck.setOnCheckedChangeListener(this); |
| 111 | mClearDefaultHint = (TextView)ap.mView.findViewById( |
| 112 | com.android.internal.R.id.clearDefaultHint); |
| 113 | mClearDefaultHint.setVisibility(View.GONE); |
| 114 | } |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 115 | mAdapter = new ResolveListAdapter(this, intent, initialIntents, rList); |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 116 | int count = mAdapter.getCount(); |
Mike Lockwood | bce6f8f | 2011-03-13 17:26:52 -0400 | [diff] [blame] | 117 | if (count > 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | ap.mAdapter = mAdapter; |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 119 | } else if (count == 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | startActivity(mAdapter.intentForPosition(0)); |
| 121 | finish(); |
| 122 | return; |
| 123 | } else { |
| 124 | ap.mMessage = getResources().getText(com.android.internal.R.string.noApplications); |
| 125 | } |
| 126 | |
| 127 | setupAlert(); |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 128 | |
| 129 | ListView lv = mAlert.getListView(); |
| 130 | if (lv != null) { |
| 131 | lv.setOnItemLongClickListener(new ItemLongClickListener()); |
| 132 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 135 | @Override |
| 136 | protected void onRestart() { |
| 137 | super.onRestart(); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame^] | 138 | mPackageMonitor.register(this, getMainLooper(), false); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 139 | mAdapter.handlePackagesChanged(); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected void onStop() { |
| 144 | super.onStop(); |
| 145 | mPackageMonitor.unregister(); |
| 146 | } |
| 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | public void onClick(DialogInterface dialog, int which) { |
| 149 | ResolveInfo ri = mAdapter.resolveInfoForPosition(which); |
| 150 | Intent intent = mAdapter.intentForPosition(which); |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 151 | boolean alwaysCheck = (mAlwaysCheck != null && mAlwaysCheck.isChecked()); |
| 152 | onIntentSelected(ri, intent, alwaysCheck); |
| 153 | finish(); |
| 154 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 156 | protected void onIntentSelected(ResolveInfo ri, Intent intent, boolean alwaysCheck) { |
| 157 | if (alwaysCheck) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | // Build a reasonable intent filter, based on what matched. |
| 159 | IntentFilter filter = new IntentFilter(); |
| 160 | |
| 161 | if (intent.getAction() != null) { |
| 162 | filter.addAction(intent.getAction()); |
| 163 | } |
| 164 | Set<String> categories = intent.getCategories(); |
| 165 | if (categories != null) { |
| 166 | for (String cat : categories) { |
| 167 | filter.addCategory(cat); |
| 168 | } |
| 169 | } |
| 170 | filter.addCategory(Intent.CATEGORY_DEFAULT); |
| 171 | |
| 172 | int cat = ri.match&IntentFilter.MATCH_CATEGORY_MASK; |
| 173 | Uri data = intent.getData(); |
| 174 | if (cat == IntentFilter.MATCH_CATEGORY_TYPE) { |
| 175 | String mimeType = intent.resolveType(this); |
| 176 | if (mimeType != null) { |
| 177 | try { |
| 178 | filter.addDataType(mimeType); |
| 179 | } catch (IntentFilter.MalformedMimeTypeException e) { |
| 180 | Log.w("ResolverActivity", e); |
| 181 | filter = null; |
| 182 | } |
| 183 | } |
Dianne Hackborn | 5ef402b | 2010-03-26 17:17:25 -0700 | [diff] [blame] | 184 | } |
| 185 | if (data != null && data.getScheme() != null) { |
| 186 | // We need the data specification if there was no type, |
| 187 | // OR if the scheme is not one of our magical "file:" |
| 188 | // or "content:" schemes (see IntentFilter for the reason). |
| 189 | if (cat != IntentFilter.MATCH_CATEGORY_TYPE |
| 190 | || (!"file".equals(data.getScheme()) |
| 191 | && !"content".equals(data.getScheme()))) { |
| 192 | filter.addDataScheme(data.getScheme()); |
| 193 | |
| 194 | // Look through the resolved filter to determine which part |
| 195 | // of it matched the original Intent. |
| 196 | Iterator<IntentFilter.AuthorityEntry> aIt = ri.filter.authoritiesIterator(); |
| 197 | if (aIt != null) { |
| 198 | while (aIt.hasNext()) { |
| 199 | IntentFilter.AuthorityEntry a = aIt.next(); |
| 200 | if (a.match(data) >= 0) { |
| 201 | int port = a.getPort(); |
| 202 | filter.addDataAuthority(a.getHost(), |
| 203 | port >= 0 ? Integer.toString(port) : null); |
| 204 | break; |
| 205 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
Dianne Hackborn | 5ef402b | 2010-03-26 17:17:25 -0700 | [diff] [blame] | 208 | Iterator<PatternMatcher> pIt = ri.filter.pathsIterator(); |
| 209 | if (pIt != null) { |
| 210 | String path = data.getPath(); |
| 211 | while (path != null && pIt.hasNext()) { |
| 212 | PatternMatcher p = pIt.next(); |
| 213 | if (p.match(path)) { |
| 214 | filter.addDataPath(p.getPath(), p.getType()); |
| 215 | break; |
| 216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (filter != null) { |
| 223 | final int N = mAdapter.mList.size(); |
| 224 | ComponentName[] set = new ComponentName[N]; |
| 225 | int bestMatch = 0; |
| 226 | for (int i=0; i<N; i++) { |
| 227 | ResolveInfo r = mAdapter.mList.get(i).ri; |
| 228 | set[i] = new ComponentName(r.activityInfo.packageName, |
| 229 | r.activityInfo.name); |
| 230 | if (r.match > bestMatch) bestMatch = r.match; |
| 231 | } |
| 232 | getPackageManager().addPreferredActivity(filter, bestMatch, set, |
| 233 | intent.getComponent()); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (intent != null) { |
| 238 | startActivity(intent); |
| 239 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | private final class DisplayResolveInfo { |
| 243 | ResolveInfo ri; |
| 244 | CharSequence displayLabel; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 245 | Drawable displayIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | CharSequence extendedInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 247 | Intent origIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 249 | DisplayResolveInfo(ResolveInfo pri, CharSequence pLabel, |
| 250 | CharSequence pInfo, Intent pOrigIntent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | ri = pri; |
| 252 | displayLabel = pLabel; |
| 253 | extendedInfo = pInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 254 | origIntent = pOrigIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | private final class ResolveListAdapter extends BaseAdapter { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 259 | private final Intent[] mInitialIntents; |
| 260 | private final List<ResolveInfo> mBaseResolveList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | private final Intent mIntent; |
| 262 | private final LayoutInflater mInflater; |
| 263 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 264 | private List<ResolveInfo> mCurrentResolveList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | private List<DisplayResolveInfo> mList; |
| 266 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 267 | public ResolveListAdapter(Context context, Intent intent, |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 268 | Intent[] initialIntents, List<ResolveInfo> rList) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | mIntent = new Intent(intent); |
| 270 | mIntent.setComponent(null); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 271 | mInitialIntents = initialIntents; |
| 272 | mBaseResolveList = rList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 274 | rebuildList(); |
| 275 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 277 | public void handlePackagesChanged() { |
| 278 | rebuildList(); |
| 279 | notifyDataSetChanged(); |
| 280 | if (mList.size() <= 0) { |
| 281 | // We no longer have any items... just finish the activity. |
| 282 | finish(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | private void rebuildList() { |
| 287 | if (mBaseResolveList != null) { |
| 288 | mCurrentResolveList = mBaseResolveList; |
| 289 | } else { |
| 290 | mCurrentResolveList = mPm.queryIntentActivities( |
| 291 | mIntent, PackageManager.MATCH_DEFAULT_ONLY |
Jeff Hamilton | d88e9aa | 2011-01-24 14:53:00 -0600 | [diff] [blame] | 292 | | (mAlwaysCheck != null ? PackageManager.GET_RESOLVED_FILTER : 0)); |
| 293 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | int N; |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 295 | if ((mCurrentResolveList != null) && ((N = mCurrentResolveList.size()) > 0)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | // Only display the first matches that are either of equal |
| 297 | // priority or have asked to be default options. |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 298 | ResolveInfo r0 = mCurrentResolveList.get(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | for (int i=1; i<N; i++) { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 300 | ResolveInfo ri = mCurrentResolveList.get(i); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 301 | if (false) Log.v( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | "ResolveListActivity", |
| 303 | r0.activityInfo.name + "=" + |
| 304 | r0.priority + "/" + r0.isDefault + " vs " + |
| 305 | ri.activityInfo.name + "=" + |
| 306 | ri.priority + "/" + ri.isDefault); |
| 307 | if (r0.priority != ri.priority || |
| 308 | r0.isDefault != ri.isDefault) { |
| 309 | while (i < N) { |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 310 | mCurrentResolveList.remove(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | N--; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | if (N > 1) { |
| 316 | ResolveInfo.DisplayNameComparator rComparator = |
| 317 | new ResolveInfo.DisplayNameComparator(mPm); |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 318 | Collections.sort(mCurrentResolveList, rComparator); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 320 | |
| 321 | mList = new ArrayList<DisplayResolveInfo>(); |
| 322 | |
| 323 | // First put the initial items at the top. |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 324 | if (mInitialIntents != null) { |
| 325 | for (int i=0; i<mInitialIntents.length; i++) { |
| 326 | Intent ii = mInitialIntents[i]; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 327 | if (ii == null) { |
| 328 | continue; |
| 329 | } |
| 330 | ActivityInfo ai = ii.resolveActivityInfo( |
| 331 | getPackageManager(), 0); |
| 332 | if (ai == null) { |
| 333 | Log.w("ResolverActivity", "No activity found for " |
| 334 | + ii); |
| 335 | continue; |
| 336 | } |
| 337 | ResolveInfo ri = new ResolveInfo(); |
| 338 | ri.activityInfo = ai; |
| 339 | if (ii instanceof LabeledIntent) { |
| 340 | LabeledIntent li = (LabeledIntent)ii; |
| 341 | ri.resolvePackageName = li.getSourcePackage(); |
| 342 | ri.labelRes = li.getLabelResource(); |
| 343 | ri.nonLocalizedLabel = li.getNonLocalizedLabel(); |
| 344 | ri.icon = li.getIconResource(); |
| 345 | } |
| 346 | mList.add(new DisplayResolveInfo(ri, |
| 347 | ri.loadLabel(getPackageManager()), null, ii)); |
| 348 | } |
| 349 | } |
| 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | // Check for applications with same name and use application name or |
| 352 | // package name if necessary |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 353 | r0 = mCurrentResolveList.get(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | int start = 0; |
| 355 | CharSequence r0Label = r0.loadLabel(mPm); |
| 356 | for (int i = 1; i < N; i++) { |
| 357 | if (r0Label == null) { |
| 358 | r0Label = r0.activityInfo.packageName; |
| 359 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 360 | ResolveInfo ri = mCurrentResolveList.get(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | CharSequence riLabel = ri.loadLabel(mPm); |
| 362 | if (riLabel == null) { |
| 363 | riLabel = ri.activityInfo.packageName; |
| 364 | } |
| 365 | if (riLabel.equals(r0Label)) { |
| 366 | continue; |
| 367 | } |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 368 | processGroup(mCurrentResolveList, start, (i-1), r0, r0Label); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | r0 = ri; |
| 370 | r0Label = riLabel; |
| 371 | start = i; |
| 372 | } |
| 373 | // Process last group |
Dianne Hackborn | e8f2c7f | 2012-03-01 17:30:53 -0800 | [diff] [blame] | 374 | processGroup(mCurrentResolveList, start, (N-1), r0, r0Label); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | private void processGroup(List<ResolveInfo> rList, int start, int end, ResolveInfo ro, |
| 379 | CharSequence roLabel) { |
| 380 | // Process labels from start to i |
| 381 | int num = end - start+1; |
| 382 | if (num == 1) { |
| 383 | // No duplicate labels. Use label for entry at start |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 384 | mList.add(new DisplayResolveInfo(ro, roLabel, null, null)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | } else { |
| 386 | boolean usePkg = false; |
| 387 | CharSequence startApp = ro.activityInfo.applicationInfo.loadLabel(mPm); |
| 388 | if (startApp == null) { |
| 389 | usePkg = true; |
| 390 | } |
| 391 | if (!usePkg) { |
| 392 | // Use HashSet to track duplicates |
| 393 | HashSet<CharSequence> duplicates = |
| 394 | new HashSet<CharSequence>(); |
| 395 | duplicates.add(startApp); |
| 396 | for (int j = start+1; j <= end ; j++) { |
| 397 | ResolveInfo jRi = rList.get(j); |
| 398 | CharSequence jApp = jRi.activityInfo.applicationInfo.loadLabel(mPm); |
| 399 | if ( (jApp == null) || (duplicates.contains(jApp))) { |
| 400 | usePkg = true; |
| 401 | break; |
| 402 | } else { |
| 403 | duplicates.add(jApp); |
| 404 | } |
| 405 | } |
| 406 | // Clear HashSet for later use |
| 407 | duplicates.clear(); |
| 408 | } |
| 409 | for (int k = start; k <= end; k++) { |
| 410 | ResolveInfo add = rList.get(k); |
| 411 | if (usePkg) { |
| 412 | // Use application name for all entries from start to end-1 |
| 413 | mList.add(new DisplayResolveInfo(add, roLabel, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 414 | add.activityInfo.packageName, null)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | } else { |
| 416 | // Use package name for all entries from start to end-1 |
| 417 | mList.add(new DisplayResolveInfo(add, roLabel, |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 418 | add.activityInfo.applicationInfo.loadLabel(mPm), null)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | public ResolveInfo resolveInfoForPosition(int position) { |
| 425 | if (mList == null) { |
| 426 | return null; |
| 427 | } |
| 428 | |
| 429 | return mList.get(position).ri; |
| 430 | } |
| 431 | |
| 432 | public Intent intentForPosition(int position) { |
| 433 | if (mList == null) { |
| 434 | return null; |
| 435 | } |
| 436 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 437 | DisplayResolveInfo dri = mList.get(position); |
| 438 | |
| 439 | Intent intent = new Intent(dri.origIntent != null |
| 440 | ? dri.origIntent : mIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT |
| 442 | |Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 443 | ActivityInfo ai = dri.ri.activityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | intent.setComponent(new ComponentName( |
| 445 | ai.applicationInfo.packageName, ai.name)); |
| 446 | return intent; |
| 447 | } |
| 448 | |
| 449 | public int getCount() { |
| 450 | return mList != null ? mList.size() : 0; |
| 451 | } |
| 452 | |
| 453 | public Object getItem(int position) { |
| 454 | return position; |
| 455 | } |
| 456 | |
| 457 | public long getItemId(int position) { |
| 458 | return position; |
| 459 | } |
| 460 | |
| 461 | public View getView(int position, View convertView, ViewGroup parent) { |
| 462 | View view; |
| 463 | if (convertView == null) { |
| 464 | view = mInflater.inflate( |
| 465 | com.android.internal.R.layout.resolve_list_item, parent, false); |
| 466 | } else { |
| 467 | view = convertView; |
| 468 | } |
| 469 | bindView(view, mList.get(position)); |
| 470 | return view; |
| 471 | } |
| 472 | |
| 473 | private final void bindView(View view, DisplayResolveInfo info) { |
| 474 | TextView text = (TextView)view.findViewById(com.android.internal.R.id.text1); |
| 475 | TextView text2 = (TextView)view.findViewById(com.android.internal.R.id.text2); |
| 476 | ImageView icon = (ImageView)view.findViewById(R.id.icon); |
| 477 | text.setText(info.displayLabel); |
| 478 | if (info.extendedInfo != null) { |
| 479 | text2.setVisibility(View.VISIBLE); |
| 480 | text2.setText(info.extendedInfo); |
| 481 | } else { |
| 482 | text2.setVisibility(View.GONE); |
| 483 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 484 | if (info.displayIcon == null) { |
| 485 | info.displayIcon = info.ri.loadIcon(mPm); |
| 486 | } |
| 487 | icon.setImageDrawable(info.displayIcon); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 492 | if (mClearDefaultHint == null) return; |
| 493 | |
| 494 | if(isChecked) { |
| 495 | mClearDefaultHint.setVisibility(View.VISIBLE); |
| 496 | } else { |
| 497 | mClearDefaultHint.setVisibility(View.GONE); |
| 498 | } |
| 499 | } |
Adam Powell | 2d80962 | 2012-03-22 15:24:43 -0700 | [diff] [blame] | 500 | |
| 501 | class ItemLongClickListener implements AdapterView.OnItemLongClickListener { |
| 502 | |
| 503 | @Override |
| 504 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 505 | ResolveInfo ri = mAdapter.resolveInfoForPosition(position); |
| 506 | Intent in = new Intent().setAction("android.settings.APPLICATION_DETAILS_SETTINGS") |
| 507 | .setData(Uri.fromParts("package", ri.activityInfo.packageName, null)); |
| 508 | startActivity(in); |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | } |
| 514 | |