DefaultNetworkEvent.java revision cfddd6879283860bb4d2cf2972ea086f585a37ec
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.SystemApi; 20import android.net.NetworkCapabilities; 21import android.os.Parcel; 22import android.os.Parcelable; 23 24/** 25 * {@hide} 26 */ 27@SystemApi 28public final class DefaultNetworkEvent implements Parcelable { 29 // The ID of the network that has become the new default or NETID_UNSET if none. 30 public final int netId; 31 // The list of transport types of the new default network, for example TRANSPORT_WIFI, as 32 // defined in NetworkCapabilities.java. 33 public final int[] transportTypes; 34 // The ID of the network that was the default before or NETID_UNSET if none. 35 public final int prevNetId; 36 // Whether the previous network had IPv4/IPv6 connectivity. 37 public final boolean prevIPv4; 38 public final boolean prevIPv6; 39 40 /** {@hide} */ 41 public DefaultNetworkEvent(int netId, int[] transportTypes, 42 int prevNetId, boolean prevIPv4, boolean prevIPv6) { 43 this.netId = netId; 44 this.transportTypes = transportTypes; 45 this.prevNetId = prevNetId; 46 this.prevIPv4 = prevIPv4; 47 this.prevIPv6 = prevIPv6; 48 } 49 50 private DefaultNetworkEvent(Parcel in) { 51 this.netId = in.readInt(); 52 this.transportTypes = in.createIntArray(); 53 this.prevNetId = in.readInt(); 54 this.prevIPv4 = (in.readByte() > 0); 55 this.prevIPv6 = (in.readByte() > 0); 56 } 57 58 public void writeToParcel(Parcel out, int flags) { 59 out.writeInt(netId); 60 out.writeIntArray(transportTypes); 61 out.writeInt(prevNetId); 62 out.writeByte(prevIPv4 ? (byte) 1 : (byte) 0); 63 out.writeByte(prevIPv6 ? (byte) 1 : (byte) 0); 64 } 65 66 public int describeContents() { 67 return 0; 68 } 69 70 @Override 71 public String toString() { 72 String prevNetwork = String.valueOf(prevNetId); 73 String newNetwork = String.valueOf(netId); 74 if (prevNetId != 0) { 75 prevNetwork += ":" + ipSupport(); 76 } 77 if (netId != 0) { 78 newNetwork += ":" + NetworkCapabilities.transportNamesOf(transportTypes); 79 } 80 return String.format("DefaultNetworkEvent(%s -> %s)", prevNetwork, newNetwork); 81 } 82 83 private String ipSupport() { 84 if (prevIPv4 && prevIPv6) { 85 return "DUAL"; 86 } 87 if (prevIPv6) { 88 return "IPv6"; 89 } 90 if (prevIPv4) { 91 return "IPv4"; 92 } 93 return "NONE"; 94 } 95 96 public static final Parcelable.Creator<DefaultNetworkEvent> CREATOR 97 = new Parcelable.Creator<DefaultNetworkEvent>() { 98 public DefaultNetworkEvent createFromParcel(Parcel in) { 99 return new DefaultNetworkEvent(in); 100 } 101 102 public DefaultNetworkEvent[] newArray(int size) { 103 return new DefaultNetworkEvent[size]; 104 } 105 }; 106 107 public static void logEvent( 108 int netId, int[] transports, int prevNetId, boolean hadIPv4, boolean hadIPv6) { 109 } 110} 111