Home | History | Annotate | Download | only in shadows
      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 
     20   public static WifiInfo newInstance() {
     21     return ReflectionHelpers.callConstructor(WifiInfo.class);
     22   }
     23 
     24   @RealObject
     25   WifiInfo realObject;
     26 
     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 
     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 
     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 
     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 
     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 
     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 
     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 
     98   public void setFrequency(int frequency) {
     99     directlyOn(realObject, WifiInfo.class).setFrequency(frequency);
    100   }
    101 
    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