ValidationProbeEvent.java revision cf6b12f50aa3251a3fc9929c565f8a19eaaac49e
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 * An event recorded by NetworkMonitor when sending a probe for finding captive portals.
32 * {@hide}
33 */
34@SystemApi
35public final class ValidationProbeEvent implements Parcelable {
36
37    public static final int PROBE_DNS   = 0;
38    public static final int PROBE_HTTP  = 1;
39    public static final int PROBE_HTTPS = 2;
40    public static final int PROBE_PAC   = 3;
41
42    public static final int DNS_FAILURE = 0;
43    public static final int DNS_SUCCESS = 1;
44
45    /** {@hide} */
46    @IntDef(value = {PROBE_DNS, PROBE_HTTP, PROBE_HTTPS, PROBE_PAC})
47    @Retention(RetentionPolicy.SOURCE)
48    public @interface ProbeType {}
49
50    /** {@hide} */
51    @IntDef(value = {DNS_FAILURE, DNS_SUCCESS})
52    @Retention(RetentionPolicy.SOURCE)
53    public @interface ReturnCode {}
54
55    public final int netId;
56    public final long durationMs;
57    public final @ProbeType int probeType;
58    public final @ReturnCode int returnCode;
59
60    /** @hide */
61    public ValidationProbeEvent(
62            int netId, long durationMs, @ProbeType int probeType, @ReturnCode int returnCode) {
63        this.netId = netId;
64        this.durationMs = durationMs;
65        this.probeType = probeType;
66        this.returnCode = returnCode;
67    }
68
69    private ValidationProbeEvent(Parcel in) {
70        netId = in.readInt();
71        durationMs = in.readLong();
72        probeType = in.readInt();
73        returnCode = in.readInt();
74    }
75
76    @Override
77    public void writeToParcel(Parcel out, int flags) {
78        out.writeInt(netId);
79        out.writeLong(durationMs);
80        out.writeInt(probeType);
81        out.writeInt(returnCode);
82    }
83
84    @Override
85    public int describeContents() {
86        return 0;
87    }
88
89    public static final Parcelable.Creator<ValidationProbeEvent> CREATOR
90        = new Parcelable.Creator<ValidationProbeEvent>() {
91        public ValidationProbeEvent createFromParcel(Parcel in) {
92            return new ValidationProbeEvent(in);
93        }
94
95        public ValidationProbeEvent[] newArray(int size) {
96            return new ValidationProbeEvent[size];
97        }
98    };
99
100    /** @hide */
101    public static String getProbeName(int probeType) {
102        return Decoder.constants.get(probeType, "PROBE_???");
103    }
104
105    public static void logEvent(int netId, long durationMs, int probeType, int returnCode) {
106    }
107
108    @Override
109    public String toString() {
110        return String.format("ValidationProbeEvent(%d, %s:%d, %dms)",
111                netId, getProbeName(probeType), returnCode, durationMs);
112    }
113
114    final static class Decoder {
115        static final SparseArray<String> constants = MessageUtils.findMessageNames(
116                new Class[]{ValidationProbeEvent.class}, new String[]{"PROBE_"});
117    }
118}
119