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.WifiP2pInfo; 26 import android.net.wifi.p2p.WifiP2pManager; 27 import android.net.wifi.p2p.WifiP2pManager.Channel; 28 import android.net.wifi.p2p.WifiP2pManager.PeerListListener; 29 import android.util.Log; 30 31 /** 32 * The utility class for testing wifi direct broadcast intent. 33 */ 34 public class P2pBroadcastReceiverTest extends BroadcastReceiver 35 implements PeerListListener { 36 37 private static final String TAG = "P2pBroadcastReceiverTest"; 38 39 private final IntentFilter mIntentFilter = new IntentFilter(); 40 private Context mContext; 41 private WifiP2pManager mP2pMgr; 42 private Channel mChannel; 43 44 private WifiP2pDeviceList mPeers; 45 private WifiP2pInfo mP2pInfo; 46 47 public P2pBroadcastReceiverTest(Context context) { 48 this.mContext = context; 49 mP2pMgr = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE); 50 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); 51 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); 52 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); 53 mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); 54 } 55 56 public void init(Channel c) { 57 mChannel = c; 58 mContext.registerReceiver(this, mIntentFilter); 59 } 60 61 public synchronized void close() { 62 mContext.unregisterReceiver(this); 63 notifyAll(); 64 } 65 66 /** 67 * Wait until the specified target device is found. 68 * 69 * @param targetAddr the p2p device address of the target device. 70 * @param msec timeout value. 71 * @return the found device. Return null if the target device is not found 72 * within the given timeout. 73 * @throws InterruptedException 74 */ 75 public synchronized WifiP2pDevice waitDeviceFound(String targetAddr, long msec) 76 throws InterruptedException { 77 78 Timeout t = new Timeout(msec); 79 while (!t.isTimeout()) { 80 if (mPeers != null) { 81 for (WifiP2pDevice dev: mPeers.getDeviceList()) { 82 if (dev.deviceAddress.equals(targetAddr)) { 83 return dev; 84 } 85 } 86 } 87 wait(t.getRemainTime()); 88 } 89 Log.e(TAG, "Appropriate WIFI_P2P_PEERS_CHANGED_ACTION didn't occur"); 90 91 return null; 92 } 93 94 /** 95 * Wait until a p2p group is created. 96 * 97 * @param msec timeout value 98 * @return a established p2p information. Return null if a connection is NOT 99 * established within the given timeout. 100 * @throws InterruptedException 101 */ 102 public synchronized WifiP2pInfo waitConnectionNotice(long msec) throws InterruptedException { 103 Timeout t = new Timeout(msec); 104 while (!t.isTimeout()) { 105 if (mP2pInfo != null && mP2pInfo.groupFormed) { 106 return mP2pInfo; 107 } 108 wait(t.getRemainTime()); 109 } 110 Log.e(TAG, "Appropriate WIFI_P2P_CONNECTION_CHANGED_ACTION didn't occur"); 111 112 return null; 113 } 114 115 /** 116 * Wait until a station gets connected. 117 * @param msec 118 * @return 119 * @throws InterruptedException 120 */ 121 public synchronized boolean waitPeerConnected(String targetAddr, long msec) 122 throws InterruptedException { 123 124 Timeout t = new Timeout(msec); 125 while (!t.isTimeout()) { 126 if (mPeers != null) { 127 for (WifiP2pDevice dev: mPeers.getDeviceList()) { 128 if (dev.deviceAddress.equals(targetAddr)) { 129 if (dev.status == WifiP2pDevice.CONNECTED) { 130 return true; 131 } 132 } 133 } 134 } 135 wait(t.getRemainTime()); 136 } 137 Log.e(TAG, "Appropriate WIFI_P2P_PEERS_CHANGED_ACTION didn't occur"); 138 139 return false; 140 } 141 142 /** 143 * Wait until a station gets disconnected. 144 * @param msec 145 * @return 146 * @throws InterruptedException 147 */ 148 public synchronized boolean waitPeerDisconnected(String targetAddr, long msec) 149 throws InterruptedException { 150 151 Timeout t = new Timeout(msec); 152 153 boolean devicePresent; 154 155 while (!t.isTimeout()) { 156 devicePresent = false; 157 if (mPeers != null) { 158 for (WifiP2pDevice dev: mPeers.getDeviceList()) { 159 if (dev.deviceAddress.equals(targetAddr)) { 160 if (dev.status != WifiP2pDevice.CONNECTED) { 161 return true; 162 } 163 devicePresent = true; 164 } 165 } 166 } 167 if (!devicePresent) return true; 168 wait(t.getRemainTime()); 169 } 170 Log.e(TAG, "Appropriate WIFI_P2P_PEERS_CHANGED_ACTION didn't occur"); 171 172 return false; 173 } 174 175 /** 176 * Wait until a connection is disconnected. 177 * 178 * @param msec timeout value. 179 * @return a disconnected p2p information. Return null if a connection is NOT disconnected 180 * within the given timeout. 181 * @throws InterruptedException 182 */ 183 public synchronized WifiP2pInfo waitDisconnectionNotice(long msec) throws InterruptedException { 184 Timeout t = new Timeout(msec); 185 while (!t.isTimeout()) { 186 if (mP2pInfo != null && !mP2pInfo.groupFormed) { 187 return mP2pInfo; 188 } 189 wait(t.getRemainTime()); 190 } 191 Log.e(TAG, "WIFI_P2P_CONNECTION_CHANGED_ACTION didn't occur"); 192 193 return null; 194 } 195 196 197 @Override 198 public void onReceive(Context context, Intent intent) { 199 String action = intent.getAction(); 200 if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { 201 Log.d(TAG, "WIFI_P2P_PEERS_CHANGED_ACTION"); 202 mP2pMgr.requestPeers(mChannel, this); 203 } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { 204 Log.d(TAG, "WIFI_P2P_CONNECTION_CHANGED_ACTION"); 205 synchronized(this) { 206 mP2pInfo = (WifiP2pInfo)intent.getParcelableExtra( 207 WifiP2pManager.EXTRA_WIFI_P2P_INFO); 208 notifyAll(); 209 } 210 } 211 } 212 213 @Override 214 public synchronized void onPeersAvailable(WifiP2pDeviceList peers) { 215 Log.d(TAG, "onPeersAvailable()"); 216 mPeers = peers; 217 notifyAll(); 218 } 219 } 220