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