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 * An event recorded by ConnectivityService when there is a change in the default network.
26 * {@hide}
27 */
28@SystemApi
29public 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;
40
41    /** {@hide} */
42    public DefaultNetworkEvent(int netId, int[] transportTypes,
43                int prevNetId, boolean prevIPv4, boolean prevIPv6) {
44        this.netId = netId;
45        this.transportTypes = transportTypes;
46        this.prevNetId = prevNetId;
47        this.prevIPv4 = prevIPv4;
48        this.prevIPv6 = prevIPv6;
49    }
50
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);
57    }
58
59    @Override
60    public void writeToParcel(Parcel out, int flags) {
61        out.writeInt(netId);
62        out.writeIntArray(transportTypes);
63        out.writeInt(prevNetId);
64        out.writeByte(prevIPv4 ? (byte) 1 : (byte) 0);
65        out.writeByte(prevIPv6 ? (byte) 1 : (byte) 0);
66    }
67
68    @Override
69    public int describeContents() {
70        return 0;
71    }
72
73    @Override
74    public String toString() {
75      String prevNetwork = String.valueOf(prevNetId);
76      String newNetwork = String.valueOf(netId);
77      if (prevNetId != 0) {
78          prevNetwork += ":" + ipSupport();
79      }
80      if (netId != 0) {
81          newNetwork += ":" + NetworkCapabilities.transportNamesOf(transportTypes);
82      }
83      return String.format("DefaultNetworkEvent(%s -> %s)", prevNetwork, newNetwork);
84    }
85
86    private String ipSupport() {
87        if (prevIPv4 && prevIPv6) {
88            return "DUAL";
89        }
90        if (prevIPv6) {
91            return "IPv6";
92        }
93        if (prevIPv4) {
94            return "IPv4";
95        }
96        return "NONE";
97    }
98
99    public static final Parcelable.Creator<DefaultNetworkEvent> CREATOR
100        = new Parcelable.Creator<DefaultNetworkEvent>() {
101        public DefaultNetworkEvent createFromParcel(Parcel in) {
102            return new DefaultNetworkEvent(in);
103        }
104
105        public DefaultNetworkEvent[] newArray(int size) {
106            return new DefaultNetworkEvent[size];
107        }
108    };
109
110    public static void logEvent(
111            int netId, int[] transports, int prevNetId, boolean hadIPv4, boolean hadIPv6) {
112    }
113}
114