1 /*
2  * Copyright (C) 2016 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 android.net.metrics;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.annotation.TestApi;
22 import android.annotation.UnsupportedAppUsage;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.text.TextUtils;
26 
27 /**
28  * An event recorded when a DhcpClient state machine transitions to a new state.
29  * {@hide}
30  */
31 @SystemApi
32 @TestApi
33 public final class DhcpClientEvent implements IpConnectivityLog.Event {
34 
35     // Names for recording DhcpClient pseudo-state transitions.
36 
37     /** @hide */
38     public final String msg;
39     /** @hide */
40     public final int durationMs;
41 
42     @UnsupportedAppUsage
DhcpClientEvent(String msg, int durationMs)43     private DhcpClientEvent(String msg, int durationMs) {
44         this.msg = msg;
45         this.durationMs = durationMs;
46     }
47 
DhcpClientEvent(Parcel in)48     private DhcpClientEvent(Parcel in) {
49         this.msg = in.readString();
50         this.durationMs = in.readInt();
51     }
52 
53     /**
54      * Utility to create an instance of {@link ApfProgramEvent}.
55      */
56     public static final class Builder {
57         private String mMsg;
58         private int mDurationMs;
59 
60         /**
61          * Set the message of the event.
62          */
63         @NonNull
setMsg(String msg)64         public Builder setMsg(String msg) {
65             mMsg = msg;
66             return this;
67         }
68 
69         /**
70          * Set the duration of the event in milliseconds.
71          */
72         @NonNull
setDurationMs(int durationMs)73         public Builder setDurationMs(int durationMs) {
74             mDurationMs = durationMs;
75             return this;
76         }
77 
78         /**
79          * Create a new {@link DhcpClientEvent}.
80          */
81         @NonNull
build()82         public DhcpClientEvent build() {
83             return new DhcpClientEvent(mMsg, mDurationMs);
84         }
85     }
86 
87     /** @hide */
88     @Override
writeToParcel(Parcel out, int flags)89     public void writeToParcel(Parcel out, int flags) {
90         out.writeString(msg);
91         out.writeInt(durationMs);
92     }
93 
94     /** @hide */
95     @Override
describeContents()96     public int describeContents() {
97         return 0;
98     }
99 
100     @Override
toString()101     public String toString() {
102         return String.format("DhcpClientEvent(%s, %dms)", msg, durationMs);
103     }
104 
105     @Override
equals(Object obj)106     public boolean equals(Object obj) {
107         if (obj == null || !(obj.getClass().equals(DhcpClientEvent.class))) return false;
108         final DhcpClientEvent other = (DhcpClientEvent) obj;
109         return TextUtils.equals(msg, other.msg)
110                 && durationMs == other.durationMs;
111     }
112 
113     /** @hide */
114     public static final @android.annotation.NonNull Parcelable.Creator<DhcpClientEvent> CREATOR
115         = new Parcelable.Creator<DhcpClientEvent>() {
116         public DhcpClientEvent createFromParcel(Parcel in) {
117             return new DhcpClientEvent(in);
118         }
119 
120         public DhcpClientEvent[] newArray(int size) {
121             return new DhcpClientEvent[size];
122         }
123     };
124 }
125