blob: 54aa50cd85ce400ddc5dd9f3a7f3d63815e6012e [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;
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 }
69 }
70
71 public void removeFilter(F f) {
72 removeFilterInternal(f);
73 mFilters.remove(f);
74 }
75
76 void removeFilterInternal(F f) {
77 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080078 Slog.v(TAG, "Removing filter: " + f);
79 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
80 Slog.v(TAG, " Cleaning Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
82
83 int numS = unregister_intent_filter(f, f.schemesIterator(),
84 mSchemeToFilter, " Scheme: ");
85 int numT = unregister_mime_types(f, " Type: ");
86 if (numS == 0 && numT == 0) {
87 unregister_intent_filter(f, f.actionsIterator(),
88 mActionToFilter, " Action: ");
89 }
90 if (numT != 0) {
91 unregister_intent_filter(f, f.actionsIterator(),
92 mTypedActionToFilter, " TypedAction: ");
93 }
94 }
95
Dianne Hackbornd4310ac2010-03-16 22:55:08 -070096 boolean dumpMap(PrintWriter out, String titlePrefix, String title,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -070097 String prefix, Map<String, F[]> map, String packageName,
Dianne Hackborncef65ee2010-09-30 18:27:22 -070098 boolean printFilter) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 String eprefix = prefix + " ";
100 String fprefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700101 boolean printedSomething = false;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700102 Printer printer = null;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700103 for (Map.Entry<String, F[]> e : map.entrySet()) {
104 F[] a = e.getValue();
105 final int N = a.length;
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700106 boolean printedHeader = false;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700107 F filter;
108 for (int i=0; i<N && (filter=a[i]) != null; i++) {
Ben Gruver4efe9402013-04-02 21:18:41 -0700109 if (packageName != null && !isPackageForFilter(packageName, filter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700110 continue;
111 }
112 if (title != null) {
113 out.print(titlePrefix); out.println(title);
114 title = null;
115 }
116 if (!printedHeader) {
117 out.print(eprefix); out.print(e.getKey()); out.println(":");
118 printedHeader = true;
119 }
120 printedSomething = true;
121 dumpFilter(out, fprefix, filter);
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700122 if (printFilter) {
123 if (printer == null) {
124 printer = new PrintWriterPrinter(out);
125 }
126 filter.dump(printer, fprefix + " ");
127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 }
129 }
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700130 return printedSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 }
132
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700133 public boolean dump(PrintWriter out, String title, String prefix, String packageName,
134 boolean printFilter) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700135 String innerPrefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700136 String sepPrefix = "\n" + prefix;
137 String curPrefix = title + "\n" + prefix;
138 if (dumpMap(out, curPrefix, "Full MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700139 mTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700140 curPrefix = sepPrefix;
141 }
142 if (dumpMap(out, curPrefix, "Base MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700143 mBaseTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700144 curPrefix = sepPrefix;
145 }
146 if (dumpMap(out, curPrefix, "Wild MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700147 mWildTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700148 curPrefix = sepPrefix;
149 }
150 if (dumpMap(out, curPrefix, "Schemes:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700151 mSchemeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700152 curPrefix = sepPrefix;
153 }
154 if (dumpMap(out, curPrefix, "Non-Data Actions:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700155 mActionToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700156 curPrefix = sepPrefix;
157 }
158 if (dumpMap(out, curPrefix, "MIME Typed Actions:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700159 mTypedActionToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700160 curPrefix = sepPrefix;
161 }
162 return curPrefix == sepPrefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 }
164
165 private class IteratorWrapper implements Iterator<F> {
166 private final Iterator<F> mI;
167 private F mCur;
168
169 IteratorWrapper(Iterator<F> it) {
170 mI = it;
171 }
172
173 public boolean hasNext() {
174 return mI.hasNext();
175 }
176
177 public F next() {
178 return (mCur = mI.next());
179 }
180
181 public void remove() {
182 if (mCur != null) {
183 removeFilterInternal(mCur);
184 }
185 mI.remove();
186 }
187
188 }
189
190 /**
191 * Returns an iterator allowing filters to be removed.
192 */
193 public Iterator<F> filterIterator() {
194 return new IteratorWrapper(mFilters.iterator());
195 }
196
197 /**
198 * Returns a read-only set of the filters.
199 */
200 public Set<F> filterSet() {
201 return Collections.unmodifiableSet(mFilters);
202 }
203
Mihai Predaeae850c2009-05-13 10:13:48 +0200204 public List<R> queryIntentFromList(Intent intent, String resolvedType,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700205 boolean defaultOnly, ArrayList<F[]> listCut, int userId) {
Mihai Predaeae850c2009-05-13 10:13:48 +0200206 ArrayList<R> resultList = new ArrayList<R>();
207
208 final boolean debug = localLOGV ||
209 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
210
Jeff Brown2c376fc2011-01-28 17:34:01 -0800211 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
Mihai Predaeae850c2009-05-13 10:13:48 +0200212 final String scheme = intent.getScheme();
213 int N = listCut.size();
214 for (int i = 0; i < N; ++i) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800215 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700216 resolvedType, scheme, listCut.get(i), resultList, userId);
Mihai Predaeae850c2009-05-13 10:13:48 +0200217 }
218 sortResults(resultList);
219 return resultList;
220 }
221
Amith Yamasani483f3b02012-03-13 16:08:00 -0700222 public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly,
223 int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 String scheme = intent.getScheme();
225
226 ArrayList<R> finalList = new ArrayList<R>();
227
228 final boolean debug = localLOGV ||
229 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
230
Joe Onorato8a9b2202010-02-26 18:56:32 -0800231 if (debug) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 TAG, "Resolving type " + resolvedType + " scheme " + scheme
233 + " of intent " + intent);
234
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700235 F[] firstTypeCut = null;
236 F[] secondTypeCut = null;
237 F[] thirdTypeCut = null;
238 F[] schemeCut = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
240 // If the intent includes a MIME type, then we want to collect all of
241 // the filters that match that MIME type.
242 if (resolvedType != null) {
243 int slashpos = resolvedType.indexOf('/');
244 if (slashpos > 0) {
245 final String baseType = resolvedType.substring(0, slashpos);
246 if (!baseType.equals("*")) {
247 if (resolvedType.length() != slashpos+2
248 || resolvedType.charAt(slashpos+1) != '*') {
249 // Not a wild card, so we can just look for all filters that
250 // completely match or wildcards whose base type matches.
251 firstTypeCut = mTypeToFilter.get(resolvedType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700252 if (debug) Slog.v(TAG, "First type cut: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 secondTypeCut = mWildTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700254 if (debug) Slog.v(TAG, "Second type cut: "
255 + Arrays.toString(secondTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 } else {
257 // We can match anything with our base type.
258 firstTypeCut = mBaseTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700259 if (debug) Slog.v(TAG, "First type cut: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 secondTypeCut = mWildTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700261 if (debug) Slog.v(TAG, "Second type cut: "
262 + Arrays.toString(secondTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
264 // Any */* types always apply, but we only need to do this
265 // if the intent type was not already */*.
266 thirdTypeCut = mWildTypeToFilter.get("*");
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700267 if (debug) Slog.v(TAG, "Third type cut: " + Arrays.toString(thirdTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 } else if (intent.getAction() != null) {
269 // The intent specified any type ({@literal *}/*). This
270 // can be a whole heck of a lot of things, so as a first
271 // cut let's use the action instead.
272 firstTypeCut = mTypedActionToFilter.get(intent.getAction());
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700273 if (debug) Slog.v(TAG, "Typed Action list: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275 }
276 }
277
278 // If the intent includes a data URI, then we want to collect all of
279 // the filters that match its scheme (we will further refine matches
280 // on the authority and path by directly matching each resulting filter).
281 if (scheme != null) {
282 schemeCut = mSchemeToFilter.get(scheme);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700283 if (debug) Slog.v(TAG, "Scheme list: " + Arrays.toString(schemeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285
286 // If the intent does not specify any data -- either a MIME type or
287 // a URI -- then we will only be looking for matches against empty
288 // data.
289 if (resolvedType == null && scheme == null && intent.getAction() != null) {
290 firstTypeCut = mActionToFilter.get(intent.getAction());
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700291 if (debug) Slog.v(TAG, "Action list: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 }
293
Jeff Brown2c376fc2011-01-28 17:34:01 -0800294 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 if (firstTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800296 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700297 resolvedType, scheme, firstTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 }
299 if (secondTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800300 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700301 resolvedType, scheme, secondTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 if (thirdTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800304 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700305 resolvedType, scheme, thirdTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307 if (schemeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800308 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700309 resolvedType, scheme, schemeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
311 sortResults(finalList);
312
313 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800314 Slog.v(TAG, "Final result list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 for (R r : finalList) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800316 Slog.v(TAG, " " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
318 }
319 return finalList;
320 }
321
322 /**
323 * Control whether the given filter is allowed to go into the result
324 * list. Mainly intended to prevent adding multiple filters for the
325 * same target object.
326 */
327 protected boolean allowFilterResult(F filter, List<R> dest) {
328 return true;
329 }
330
Dianne Hackborne7f97212011-02-24 14:40:20 -0800331 /**
332 * Returns whether the object associated with the given filter is
333 * "stopped," that is whether it should not be included in the result
334 * if the intent requests to excluded stopped objects.
335 */
Amith Yamasani483f3b02012-03-13 16:08:00 -0700336 protected boolean isFilterStopped(F filter, int userId) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800337 return false;
338 }
339
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700340 /**
Ben Gruver4efe9402013-04-02 21:18:41 -0700341 * Returns whether this filter is owned by this package. This must be
342 * implemented to provide correct filtering of Intents that have
343 * specified a package name they are to be delivered to.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700344 */
Ben Gruver4efe9402013-04-02 21:18:41 -0700345 protected abstract boolean isPackageForFilter(String packageName, F filter);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700346
347 protected abstract F[] newArray(int size);
348
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700349 @SuppressWarnings("unchecked")
Amith Yamasani483f3b02012-03-13 16:08:00 -0700350 protected R newResult(F filter, int match, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 return (R)filter;
352 }
353
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700354 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 protected void sortResults(List<R> results) {
356 Collections.sort(results, mResolvePrioritySorter);
357 }
358
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700359 protected void dumpFilter(PrintWriter out, String prefix, F filter) {
360 out.print(prefix); out.println(filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700363 private final void addFilter(ArrayMap<String, F[]> map, String name, F filter) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700364 F[] array = map.get(name);
365 if (array == null) {
366 array = newArray(2);
367 map.put(name, array);
368 array[0] = filter;
369 } else {
370 final int N = array.length;
371 int i = N;
372 while (i > 0 && array[i-1] == null) {
373 i--;
374 }
375 if (i < N) {
376 array[i] = filter;
377 } else {
378 F[] newa = newArray((N*3)/2);
379 System.arraycopy(array, 0, newa, 0, N);
380 newa[N] = filter;
381 map.put(name, newa);
382 }
383 }
384 }
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 private final int register_mime_types(F filter, String prefix) {
387 final Iterator<String> i = filter.typesIterator();
388 if (i == null) {
389 return 0;
390 }
391
392 int num = 0;
393 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800394 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800396 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 String baseName = name;
398 final int slashpos = name.indexOf('/');
399 if (slashpos > 0) {
400 baseName = name.substring(0, slashpos).intern();
401 } else {
402 name = name + "/*";
403 }
404
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700405 addFilter(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406
407 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700408 addFilter(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700410 addFilter(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
412 }
413
414 return num;
415 }
416
417 private final int unregister_mime_types(F filter, String prefix) {
418 final Iterator<String> i = filter.typesIterator();
419 if (i == null) {
420 return 0;
421 }
422
423 int num = 0;
424 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800425 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800427 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 String baseName = name;
429 final int slashpos = name.indexOf('/');
430 if (slashpos > 0) {
431 baseName = name.substring(0, slashpos).intern();
432 } else {
433 name = name + "/*";
434 }
435
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700436 remove_all_objects(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437
438 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700439 remove_all_objects(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700441 remove_all_objects(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443 }
444 return num;
445 }
446
447 private final int register_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700448 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 if (i == null) {
450 return 0;
451 }
452
453 int num = 0;
454 while (i.hasNext()) {
455 String name = i.next();
456 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800457 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700458 addFilter(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460 return num;
461 }
462
463 private final int unregister_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700464 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 if (i == null) {
466 return 0;
467 }
468
469 int num = 0;
470 while (i.hasNext()) {
471 String name = i.next();
472 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800473 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700474 remove_all_objects(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
476 return num;
477 }
478
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700479 private final void remove_all_objects(ArrayMap<String, F[]> map, String name,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700480 Object object) {
481 F[] array = map.get(name);
482 if (array != null) {
483 int LAST = array.length-1;
484 while (LAST >= 0 && array[LAST] == null) {
485 LAST--;
486 }
487 for (int idx=LAST; idx>=0; idx--) {
488 if (array[idx] == object) {
489 final int remain = LAST - idx;
490 if (remain > 0) {
491 System.arraycopy(array, idx+1, array, idx, remain);
492 }
493 array[LAST] = null;
494 LAST--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496 }
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700497 if (LAST < 0) {
498 map.remove(name);
499 } else if (LAST < (array.length/2)) {
500 F[] newa = newArray(LAST+2);
501 System.arraycopy(array, 0, newa, 0, LAST+1);
502 map.put(name, newa);
503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
506
Jeff Brown2c376fc2011-01-28 17:34:01 -0800507 private static FastImmutableArraySet<String> getFastIntentCategories(Intent intent) {
508 final Set<String> categories = intent.getCategories();
509 if (categories == null) {
510 return null;
511 }
512 return new FastImmutableArraySet<String>(categories.toArray(new String[categories.size()]));
513 }
514
515 private void buildResolveList(Intent intent, FastImmutableArraySet<String> categories,
516 boolean debug, boolean defaultOnly,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700517 String resolvedType, String scheme, F[] src, List<R> dest, int userId) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800518 final String action = intent.getAction();
519 final Uri data = intent.getData();
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700520 final String packageName = intent.getPackage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521
Dianne Hackborne7f97212011-02-24 14:40:20 -0800522 final boolean excludingStopped = intent.isExcludingStopped();
523
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700524 final int N = src != null ? src.length : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 boolean hasNonDefaults = false;
526 int i;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700527 F filter;
528 for (i=0; i<N && (filter=src[i]) != null; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 int match;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800530 if (debug) Slog.v(TAG, "Matching against filter " + filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531
Amith Yamasani483f3b02012-03-13 16:08:00 -0700532 if (excludingStopped && isFilterStopped(filter, userId)) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800533 if (debug) {
534 Slog.v(TAG, " Filter's target is stopped; skipping");
535 }
536 continue;
537 }
538
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700539 // Is delivery being limited to filters owned by a particular package?
Ben Gruver4efe9402013-04-02 21:18:41 -0700540 if (packageName != null && !isPackageForFilter(packageName, filter)) {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700541 if (debug) {
542 Slog.v(TAG, " Filter is not from package " + packageName + "; skipping");
543 }
544 continue;
545 }
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 // Do we already have this one?
548 if (!allowFilterResult(filter, dest)) {
549 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800550 Slog.v(TAG, " Filter's target already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 continue;
553 }
554
Jeff Brown2c376fc2011-01-28 17:34:01 -0800555 match = filter.match(action, resolvedType, scheme, data, categories, TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 if (match >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800557 if (debug) Slog.v(TAG, " Filter matched! match=0x" +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 Integer.toHexString(match));
559 if (!defaultOnly || filter.hasCategory(Intent.CATEGORY_DEFAULT)) {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700560 final R oneResult = newResult(filter, match, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 if (oneResult != null) {
562 dest.add(oneResult);
563 }
564 } else {
565 hasNonDefaults = true;
566 }
567 } else {
568 if (debug) {
569 String reason;
570 switch (match) {
571 case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;
572 case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;
573 case IntentFilter.NO_MATCH_DATA: reason = "data"; break;
574 case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;
575 default: reason = "unknown reason"; break;
576 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800577 Slog.v(TAG, " Filter did not match: " + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579 }
580 }
581
582 if (dest.size() == 0 && hasNonDefaults) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800583 Slog.w(TAG, "resolveIntent failed: found match, but none with Intent.CATEGORY_DEFAULT");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585 }
586
587 // Sorts a List of IntentFilter objects into descending priority order.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700588 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 private static final Comparator mResolvePrioritySorter = new Comparator() {
590 public int compare(Object o1, Object o2) {
Kenny Root502e9a42011-01-10 13:48:15 -0800591 final int q1 = ((IntentFilter) o1).getPriority();
592 final int q2 = ((IntentFilter) o2).getPriority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 return (q1 > q2) ? -1 : ((q1 < q2) ? 1 : 0);
594 }
595 };
596
597 /**
598 * All filters that have been registered.
599 */
600 private final HashSet<F> mFilters = new HashSet<F>();
601
602 /**
603 * All of the MIME types that have been registered, such as "image/jpeg",
604 * "image/*", or "{@literal *}/*".
605 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700606 private final ArrayMap<String, F[]> mTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607
608 /**
609 * The base names of all of all fully qualified MIME types that have been
610 * registered, such as "image" or "*". Wild card MIME types such as
611 * "image/*" will not be here.
612 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700613 private final ArrayMap<String, F[]> mBaseTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
615 /**
616 * The base names of all of the MIME types with a sub-type wildcard that
617 * have been registered. For example, a filter with "image/*" will be
618 * included here as "image" but one with "image/jpeg" will not be
619 * included here. This also includes the "*" for the "{@literal *}/*"
620 * MIME type.
621 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700622 private final ArrayMap<String, F[]> mWildTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 /**
625 * All of the URI schemes (such as http) that have been registered.
626 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700627 private final ArrayMap<String, F[]> mSchemeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
629 /**
630 * All of the actions that have been registered, but only those that did
631 * not specify data.
632 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700633 private final ArrayMap<String, F[]> mActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
635 /**
636 * All of the actions that have been registered and specified a MIME type.
637 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700638 private final ArrayMap<String, F[]> mTypedActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639}