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