• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import android.net.TrafficStats;
4 import android.os.Build;
5 import org.robolectric.annotation.Implementation;
6 import org.robolectric.annotation.Implements;
7 import org.robolectric.annotation.Resetter;
8 
9 @Implements(TrafficStats.class)
10 public class ShadowTrafficStats {
11 
12   private static int mobileTxPackets = TrafficStats.UNSUPPORTED;
13   private static int mobileRxPackets = TrafficStats.UNSUPPORTED;
14   private static int mobileTxBytes = TrafficStats.UNSUPPORTED;
15   private static int mobileRxBytes = TrafficStats.UNSUPPORTED;
16   private static int totalTxPackets = TrafficStats.UNSUPPORTED;
17   private static int totalRxPackets = TrafficStats.UNSUPPORTED;
18   private static int totalTxBytes = TrafficStats.UNSUPPORTED;
19   private static int totalRxBytes = TrafficStats.UNSUPPORTED;
20 
21   private static final ThreadLocal<Integer> threadTag =
22       ThreadLocal.withInitial(() -> TrafficStats.UNSUPPORTED);
23 
24   @Implementation
setThreadStatsTag(int tag)25   protected static void setThreadStatsTag(int tag) {
26     threadTag.set(tag);
27   }
28 
29   @Implementation
getThreadStatsTag()30   protected static int getThreadStatsTag() {
31     return threadTag.get();
32   }
33 
34   @Implementation
clearThreadStatsTag()35   protected static void clearThreadStatsTag() {
36     threadTag.set(TrafficStats.UNSUPPORTED);
37   }
38 
39   @Implementation
tagSocket(java.net.Socket socket)40   protected static void tagSocket(java.net.Socket socket) throws java.net.SocketException {}
41 
42   /** No-op in tests. */
43   @Implementation(minSdk = Build.VERSION_CODES.N)
tagDatagramSocket(java.net.DatagramSocket socket)44   protected static void tagDatagramSocket(java.net.DatagramSocket socket)
45       throws java.net.SocketException {}
46 
47   @Implementation
untagSocket(java.net.Socket socket)48   protected static void untagSocket(java.net.Socket socket) throws java.net.SocketException {}
49 
50   @Implementation
incrementOperationCount(int operationCount)51   protected static void incrementOperationCount(int operationCount) {}
52 
53   @Implementation
incrementOperationCount(int tag, int operationCount)54   protected static void incrementOperationCount(int tag, int operationCount) {}
55 
56   @Implementation
getMobileTxPackets()57   protected static long getMobileTxPackets() {
58     return mobileTxPackets;
59   }
60 
61   @Implementation
getMobileRxPackets()62   protected static long getMobileRxPackets() {
63     return mobileRxPackets;
64   }
65 
66   @Implementation
getMobileTxBytes()67   protected static long getMobileTxBytes() {
68     return mobileTxBytes;
69   }
70 
71   @Implementation
getMobileRxBytes()72   protected static long getMobileRxBytes() {
73     return mobileRxBytes;
74   }
75 
76   @Implementation
getTotalTxPackets()77   protected static long getTotalTxPackets() {
78     return totalTxPackets;
79   }
80 
81   @Implementation
getTotalRxPackets()82   protected static long getTotalRxPackets() {
83     return totalRxPackets;
84   }
85 
86   @Implementation
getTotalTxBytes()87   protected static long getTotalTxBytes() {
88     return totalTxBytes;
89   }
90 
91   @Implementation
getTotalRxBytes()92   protected static long getTotalRxBytes() {
93     return totalRxBytes;
94   }
95 
96   @Implementation
getUidTxBytes(int i)97   protected static long getUidTxBytes(int i) {
98     return TrafficStats.UNSUPPORTED;
99   }
100 
101   @Implementation
getUidRxBytes(int i)102   protected static long getUidRxBytes(int i) {
103     return TrafficStats.UNSUPPORTED;
104   }
105 
106   @Implementation
getUidTxPackets(int i)107   protected static long getUidTxPackets(int i) {
108     return TrafficStats.UNSUPPORTED;
109   }
110 
111   @Implementation
getUidRxPackets(int i)112   protected static long getUidRxPackets(int i) {
113     return TrafficStats.UNSUPPORTED;
114   }
115 
116   @Implementation
getUidTcpTxBytes(int i)117   protected static long getUidTcpTxBytes(int i) {
118     return TrafficStats.UNSUPPORTED;
119   }
120 
121   @Implementation
getUidTcpRxBytes(int i)122   protected static long getUidTcpRxBytes(int i) {
123     return TrafficStats.UNSUPPORTED;
124   }
125 
126   @Implementation
getUidUdpTxBytes(int i)127   protected static long getUidUdpTxBytes(int i) {
128     return TrafficStats.UNSUPPORTED;
129   }
130 
131   @Implementation
getUidUdpRxBytes(int i)132   protected static long getUidUdpRxBytes(int i) {
133     return TrafficStats.UNSUPPORTED;
134   }
135 
136   @Implementation
getUidTcpTxSegments(int i)137   protected static long getUidTcpTxSegments(int i) {
138     return TrafficStats.UNSUPPORTED;
139   }
140 
141   @Implementation
getUidTcpRxSegments(int i)142   protected static long getUidTcpRxSegments(int i) {
143     return TrafficStats.UNSUPPORTED;
144   }
145 
146   @Implementation
getUidUdpTxPackets(int i)147   protected static long getUidUdpTxPackets(int i) {
148     return TrafficStats.UNSUPPORTED;
149   }
150 
151   @Implementation
getUidUdpRxPackets(int i)152   protected static long getUidUdpRxPackets(int i) {
153     return TrafficStats.UNSUPPORTED;
154   }
155 
156   /** Sets the value returned by {@link #getMobileTxPackets()} for testing */
setMobileTxPackets(int mobileTxPackets)157   public static void setMobileTxPackets(int mobileTxPackets) {
158     ShadowTrafficStats.mobileTxPackets = mobileTxPackets;
159   }
160 
161   /** Sets the value returned by {@link #getMobileRxPackets()} for testing */
setMobileRxPackets(int mobileRxPackets)162   public static void setMobileRxPackets(int mobileRxPackets) {
163     ShadowTrafficStats.mobileRxPackets = mobileRxPackets;
164   }
165 
166   /** Sets the value returned by {@link #getMobileTxBytes()} for testing */
setMobileTxBytes(int mobileTxBytes)167   public static void setMobileTxBytes(int mobileTxBytes) {
168     ShadowTrafficStats.mobileTxBytes = mobileTxBytes;
169   }
170 
171   /** Sets the value returned by {@link #getMobileRxBytes()} for testing */
setMobileRxBytes(int mobileRxBytes)172   public static void setMobileRxBytes(int mobileRxBytes) {
173     ShadowTrafficStats.mobileRxBytes = mobileRxBytes;
174   }
175 
176   /** Sets the value returned by {@link #getTotalTxPackets()} for testing */
setTotalTxPackets(int totalTxPackets)177   public static void setTotalTxPackets(int totalTxPackets) {
178     ShadowTrafficStats.totalTxPackets = totalTxPackets;
179   }
180 
181   /** Sets the value returned by {@link #getTotalRxPackets()} for testing */
setTotalRxPackets(int totalRxPackets)182   public static void setTotalRxPackets(int totalRxPackets) {
183     ShadowTrafficStats.totalRxPackets = totalRxPackets;
184   }
185 
186   /** Sets the value returned by {@link #getTotalTxBytes()} for testing */
setTotalTxBytes(int totalTxBytes)187   public static void setTotalTxBytes(int totalTxBytes) {
188     ShadowTrafficStats.totalTxBytes = totalTxBytes;
189   }
190 
191   /** Sets the value returned by {@link #getTotalRxBytes()} for testing */
setTotalRxBytes(int totalRxBytes)192   public static void setTotalRxBytes(int totalRxBytes) {
193     ShadowTrafficStats.totalRxBytes = totalRxBytes;
194   }
195 
196   /** Updates all non UID specific fields back to {@link TrafficStats#UNSUPPORTED} */
197   @Resetter
restoreDefaults()198   public static void restoreDefaults() {
199     mobileTxPackets = TrafficStats.UNSUPPORTED;
200     mobileRxPackets = TrafficStats.UNSUPPORTED;
201     mobileTxBytes = TrafficStats.UNSUPPORTED;
202     mobileRxBytes = TrafficStats.UNSUPPORTED;
203     totalTxPackets = TrafficStats.UNSUPPORTED;
204     totalRxPackets = TrafficStats.UNSUPPORTED;
205     totalTxBytes = TrafficStats.UNSUPPORTED;
206     totalRxBytes = TrafficStats.UNSUPPORTED;
207     threadTag.remove();
208   }
209 }
210