Merge "Add id to NetworkEvent."

This commit is contained in:
Naomi Musgrave
2017-11-28 13:30:19 +00:00
committed by Android (Google) Code Review
6 changed files with 78 additions and 52 deletions

View File

@@ -6588,6 +6588,7 @@ package android.app.admin {
public abstract class NetworkEvent implements android.os.Parcelable {
method public int describeContents();
method public long getId();
method public java.lang.String getPackageName();
method public long getTimestamp();
field public static final android.os.Parcelable.Creator<android.app.admin.NetworkEvent> CREATOR;

View File

@@ -32,29 +32,30 @@ import java.net.UnknownHostException;
public final class ConnectEvent extends NetworkEvent implements Parcelable {
/** The destination IP address. */
private final String ipAddress;
private final String mIpAddress;
/** The destination port number. */
private final int port;
private final int mPort;
/** @hide */
public ConnectEvent(String ipAddress, int port, String packageName, long timestamp) {
super(packageName, timestamp);
this.ipAddress = ipAddress;
this.port = port;
this.mIpAddress = ipAddress;
this.mPort = port;
}
private ConnectEvent(Parcel in) {
this.ipAddress = in.readString();
this.port = in.readInt();
this.packageName = in.readString();
this.timestamp = in.readLong();
this.mIpAddress = in.readString();
this.mPort = in.readInt();
this.mPackageName = in.readString();
this.mTimestamp = in.readLong();
this.mId = in.readLong();
}
public InetAddress getInetAddress() {
try {
// ipAddress is already an address, not a host name, no DNS resolution will happen.
return InetAddress.getByName(ipAddress);
return InetAddress.getByName(mIpAddress);
} catch (UnknownHostException e) {
// Should never happen as we aren't passing a host name.
return InetAddress.getLoopbackAddress();
@@ -62,13 +63,13 @@ public final class ConnectEvent extends NetworkEvent implements Parcelable {
}
public int getPort() {
return port;
return mPort;
}
@Override
public String toString() {
return String.format("ConnectEvent(%s, %d, %d, %s)", ipAddress, port, timestamp,
packageName);
return String.format("ConnectEvent(%s, %d, %d, %s)", mIpAddress, mPort, mTimestamp,
mPackageName);
}
public static final Parcelable.Creator<ConnectEvent> CREATOR
@@ -96,10 +97,10 @@ public final class ConnectEvent extends NetworkEvent implements Parcelable {
public void writeToParcel(Parcel out, int flags) {
// write parcel token first
out.writeInt(PARCEL_TOKEN_CONNECT_EVENT);
out.writeString(ipAddress);
out.writeInt(port);
out.writeString(packageName);
out.writeLong(timestamp);
out.writeString(mIpAddress);
out.writeInt(mPort);
out.writeString(mPackageName);
out.writeLong(mTimestamp);
out.writeLong(mId);
}
}

View File

@@ -34,46 +34,47 @@ import java.util.List;
public final class DnsEvent extends NetworkEvent implements Parcelable {
/** The hostname that was looked up. */
private final String hostname;
private final String mHostname;
/** Contains (possibly a subset of) the IP addresses returned. */
private final String[] ipAddresses;
private final String[] mIpAddresses;
/**
* The number of IP addresses returned from the DNS lookup event. May be different from the
* length of ipAddresses if there were too many addresses to log.
*/
private final int ipAddressesCount;
private final int mIpAddressesCount;
/** @hide */
public DnsEvent(String hostname, String[] ipAddresses, int ipAddressesCount,
String packageName, long timestamp) {
super(packageName, timestamp);
this.hostname = hostname;
this.ipAddresses = ipAddresses;
this.ipAddressesCount = ipAddressesCount;
this.mHostname = hostname;
this.mIpAddresses = ipAddresses;
this.mIpAddressesCount = ipAddressesCount;
}
private DnsEvent(Parcel in) {
this.hostname = in.readString();
this.ipAddresses = in.createStringArray();
this.ipAddressesCount = in.readInt();
this.packageName = in.readString();
this.timestamp = in.readLong();
this.mHostname = in.readString();
this.mIpAddresses = in.createStringArray();
this.mIpAddressesCount = in.readInt();
this.mPackageName = in.readString();
this.mTimestamp = in.readLong();
this.mId = in.readLong();
}
/** Returns the hostname that was looked up. */
public String getHostname() {
return hostname;
return mHostname;
}
/** Returns (possibly a subset of) the IP addresses returned. */
public List<InetAddress> getInetAddresses() {
if (ipAddresses == null || ipAddresses.length == 0) {
if (mIpAddresses == null || mIpAddresses.length == 0) {
return Collections.emptyList();
}
final List<InetAddress> inetAddresses = new ArrayList<>(ipAddresses.length);
for (final String ipAddress : ipAddresses) {
final List<InetAddress> inetAddresses = new ArrayList<>(mIpAddresses.length);
for (final String ipAddress : mIpAddresses) {
try {
// ipAddress is already an address, not a host name, no DNS resolution will happen.
inetAddresses.add(InetAddress.getByName(ipAddress));
@@ -90,14 +91,14 @@ public final class DnsEvent extends NetworkEvent implements Parcelable {
* addresses to log.
*/
public int getTotalResolvedAddressCount() {
return ipAddressesCount;
return mIpAddressesCount;
}
@Override
public String toString() {
return String.format("DnsEvent(%s, %s, %d, %d, %s)", hostname,
(ipAddresses == null) ? "NONE" : String.join(" ", ipAddresses),
ipAddressesCount, timestamp, packageName);
return String.format("DnsEvent(%s, %s, %d, %d, %s)", mHostname,
(mIpAddresses == null) ? "NONE" : String.join(" ", mIpAddresses),
mIpAddressesCount, mTimestamp, mPackageName);
}
public static final Parcelable.Creator<DnsEvent> CREATOR
@@ -125,11 +126,11 @@ public final class DnsEvent extends NetworkEvent implements Parcelable {
public void writeToParcel(Parcel out, int flags) {
// write parcel token first
out.writeInt(PARCEL_TOKEN_DNS_EVENT);
out.writeString(hostname);
out.writeStringArray(ipAddresses);
out.writeInt(ipAddressesCount);
out.writeString(packageName);
out.writeLong(timestamp);
out.writeString(mHostname);
out.writeStringArray(mIpAddresses);
out.writeInt(mIpAddressesCount);
out.writeString(mPackageName);
out.writeLong(mTimestamp);
out.writeLong(mId);
}
}

View File

@@ -18,8 +18,8 @@ package android.app.admin;
import android.content.pm.PackageManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.ParcelFormatException;
import android.os.Parcelable;
/**
* An abstract class that represents a network event.
@@ -32,10 +32,13 @@ public abstract class NetworkEvent implements Parcelable {
static final int PARCEL_TOKEN_CONNECT_EVENT = 2;
/** The package name of the UID that performed the query. */
String packageName;
String mPackageName;
/** The timestamp of the event being reported in milliseconds. */
long timestamp;
long mTimestamp;
/** The id of the event. */
long mId;
/** @hide */
NetworkEvent() {
@@ -44,8 +47,8 @@ public abstract class NetworkEvent implements Parcelable {
/** @hide */
NetworkEvent(String packageName, long timestamp) {
this.packageName = packageName;
this.timestamp = timestamp;
this.mPackageName = packageName;
this.mTimestamp = timestamp;
}
/**
@@ -53,7 +56,7 @@ public abstract class NetworkEvent implements Parcelable {
* {@link PackageManager#getNameForUid}.
*/
public String getPackageName() {
return packageName;
return mPackageName;
}
/**
@@ -61,7 +64,20 @@ public abstract class NetworkEvent implements Parcelable {
* the time the event was reported and midnight, January 1, 1970 UTC.
*/
public long getTimestamp() {
return timestamp;
return mTimestamp;
}
/** @hide */
public void setId(long id) {
this.mId = id;
}
/**
* Returns the id of the event, where the id monotonically increases for each event. The id
* is reset when the device reboots, and when network logging is enabled.
*/
public long getId() {
return this.mId;
}
@Override
@@ -95,4 +111,3 @@ public abstract class NetworkEvent implements Parcelable {
@Override
public abstract void writeToParcel(Parcel out, int flags);
}

View File

@@ -64,6 +64,8 @@ final class NetworkLoggingHandler extends Handler {
private final DevicePolicyManagerService mDpm;
private final AlarmManager mAlarmManager;
private long mId;
private final OnAlarmListener mBatchTimeoutAlarmListener = new OnAlarmListener() {
@Override
public void onAlarm() {
@@ -185,6 +187,10 @@ final class NetworkLoggingHandler extends Handler {
private Bundle finalizeBatchAndBuildDeviceOwnerMessageLocked() {
Bundle notificationExtras = null;
if (mNetworkEvents.size() > 0) {
// Assign ids to the events.
for (NetworkEvent event : mNetworkEvents) {
event.setId(mId++);
}
// Finalize the batch and start a new one from scratch.
if (mBatches.size() >= MAX_BATCHES) {
// Remove the oldest batch if we hit the limit.

View File

@@ -20,8 +20,6 @@ import android.app.admin.DnsEvent;
import android.os.Parcel;
import android.test.suitebuilder.annotation.SmallTest;
import static junit.framework.Assert.assertEquals;
@SmallTest
public class NetworkEventTest extends DpmTestBase {
@@ -30,6 +28,7 @@ public class NetworkEventTest extends DpmTestBase {
*/
public void testConnectEventParceling() {
ConnectEvent event = new ConnectEvent("127.0.0.1", 80, "com.android.whateverdude", 100000);
event.setId(5L);
Parcel p = Parcel.obtain();
p.writeParcelable(event, 0);
p.setDataPosition(0);
@@ -39,6 +38,7 @@ public class NetworkEventTest extends DpmTestBase {
assertEquals(event.getPort(), unparceledEvent.getPort());
assertEquals(event.getPackageName(), unparceledEvent.getPackageName());
assertEquals(event.getTimestamp(), unparceledEvent.getTimestamp());
assertEquals(event.getId(), unparceledEvent.getId());
}
/**
@@ -47,6 +47,7 @@ public class NetworkEventTest extends DpmTestBase {
public void testDnsEventParceling() {
DnsEvent event = new DnsEvent("d.android.com", new String[]{"192.168.0.1", "127.0.0.1"}, 2,
"com.android.whateverdude", 100000);
event.setId(5L);
Parcel p = Parcel.obtain();
p.writeParcelable(event, 0);
p.setDataPosition(0);
@@ -59,5 +60,6 @@ public class NetworkEventTest extends DpmTestBase {
unparceledEvent.getTotalResolvedAddressCount());
assertEquals(event.getPackageName(), unparceledEvent.getPackageName());
assertEquals(event.getTimestamp(), unparceledEvent.getTimestamp());
assertEquals(event.getId(), unparceledEvent.getId());
}
}