blob: 3a35f3f857ddaff6412531e9d83b72fd244910d9 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.HashSet;
24import java.util.Iterator;
25import java.util.List;
26import java.util.Map;
27import java.util.Set;
28
Jeff Brown2c376fc2011-01-28 17:34:01 -080029import android.net.Uri;
30import android.util.FastImmutableArraySet;
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070031import android.util.ArrayMap;
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;
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.Intent;
39import android.content.IntentFilter;
40
41/**
42 * {@hide}
43 */
Dianne Hackborn6c418d52011-06-29 14:05:33 -070044public abstract class IntentResolver<F extends IntentFilter, R extends Object> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 final private static String TAG = "IntentResolver";
46 final private static boolean DEBUG = false;
Joe Onorato43a17652011-04-06 19:22:23 -070047 final private static boolean localLOGV = DEBUG || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49 public void addFilter(F f) {
50 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080051 Slog.v(TAG, "Adding filter: " + f);
52 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
53 Slog.v(TAG, " Building Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 }
55
56 mFilters.add(f);
57 int numS = register_intent_filter(f, f.schemesIterator(),
58 mSchemeToFilter, " Scheme: ");
59 int numT = register_mime_types(f, " Type: ");
60 if (numS == 0 && numT == 0) {
61 register_intent_filter(f, f.actionsIterator(),
62 mActionToFilter, " Action: ");
63 }
64 if (numT != 0) {
65 register_intent_filter(f, f.actionsIterator(),
66 mTypedActionToFilter, " TypedAction: ");
67 }
68 }
69
70 public void removeFilter(F f) {
71 removeFilterInternal(f);
72 mFilters.remove(f);
73 }
74
75 void removeFilterInternal(F f) {
76 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080077 Slog.v(TAG, "Removing filter: " + f);
78 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
79 Slog.v(TAG, " Cleaning Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
81
82 int numS = unregister_intent_filter(f, f.schemesIterator(),
83 mSchemeToFilter, " Scheme: ");
84 int numT = unregister_mime_types(f, " Type: ");
85 if (numS == 0 && numT == 0) {
86 unregister_intent_filter(f, f.actionsIterator(),
87 mActionToFilter, " Action: ");
88 }
89 if (numT != 0) {
90 unregister_intent_filter(f, f.actionsIterator(),
91 mTypedActionToFilter, " TypedAction: ");
92 }
93 }
94
Dianne Hackbornd4310ac2010-03-16 22:55:08 -070095 boolean dumpMap(PrintWriter out, String titlePrefix, String title,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -070096 String prefix, Map<String, F[]> map, String packageName,
Dianne Hackborncef65ee2010-09-30 18:27:22 -070097 boolean printFilter) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 String eprefix = prefix + " ";
99 String fprefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700100 boolean printedSomething = false;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700101 Printer printer = null;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700102 for (Map.Entry<String, F[]> e : map.entrySet()) {
103 F[] a = e.getValue();
104 final int N = a.length;
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700105 boolean printedHeader = false;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700106 F filter;
107 for (int i=0; i<N && (filter=a[i]) != null; i++) {
Ben Gruver4efe9402013-04-02 21:18:41 -0700108 if (packageName != null && !isPackageForFilter(packageName, filter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700109 continue;
110 }
111 if (title != null) {
112 out.print(titlePrefix); out.println(title);
113 title = null;
114 }
115 if (!printedHeader) {
116 out.print(eprefix); out.print(e.getKey()); out.println(":");
117 printedHeader = true;
118 }
119 printedSomething = true;
120 dumpFilter(out, fprefix, filter);
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700121 if (printFilter) {
122 if (printer == null) {
123 printer = new PrintWriterPrinter(out);
124 }
125 filter.dump(printer, fprefix + " ");
126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 }
128 }
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700129 return printedSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 }
131
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700132 public boolean dump(PrintWriter out, String title, String prefix, String packageName,
133 boolean printFilter) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700134 String innerPrefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700135 String sepPrefix = "\n" + prefix;
136 String curPrefix = title + "\n" + prefix;
137 if (dumpMap(out, curPrefix, "Full MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700138 mTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700139 curPrefix = sepPrefix;
140 }
141 if (dumpMap(out, curPrefix, "Base MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700142 mBaseTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700143 curPrefix = sepPrefix;
144 }
145 if (dumpMap(out, curPrefix, "Wild MIME Types:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700146 mWildTypeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700147 curPrefix = sepPrefix;
148 }
149 if (dumpMap(out, curPrefix, "Schemes:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700150 mSchemeToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700151 curPrefix = sepPrefix;
152 }
153 if (dumpMap(out, curPrefix, "Non-Data Actions:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700154 mActionToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700155 curPrefix = sepPrefix;
156 }
157 if (dumpMap(out, curPrefix, "MIME Typed Actions:", innerPrefix,
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700158 mTypedActionToFilter, packageName, printFilter)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700159 curPrefix = sepPrefix;
160 }
161 return curPrefix == sepPrefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 }
163
164 private class IteratorWrapper implements Iterator<F> {
165 private final Iterator<F> mI;
166 private F mCur;
167
168 IteratorWrapper(Iterator<F> it) {
169 mI = it;
170 }
171
172 public boolean hasNext() {
173 return mI.hasNext();
174 }
175
176 public F next() {
177 return (mCur = mI.next());
178 }
179
180 public void remove() {
181 if (mCur != null) {
182 removeFilterInternal(mCur);
183 }
184 mI.remove();
185 }
186
187 }
188
189 /**
190 * Returns an iterator allowing filters to be removed.
191 */
192 public Iterator<F> filterIterator() {
193 return new IteratorWrapper(mFilters.iterator());
194 }
195
196 /**
197 * Returns a read-only set of the filters.
198 */
199 public Set<F> filterSet() {
200 return Collections.unmodifiableSet(mFilters);
201 }
202
Mihai Predaeae850c2009-05-13 10:13:48 +0200203 public List<R> queryIntentFromList(Intent intent, String resolvedType,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700204 boolean defaultOnly, ArrayList<F[]> listCut, int userId) {
Mihai Predaeae850c2009-05-13 10:13:48 +0200205 ArrayList<R> resultList = new ArrayList<R>();
206
207 final boolean debug = localLOGV ||
208 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
209
Jeff Brown2c376fc2011-01-28 17:34:01 -0800210 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
Mihai Predaeae850c2009-05-13 10:13:48 +0200211 final String scheme = intent.getScheme();
212 int N = listCut.size();
213 for (int i = 0; i < N; ++i) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800214 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700215 resolvedType, scheme, listCut.get(i), resultList, userId);
Mihai Predaeae850c2009-05-13 10:13:48 +0200216 }
217 sortResults(resultList);
218 return resultList;
219 }
220
Amith Yamasani483f3b02012-03-13 16:08:00 -0700221 public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly,
222 int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 String scheme = intent.getScheme();
224
225 ArrayList<R> finalList = new ArrayList<R>();
226
227 final boolean debug = localLOGV ||
228 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
229
Joe Onorato8a9b2202010-02-26 18:56:32 -0800230 if (debug) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 TAG, "Resolving type " + resolvedType + " scheme " + scheme
232 + " of intent " + intent);
233
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700234 F[] firstTypeCut = null;
235 F[] secondTypeCut = null;
236 F[] thirdTypeCut = null;
237 F[] schemeCut = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 // If the intent includes a MIME type, then we want to collect all of
240 // the filters that match that MIME type.
241 if (resolvedType != null) {
242 int slashpos = resolvedType.indexOf('/');
243 if (slashpos > 0) {
244 final String baseType = resolvedType.substring(0, slashpos);
245 if (!baseType.equals("*")) {
246 if (resolvedType.length() != slashpos+2
247 || resolvedType.charAt(slashpos+1) != '*') {
248 // Not a wild card, so we can just look for all filters that
249 // completely match or wildcards whose base type matches.
250 firstTypeCut = mTypeToFilter.get(resolvedType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800251 if (debug) Slog.v(TAG, "First type cut: " + firstTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 secondTypeCut = mWildTypeToFilter.get(baseType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800253 if (debug) Slog.v(TAG, "Second type cut: " + secondTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 } else {
255 // We can match anything with our base type.
256 firstTypeCut = mBaseTypeToFilter.get(baseType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800257 if (debug) Slog.v(TAG, "First type cut: " + firstTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 secondTypeCut = mWildTypeToFilter.get(baseType);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800259 if (debug) Slog.v(TAG, "Second type cut: " + secondTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
261 // Any */* types always apply, but we only need to do this
262 // if the intent type was not already */*.
263 thirdTypeCut = mWildTypeToFilter.get("*");
Joe Onorato8a9b2202010-02-26 18:56:32 -0800264 if (debug) Slog.v(TAG, "Third type cut: " + thirdTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 } else if (intent.getAction() != null) {
266 // The intent specified any type ({@literal *}/*). This
267 // can be a whole heck of a lot of things, so as a first
268 // cut let's use the action instead.
269 firstTypeCut = mTypedActionToFilter.get(intent.getAction());
Joe Onorato8a9b2202010-02-26 18:56:32 -0800270 if (debug) Slog.v(TAG, "Typed Action list: " + firstTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
272 }
273 }
274
275 // If the intent includes a data URI, then we want to collect all of
276 // the filters that match its scheme (we will further refine matches
277 // on the authority and path by directly matching each resulting filter).
278 if (scheme != null) {
279 schemeCut = mSchemeToFilter.get(scheme);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800280 if (debug) Slog.v(TAG, "Scheme list: " + schemeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
282
283 // If the intent does not specify any data -- either a MIME type or
284 // a URI -- then we will only be looking for matches against empty
285 // data.
286 if (resolvedType == null && scheme == null && intent.getAction() != null) {
287 firstTypeCut = mActionToFilter.get(intent.getAction());
Joe Onorato8a9b2202010-02-26 18:56:32 -0800288 if (debug) Slog.v(TAG, "Action list: " + firstTypeCut);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 }
290
Jeff Brown2c376fc2011-01-28 17:34:01 -0800291 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 if (firstTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800293 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700294 resolvedType, scheme, firstTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296 if (secondTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800297 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700298 resolvedType, scheme, secondTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
300 if (thirdTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800301 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700302 resolvedType, scheme, thirdTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304 if (schemeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800305 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700306 resolvedType, scheme, schemeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308 sortResults(finalList);
309
310 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800311 Slog.v(TAG, "Final result list:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 for (R r : finalList) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800313 Slog.v(TAG, " " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 }
315 }
316 return finalList;
317 }
318
319 /**
320 * Control whether the given filter is allowed to go into the result
321 * list. Mainly intended to prevent adding multiple filters for the
322 * same target object.
323 */
324 protected boolean allowFilterResult(F filter, List<R> dest) {
325 return true;
326 }
327
Dianne Hackborne7f97212011-02-24 14:40:20 -0800328 /**
329 * Returns whether the object associated with the given filter is
330 * "stopped," that is whether it should not be included in the result
331 * if the intent requests to excluded stopped objects.
332 */
Amith Yamasani483f3b02012-03-13 16:08:00 -0700333 protected boolean isFilterStopped(F filter, int userId) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800334 return false;
335 }
336
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700337 /**
Ben Gruver4efe9402013-04-02 21:18:41 -0700338 * Returns whether this filter is owned by this package. This must be
339 * implemented to provide correct filtering of Intents that have
340 * specified a package name they are to be delivered to.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700341 */
Ben Gruver4efe9402013-04-02 21:18:41 -0700342 protected abstract boolean isPackageForFilter(String packageName, F filter);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700343
344 protected abstract F[] newArray(int size);
345
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700346 @SuppressWarnings("unchecked")
Amith Yamasani483f3b02012-03-13 16:08:00 -0700347 protected R newResult(F filter, int match, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 return (R)filter;
349 }
350
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700351 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 protected void sortResults(List<R> results) {
353 Collections.sort(results, mResolvePrioritySorter);
354 }
355
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700356 protected void dumpFilter(PrintWriter out, String prefix, F filter) {
357 out.print(prefix); out.println(filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700360 private final void addFilter(ArrayMap<String, F[]> map, String name, F filter) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700361 F[] array = map.get(name);
362 if (array == null) {
363 array = newArray(2);
364 map.put(name, array);
365 array[0] = filter;
366 } else {
367 final int N = array.length;
368 int i = N;
369 while (i > 0 && array[i-1] == null) {
370 i--;
371 }
372 if (i < N) {
373 array[i] = filter;
374 } else {
375 F[] newa = newArray((N*3)/2);
376 System.arraycopy(array, 0, newa, 0, N);
377 newa[N] = filter;
378 map.put(name, newa);
379 }
380 }
381 }
382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 private final int register_mime_types(F filter, String prefix) {
384 final Iterator<String> i = filter.typesIterator();
385 if (i == null) {
386 return 0;
387 }
388
389 int num = 0;
390 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800391 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800393 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 String baseName = name;
395 final int slashpos = name.indexOf('/');
396 if (slashpos > 0) {
397 baseName = name.substring(0, slashpos).intern();
398 } else {
399 name = name + "/*";
400 }
401
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700402 addFilter(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
404 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700405 addFilter(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700407 addFilter(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409 }
410
411 return num;
412 }
413
414 private final int unregister_mime_types(F filter, String prefix) {
415 final Iterator<String> i = filter.typesIterator();
416 if (i == null) {
417 return 0;
418 }
419
420 int num = 0;
421 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800422 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800424 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 String baseName = name;
426 final int slashpos = name.indexOf('/');
427 if (slashpos > 0) {
428 baseName = name.substring(0, slashpos).intern();
429 } else {
430 name = name + "/*";
431 }
432
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700433 remove_all_objects(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434
435 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700436 remove_all_objects(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700438 remove_all_objects(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440 }
441 return num;
442 }
443
444 private final int register_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700445 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 if (i == null) {
447 return 0;
448 }
449
450 int num = 0;
451 while (i.hasNext()) {
452 String name = i.next();
453 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800454 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700455 addFilter(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
457 return num;
458 }
459
460 private final int unregister_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700461 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 if (i == null) {
463 return 0;
464 }
465
466 int num = 0;
467 while (i.hasNext()) {
468 String name = i.next();
469 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800470 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700471 remove_all_objects(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 }
473 return num;
474 }
475
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700476 private final void remove_all_objects(ArrayMap<String, F[]> map, String name,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700477 Object object) {
478 F[] array = map.get(name);
479 if (array != null) {
480 int LAST = array.length-1;
481 while (LAST >= 0 && array[LAST] == null) {
482 LAST--;
483 }
484 for (int idx=LAST; idx>=0; idx--) {
485 if (array[idx] == object) {
486 final int remain = LAST - idx;
487 if (remain > 0) {
488 System.arraycopy(array, idx+1, array, idx, remain);
489 }
490 array[LAST] = null;
491 LAST--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
493 }
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700494 if (LAST < 0) {
495 map.remove(name);
496 } else if (LAST < (array.length/2)) {
497 F[] newa = newArray(LAST+2);
498 System.arraycopy(array, 0, newa, 0, LAST+1);
499 map.put(name, newa);
500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503
Jeff Brown2c376fc2011-01-28 17:34:01 -0800504 private static FastImmutableArraySet<String> getFastIntentCategories(Intent intent) {
505 final Set<String> categories = intent.getCategories();
506 if (categories == null) {
507 return null;
508 }
509 return new FastImmutableArraySet<String>(categories.toArray(new String[categories.size()]));
510 }
511
512 private void buildResolveList(Intent intent, FastImmutableArraySet<String> categories,
513 boolean debug, boolean defaultOnly,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700514 String resolvedType, String scheme, F[] src, List<R> dest, int userId) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800515 final String action = intent.getAction();
516 final Uri data = intent.getData();
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700517 final String packageName = intent.getPackage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
Dianne Hackborne7f97212011-02-24 14:40:20 -0800519 final boolean excludingStopped = intent.isExcludingStopped();
520
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700521 final int N = src != null ? src.length : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 boolean hasNonDefaults = false;
523 int i;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700524 F filter;
525 for (i=0; i<N && (filter=src[i]) != null; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 int match;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 if (debug) Slog.v(TAG, "Matching against filter " + filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528
Amith Yamasani483f3b02012-03-13 16:08:00 -0700529 if (excludingStopped && isFilterStopped(filter, userId)) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800530 if (debug) {
531 Slog.v(TAG, " Filter's target is stopped; skipping");
532 }
533 continue;
534 }
535
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700536 // Is delivery being limited to filters owned by a particular package?
Ben Gruver4efe9402013-04-02 21:18:41 -0700537 if (packageName != null && !isPackageForFilter(packageName, filter)) {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700538 if (debug) {
539 Slog.v(TAG, " Filter is not from package " + packageName + "; skipping");
540 }
541 continue;
542 }
543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 // Do we already have this one?
545 if (!allowFilterResult(filter, dest)) {
546 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800547 Slog.v(TAG, " Filter's target already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549 continue;
550 }
551
Jeff Brown2c376fc2011-01-28 17:34:01 -0800552 match = filter.match(action, resolvedType, scheme, data, categories, TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 if (match >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800554 if (debug) Slog.v(TAG, " Filter matched! match=0x" +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 Integer.toHexString(match));
556 if (!defaultOnly || filter.hasCategory(Intent.CATEGORY_DEFAULT)) {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700557 final R oneResult = newResult(filter, match, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 if (oneResult != null) {
559 dest.add(oneResult);
560 }
561 } else {
562 hasNonDefaults = true;
563 }
564 } else {
565 if (debug) {
566 String reason;
567 switch (match) {
568 case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;
569 case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;
570 case IntentFilter.NO_MATCH_DATA: reason = "data"; break;
571 case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;
572 default: reason = "unknown reason"; break;
573 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800574 Slog.v(TAG, " Filter did not match: " + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 }
576 }
577 }
578
579 if (dest.size() == 0 && hasNonDefaults) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800580 Slog.w(TAG, "resolveIntent failed: found match, but none with Intent.CATEGORY_DEFAULT");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582 }
583
584 // Sorts a List of IntentFilter objects into descending priority order.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700585 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 private static final Comparator mResolvePrioritySorter = new Comparator() {
587 public int compare(Object o1, Object o2) {
Kenny Root502e9a42011-01-10 13:48:15 -0800588 final int q1 = ((IntentFilter) o1).getPriority();
589 final int q2 = ((IntentFilter) o2).getPriority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 return (q1 > q2) ? -1 : ((q1 < q2) ? 1 : 0);
591 }
592 };
593
594 /**
595 * All filters that have been registered.
596 */
597 private final HashSet<F> mFilters = new HashSet<F>();
598
599 /**
600 * All of the MIME types that have been registered, such as "image/jpeg",
601 * "image/*", or "{@literal *}/*".
602 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700603 private final ArrayMap<String, F[]> mTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604
605 /**
606 * The base names of all of all fully qualified MIME types that have been
607 * registered, such as "image" or "*". Wild card MIME types such as
608 * "image/*" will not be here.
609 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700610 private final ArrayMap<String, F[]> mBaseTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611
612 /**
613 * The base names of all of the MIME types with a sub-type wildcard that
614 * have been registered. For example, a filter with "image/*" will be
615 * included here as "image" but one with "image/jpeg" will not be
616 * included here. This also includes the "*" for the "{@literal *}/*"
617 * MIME type.
618 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700619 private final ArrayMap<String, F[]> mWildTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
621 /**
622 * All of the URI schemes (such as http) that have been registered.
623 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700624 private final ArrayMap<String, F[]> mSchemeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625
626 /**
627 * All of the actions that have been registered, but only those that did
628 * not specify data.
629 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700630 private final ArrayMap<String, F[]> mActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631
632 /**
633 * All of the actions that have been registered and specified a MIME type.
634 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700635 private final ArrayMap<String, F[]> mTypedActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636}