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 com.android.server.stats.pull.netstats;
18 
19 import static android.net.NetworkTemplate.OEM_MANAGED_ALL;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.net.NetworkStats;
24 import android.telephony.TelephonyManager;
25 
26 import java.util.Arrays;
27 import java.util.Objects;
28 
29 /**
30  * A data class to store a NetworkStats object with information associated to it.
31  *
32  * @hide
33  */
34 public class NetworkStatsExt {
35     @NonNull
36     public final NetworkStats stats;
37     public final int[] transports;
38     public final boolean slicedByFgbg;
39     public final boolean slicedByTag;
40     public final boolean slicedByMetered;
41     public final int ratType;
42     public final int oemManaged;
43     @Nullable
44     public final SubInfo subInfo;
45 
NetworkStatsExt(@onNull NetworkStats stats, int[] transports, boolean slicedByFgbg)46     public NetworkStatsExt(@NonNull NetworkStats stats, int[] transports, boolean slicedByFgbg) {
47         this(stats, transports, slicedByFgbg, /*slicedByTag=*/false, /*slicedByMetered=*/false,
48                 TelephonyManager.NETWORK_TYPE_UNKNOWN, /*subInfo=*/null, OEM_MANAGED_ALL);
49     }
50 
NetworkStatsExt(@onNull NetworkStats stats, int[] transports, boolean slicedByFgbg, boolean slicedByTag, boolean slicedByMetered, int ratType, @Nullable SubInfo subInfo, int oemManaged)51     public NetworkStatsExt(@NonNull NetworkStats stats, int[] transports, boolean slicedByFgbg,
52             boolean slicedByTag, boolean slicedByMetered, int ratType,
53             @Nullable SubInfo subInfo, int oemManaged) {
54         this.stats = stats;
55 
56         // Sort transports array so that we can test for equality without considering order.
57         this.transports = Arrays.copyOf(transports, transports.length);
58         Arrays.sort(this.transports);
59 
60         this.slicedByFgbg = slicedByFgbg;
61         this.slicedByTag = slicedByTag;
62         this.slicedByMetered = slicedByMetered;
63         this.ratType = ratType;
64         this.subInfo = subInfo;
65         this.oemManaged = oemManaged;
66     }
67 
68     /**
69      * A helper function to compare if all fields except NetworkStats are the same.
70      */
hasSameSlicing(@onNull NetworkStatsExt other)71     public boolean hasSameSlicing(@NonNull NetworkStatsExt other) {
72         return Arrays.equals(transports, other.transports) && slicedByFgbg == other.slicedByFgbg
73                 && slicedByTag == other.slicedByTag && slicedByMetered == other.slicedByMetered
74                 && ratType == other.ratType && Objects.equals(subInfo, other.subInfo)
75                 && oemManaged == other.oemManaged;
76     }
77 }
78