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.os.Parcel;
21import android.os.Parcelable;
22import android.util.SparseArray;
23
24import com.android.internal.util.MessageUtils;
25
26import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
28
29/**
30 * An event recorded by NetworkMonitor when sending a probe for finding captive portals.
31 * {@hide}
32 */
33public final class ValidationProbeEvent implements Parcelable {
34
35    public static final int PROBE_DNS       = 0;
36    public static final int PROBE_HTTP      = 1;
37    public static final int PROBE_HTTPS     = 2;
38    public static final int PROBE_PAC       = 3;
39    public static final int PROBE_FALLBACK  = 4;
40
41    public static final int DNS_FAILURE = 0;
42    public static final int DNS_SUCCESS = 1;
43
44    private static final int FIRST_VALIDATION  = 1 << 8;
45    private static final int REVALIDATION      = 2 << 8;
46
47    @IntDef(value = {DNS_FAILURE, DNS_SUCCESS})
48    @Retention(RetentionPolicy.SOURCE)
49    public @interface ReturnCode {}
50
51    public long durationMs;
52    // probeType byte format (MSB to LSB):
53    // byte 0: unused
54    // byte 1: unused
55    // byte 2: 0 = UNKNOWN, 1 = FIRST_VALIDATION, 2 = REVALIDATION
56    // byte 3: PROBE_* constant
57    public int probeType;
58    public @ReturnCode int returnCode;
59
60    public ValidationProbeEvent() {
61    }
62
63    private ValidationProbeEvent(Parcel in) {
64        durationMs = in.readLong();
65        probeType = in.readInt();
66        returnCode = in.readInt();
67    }
68
69    @Override
70    public void writeToParcel(Parcel out, int flags) {
71        out.writeLong(durationMs);
72        out.writeInt(probeType);
73        out.writeInt(returnCode);
74    }
75
76    @Override
77    public int describeContents() {
78        return 0;
79    }
80
81    public static final Parcelable.Creator<ValidationProbeEvent> CREATOR
82        = new Parcelable.Creator<ValidationProbeEvent>() {
83        public ValidationProbeEvent createFromParcel(Parcel in) {
84            return new ValidationProbeEvent(in);
85        }
86
87        public ValidationProbeEvent[] newArray(int size) {
88            return new ValidationProbeEvent[size];
89        }
90    };
91
92    public static int makeProbeType(int probeType, boolean firstValidation) {
93        return (probeType & 0xff) | (firstValidation ? FIRST_VALIDATION : REVALIDATION);
94    }
95
96    public static String getProbeName(int probeType) {
97        return Decoder.constants.get(probeType & 0xff, "PROBE_???");
98    }
99
100    public static String getValidationStage(int probeType) {
101        return Decoder.constants.get(probeType & 0xff00, "UNKNOWN");
102    }
103
104    @Override
105    public String toString() {
106        return String.format("ValidationProbeEvent(%s:%d %s, %dms)",
107                getProbeName(probeType), returnCode, getValidationStage(probeType), durationMs);
108    }
109
110    final static class Decoder {
111        static final SparseArray<String> constants = MessageUtils.findMessageNames(
112                new Class[]{ValidationProbeEvent.class},
113                new String[]{"PROBE_", "FIRST_", "REVALIDATION"});
114    }
115}
116