1 /*
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 
17 package com.android.server.am;
18 
19 import android.annotation.Nullable;
20 import android.content.IntentFilter;
21 import android.util.PrintWriterPrinter;
22 import android.util.Printer;
23 import android.util.proto.ProtoOutputStream;
24 
25 import dalvik.annotation.optimization.NeverCompile;
26 
27 import java.io.PrintWriter;
28 
29 public final class BroadcastFilter extends IntentFilter {
30     // Back-pointer to the list this filter is in.
31     final ReceiverList receiverList;
32     final String packageName;
33     final String featureId;
34     final String receiverId;
35     final String requiredPermission;
36     final int owningUid;
37     final int owningUserId;
38     final boolean instantApp;
39     final boolean visibleToInstantApp;
40     public final boolean exported;
41 
BroadcastFilter(IntentFilter _filter, ReceiverList _receiverList, String _packageName, String _featureId, String _receiverId, String _requiredPermission, int _owningUid, int _userId, boolean _instantApp, boolean _visibleToInstantApp, boolean _exported)42     BroadcastFilter(IntentFilter _filter, ReceiverList _receiverList,
43             String _packageName, String _featureId, String _receiverId, String _requiredPermission,
44             int _owningUid, int _userId, boolean _instantApp, boolean _visibleToInstantApp,
45             boolean _exported) {
46         super(_filter);
47         receiverList = _receiverList;
48         packageName = _packageName;
49         featureId = _featureId;
50         receiverId = _receiverId;
51         requiredPermission = _requiredPermission;
52         owningUid = _owningUid;
53         owningUserId = _userId;
54         instantApp = _instantApp;
55         visibleToInstantApp = _visibleToInstantApp;
56         exported = _exported;
57     }
58 
getReceiverClassName()59     public @Nullable String getReceiverClassName() {
60         if (receiverId != null) {
61             final int index = receiverId.lastIndexOf('@');
62             if (index > 0) {
63                 return receiverId.substring(0, index);
64             }
65         }
66         return null;
67     }
68 
69     @NeverCompile
dumpDebug(ProtoOutputStream proto, long fieldId)70     public void dumpDebug(ProtoOutputStream proto, long fieldId) {
71         long token = proto.start(fieldId);
72         super.dumpDebug(proto, BroadcastFilterProto.INTENT_FILTER);
73         if (requiredPermission != null) {
74             proto.write(BroadcastFilterProto.REQUIRED_PERMISSION, requiredPermission);
75         }
76         proto.write(BroadcastFilterProto.HEX_HASH, Integer.toHexString(System.identityHashCode(this)));
77         proto.write(BroadcastFilterProto.OWNING_USER_ID, owningUserId);
78         proto.end(token);
79     }
80 
81     @NeverCompile
dump(PrintWriter pw, String prefix)82     public void dump(PrintWriter pw, String prefix) {
83         dumpInReceiverList(pw, new PrintWriterPrinter(pw), prefix);
84         receiverList.dumpLocal(pw, prefix);
85     }
86 
87     @NeverCompile
dumpBrief(PrintWriter pw, String prefix)88     public void dumpBrief(PrintWriter pw, String prefix) {
89         dumpBroadcastFilterState(pw, prefix);
90     }
91 
92     @NeverCompile
dumpInReceiverList(PrintWriter pw, Printer pr, String prefix)93     public void dumpInReceiverList(PrintWriter pw, Printer pr, String prefix) {
94         super.dump(pr, prefix);
95         dumpBroadcastFilterState(pw, prefix);
96     }
97 
98     @NeverCompile
dumpBroadcastFilterState(PrintWriter pw, String prefix)99     void dumpBroadcastFilterState(PrintWriter pw, String prefix) {
100         if (requiredPermission != null) {
101             pw.print(prefix); pw.print("requiredPermission="); pw.println(requiredPermission);
102         }
103     }
104 
toString()105     public String toString() {
106         StringBuilder sb = new StringBuilder();
107         sb.append("BroadcastFilter{");
108         sb.append(Integer.toHexString(System.identityHashCode(this)));
109         sb.append(' ');
110         sb.append(owningUid);
111         sb.append("/u");
112         sb.append(owningUserId);
113         sb.append(' ');
114         sb.append(receiverList);
115         sb.append('}');
116         return sb.toString();
117     }
118 }
119