1 /*
2  * Copyright (C) 2020 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.media.metrics;
18 
19 import android.annotation.IntDef;
20 import android.annotation.IntRange;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.os.Bundle;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 import java.util.Objects;
30 
31 /**
32  * Media network event.
33  */
34 public final class NetworkEvent extends Event implements Parcelable {
35     /** Network type is not known. Default type. */
36     public static final int NETWORK_TYPE_UNKNOWN = 0;
37     /** Other network type */
38     public static final int NETWORK_TYPE_OTHER = 1;
39     /** Wi-Fi network */
40     public static final int NETWORK_TYPE_WIFI = 2;
41     /** Ethernet network */
42     public static final int NETWORK_TYPE_ETHERNET = 3;
43     /** 2G network */
44     public static final int NETWORK_TYPE_2G = 4;
45     /** 3G network */
46     public static final int NETWORK_TYPE_3G = 5;
47     /** 4G network */
48     public static final int NETWORK_TYPE_4G = 6;
49     /** 5G NSA network */
50     public static final int NETWORK_TYPE_5G_NSA = 7;
51     /** 5G SA network */
52     public static final int NETWORK_TYPE_5G_SA = 8;
53     /** Not network connected */
54     public static final int NETWORK_TYPE_OFFLINE = 9;
55 
56     private final int mNetworkType;
57     private final long mTimeSinceCreatedMillis;
58 
59     /** @hide */
60     @IntDef(prefix = "NETWORK_TYPE_", value = {
61         NETWORK_TYPE_UNKNOWN,
62         NETWORK_TYPE_OTHER,
63         NETWORK_TYPE_WIFI,
64         NETWORK_TYPE_ETHERNET,
65         NETWORK_TYPE_2G,
66         NETWORK_TYPE_3G,
67         NETWORK_TYPE_4G,
68         NETWORK_TYPE_5G_NSA,
69         NETWORK_TYPE_5G_SA,
70         NETWORK_TYPE_OFFLINE
71     })
72     @Retention(RetentionPolicy.SOURCE)
73     public @interface NetworkType {}
74 
75     /**
76      * Network type to string.
77      * @hide
78      */
networkTypeToString(@etworkType int value)79     public static String networkTypeToString(@NetworkType int value) {
80         switch (value) {
81             case NETWORK_TYPE_UNKNOWN:
82                 return "NETWORK_TYPE_UNKNOWN";
83             case NETWORK_TYPE_OTHER:
84                 return "NETWORK_TYPE_OTHER";
85             case NETWORK_TYPE_WIFI:
86                 return "NETWORK_TYPE_WIFI";
87             case NETWORK_TYPE_ETHERNET:
88                 return "NETWORK_TYPE_ETHERNET";
89             case NETWORK_TYPE_2G:
90                 return "NETWORK_TYPE_2G";
91             case NETWORK_TYPE_3G:
92                 return "NETWORK_TYPE_3G";
93             case NETWORK_TYPE_4G:
94                 return "NETWORK_TYPE_4G";
95             case NETWORK_TYPE_5G_NSA:
96                 return "NETWORK_TYPE_5G_NSA";
97             case NETWORK_TYPE_5G_SA:
98                 return "NETWORK_TYPE_5G_SA";
99             case NETWORK_TYPE_OFFLINE:
100                 return "NETWORK_TYPE_OFFLINE";
101             default:
102                 return Integer.toHexString(value);
103         }
104     }
105 
106     /**
107      * Creates a new NetworkEvent.
108      */
NetworkEvent(@etworkType int type, long timeSinceCreatedMillis, @NonNull Bundle extras)109     private NetworkEvent(@NetworkType int type, long timeSinceCreatedMillis,
110             @NonNull Bundle extras) {
111         this.mNetworkType = type;
112         this.mTimeSinceCreatedMillis = timeSinceCreatedMillis;
113         this.mMetricsBundle = extras == null ? null : extras.deepCopy();
114     }
115 
116     /**
117      * Gets network type.
118      */
119     @NetworkType
getNetworkType()120     public int getNetworkType() {
121         return mNetworkType;
122     }
123 
124     /**
125      * Gets timestamp since the creation of the log session in milliseconds.
126      * @return the timestamp since the creation in milliseconds, or -1 if unknown.
127      * @see LogSessionId
128      * @see PlaybackSession
129      * @see RecordingSession
130      */
131     @Override
132     @IntRange(from = -1)
getTimeSinceCreatedMillis()133     public long getTimeSinceCreatedMillis() {
134         return mTimeSinceCreatedMillis;
135     }
136 
137     /**
138      * Gets metrics-related information that is not supported by dedicated methods.
139      * <p>It is intended to be used for backwards compatibility by the metrics infrastructure.
140      */
141     @Override
142     @NonNull
getMetricsBundle()143     public Bundle getMetricsBundle() {
144         return mMetricsBundle;
145     }
146 
147     @Override
toString()148     public String toString() {
149         return "NetworkEvent { "
150                 + "networkType = " + mNetworkType + ", "
151                 + "timeSinceCreatedMillis = " + mTimeSinceCreatedMillis
152                 + " }";
153     }
154 
155     @Override
equals(@ullable Object o)156     public boolean equals(@Nullable Object o) {
157         if (this == o) return true;
158         if (o == null || getClass() != o.getClass()) return false;
159         NetworkEvent that = (NetworkEvent) o;
160         return mNetworkType == that.mNetworkType
161                 && mTimeSinceCreatedMillis == that.mTimeSinceCreatedMillis;
162     }
163 
164     @Override
hashCode()165     public int hashCode() {
166         return Objects.hash(mNetworkType, mTimeSinceCreatedMillis);
167     }
168 
169     @Override
writeToParcel(@onNull android.os.Parcel dest, int flags)170     public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
171         dest.writeInt(mNetworkType);
172         dest.writeLong(mTimeSinceCreatedMillis);
173         dest.writeBundle(mMetricsBundle);
174     }
175 
176     @Override
describeContents()177     public int describeContents() {
178         return 0;
179     }
180 
NetworkEvent(@onNull android.os.Parcel in)181     private NetworkEvent(@NonNull android.os.Parcel in) {
182         int type = in.readInt();
183         long timeSinceCreatedMillis = in.readLong();
184         Bundle extras = in.readBundle();
185 
186         this.mNetworkType = type;
187         this.mTimeSinceCreatedMillis = timeSinceCreatedMillis;
188         this.mMetricsBundle = extras;
189     }
190 
191     /**
192      * Used to read a NetworkEvent from a Parcel.
193      */
194     public static final @NonNull Parcelable.Creator<NetworkEvent> CREATOR =
195             new Parcelable.Creator<NetworkEvent>() {
196         @Override
197         public NetworkEvent[] newArray(int size) {
198             return new NetworkEvent[size];
199         }
200 
201         @Override
202         public NetworkEvent createFromParcel(@NonNull Parcel in) {
203             return new NetworkEvent(in);
204         }
205     };
206 
207     /**
208      * A builder for {@link NetworkEvent}
209      */
210     public static final class Builder {
211         private int mNetworkType = NETWORK_TYPE_UNKNOWN;
212         private long mTimeSinceCreatedMillis = -1;
213         private Bundle mMetricsBundle = new Bundle();
214 
215         /**
216          * Creates a new Builder.
217          */
Builder()218         public Builder() {
219         }
220 
221         /**
222          * Sets network type.
223          */
setNetworkType(@etworkType int value)224         public @NonNull Builder setNetworkType(@NetworkType int value) {
225             mNetworkType = value;
226             return this;
227         }
228 
229         /**
230          * Sets timestamp since the creation in milliseconds.
231          * @param value the timestamp since the creation in milliseconds.
232          *              -1 indicates the value is unknown.
233          * @see #getTimeSinceCreatedMillis()
234          */
setTimeSinceCreatedMillis(@ntRangefrom = -1) long value)235         public @NonNull Builder setTimeSinceCreatedMillis(@IntRange(from = -1) long value) {
236             mTimeSinceCreatedMillis = value;
237             return this;
238         }
239 
240         /**
241          * Sets metrics-related information that is not supported by dedicated
242          * methods.
243          * <p>It is intended to be used for backwards compatibility by the
244          * metrics infrastructure.
245          */
setMetricsBundle(@onNull Bundle metricsBundle)246         public @NonNull Builder setMetricsBundle(@NonNull Bundle metricsBundle) {
247             mMetricsBundle = metricsBundle;
248             return this;
249         }
250 
251         /** Builds the instance. */
build()252         public @NonNull NetworkEvent build() {
253             NetworkEvent o =
254                     new NetworkEvent(mNetworkType, mTimeSinceCreatedMillis, mMetricsBundle);
255             return o;
256         }
257     }
258 }
259