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.os.Parcel;
21 import android.os.Parcelable;
22 import android.util.SparseArray;
23 
24 import com.android.internal.util.MessageUtils;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * {@hide}
31  */
32 public final class NetworkEvent implements Parcelable {
33 
34     public static final int NETWORK_CONNECTED            = 1;
35     public static final int NETWORK_VALIDATED            = 2;
36     public static final int NETWORK_VALIDATION_FAILED    = 3;
37     public static final int NETWORK_CAPTIVE_PORTAL_FOUND = 4;
38     public static final int NETWORK_LINGER               = 5;
39     public static final int NETWORK_UNLINGER             = 6;
40     public static final int NETWORK_DISCONNECTED         = 7;
41 
42     public static final int NETWORK_FIRST_VALIDATION_SUCCESS      = 8;
43     public static final int NETWORK_REVALIDATION_SUCCESS          = 9;
44     public static final int NETWORK_FIRST_VALIDATION_PORTAL_FOUND = 10;
45     public static final int NETWORK_REVALIDATION_PORTAL_FOUND     = 11;
46 
47     @IntDef(value = {
48             NETWORK_CONNECTED,
49             NETWORK_VALIDATED,
50             NETWORK_VALIDATION_FAILED,
51             NETWORK_CAPTIVE_PORTAL_FOUND,
52             NETWORK_LINGER,
53             NETWORK_UNLINGER,
54             NETWORK_DISCONNECTED,
55             NETWORK_FIRST_VALIDATION_SUCCESS,
56             NETWORK_REVALIDATION_SUCCESS,
57             NETWORK_FIRST_VALIDATION_PORTAL_FOUND,
58             NETWORK_REVALIDATION_PORTAL_FOUND,
59     })
60     @Retention(RetentionPolicy.SOURCE)
61     public @interface EventType {}
62 
63     public final @EventType int eventType;
64     public final long durationMs;
65 
NetworkEvent(@ventType int eventType, long durationMs)66     public NetworkEvent(@EventType int eventType, long durationMs) {
67         this.eventType = eventType;
68         this.durationMs = durationMs;
69     }
70 
NetworkEvent(@ventType int eventType)71     public NetworkEvent(@EventType int eventType) {
72         this(eventType, 0);
73     }
74 
NetworkEvent(Parcel in)75     private NetworkEvent(Parcel in) {
76         eventType = in.readInt();
77         durationMs = in.readLong();
78     }
79 
80     @Override
writeToParcel(Parcel out, int flags)81     public void writeToParcel(Parcel out, int flags) {
82         out.writeInt(eventType);
83         out.writeLong(durationMs);
84     }
85 
86     @Override
describeContents()87     public int describeContents() {
88         return 0;
89     }
90 
91     public static final Parcelable.Creator<NetworkEvent> CREATOR
92         = new Parcelable.Creator<NetworkEvent>() {
93         public NetworkEvent createFromParcel(Parcel in) {
94             return new NetworkEvent(in);
95         }
96 
97         public NetworkEvent[] newArray(int size) {
98             return new NetworkEvent[size];
99         }
100     };
101 
102     @Override
toString()103     public String toString() {
104         return String.format("NetworkEvent(%s, %dms)",
105                 Decoder.constants.get(eventType), durationMs);
106     }
107 
108     final static class Decoder {
109         static final SparseArray<String> constants = MessageUtils.findMessageNames(
110                 new Class[]{NetworkEvent.class}, new String[]{"NETWORK_"});
111     }
112 }
113