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