Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 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 | |
| 17 | package android.net.metrics; |
| 18 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
| 21 | import android.annotation.TestApi; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 24 | import android.util.SparseArray; |
| 25 | |
| 26 | import com.android.internal.util.MessageUtils; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 27 | |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
| 30 | |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 31 | /** |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 32 | * An event recorded by NetworkMonitor when sending a probe for finding captive portals. |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 33 | * {@hide} |
| 34 | */ |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 35 | @SystemApi |
| 36 | @TestApi |
| 37 | public final class ValidationProbeEvent implements IpConnectivityLog.Event { |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 38 | |
Hugo Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 39 | 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 Benichi | d953bf8 | 2016-09-27 09:22:35 +0900 | [diff] [blame] | 43 | public static final int PROBE_FALLBACK = 4; |
Remi NGUYEN VAN | dcb722f | 2018-07-20 18:13:24 +0900 | [diff] [blame] | 44 | public static final int PROBE_PRIVDNS = 5; |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 45 | |
| 46 | public static final int DNS_FAILURE = 0; |
| 47 | public static final int DNS_SUCCESS = 1; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 48 | |
Hugo Benichi | 147aa6d | 2016-11-15 23:23:24 +0900 | [diff] [blame] | 49 | private static final int FIRST_VALIDATION = 1 << 8; |
| 50 | private static final int REVALIDATION = 2 << 8; |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 51 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 52 | /** @hide */ |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 53 | @IntDef(value = {DNS_FAILURE, DNS_SUCCESS}) |
| 54 | @Retention(RetentionPolicy.SOURCE) |
| 55 | public @interface ReturnCode {} |
| 56 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 57 | /** @hide */ |
| 58 | public final long durationMs; |
Hugo Benichi | 147aa6d | 2016-11-15 23:23:24 +0900 | [diff] [blame] | 59 | // 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 VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 64 | /** @hide */ |
| 65 | public final int probeType; |
| 66 | /** @hide */ |
| 67 | public final @ReturnCode int returnCode; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 68 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 69 | private ValidationProbeEvent(long durationMs, int probeType, int returnCode) { |
| 70 | this.durationMs = durationMs; |
| 71 | this.probeType = probeType; |
| 72 | this.returnCode = returnCode; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private ValidationProbeEvent(Parcel in) { |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 76 | durationMs = in.readLong(); |
| 77 | probeType = in.readInt(); |
| 78 | returnCode = in.readInt(); |
| 79 | } |
| 80 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 81 | /** |
Chiachang Wang | 95489ca | 2019-02-26 11:32:18 +0800 | [diff] [blame] | 82 | * Utility to create an instance of {@link ValidationProbeEvent}. |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 83 | */ |
| 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 Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 122 | @Override |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 123 | public void writeToParcel(Parcel out, int flags) { |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 124 | out.writeLong(durationMs); |
| 125 | out.writeInt(probeType); |
| 126 | out.writeInt(returnCode); |
| 127 | } |
| 128 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 129 | /** @hide */ |
Hugo Benichi | cf6b12f | 2016-07-04 11:28:05 +0900 | [diff] [blame] | 130 | @Override |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 131 | public int describeContents() { |
| 132 | return 0; |
| 133 | } |
| 134 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 135 | /** @hide */ |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 136 | public static final Parcelable.Creator<ValidationProbeEvent> CREATOR |
| 137 | = 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 VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 147 | private static int makeProbeType(int probeType, boolean firstValidation) { |
Hugo Benichi | 147aa6d | 2016-11-15 23:23:24 +0900 | [diff] [blame] | 148 | return (probeType & 0xff) | (firstValidation ? FIRST_VALIDATION : REVALIDATION); |
| 149 | } |
| 150 | |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 151 | public static String getProbeName(int probeType) { |
Hugo Benichi | 147aa6d | 2016-11-15 23:23:24 +0900 | [diff] [blame] | 152 | return Decoder.constants.get(probeType & 0xff, "PROBE_???"); |
| 153 | } |
| 154 | |
Remi NGUYEN VAN | 7b84fb3 | 2019-01-19 21:13:24 +0900 | [diff] [blame] | 155 | private static String getValidationStage(int probeType) { |
Hugo Benichi | 147aa6d | 2016-11-15 23:23:24 +0900 | [diff] [blame] | 156 | return Decoder.constants.get(probeType & 0xff00, "UNKNOWN"); |
Lorenzo Colitti | c5be12e | 2016-04-19 21:57:31 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 159 | @Override |
| 160 | public String toString() { |
Hugo Benichi | f927f0c | 2017-03-17 15:42:40 +0900 | [diff] [blame] | 161 | return String.format("ValidationProbeEvent(%s:%d %s, %dms)", |
Hugo Benichi | 147aa6d | 2016-11-15 23:23:24 +0900 | [diff] [blame] | 162 | getProbeName(probeType), returnCode, getValidationStage(probeType), durationMs); |
Hugo Benichi | 5df9d72 | 2016-04-25 17:16:35 +0900 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | final static class Decoder { |
| 166 | static final SparseArray<String> constants = MessageUtils.findMessageNames( |
Hugo Benichi | 147aa6d | 2016-11-15 23:23:24 +0900 | [diff] [blame] | 167 | new Class[]{ValidationProbeEvent.class}, |
| 168 | new String[]{"PROBE_", "FIRST_", "REVALIDATION"}); |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 169 | } |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 170 | } |