import android.os.Parcel;
import android.os.Parcelable;
-/** {@hide} */
+/**
+ * Represents a core networking event defined in package android.net.metrics.
+ * Logged by IpConnectivityLog and managed by ConnectivityMetrics service.
+ * {@hide}
+ * */
public final class ConnectivityMetricsEvent implements Parcelable {
- /** The time when this event was collected, as returned by System.currentTimeMillis(). */
+ /** Time when this event was collected, as returned by System.currentTimeMillis(). */
public long timestamp;
-
+ /** Transports of the network associated with the event, as defined in NetworkCapabilities. */
+ public long transports;
+ /** Network id of the network associated with the event, or 0 if unspecified. */
+ public int netId;
+ /** Name of the network interface associated with the event, or null if unspecified. */
+ public String ifname;
/** Opaque event-specific data. */
public Parcelable data;
public ConnectivityMetricsEvent() {
}
- public ConnectivityMetricsEvent(Parcel in) {
+ private ConnectivityMetricsEvent(Parcel in) {
timestamp = in.readLong();
+ transports = in.readLong();
+ netId = in.readInt();
+ ifname = in.readString();
data = in.readParcelable(null);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(timestamp);
+ dest.writeLong(transports);
+ dest.writeInt(netId);
+ dest.writeString(ifname);
dest.writeParcelable(data, 0);
}
@Override
public String toString() {
+ // TODO: add transports, netId, ifname
return String.format("ConnectivityMetricsEvent(%tT.%tL): %s", timestamp, timestamp, data);
}
}
}
/**
- * Log an IpConnectivity event.
- * @param timestamp is the epoch timestamp of the event in ms.
- * @param data is a Parcelable instance representing the event.
+ * Log a ConnectivityMetricsEvent.
+ * @param ev the event to log. If the event timestamp is 0,
+ * the timestamp is set to the current time in milliseconds.
* @return true if the event was successfully logged.
*/
- public boolean log(long timestamp, Parcelable data) {
+ public boolean log(ConnectivityMetricsEvent ev) {
if (!checkLoggerService()) {
if (DBG) {
Log.d(TAG, SERVICE_NAME + " service was not ready");
}
return false;
}
-
+ if (ev.timestamp == 0) {
+ ev.timestamp = System.currentTimeMillis();
+ }
try {
- ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
- ev.timestamp = timestamp;
- ev.data = data;
int left = mService.logEvent(ev);
return left >= 0;
} catch (RemoteException e) {
}
}
- public void log(Parcelable event) {
- log(System.currentTimeMillis(), event);
+ /**
+ * Log an IpConnectivity event.
+ * @param timestamp is the epoch timestamp of the event in ms.
+ * If the timestamp is 0, the timestamp is set to the current time in milliseconds.
+ * @param data is a Parcelable instance representing the event.
+ * @return true if the event was successfully logged.
+ */
+ public boolean log(long timestamp, Parcelable data) {
+ ConnectivityMetricsEvent ev = makeEv(data);
+ ev.timestamp = timestamp;
+ return log(ev);
+ }
+
+ /**
+ * Log an IpConnectivity event.
+ * @param data is a Parcelable instance representing the event.
+ * @return true if the event was successfully logged.
+ */
+ public boolean log(Parcelable data) {
+ return log(makeEv(data));
+ }
+
+ private static ConnectivityMetricsEvent makeEv(Parcelable data) {
+ ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
+ ev.data = data;
+ return ev;
}
}
new Thread() {
public void run() {
for (int j = 0; j < nEvents; j++) {
- assertTrue(logger.log(i * 100 + j, FAKE_EV));
+ assertTrue(logger.log(1 + i * 100 + j, FAKE_EV));
}
}
}.start();
Iterator<ConnectivityMetricsEvent> iter = got.iterator();
for (int i = 0; i < nCallers; i++) {
for (int j = 0; j < nEvents; j++) {
- int expectedTimestamp = i * 100 + j;
+ int expectedTimestamp = 1 + i * 100 + j;
assertEventsEqual(expectedEvent(expectedTimestamp), iter.next());
}
}
import android.net.metrics.DnsEvent;
import android.net.metrics.INetdEventListener;
import android.net.metrics.IpConnectivityLog;
+import android.os.Parcelable;
import android.os.RemoteException;
import android.system.OsConstants;
import android.test.suitebuilder.annotation.SmallTest;
// call onLost() asynchronously to logDnsAsync's onDnsEvent() calls.
mCallbackCaptor.getValue().onLost(new Network(105));
- // do not verify unpredictable batch
- verify(mLog, timeout(500).times(1)).log(any());
+ // do not verify batch with unpredictable length
+ verify(mLog, timeout(500).times(1)).log(any(Parcelable.class));
}
@SmallTest
}
void logDnsAsync(int netId, int[] latencies) {
- new Thread() {
- public void run() {
- log(netId, latencies);
- }
- }.start();
+ new Thread(() -> log(netId, latencies)).start();
}
void verifyLoggedDnsEvents(DnsEvent... expected) {