1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.net.ConnectivityManager;
4 import android.net.NetworkInfo;
5 import com.xtremelabs.robolectric.internal.Implementation;
6 import com.xtremelabs.robolectric.internal.Implements;
7 
8 import java.util.HashMap;
9 import java.util.Map;
10 
11 /**
12  * Shadow of {@code ConnectivityManager} that provides for the simulation of
13  * the active connection status.
14  */
15 
16 @Implements(ConnectivityManager.class)
17 public class ShadowConnectivityManager {
18 
19     private NetworkInfo activeNetwork;
20     private boolean backgroundDataSetting;
21 
22     private Map<Integer, NetworkInfo> networkTypeToNetworkInfo = new HashMap<Integer, NetworkInfo>();
23 
__constructor__()24     public void __constructor__() {
25         setActiveNetworkInfo(ShadowNetworkInfo.newInstance());
26     }
27 
28     @Implementation
getActiveNetworkInfo()29     public NetworkInfo getActiveNetworkInfo() {
30         return activeNetwork;
31     }
32 
33     @Implementation
getAllNetworkInfo()34     public NetworkInfo[] getAllNetworkInfo() {
35         return networkTypeToNetworkInfo.values().toArray(new NetworkInfo[networkTypeToNetworkInfo.size()]);
36     }
37 
38     @Implementation
getNetworkInfo(int networkType)39     public NetworkInfo getNetworkInfo(int networkType) {
40         return networkTypeToNetworkInfo.get(networkType);
41     }
42 
43     @Implementation
getBackgroundDataSetting()44     public boolean getBackgroundDataSetting() {
45         return backgroundDataSetting;
46     }
47 
setNetworkInfo(int networkType, NetworkInfo networkInfo)48     public void setNetworkInfo(int networkType, NetworkInfo networkInfo) {
49         networkTypeToNetworkInfo.put(networkType, networkInfo);
50     }
51 
setBackgroundDataSetting(boolean b)52     public void setBackgroundDataSetting(boolean b) {
53         backgroundDataSetting = b;
54     }
55 
setActiveNetworkInfo(NetworkInfo info)56     public void setActiveNetworkInfo(NetworkInfo info) {
57         activeNetwork = info;
58         if (info != null) {
59             networkTypeToNetworkInfo.put(info.getType(), info);
60         }  else {
61             networkTypeToNetworkInfo.clear();
62         }
63     }
64 }
65