• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 
17 package com.android.nfc;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.PendingIntent;
22 import android.app.PendingIntentProto;
23 import android.content.AuthorityEntryProto;
24 import android.content.IntentFilter;
25 import android.content.IntentFilterProto;
26 import android.os.PatternMatcher;
27 import android.os.PatternMatcherProto;
28 import android.util.proto.ProtoOutputStream;
29 
30 import java.util.Iterator;
31 import java.util.Objects;
32 
33 public final class Utils {
Utils()34     private Utils() {
35     }
36 
37     /**
38      * Returns true if the given {@code array} contains the given element.
39      *
40      * @param array {@code array} to check for {@code elem}
41      * @param elem {@code elem} to test for
42      * @return {@code true} if the given element is contained
43      */
arrayContains(@ullable T[] array, T elem)44     public static <T> boolean arrayContains(@Nullable T[] array, T elem) {
45         if (array == null) {
46             return false;
47         }
48         for (int i = 0; i < array.length; i++) {
49             if (Objects.equals(array[i], elem)) {
50                 return true;
51             }
52         }
53         return false;
54     }
55 
56     /** Ported (this uses public API's as opposed to private fields used in the original impl from
57      * {@link IntentFilter#dumpDebug(ProtoOutputStream, long)} */
dumpDebugIntentFilter( @onNull IntentFilter intentFilter, @NonNull ProtoOutputStream proto, long fieldId)58     public static void dumpDebugIntentFilter(
59             @NonNull IntentFilter intentFilter, @NonNull ProtoOutputStream proto, long fieldId) {
60         long token = proto.start(fieldId);
61         for (int i = 0; i < intentFilter.countActions(); i++) {
62             proto.write(IntentFilterProto.ACTIONS, intentFilter.getAction(i));
63         }
64         for (int i = 0; i < intentFilter.countCategories(); i++) {
65             proto.write(IntentFilterProto.CATEGORIES, intentFilter.getCategory(i));
66         }
67         for (int i = 0; i < intentFilter.countDataSchemes(); i++) {
68             proto.write(IntentFilterProto.DATA_SCHEMES, intentFilter.getDataScheme(i));
69         }
70         for (int i = 0; i < intentFilter.countDataSchemeSpecificParts(); i++) {
71             dumpDebugPatternMatcher(
72                     intentFilter.getDataSchemeSpecificPart(i), proto,
73                     IntentFilterProto.DATA_SCHEME_SPECS);
74         }
75         for (int i = 0; i < intentFilter.countDataAuthorities(); i++) {
76             dumpDebugAuthorityEntry(
77                     intentFilter.getDataAuthority(i), proto,
78                     IntentFilterProto.DATA_AUTHORITIES);
79         }
80         for (int i = 0; i < intentFilter.countDataPaths(); i++) {
81             dumpDebugPatternMatcher(
82                     intentFilter.getDataPath(i), proto,
83                     IntentFilterProto.DATA_PATHS);
84         }
85         for (int i = 0; i < intentFilter.countDataTypes(); i++) {
86             proto.write(IntentFilterProto.DATA_TYPES, intentFilter.getDataType(i));
87         }
88         for (int i = 0; i < intentFilter.countMimeGroups(); i++) {
89             proto.write(IntentFilterProto.MIME_GROUPS, intentFilter.getMimeGroup(i));
90         }
91         if (intentFilter.getPriority() != 0
92                 /* || TODO(b/271463752): Get this info - hasPartialTypes() */) {
93             proto.write(IntentFilterProto.PRIORITY, intentFilter.getPriority());
94             proto.write(IntentFilterProto.HAS_PARTIAL_TYPES, false /* hasPartialTypes() */);
95         }
96         proto.write(IntentFilterProto.GET_AUTO_VERIFY, intentFilter.getAutoVerify());
97         proto.end(token);
98     }
99 
100     /** Ported (this uses public API's as opposed to private fields used in the original impl from
101      * {@link PatternMatcher#dumpDebug(ProtoOutputStream, long)} */
dumpDebugPatternMatcher(@onNull PatternMatcher patternMatcher, @NonNull ProtoOutputStream proto, long fieldId)102     private static void dumpDebugPatternMatcher(@NonNull PatternMatcher patternMatcher,
103             @NonNull ProtoOutputStream proto, long fieldId) {
104         long token = proto.start(fieldId);
105         proto.write(PatternMatcherProto.PATTERN, patternMatcher.getPath());
106         proto.write(PatternMatcherProto.TYPE, patternMatcher.getType());
107         // PatternMatcherProto.PARSED_PATTERN is too much to dump, but the field is reserved to
108         // match the current data structure.
109         proto.end(token);
110     }
111 
112     /** Ported (this uses public API's as opposed to private fields used in the original impl from
113      * {@link IntentFilter.AuthorityEntry#dumpDebug(ProtoOutputStream, long)} */
dumpDebugAuthorityEntry( @onNull IntentFilter.AuthorityEntry authorityEntry, ProtoOutputStream proto, long fieldId)114     private static void dumpDebugAuthorityEntry(
115             @NonNull IntentFilter.AuthorityEntry authorityEntry, ProtoOutputStream proto,
116             long fieldId) {
117         long token = proto.start(fieldId);
118         // The public API's only give the orig host name. The fields in the proto are derived from
119         // this host info. {@see AuthorityEntry{String, String}
120         String origHost = authorityEntry.getHost();
121         boolean wild = origHost.length() > 0 && origHost.charAt(0) == '*';
122         String host = wild ? origHost.substring(1).intern() : origHost;
123         proto.write(AuthorityEntryProto.HOST, host);
124         proto.write(AuthorityEntryProto.WILD, wild);
125         proto.write(AuthorityEntryProto.PORT, authorityEntry.getPort());
126         proto.end(token);
127     }
128 
129     /** Ported (this uses public API's as opposed to private fields used in the original impl from
130      * {@link PendingIntent#dumpDebug(ProtoOutputStream, long)} */
dumpDebugPendingIntent( @onNull PendingIntent pendingIntent, @NonNull ProtoOutputStream proto, long fieldId)131     public static void dumpDebugPendingIntent(
132             @NonNull PendingIntent pendingIntent, @NonNull ProtoOutputStream proto, long fieldId) {
133         final long token = proto.start(fieldId);
134         // TODO: This is not exactly the same format as the original impl. But, it still prints
135         // the target info.
136         proto.write(PendingIntentProto.TARGET, pendingIntent.toString());
137         proto.end(token);
138     }
139 }
140