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