blob: 14abb537db9f8bd97c5f5ea9b3393e025b2d0339 [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 Hackborn3cdb56e2015-11-11 12:45:44 -0800229 if (collapseDuplicates && !printFilter) {
Dianne Hackbornd052a942014-11-21 15:23:13 -0800230 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
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800353 public List<R> queryIntentFromList(Intent intent, String resolvedType, boolean defaultOnly,
354 boolean visibleToEphemeral, boolean isEphemeral, 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) {
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800364 buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
365 isEphemeral, resolvedType, scheme, listCut.get(i), resultList, userId);
Mihai Predaeae850c2009-05-13 10:13:48 +0200366 }
Todd Kennedyc2e96d42016-09-08 15:34:03 -0700367 filterResults(resultList);
Mihai Predaeae850c2009-05-13 10:13:48 +0200368 sortResults(resultList);
369 return resultList;
370 }
371
Amith Yamasani483f3b02012-03-13 16:08:00 -0700372 public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly,
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800373 boolean visibleToEphemeral, boolean isEphemeral, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 String scheme = intent.getScheme();
375
376 ArrayList<R> finalList = new ArrayList<R>();
377
378 final boolean debug = localLOGV ||
379 ((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);
380
Joe Onorato8a9b2202010-02-26 18:56:32 -0800381 if (debug) Slog.v(
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700382 TAG, "Resolving type=" + resolvedType + " scheme=" + scheme
383 + " defaultOnly=" + defaultOnly + " userId=" + userId + " of " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700385 F[] firstTypeCut = null;
386 F[] secondTypeCut = null;
387 F[] thirdTypeCut = null;
388 F[] schemeCut = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389
390 // If the intent includes a MIME type, then we want to collect all of
391 // the filters that match that MIME type.
392 if (resolvedType != null) {
393 int slashpos = resolvedType.indexOf('/');
394 if (slashpos > 0) {
395 final String baseType = resolvedType.substring(0, slashpos);
396 if (!baseType.equals("*")) {
397 if (resolvedType.length() != slashpos+2
398 || resolvedType.charAt(slashpos+1) != '*') {
399 // Not a wild card, so we can just look for all filters that
400 // completely match or wildcards whose base type matches.
401 firstTypeCut = mTypeToFilter.get(resolvedType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700402 if (debug) Slog.v(TAG, "First type cut: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 secondTypeCut = mWildTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700404 if (debug) Slog.v(TAG, "Second type cut: "
405 + Arrays.toString(secondTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 } else {
407 // We can match anything with our base type.
408 firstTypeCut = mBaseTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700409 if (debug) Slog.v(TAG, "First type cut: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 secondTypeCut = mWildTypeToFilter.get(baseType);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700411 if (debug) Slog.v(TAG, "Second type cut: "
412 + Arrays.toString(secondTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
414 // Any */* types always apply, but we only need to do this
415 // if the intent type was not already */*.
416 thirdTypeCut = mWildTypeToFilter.get("*");
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700417 if (debug) Slog.v(TAG, "Third type cut: " + Arrays.toString(thirdTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 } else if (intent.getAction() != null) {
419 // The intent specified any type ({@literal *}/*). This
420 // can be a whole heck of a lot of things, so as a first
421 // cut let's use the action instead.
422 firstTypeCut = mTypedActionToFilter.get(intent.getAction());
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700423 if (debug) Slog.v(TAG, "Typed Action list: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425 }
426 }
427
428 // If the intent includes a data URI, then we want to collect all of
429 // the filters that match its scheme (we will further refine matches
430 // on the authority and path by directly matching each resulting filter).
431 if (scheme != null) {
432 schemeCut = mSchemeToFilter.get(scheme);
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700433 if (debug) Slog.v(TAG, "Scheme list: " + Arrays.toString(schemeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 }
435
436 // If the intent does not specify any data -- either a MIME type or
437 // a URI -- then we will only be looking for matches against empty
438 // data.
439 if (resolvedType == null && scheme == null && intent.getAction() != null) {
440 firstTypeCut = mActionToFilter.get(intent.getAction());
Dianne Hackborn38ba6e92013-09-23 11:08:52 -0700441 if (debug) Slog.v(TAG, "Action list: " + Arrays.toString(firstTypeCut));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443
Jeff Brown2c376fc2011-01-28 17:34:01 -0800444 FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 if (firstTypeCut != null) {
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800446 buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
447 isEphemeral, resolvedType, scheme, firstTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 }
449 if (secondTypeCut != null) {
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800450 buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
451 isEphemeral, resolvedType, scheme, secondTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
453 if (thirdTypeCut != null) {
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800454 buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
455 isEphemeral, resolvedType, scheme, thirdTypeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 }
457 if (schemeCut != null) {
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800458 buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
459 isEphemeral, resolvedType, scheme, schemeCut, finalList, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
Todd Kennedyc2e96d42016-09-08 15:34:03 -0700461 filterResults(finalList);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 sortResults(finalList);
463
464 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800465 Slog.v(TAG, "Final result list:");
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700466 for (int i=0; i<finalList.size(); i++) {
467 Slog.v(TAG, " " + finalList.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469 }
470 return finalList;
471 }
472
473 /**
474 * Control whether the given filter is allowed to go into the result
475 * list. Mainly intended to prevent adding multiple filters for the
476 * same target object.
477 */
478 protected boolean allowFilterResult(F filter, List<R> dest) {
479 return true;
480 }
481
Dianne Hackborne7f97212011-02-24 14:40:20 -0800482 /**
483 * Returns whether the object associated with the given filter is
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800484 * "stopped", that is whether it should not be included in the result
Dianne Hackborne7f97212011-02-24 14:40:20 -0800485 * if the intent requests to excluded stopped objects.
486 */
Amith Yamasani483f3b02012-03-13 16:08:00 -0700487 protected boolean isFilterStopped(F filter, int userId) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800488 return false;
489 }
490
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700491 /**
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800492 * Returns whether the given filter is "verified" that is whether it has been verified against
493 * its data URIs.
494 *
495 * The verification would happen only and only if the Intent action is
496 * {@link android.content.Intent#ACTION_VIEW} and the Intent category is
497 * {@link android.content.Intent#CATEGORY_BROWSABLE} and the Intent data scheme
498 * is "http" or "https".
499 *
500 * @see android.content.IntentFilter#setAutoVerify(boolean)
501 * @see android.content.IntentFilter#getAutoVerify()
502 */
503 protected boolean isFilterVerified(F filter) {
504 return filter.isVerified();
505 }
506
507 /**
Ben Gruver4efe9402013-04-02 21:18:41 -0700508 * Returns whether this filter is owned by this package. This must be
509 * implemented to provide correct filtering of Intents that have
510 * specified a package name they are to be delivered to.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700511 */
Ben Gruver4efe9402013-04-02 21:18:41 -0700512 protected abstract boolean isPackageForFilter(String packageName, F filter);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700513
514 protected abstract F[] newArray(int size);
515
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700516 @SuppressWarnings("unchecked")
Amith Yamasani483f3b02012-03-13 16:08:00 -0700517 protected R newResult(F filter, int match, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 return (R)filter;
519 }
520
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700521 @SuppressWarnings("unchecked")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 protected void sortResults(List<R> results) {
523 Collections.sort(results, mResolvePrioritySorter);
524 }
525
Todd Kennedyc2e96d42016-09-08 15:34:03 -0700526 /**
527 * Apply filtering to the results. This happens before the results are sorted.
528 */
529 protected void filterResults(List<R> results) {
530 }
531
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700532 protected void dumpFilter(PrintWriter out, String prefix, F filter) {
533 out.print(prefix); out.println(filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 }
535
Dianne Hackbornd052a942014-11-21 15:23:13 -0800536 protected Object filterToLabel(F filter) {
537 return "IntentFilter";
538 }
539
540 protected void dumpFilterLabel(PrintWriter out, String prefix, Object label, int count) {
541 out.print(prefix); out.print(label); out.print(": "); out.println(count);
542 }
543
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700544 private final void addFilter(ArrayMap<String, F[]> map, String name, F filter) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700545 F[] array = map.get(name);
546 if (array == null) {
547 array = newArray(2);
548 map.put(name, array);
549 array[0] = filter;
550 } else {
551 final int N = array.length;
552 int i = N;
553 while (i > 0 && array[i-1] == null) {
554 i--;
555 }
556 if (i < N) {
557 array[i] = filter;
558 } else {
559 F[] newa = newArray((N*3)/2);
560 System.arraycopy(array, 0, newa, 0, N);
561 newa[N] = filter;
562 map.put(name, newa);
563 }
564 }
565 }
566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 private final int register_mime_types(F filter, String prefix) {
568 final Iterator<String> i = filter.typesIterator();
569 if (i == null) {
570 return 0;
571 }
572
573 int num = 0;
574 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800575 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800577 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 String baseName = name;
579 final int slashpos = name.indexOf('/');
580 if (slashpos > 0) {
581 baseName = name.substring(0, slashpos).intern();
582 } else {
583 name = name + "/*";
584 }
585
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700586 addFilter(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587
588 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700589 addFilter(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700591 addFilter(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 }
593 }
594
595 return num;
596 }
597
598 private final int unregister_mime_types(F filter, String prefix) {
599 final Iterator<String> i = filter.typesIterator();
600 if (i == null) {
601 return 0;
602 }
603
604 int num = 0;
605 while (i.hasNext()) {
Kenny Root502e9a42011-01-10 13:48:15 -0800606 String name = i.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800608 if (localLOGV) Slog.v(TAG, prefix + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 String baseName = name;
610 final int slashpos = name.indexOf('/');
611 if (slashpos > 0) {
612 baseName = name.substring(0, slashpos).intern();
613 } else {
614 name = name + "/*";
615 }
616
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700617 remove_all_objects(mTypeToFilter, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618
619 if (slashpos > 0) {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700620 remove_all_objects(mBaseTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 } else {
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700622 remove_all_objects(mWildTypeToFilter, baseName, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
624 }
625 return num;
626 }
627
628 private final int register_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700629 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 if (i == null) {
631 return 0;
632 }
633
634 int num = 0;
635 while (i.hasNext()) {
636 String name = i.next();
637 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800638 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700639 addFilter(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
641 return num;
642 }
643
644 private final int unregister_intent_filter(F filter, Iterator<String> i,
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700645 ArrayMap<String, F[]> dest, String prefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 if (i == null) {
647 return 0;
648 }
649
650 int num = 0;
651 while (i.hasNext()) {
652 String name = i.next();
653 num++;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800654 if (localLOGV) Slog.v(TAG, prefix + name);
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700655 remove_all_objects(dest, name, filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
657 return num;
658 }
659
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700660 private final void remove_all_objects(ArrayMap<String, F[]> map, String name,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700661 Object object) {
662 F[] array = map.get(name);
663 if (array != null) {
664 int LAST = array.length-1;
665 while (LAST >= 0 && array[LAST] == null) {
666 LAST--;
667 }
668 for (int idx=LAST; idx>=0; idx--) {
669 if (array[idx] == object) {
670 final int remain = LAST - idx;
671 if (remain > 0) {
672 System.arraycopy(array, idx+1, array, idx, remain);
673 }
674 array[LAST] = null;
675 LAST--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
677 }
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700678 if (LAST < 0) {
679 map.remove(name);
680 } else if (LAST < (array.length/2)) {
681 F[] newa = newArray(LAST+2);
682 System.arraycopy(array, 0, newa, 0, LAST+1);
683 map.put(name, newa);
684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687
Jeff Brown2c376fc2011-01-28 17:34:01 -0800688 private static FastImmutableArraySet<String> getFastIntentCategories(Intent intent) {
689 final Set<String> categories = intent.getCategories();
690 if (categories == null) {
691 return null;
692 }
693 return new FastImmutableArraySet<String>(categories.toArray(new String[categories.size()]));
694 }
695
696 private void buildResolveList(Intent intent, FastImmutableArraySet<String> categories,
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800697 boolean debug, boolean defaultOnly, boolean visibleToEphemeral, boolean isEphemeral,
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700698 String resolvedType, String scheme, F[] src, List<R> dest, int userId) {
Jeff Brown2c376fc2011-01-28 17:34:01 -0800699 final String action = intent.getAction();
700 final Uri data = intent.getData();
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700701 final String packageName = intent.getPackage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702
Dianne Hackborne7f97212011-02-24 14:40:20 -0800703 final boolean excludingStopped = intent.isExcludingStopped();
704
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700705 final Printer logPrinter;
706 final PrintWriter logPrintWriter;
707 if (debug) {
708 logPrinter = new LogPrinter(Log.VERBOSE, TAG, Log.LOG_ID_SYSTEM);
709 logPrintWriter = new FastPrintWriter(logPrinter);
710 } else {
711 logPrinter = null;
712 logPrintWriter = null;
713 }
714
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700715 final int N = src != null ? src.length : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 boolean hasNonDefaults = false;
717 int i;
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700718 F filter;
719 for (i=0; i<N && (filter=src[i]) != null; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 int match;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800721 if (debug) Slog.v(TAG, "Matching against filter " + filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722
Amith Yamasani483f3b02012-03-13 16:08:00 -0700723 if (excludingStopped && isFilterStopped(filter, userId)) {
Dianne Hackborne7f97212011-02-24 14:40:20 -0800724 if (debug) {
725 Slog.v(TAG, " Filter's target is stopped; skipping");
726 }
727 continue;
728 }
729
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700730 // Is delivery being limited to filters owned by a particular package?
Ben Gruver4efe9402013-04-02 21:18:41 -0700731 if (packageName != null && !isPackageForFilter(packageName, filter)) {
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700732 if (debug) {
733 Slog.v(TAG, " Filter is not from package " + packageName + "; skipping");
734 }
735 continue;
736 }
737
Todd Kennedy7bc3a702016-12-08 14:54:48 -0800738 // throw out filters that aren't visible to ephemeral apps
739 if (visibleToEphemeral && !filter.isVisibleToEphemeral()) {
740 continue;
741 }
742 // throw out ephemeral filters if we're not explicitly requesting them
743 if (!isEphemeral && filter.isEphemeral()) {
744 continue;
745 }
746
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800747 // Are we verified ?
748 if (filter.getAutoVerify()) {
749 if (localVerificationLOGV || debug) {
750 Slog.v(TAG, " Filter verified: " + isFilterVerified(filter));
Christopher Tate72c10a22015-06-12 18:31:24 -0700751 int authorities = filter.countDataAuthorities();
752 for (int z = 0; z < authorities; z++) {
753 Slog.v(TAG, " " + filter.getDataAuthority(z).getHost());
754 }
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -0800755 }
756 }
757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 // Do we already have this one?
759 if (!allowFilterResult(filter, dest)) {
760 if (debug) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 Slog.v(TAG, " Filter's target already added");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 continue;
764 }
765
Jeff Brown2c376fc2011-01-28 17:34:01 -0800766 match = filter.match(action, resolvedType, scheme, data, categories, TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 if (match >= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800768 if (debug) Slog.v(TAG, " Filter matched! match=0x" +
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700769 Integer.toHexString(match) + " hasDefault="
770 + filter.hasCategory(Intent.CATEGORY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 if (!defaultOnly || filter.hasCategory(Intent.CATEGORY_DEFAULT)) {
Amith Yamasani483f3b02012-03-13 16:08:00 -0700772 final R oneResult = newResult(filter, match, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 if (oneResult != null) {
774 dest.add(oneResult);
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700775 if (debug) {
776 dumpFilter(logPrintWriter, " ", filter);
777 logPrintWriter.flush();
778 filter.dump(logPrinter, " ");
779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
781 } else {
782 hasNonDefaults = true;
783 }
784 } else {
785 if (debug) {
786 String reason;
787 switch (match) {
788 case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;
789 case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;
790 case IntentFilter.NO_MATCH_DATA: reason = "data"; break;
791 case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;
792 default: reason = "unknown reason"; break;
793 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800794 Slog.v(TAG, " Filter did not match: " + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
796 }
797 }
798
Todd Kennedy5cb3b7a2016-03-25 06:58:12 -0700799 if (debug && hasNonDefaults) {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700800 if (dest.size() == 0) {
Todd Kennedy5cb3b7a2016-03-25 06:58:12 -0700801 Slog.v(TAG, "resolveIntent failed: found match, but none with CATEGORY_DEFAULT");
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700802 } else if (dest.size() > 1) {
Todd Kennedy5cb3b7a2016-03-25 06:58:12 -0700803 Slog.v(TAG, "resolveIntent: multiple matches, only some with CATEGORY_DEFAULT");
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -0700804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806 }
807
808 // Sorts a List of IntentFilter objects into descending priority order.
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700809 @SuppressWarnings("rawtypes")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 private static final Comparator mResolvePrioritySorter = new Comparator() {
811 public int compare(Object o1, Object o2) {
Kenny Root502e9a42011-01-10 13:48:15 -0800812 final int q1 = ((IntentFilter) o1).getPriority();
813 final int q2 = ((IntentFilter) o2).getPriority();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 return (q1 > q2) ? -1 : ((q1 < q2) ? 1 : 0);
815 }
816 };
817
818 /**
819 * All filters that have been registered.
820 */
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700821 private final ArraySet<F> mFilters = new ArraySet<F>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822
823 /**
824 * All of the MIME types that have been registered, such as "image/jpeg",
825 * "image/*", or "{@literal *}/*".
826 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700827 private final ArrayMap<String, F[]> mTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
829 /**
830 * The base names of all of all fully qualified MIME types that have been
831 * registered, such as "image" or "*". Wild card MIME types such as
832 * "image/*" will not be here.
833 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700834 private final ArrayMap<String, F[]> mBaseTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835
836 /**
837 * The base names of all of the MIME types with a sub-type wildcard that
838 * have been registered. For example, a filter with "image/*" will be
839 * included here as "image" but one with "image/jpeg" will not be
840 * included here. This also includes the "*" for the "{@literal *}/*"
841 * MIME type.
842 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700843 private final ArrayMap<String, F[]> mWildTypeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844
845 /**
846 * All of the URI schemes (such as http) that have been registered.
847 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700848 private final ArrayMap<String, F[]> mSchemeToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849
850 /**
851 * All of the actions that have been registered, but only those that did
852 * not specify data.
853 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700854 private final ArrayMap<String, F[]> mActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855
856 /**
857 * All of the actions that have been registered and specified a MIME type.
858 */
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -0700859 private final ArrayMap<String, F[]> mTypedActionToFilter = new ArrayMap<String, F[]>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860}