NetworkEvent.java revision 95cb226c1a4ff47531da65ef2617ade0dea5c9b8
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
17package android.net.metrics;
18
19import android.annotation.IntDef;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.util.SparseArray;
23
24import com.android.internal.util.MessageUtils;
25
26import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
28
29/**
30 * {@hide}
31 */
32public 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 int netId;
64    public final @EventType int eventType;
65    public final long durationMs;
66
67    public NetworkEvent(int netId, @EventType int eventType, long durationMs) {
68        this.netId = netId;
69        this.eventType = eventType;
70        this.durationMs = durationMs;
71    }
72
73    public NetworkEvent(int netId, @EventType int eventType) {
74        this(netId, eventType, 0);
75    }
76
77    private NetworkEvent(Parcel in) {
78        netId = in.readInt();
79        eventType = in.readInt();
80        durationMs = in.readLong();
81    }
82
83    @Override
84    public void writeToParcel(Parcel out, int flags) {
85        out.writeInt(netId);
86        out.writeInt(eventType);
87        out.writeLong(durationMs);
88    }
89
90    @Override
91    public int describeContents() {
92        return 0;
93    }
94
95    public static final Parcelable.Creator<NetworkEvent> CREATOR
96        = new Parcelable.Creator<NetworkEvent>() {
97        public NetworkEvent createFromParcel(Parcel in) {
98            return new NetworkEvent(in);
99        }
100
101        public NetworkEvent[] newArray(int size) {
102            return new NetworkEvent[size];
103        }
104    };
105
106    @Override
107    public String toString() {
108        return String.format("NetworkEvent(%d, %s, %dms)",
109                netId, Decoder.constants.get(eventType), durationMs);
110    }
111
112    final static class Decoder {
113        static final SparseArray<String> constants = MessageUtils.findMessageNames(
114                new Class[]{NetworkEvent.class}, new String[]{"NETWORK_"});
115    }
116}
117