1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.KITKAT; 4 5 import android.content.Context; 6 import android.net.wifi.p2p.WifiP2pGroup; 7 import android.net.wifi.p2p.WifiP2pManager; 8 import android.net.wifi.p2p.WifiP2pManager.ActionListener; 9 import android.net.wifi.p2p.WifiP2pManager.Channel; 10 import android.os.Handler; 11 import android.os.Looper; 12 import com.google.common.base.Preconditions; 13 import java.util.HashMap; 14 import java.util.Map; 15 import org.robolectric.annotation.Implementation; 16 import org.robolectric.annotation.Implements; 17 import org.robolectric.util.ReflectionHelpers; 18 19 @Implements(WifiP2pManager.class) 20 public class ShadowWifiP2pManager { 21 22 private static final int NO_FAILURE = -1; 23 24 private int listeningChannel; 25 private int operatingChannel; 26 private WifiP2pManager.GroupInfoListener groupInfoListener; 27 private Handler handler; 28 private int nextActionFailure = NO_FAILURE; 29 private Map<Channel, WifiP2pGroup> p2pGroupmap = new HashMap<>(); 30 getListeningChannel()31 public int getListeningChannel() { 32 return listeningChannel; 33 } 34 getOperatingChannel()35 public int getOperatingChannel() { 36 return operatingChannel; 37 } 38 getGroupInfoListener()39 public WifiP2pManager.GroupInfoListener getGroupInfoListener() { 40 return groupInfoListener; 41 } 42 43 @Implementation(minSdk = KITKAT) setWifiP2pChannels( Channel c, int listeningChannel, int operatingChannel, ActionListener al)44 protected void setWifiP2pChannels( 45 Channel c, int listeningChannel, int operatingChannel, ActionListener al) { 46 Preconditions.checkNotNull(c); 47 Preconditions.checkNotNull(al); 48 this.listeningChannel = listeningChannel; 49 this.operatingChannel = operatingChannel; 50 } 51 52 @Implementation initialize( Context context, Looper looper, WifiP2pManager.ChannelListener listener)53 protected Channel initialize( 54 Context context, Looper looper, WifiP2pManager.ChannelListener listener) { 55 handler = new Handler(looper); 56 return ReflectionHelpers.newInstance(Channel.class); 57 } 58 59 @Implementation createGroup(Channel c, ActionListener al)60 protected void createGroup(Channel c, ActionListener al) { 61 postActionListener(al); 62 } 63 postActionListener(final ActionListener al)64 private void postActionListener(final ActionListener al) { 65 if (al == null) { 66 return; 67 } 68 69 handler.post(new Runnable() { 70 @Override 71 public void run() { 72 if (nextActionFailure == -1) { 73 al.onSuccess(); 74 } else { 75 al.onFailure(nextActionFailure); 76 } 77 nextActionFailure = NO_FAILURE; 78 } 79 }); 80 } 81 82 @Implementation requestGroupInfo(final Channel c, final WifiP2pManager.GroupInfoListener gl)83 protected void requestGroupInfo(final Channel c, final WifiP2pManager.GroupInfoListener gl) { 84 if (gl == null) { 85 return; 86 } 87 88 handler.post(new Runnable() { 89 @Override 90 public void run() { 91 gl.onGroupInfoAvailable(p2pGroupmap.get(c)); 92 } 93 }); 94 } 95 96 @Implementation removeGroup(Channel c, ActionListener al)97 protected void removeGroup(Channel c, ActionListener al) { 98 postActionListener(al); 99 } 100 setNextActionFailure(int nextActionFailure)101 public void setNextActionFailure(int nextActionFailure) { 102 this.nextActionFailure = nextActionFailure; 103 } 104 setGroupInfo(Channel channel, WifiP2pGroup wifiP2pGroup)105 public void setGroupInfo(Channel channel, WifiP2pGroup wifiP2pGroup) { 106 p2pGroupmap.put(channel, wifiP2pGroup); 107 } 108 } 109