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