ValidationProbeEvent.java revision 147aa6d53bc1e9f8a3632553abcf936023806e1d
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    /** {@hide} */
42    public static final int PROBE_FALLBACK  = 4;
43
44    public static final int DNS_FAILURE = 0;
45    public static final int DNS_SUCCESS = 1;
46
47    private static final int FIRST_VALIDATION  = 1 << 8;
48    private static final int REVALIDATION      = 2 << 8;
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    // probeType byte format (MSB to LSB):
58    // byte 0: unused
59    // byte 1: unused
60    // byte 2: 0 = UNKNOWN, 1 = FIRST_VALIDATION, 2 = REVALIDATION
61    // byte 3: PROBE_* constant
62    public final int probeType;
63    public final @ReturnCode int returnCode;
64
65    /** {@hide} */
66    public ValidationProbeEvent(
67            int netId, long durationMs, int probeType, @ReturnCode int returnCode) {
68        this.netId = netId;
69        this.durationMs = durationMs;
70        this.probeType = probeType;
71        this.returnCode = returnCode;
72    }
73
74    private ValidationProbeEvent(Parcel in) {
75        netId = in.readInt();
76        durationMs = in.readLong();
77        probeType = in.readInt();
78        returnCode = in.readInt();
79    }
80
81    @Override
82    public void writeToParcel(Parcel out, int flags) {
83        out.writeInt(netId);
84        out.writeLong(durationMs);
85        out.writeInt(probeType);
86        out.writeInt(returnCode);
87    }
88
89    @Override
90    public int describeContents() {
91        return 0;
92    }
93
94    public static final Parcelable.Creator<ValidationProbeEvent> CREATOR
95        = new Parcelable.Creator<ValidationProbeEvent>() {
96        public ValidationProbeEvent createFromParcel(Parcel in) {
97            return new ValidationProbeEvent(in);
98        }
99
100        public ValidationProbeEvent[] newArray(int size) {
101            return new ValidationProbeEvent[size];
102        }
103    };
104
105    /** @hide */
106    public static int makeProbeType(int probeType, boolean firstValidation) {
107        return (probeType & 0xff) | (firstValidation ? FIRST_VALIDATION : REVALIDATION);
108    }
109
110    /** @hide */
111    public static String getProbeName(int probeType) {
112        return Decoder.constants.get(probeType & 0xff, "PROBE_???");
113    }
114
115    /** @hide */
116    public static String getValidationStage(int probeType) {
117        return Decoder.constants.get(probeType & 0xff00, "UNKNOWN");
118    }
119
120    public static void logEvent(int netId, long durationMs, int probeType, int returnCode) {
121    }
122
123    @Override
124    public String toString() {
125        return String.format("ValidationProbeEvent(%d, %s:%d %s, %dms)", netId,
126                getProbeName(probeType), returnCode, getValidationStage(probeType), durationMs);
127    }
128
129    final static class Decoder {
130        static final SparseArray<String> constants = MessageUtils.findMessageNames(
131                new Class[]{ValidationProbeEvent.class},
132                new String[]{"PROBE_", "FIRST_", "REVALIDATION"});
133    }
134}
135