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.IntDef;
20 import android.annotation.SystemApi;
21 import android.annotation.TestApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.util.SparseArray;
25 
26 import com.android.internal.util.MessageUtils;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 
31 /**
32  * An event recorded by IpClient when IP provisioning completes for a network or
33  * when a network disconnects.
34  * {@hide}
35  */
36 @SystemApi
37 @TestApi
38 public final class IpManagerEvent implements IpConnectivityLog.Event {
39 
40     public static final int PROVISIONING_OK                       = 1;
41     public static final int PROVISIONING_FAIL                     = 2;
42     public static final int COMPLETE_LIFECYCLE                    = 3;
43     public static final int ERROR_STARTING_IPV4                   = 4;
44     public static final int ERROR_STARTING_IPV6                   = 5;
45     public static final int ERROR_STARTING_IPREACHABILITYMONITOR  = 6;
46     public static final int ERROR_INVALID_PROVISIONING            = 7;
47     public static final int ERROR_INTERFACE_NOT_FOUND             = 8;
48 
49     /** @hide */
50     @IntDef(value = {
51             PROVISIONING_OK, PROVISIONING_FAIL, COMPLETE_LIFECYCLE,
52             ERROR_STARTING_IPV4, ERROR_STARTING_IPV6, ERROR_STARTING_IPREACHABILITYMONITOR,
53             ERROR_INVALID_PROVISIONING, ERROR_INTERFACE_NOT_FOUND,
54     })
55     @Retention(RetentionPolicy.SOURCE)
56     public @interface EventType {}
57 
58     /** @hide */
59     public final @EventType int eventType;
60     /** @hide */
61     public final long durationMs;
62 
IpManagerEvent(@ventType int eventType, long duration)63     public IpManagerEvent(@EventType int eventType, long duration) {
64         this.eventType = eventType;
65         this.durationMs = duration;
66     }
67 
IpManagerEvent(Parcel in)68     private IpManagerEvent(Parcel in) {
69         this.eventType = in.readInt();
70         this.durationMs = in.readLong();
71     }
72 
73     /** @hide */
74     @Override
writeToParcel(Parcel out, int flags)75     public void writeToParcel(Parcel out, int flags) {
76         out.writeInt(eventType);
77         out.writeLong(durationMs);
78     }
79 
80     /** @hide */
81     @Override
describeContents()82     public int describeContents() {
83         return 0;
84     }
85 
86     /** @hide */
87     public static final @android.annotation.NonNull Parcelable.Creator<IpManagerEvent> CREATOR
88         = new Parcelable.Creator<IpManagerEvent>() {
89         public IpManagerEvent createFromParcel(Parcel in) {
90             return new IpManagerEvent(in);
91         }
92 
93         public IpManagerEvent[] newArray(int size) {
94             return new IpManagerEvent[size];
95         }
96     };
97 
98     @Override
toString()99     public String toString() {
100         return String.format("IpManagerEvent(%s, %dms)",
101                 Decoder.constants.get(eventType), durationMs);
102     }
103 
104     @Override
equals(Object obj)105     public boolean equals(Object obj) {
106         if (obj == null || !(obj.getClass().equals(IpManagerEvent.class))) return false;
107         final IpManagerEvent other = (IpManagerEvent) obj;
108         return eventType == other.eventType
109                 && durationMs == other.durationMs;
110     }
111 
112     final static class Decoder {
113         static final SparseArray<String> constants = MessageUtils.findMessageNames(
114                 new Class[]{IpManagerEvent.class},
115                 new String[]{"PROVISIONING_", "COMPLETE_", "ERROR_"});
116     }
117 }
118