blob: 64b0487fa320864f9b2d53f55377bbd4e1386b4a [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;
Dianne Hackborn38ba6e92013-09-23 11:08:52 -070021import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import java.util.Collections;
23import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import 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;
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070032import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.Log;
Dianne Hackborncef65ee2010-09-30 18:27:22 -070034import android.util.PrintWriterPrinter;
Joe Onorato8a9b2202010-02-26 18:56:32 -080035import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.LogPrinter;
37import android.util.Printer;
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.Intent;
40import android.content.IntentFilter;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -070041import com.android.internal.util.FastPrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43/**
44 * {@hide}
45 */
Dianne Hackborn6c418d52011-06-29 14:05:33 -070046public abstract class IntentResolver<F extends IntentFilter, R extends Object> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 final private static String TAG = "IntentResolver";
48 final private static boolean DEBUG = false;
Joe Onorato43a17652011-04-06 19:22:23 -070049 final private static boolean localLOGV = DEBUG || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51 public void addFilter(F f) {
52 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080053 Slog.v(TAG, "Adding filter: " + f);
54 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
55 Slog.v(TAG, " Building Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 }
57
58 mFilters.add(f);
59 int numS = register_intent_filter(f, f.schemesIterator(),
60 mSchemeToFilter, " Scheme: ");
61 int numT = register_mime_types(f, " Type: ");
62 if (numS == 0 && numT == 0) {
63 register_intent_filter(f, f.actionsIterator(),
64 mActionToFilter, " Action: ");
65 }
66 if (numT != 0) {
67 register_intent_filter(f, f.actionsIterator(),
68 mTypedActionToFilter, " TypedAction: ");
69 }
70 }
71
72 public void removeFilter(F f) {
73 removeFilterInternal(f);
74 mFilters.remove(f);
75 }
76
77 void removeFilterInternal(F f) {
78 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080079 Slog.v(TAG, "Removing filter: " + f);
80 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
81 Slog.v(TAG, " Cleaning Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 }
83
84 int numS = unregister_intent_filter(f, f.schemesIterator(),
85 mSchemeToFilter, " Scheme: ");
86 int numT = unregister_mime_types(f, " Type: ");
87 if (numS == 0 && numT == 0) {
88 unregister_intent_filter(f, f.actionsIterator(),
89 mActionToFilter, " Action: ");
90 }
91 if (numT != 0) {
92 unregister_intent_filter(f, f.actionsIterator(),
93 mTypedActionToFilter, " TypedAction: ");
94 }
95 }
96
Dianne Hackbornd4310ac2010-03-16 22:55:08 -070097 boolean dumpMap(PrintWriter out, String titlePrefix, String title,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -070098 String prefix, Map<String, F[]> map, String packageName,
Dianne Hackborncef65ee2010-09-30 18:27:22 -070099 boolean printFilter) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 String eprefix = prefix + " ";
101 String fprefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700102 boolean printedSomething = false;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700103 Printer printer = null;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700104 for (Map.Entry<String, F[]> e : map.entrySet()) {
105 F[] a = e.getValue();
106 final int N = a.length;
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700107 boolean printedHeader = false;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700108 F filter;
109 for (int i=0; i<N && (filter=a[i]) != null; i++) {
Ben Gruver4efe9402013-04-02 21:18:41 -0700110 if (packageName != null && !isPackageForFilter(packageName, filter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700111 continue;
112 }
113 if (title != null) {
114 out.print(titlePrefix); out.println(title);
115 title = null;
116 }
117 if (!printedHeader) {
118 out.print(eprefix); out.print(e.getKey()); out.println(":");
119 printedHeader = true;
120 }
121 printedSomething = true;
122 dumpFilter(out, fprefix, filter);
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700123 if (printFilter) {
124 if (printer == null) {
125 printer = new PrintWriterPrinter(out);
126 }
127 filter.dump(printer, fprefix + " ");
128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 }
130 }
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700131 return printedSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 }
133
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700134 public boolean dump(PrintWriter out, String title, String prefix, String packageName,
135 boolean printFilter) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700136 String innerPrefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700137 String sepPrefix = "\n" + prefix;
138 String curPrefix = title + "\n" + prefix;
139 if (dumpMap(out, curPrefix, "Full MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700140 mTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700141 curPrefix = sepPrefix;
142 }
143 if (dumpMap(out, curPrefix, "Base MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700144 mBaseTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700145 curPrefix = sepPrefix;
146 }
147 if (dumpMap(out, curPrefix, "Wild MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700148 mWildTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700149 curPrefix = sepPrefix;
150 }
151 if (dumpMap(out, curPrefix, "Schemes:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700152 mSchemeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700153 curPrefix = sepPrefix;
154 }
155 if (dumpMap(out, curPrefix, "Non-Data Actions:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700156 mActionToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700157 curPrefix = sepPrefix;
158 }
159 if (dumpMap(out, curPrefix, "MIME Typed Actions:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700160 mTypedActionToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700161 curPrefix = sepPrefix;
162 }
163 return curPrefix == sepPrefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 }
165
166 private class IteratorWrapper implements Iterator<F> {
167 private final Iterator<F> mI;
168 private F mCur;
169
170 IteratorWrapper(Iterator<F> it) {
171 mI = it;
172 }
173
174 public boolean hasNext() {
175 return mI.hasNext();
176 }
177
178 public F next() {
179 return (mCur = mI.next());
180 }
181
182 public void remove() {
183 if (mCur != null) {
184 removeFilterInternal(mCur);
185 }
186 mI.remove();
187 }
188
189 }
190
191 /**
192 * Returns an iterator allowing filters to be removed.
193 */
194 public Iterator<F> filterIterator() {
195 return new IteratorWrapper(mFilters.iterator());
196 }
197
198 /**
199 * Returns a read-only set of the filters.
200 */
201 public Set<F> filterSet() {
202 return Collections.unmodifiableSet(mFilters);
203 }
204
Mihai Predaeae850c2009-05-13 10:13:48 +0200205 public List<R> queryIntentFromList(Intent intent, String resolvedType,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700206 boolean defaultOnly, ArrayList<F[]> listCut, int userId) {
Mihai Predaeae850c2009-05-13 10:13:48 +0200207 ArrayList<R> resultList = new ArrayList<R>();
208
209 final boolean debug = localLOGV ||
210 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
211
Jeff Brown2c376fc2011-01-28 17:34:01 -0800212 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
Mihai Predaeae850c2009-05-13 10:13:48 +0200213 final String scheme = intent.getScheme();
214 int N = listCut.size();
215 for (int i = 0; i < N; ++i) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800216 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700217 resolvedType, scheme, listCut.get(i), resultList, userId);
Mihai Predaeae850c2009-05-13 10:13:48 +0200218 }
219 sortResults(resultList);
220 return resultList;
221 }
222
Amith Yamasani483f3b02012-03-13 16:08:00 -0700223 public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly,
224 int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 String scheme = intent.getScheme();
226
227 ArrayList<R> finalList = new ArrayList<R>();
228
229 final boolean debug = localLOGV ||
230 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
231
Joe Onorato8a9b2202010-02-26 18:56:32 -0800232 if (debug) Slog.v(
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700233 TAG, "Resolving type=" + resolvedType + " scheme=" + scheme
234 + " defaultOnly=" + defaultOnly + " userId=" + userId + " of " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700236 F[] firstTypeCut = null;
237 F[] secondTypeCut = null;
238 F[] thirdTypeCut = null;
239 F[] schemeCut = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
241 // If the intent includes a MIME type, then we want to collect all of
242 // the filters that match that MIME type.
243 if (resolvedType != null) {
244 int slashpos = resolvedType.indexOf('/');
245 if (slashpos > 0) {
246 final String baseType = resolvedType.substring(0, slashpos);
247 if (!baseType.equals("*")) {
248 if (resolvedType.length() != slashpos+2
249 || resolvedType.charAt(slashpos+1) != '*') {
250 // Not a wild card, so we can just look for all filters that
251 // completely match or wildcards whose base type matches.
252 firstTypeCut = mTypeToFilter.get(resolvedType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700253 if (debug) Slog.v(TAG, "First type cut: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 secondTypeCut = mWildTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700255 if (debug) Slog.v(TAG, "Second type cut: "
256 + Arrays.toString(secondTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 } else {
258 // We can match anything with our base type.
259 firstTypeCut = mBaseTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700260 if (debug) Slog.v(TAG, "First type cut: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 secondTypeCut = mWildTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700262 if (debug) Slog.v(TAG, "Second type cut: "
263 + Arrays.toString(secondTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265 // Any */* types always apply, but we only need to do this
266 // if the intent type was not already */*.
267 thirdTypeCut = mWildTypeToFilter.get("*");
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700268 if (debug) Slog.v(TAG, "Third type cut: " + Arrays.toString(thirdTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 } else if (intent.getAction() != null) {
270 // The intent specified any type ({@literal *}/*). This
271 // can be a whole heck of a lot of things, so as a first
272 // cut let's use the action instead.
273 firstTypeCut = mTypedActionToFilter.get(intent.getAction());
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700274 if (debug) Slog.v(TAG, "Typed Action list: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276 }
277 }
278
279 // If the intent includes a data URI, then we want to collect all of
280 // the filters that match its scheme (we will further refine matches
281 // on the authority and path by directly matching each resulting filter).
282 if (scheme != null) {
283 schemeCut = mSchemeToFilter.get(scheme);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700284 if (debug) Slog.v(TAG, "Scheme list: " + Arrays.toString(schemeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 }
286
287 // If the intent does not specify any data -- either a MIME type or
288 // a URI -- then we will only be looking for matches against empty
289 // data.
290 if (resolvedType == null && scheme == null && intent.getAction() != null) {
291 firstTypeCut = mActionToFilter.get(intent.getAction());
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700292 if (debug) Slog.v(TAG, "Action list: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 }
294
Jeff Brown2c376fc2011-01-28 17:34:01 -0800295 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 if (firstTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800297 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700298 resolvedType, scheme, firstTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
300 if (secondTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800301 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700302 resolvedType, scheme, secondTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304 if (thirdTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800305 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700306 resolvedType, scheme, thirdTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308 if (schemeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800309 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700310 resolvedType, scheme, schemeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312 sortResults(finalList);
313
314 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800315 Slog.v(TAG, "Final result list:");
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700316 for (int i=0; i<finalList.size(); i++) {
317 Slog.v(TAG, " " + finalList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
319 }
320 return finalList;
321 }
322
323 /**
324 * Control whether the given filter is allowed to go into the result
325 * list. Mainly intended to prevent adding multiple filters for the
326 * same target object.
327 */
328 protected boolean allowFilterResult(F filter, List<R> dest) {
329 return true;
330 }
331
Dianne Hackborne7f97212011-02-24 14:40:20 -0800332 /**
333 * Returns whether the object associated with the given filter is
334 * "stopped," that is whether it should not be included in the result
335 * if the intent requests to excluded stopped objects.
336 */
Amith Yamasani483f3b02012-03-13 16:08:00 -0700337 protected boolean isFilterStopped(F filter, int userId) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800338 return false;
339 }
340
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700341 /**
Ben Gruver4efe9402013-04-02 21:18:41 -0700342 * Returns whether this filter is owned by this package. This must be
343 * implemented to provide correct filtering of Intents that have
344 * specified a package name they are to be delivered to.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700345 */
Ben Gruver4efe9402013-04-02 21:18:41 -0700346 protected abstract boolean isPackageForFilter(String packageName, F filter);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700347
348 protected abstract F[] newArray(int size);
349
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700350 @SuppressWarnings("unchecked")
Amith Yamasani483f3b02012-03-13 16:08:00 -0700351 protected R newResult(F filter, int match, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 return (R)filter;
353 }
354
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700355 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 protected void sortResults(List<R> results) {
357 Collections.sort(results, mResolvePrioritySorter);
358 }
359
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700360 protected void dumpFilter(PrintWriter out, String prefix, F filter) {
361 out.print(prefix); out.println(filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 }
363
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700364 private final void addFilter(ArrayMap<String, F[]> map, String name, F filter) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700365 F[] array = map.get(name);
366 if (array == null) {
367 array = newArray(2);
368 map.put(name, array);
369 array[0] = filter;
370 } else {
371 final int N = array.length;
372 int i = N;
373 while (i > 0 && array[i-1] == null) {
374 i--;
375 }
376 if (i < N) {
377 array[i] = filter;
378 } else {
379 F[] newa = newArray((N*3)/2);
380 System.arraycopy(array, 0, newa, 0, N);
381 newa[N] = filter;
382 map.put(name, newa);
383 }
384 }
385 }
386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 private final int register_mime_types(F filter, String prefix) {
388 final Iterator<String> i = filter.typesIterator();
389 if (i == null) {
390 return 0;
391 }
392
393 int num = 0;
394 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800395 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800397 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 String baseName = name;
399 final int slashpos = name.indexOf('/');
400 if (slashpos > 0) {
401 baseName = name.substring(0, slashpos).intern();
402 } else {
403 name = name + "/*";
404 }
405
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700406 addFilter(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407
408 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700409 addFilter(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700411 addFilter(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
413 }
414
415 return num;
416 }
417
418 private final int unregister_mime_types(F filter, String prefix) {
419 final Iterator<String> i = filter.typesIterator();
420 if (i == null) {
421 return 0;
422 }
423
424 int num = 0;
425 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800426 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800428 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 String baseName = name;
430 final int slashpos = name.indexOf('/');
431 if (slashpos > 0) {
432 baseName = name.substring(0, slashpos).intern();
433 } else {
434 name = name + "/*";
435 }
436
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700437 remove_all_objects(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
439 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700440 remove_all_objects(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700442 remove_all_objects(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444 }
445 return num;
446 }
447
448 private final int register_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700449 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 if (i == null) {
451 return 0;
452 }
453
454 int num = 0;
455 while (i.hasNext()) {
456 String name = i.next();
457 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800458 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700459 addFilter(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461 return num;
462 }
463
464 private final int unregister_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700465 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 if (i == null) {
467 return 0;
468 }
469
470 int num = 0;
471 while (i.hasNext()) {
472 String name = i.next();
473 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800474 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700475 remove_all_objects(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477 return num;
478 }
479
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700480 private final void remove_all_objects(ArrayMap<String, F[]> map, String name,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700481 Object object) {
482 F[] array = map.get(name);
483 if (array != null) {
484 int LAST = array.length-1;
485 while (LAST >= 0 && array[LAST] == null) {
486 LAST--;
487 }
488 for (int idx=LAST; idx>=0; idx--) {
489 if (array[idx] == object) {
490 final int remain = LAST - idx;
491 if (remain > 0) {
492 System.arraycopy(array, idx+1, array, idx, remain);
493 }
494 array[LAST] = null;
495 LAST--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497 }
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700498 if (LAST < 0) {
499 map.remove(name);
500 } else if (LAST < (array.length/2)) {
501 F[] newa = newArray(LAST+2);
502 System.arraycopy(array, 0, newa, 0, LAST+1);
503 map.put(name, newa);
504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507
Jeff Brown2c376fc2011-01-28 17:34:01 -0800508 private static FastImmutableArraySet<String> getFastIntentCategories(Intent intent) {
509 final Set<String> categories = intent.getCategories();
510 if (categories == null) {
511 return null;
512 }
513 return new FastImmutableArraySet<String>(categories.toArray(new String[categories.size()]));
514 }
515
516 private void buildResolveList(Intent intent, FastImmutableArraySet<String> categories,
517 boolean debug, boolean defaultOnly,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700518 String resolvedType, String scheme, F[] src, List<R> dest, int userId) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800519 final String action = intent.getAction();
520 final Uri data = intent.getData();
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700521 final String packageName = intent.getPackage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522
Dianne Hackborne7f97212011-02-24 14:40:20 -0800523 final boolean excludingStopped = intent.isExcludingStopped();
524
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700525 final Printer logPrinter;
526 final PrintWriter logPrintWriter;
527 if (debug) {
528 logPrinter = new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM);
529 logPrintWriter = new FastPrintWriter(logPrinter);
530 } else {
531 logPrinter = null;
532 logPrintWriter = null;
533 }
534
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700535 final int N = src != null ? src.length : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 boolean hasNonDefaults = false;
537 int i;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700538 F filter;
539 for (i=0; i<N && (filter=src[i]) != null; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 int match;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800541 if (debug) Slog.v(TAG, "Matching against filter " + filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542
Amith Yamasani483f3b02012-03-13 16:08:00 -0700543 if (excludingStopped && isFilterStopped(filter, userId)) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800544 if (debug) {
545 Slog.v(TAG, " Filter's target is stopped; skipping");
546 }
547 continue;
548 }
549
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700550 // Is delivery being limited to filters owned by a particular package?
Ben Gruver4efe9402013-04-02 21:18:41 -0700551 if (packageName != null && !isPackageForFilter(packageName, filter)) {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700552 if (debug) {
553 Slog.v(TAG, " Filter is not from package " + packageName + "; skipping");
554 }
555 continue;
556 }
557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 // Do we already have this one?
559 if (!allowFilterResult(filter, dest)) {
560 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800561 Slog.v(TAG, " Filter's target already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563 continue;
564 }
565
Jeff Brown2c376fc2011-01-28 17:34:01 -0800566 match = filter.match(action, resolvedType, scheme, data, categories, TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 if (match >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800568 if (debug) Slog.v(TAG, " Filter matched! match=0x" +
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700569 Integer.toHexString(match) + " hasDefault="
570 + filter.hasCategory(Intent.CATEGORY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 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);
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700575 if (debug) {
576 dumpFilter(logPrintWriter, " ", filter);
577 logPrintWriter.flush();
578 filter.dump(logPrinter, " ");
579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581 } else {
582 hasNonDefaults = true;
583 }
584 } else {
585 if (debug) {
586 String reason;
587 switch (match) {
588 case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;
589 case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;
590 case IntentFilter.NO_MATCH_DATA: reason = "data"; break;
591 case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;
592 default: reason = "unknown reason"; break;
593 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800594 Slog.v(TAG, " Filter did not match: " + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596 }
597 }
598
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700599 if (hasNonDefaults) {
600 if (dest.size() == 0) {
601 Slog.w(TAG, "resolveIntent failed: found match, but none with CATEGORY_DEFAULT");
602 } else if (dest.size() > 1) {
603 Slog.w(TAG, "resolveIntent: multiple matches, only some with CATEGORY_DEFAULT");
604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606 }
607
608 // Sorts a List of IntentFilter objects into descending priority order.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700609 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 private static final Comparator mResolvePrioritySorter = new Comparator() {
611 public int compare(Object o1, Object o2) {
Kenny Root502e9a42011-01-10 13:48:15 -0800612 final int q1 = ((IntentFilter) o1).getPriority();
613 final int q2 = ((IntentFilter) o2).getPriority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 return (q1 > q2) ? -1 : ((q1 < q2) ? 1 : 0);
615 }
616 };
617
618 /**
619 * All filters that have been registered.
620 */
621 private final HashSet<F> mFilters = new HashSet<F>();
622
623 /**
624 * All of the MIME types that have been registered, such as "image/jpeg",
625 * "image/*", or "{@literal *}/*".
626 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700627 private final ArrayMap<String, F[]> mTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
629 /**
630 * The base names of all of all fully qualified MIME types that have been
631 * registered, such as "image" or "*". Wild card MIME types such as
632 * "image/*" will not be here.
633 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700634 private final ArrayMap<String, F[]> mBaseTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635
636 /**
637 * The base names of all of the MIME types with a sub-type wildcard that
638 * have been registered. For example, a filter with "image/*" will be
639 * included here as "image" but one with "image/jpeg" will not be
640 * included here. This also includes the "*" for the "{@literal *}/*"
641 * MIME type.
642 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700643 private final ArrayMap<String, F[]> mWildTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644
645 /**
646 * All of the URI schemes (such as http) that have been registered.
647 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700648 private final ArrayMap<String, F[]> mSchemeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649
650 /**
651 * All of the actions that have been registered, but only those that did
652 * not specify data.
653 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700654 private final ArrayMap<String, F[]> mActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655
656 /**
657 * All of the actions that have been registered and specified a MIME type.
658 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700659 private final ArrayMap<String, F[]> mTypedActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660}