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.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
22 import android.net.wifi.p2p.WifiP2pDevice;
23 import android.net.wifi.p2p.WifiP2pManager;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.view.WindowManager;
27 import android.widget.TextView;
28 
29 import com.android.cts.verifier.PassFailButtons;
30 import com.android.cts.verifier.R;
31 import com.android.cts.verifier.R.id;
32 import com.android.cts.verifier.p2p.testcase.TestCase;
33 import com.android.cts.verifier.p2p.testcase.TestCase.TestCaseListener;
34 
35 /**
36  * A base class for responder test activity.
37  *
38  * This class provides the feature to show own device name and test results.
39  * A responder test activity just have to extend this class and implement getTestCase()
40  * and getReadyMsgId().
41  */
42 public abstract class ResponderTestActivity extends PassFailButtons.Activity
43     implements TestCaseListener {
44 
45     /*
46      * Test case to be executed
47      */
48     private TestCase mTestCase;
49 
50     /*
51      * Text view to show the result of the test.
52      */
53     private TextView mTextView;
54 
55     /*
56      * Text view to show own device name.
57      * It would be helpful to select this device on the other device.
58      */
59     private TextView mMyDeviceView;
60 
61     /*
62      * BroadcastReceiver to obtain own device information.
63      */
64     private final P2pBroadcastReceiver mReceiver = new P2pBroadcastReceiver();
65     private final IntentFilter mIntentFilter = new IntentFilter();
66     private Handler mHandler = new Handler();
67 
68     /**
69      * Constructor
70      */
ResponderTestActivity()71     public ResponderTestActivity() {
72         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
73     }
74 
getTestCase(Context context)75     abstract protected TestCase getTestCase(Context context);
76 
getReadyMsgId()77     abstract protected int getReadyMsgId();
78 
79     @Override
onCreate(Bundle savedInstanceState)80     protected void onCreate(Bundle savedInstanceState) {
81         super.onCreate(savedInstanceState);
82         setContentView(R.layout.p2p_responder_main);
83         setPassFailButtonClickListeners();
84         getPassButton().setEnabled(false);
85 
86         // Get UI component.
87         mTextView = (TextView) findViewById(id.p2p_resp_text);
88         mMyDeviceView = (TextView) findViewById(id.p2p_my_device);
89 
90         // Initialize test components.
91         mTestCase = getTestCase(this);
92 
93         // keep screen on while this activity is front view.
94         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
95     }
96 
97     @Override
onResume()98     protected void onResume() {
99         super.onResume();
100         mTestCase.start(this);
101         registerReceiver(mReceiver, mIntentFilter);
102     }
103 
104     @Override
onPause()105     protected void onPause() {
106         super.onPause();
107         mTestCase.stop();
108         unregisterReceiver(mReceiver);
109     }
110 
111     /**
112      * Receive the WIFI_P2P_THIS_DEVICE_CHANGED_ACTION action and show the
113      * this device information in the text view.
114      */
115     class P2pBroadcastReceiver extends BroadcastReceiver {
116         @Override
onReceive(Context context, Intent intent)117         public void onReceive(Context context, Intent intent) {
118             String action = intent.getAction();
119             if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
120                 final WifiP2pDevice myDevice = (WifiP2pDevice) intent.getParcelableExtra(
121                         WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
122                 if (myDevice != null) {
123                     // need to show in the GUI thread.
124                     mHandler.post(new Runnable() {
125                         @Override
126                         public void run() {
127                             mMyDeviceView.setText("Device Name: " + myDevice.deviceName);
128                         }
129                     });
130                 }
131             }
132         }
133     }
134 
135     @Override
onTestStarted()136     public void onTestStarted() {
137     }
138 
onTestMsgReceived(final String msg)139     public void onTestMsgReceived(final String msg) {
140         mHandler.post(new Runnable() {
141             @Override
142             public void run() {
143                 mTextView.setText(msg);
144             }
145         });
146     }
147 
148     @Override
onTestSuccess()149     public void onTestSuccess() {
150         mHandler.post(new Runnable() {
151             @Override
152             public void run() {
153                 mTextView.setText(getReadyMsgId());
154                 getPassButton().setEnabled(true);
155             }
156         });
157     }
158 
159     @Override
onTestFailed(final String reason)160     public void onTestFailed(final String reason) {
161         mHandler.post(new Runnable() {
162             @Override
163             public void run() {
164                 mTextView.setText(reason);
165             }
166         });
167     }
168 }
169