ValidationProbeEvent.java revision 95cb226c1a4ff47531da65ef2617ade0dea5c9b8
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 final int netId; 52 public final long durationMs; 53 // probeType byte format (MSB to LSB): 54 // byte 0: unused 55 // byte 1: unused 56 // byte 2: 0 = UNKNOWN, 1 = FIRST_VALIDATION, 2 = REVALIDATION 57 // byte 3: PROBE_* constant 58 public final int probeType; 59 public final @ReturnCode int returnCode; 60 61 public ValidationProbeEvent( 62 int netId, long durationMs, 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 public static int makeProbeType(int probeType, boolean firstValidation) { 101 return (probeType & 0xff) | (firstValidation ? FIRST_VALIDATION : REVALIDATION); 102 } 103 104 public static String getProbeName(int probeType) { 105 return Decoder.constants.get(probeType & 0xff, "PROBE_???"); 106 } 107 108 public static String getValidationStage(int probeType) { 109 return Decoder.constants.get(probeType & 0xff00, "UNKNOWN"); 110 } 111 112 @Override 113 public String toString() { 114 return String.format("ValidationProbeEvent(%d, %s:%d %s, %dms)", netId, 115 getProbeName(probeType), returnCode, getValidationStage(probeType), durationMs); 116 } 117 118 final static class Decoder { 119 static final SparseArray<String> constants = MessageUtils.findMessageNames( 120 new Class[]{ValidationProbeEvent.class}, 121 new String[]{"PROBE_", "FIRST_", "REVALIDATION"}); 122 } 123} 124