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  * {@hide}
33  */
34 @SystemApi
35 @TestApi
36 public final class NetworkEvent implements IpConnectivityLog.Event {
37 
38     public static final int NETWORK_CONNECTED            = 1;
39     public static final int NETWORK_VALIDATED            = 2;
40     public static final int NETWORK_VALIDATION_FAILED    = 3;
41     public static final int NETWORK_CAPTIVE_PORTAL_FOUND = 4;
42     public static final int NETWORK_LINGER               = 5;
43     public static final int NETWORK_UNLINGER             = 6;
44     public static final int NETWORK_DISCONNECTED         = 7;
45 
46     public static final int NETWORK_FIRST_VALIDATION_SUCCESS      = 8;
47     public static final int NETWORK_REVALIDATION_SUCCESS          = 9;
48     public static final int NETWORK_FIRST_VALIDATION_PORTAL_FOUND = 10;
49     public static final int NETWORK_REVALIDATION_PORTAL_FOUND     = 11;
50 
51     public static final int NETWORK_CONSECUTIVE_DNS_TIMEOUT_FOUND = 12;
52 
53     public static final int NETWORK_PARTIAL_CONNECTIVITY = 13;
54 
55     /** @hide */
56     @IntDef(value = {
57             NETWORK_CONNECTED,
58             NETWORK_VALIDATED,
59             NETWORK_VALIDATION_FAILED,
60             NETWORK_CAPTIVE_PORTAL_FOUND,
61             NETWORK_LINGER,
62             NETWORK_UNLINGER,
63             NETWORK_DISCONNECTED,
64             NETWORK_FIRST_VALIDATION_SUCCESS,
65             NETWORK_REVALIDATION_SUCCESS,
66             NETWORK_FIRST_VALIDATION_PORTAL_FOUND,
67             NETWORK_REVALIDATION_PORTAL_FOUND,
68             NETWORK_CONSECUTIVE_DNS_TIMEOUT_FOUND,
69             NETWORK_PARTIAL_CONNECTIVITY,
70     })
71     @Retention(RetentionPolicy.SOURCE)
72     public @interface EventType {}
73 
74     /** @hide */
75     public final @EventType int eventType;
76     /** @hide */
77     public final long durationMs;
78 
NetworkEvent(@ventType int eventType, long durationMs)79     public NetworkEvent(@EventType int eventType, long durationMs) {
80         this.eventType = eventType;
81         this.durationMs = durationMs;
82     }
83 
NetworkEvent(@ventType int eventType)84     public NetworkEvent(@EventType int eventType) {
85         this(eventType, 0);
86     }
87 
NetworkEvent(Parcel in)88     private NetworkEvent(Parcel in) {
89         eventType = in.readInt();
90         durationMs = in.readLong();
91     }
92 
93     /** @hide */
94     @Override
writeToParcel(Parcel out, int flags)95     public void writeToParcel(Parcel out, int flags) {
96         out.writeInt(eventType);
97         out.writeLong(durationMs);
98     }
99 
100     /** @hide */
101     @Override
describeContents()102     public int describeContents() {
103         return 0;
104     }
105 
106     /** @hide */
107     public static final @android.annotation.NonNull Parcelable.Creator<NetworkEvent> CREATOR
108         = new Parcelable.Creator<NetworkEvent>() {
109         public NetworkEvent createFromParcel(Parcel in) {
110             return new NetworkEvent(in);
111         }
112 
113         public NetworkEvent[] newArray(int size) {
114             return new NetworkEvent[size];
115         }
116     };
117 
118     @Override
toString()119     public String toString() {
120         return String.format("NetworkEvent(%s, %dms)",
121                 Decoder.constants.get(eventType), durationMs);
122     }
123 
124     @Override
equals(Object obj)125     public boolean equals(Object obj) {
126         if (obj == null || !(obj.getClass().equals(NetworkEvent.class))) return false;
127         final NetworkEvent other = (NetworkEvent) obj;
128         return eventType == other.eventType
129                 && durationMs == other.durationMs;
130     }
131 
132     final static class Decoder {
133         static final SparseArray<String> constants = MessageUtils.findMessageNames(
134                 new Class[]{NetworkEvent.class}, new String[]{"NETWORK_"});
135     }
136 }
137