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 android.app.AlertDialog;
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.DialogInterface;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.database.DataSetObserver;
     25 import android.net.wifi.p2p.WifiP2pManager;
     26 import android.os.Bundle;
     27 import android.provider.Settings;
     28 import android.view.View;
     29 import android.widget.ListView;
     30 
     31 import com.android.cts.verifier.ArrayTestListAdapter;
     32 import com.android.cts.verifier.PassFailButtons;
     33 import com.android.cts.verifier.R;
     34 import com.android.cts.verifier.TestListAdapter.TestListItem;
     35 
     36 /**
     37  * Activity that lists all the WiFi Direct tests.
     38  */
     39 public class P2pTestListActivity extends PassFailButtons.TestListActivity {
     40 
     41     /*
     42      * BroadcastReceiver to check p2p status.
     43      * If WiFi Direct is disabled, show the dialog message to user.
     44      */
     45     private final P2pBroadcastReceiver mReceiver = new P2pBroadcastReceiver();
     46     private final IntentFilter mIntentFilter = new IntentFilter();
     47     private boolean mIsP2pEnabled = false;
     48 
     49     /**
     50      * Constructor
     51      */
     52     public P2pTestListActivity() {
     53         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
     54     }
     55 
     56     @Override
     57     protected void onCreate(Bundle savedInstanceState) {
     58         super.onCreate(savedInstanceState);
     59         setContentView(R.layout.pass_fail_list);
     60         setInfoResources(R.string.p2p_test, R.string.p2p_test_info, 0);
     61         setPassFailButtonClickListeners();
     62 
     63         getPassButton().setEnabled(false);
     64 
     65         /**
     66          * Added WiFiDirect test activity to the list.
     67          */
     68         ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
     69 
     70         adapter.add(TestListItem.newCategory(this, R.string.p2p_group_formation));
     71         adapter.add(TestListItem.newTest(this,
     72                 R.string.p2p_go_neg_responder_test,
     73                 GoNegResponderTestActivity.class.getName(),
     74                 new Intent(this, GoNegResponderTestActivity.class), null));
     75         adapter.add(TestListItem.newTest(this,
     76                 R.string.p2p_go_neg_requester_test,
     77                 GoNegRequesterTestListActivity.class.getName(),
     78                 new Intent(this, GoNegRequesterTestListActivity.class), null));
     79 
     80         adapter.add(TestListItem.newCategory(this, R.string.p2p_join));
     81         adapter.add(TestListItem.newTest(this,
     82                 R.string.p2p_group_owner_test,
     83                 GoTestActivity.class.getName(),
     84                 new Intent(this, GoTestActivity.class), null));
     85         adapter.add(TestListItem.newTest(this,
     86                 R.string.p2p_group_client_test,
     87                 P2pClientTestListActivity.class.getName(),
     88                 new Intent(this, P2pClientTestListActivity.class), null));
     89 
     90         adapter.add(TestListItem.newCategory(this, R.string.p2p_service_discovery));
     91         adapter.add(TestListItem.newTest(this,
     92                 R.string.p2p_service_discovery_responder_test,
     93                 ServiceResponderTestActivity.class.getName(),
     94                 new Intent(this, ServiceResponderTestActivity.class), null));
     95         adapter.add(TestListItem.newTest(this,
     96                 R.string.p2p_service_discovery_requester_test,
     97                 ServiceRequesterTestListActivity.class.getName(),
     98                 new Intent(this, ServiceRequesterTestListActivity.class), null));
     99 
    100 
    101         adapter.registerDataSetObserver(new DataSetObserver() {
    102             @Override
    103             public void onChanged() {
    104                 updatePassButton();
    105             }
    106         });
    107 
    108         setTestListAdapter(adapter);
    109     }
    110 
    111     @Override
    112     protected void onResume() {
    113         super.onResume();
    114         registerReceiver(mReceiver, mIntentFilter);
    115     }
    116 
    117     @Override
    118     protected void onPause() {
    119         super.onResume();
    120         unregisterReceiver(mReceiver);
    121     }
    122 
    123     /**
    124      * Launch the activity when its {@link ListView} item is clicked.
    125      * If WiFi Direct is disabled, show the dialog to jump to system setting activity.
    126      **/
    127     @Override
    128     protected void handleItemClick(ListView listView, View view, int position, long id) {
    129         if (!mIsP2pEnabled) {
    130             showP2pEnableDialog();
    131             return;
    132         }
    133         super.handleItemClick(listView, view, position, id);
    134     }
    135 
    136     /**
    137      * Show the dialog to jump to system settings in order to enable
    138      * WiFi Direct.
    139      */
    140     private void showP2pEnableDialog() {
    141         AlertDialog.Builder builder = new AlertDialog.Builder(this);
    142         builder.setIcon(android.R.drawable.ic_dialog_alert);
    143         builder.setTitle(R.string.p2p_not_enabled);
    144         builder.setMessage(R.string.p2p_not_enabled_message);
    145         builder.setPositiveButton(R.string.p2p_settings,
    146                 new DialogInterface.OnClickListener() {
    147             @Override
    148             public void onClick(DialogInterface dialog, int which) {
    149                 startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
    150             }
    151         });
    152         builder.create().show();
    153     }
    154 
    155     /**
    156      * Receive the WIFI_P2P_STATE_CHANGED_ACTION action.
    157      */
    158     class P2pBroadcastReceiver extends BroadcastReceiver {
    159         @Override
    160         public void onReceive(Context context, Intent intent) {
    161             String action = intent.getAction();
    162             if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
    163                 int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
    164                 if ((state == WifiP2pManager.WIFI_P2P_STATE_ENABLED)) {
    165                     mIsP2pEnabled = true;
    166                 }
    167             }
    168         }
    169     }
    170 }
    171