Home | History | Annotate | Download | only in p2p
      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 onResume() {
    130         super.onResume();
    131         /*
    132          * If the target device is NOT set, search targets and show
    133          * the target device list on the dialog.
    134          * After the user selection, the specified test will be executed.
    135          */
    136         if (sTargetAddr == null) {
    137             searchTarget();
    138             return;
    139         }
    140 
    141         mTestCase.setTargetAddress(sTargetAddr);
    142         mTestCase.start(this);
    143     }
    144 
    145     @Override
    146     protected void onPause() {
    147         super.onPause();
    148         if (mTimer != null) {
    149             mTimer.cancel();
    150             mTimer = null;
    151         }
    152         mTestCase.stop();
    153     }
    154 
    155     @Override
    156     public String getTestId() {
    157         return mTestCase.getTestId();
    158     }
    159 
    160     @Override
    161     public void onTestStarted() {
    162         mHandler.post(new Runnable() {
    163             @Override
    164             public void run() {
    165                 mProgress.setVisibility(ProgressBar.VISIBLE);
    166             }
    167         });
    168     }
    169 
    170     public void onTestMsgReceived(final String msg) {
    171         mHandler.post(new Runnable() {
    172             @Override
    173             public void run() {
    174                 mTextView.setText(msg);
    175             }
    176         });
    177     }
    178 
    179     @Override
    180     public void onTestFailed(final String reason) {
    181         // if test failed, forget target device address.
    182         sTargetAddr = null;
    183 
    184         mHandler.post(new Runnable() {
    185             @Override
    186             public void run() {
    187                 mProgress.setVisibility(ProgressBar.INVISIBLE);
    188                 mTextView.setText(reason);
    189             }
    190         });
    191     }
    192 
    193     @Override
    194     public void onTestSuccess() {
    195         mHandler.post(new Runnable() {
    196             @Override
    197             public void run() {
    198                 mProgress.setVisibility(ProgressBar.INVISIBLE);
    199                 mTextView.setText(R.string.p2p_result_success);
    200                 getPassButton().setEnabled(true);
    201             }
    202         });
    203     }
    204 
    205     /**
    206      * Search devices and show the found devices on the dialog.
    207      * After user selection, the specified test will be executed.
    208      */
    209     private void searchTarget() {
    210         // Discover peers.
    211         mP2pMgr.discoverPeers(mChannel, null);
    212         mTextView.setText(R.string.p2p_searching_target);
    213         mProgress.setVisibility(ProgressBar.VISIBLE);
    214 
    215         /*
    216          * Show the peer list dialog after searching devices for 8 seconds.
    217          */
    218         if (mTimer == null) {
    219             mTimer = new Timer(true);
    220         }
    221         mTimer.schedule(new TimerTask() {
    222             @Override
    223             public void run() {
    224                 mP2pMgr.requestPeers(mChannel, new PeerListListener() {
    225                     @Override
    226                     public void onPeersAvailable(WifiP2pDeviceList _peers) {
    227                         final WifiP2pDeviceList peers = _peers;
    228                         /*
    229                          * Need to show dialog in GUI thread.
    230                          */
    231                         mHandler.post(new Runnable() {
    232                             @Override
    233                             public void run() {
    234                                 mProgress.setVisibility(ProgressBar.INVISIBLE);
    235                                 if (peers.getDeviceList().size() == 0) {
    236                                     mTextView.setText(
    237                                             R.string.p2p_target_not_found_error);
    238                                 } else {
    239                                     showSelectTargetDialog(peers);
    240                                 }
    241                             }
    242                         });
    243                     }
    244                 });
    245             }
    246         }, SEARCH_TARGET_TIMEOUT);
    247     }
    248 
    249     /**
    250      * Show the found device list on the dialog.
    251      * The target device address selected by user is stored in {@link #mTargetAddr}.
    252      * After user selection, the specified test will be executed.
    253      * @param peers
    254      * @param testIndex
    255      */
    256     private void showSelectTargetDialog(WifiP2pDeviceList peers) {
    257         final Collection<WifiP2pDevice> peerList = peers.getDeviceList();
    258         final CharSequence[] items = new CharSequence[peerList.size()];
    259         int i=0;
    260         for (WifiP2pDevice dev: peerList) {
    261             items[i++] = dev.deviceName;
    262         }
    263 
    264         new AlertDialog.Builder(this)
    265                 .setIcon(android.R.drawable.ic_dialog_info)
    266                 .setTitle(R.string.p2p_search_target)
    267                 .setItems(items, new android.content.DialogInterface.OnClickListener() {
    268             @Override
    269             public void onClick(DialogInterface dialog, int which) {
    270                 int i=0;
    271                 for (WifiP2pDevice dev: peerList) {
    272                     if (i == which) {
    273                         sTargetAddr = dev.deviceAddress;
    274                         mTestCase.setTargetAddress(sTargetAddr);
    275                         mTestCase.start(getTestCaseListener());
    276                         break;
    277                     }
    278                     i++;
    279                 }
    280             }
    281         }).show();
    282     }
    283 
    284     private TestCaseListener getTestCaseListener() {
    285         return this;
    286     }
    287 }
    288