blob: d4769e85a4a165deb72720fe30d207db3d75004e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Dianne Hackborn1d442e02009-04-20 18:14:05 -070019import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import java.util.ArrayList;
21import java.util.Collections;
22import java.util.Comparator;
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.Iterator;
26import java.util.List;
27import java.util.Map;
28import java.util.Set;
29
Jeff Brown2c376fc2011-01-28 17:34:01 -080030import android.net.Uri;
31import android.util.FastImmutableArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Log;
Dianne Hackborncef65ee2010-09-30 18:27:22 -070033import android.util.PrintWriterPrinter;
Joe Onorato8a9b2202010-02-26 18:56:32 -080034import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.util.LogPrinter;
36import android.util.Printer;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -070037import android.util.StringBuilderPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.Intent;
40import android.content.IntentFilter;
41
42/**
43 * {@hide}
44 */
Dianne Hackborn6c418d52011-06-29 14:05:33 -070045public abstract class IntentResolver<F extends IntentFilter, R extends Object> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 final private static String TAG = "IntentResolver";
47 final private static boolean DEBUG = false;
Joe Onorato43a17652011-04-06 19:22:23 -070048 final private static boolean localLOGV = DEBUG || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50 public void addFilter(F f) {
51 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080052 Slog.v(TAG, "Adding filter: " + f);
53 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
54 Slog.v(TAG, " Building Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 }
56
57 mFilters.add(f);
58 int numS = register_intent_filter(f, f.schemesIterator(),
59 mSchemeToFilter, " Scheme: ");
60 int numT = register_mime_types(f, " Type: ");
61 if (numS == 0 && numT == 0) {
62 register_intent_filter(f, f.actionsIterator(),
63 mActionToFilter, " Action: ");
64 }
65 if (numT != 0) {
66 register_intent_filter(f, f.actionsIterator(),
67 mTypedActionToFilter, " TypedAction: ");
68 }
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -070069
70 mOldResolver.addFilter(f);
71 verifyDataStructures(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 }
73
74 public void removeFilter(F f) {
75 removeFilterInternal(f);
76 mFilters.remove(f);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -070077
78 mOldResolver.removeFilter(f);
79 verifyDataStructures(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
81
82 void removeFilterInternal(F f) {
83 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080084 Slog.v(TAG, "Removing filter: " + f);
85 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
86 Slog.v(TAG, " Cleaning Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 }
88
89 int numS = unregister_intent_filter(f, f.schemesIterator(),
90 mSchemeToFilter, " Scheme: ");
91 int numT = unregister_mime_types(f, " Type: ");
92 if (numS == 0 && numT == 0) {
93 unregister_intent_filter(f, f.actionsIterator(),
94 mActionToFilter, " Action: ");
95 }
96 if (numT != 0) {
97 unregister_intent_filter(f, f.actionsIterator(),
98 mTypedActionToFilter, " TypedAction: ");
99 }
100 }
101
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700102 boolean dumpMap(PrintWriter out, String titlePrefix, String title,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700103 String prefix, Map<String, F[]> map, String packageName,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700104 boolean printFilter) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 String eprefix = prefix + " ";
106 String fprefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700107 boolean printedSomething = false;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700108 Printer printer = null;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700109 for (Map.Entry<String, F[]> e : map.entrySet()) {
110 F[] a = e.getValue();
111 final int N = a.length;
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700112 boolean printedHeader = false;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700113 F filter;
114 for (int i=0; i<N && (filter=a[i]) != null; i++) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700115 if (packageName != null && !packageName.equals(packageForFilter(filter))) {
116 continue;
117 }
118 if (title != null) {
119 out.print(titlePrefix); out.println(title);
120 title = null;
121 }
122 if (!printedHeader) {
123 out.print(eprefix); out.print(e.getKey()); out.println(":");
124 printedHeader = true;
125 }
126 printedSomething = true;
127 dumpFilter(out, fprefix, filter);
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700128 if (printFilter) {
129 if (printer == null) {
130 printer = new PrintWriterPrinter(out);
131 }
132 filter.dump(printer, fprefix + " ");
133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 }
135 }
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700136 return printedSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 }
138
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700139 public boolean dump(PrintWriter out, String title, String prefix, String packageName,
140 boolean printFilter) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700141 String innerPrefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700142 String sepPrefix = "\n" + prefix;
143 String curPrefix = title + "\n" + prefix;
144 if (dumpMap(out, curPrefix, "Full MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700145 mTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700146 curPrefix = sepPrefix;
147 }
148 if (dumpMap(out, curPrefix, "Base MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700149 mBaseTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700150 curPrefix = sepPrefix;
151 }
152 if (dumpMap(out, curPrefix, "Wild MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700153 mWildTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700154 curPrefix = sepPrefix;
155 }
156 if (dumpMap(out, curPrefix, "Schemes:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700157 mSchemeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700158 curPrefix = sepPrefix;
159 }
160 if (dumpMap(out, curPrefix, "Non-Data Actions:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700161 mActionToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700162 curPrefix = sepPrefix;
163 }
164 if (dumpMap(out, curPrefix, "MIME Typed Actions:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700165 mTypedActionToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700166 curPrefix = sepPrefix;
167 }
168 return curPrefix == sepPrefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
170
171 private class IteratorWrapper implements Iterator<F> {
172 private final Iterator<F> mI;
173 private F mCur;
174
175 IteratorWrapper(Iterator<F> it) {
176 mI = it;
177 }
178
179 public boolean hasNext() {
180 return mI.hasNext();
181 }
182
183 public F next() {
184 return (mCur = mI.next());
185 }
186
187 public void remove() {
188 if (mCur != null) {
189 removeFilterInternal(mCur);
190 }
191 mI.remove();
192 }
193
194 }
195
196 /**
197 * Returns an iterator allowing filters to be removed.
198 */
199 public Iterator<F> filterIterator() {
200 return new IteratorWrapper(mFilters.iterator());
201 }
202
203 /**
204 * Returns a read-only set of the filters.
205 */
206 public Set<F> filterSet() {
207 return Collections.unmodifiableSet(mFilters);
208 }
209
Mihai Predaeae850c2009-05-13 10:13:48 +0200210 public List<R> queryIntentFromList(Intent intent, String resolvedType,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700211 boolean defaultOnly, ArrayList<F[]> listCut, int userId) {
Mihai Predaeae850c2009-05-13 10:13:48 +0200212 ArrayList<R> resultList = new ArrayList<R>();
213
214 final boolean debug = localLOGV ||
215 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
216
Jeff Brown2c376fc2011-01-28 17:34:01 -0800217 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
Mihai Predaeae850c2009-05-13 10:13:48 +0200218 final String scheme = intent.getScheme();
219 int N = listCut.size();
220 for (int i = 0; i < N; ++i) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800221 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700222 resolvedType, scheme, listCut.get(i), resultList, userId);
Mihai Predaeae850c2009-05-13 10:13:48 +0200223 }
224 sortResults(resultList);
225 return resultList;
226 }
227
Amith Yamasani483f3b02012-03-13 16:08:00 -0700228 public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly,
229 int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 String scheme = intent.getScheme();
231
232 ArrayList<R> finalList = new ArrayList<R>();
233
234 final boolean debug = localLOGV ||
235 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
236
Joe Onorato8a9b2202010-02-26 18:56:32 -0800237 if (debug) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 TAG, "Resolving type " + resolvedType + " scheme " + scheme
239 + " of intent " + intent);
240
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700241 F[] firstTypeCut = null;
242 F[] secondTypeCut = null;
243 F[] thirdTypeCut = null;
244 F[] schemeCut = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
246 // If the intent includes a MIME type, then we want to collect all of
247 // the filters that match that MIME type.
248 if (resolvedType != null) {
249 int slashpos = resolvedType.indexOf('/');
250 if (slashpos > 0) {
251 final String baseType = resolvedType.substring(0, slashpos);
252 if (!baseType.equals("*")) {
253 if (resolvedType.length() != slashpos+2
254 || resolvedType.charAt(slashpos+1) != '*') {
255 // Not a wild card, so we can just look for all filters that
256 // completely match or wildcards whose base type matches.
257 firstTypeCut = mTypeToFilter.get(resolvedType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800258 if (debug) Slog.v(TAG, "First type cut: " + firstTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 secondTypeCut = mWildTypeToFilter.get(baseType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800260 if (debug) Slog.v(TAG, "Second type cut: " + secondTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 } else {
262 // We can match anything with our base type.
263 firstTypeCut = mBaseTypeToFilter.get(baseType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800264 if (debug) Slog.v(TAG, "First type cut: " + firstTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 secondTypeCut = mWildTypeToFilter.get(baseType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800266 if (debug) Slog.v(TAG, "Second type cut: " + secondTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 }
268 // Any */* types always apply, but we only need to do this
269 // if the intent type was not already */*.
270 thirdTypeCut = mWildTypeToFilter.get("*");
Joe Onorato8a9b2202010-02-26 18:56:32 -0800271 if (debug) Slog.v(TAG, "Third type cut: " + thirdTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 } else if (intent.getAction() != null) {
273 // The intent specified any type ({@literal *}/*). This
274 // can be a whole heck of a lot of things, so as a first
275 // cut let's use the action instead.
276 firstTypeCut = mTypedActionToFilter.get(intent.getAction());
Joe Onorato8a9b2202010-02-26 18:56:32 -0800277 if (debug) Slog.v(TAG, "Typed Action list: " + firstTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 }
279 }
280 }
281
282 // If the intent includes a data URI, then we want to collect all of
283 // the filters that match its scheme (we will further refine matches
284 // on the authority and path by directly matching each resulting filter).
285 if (scheme != null) {
286 schemeCut = mSchemeToFilter.get(scheme);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800287 if (debug) Slog.v(TAG, "Scheme list: " + schemeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 }
289
290 // If the intent does not specify any data -- either a MIME type or
291 // a URI -- then we will only be looking for matches against empty
292 // data.
293 if (resolvedType == null && scheme == null && intent.getAction() != null) {
294 firstTypeCut = mActionToFilter.get(intent.getAction());
Joe Onorato8a9b2202010-02-26 18:56:32 -0800295 if (debug) Slog.v(TAG, "Action list: " + firstTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
297
Jeff Brown2c376fc2011-01-28 17:34:01 -0800298 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 if (firstTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800300 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700301 resolvedType, scheme, firstTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 if (secondTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800304 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700305 resolvedType, scheme, secondTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307 if (thirdTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800308 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700309 resolvedType, scheme, thirdTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
311 if (schemeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800312 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700313 resolvedType, scheme, schemeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
315 sortResults(finalList);
316
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700317 List<R> oldList = mOldResolver.queryIntent(intent, resolvedType, defaultOnly, userId);
318 if (oldList.size() != finalList.size()) {
319 ValidationFailure here = new ValidationFailure();
320 here.fillInStackTrace();
321 Log.wtf(TAG, "Query result " + intent + " size is " + finalList.size()
322 + "; old implementation is " + oldList.size(), here);
323 }
324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800326 Slog.v(TAG, "Final result list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 for (R r : finalList) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800328 Slog.v(TAG, " " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330 }
331 return finalList;
332 }
333
334 /**
335 * Control whether the given filter is allowed to go into the result
336 * list. Mainly intended to prevent adding multiple filters for the
337 * same target object.
338 */
339 protected boolean allowFilterResult(F filter, List<R> dest) {
340 return true;
341 }
342
Dianne Hackborne7f97212011-02-24 14:40:20 -0800343 /**
344 * Returns whether the object associated with the given filter is
345 * "stopped," that is whether it should not be included in the result
346 * if the intent requests to excluded stopped objects.
347 */
Amith Yamasani483f3b02012-03-13 16:08:00 -0700348 protected boolean isFilterStopped(F filter, int userId) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800349 return false;
350 }
351
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700352 /**
353 * Return the package that owns this filter. This must be implemented to
354 * provide correct filtering of Intents that have specified a package name
355 * they are to be delivered to.
356 */
357 protected abstract String packageForFilter(F filter);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700358
359 protected abstract F[] newArray(int size);
360
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700361 @SuppressWarnings("unchecked")
Amith Yamasani483f3b02012-03-13 16:08:00 -0700362 protected R newResult(F filter, int match, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 return (R)filter;
364 }
365
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700366 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 protected void sortResults(List<R> results) {
368 Collections.sort(results, mResolvePrioritySorter);
369 }
370
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700371 protected void dumpFilter(PrintWriter out, String prefix, F filter) {
372 out.print(prefix); out.println(filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 }
374
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700375 private final void addFilter(HashMap<String, F[]> map, String name, F filter) {
376 F[] array = map.get(name);
377 if (array == null) {
378 array = newArray(2);
379 map.put(name, array);
380 array[0] = filter;
381 } else {
382 final int N = array.length;
383 int i = N;
384 while (i > 0 && array[i-1] == null) {
385 i--;
386 }
387 if (i < N) {
388 array[i] = filter;
389 } else {
390 F[] newa = newArray((N*3)/2);
391 System.arraycopy(array, 0, newa, 0, N);
392 newa[N] = filter;
393 map.put(name, newa);
394 }
395 }
396 }
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 private final int register_mime_types(F filter, String prefix) {
399 final Iterator<String> i = filter.typesIterator();
400 if (i == null) {
401 return 0;
402 }
403
404 int num = 0;
405 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800406 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800408 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 String baseName = name;
410 final int slashpos = name.indexOf('/');
411 if (slashpos > 0) {
412 baseName = name.substring(0, slashpos).intern();
413 } else {
414 name = name + "/*";
415 }
416
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700417 addFilter(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
419 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700420 addFilter(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700422 addFilter(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 }
424 }
425
426 return num;
427 }
428
429 private final int unregister_mime_types(F filter, String prefix) {
430 final Iterator<String> i = filter.typesIterator();
431 if (i == null) {
432 return 0;
433 }
434
435 int num = 0;
436 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800437 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800439 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 String baseName = name;
441 final int slashpos = name.indexOf('/');
442 if (slashpos > 0) {
443 baseName = name.substring(0, slashpos).intern();
444 } else {
445 name = name + "/*";
446 }
447
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700448 remove_all_objects(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449
450 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700451 remove_all_objects(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700453 remove_all_objects(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455 }
456 return num;
457 }
458
459 private final int register_intent_filter(F filter, Iterator<String> i,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700460 HashMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 if (i == null) {
462 return 0;
463 }
464
465 int num = 0;
466 while (i.hasNext()) {
467 String name = i.next();
468 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800469 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700470 addFilter(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472 return num;
473 }
474
475 private final int unregister_intent_filter(F filter, Iterator<String> i,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700476 HashMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 if (i == null) {
478 return 0;
479 }
480
481 int num = 0;
482 while (i.hasNext()) {
483 String name = i.next();
484 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800485 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700486 remove_all_objects(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488 return num;
489 }
490
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700491 private final void remove_all_objects(HashMap<String, F[]> map, String name,
492 Object object) {
493 F[] array = map.get(name);
494 if (array != null) {
495 int LAST = array.length-1;
496 while (LAST >= 0 && array[LAST] == null) {
497 LAST--;
498 }
499 for (int idx=LAST; idx>=0; idx--) {
500 if (array[idx] == object) {
501 final int remain = LAST - idx;
502 if (remain > 0) {
503 System.arraycopy(array, idx+1, array, idx, remain);
504 }
505 array[LAST] = null;
506 LAST--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 }
508 }
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700509 if (LAST < 0) {
510 map.remove(name);
511 } else if (LAST < (array.length/2)) {
512 F[] newa = newArray(LAST+2);
513 System.arraycopy(array, 0, newa, 0, LAST+1);
514 map.put(name, newa);
515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518
Jeff Brown2c376fc2011-01-28 17:34:01 -0800519 private static FastImmutableArraySet<String> getFastIntentCategories(Intent intent) {
520 final Set<String> categories = intent.getCategories();
521 if (categories == null) {
522 return null;
523 }
524 return new FastImmutableArraySet<String>(categories.toArray(new String[categories.size()]));
525 }
526
527 private void buildResolveList(Intent intent, FastImmutableArraySet<String> categories,
528 boolean debug, boolean defaultOnly,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700529 String resolvedType, String scheme, F[] src, List<R> dest, int userId) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800530 final String action = intent.getAction();
531 final Uri data = intent.getData();
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700532 final String packageName = intent.getPackage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533
Dianne Hackborne7f97212011-02-24 14:40:20 -0800534 final boolean excludingStopped = intent.isExcludingStopped();
535
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700536 final int N = src != null ? src.length : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 boolean hasNonDefaults = false;
538 int i;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700539 F filter;
540 for (i=0; i<N && (filter=src[i]) != null; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 int match;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800542 if (debug) Slog.v(TAG, "Matching against filter " + filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
Amith Yamasani483f3b02012-03-13 16:08:00 -0700544 if (excludingStopped && isFilterStopped(filter, userId)) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800545 if (debug) {
546 Slog.v(TAG, " Filter's target is stopped; skipping");
547 }
548 continue;
549 }
550
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700551 // Is delivery being limited to filters owned by a particular package?
552 if (packageName != null && !packageName.equals(packageForFilter(filter))) {
553 if (debug) {
554 Slog.v(TAG, " Filter is not from package " + packageName + "; skipping");
555 }
556 continue;
557 }
558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 // Do we already have this one?
560 if (!allowFilterResult(filter, dest)) {
561 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800562 Slog.v(TAG, " Filter's target already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 continue;
565 }
566
Jeff Brown2c376fc2011-01-28 17:34:01 -0800567 match = filter.match(action, resolvedType, scheme, data, categories, TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 if (match >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800569 if (debug) Slog.v(TAG, " Filter matched! match=0x" +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 Integer.toHexString(match));
571 if (!defaultOnly || filter.hasCategory(Intent.CATEGORY_DEFAULT)) {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700572 final R oneResult = newResult(filter, match, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 if (oneResult != null) {
574 dest.add(oneResult);
575 }
576 } else {
577 hasNonDefaults = true;
578 }
579 } else {
580 if (debug) {
581 String reason;
582 switch (match) {
583 case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;
584 case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;
585 case IntentFilter.NO_MATCH_DATA: reason = "data"; break;
586 case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;
587 default: reason = "unknown reason"; break;
588 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800589 Slog.v(TAG, " Filter did not match: " + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
591 }
592 }
593
594 if (dest.size() == 0 && hasNonDefaults) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800595 Slog.w(TAG, "resolveIntent failed: found match, but none with Intent.CATEGORY_DEFAULT");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
597 }
598
599 // Sorts a List of IntentFilter objects into descending priority order.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700600 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 private static final Comparator mResolvePrioritySorter = new Comparator() {
602 public int compare(Object o1, Object o2) {
Kenny Root502e9a42011-01-10 13:48:15 -0800603 final int q1 = ((IntentFilter) o1).getPriority();
604 final int q2 = ((IntentFilter) o2).getPriority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 return (q1 > q2) ? -1 : ((q1 < q2) ? 1 : 0);
606 }
607 };
608
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700609 static class ValidationFailure extends RuntimeException {
610 }
611
612 private void verifyDataStructures(IntentFilter src) {
613 compareMaps(src, "mTypeToFilter", mTypeToFilter, mOldResolver.mTypeToFilter);
614 compareMaps(src, "mBaseTypeToFilter", mBaseTypeToFilter, mOldResolver.mBaseTypeToFilter);
615 compareMaps(src, "mWildTypeToFilter", mWildTypeToFilter, mOldResolver.mWildTypeToFilter);
616 compareMaps(src, "mSchemeToFilter", mSchemeToFilter, mOldResolver.mSchemeToFilter);
617 compareMaps(src, "mActionToFilter", mActionToFilter, mOldResolver.mActionToFilter);
618 compareMaps(src, "mTypedActionToFilter", mTypedActionToFilter, mOldResolver.mTypedActionToFilter);
619 }
620
621 private void compareMaps(IntentFilter src, String name, HashMap<String, F[]> cur,
622 HashMap<String, ArrayList<F>> old) {
623 if (cur.size() != old.size()) {
624 StringBuilder missing = new StringBuilder(128);
625 for (Map.Entry<String, ArrayList<F>> e : old.entrySet()) {
626 final F[] curArray = cur.get(e.getKey());
627 if (curArray == null) {
628 if (missing.length() > 0) {
629 missing.append(' ');
630 }
631 missing.append(e.getKey());
632 }
633 }
634 StringBuilder extra = new StringBuilder(128);
635 for (Map.Entry<String, F[]> e : cur.entrySet()) {
636 if (old.get(e.getKey()) == null) {
637 if (extra.length() > 0) {
638 extra.append(' ');
639 }
640 extra.append(e.getKey());
641 }
642 }
643 StringBuilder srcStr = new StringBuilder(1024);
644 StringBuilderPrinter printer = new StringBuilderPrinter(srcStr);
645 src.dump(printer, "");
646 ValidationFailure here = new ValidationFailure();
647 here.fillInStackTrace();
648 Log.wtf(TAG, "New map " + name + " size is " + cur.size()
649 + "; old implementation is " + old.size()
650 + "; missing: " + missing.toString()
651 + "; extra: " + extra.toString()
652 + "; src: " + srcStr.toString(), here);
653 return;
654 }
655 for (Map.Entry<String, ArrayList<F>> e : old.entrySet()) {
656 final F[] curArray = cur.get(e.getKey());
657 int curLen = curArray != null ? curArray.length : 0;
658 if (curLen == 0) {
659 ValidationFailure here = new ValidationFailure();
660 here.fillInStackTrace();
661 Log.wtf(TAG, "New map " + name + " doesn't contain expected key "
662 + e.getKey() + " (array=" + curArray + ")");
663 return;
664 }
665 while (curLen > 0 && curArray[curLen-1] == null) {
666 curLen--;
667 }
668 final ArrayList<F> oldArray = e.getValue();
669 final int oldLen = oldArray.size();
670 if (curLen != oldLen) {
671 ValidationFailure here = new ValidationFailure();
672 here.fillInStackTrace();
673 Log.wtf(TAG, "New map " + name + " entry " + e.getKey() + " size is "
674 + curLen + "; old implementation is " + oldLen, here);
675 return;
676 }
677 for (int i=0; i<oldLen; i++) {
678 F f = oldArray.get(i);
679 boolean found = false;
680 for (int j=0; j<curLen; j++) {
681 if (curArray[j] == f) {
682 found = true;
683 break;
684 }
685 }
686 if (!found) {
687 ValidationFailure here = new ValidationFailure();
688 here.fillInStackTrace();
689 Log.wtf(TAG, "New map " + name + " entry + " + e.getKey()
690 + " doesn't contain expected filter " + f, here);
691 }
692 }
693 for (int i=0; i<curLen; i++) {
694 if (curArray[i] == null) {
695 ValidationFailure here = new ValidationFailure();
696 here.fillInStackTrace();
697 Log.wtf(TAG, "New map " + name + " entry + " + e.getKey()
698 + " has unexpected null at " + i + "; array: " + curArray, here);
699 break;
700 }
701 }
702 }
703 }
704
705 private final IntentResolverOld<F, R> mOldResolver = new IntentResolverOld<F, R>() {
706 @Override protected String packageForFilter(F filter) {
707 return IntentResolver.this.packageForFilter(filter);
708 }
709 @Override protected boolean allowFilterResult(F filter, List<R> dest) {
710 return IntentResolver.this.allowFilterResult(filter, dest);
711 }
712 @Override protected boolean isFilterStopped(F filter, int userId) {
713 return IntentResolver.this.isFilterStopped(filter, userId);
714 }
715 @Override protected R newResult(F filter, int match, int userId) {
716 return IntentResolver.this.newResult(filter, match, userId);
717 }
718 @Override protected void sortResults(List<R> results) {
719 IntentResolver.this.sortResults(results);
720 }
721 };
722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 /**
724 * All filters that have been registered.
725 */
726 private final HashSet<F> mFilters = new HashSet<F>();
727
728 /**
729 * All of the MIME types that have been registered, such as "image/jpeg",
730 * "image/*", or "{@literal *}/*".
731 */
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700732 private final HashMap<String, F[]> mTypeToFilter = new HashMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
734 /**
735 * The base names of all of all fully qualified MIME types that have been
736 * registered, such as "image" or "*". Wild card MIME types such as
737 * "image/*" will not be here.
738 */
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700739 private final HashMap<String, F[]> mBaseTypeToFilter = new HashMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740
741 /**
742 * The base names of all of the MIME types with a sub-type wildcard that
743 * have been registered. For example, a filter with "image/*" will be
744 * included here as "image" but one with "image/jpeg" will not be
745 * included here. This also includes the "*" for the "{@literal *}/*"
746 * MIME type.
747 */
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700748 private final HashMap<String, F[]> mWildTypeToFilter = new HashMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749
750 /**
751 * All of the URI schemes (such as http) that have been registered.
752 */
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700753 private final HashMap<String, F[]> mSchemeToFilter = new HashMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754
755 /**
756 * All of the actions that have been registered, but only those that did
757 * not specify data.
758 */
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700759 private final HashMap<String, F[]> mActionToFilter = new HashMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760
761 /**
762 * All of the actions that have been registered and specified a MIME type.
763 */
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700764 private final HashMap<String, F[]> mTypedActionToFilter = new HashMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765}