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 19 import java.util.BitSet; 20 import java.util.StringJoiner; 21 22 /** 23 * An event recorded by ConnectivityService when there is a change in the default network. 24 * {@hide} 25 */ 26 public class DefaultNetworkEvent { 27 28 // The creation time in milliseconds of this DefaultNetworkEvent. 29 public final long creationTimeMs; 30 // The network ID of the network or 0 if none. 31 public int netId = 0; 32 // The list of transport types, as defined in NetworkCapabilities.java. 33 public int transports; 34 // The list of transport types of the last previous default network. 35 public int previousTransports; 36 // Whether the network has IPv4/IPv6 connectivity. 37 public boolean ipv4; 38 public boolean ipv6; 39 // The initial network score when this network became the default network. 40 public int initialScore; 41 // The initial network score when this network stopped being the default network. 42 public int finalScore; 43 // The total duration in milliseconds this network was the default network. 44 public long durationMs; 45 // The total duration in milliseconds this network was the default network and was validated. 46 public long validatedMs; 47 DefaultNetworkEvent(long timeMs)48 public DefaultNetworkEvent(long timeMs) { 49 creationTimeMs = timeMs; 50 } 51 52 /** Update the durationMs of this DefaultNetworkEvent for the given current time. */ updateDuration(long timeMs)53 public void updateDuration(long timeMs) { 54 durationMs = timeMs - creationTimeMs; 55 } 56 57 @Override toString()58 public String toString() { 59 StringJoiner j = new StringJoiner(", ", "DefaultNetworkEvent(", ")"); 60 j.add("netId=" + netId); 61 j.add("transports=" + BitSet.valueOf(new long[] { transports })); 62 j.add("ip=" + ipSupport()); 63 if (initialScore > 0) { 64 j.add("initial_score=" + initialScore); 65 } 66 if (finalScore > 0) { 67 j.add("final_score=" + finalScore); 68 } 69 j.add(String.format("duration=%.0fs", durationMs / 1000.0)); 70 j.add(String.format("validation=%04.1f%%", (validatedMs * 100.0) / durationMs)); 71 return j.toString(); 72 } 73 ipSupport()74 private String ipSupport() { 75 if (ipv4 && ipv6) { 76 return "IPv4v6"; 77 } 78 if (ipv6) { 79 return "IPv6"; 80 } 81 if (ipv4) { 82 return "IPv4"; 83 } 84 return "NONE"; 85 } 86 } 87