2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.net.metrics;
19 import android.annotation.SystemApi;
20 import android.net.NetworkCapabilities;
21 import android.os.Parcel;
22 import android.os.Parcelable;
25 * An event recorded by ConnectivityService when there is a change in the default network.
29 public final class DefaultNetworkEvent implements Parcelable {
30 // The ID of the network that has become the new default or NETID_UNSET if none.
31 public final int netId;
32 // The list of transport types of the new default network, for example TRANSPORT_WIFI, as
33 // defined in NetworkCapabilities.java.
34 public final int[] transportTypes;
35 // The ID of the network that was the default before or NETID_UNSET if none.
36 public final int prevNetId;
37 // Whether the previous network had IPv4/IPv6 connectivity.
38 public final boolean prevIPv4;
39 public final boolean prevIPv6;
42 public DefaultNetworkEvent(int netId, int[] transportTypes,
43 int prevNetId, boolean prevIPv4, boolean prevIPv6) {
45 this.transportTypes = transportTypes;
46 this.prevNetId = prevNetId;
47 this.prevIPv4 = prevIPv4;
48 this.prevIPv6 = prevIPv6;
51 private DefaultNetworkEvent(Parcel in) {
52 this.netId = in.readInt();
53 this.transportTypes = in.createIntArray();
54 this.prevNetId = in.readInt();
55 this.prevIPv4 = (in.readByte() > 0);
56 this.prevIPv6 = (in.readByte() > 0);
60 public void writeToParcel(Parcel out, int flags) {
62 out.writeIntArray(transportTypes);
63 out.writeInt(prevNetId);
64 out.writeByte(prevIPv4 ? (byte) 1 : (byte) 0);
65 out.writeByte(prevIPv6 ? (byte) 1 : (byte) 0);
69 public int describeContents() {
74 public String toString() {
75 String prevNetwork = String.valueOf(prevNetId);
76 String newNetwork = String.valueOf(netId);
78 prevNetwork += ":" + ipSupport();
81 newNetwork += ":" + NetworkCapabilities.transportNamesOf(transportTypes);
83 return String.format("DefaultNetworkEvent(%s -> %s)", prevNetwork, newNetwork);
86 private String ipSupport() {
87 if (prevIPv4 && prevIPv6) {
99 public static final Parcelable.Creator<DefaultNetworkEvent> CREATOR
100 = new Parcelable.Creator<DefaultNetworkEvent>() {
101 public DefaultNetworkEvent createFromParcel(Parcel in) {
102 return new DefaultNetworkEvent(in);
105 public DefaultNetworkEvent[] newArray(int size) {
106 return new DefaultNetworkEvent[size];
110 public static void logEvent(
111 int netId, int[] transports, int prevNetId, boolean hadIPv4, boolean hadIPv6) {