blob: eb61c1532d497dbfe1edf52dae573fb90f873648 [file] [log] [blame]
Pierre Imai55618be2016-02-19 15:25:54 +09001/*
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
Hugo Benichi1193a9c2017-10-19 14:58:15 +090019import static android.net.ConnectivityManager.NETID_UNSET;
20
Hugo Benichi5df9d722016-04-25 17:16:35 +090021import android.net.NetworkCapabilities;
Pierre Imai55618be2016-02-19 15:25:54 +090022
Pierre Imaibc9cc502016-03-29 14:50:51 +090023/**
Hugo Benichicf6b12f2016-07-04 11:28:05 +090024 * An event recorded by ConnectivityService when there is a change in the default network.
Pierre Imaibc9cc502016-03-29 14:50:51 +090025 * {@hide}
26 */
Hugo Benichi1193a9c2017-10-19 14:58:15 +090027public class DefaultNetworkEvent {
28
Pierre Imai6b4e15b2016-04-07 12:21:05 +090029 // The ID of the network that has become the new default or NETID_UNSET if none.
Hugo Benichi1193a9c2017-10-19 14:58:15 +090030 public int netId = NETID_UNSET;
Pierre Imai6b4e15b2016-04-07 12:21:05 +090031 // The list of transport types of the new default network, for example TRANSPORT_WIFI, as
32 // defined in NetworkCapabilities.java.
Hugo Benichi1193a9c2017-10-19 14:58:15 +090033 public int[] transportTypes = new int[0];
Erik Kline48f12f22016-04-14 17:30:59 +090034 // The ID of the network that was the default before or NETID_UNSET if none.
Hugo Benichi1193a9c2017-10-19 14:58:15 +090035 public int prevNetId = NETID_UNSET;
Erik Kline48f12f22016-04-14 17:30:59 +090036 // Whether the previous network had IPv4/IPv6 connectivity.
Hugo Benichi1193a9c2017-10-19 14:58:15 +090037 public boolean prevIPv4;
38 public boolean prevIPv6;
Pierre Imai55618be2016-02-19 15:25:54 +090039
Hugo Benichi5df9d722016-04-25 17:16:35 +090040 @Override
41 public String toString() {
Hugo Benichi1193a9c2017-10-19 14:58:15 +090042 String prevNetwork = String.valueOf(prevNetId);
43 String newNetwork = String.valueOf(netId);
44 if (prevNetId != 0) {
45 prevNetwork += ":" + ipSupport();
46 }
47 if (netId != 0) {
48 newNetwork += ":" + NetworkCapabilities.transportNamesOf(transportTypes);
49 }
50 return String.format("DefaultNetworkEvent(%s -> %s)", prevNetwork, newNetwork);
Hugo Benichi1654b1d2016-05-24 11:50:31 +090051 }
52
53 private String ipSupport() {
54 if (prevIPv4 && prevIPv6) {
Hugo Benichi1193a9c2017-10-19 14:58:15 +090055 return "IPv4v6";
Hugo Benichi1654b1d2016-05-24 11:50:31 +090056 }
57 if (prevIPv6) {
58 return "IPv6";
59 }
60 if (prevIPv4) {
61 return "IPv4";
62 }
63 return "NONE";
Hugo Benichi5df9d722016-04-25 17:16:35 +090064 }
Hugo Benichicfddd682016-05-31 16:28:06 +090065}