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