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.annotation.SystemApi;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.SparseArray;
24
25import com.android.internal.util.MessageUtils;
26
27import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
29
30/**
31 * {@hide}
32 */
33@SystemApi
34public final class NetworkEvent implements Parcelable {
35
36    public static final int NETWORK_CONNECTED            = 1;
37    public static final int NETWORK_VALIDATED            = 2;
38    public static final int NETWORK_VALIDATION_FAILED    = 3;
39    public static final int NETWORK_CAPTIVE_PORTAL_FOUND = 4;
40    public static final int NETWORK_LINGER               = 5;
41    public static final int NETWORK_UNLINGER             = 6;
42    public static final int NETWORK_DISCONNECTED         = 7;
43
44    /** {@hide} */
45    @IntDef(value = {
46            NETWORK_CONNECTED,
47            NETWORK_VALIDATED,
48            NETWORK_VALIDATION_FAILED,
49            NETWORK_CAPTIVE_PORTAL_FOUND,
50            NETWORK_LINGER,
51            NETWORK_UNLINGER,
52            NETWORK_DISCONNECTED,
53    })
54    @Retention(RetentionPolicy.SOURCE)
55    public @interface EventType {}
56
57    public final int netId;
58    public final @EventType int eventType;
59    public final long durationMs;
60
61    /** {@hide} */
62    public NetworkEvent(int netId, @EventType int eventType, long durationMs) {
63        this.netId = netId;
64        this.eventType = eventType;
65        this.durationMs = durationMs;
66    }
67
68    /** {@hide} */
69    public NetworkEvent(int netId, @EventType int eventType) {
70        this(netId, eventType, 0);
71    }
72
73    private NetworkEvent(Parcel in) {
74        netId = in.readInt();
75        eventType = in.readInt();
76        durationMs = in.readLong();
77    }
78
79    @Override
80    public void writeToParcel(Parcel out, int flags) {
81        out.writeInt(netId);
82        out.writeInt(eventType);
83        out.writeLong(durationMs);
84    }
85
86    @Override
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    public static void logEvent(int netId, int eventType) {
103    }
104
105    public static void logValidated(int netId, long durationMs) {
106    }
107
108    public static void logCaptivePortalFound(int netId, long durationMs) {
109    }
110
111    @Override
112    public String toString() {
113        return String.format("NetworkEvent(%d, %s, %dms)",
114                netId, Decoder.constants.get(eventType), durationMs);
115    }
116
117    final static class Decoder {
118        static final SparseArray<String> constants = MessageUtils.findMessageNames(
119                new Class[]{NetworkEvent.class}, new String[]{"NETWORK_"});
120    }
121}
122