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.content.pm.PackageManager;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.os.ParcelFormatException;
23
24/**
25 * An abstract class that represents a network event.
26 */
27public abstract class NetworkEvent implements Parcelable {
28
29    /** @hide */
30    static final int PARCEL_TOKEN_DNS_EVENT = 1;
31    /** @hide */
32    static final int PARCEL_TOKEN_CONNECT_EVENT = 2;
33
34    /** The package name of the UID that performed the query. */
35    String packageName;
36
37    /** The timestamp of the event being reported in milliseconds. */
38    long timestamp;
39
40    /** @hide */
41    NetworkEvent() {
42        //empty constructor
43    }
44
45    /** @hide */
46    NetworkEvent(String packageName, long timestamp) {
47        this.packageName = packageName;
48        this.timestamp = timestamp;
49    }
50
51    /**
52     * Returns the package name of the UID that performed the query, as returned by
53     * {@link PackageManager#getNameForUid}.
54     */
55    public String getPackageName() {
56        return packageName;
57    }
58
59    /**
60     * Returns the timestamp of the event being reported in milliseconds, the difference between
61     * the time the event was reported and midnight, January 1, 1970 UTC.
62     */
63    public long getTimestamp() {
64        return timestamp;
65    }
66
67    @Override
68    public int describeContents() {
69        return 0;
70    }
71
72    public static final Parcelable.Creator<NetworkEvent> CREATOR
73            = new Parcelable.Creator<NetworkEvent>() {
74        public NetworkEvent createFromParcel(Parcel in) {
75            final int initialPosition = in.dataPosition();
76            final int parcelToken = in.readInt();
77            // we need to move back to the position from before we read parcelToken
78            in.setDataPosition(initialPosition);
79            switch (parcelToken) {
80                case PARCEL_TOKEN_DNS_EVENT:
81                    return DnsEvent.CREATOR.createFromParcel(in);
82                case PARCEL_TOKEN_CONNECT_EVENT:
83                    return ConnectEvent.CREATOR.createFromParcel(in);
84                default:
85                    throw new ParcelFormatException("Unexpected NetworkEvent token in parcel: "
86                            + parcelToken);
87            }
88        }
89
90        public NetworkEvent[] newArray(int size) {
91            return new NetworkEvent[size];
92        }
93    };
94
95    @Override
96    public abstract void writeToParcel(Parcel out, int flags);
97}
98
99