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 17package android.app.admin; 18 19import android.os.Parcel; 20import android.os.Parcelable; 21 22import java.net.InetAddress; 23import java.net.UnknownHostException; 24 25/** 26 * A class that represents a connect library call event. 27 */ 28public final class ConnectEvent extends NetworkEvent implements Parcelable { 29 30 /** The destination IP address. */ 31 private final String ipAddress; 32 33 /** The destination port number. */ 34 private final int port; 35 36 /** @hide */ 37 public ConnectEvent(String ipAddress, int port, String packageName, long timestamp) { 38 super(packageName, timestamp); 39 this.ipAddress = ipAddress; 40 this.port = port; 41 } 42 43 private ConnectEvent(Parcel in) { 44 this.ipAddress = in.readString(); 45 this.port = in.readInt(); 46 this.packageName = in.readString(); 47 this.timestamp = in.readLong(); 48 } 49 50 public InetAddress getInetAddress() { 51 try { 52 // ipAddress is already an address, not a host name, no DNS resolution will happen. 53 return InetAddress.getByName(ipAddress); 54 } catch (UnknownHostException e) { 55 // Should never happen as we aren't passing a host name. 56 return InetAddress.getLoopbackAddress(); 57 } 58 } 59 60 public int getPort() { 61 return port; 62 } 63 64 @Override 65 public String toString() { 66 return String.format("ConnectEvent(%s, %d, %d, %s)", ipAddress, port, timestamp, 67 packageName); 68 } 69 70 public static final Parcelable.Creator<ConnectEvent> CREATOR 71 = new Parcelable.Creator<ConnectEvent>() { 72 @Override 73 public ConnectEvent createFromParcel(Parcel in) { 74 if (in.readInt() != PARCEL_TOKEN_CONNECT_EVENT) { 75 return null; 76 } 77 return new ConnectEvent(in); 78 } 79 80 @Override 81 public ConnectEvent[] newArray(int size) { 82 return new ConnectEvent[size]; 83 } 84 }; 85 86 @Override 87 public int describeContents() { 88 return 0; 89 } 90 91 @Override 92 public void writeToParcel(Parcel out, int flags) { 93 // write parcel token first 94 out.writeInt(PARCEL_TOKEN_CONNECT_EVENT); 95 out.writeString(ipAddress); 96 out.writeInt(port); 97 out.writeString(packageName); 98 out.writeLong(timestamp); 99 } 100} 101 102