Home | History | Annotate | Download | only in metrics
      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 android.net.NetworkCapabilities;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 
     23 /**
     24  * An event recorded by ConnectivityService when there is a change in the default network.
     25  * {@hide}
     26  */
     27 public final class DefaultNetworkEvent implements Parcelable {
     28     // The ID of the network that has become the new default or NETID_UNSET if none.
     29     public final int netId;
     30     // The list of transport types of the new default network, for example TRANSPORT_WIFI, as
     31     // defined in NetworkCapabilities.java.
     32     public final int[] transportTypes;
     33     // The ID of the network that was the default before or NETID_UNSET if none.
     34     public final int prevNetId;
     35     // Whether the previous network had IPv4/IPv6 connectivity.
     36     public final boolean prevIPv4;
     37     public final boolean prevIPv6;
     38 
     39     public DefaultNetworkEvent(int netId, int[] transportTypes,
     40                 int prevNetId, boolean prevIPv4, boolean prevIPv6) {
     41         this.netId = netId;
     42         this.transportTypes = transportTypes;
     43         this.prevNetId = prevNetId;
     44         this.prevIPv4 = prevIPv4;
     45         this.prevIPv6 = prevIPv6;
     46     }
     47 
     48     private DefaultNetworkEvent(Parcel in) {
     49         this.netId = in.readInt();
     50         this.transportTypes = in.createIntArray();
     51         this.prevNetId = in.readInt();
     52         this.prevIPv4 = (in.readByte() > 0);
     53         this.prevIPv6 = (in.readByte() > 0);
     54     }
     55 
     56     @Override
     57     public void writeToParcel(Parcel out, int flags) {
     58         out.writeInt(netId);
     59         out.writeIntArray(transportTypes);
     60         out.writeInt(prevNetId);
     61         out.writeByte(prevIPv4 ? (byte) 1 : (byte) 0);
     62         out.writeByte(prevIPv6 ? (byte) 1 : (byte) 0);
     63     }
     64 
     65     @Override
     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