1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.verifier.p2p.testcase; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.net.wifi.p2p.WifiP2pDevice; 24 import android.net.wifi.p2p.WifiP2pDeviceList; 25 import android.net.wifi.p2p.WifiP2pGroup; 26 import android.net.wifi.p2p.WifiP2pInfo; 27 import android.net.wifi.p2p.WifiP2pManager; 28 import android.net.wifi.p2p.WifiP2pManager.Channel; 29 import android.net.wifi.p2p.WifiP2pManager.PeerListListener; 30 import android.util.Log; 31 32 /** 33 * The utility class for testing wifi direct broadcast intent. 34 */ 35 public class P2pBroadcastReceiverTest extends BroadcastReceiver 36 implements PeerListListener { 37 38 private static final String TAG = "P2pBroadcastReceiverTest"; 39 40 private final IntentFilter mIntentFilter = new IntentFilter(); 41 private Context mContext; 42 private WifiP2pManager mP2pMgr; 43 private Channel mChannel; 44 45 private WifiP2pDeviceList mPeers = new WifiP2pDeviceList(); 46 private WifiP2pInfo mP2pInfo; 47 private WifiP2pGroup mP2pGroup; 48 P2pBroadcastReceiverTest(Context context)49 public P2pBroadcastReceiverTest(Context context) { 50 this.mContext = context; 51 mP2pMgr = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE); 52 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); 53 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); 54 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); 55 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); 56 } 57 init(Channel c)58 public void init(Channel c) { 59 mChannel = c; 60 mContext.registerReceiver(this, mIntentFilter); 61 } 62 close()63 public synchronized void close() { 64 mContext.unregisterReceiver(this); 65 notifyAll(); 66 } 67 68 /** 69 * Wait until the specified target device is found. 70 * 71 * @param targetAddr the p2p device address of the target device. 72 * @param msec timeout value. 73 * @return the found device. Return null if the target device is not found 74 * within the given timeout. 75 * @throws InterruptedException 76 */ waitDeviceFound(String targetAddr, long msec)77 public synchronized WifiP2pDevice waitDeviceFound(String targetAddr, long msec) 78 throws InterruptedException { 79 80 Timeout t = new Timeout(msec); 81 while (!t.isTimeout()) { 82 WifiP2pDevice dev = mPeers.get(targetAddr); 83 if (dev != null) { 84 return dev; 85 } 86 wait(t.getRemainTime()); 87 } 88 Log.e(TAG, "Appropriate WIFI_P2P_PEERS_CHANGED_ACTION didn't occur"); 89 90 return null; 91 } 92 93 /** 94 * Wait until a p2p group is created. 95 * 96 * @param msec timeout value 97 * @return a established p2p information. Return null if a connection is NOT 98 * established within the given timeout. 99 * @throws InterruptedException 100 */ waitConnectionNotice(long msec)101 public synchronized WifiP2pInfo waitConnectionNotice(long msec) throws InterruptedException { 102 Timeout t = new Timeout(msec); 103 while (!t.isTimeout()) { 104 if (mP2pInfo != null && mP2pInfo.groupFormed) { 105 return mP2pInfo; 106 } 107 wait(t.getRemainTime()); 108 } 109 Log.e(TAG, "Appropriate WIFI_P2P_CONNECTION_CHANGED_ACTION didn't occur"); 110 111 return null; 112 } 113 114 /** 115 * Wait until a station gets connected. 116 * @param msec 117 * @return 118 * @throws InterruptedException 119 */ waitPeerConnected(String targetAddr, long msec)120 public synchronized boolean waitPeerConnected(String targetAddr, long msec) 121 throws InterruptedException { 122 123 Timeout t = new Timeout(msec); 124 while (!t.isTimeout()) { 125 WifiP2pDevice dev = mPeers.get(targetAddr); 126 if (dev != null && dev.status == WifiP2pDevice.CONNECTED) { 127 return true; 128 } 129 wait(t.getRemainTime()); 130 } 131 Log.e(TAG, "Appropriate WIFI_P2P_PEERS_CHANGED_ACTION didn't occur"); 132 133 return false; 134 } 135 136 /** 137 * Wait until a station gets disconnected. 138 * @param msec 139 * @return 140 * @throws InterruptedException 141 */ waitPeerDisconnected(String targetAddr, long msec)142 public synchronized boolean waitPeerDisconnected(String targetAddr, long msec) 143 throws InterruptedException { 144 145 Timeout t = new Timeout(msec); 146 147 while (!t.isTimeout()) { 148 WifiP2pDevice dev = mPeers.get(targetAddr); 149 150 if (dev == null ) return true; 151 152 if (dev.status != WifiP2pDevice.CONNECTED) { 153 return true; 154 } 155 wait(t.getRemainTime()); 156 } 157 Log.e(TAG, "Appropriate WIFI_P2P_PEERS_CHANGED_ACTION didn't occur"); 158 159 return false; 160 } 161 162 /** 163 * Wait until a connection is disconnected. 164 * 165 * @param msec timeout value. 166 * @return a disconnected p2p information. Return null if a connection is NOT disconnected 167 * within the given timeout. 168 * @throws InterruptedException 169 */ waitDisconnectionNotice(long msec)170 public synchronized WifiP2pInfo waitDisconnectionNotice(long msec) throws InterruptedException { 171 Timeout t = new Timeout(msec); 172 while (!t.isTimeout()) { 173 if (mP2pInfo != null && !mP2pInfo.groupFormed) { 174 return mP2pInfo; 175 } 176 wait(t.getRemainTime()); 177 } 178 Log.e(TAG, "WIFI_P2P_CONNECTION_CHANGED_ACTION didn't occur"); 179 180 return null; 181 } 182 183 184 @Override onReceive(Context context, Intent intent)185 public void onReceive(Context context, Intent intent) { 186 String action = intent.getAction(); 187 if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { 188 Log.d(TAG, "WIFI_P2P_PEERS_CHANGED_ACTION"); 189 mP2pMgr.requestPeers(mChannel, this); 190 } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { 191 Log.d(TAG, "WIFI_P2P_CONNECTION_CHANGED_ACTION"); 192 synchronized(this) { 193 mP2pInfo = (WifiP2pInfo)intent.getParcelableExtra( 194 WifiP2pManager.EXTRA_WIFI_P2P_INFO); 195 mP2pGroup = (WifiP2pGroup) intent.getParcelableExtra( 196 WifiP2pManager.EXTRA_WIFI_P2P_GROUP); 197 notifyAll(); 198 } 199 } 200 } 201 202 @Override onPeersAvailable(WifiP2pDeviceList peers)203 public synchronized void onPeersAvailable(WifiP2pDeviceList peers) { 204 Log.d(TAG, "onPeersAvailable()"); 205 mPeers = new WifiP2pDeviceList(peers); 206 notifyAll(); 207 } 208 getWifiP2pGroup()209 public synchronized WifiP2pGroup getWifiP2pGroup() { 210 return mP2pGroup; 211 } 212 } 213