| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.server; |
| 18 | |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 19 | import java.io.PrintWriter; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import java.util.ArrayList; |
| 21 | import java.util.Collections; |
| 22 | import java.util.Comparator; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import android.util.Log; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 31 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.util.LogPrinter; |
| 33 | import android.util.Printer; |
| 34 | |
| 35 | import android.util.Config; |
| 36 | import android.content.ContentResolver; |
| 37 | import android.content.Intent; |
| 38 | import android.content.IntentFilter; |
| 39 | |
| 40 | /** |
| 41 | * {@hide} |
| 42 | */ |
| 43 | public class IntentResolver<F extends IntentFilter, R extends Object> { |
| 44 | final private static String TAG = "IntentResolver"; |
| 45 | final private static boolean DEBUG = false; |
| 46 | final private static boolean localLOGV = DEBUG || Config.LOGV; |
| 47 | |
| 48 | public void addFilter(F f) { |
| 49 | if (localLOGV) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 50 | Slog.v(TAG, "Adding filter: " + f); |
| 51 | f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " "); |
| 52 | Slog.v(TAG, " Building Lookup Maps:"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | mFilters.add(f); |
| 56 | int numS = register_intent_filter(f, f.schemesIterator(), |
| 57 | mSchemeToFilter, " Scheme: "); |
| 58 | int numT = register_mime_types(f, " Type: "); |
| 59 | if (numS == 0 && numT == 0) { |
| 60 | register_intent_filter(f, f.actionsIterator(), |
| 61 | mActionToFilter, " Action: "); |
| 62 | } |
| 63 | if (numT != 0) { |
| 64 | register_intent_filter(f, f.actionsIterator(), |
| 65 | mTypedActionToFilter, " TypedAction: "); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public void removeFilter(F f) { |
| 70 | removeFilterInternal(f); |
| 71 | mFilters.remove(f); |
| 72 | } |
| 73 | |
| 74 | void removeFilterInternal(F f) { |
| 75 | if (localLOGV) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 76 | Slog.v(TAG, "Removing filter: " + f); |
| 77 | f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " "); |
| 78 | Slog.v(TAG, " Cleaning Lookup Maps:"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | int numS = unregister_intent_filter(f, f.schemesIterator(), |
| 82 | mSchemeToFilter, " Scheme: "); |
| 83 | int numT = unregister_mime_types(f, " Type: "); |
| 84 | if (numS == 0 && numT == 0) { |
| 85 | unregister_intent_filter(f, f.actionsIterator(), |
| 86 | mActionToFilter, " Action: "); |
| 87 | } |
| 88 | if (numT != 0) { |
| 89 | unregister_intent_filter(f, f.actionsIterator(), |
| 90 | mTypedActionToFilter, " TypedAction: "); |
| 91 | } |
| 92 | } |
| 93 | |
| Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 94 | boolean dumpMap(PrintWriter out, String titlePrefix, String title, |
| 95 | String prefix, Map<String, ArrayList<F>> map, String packageName) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | String eprefix = prefix + " "; |
| 97 | String fprefix = prefix + " "; |
| Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 98 | boolean printedSomething = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | for (Map.Entry<String, ArrayList<F>> e : map.entrySet()) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | ArrayList<F> a = e.getValue(); |
| 101 | final int N = a.size(); |
| Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 102 | boolean printedHeader = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | for (int i=0; i<N; i++) { |
| Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 104 | F filter = a.get(i); |
| 105 | if (packageName != null && !packageName.equals(packageForFilter(filter))) { |
| 106 | continue; |
| 107 | } |
| 108 | if (title != null) { |
| 109 | out.print(titlePrefix); out.println(title); |
| 110 | title = null; |
| 111 | } |
| 112 | if (!printedHeader) { |
| 113 | out.print(eprefix); out.print(e.getKey()); out.println(":"); |
| 114 | printedHeader = true; |
| 115 | } |
| 116 | printedSomething = true; |
| 117 | dumpFilter(out, fprefix, filter); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 120 | return printedSomething; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 123 | public boolean dump(PrintWriter out, String title, String prefix, String packageName) { |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 124 | String innerPrefix = prefix + " "; |
| Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 125 | String sepPrefix = "\n" + prefix; |
| 126 | String curPrefix = title + "\n" + prefix; |
| 127 | if (dumpMap(out, curPrefix, "Full MIME Types:", innerPrefix, |
| 128 | mTypeToFilter, packageName)) { |
| 129 | curPrefix = sepPrefix; |
| 130 | } |
| 131 | if (dumpMap(out, curPrefix, "Base MIME Types:", innerPrefix, |
| 132 | mBaseTypeToFilter, packageName)) { |
| 133 | curPrefix = sepPrefix; |
| 134 | } |
| 135 | if (dumpMap(out, curPrefix, "Wild MIME Types:", innerPrefix, |
| 136 | mWildTypeToFilter, packageName)) { |
| 137 | curPrefix = sepPrefix; |
| 138 | } |
| 139 | if (dumpMap(out, curPrefix, "Schemes:", innerPrefix, |
| 140 | mSchemeToFilter, packageName)) { |
| 141 | curPrefix = sepPrefix; |
| 142 | } |
| 143 | if (dumpMap(out, curPrefix, "Non-Data Actions:", innerPrefix, |
| 144 | mActionToFilter, packageName)) { |
| 145 | curPrefix = sepPrefix; |
| 146 | } |
| 147 | if (dumpMap(out, curPrefix, "MIME Typed Actions:", innerPrefix, |
| 148 | mTypedActionToFilter, packageName)) { |
| 149 | curPrefix = sepPrefix; |
| 150 | } |
| 151 | return curPrefix == sepPrefix; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | private class IteratorWrapper implements Iterator<F> { |
| 155 | private final Iterator<F> mI; |
| 156 | private F mCur; |
| 157 | |
| 158 | IteratorWrapper(Iterator<F> it) { |
| 159 | mI = it; |
| 160 | } |
| 161 | |
| 162 | public boolean hasNext() { |
| 163 | return mI.hasNext(); |
| 164 | } |
| 165 | |
| 166 | public F next() { |
| 167 | return (mCur = mI.next()); |
| 168 | } |
| 169 | |
| 170 | public void remove() { |
| 171 | if (mCur != null) { |
| 172 | removeFilterInternal(mCur); |
| 173 | } |
| 174 | mI.remove(); |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Returns an iterator allowing filters to be removed. |
| 181 | */ |
| 182 | public Iterator<F> filterIterator() { |
| 183 | return new IteratorWrapper(mFilters.iterator()); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns a read-only set of the filters. |
| 188 | */ |
| 189 | public Set<F> filterSet() { |
| 190 | return Collections.unmodifiableSet(mFilters); |
| 191 | } |
| 192 | |
| Mihai Preda | eae850c | 2009-05-13 10:13:48 +0200 | [diff] [blame] | 193 | public List<R> queryIntentFromList(Intent intent, String resolvedType, |
| 194 | boolean defaultOnly, ArrayList<ArrayList<F>> listCut) { |
| 195 | ArrayList<R> resultList = new ArrayList<R>(); |
| 196 | |
| 197 | final boolean debug = localLOGV || |
| 198 | ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0); |
| 199 | |
| 200 | final String scheme = intent.getScheme(); |
| 201 | int N = listCut.size(); |
| 202 | for (int i = 0; i < N; ++i) { |
| 203 | buildResolveList(intent, debug, defaultOnly, |
| 204 | resolvedType, scheme, listCut.get(i), resultList); |
| 205 | } |
| 206 | sortResults(resultList); |
| 207 | return resultList; |
| 208 | } |
| 209 | |
| Mihai Preda | 074edef | 2009-05-18 17:13:31 +0200 | [diff] [blame] | 210 | public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | String scheme = intent.getScheme(); |
| 212 | |
| 213 | ArrayList<R> finalList = new ArrayList<R>(); |
| 214 | |
| 215 | final boolean debug = localLOGV || |
| 216 | ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0); |
| 217 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 218 | if (debug) Slog.v( |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | TAG, "Resolving type " + resolvedType + " scheme " + scheme |
| 220 | + " of intent " + intent); |
| 221 | |
| 222 | ArrayList<F> firstTypeCut = null; |
| 223 | ArrayList<F> secondTypeCut = null; |
| 224 | ArrayList<F> thirdTypeCut = null; |
| 225 | ArrayList<F> schemeCut = null; |
| 226 | |
| 227 | // If the intent includes a MIME type, then we want to collect all of |
| 228 | // the filters that match that MIME type. |
| 229 | if (resolvedType != null) { |
| 230 | int slashpos = resolvedType.indexOf('/'); |
| 231 | if (slashpos > 0) { |
| 232 | final String baseType = resolvedType.substring(0, slashpos); |
| 233 | if (!baseType.equals("*")) { |
| 234 | if (resolvedType.length() != slashpos+2 |
| 235 | || resolvedType.charAt(slashpos+1) != '*') { |
| 236 | // Not a wild card, so we can just look for all filters that |
| 237 | // completely match or wildcards whose base type matches. |
| 238 | firstTypeCut = mTypeToFilter.get(resolvedType); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 239 | if (debug) Slog.v(TAG, "First type cut: " + firstTypeCut); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | secondTypeCut = mWildTypeToFilter.get(baseType); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 241 | if (debug) Slog.v(TAG, "Second type cut: " + secondTypeCut); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } else { |
| 243 | // We can match anything with our base type. |
| 244 | firstTypeCut = mBaseTypeToFilter.get(baseType); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 245 | if (debug) Slog.v(TAG, "First type cut: " + firstTypeCut); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | secondTypeCut = mWildTypeToFilter.get(baseType); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 247 | if (debug) Slog.v(TAG, "Second type cut: " + secondTypeCut); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | } |
| 249 | // Any */* types always apply, but we only need to do this |
| 250 | // if the intent type was not already */*. |
| 251 | thirdTypeCut = mWildTypeToFilter.get("*"); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 252 | if (debug) Slog.v(TAG, "Third type cut: " + thirdTypeCut); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } else if (intent.getAction() != null) { |
| 254 | // The intent specified any type ({@literal *}/*). This |
| 255 | // can be a whole heck of a lot of things, so as a first |
| 256 | // cut let's use the action instead. |
| 257 | firstTypeCut = mTypedActionToFilter.get(intent.getAction()); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 258 | if (debug) Slog.v(TAG, "Typed Action list: " + firstTypeCut); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // If the intent includes a data URI, then we want to collect all of |
| 264 | // the filters that match its scheme (we will further refine matches |
| 265 | // on the authority and path by directly matching each resulting filter). |
| 266 | if (scheme != null) { |
| 267 | schemeCut = mSchemeToFilter.get(scheme); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 268 | if (debug) Slog.v(TAG, "Scheme list: " + schemeCut); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // If the intent does not specify any data -- either a MIME type or |
| 272 | // a URI -- then we will only be looking for matches against empty |
| 273 | // data. |
| 274 | if (resolvedType == null && scheme == null && intent.getAction() != null) { |
| 275 | firstTypeCut = mActionToFilter.get(intent.getAction()); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 276 | if (debug) Slog.v(TAG, "Action list: " + firstTypeCut); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (firstTypeCut != null) { |
| 280 | buildResolveList(intent, debug, defaultOnly, |
| 281 | resolvedType, scheme, firstTypeCut, finalList); |
| 282 | } |
| 283 | if (secondTypeCut != null) { |
| 284 | buildResolveList(intent, debug, defaultOnly, |
| 285 | resolvedType, scheme, secondTypeCut, finalList); |
| 286 | } |
| 287 | if (thirdTypeCut != null) { |
| 288 | buildResolveList(intent, debug, defaultOnly, |
| 289 | resolvedType, scheme, thirdTypeCut, finalList); |
| 290 | } |
| 291 | if (schemeCut != null) { |
| 292 | buildResolveList(intent, debug, defaultOnly, |
| 293 | resolvedType, scheme, schemeCut, finalList); |
| 294 | } |
| 295 | sortResults(finalList); |
| 296 | |
| 297 | if (debug) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 298 | Slog.v(TAG, "Final result list:"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | for (R r : finalList) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 300 | Slog.v(TAG, " " + r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | return finalList; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Control whether the given filter is allowed to go into the result |
| 308 | * list. Mainly intended to prevent adding multiple filters for the |
| 309 | * same target object. |
| 310 | */ |
| 311 | protected boolean allowFilterResult(F filter, List<R> dest) { |
| 312 | return true; |
| 313 | } |
| 314 | |
| Dianne Hackborn | d4310ac | 2010-03-16 22:55:08 -0700 | [diff] [blame] | 315 | protected String packageForFilter(F filter) { |
| 316 | return null; |
| 317 | } |
| 318 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | protected R newResult(F filter, int match) { |
| 320 | return (R)filter; |
| 321 | } |
| 322 | |
| 323 | protected void sortResults(List<R> results) { |
| 324 | Collections.sort(results, mResolvePrioritySorter); |
| 325 | } |
| 326 | |
| Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 327 | protected void dumpFilter(PrintWriter out, String prefix, F filter) { |
| 328 | out.print(prefix); out.println(filter); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | private final int register_mime_types(F filter, String prefix) { |
| 332 | final Iterator<String> i = filter.typesIterator(); |
| 333 | if (i == null) { |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | int num = 0; |
| 338 | while (i.hasNext()) { |
| 339 | String name = (String)i.next(); |
| 340 | num++; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 341 | if (localLOGV) Slog.v(TAG, prefix + name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | String baseName = name; |
| 343 | final int slashpos = name.indexOf('/'); |
| 344 | if (slashpos > 0) { |
| 345 | baseName = name.substring(0, slashpos).intern(); |
| 346 | } else { |
| 347 | name = name + "/*"; |
| 348 | } |
| 349 | |
| 350 | ArrayList<F> array = mTypeToFilter.get(name); |
| 351 | if (array == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 352 | //Slog.v(TAG, "Creating new array for " + name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | array = new ArrayList<F>(); |
| 354 | mTypeToFilter.put(name, array); |
| 355 | } |
| 356 | array.add(filter); |
| 357 | |
| 358 | if (slashpos > 0) { |
| 359 | array = mBaseTypeToFilter.get(baseName); |
| 360 | if (array == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 361 | //Slog.v(TAG, "Creating new array for " + name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | array = new ArrayList<F>(); |
| 363 | mBaseTypeToFilter.put(baseName, array); |
| 364 | } |
| 365 | array.add(filter); |
| 366 | } else { |
| 367 | array = mWildTypeToFilter.get(baseName); |
| 368 | if (array == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 369 | //Slog.v(TAG, "Creating new array for " + name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | array = new ArrayList<F>(); |
| 371 | mWildTypeToFilter.put(baseName, array); |
| 372 | } |
| 373 | array.add(filter); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return num; |
| 378 | } |
| 379 | |
| 380 | private final int unregister_mime_types(F filter, String prefix) { |
| 381 | final Iterator<String> i = filter.typesIterator(); |
| 382 | if (i == null) { |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | int num = 0; |
| 387 | while (i.hasNext()) { |
| 388 | String name = (String)i.next(); |
| 389 | num++; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 390 | if (localLOGV) Slog.v(TAG, prefix + name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | String baseName = name; |
| 392 | final int slashpos = name.indexOf('/'); |
| 393 | if (slashpos > 0) { |
| 394 | baseName = name.substring(0, slashpos).intern(); |
| 395 | } else { |
| 396 | name = name + "/*"; |
| 397 | } |
| 398 | |
| 399 | if (!remove_all_objects(mTypeToFilter.get(name), filter)) { |
| 400 | mTypeToFilter.remove(name); |
| 401 | } |
| 402 | |
| 403 | if (slashpos > 0) { |
| 404 | if (!remove_all_objects(mBaseTypeToFilter.get(baseName), filter)) { |
| 405 | mBaseTypeToFilter.remove(baseName); |
| 406 | } |
| 407 | } else { |
| 408 | if (!remove_all_objects(mWildTypeToFilter.get(baseName), filter)) { |
| 409 | mWildTypeToFilter.remove(baseName); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | return num; |
| 414 | } |
| 415 | |
| 416 | private final int register_intent_filter(F filter, Iterator<String> i, |
| 417 | HashMap<String, ArrayList<F>> dest, String prefix) { |
| 418 | if (i == null) { |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | int num = 0; |
| 423 | while (i.hasNext()) { |
| 424 | String name = i.next(); |
| 425 | num++; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 426 | if (localLOGV) Slog.v(TAG, prefix + name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | ArrayList<F> array = dest.get(name); |
| 428 | if (array == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 429 | //Slog.v(TAG, "Creating new array for " + name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | array = new ArrayList<F>(); |
| 431 | dest.put(name, array); |
| 432 | } |
| 433 | array.add(filter); |
| 434 | } |
| 435 | return num; |
| 436 | } |
| 437 | |
| 438 | private final int unregister_intent_filter(F filter, Iterator<String> i, |
| 439 | HashMap<String, ArrayList<F>> dest, String prefix) { |
| 440 | if (i == null) { |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | int num = 0; |
| 445 | while (i.hasNext()) { |
| 446 | String name = i.next(); |
| 447 | num++; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 448 | if (localLOGV) Slog.v(TAG, prefix + name); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | if (!remove_all_objects(dest.get(name), filter)) { |
| 450 | dest.remove(name); |
| 451 | } |
| 452 | } |
| 453 | return num; |
| 454 | } |
| 455 | |
| 456 | private final boolean remove_all_objects(List<F> list, Object object) { |
| 457 | if (list != null) { |
| 458 | int N = list.size(); |
| 459 | for (int idx=0; idx<N; idx++) { |
| 460 | if (list.get(idx) == object) { |
| 461 | list.remove(idx); |
| 462 | idx--; |
| 463 | N--; |
| 464 | } |
| 465 | } |
| 466 | return N > 0; |
| 467 | } |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | private void buildResolveList(Intent intent, boolean debug, boolean defaultOnly, |
| 472 | String resolvedType, String scheme, List<F> src, List<R> dest) { |
| 473 | Set<String> categories = intent.getCategories(); |
| 474 | |
| 475 | final int N = src != null ? src.size() : 0; |
| 476 | boolean hasNonDefaults = false; |
| 477 | int i; |
| 478 | for (i=0; i<N; i++) { |
| 479 | F filter = src.get(i); |
| 480 | int match; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 481 | if (debug) Slog.v(TAG, "Matching against filter " + filter); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | |
| 483 | // Do we already have this one? |
| 484 | if (!allowFilterResult(filter, dest)) { |
| 485 | if (debug) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 486 | Slog.v(TAG, " Filter's target already added"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | } |
| 488 | continue; |
| 489 | } |
| 490 | |
| 491 | match = filter.match( |
| 492 | intent.getAction(), resolvedType, scheme, intent.getData(), categories, TAG); |
| 493 | if (match >= 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 494 | if (debug) Slog.v(TAG, " Filter matched! match=0x" + |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | Integer.toHexString(match)); |
| 496 | if (!defaultOnly || filter.hasCategory(Intent.CATEGORY_DEFAULT)) { |
| 497 | final R oneResult = newResult(filter, match); |
| 498 | if (oneResult != null) { |
| 499 | dest.add(oneResult); |
| 500 | } |
| 501 | } else { |
| 502 | hasNonDefaults = true; |
| 503 | } |
| 504 | } else { |
| 505 | if (debug) { |
| 506 | String reason; |
| 507 | switch (match) { |
| 508 | case IntentFilter.NO_MATCH_ACTION: reason = "action"; break; |
| 509 | case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break; |
| 510 | case IntentFilter.NO_MATCH_DATA: reason = "data"; break; |
| 511 | case IntentFilter.NO_MATCH_TYPE: reason = "type"; break; |
| 512 | default: reason = "unknown reason"; break; |
| 513 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 514 | Slog.v(TAG, " Filter did not match: " + reason); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (dest.size() == 0 && hasNonDefaults) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 520 | Slog.w(TAG, "resolveIntent failed: found match, but none with Intent.CATEGORY_DEFAULT"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | // Sorts a List of IntentFilter objects into descending priority order. |
| 525 | private static final Comparator mResolvePrioritySorter = new Comparator() { |
| 526 | public int compare(Object o1, Object o2) { |
| 527 | float q1 = ((IntentFilter)o1).getPriority(); |
| 528 | float q2 = ((IntentFilter)o2).getPriority(); |
| 529 | return (q1 > q2) ? -1 : ((q1 < q2) ? 1 : 0); |
| 530 | } |
| 531 | }; |
| 532 | |
| 533 | /** |
| 534 | * All filters that have been registered. |
| 535 | */ |
| 536 | private final HashSet<F> mFilters = new HashSet<F>(); |
| 537 | |
| 538 | /** |
| 539 | * All of the MIME types that have been registered, such as "image/jpeg", |
| 540 | * "image/*", or "{@literal *}/*". |
| 541 | */ |
| 542 | private final HashMap<String, ArrayList<F>> mTypeToFilter |
| 543 | = new HashMap<String, ArrayList<F>>(); |
| 544 | |
| 545 | /** |
| 546 | * The base names of all of all fully qualified MIME types that have been |
| 547 | * registered, such as "image" or "*". Wild card MIME types such as |
| 548 | * "image/*" will not be here. |
| 549 | */ |
| 550 | private final HashMap<String, ArrayList<F>> mBaseTypeToFilter |
| 551 | = new HashMap<String, ArrayList<F>>(); |
| 552 | |
| 553 | /** |
| 554 | * The base names of all of the MIME types with a sub-type wildcard that |
| 555 | * have been registered. For example, a filter with "image/*" will be |
| 556 | * included here as "image" but one with "image/jpeg" will not be |
| 557 | * included here. This also includes the "*" for the "{@literal *}/*" |
| 558 | * MIME type. |
| 559 | */ |
| 560 | private final HashMap<String, ArrayList<F>> mWildTypeToFilter |
| 561 | = new HashMap<String, ArrayList<F>>(); |
| 562 | |
| 563 | /** |
| 564 | * All of the URI schemes (such as http) that have been registered. |
| 565 | */ |
| 566 | private final HashMap<String, ArrayList<F>> mSchemeToFilter |
| 567 | = new HashMap<String, ArrayList<F>>(); |
| 568 | |
| 569 | /** |
| 570 | * All of the actions that have been registered, but only those that did |
| 571 | * not specify data. |
| 572 | */ |
| 573 | private final HashMap<String, ArrayList<F>> mActionToFilter |
| 574 | = new HashMap<String, ArrayList<F>>(); |
| 575 | |
| 576 | /** |
| 577 | * All of the actions that have been registered and specified a MIME type. |
| 578 | */ |
| 579 | private final HashMap<String, ArrayList<F>> mTypedActionToFilter |
| 580 | = new HashMap<String, ArrayList<F>>(); |
| 581 | } |
| 582 | |