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