blob: 1999e78decc473695a7a99a0e8243f5f7f2acb19 [file] [log] [blame]
Hugo Benichicc92c6e2016-04-21 15:02:38 +09001/*
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
Hugo Benichicf6b12f2016-07-04 11:28:05 +090019import android.annotation.IntDef;
Hugo Benichicc92c6e2016-04-21 15:02:38 +090020import android.os.Parcel;
21import android.os.Parcelable;
Hugo Benichi5df9d722016-04-25 17:16:35 +090022import android.util.SparseArray;
23
24import com.android.internal.util.MessageUtils;
Hugo Benichicc92c6e2016-04-21 15:02:38 +090025
Hugo Benichicf6b12f2016-07-04 11:28:05 +090026import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
28
Hugo Benichicc92c6e2016-04-21 15:02:38 +090029/**
30 * {@hide}
31 */
Hugo Benichicfddd682016-05-31 16:28:06 +090032public final class NetworkEvent implements Parcelable {
Hugo Benichicc92c6e2016-04-21 15:02:38 +090033
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
Hugo Benichi147aa6d2016-11-15 23:23:24 +090042 public static final int NETWORK_FIRST_VALIDATION_SUCCESS = 8;
Hugo Benichi147aa6d2016-11-15 23:23:24 +090043 public static final int NETWORK_REVALIDATION_SUCCESS = 9;
Hugo Benichi147aa6d2016-11-15 23:23:24 +090044 public static final int NETWORK_FIRST_VALIDATION_PORTAL_FOUND = 10;
Hugo Benichi147aa6d2016-11-15 23:23:24 +090045 public static final int NETWORK_REVALIDATION_PORTAL_FOUND = 11;
46
Hugo Benichicf6b12f2016-07-04 11:28:05 +090047 @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,
Hugo Benichi147aa6d2016-11-15 23:23:24 +090055 NETWORK_FIRST_VALIDATION_SUCCESS,
56 NETWORK_REVALIDATION_SUCCESS,
57 NETWORK_FIRST_VALIDATION_PORTAL_FOUND,
58 NETWORK_REVALIDATION_PORTAL_FOUND,
Hugo Benichicf6b12f2016-07-04 11:28:05 +090059 })
60 @Retention(RetentionPolicy.SOURCE)
61 public @interface EventType {}
62
Hugo Benichicf6b12f2016-07-04 11:28:05 +090063 public final @EventType int eventType;
Hugo Benichicc92c6e2016-04-21 15:02:38 +090064 public final long durationMs;
65
Hugo Benichi97bfd272017-11-11 08:06:43 +090066 public NetworkEvent(@EventType int eventType, long durationMs) {
Hugo Benichicc92c6e2016-04-21 15:02:38 +090067 this.eventType = eventType;
68 this.durationMs = durationMs;
69 }
70
Hugo Benichi97bfd272017-11-11 08:06:43 +090071 public NetworkEvent(@EventType int eventType) {
72 this(eventType, 0);
Hugo Benichicfddd682016-05-31 16:28:06 +090073 }
74
Hugo Benichicc92c6e2016-04-21 15:02:38 +090075 private NetworkEvent(Parcel in) {
Hugo Benichicc92c6e2016-04-21 15:02:38 +090076 eventType = in.readInt();
77 durationMs = in.readLong();
78 }
79
Hugo Benichicf6b12f2016-07-04 11:28:05 +090080 @Override
Hugo Benichicc92c6e2016-04-21 15:02:38 +090081 public void writeToParcel(Parcel out, int flags) {
Hugo Benichicc92c6e2016-04-21 15:02:38 +090082 out.writeInt(eventType);
83 out.writeLong(durationMs);
84 }
85
Hugo Benichicf6b12f2016-07-04 11:28:05 +090086 @Override
Hugo Benichicc92c6e2016-04-21 15:02:38 +090087 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
Hugo Benichi5df9d722016-04-25 17:16:35 +0900102 @Override
103 public String toString() {
Hugo Benichi97bfd272017-11-11 08:06:43 +0900104 return String.format("NetworkEvent(%s, %dms)",
105 Decoder.constants.get(eventType), durationMs);
Hugo Benichi5df9d722016-04-25 17:16:35 +0900106 }
107
108 final static class Decoder {
109 static final SparseArray<String> constants = MessageUtils.findMessageNames(
110 new Class[]{NetworkEvent.class}, new String[]{"NETWORK_"});
111 }
Hugo Benichicfddd682016-05-31 16:28:06 +0900112}