blob: 052758da7aba040481d8aeccd651afb867cc1e18 [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;
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090020import android.annotation.SystemApi;
21import android.annotation.TestApi;
Hugo Benichicc92c6e2016-04-21 15:02:38 +090022import android.os.Parcel;
23import android.os.Parcelable;
Hugo Benichi5df9d722016-04-25 17:16:35 +090024import android.util.SparseArray;
25
26import com.android.internal.util.MessageUtils;
Hugo Benichicc92c6e2016-04-21 15:02:38 +090027
Hugo Benichicf6b12f2016-07-04 11:28:05 +090028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30
Hugo Benichicc92c6e2016-04-21 15:02:38 +090031/**
Hugo Benichicf6b12f2016-07-04 11:28:05 +090032 * An event recorded by NetworkMonitor when sending a probe for finding captive portals.
Hugo Benichicc92c6e2016-04-21 15:02:38 +090033 * {@hide}
34 */
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090035@SystemApi
36@TestApi
37public final class ValidationProbeEvent implements IpConnectivityLog.Event {
Hugo Benichicc92c6e2016-04-21 15:02:38 +090038
Hugo Benichid953bf82016-09-27 09:22:35 +090039 public static final int PROBE_DNS = 0;
40 public static final int PROBE_HTTP = 1;
41 public static final int PROBE_HTTPS = 2;
42 public static final int PROBE_PAC = 3;
Hugo Benichid953bf82016-09-27 09:22:35 +090043 public static final int PROBE_FALLBACK = 4;
Remi NGUYEN VANdcb722f2018-07-20 18:13:24 +090044 public static final int PROBE_PRIVDNS = 5;
Lorenzo Colittic5be12e2016-04-19 21:57:31 +090045
46 public static final int DNS_FAILURE = 0;
47 public static final int DNS_SUCCESS = 1;
Hugo Benichicc92c6e2016-04-21 15:02:38 +090048
Hugo Benichi147aa6d2016-11-15 23:23:24 +090049 private static final int FIRST_VALIDATION = 1 << 8;
50 private static final int REVALIDATION = 2 << 8;
Hugo Benichicf6b12f2016-07-04 11:28:05 +090051
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090052 /** @hide */
Hugo Benichicf6b12f2016-07-04 11:28:05 +090053 @IntDef(value = {DNS_FAILURE, DNS_SUCCESS})
54 @Retention(RetentionPolicy.SOURCE)
55 public @interface ReturnCode {}
56
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090057 /** @hide */
58 public final long durationMs;
Hugo Benichi147aa6d2016-11-15 23:23:24 +090059 // probeType byte format (MSB to LSB):
60 // byte 0: unused
61 // byte 1: unused
62 // byte 2: 0 = UNKNOWN, 1 = FIRST_VALIDATION, 2 = REVALIDATION
63 // byte 3: PROBE_* constant
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090064 /** @hide */
65 public final int probeType;
66 /** @hide */
67 public final @ReturnCode int returnCode;
Hugo Benichicc92c6e2016-04-21 15:02:38 +090068
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090069 private ValidationProbeEvent(long durationMs, int probeType, int returnCode) {
70 this.durationMs = durationMs;
71 this.probeType = probeType;
72 this.returnCode = returnCode;
Hugo Benichicc92c6e2016-04-21 15:02:38 +090073 }
74
75 private ValidationProbeEvent(Parcel in) {
Hugo Benichicc92c6e2016-04-21 15:02:38 +090076 durationMs = in.readLong();
77 probeType = in.readInt();
78 returnCode = in.readInt();
79 }
80
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090081 /**
Chiachang Wang95489ca2019-02-26 11:32:18 +080082 * Utility to create an instance of {@link ValidationProbeEvent}.
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +090083 */
84 public static class Builder {
85 private long mDurationMs;
86 private int mProbeType;
87 private int mReturnCode;
88
89 /**
90 * Set the duration of the probe in milliseconds.
91 */
92 public Builder setDurationMs(long durationMs) {
93 mDurationMs = durationMs;
94 return this;
95 }
96
97 /**
98 * Set the probe type based on whether it was the first validation.
99 */
100 public Builder setProbeType(int probeType, boolean firstValidation) {
101 mProbeType = makeProbeType(probeType, firstValidation);
102 return this;
103 }
104
105 /**
106 * Set the return code of the probe.
107 */
108 public Builder setReturnCode(int returnCode) {
109 mReturnCode = returnCode;
110 return this;
111 }
112
113 /**
114 * Create a new {@link ValidationProbeEvent}.
115 */
116 public ValidationProbeEvent build() {
117 return new ValidationProbeEvent(mDurationMs, mProbeType, mReturnCode);
118 }
119 }
120
121 /** @hide */
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900122 @Override
Hugo Benichicc92c6e2016-04-21 15:02:38 +0900123 public void writeToParcel(Parcel out, int flags) {
Hugo Benichicc92c6e2016-04-21 15:02:38 +0900124 out.writeLong(durationMs);
125 out.writeInt(probeType);
126 out.writeInt(returnCode);
127 }
128
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +0900129 /** @hide */
Hugo Benichicf6b12f2016-07-04 11:28:05 +0900130 @Override
Hugo Benichicc92c6e2016-04-21 15:02:38 +0900131 public int describeContents() {
132 return 0;
133 }
134
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +0900135 /** @hide */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700136 public static final @android.annotation.NonNull Parcelable.Creator<ValidationProbeEvent> CREATOR
Hugo Benichicc92c6e2016-04-21 15:02:38 +0900137 = new Parcelable.Creator<ValidationProbeEvent>() {
138 public ValidationProbeEvent createFromParcel(Parcel in) {
139 return new ValidationProbeEvent(in);
140 }
141
142 public ValidationProbeEvent[] newArray(int size) {
143 return new ValidationProbeEvent[size];
144 }
145 };
146
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +0900147 private static int makeProbeType(int probeType, boolean firstValidation) {
Hugo Benichi147aa6d2016-11-15 23:23:24 +0900148 return (probeType & 0xff) | (firstValidation ? FIRST_VALIDATION : REVALIDATION);
149 }
150
Lorenzo Colittic5be12e2016-04-19 21:57:31 +0900151 public static String getProbeName(int probeType) {
Hugo Benichi147aa6d2016-11-15 23:23:24 +0900152 return Decoder.constants.get(probeType & 0xff, "PROBE_???");
153 }
154
Remi NGUYEN VAN7b84fb32019-01-19 21:13:24 +0900155 private static String getValidationStage(int probeType) {
Hugo Benichi147aa6d2016-11-15 23:23:24 +0900156 return Decoder.constants.get(probeType & 0xff00, "UNKNOWN");
Lorenzo Colittic5be12e2016-04-19 21:57:31 +0900157 }
158
Hugo Benichi5df9d722016-04-25 17:16:35 +0900159 @Override
160 public String toString() {
Hugo Benichif927f0c2017-03-17 15:42:40 +0900161 return String.format("ValidationProbeEvent(%s:%d %s, %dms)",
Hugo Benichi147aa6d2016-11-15 23:23:24 +0900162 getProbeName(probeType), returnCode, getValidationStage(probeType), durationMs);
Hugo Benichi5df9d722016-04-25 17:16:35 +0900163 }
164
165 final static class Decoder {
166 static final SparseArray<String> constants = MessageUtils.findMessageNames(
Hugo Benichi147aa6d2016-11-15 23:23:24 +0900167 new Class[]{ValidationProbeEvent.class},
168 new String[]{"PROBE_", "FIRST_", "REVALIDATION"});
Hugo Benichicc92c6e2016-04-21 15:02:38 +0900169 }
Hugo Benichicfddd682016-05-31 16:28:06 +0900170}