blob: 3359060d3fe7c5da8b6e70758a05b64c974dfeba [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.Iterator;
25import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.util.Set;
27
Jeff Brown2c376fc2011-01-28 17:34:01 -080028import android.net.Uri;
29import android.util.FastImmutableArraySet;
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070030import android.util.ArrayMap;
Jeff Sharkey9f837a92014-10-24 12:07:24 -070031import android.util.ArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Log;
Dianne Hackbornd052a942014-11-21 15:23:13 -080033import android.util.MutableInt;
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;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080050 final private static boolean localVerificationLOGV = DEBUG || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52 public void addFilter(F f) {
53 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080054 Slog.v(TAG, "Adding filter: " + f);
55 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
56 Slog.v(TAG, " Building Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 }
58
59 mFilters.add(f);
60 int numS = register_intent_filter(f, f.schemesIterator(),
61 mSchemeToFilter, " Scheme: ");
62 int numT = register_mime_types(f, " Type: ");
63 if (numS == 0 && numT == 0) {
64 register_intent_filter(f, f.actionsIterator(),
65 mActionToFilter, " Action: ");
66 }
67 if (numT != 0) {
68 register_intent_filter(f, f.actionsIterator(),
69 mTypedActionToFilter, " TypedAction: ");
70 }
71 }
72
Dianne Hackbornf2ac2762014-08-16 11:44:40 -070073 private boolean filterEquals(IntentFilter f1, IntentFilter f2) {
74 int s1 = f1.countActions();
75 int s2 = f2.countActions();
76 if (s1 != s2) {
77 return false;
78 }
79 for (int i=0; i<s1; i++) {
80 if (!f2.hasAction(f1.getAction(i))) {
81 return false;
82 }
83 }
84 s1 = f1.countCategories();
85 s2 = f2.countCategories();
86 if (s1 != s2) {
87 return false;
88 }
89 for (int i=0; i<s1; i++) {
90 if (!f2.hasCategory(f1.getCategory(i))) {
91 return false;
92 }
93 }
94 s1 = f1.countDataTypes();
95 s2 = f2.countDataTypes();
96 if (s1 != s2) {
97 return false;
98 }
99 for (int i=0; i<s1; i++) {
100 if (!f2.hasExactDataType(f1.getDataType(i))) {
101 return false;
102 }
103 }
104 s1 = f1.countDataSchemes();
105 s2 = f2.countDataSchemes();
106 if (s1 != s2) {
107 return false;
108 }
109 for (int i=0; i<s1; i++) {
110 if (!f2.hasDataScheme(f1.getDataScheme(i))) {
111 return false;
112 }
113 }
114 s1 = f1.countDataAuthorities();
115 s2 = f2.countDataAuthorities();
116 if (s1 != s2) {
117 return false;
118 }
119 for (int i=0; i<s1; i++) {
120 if (!f2.hasDataAuthority(f1.getDataAuthority(i))) {
121 return false;
122 }
123 }
124 s1 = f1.countDataPaths();
125 s2 = f2.countDataPaths();
126 if (s1 != s2) {
127 return false;
128 }
129 for (int i=0; i<s1; i++) {
130 if (!f2.hasDataPath(f1.getDataPath(i))) {
131 return false;
132 }
133 }
134 s1 = f1.countDataSchemeSpecificParts();
135 s2 = f2.countDataSchemeSpecificParts();
136 if (s1 != s2) {
137 return false;
138 }
139 for (int i=0; i<s1; i++) {
140 if (!f2.hasDataSchemeSpecificPart(f1.getDataSchemeSpecificPart(i))) {
141 return false;
142 }
143 }
144 return true;
145 }
146
147 private ArrayList<F> collectFilters(F[] array, IntentFilter matching) {
148 ArrayList<F> res = null;
149 if (array != null) {
150 for (int i=0; i<array.length; i++) {
151 F cur = array[i];
152 if (cur == null) {
153 break;
154 }
155 if (filterEquals(cur, matching)) {
156 if (res == null) {
157 res = new ArrayList<>();
158 }
159 res.add(cur);
160 }
161 }
162 }
163 return res;
164 }
165
166 public ArrayList<F> findFilters(IntentFilter matching) {
167 if (matching.countDataSchemes() == 1) {
168 // Fast case.
169 return collectFilters(mSchemeToFilter.get(matching.getDataScheme(0)), matching);
170 } else if (matching.countDataTypes() != 0 && matching.countActions() == 1) {
171 // Another fast case.
172 return collectFilters(mTypedActionToFilter.get(matching.getAction(0)), matching);
173 } else if (matching.countDataTypes() == 0 && matching.countDataSchemes() == 0
174 && matching.countActions() == 1) {
175 // Last fast case.
176 return collectFilters(mActionToFilter.get(matching.getAction(0)), matching);
177 } else {
178 ArrayList<F> res = null;
179 for (F cur : mFilters) {
180 if (filterEquals(cur, matching)) {
181 if (res == null) {
182 res = new ArrayList<>();
183 }
184 res.add(cur);
185 }
186 }
187 return res;
188 }
189 }
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 public void removeFilter(F f) {
192 removeFilterInternal(f);
193 mFilters.remove(f);
194 }
195
196 void removeFilterInternal(F f) {
197 if (localLOGV) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800198 Slog.v(TAG, "Removing filter: " + f);
199 f.dump(new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM), " ");
200 Slog.v(TAG, " Cleaning Lookup Maps:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
202
203 int numS = unregister_intent_filter(f, f.schemesIterator(),
204 mSchemeToFilter, " Scheme: ");
205 int numT = unregister_mime_types(f, " Type: ");
206 if (numS == 0 && numT == 0) {
207 unregister_intent_filter(f, f.actionsIterator(),
208 mActionToFilter, " Action: ");
209 }
210 if (numT != 0) {
211 unregister_intent_filter(f, f.actionsIterator(),
212 mTypedActionToFilter, " TypedAction: ");
213 }
214 }
215
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700216 boolean dumpMap(PrintWriter out, String titlePrefix, String title,
Dianne Hackbornd052a942014-11-21 15:23:13 -0800217 String prefix, ArrayMap<String, F[]> map, String packageName,
218 boolean printFilter, boolean collapseDuplicates) {
219 final String eprefix = prefix + " ";
220 final String fprefix = prefix + " ";
221 final ArrayMap<Object, MutableInt> found = new ArrayMap<>();
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700222 boolean printedSomething = false;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700223 Printer printer = null;
Dianne Hackbornd052a942014-11-21 15:23:13 -0800224 for (int mapi=0; mapi<map.size(); mapi++) {
225 F[] a = map.valueAt(mapi);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700226 final int N = a.length;
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700227 boolean printedHeader = false;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700228 F filter;
Dianne Hackbornd052a942014-11-21 15:23:13 -0800229 if (collapseDuplicates) {
230 found.clear();
231 for (int i=0; i<N && (filter=a[i]) != null; i++) {
232 if (packageName != null && !isPackageForFilter(packageName, filter)) {
233 continue;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700234 }
Dianne Hackbornd052a942014-11-21 15:23:13 -0800235 Object label = filterToLabel(filter);
236 int index = found.indexOfKey(label);
237 if (index < 0) {
238 found.put(label, new MutableInt(1));
239 } else {
240 found.valueAt(index).value++;
241 }
242 }
243 for (int i=0; i<found.size(); i++) {
244 if (title != null) {
245 out.print(titlePrefix); out.println(title);
246 title = null;
247 }
248 if (!printedHeader) {
249 out.print(eprefix); out.print(map.keyAt(mapi)); out.println(":");
250 printedHeader = true;
251 }
252 printedSomething = true;
253 dumpFilterLabel(out, fprefix, found.keyAt(i), found.valueAt(i).value);
254 }
255 } else {
256 for (int i=0; i<N && (filter=a[i]) != null; i++) {
257 if (packageName != null && !isPackageForFilter(packageName, filter)) {
258 continue;
259 }
260 if (title != null) {
261 out.print(titlePrefix); out.println(title);
262 title = null;
263 }
264 if (!printedHeader) {
265 out.print(eprefix); out.print(map.keyAt(mapi)); out.println(":");
266 printedHeader = true;
267 }
268 printedSomething = true;
269 dumpFilter(out, fprefix, filter);
270 if (printFilter) {
271 if (printer == null) {
272 printer = new PrintWriterPrinter(out);
273 }
274 filter.dump(printer, fprefix + " ");
275 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 }
278 }
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700279 return printedSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700282 public boolean dump(PrintWriter out, String title, String prefix, String packageName,
Dianne Hackbornd052a942014-11-21 15:23:13 -0800283 boolean printFilter, boolean collapseDuplicates) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700284 String innerPrefix = prefix + " ";
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700285 String sepPrefix = "\n" + prefix;
286 String curPrefix = title + "\n" + prefix;
287 if (dumpMap(out, curPrefix, "Full MIME Types:", innerPrefix,
Dianne Hackbornd052a942014-11-21 15:23:13 -0800288 mTypeToFilter, packageName, printFilter, collapseDuplicates)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700289 curPrefix = sepPrefix;
290 }
291 if (dumpMap(out, curPrefix, "Base MIME Types:", innerPrefix,
Dianne Hackbornd052a942014-11-21 15:23:13 -0800292 mBaseTypeToFilter, packageName, printFilter, collapseDuplicates)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700293 curPrefix = sepPrefix;
294 }
295 if (dumpMap(out, curPrefix, "Wild MIME Types:", innerPrefix,
Dianne Hackbornd052a942014-11-21 15:23:13 -0800296 mWildTypeToFilter, packageName, printFilter, collapseDuplicates)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700297 curPrefix = sepPrefix;
298 }
299 if (dumpMap(out, curPrefix, "Schemes:", innerPrefix,
Dianne Hackbornd052a942014-11-21 15:23:13 -0800300 mSchemeToFilter, packageName, printFilter, collapseDuplicates)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700301 curPrefix = sepPrefix;
302 }
303 if (dumpMap(out, curPrefix, "Non-Data Actions:", innerPrefix,
Dianne Hackbornd052a942014-11-21 15:23:13 -0800304 mActionToFilter, packageName, printFilter, collapseDuplicates)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700305 curPrefix = sepPrefix;
306 }
307 if (dumpMap(out, curPrefix, "MIME Typed Actions:", innerPrefix,
Dianne Hackbornd052a942014-11-21 15:23:13 -0800308 mTypedActionToFilter, packageName, printFilter, collapseDuplicates)) {
Dianne Hackbornd4310ac2010-03-16 22:55:08 -0700309 curPrefix = sepPrefix;
310 }
311 return curPrefix == sepPrefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 }
313
314 private class IteratorWrapper implements Iterator<F> {
315 private final Iterator<F> mI;
316 private F mCur;
317
318 IteratorWrapper(Iterator<F> it) {
319 mI = it;
320 }
321
322 public boolean hasNext() {
323 return mI.hasNext();
324 }
325
326 public F next() {
327 return (mCur = mI.next());
328 }
329
330 public void remove() {
331 if (mCur != null) {
332 removeFilterInternal(mCur);
333 }
334 mI.remove();
335 }
336
337 }
338
339 /**
340 * Returns an iterator allowing filters to be removed.
341 */
342 public Iterator<F> filterIterator() {
343 return new IteratorWrapper(mFilters.iterator());
344 }
345
346 /**
347 * Returns a read-only set of the filters.
348 */
349 public Set<F> filterSet() {
350 return Collections.unmodifiableSet(mFilters);
351 }
352
Mihai Predaeae850c2009-05-13 10:13:48 +0200353 public List<R> queryIntentFromList(Intent intent, String resolvedType,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700354 boolean defaultOnly, ArrayList<F[]> listCut, int userId) {
Mihai Predaeae850c2009-05-13 10:13:48 +0200355 ArrayList<R> resultList = new ArrayList<R>();
356
357 final boolean debug = localLOGV ||
358 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
359
Jeff Brown2c376fc2011-01-28 17:34:01 -0800360 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
Mihai Predaeae850c2009-05-13 10:13:48 +0200361 final String scheme = intent.getScheme();
362 int N = listCut.size();
363 for (int i = 0; i < N; ++i) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800364 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700365 resolvedType, scheme, listCut.get(i), resultList, userId);
Mihai Predaeae850c2009-05-13 10:13:48 +0200366 }
367 sortResults(resultList);
368 return resultList;
369 }
370
Amith Yamasani483f3b02012-03-13 16:08:00 -0700371 public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly,
372 int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 String scheme = intent.getScheme();
374
375 ArrayList<R> finalList = new ArrayList<R>();
376
377 final boolean debug = localLOGV ||
378 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
379
Joe Onorato8a9b2202010-02-26 18:56:32 -0800380 if (debug) Slog.v(
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700381 TAG, "Resolving type=" + resolvedType + " scheme=" + scheme
382 + " defaultOnly=" + defaultOnly + " userId=" + userId + " of " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700384 F[] firstTypeCut = null;
385 F[] secondTypeCut = null;
386 F[] thirdTypeCut = null;
387 F[] schemeCut = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388
389 // If the intent includes a MIME type, then we want to collect all of
390 // the filters that match that MIME type.
391 if (resolvedType != null) {
392 int slashpos = resolvedType.indexOf('/');
393 if (slashpos > 0) {
394 final String baseType = resolvedType.substring(0, slashpos);
395 if (!baseType.equals("*")) {
396 if (resolvedType.length() != slashpos+2
397 || resolvedType.charAt(slashpos+1) != '*') {
398 // Not a wild card, so we can just look for all filters that
399 // completely match or wildcards whose base type matches.
400 firstTypeCut = mTypeToFilter.get(resolvedType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700401 if (debug) Slog.v(TAG, "First type cut: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 secondTypeCut = mWildTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700403 if (debug) Slog.v(TAG, "Second type cut: "
404 + Arrays.toString(secondTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 } else {
406 // We can match anything with our base type.
407 firstTypeCut = mBaseTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700408 if (debug) Slog.v(TAG, "First type cut: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 secondTypeCut = mWildTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700410 if (debug) Slog.v(TAG, "Second type cut: "
411 + Arrays.toString(secondTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
413 // Any */* types always apply, but we only need to do this
414 // if the intent type was not already */*.
415 thirdTypeCut = mWildTypeToFilter.get("*");
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700416 if (debug) Slog.v(TAG, "Third type cut: " + Arrays.toString(thirdTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 } else if (intent.getAction() != null) {
418 // The intent specified any type ({@literal *}/*). This
419 // can be a whole heck of a lot of things, so as a first
420 // cut let's use the action instead.
421 firstTypeCut = mTypedActionToFilter.get(intent.getAction());
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700422 if (debug) Slog.v(TAG, "Typed Action list: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 }
424 }
425 }
426
427 // If the intent includes a data URI, then we want to collect all of
428 // the filters that match its scheme (we will further refine matches
429 // on the authority and path by directly matching each resulting filter).
430 if (scheme != null) {
431 schemeCut = mSchemeToFilter.get(scheme);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700432 if (debug) Slog.v(TAG, "Scheme list: " + Arrays.toString(schemeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
434
435 // If the intent does not specify any data -- either a MIME type or
436 // a URI -- then we will only be looking for matches against empty
437 // data.
438 if (resolvedType == null && scheme == null && intent.getAction() != null) {
439 firstTypeCut = mActionToFilter.get(intent.getAction());
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700440 if (debug) Slog.v(TAG, "Action list: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442
Jeff Brown2c376fc2011-01-28 17:34:01 -0800443 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 if (firstTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800445 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700446 resolvedType, scheme, firstTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
448 if (secondTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800449 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700450 resolvedType, scheme, secondTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
452 if (thirdTypeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800453 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700454 resolvedType, scheme, thirdTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456 if (schemeCut != null) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800457 buildResolveList(intent, categories, debug, defaultOnly,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700458 resolvedType, scheme, schemeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460 sortResults(finalList);
461
462 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800463 Slog.v(TAG, "Final result list:");
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700464 for (int i=0; i<finalList.size(); i++) {
465 Slog.v(TAG, " " + finalList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467 }
468 return finalList;
469 }
470
471 /**
472 * Control whether the given filter is allowed to go into the result
473 * list. Mainly intended to prevent adding multiple filters for the
474 * same target object.
475 */
476 protected boolean allowFilterResult(F filter, List<R> dest) {
477 return true;
478 }
479
Dianne Hackborne7f97212011-02-24 14:40:20 -0800480 /**
481 * Returns whether the object associated with the given filter is
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800482 * "stopped", that is whether it should not be included in the result
Dianne Hackborne7f97212011-02-24 14:40:20 -0800483 * if the intent requests to excluded stopped objects.
484 */
Amith Yamasani483f3b02012-03-13 16:08:00 -0700485 protected boolean isFilterStopped(F filter, int userId) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800486 return false;
487 }
488
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700489 /**
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800490 * Returns whether the given filter is "verified" that is whether it has been verified against
491 * its data URIs.
492 *
493 * The verification would happen only and only if the Intent action is
494 * {@link android.content.Intent#ACTION_VIEW} and the Intent category is
495 * {@link android.content.Intent#CATEGORY_BROWSABLE} and the Intent data scheme
496 * is "http" or "https".
497 *
498 * @see android.content.IntentFilter#setAutoVerify(boolean)
499 * @see android.content.IntentFilter#getAutoVerify()
500 */
501 protected boolean isFilterVerified(F filter) {
502 return filter.isVerified();
503 }
504
505 /**
Ben Gruver4efe9402013-04-02 21:18:41 -0700506 * Returns whether this filter is owned by this package. This must be
507 * implemented to provide correct filtering of Intents that have
508 * specified a package name they are to be delivered to.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700509 */
Ben Gruver4efe9402013-04-02 21:18:41 -0700510 protected abstract boolean isPackageForFilter(String packageName, F filter);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700511
512 protected abstract F[] newArray(int size);
513
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700514 @SuppressWarnings("unchecked")
Amith Yamasani483f3b02012-03-13 16:08:00 -0700515 protected R newResult(F filter, int match, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 return (R)filter;
517 }
518
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700519 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 protected void sortResults(List<R> results) {
521 Collections.sort(results, mResolvePrioritySorter);
522 }
523
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700524 protected void dumpFilter(PrintWriter out, String prefix, F filter) {
525 out.print(prefix); out.println(filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527
Dianne Hackbornd052a942014-11-21 15:23:13 -0800528 protected Object filterToLabel(F filter) {
529 return "IntentFilter";
530 }
531
532 protected void dumpFilterLabel(PrintWriter out, String prefix, Object label, int count) {
533 out.print(prefix); out.print(label); out.print(": "); out.println(count);
534 }
535
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700536 private final void addFilter(ArrayMap<String, F[]> map, String name, F filter) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700537 F[] array = map.get(name);
538 if (array == null) {
539 array = newArray(2);
540 map.put(name, array);
541 array[0] = filter;
542 } else {
543 final int N = array.length;
544 int i = N;
545 while (i > 0 && array[i-1] == null) {
546 i--;
547 }
548 if (i < N) {
549 array[i] = filter;
550 } else {
551 F[] newa = newArray((N*3)/2);
552 System.arraycopy(array, 0, newa, 0, N);
553 newa[N] = filter;
554 map.put(name, newa);
555 }
556 }
557 }
558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 private final int register_mime_types(F filter, String prefix) {
560 final Iterator<String> i = filter.typesIterator();
561 if (i == null) {
562 return 0;
563 }
564
565 int num = 0;
566 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800567 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800569 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 String baseName = name;
571 final int slashpos = name.indexOf('/');
572 if (slashpos > 0) {
573 baseName = name.substring(0, slashpos).intern();
574 } else {
575 name = name + "/*";
576 }
577
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700578 addFilter(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579
580 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700581 addFilter(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700583 addFilter(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 }
585 }
586
587 return num;
588 }
589
590 private final int unregister_mime_types(F filter, String prefix) {
591 final Iterator<String> i = filter.typesIterator();
592 if (i == null) {
593 return 0;
594 }
595
596 int num = 0;
597 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800598 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800600 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 String baseName = name;
602 final int slashpos = name.indexOf('/');
603 if (slashpos > 0) {
604 baseName = name.substring(0, slashpos).intern();
605 } else {
606 name = name + "/*";
607 }
608
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700609 remove_all_objects(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610
611 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700612 remove_all_objects(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700614 remove_all_objects(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
616 }
617 return num;
618 }
619
620 private final int register_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700621 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 if (i == null) {
623 return 0;
624 }
625
626 int num = 0;
627 while (i.hasNext()) {
628 String name = i.next();
629 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800630 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700631 addFilter(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633 return num;
634 }
635
636 private final int unregister_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700637 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 if (i == null) {
639 return 0;
640 }
641
642 int num = 0;
643 while (i.hasNext()) {
644 String name = i.next();
645 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800646 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700647 remove_all_objects(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 }
649 return num;
650 }
651
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700652 private final void remove_all_objects(ArrayMap<String, F[]> map, String name,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700653 Object object) {
654 F[] array = map.get(name);
655 if (array != null) {
656 int LAST = array.length-1;
657 while (LAST >= 0 && array[LAST] == null) {
658 LAST--;
659 }
660 for (int idx=LAST; idx>=0; idx--) {
661 if (array[idx] == object) {
662 final int remain = LAST - idx;
663 if (remain > 0) {
664 System.arraycopy(array, idx+1, array, idx, remain);
665 }
666 array[LAST] = null;
667 LAST--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
669 }
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700670 if (LAST < 0) {
671 map.remove(name);
672 } else if (LAST < (array.length/2)) {
673 F[] newa = newArray(LAST+2);
674 System.arraycopy(array, 0, newa, 0, LAST+1);
675 map.put(name, newa);
676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 }
679
Jeff Brown2c376fc2011-01-28 17:34:01 -0800680 private static FastImmutableArraySet<String> getFastIntentCategories(Intent intent) {
681 final Set<String> categories = intent.getCategories();
682 if (categories == null) {
683 return null;
684 }
685 return new FastImmutableArraySet<String>(categories.toArray(new String[categories.size()]));
686 }
687
688 private void buildResolveList(Intent intent, FastImmutableArraySet<String> categories,
689 boolean debug, boolean defaultOnly,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700690 String resolvedType, String scheme, F[] src, List<R> dest, int userId) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800691 final String action = intent.getAction();
692 final Uri data = intent.getData();
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700693 final String packageName = intent.getPackage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694
Dianne Hackborne7f97212011-02-24 14:40:20 -0800695 final boolean excludingStopped = intent.isExcludingStopped();
696
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700697 final Printer logPrinter;
698 final PrintWriter logPrintWriter;
699 if (debug) {
700 logPrinter = new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM);
701 logPrintWriter = new FastPrintWriter(logPrinter);
702 } else {
703 logPrinter = null;
704 logPrintWriter = null;
705 }
706
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700707 final int N = src != null ? src.length : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 boolean hasNonDefaults = false;
709 int i;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700710 F filter;
711 for (i=0; i<N && (filter=src[i]) != null; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 int match;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800713 if (debug) Slog.v(TAG, "Matching against filter " + filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714
Amith Yamasani483f3b02012-03-13 16:08:00 -0700715 if (excludingStopped && isFilterStopped(filter, userId)) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800716 if (debug) {
717 Slog.v(TAG, " Filter's target is stopped; skipping");
718 }
719 continue;
720 }
721
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700722 // Is delivery being limited to filters owned by a particular package?
Ben Gruver4efe9402013-04-02 21:18:41 -0700723 if (packageName != null && !isPackageForFilter(packageName, filter)) {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700724 if (debug) {
725 Slog.v(TAG, " Filter is not from package " + packageName + "; skipping");
726 }
727 continue;
728 }
729
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800730 // Are we verified ?
731 if (filter.getAutoVerify()) {
732 if (localVerificationLOGV || debug) {
733 Slog.v(TAG, " Filter verified: " + isFilterVerified(filter));
Christopher Tate72c10a22015-06-12 18:31:24 -0700734 int authorities = filter.countDataAuthorities();
735 for (int z = 0; z < authorities; z++) {
736 Slog.v(TAG, " " + filter.getDataAuthority(z).getHost());
737 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800738 }
739 }
740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 // Do we already have this one?
742 if (!allowFilterResult(filter, dest)) {
743 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800744 Slog.v(TAG, " Filter's target already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746 continue;
747 }
748
Jeff Brown2c376fc2011-01-28 17:34:01 -0800749 match = filter.match(action, resolvedType, scheme, data, categories, TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 if (match >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800751 if (debug) Slog.v(TAG, " Filter matched! match=0x" +
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700752 Integer.toHexString(match) + " hasDefault="
753 + filter.hasCategory(Intent.CATEGORY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 if (!defaultOnly || filter.hasCategory(Intent.CATEGORY_DEFAULT)) {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700755 final R oneResult = newResult(filter, match, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 if (oneResult != null) {
757 dest.add(oneResult);
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700758 if (debug) {
759 dumpFilter(logPrintWriter, " ", filter);
760 logPrintWriter.flush();
761 filter.dump(logPrinter, " ");
762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 } else {
765 hasNonDefaults = true;
766 }
767 } else {
768 if (debug) {
769 String reason;
770 switch (match) {
771 case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;
772 case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;
773 case IntentFilter.NO_MATCH_DATA: reason = "data"; break;
774 case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;
775 default: reason = "unknown reason"; break;
776 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800777 Slog.v(TAG, " Filter did not match: " + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 }
779 }
780 }
781
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700782 if (hasNonDefaults) {
783 if (dest.size() == 0) {
784 Slog.w(TAG, "resolveIntent failed: found match, but none with CATEGORY_DEFAULT");
785 } else if (dest.size() > 1) {
786 Slog.w(TAG, "resolveIntent: multiple matches, only some with CATEGORY_DEFAULT");
787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 }
789 }
790
791 // Sorts a List of IntentFilter objects into descending priority order.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700792 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 private static final Comparator mResolvePrioritySorter = new Comparator() {
794 public int compare(Object o1, Object o2) {
Kenny Root502e9a42011-01-10 13:48:15 -0800795 final int q1 = ((IntentFilter) o1).getPriority();
796 final int q2 = ((IntentFilter) o2).getPriority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 return (q1 > q2) ? -1 : ((q1 < q2) ? 1 : 0);
798 }
799 };
800
801 /**
802 * All filters that have been registered.
803 */
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700804 private final ArraySet<F> mFilters = new ArraySet<F>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805
806 /**
807 * All of the MIME types that have been registered, such as "image/jpeg",
808 * "image/*", or "{@literal *}/*".
809 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700810 private final ArrayMap<String, F[]> mTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811
812 /**
813 * The base names of all of all fully qualified MIME types that have been
814 * registered, such as "image" or "*". Wild card MIME types such as
815 * "image/*" will not be here.
816 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700817 private final ArrayMap<String, F[]> mBaseTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818
819 /**
820 * The base names of all of the MIME types with a sub-type wildcard that
821 * have been registered. For example, a filter with "image/*" will be
822 * included here as "image" but one with "image/jpeg" will not be
823 * included here. This also includes the "*" for the "{@literal *}/*"
824 * MIME type.
825 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700826 private final ArrayMap<String, F[]> mWildTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827
828 /**
829 * All of the URI schemes (such as http) that have been registered.
830 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700831 private final ArrayMap<String, F[]> mSchemeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832
833 /**
834 * All of the actions that have been registered, but only those that did
835 * not specify data.
836 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700837 private final ArrayMap<String, F[]> mActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838
839 /**
840 * All of the actions that have been registered and specified a MIME type.
841 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700842 private final ArrayMap<String, F[]> mTypedActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843}