• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.AlertDialog;
19 import android.content.Context;
20 import android.content.DialogInterface;
21 import android.net.wifi.p2p.WifiP2pDevice;
22 import android.net.wifi.p2p.WifiP2pDeviceList;
23 import android.net.wifi.p2p.WifiP2pManager;
24 import android.net.wifi.p2p.WifiP2pManager.Channel;
25 import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.view.WindowManager;
29 import android.widget.ProgressBar;
30 import android.widget.TextView;
31 
32 import com.android.cts.verifier.PassFailButtons;
33 import com.android.cts.verifier.R;
34 import com.android.cts.verifier.R.id;
35 import com.android.cts.verifier.p2p.testcase.ReqTestCase;
36 import com.android.cts.verifier.p2p.testcase.TestCase;
37 import com.android.cts.verifier.p2p.testcase.TestCase.TestCaseListener;
38 
39 import java.util.Collection;
40 import java.util.Timer;
41 import java.util.TimerTask;
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      */
getTestCase(Context context, String testId)102     protected abstract ReqTestCase getTestCase(Context context, String testId);
103 
104     @Override
onCreate(Bundle savedInstanceState)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
onDestroy()129     protected void onDestroy() {
130         if (mChannel != null) {
131             mChannel.close();
132         }
133 
134         super.onDestroy();
135     }
136 
137     @Override
onStart()138     protected void onStart() {
139         super.onStart();
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 && !isNoPeerDiscoveryOnResume()) {
146             searchTarget();
147             return;
148         }
149 
150         mTestCase.setTargetAddress(sTargetAddr);
151         mTestCase.start(this);
152     }
153 
154     @Override
onStop()155     protected void onStop() {
156         super.onStop();
157         if (mTimer != null) {
158             mTimer.cancel();
159             mTimer = null;
160         }
161         mTestCase.stop();
162     }
163 
164     @Override
getTestId()165     public String getTestId() {
166         return mTestCase.getTestId();
167     }
168 
169     @Override
onTestStarted()170     public void onTestStarted() {
171         mHandler.post(new Runnable() {
172             @Override
173             public void run() {
174                 mProgress.setVisibility(ProgressBar.VISIBLE);
175             }
176         });
177     }
178 
onTestMsgReceived(final String msg)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
onTestFailed(final String reason)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
onTestSuccess()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      * Do peer searching only, don't do peer selection.
216      * For requester test which do not need to select a peer first.
217      */
isSearchOnlyOnResume()218     protected boolean isSearchOnlyOnResume() {
219         return false;
220     }
221 
222     /** Do peer discovery or not
223      *  For requester test which do not need to discover peer first.
224      */
isNoPeerDiscoveryOnResume()225     protected boolean isNoPeerDiscoveryOnResume() {
226         return false;
227     }
228 
229     /**
230      * Search devices and show the found devices on the dialog.
231      * After user selection, the specified test will be executed.
232      */
searchTarget()233     private void searchTarget() {
234         // Discover peers.
235         mP2pMgr.discoverPeers(mChannel, null);
236         mTextView.setText(R.string.p2p_searching_target);
237         mProgress.setVisibility(ProgressBar.VISIBLE);
238 
239         /*
240          * Show the peer list dialog after searching devices for 8 seconds.
241          */
242         if (mTimer == null) {
243             mTimer = new Timer(true);
244         }
245         mTimer.schedule(new TimerTask() {
246             @Override
247             public void run() {
248                 mP2pMgr.requestPeers(mChannel, new PeerListListener() {
249                     @Override
250                     public void onPeersAvailable(WifiP2pDeviceList _peers) {
251                         final WifiP2pDeviceList peers = _peers;
252                         /*
253                          * Need to show dialog in GUI thread.
254                          */
255                         mHandler.post(new Runnable() {
256                             @Override
257                             public void run() {
258                                 mProgress.setVisibility(ProgressBar.INVISIBLE);
259                                 if (peers.getDeviceList().size() == 0) {
260                                     mTextView.setText(
261                                             R.string.p2p_target_not_found_error);
262                                 } else {
263                                     if (isSearchOnlyOnResume()) {
264                                         mTestCase.start(getTestCaseListener());
265                                     } else {
266                                         showSelectTargetDialog(peers);
267                                     }
268                                 }
269                             }
270                         });
271                     }
272                 });
273             }
274         }, SEARCH_TARGET_TIMEOUT);
275     }
276 
277     /**
278      * Show the found device list on the dialog.
279      * The target device address selected by user is stored in {@link #mTargetAddr}.
280      * After user selection, the specified test will be executed.
281      * @param peers
282      * @param testIndex
283      */
showSelectTargetDialog(WifiP2pDeviceList peers)284     private void showSelectTargetDialog(WifiP2pDeviceList peers) {
285         final Collection<WifiP2pDevice> peerList = peers.getDeviceList();
286         final CharSequence[] items = new CharSequence[peerList.size()];
287         int i=0;
288         for (WifiP2pDevice dev: peerList) {
289             items[i++] = dev.deviceName;
290         }
291 
292         new AlertDialog.Builder(this)
293                 .setIcon(android.R.drawable.ic_dialog_info)
294                 .setTitle(R.string.p2p_search_target)
295                 .setItems(items, new android.content.DialogInterface.OnClickListener() {
296             @Override
297             public void onClick(DialogInterface dialog, int which) {
298                 int i=0;
299                 for (WifiP2pDevice dev: peerList) {
300                     if (i == which) {
301                         sTargetAddr = dev.deviceAddress;
302                         mTestCase.setTargetAddress(sTargetAddr);
303                         mTestCase.start(getTestCaseListener());
304                         break;
305                     }
306                     i++;
307                 }
308             }
309         }).show();
310     }
311 
getTestCaseListener()312     private TestCaseListener getTestCaseListener() {
313         return this;
314     }
315 }
316