Michal Karpinski | 4c47ade | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 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.app.admin; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | |
| 22 | /** |
| 23 | * A class that represents a DNS lookup event. |
| 24 | * @hide |
| 25 | */ |
| 26 | public final class DnsEvent extends NetworkEvent implements Parcelable { |
| 27 | |
| 28 | /** The hostname that was looked up. */ |
| 29 | private final String hostname; |
| 30 | |
| 31 | /** Contains (possibly a subset of) the IP addresses returned. */ |
| 32 | private final String[] ipAddresses; |
| 33 | |
| 34 | /** |
| 35 | * The number of IP addresses returned from the DNS lookup event. May be different from the |
| 36 | * length of ipAddresses if there were too many addresses to log. |
| 37 | */ |
| 38 | private final int ipAddressesCount; |
| 39 | |
Michal Karpinski | e3639a0 | 2016-12-05 13:31:40 +0000 | [diff] [blame] | 40 | /** @hide */ |
Michal Karpinski | 4c47ade | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 41 | public DnsEvent(String hostname, String[] ipAddresses, int ipAddressesCount, |
| 42 | String packageName, long timestamp) { |
| 43 | super(packageName, timestamp); |
| 44 | this.hostname = hostname; |
| 45 | this.ipAddresses = ipAddresses; |
| 46 | this.ipAddressesCount = ipAddressesCount; |
| 47 | } |
| 48 | |
| 49 | private DnsEvent(Parcel in) { |
| 50 | this.hostname = in.readString(); |
| 51 | this.ipAddresses = in.createStringArray(); |
| 52 | this.ipAddressesCount = in.readInt(); |
| 53 | this.packageName = in.readString(); |
| 54 | this.timestamp = in.readLong(); |
| 55 | } |
| 56 | |
| 57 | /** Returns the hostname that was looked up. */ |
| 58 | public String getHostname() { |
| 59 | return hostname; |
| 60 | } |
| 61 | |
| 62 | /** Returns (possibly a subset of) the IP addresses returned. */ |
| 63 | public String[] getIpAddresses() { |
| 64 | return ipAddresses; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the number of IP addresses returned from the DNS lookup event. May be different from |
| 69 | * the length of ipAddresses if there were too many addresses to log. |
| 70 | */ |
| 71 | public int getIpAddressesCount() { |
| 72 | return ipAddressesCount; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String toString() { |
| 77 | StringBuilder sb = new StringBuilder(); |
| 78 | if (ipAddresses != null) { |
| 79 | for (int i = 0; i < ipAddresses.length; i++) { |
| 80 | sb.append(ipAddresses[i]); |
| 81 | if (i < ipAddresses.length - 1) { |
| 82 | sb.append(" "); |
| 83 | } |
| 84 | } |
| 85 | } else { |
| 86 | sb.append("NONE"); |
| 87 | } |
| 88 | return String.format("DnsEvent(%s, %s, %d, %d, %s)", hostname, sb.toString(), |
| 89 | ipAddressesCount, timestamp, packageName); |
| 90 | } |
| 91 | |
| 92 | public static final Parcelable.Creator<DnsEvent> CREATOR |
| 93 | = new Parcelable.Creator<DnsEvent>() { |
| 94 | @Override |
| 95 | public DnsEvent createFromParcel(Parcel in) { |
| 96 | if (in.readInt() != PARCEL_TOKEN_DNS_EVENT) { |
| 97 | return null; |
| 98 | } |
| 99 | return new DnsEvent(in); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public DnsEvent[] newArray(int size) { |
| 104 | return new DnsEvent[size]; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | @Override |
Michal Karpinski | 0c4ec00 | 2016-11-25 17:17:35 +0000 | [diff] [blame] | 109 | public int describeContents() { |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | @Override |
Michal Karpinski | 4c47ade | 2016-10-12 16:40:06 +0100 | [diff] [blame] | 114 | public void writeToParcel(Parcel out, int flags) { |
| 115 | // write parcel token first |
| 116 | out.writeInt(PARCEL_TOKEN_DNS_EVENT); |
| 117 | out.writeString(hostname); |
| 118 | out.writeStringArray(ipAddresses); |
| 119 | out.writeInt(ipAddressesCount); |
| 120 | out.writeString(packageName); |
| 121 | out.writeLong(timestamp); |
| 122 | } |
| 123 | } |
| 124 | |