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