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; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import com.android.internal.util.BitUtils; 22 23 /** 24 * Represents a core networking event defined in package android.net.metrics. 25 * Logged by IpConnectivityLog and managed by ConnectivityMetrics service. 26 * {@hide} 27 * */ 28 public final class ConnectivityMetricsEvent implements Parcelable { 29 30 /** Time when this event was collected, as returned by System.currentTimeMillis(). */ 31 public long timestamp; 32 /** Transports of the network associated with the event, as defined in NetworkCapabilities. */ 33 public long transports; 34 /** Network id of the network associated with the event, or 0 if unspecified. */ 35 public int netId; 36 /** Name of the network interface associated with the event, or null if unspecified. */ 37 public String ifname; 38 /** Opaque event-specific data. */ 39 public Parcelable data; 40 ConnectivityMetricsEvent()41 public ConnectivityMetricsEvent() { 42 } 43 ConnectivityMetricsEvent(Parcel in)44 private ConnectivityMetricsEvent(Parcel in) { 45 timestamp = in.readLong(); 46 transports = in.readLong(); 47 netId = in.readInt(); 48 ifname = in.readString(); 49 data = in.readParcelable(null); 50 } 51 52 /** Implement the Parcelable interface */ 53 public static final Parcelable.Creator<ConnectivityMetricsEvent> CREATOR 54 = new Parcelable.Creator<ConnectivityMetricsEvent> (){ 55 public ConnectivityMetricsEvent createFromParcel(Parcel source) { 56 return new ConnectivityMetricsEvent(source); 57 } 58 59 public ConnectivityMetricsEvent[] newArray(int size) { 60 return new ConnectivityMetricsEvent[size]; 61 } 62 }; 63 64 @Override describeContents()65 public int describeContents() { 66 return 0; 67 } 68 69 @Override writeToParcel(Parcel dest, int flags)70 public void writeToParcel(Parcel dest, int flags) { 71 dest.writeLong(timestamp); 72 dest.writeLong(transports); 73 dest.writeInt(netId); 74 dest.writeString(ifname); 75 dest.writeParcelable(data, 0); 76 } 77 78 @Override toString()79 public String toString() { 80 StringBuilder buffer = new StringBuilder("ConnectivityMetricsEvent("); 81 buffer.append(String.format("%tT.%tL", timestamp, timestamp)); 82 if (netId != 0) { 83 buffer.append(", ").append(netId); 84 } 85 if (ifname != null) { 86 buffer.append(", ").append(ifname); 87 } 88 for (int t : BitUtils.unpackBits(transports)) { 89 buffer.append(", ").append(NetworkCapabilities.transportNameOf(t)); 90 } 91 buffer.append("): ").append(data.toString()); 92 return buffer.toString(); 93 } 94 } 95