1package com.xtremelabs.robolectric.shadows;
2
3import android.net.ConnectivityManager;
4import android.net.NetworkInfo;
5import com.xtremelabs.robolectric.internal.Implementation;
6import com.xtremelabs.robolectric.internal.Implements;
7
8import java.util.HashMap;
9import 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)
17public class ShadowConnectivityManager {
18
19    private NetworkInfo activeNetwork;
20    private boolean backgroundDataSetting;
21
22    private Map<Integer, NetworkInfo> networkTypeToNetworkInfo = new HashMap<Integer, NetworkInfo>();
23
24    public void __constructor__() {
25        setActiveNetworkInfo(ShadowNetworkInfo.newInstance());
26    }
27
28    @Implementation
29    public NetworkInfo getActiveNetworkInfo() {
30        return activeNetwork;
31    }
32
33    @Implementation
34    public NetworkInfo[] getAllNetworkInfo() {
35        return networkTypeToNetworkInfo.values().toArray(new NetworkInfo[networkTypeToNetworkInfo.size()]);
36    }
37
38    @Implementation
39    public NetworkInfo getNetworkInfo(int networkType) {
40        return networkTypeToNetworkInfo.get(networkType);
41    }
42
43    @Implementation
44    public boolean getBackgroundDataSetting() {
45        return backgroundDataSetting;
46    }
47
48    public void setNetworkInfo(int networkType, NetworkInfo networkInfo) {
49        networkTypeToNetworkInfo.put(networkType, networkInfo);
50    }
51
52    public void setBackgroundDataSetting(boolean b) {
53        backgroundDataSetting = b;
54    }
55
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