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