1 /*
2  * Copyright (C) 2020 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.eventlib.events;
18 
19 import android.content.Context;
20 
21 import androidx.annotation.CheckResult;
22 
23 import com.android.eventlib.Event;
24 import com.android.eventlib.EventLogger;
25 import com.android.eventlib.EventLogsQuery;
26 import com.android.queryable.queries.SerializableQuery;
27 import com.android.queryable.queries.SerializableQueryHelper;
28 import com.android.queryable.queries.StringQuery;
29 import com.android.queryable.queries.StringQueryHelper;
30 
31 import java.io.Serializable;
32 
33 /**
34  * Implementation of {@link Event} which can be used for events not covered by other
35  * {@link Event} subclasses.
36  *
37  * <p>To use, set custom data as {@link #tag()} and {@link #data()}.
38  */
39 public final class CustomEvent extends Event {
40 
41     /** Begin a query for {@link CustomEvent} events. */
queryPackage(String packageName)42     public static CustomEventQuery queryPackage(String packageName) {
43         return new CustomEventQuery(packageName);
44     }
45 
46     public static final class CustomEventQuery
47             extends EventLogsQuery<CustomEvent, CustomEventQuery> {
48         StringQueryHelper<CustomEventQuery> mTag = new StringQueryHelper<>(this);
49         SerializableQueryHelper<CustomEventQuery> mData = new SerializableQueryHelper<>(this);
50 
CustomEventQuery(String packageName)51         private CustomEventQuery(String packageName) {
52             super(CustomEvent.class, packageName);
53         }
54 
55         /** Filter for a particular {@link CustomEvent#tag()}. */
56         @CheckResult
whereTag()57         public StringQuery<CustomEventQuery> whereTag() {
58             return mTag;
59         }
60 
61         /** Filter for a particular {@link CustomEvent#data()}. */
62         @CheckResult
whereData()63         public SerializableQuery<CustomEventQuery> whereData() {
64             return mData;
65         }
66 
67         @Override
filter(CustomEvent event)68         protected boolean filter(CustomEvent event) {
69             if (!mTag.matches(event.mTag)) {
70                 return false;
71             }
72             if (!mData.matches(event.mData)) {
73                 return false;
74             }
75             return true;
76         }
77     }
78 
79     /** Begin logging a {@link CustomEvent}. */
logger(Context context)80     public static CustomEventLogger logger(Context context) {
81         return new CustomEventLogger(context);
82     }
83 
84     public static final class CustomEventLogger extends EventLogger<CustomEvent> {
CustomEventLogger(Context context)85         private CustomEventLogger(Context context) {
86             super(context, new CustomEvent());
87         }
88 
89         /** Set the {@link CustomEvent#tag()}. */
setTag(String tag)90         public CustomEventLogger setTag(String tag) {
91             mEvent.mTag = tag;
92             return this;
93         }
94 
95         /** Set the {@link CustomEvent#data()}. */
setData(Serializable data)96         public CustomEventLogger setData(Serializable data) {
97             mEvent.mData = data;
98             return this;
99         }
100     }
101 
102     protected String mTag;
103     protected Serializable mData;
104 
105     /** Get the tag set using {@link CustomEventLogger#setTag(String)}. */
tag()106     public String tag() {
107         return mTag;
108     }
109 
110     /** Get the tag set using {@link CustomEventLogger#setData(Serializable)}. */
data()111     public Serializable data() {
112         return mData;
113     }
114 
115     @Override
toString()116     public String toString() {
117         return "CustomEvent{"
118                 + "tag='" + mTag + "'"
119                 + ", data=" + mData
120                 + ", packageName='" + mPackageName + "'"
121                 + ", timestamp=" + mTimestamp
122                 + "}";
123     }
124 }
125 
126 
127