• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.JELLY_BEAN;
4 import static android.os.Build.VERSION_CODES.KITKAT;
5 import static android.os.Build.VERSION_CODES.LOLLIPOP;
6 import static org.robolectric.shadow.api.Shadow.directlyOn;
7 
8 import android.net.wifi.WifiInfo;
9 import android.net.wifi.WifiSsid;
10 import java.net.InetAddress;
11 import org.robolectric.RuntimeEnvironment;
12 import org.robolectric.annotation.Implements;
13 import org.robolectric.annotation.RealObject;
14 import org.robolectric.util.ReflectionHelpers;
15 import org.robolectric.util.ReflectionHelpers.ClassParameter;
16 
17 @Implements(WifiInfo.class)
18 public class ShadowWifiInfo {
19 
newInstance()20   public static WifiInfo newInstance() {
21     return ReflectionHelpers.callConstructor(WifiInfo.class);
22   }
23 
24   @RealObject
25   WifiInfo realObject;
26 
setInetAddress(InetAddress address)27   public void setInetAddress(InetAddress address) {
28     if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
29       directlyOn(realObject, WifiInfo.class).setInetAddress(address);
30     } else {
31       directlyOn(
32           realObject,
33           WifiInfo.class,
34           "setInetAddress",
35           ClassParameter.from(InetAddress.class, address));
36     }
37   }
38 
setMacAddress(String newMacAddress)39   public void setMacAddress(String newMacAddress) {
40     if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
41       directlyOn(realObject, WifiInfo.class).setMacAddress(newMacAddress);
42     } else {
43       directlyOn(realObject, WifiInfo.class,
44           "setMacAddress", ClassParameter.from(String.class, newMacAddress));
45     }
46   }
47 
setSSID(String ssid)48   public void setSSID(String ssid) {
49     if (RuntimeEnvironment.getApiLevel() <= JELLY_BEAN) {
50       directlyOn(realObject, WifiInfo.class,
51           "setSSID", ClassParameter.from(String.class, ssid));
52     } else if (RuntimeEnvironment.getApiLevel() <= KITKAT) {
53 
54       directlyOn(realObject, WifiInfo.class,
55           "setSSID", ClassParameter.from(WifiSsid.class, getWifiSsid(ssid)));
56     } else {
57       directlyOn(realObject, WifiInfo.class).setSSID((WifiSsid) getWifiSsid(ssid));
58     }
59   }
60 
getWifiSsid(String ssid)61   private static Object getWifiSsid(String ssid) {
62     WifiSsid wifiSsid;
63     if (ssid.startsWith("0x")) {
64       wifiSsid = WifiSsid.createFromHex(ssid);
65     } else {
66       wifiSsid = WifiSsid.createFromAsciiEncoded(ssid);
67     }
68     return wifiSsid;
69   }
70 
setBSSID(String bssid)71   public void setBSSID(String bssid) {
72     if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
73       directlyOn(realObject, WifiInfo.class).setBSSID(bssid);
74     } else {
75       directlyOn(realObject, WifiInfo.class,
76           "setBSSID", ClassParameter.from(String.class, bssid));
77     }
78   }
79 
setRssi(int rssi)80   public void setRssi(int rssi) {
81     if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
82       directlyOn(realObject, WifiInfo.class).setRssi(rssi);
83     } else {
84       directlyOn(realObject, WifiInfo.class,
85           "setRssi", ClassParameter.from(int.class, rssi));
86     }
87   }
88 
setLinkSpeed(int linkSpeed)89   public void setLinkSpeed(int linkSpeed) {
90     if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
91       directlyOn(realObject, WifiInfo.class).setLinkSpeed(linkSpeed);
92     } else {
93       directlyOn(realObject, WifiInfo.class,
94           "setLinkSpeed", ClassParameter.from(int.class, linkSpeed));
95     }
96   }
97 
setFrequency(int frequency)98   public void setFrequency(int frequency) {
99     directlyOn(realObject, WifiInfo.class).setFrequency(frequency);
100   }
101 
setNetworkId(int id)102   public void setNetworkId(int id) {
103     if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP) {
104       directlyOn(realObject, WifiInfo.class).setNetworkId(id);
105     } else {
106       directlyOn(realObject, WifiInfo.class,
107           "setNetworkId", ClassParameter.from(int.class, id));
108     }
109   }
110 }
111