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 package com.android.cts.verifier.p2p; 17 18 import java.util.Collection; 19 import java.util.Timer; 20 import java.util.TimerTask; 21 22 import android.app.AlertDialog; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.net.wifi.p2p.WifiP2pDevice; 26 import android.net.wifi.p2p.WifiP2pDeviceList; 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.os.Bundle; 31 import android.os.Handler; 32 import android.view.WindowManager; 33 import android.widget.ProgressBar; 34 import android.widget.TextView; 35 36 import com.android.cts.verifier.PassFailButtons; 37 import com.android.cts.verifier.R; 38 import com.android.cts.verifier.R.id; 39 import com.android.cts.verifier.p2p.testcase.ReqTestCase; 40 import com.android.cts.verifier.p2p.testcase.TestCase; 41 import com.android.cts.verifier.p2p.testcase.TestCase.TestCaseListener; 42 43 /** 44 * A base class for requester test activity. 45 * 46 * This class provides the feature to search target device and show test results. 47 * A requester test activity just have to extend this class and implement getTestCase(). 48 */ 49 public abstract class RequesterTestActivity extends PassFailButtons.Activity 50 implements TestCaseListener { 51 52 /* 53 * Timeout for searching devices. The unit is millisecond 54 */ 55 private final static int SEARCH_TARGET_TIMEOUT = 8000; 56 57 /* 58 * The target device address. 59 * The service discovery request test needs the responder address. 60 * The target device address is reused until test failed. 61 */ 62 private static String sTargetAddr; 63 64 /* 65 * The test case to be executed. 66 */ 67 private ReqTestCase mTestCase; 68 69 /* 70 * The text view to print the test result 71 */ 72 private TextView mTextView; 73 74 /* 75 * The progress bar. 76 */ 77 private ProgressBar mProgress; 78 79 /* 80 * GUI thread handler. 81 */ 82 private Handler mHandler = new Handler(); 83 84 /* 85 * Timer object. It's used for searching devices. 86 */ 87 private Timer mTimer; 88 89 /* 90 * p2p manager 91 */ 92 private WifiP2pManager mP2pMgr; 93 private Channel mChannel; 94 95 /** 96 * Return the specified requester test case. 97 * 98 * @param context 99 * @param testId test id. 100 * @return requester test case 101 */ 102 protected abstract ReqTestCase getTestCase(Context context, String testId); 103 104 @Override 105 protected void onCreate(Bundle savedInstanceState) { 106 super.onCreate(savedInstanceState); 107 setContentView(R.layout.p2p_requester_main); 108 setPassFailButtonClickListeners(); 109 getPassButton().setEnabled(false); 110 111 mProgress = (ProgressBar) findViewById(R.id.p2p_progress); 112 mProgress.setVisibility(ProgressBar.VISIBLE); 113 mTextView = (TextView) findViewById(id.p2p_req_text); 114 115 String testId = (String) getIntent().getSerializableExtra( 116 TestCase.EXTRA_TEST_NAME); 117 mTestCase = getTestCase(this, testId); 118 setTitle(mTestCase.getTestName()); 119 120 // Initialize p2p manager. 121 mP2pMgr = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); 122 mChannel = mP2pMgr.initialize(this, getMainLooper(), null); 123 124 // keep screen on while this activity is front view. 125 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 126 } 127 128 @Override 129 protected void onDestroy() { 130 if (mChannel != null) { 131 mChannel.close(); 132 } 133 134 super.onDestroy(); 135 } 136 137 @Override 138 protected void onResume() { 139 super.onResume(); 140 /* 141 * If the target device is NOT set, search targets and show 142 * the target device list on the dialog. 143 * After the user selection, the specified test will be executed. 144 */ 145 if (sTargetAddr == null) { 146 searchTarget(); 147 return; 148 } 149 150 mTestCase.setTargetAddress(sTargetAddr); 151 mTestCase.start(this); 152 } 153 154 @Override 155 protected void onPause() { 156 super.onPause(); 157 if (mTimer != null) { 158 mTimer.cancel(); 159 mTimer = null; 160 } 161 mTestCase.stop(); 162 } 163 164 @Override 165 public String getTestId() { 166 return mTestCase.getTestId(); 167 } 168 169 @Override 170 public void onTestStarted() { 171 mHandler.post(new Runnable() { 172 @Override 173 public void run() { 174 mProgress.setVisibility(ProgressBar.VISIBLE); 175 } 176 }); 177 } 178 179 public void onTestMsgReceived(final String msg) { 180 mHandler.post(new Runnable() { 181 @Override 182 public void run() { 183 mTextView.setText(msg); 184 } 185 }); 186 } 187 188 @Override 189 public void onTestFailed(final String reason) { 190 // if test failed, forget target device address. 191 sTargetAddr = null; 192 193 mHandler.post(new Runnable() { 194 @Override 195 public void run() { 196 mProgress.setVisibility(ProgressBar.INVISIBLE); 197 mTextView.setText(reason); 198 } 199 }); 200 } 201 202 @Override 203 public void onTestSuccess() { 204 mHandler.post(new Runnable() { 205 @Override 206 public void run() { 207 mProgress.setVisibility(ProgressBar.INVISIBLE); 208 mTextView.setText(R.string.p2p_result_success); 209 getPassButton().setEnabled(true); 210 } 211 }); 212 } 213 214 /** 215 * Search devices and show the found devices on the dialog. 216 * After user selection, the specified test will be executed. 217 */ 218 private void searchTarget() { 219 // Discover peers. 220 mP2pMgr.discoverPeers(mChannel, null); 221 mTextView.setText(R.string.p2p_searching_target); 222 mProgress.setVisibility(ProgressBar.VISIBLE); 223 224 /* 225 * Show the peer list dialog after searching devices for 8 seconds. 226 */ 227 if (mTimer == null) { 228 mTimer = new Timer(true); 229 } 230 mTimer.schedule(new TimerTask() { 231 @Override 232 public void run() { 233 mP2pMgr.requestPeers(mChannel, new PeerListListener() { 234 @Override 235 public void onPeersAvailable(WifiP2pDeviceList _peers) { 236 final WifiP2pDeviceList peers = _peers; 237 /* 238 * Need to show dialog in GUI thread. 239 */ 240 mHandler.post(new Runnable() { 241 @Override 242 public void run() { 243 mProgress.setVisibility(ProgressBar.INVISIBLE); 244 if (peers.getDeviceList().size() == 0) { 245 mTextView.setText( 246 R.string.p2p_target_not_found_error); 247 } else { 248 showSelectTargetDialog(peers); 249 } 250 } 251 }); 252 } 253 }); 254 } 255 }, SEARCH_TARGET_TIMEOUT); 256 } 257 258 /** 259 * Show the found device list on the dialog. 260 * The target device address selected by user is stored in {@link #mTargetAddr}. 261 * After user selection, the specified test will be executed. 262 * @param peers 263 * @param testIndex 264 */ 265 private void showSelectTargetDialog(WifiP2pDeviceList peers) { 266 final Collection<WifiP2pDevice> peerList = peers.getDeviceList(); 267 final CharSequence[] items = new CharSequence[peerList.size()]; 268 int i=0; 269 for (WifiP2pDevice dev: peerList) { 270 items[i++] = dev.deviceName; 271 } 272 273 new AlertDialog.Builder(this) 274 .setIcon(android.R.drawable.ic_dialog_info) 275 .setTitle(R.string.p2p_search_target) 276 .setItems(items, new android.content.DialogInterface.OnClickListener() { 277 @Override 278 public void onClick(DialogInterface dialog, int which) { 279 int i=0; 280 for (WifiP2pDevice dev: peerList) { 281 if (i == which) { 282 sTargetAddr = dev.deviceAddress; 283 mTestCase.setTargetAddress(sTargetAddr); 284 mTestCase.start(getTestCaseListener()); 285 break; 286 } 287 i++; 288 } 289 } 290 }).show(); 291 } 292 293 private TestCaseListener getTestCaseListener() { 294 return this; 295 } 296 } 297