Home | History | Annotate | Download | only in p2p
      1 /*
      2  * Copyright (C) 2011 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 
     17 package com.android.settings.wifi.p2p;
     18 
     19 import android.app.ActionBar;
     20 import android.app.Activity;
     21 import android.app.AlertDialog;
     22 import android.app.Dialog;
     23 import android.content.BroadcastReceiver;
     24 import android.content.Context;
     25 import android.content.DialogInterface;
     26 import android.content.DialogInterface.OnClickListener;
     27 import android.content.Intent;
     28 import android.content.IntentFilter;
     29 import android.net.NetworkInfo;
     30 import android.net.wifi.p2p.WifiP2pConfig;
     31 import android.net.wifi.p2p.WifiP2pInfo;
     32 import android.net.wifi.p2p.WifiP2pDevice;
     33 import android.net.wifi.p2p.WifiP2pDeviceList;
     34 import android.net.wifi.p2p.WifiP2pGroup;
     35 import android.net.wifi.p2p.WifiP2pGroupList;
     36 import android.net.wifi.p2p.WifiP2pManager;
     37 import android.net.wifi.p2p.WifiP2pManager.GroupInfoListener;
     38 import android.net.wifi.p2p.WifiP2pManager.PersistentGroupInfoListener;
     39 import android.net.wifi.WpsInfo;
     40 import android.os.Bundle;
     41 import android.os.Handler;
     42 import android.os.SystemProperties;
     43 import android.preference.Preference;
     44 import android.preference.PreferenceActivity;
     45 import android.preference.PreferenceCategory;
     46 import android.preference.PreferenceGroup;
     47 import android.preference.PreferenceScreen;
     48 import android.text.InputFilter;
     49 import android.text.TextUtils;
     50 import android.util.Log;
     51 import android.view.Gravity;
     52 import android.view.Menu;
     53 import android.view.MenuInflater;
     54 import android.view.MenuItem;
     55 import android.widget.EditText;
     56 import android.widget.Switch;
     57 import android.widget.Toast;
     58 
     59 import com.android.settings.R;
     60 import com.android.settings.SettingsPreferenceFragment;
     61 
     62 import java.util.Arrays;
     63 import java.util.List;
     64 import java.util.Collection;
     65 
     66 /*
     67  * Displays Wi-fi p2p settings UI
     68  */
     69 public class WifiP2pSettings extends SettingsPreferenceFragment
     70         implements PersistentGroupInfoListener, GroupInfoListener {
     71 
     72     private static final String TAG = "WifiP2pSettings";
     73     private static final boolean DBG = false;
     74     private static final int MENU_ID_SEARCH = Menu.FIRST;
     75     private static final int MENU_ID_RENAME = Menu.FIRST + 1;
     76 
     77     private final IntentFilter mIntentFilter = new IntentFilter();
     78     private WifiP2pManager mWifiP2pManager;
     79     private WifiP2pManager.Channel mChannel;
     80     private OnClickListener mRenameListener;
     81     private OnClickListener mDisconnectListener;
     82     private OnClickListener mCancelConnectListener;
     83     private OnClickListener mDeleteGroupListener;
     84     private WifiP2pPeer mSelectedWifiPeer;
     85     private WifiP2pPersistentGroup mSelectedGroup;
     86     private EditText mDeviceNameText;
     87 
     88     private boolean mWifiP2pEnabled;
     89     private boolean mWifiP2pSearching;
     90     private int mConnectedDevices;
     91     private WifiP2pGroup mConnectedGroup;
     92     private boolean mLastGroupFormed = false;
     93 
     94     private PreferenceGroup mPeersGroup;
     95     private PreferenceGroup mPersistentGroup;
     96     private Preference mThisDevicePref;
     97 
     98     private static final int DIALOG_DISCONNECT  = 1;
     99     private static final int DIALOG_CANCEL_CONNECT = 2;
    100     private static final int DIALOG_RENAME = 3;
    101     private static final int DIALOG_DELETE_GROUP = 4;
    102 
    103     private static final String SAVE_DIALOG_PEER = "PEER_STATE";
    104     private static final String SAVE_DEVICE_NAME = "DEV_NAME";
    105 
    106     private WifiP2pDevice mThisDevice;
    107     private WifiP2pDeviceList mPeers = new WifiP2pDeviceList();
    108 
    109     private String mSavedDeviceName;
    110 
    111     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    112         @Override
    113         public void onReceive(Context context, Intent intent) {
    114             String action = intent.getAction();
    115 
    116             if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
    117                 mWifiP2pEnabled = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,
    118                     WifiP2pManager.WIFI_P2P_STATE_DISABLED) == WifiP2pManager.WIFI_P2P_STATE_ENABLED;
    119                 handleP2pStateChanged();
    120             } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
    121                 mPeers = (WifiP2pDeviceList) intent.getParcelableExtra(
    122                         WifiP2pManager.EXTRA_P2P_DEVICE_LIST);
    123                 handlePeersChanged();
    124             } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
    125                 if (mWifiP2pManager == null) return;
    126                 NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(
    127                         WifiP2pManager.EXTRA_NETWORK_INFO);
    128                 WifiP2pInfo wifip2pinfo = (WifiP2pInfo) intent.getParcelableExtra(
    129                         WifiP2pManager.EXTRA_WIFI_P2P_INFO);
    130                 if (mWifiP2pManager != null) {
    131                     mWifiP2pManager.requestGroupInfo(mChannel, WifiP2pSettings.this);
    132                 }
    133                 if (networkInfo.isConnected()) {
    134                     if (DBG) Log.d(TAG, "Connected");
    135                 } else if (mLastGroupFormed != true) {
    136                     //start a search when we are disconnected
    137                     //but not on group removed broadcast event
    138                     startSearch();
    139                 }
    140                 mLastGroupFormed = wifip2pinfo.groupFormed;
    141             } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
    142                 mThisDevice = (WifiP2pDevice) intent.getParcelableExtra(
    143                         WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
    144                 if (DBG) Log.d(TAG, "Update device info: " + mThisDevice);
    145                 updateDevicePref();
    146             } else if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(action)) {
    147                 int discoveryState = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE,
    148                     WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED);
    149                 if (DBG) Log.d(TAG, "Discovery state changed: " + discoveryState);
    150                 if (discoveryState == WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED) {
    151                     updateSearchMenu(true);
    152                 } else {
    153                     updateSearchMenu(false);
    154                 }
    155             } else if (WifiP2pManager.WIFI_P2P_PERSISTENT_GROUPS_CHANGED_ACTION.equals(action)) {
    156                 if (mWifiP2pManager != null) {
    157                     mWifiP2pManager.requestPersistentGroupInfo(mChannel, WifiP2pSettings.this);
    158                 }
    159             }
    160         }
    161     };
    162 
    163     @Override
    164     public void onActivityCreated(Bundle savedInstanceState) {
    165         addPreferencesFromResource(R.xml.wifi_p2p_settings);
    166 
    167         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    168         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    169         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    170         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
    171         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION);
    172         mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PERSISTENT_GROUPS_CHANGED_ACTION);
    173 
    174         final Activity activity = getActivity();
    175         mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    176         if (mWifiP2pManager != null) {
    177             mChannel = mWifiP2pManager.initialize(activity, getActivity().getMainLooper(), null);
    178             if (mChannel == null) {
    179                 //Failure to set up connection
    180                 Log.e(TAG, "Failed to set up connection with wifi p2p service");
    181                 mWifiP2pManager = null;
    182             }
    183         } else {
    184             Log.e(TAG, "mWifiP2pManager is null !");
    185         }
    186 
    187         if (savedInstanceState != null && savedInstanceState.containsKey(SAVE_DIALOG_PEER)) {
    188             WifiP2pDevice device = savedInstanceState.getParcelable(SAVE_DIALOG_PEER);
    189             mSelectedWifiPeer = new WifiP2pPeer(getActivity(), device);
    190         }
    191         if (savedInstanceState != null && savedInstanceState.containsKey(SAVE_DEVICE_NAME)) {
    192             mSavedDeviceName = savedInstanceState.getString(SAVE_DEVICE_NAME);
    193         }
    194 
    195         mRenameListener = new OnClickListener() {
    196             @Override
    197             public void onClick(DialogInterface dialog, int which) {
    198                 if (which == DialogInterface.BUTTON_POSITIVE) {
    199                     if (mWifiP2pManager != null) {
    200                         mWifiP2pManager.setDeviceName(mChannel,
    201                                 mDeviceNameText.getText().toString(),
    202                                 new WifiP2pManager.ActionListener() {
    203                             public void onSuccess() {
    204                                 if (DBG) Log.d(TAG, " device rename success");
    205                             }
    206                             public void onFailure(int reason) {
    207                                 Toast.makeText(getActivity(),
    208                                         R.string.wifi_p2p_failed_rename_message,
    209                                         Toast.LENGTH_LONG).show();
    210                             }
    211                         });
    212                     }
    213                 }
    214             }
    215         };
    216 
    217         //disconnect dialog listener
    218         mDisconnectListener = new OnClickListener() {
    219             @Override
    220             public void onClick(DialogInterface dialog, int which) {
    221                 if (which == DialogInterface.BUTTON_POSITIVE) {
    222                     if (mWifiP2pManager != null) {
    223                         mWifiP2pManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() {
    224                             public void onSuccess() {
    225                                 if (DBG) Log.d(TAG, " remove group success");
    226                             }
    227                             public void onFailure(int reason) {
    228                                 if (DBG) Log.d(TAG, " remove group fail " + reason);
    229                             }
    230                         });
    231                     }
    232                 }
    233             }
    234         };
    235 
    236         //cancel connect dialog listener
    237         mCancelConnectListener = new OnClickListener() {
    238             @Override
    239             public void onClick(DialogInterface dialog, int which) {
    240                 if (which == DialogInterface.BUTTON_POSITIVE) {
    241                     if (mWifiP2pManager != null) {
    242                         mWifiP2pManager.cancelConnect(mChannel,
    243                                 new WifiP2pManager.ActionListener() {
    244                             public void onSuccess() {
    245                                 if (DBG) Log.d(TAG, " cancel connect success");
    246                             }
    247                             public void onFailure(int reason) {
    248                                 if (DBG) Log.d(TAG, " cancel connect fail " + reason);
    249                             }
    250                         });
    251                     }
    252                 }
    253             }
    254         };
    255 
    256         //delete persistent group dialog listener
    257         mDeleteGroupListener = new OnClickListener() {
    258             @Override
    259             public void onClick(DialogInterface dialog, int which) {
    260                 if (which == DialogInterface.BUTTON_POSITIVE) {
    261                     if (mWifiP2pManager != null) {
    262                         mWifiP2pManager.deletePersistentGroup(mChannel,
    263                                 mSelectedGroup.getNetworkId(),
    264                                 new WifiP2pManager.ActionListener() {
    265                             public void onSuccess() {
    266                                 if (DBG) Log.d(TAG, " delete group success");
    267                             }
    268                             public void onFailure(int reason) {
    269                                 if (DBG) Log.d(TAG, " delete group fail " + reason);
    270                             }
    271                         });
    272                     }
    273                 }
    274             }
    275         };
    276 
    277         setHasOptionsMenu(true);
    278 
    279         final PreferenceScreen preferenceScreen = getPreferenceScreen();
    280         preferenceScreen.removeAll();
    281 
    282         preferenceScreen.setOrderingAsAdded(true);
    283         mThisDevicePref = new Preference(getActivity());
    284         preferenceScreen.addPreference(mThisDevicePref);
    285 
    286         mPeersGroup = new PreferenceCategory(getActivity());
    287         mPeersGroup.setTitle(R.string.wifi_p2p_peer_devices);
    288 
    289         mPersistentGroup = new PreferenceCategory(getActivity());
    290         mPersistentGroup.setTitle(R.string.wifi_p2p_remembered_groups);
    291 
    292         super.onActivityCreated(savedInstanceState);
    293     }
    294 
    295     @Override
    296     public void onResume() {
    297         super.onResume();
    298         getActivity().registerReceiver(mReceiver, mIntentFilter);
    299     }
    300 
    301     @Override
    302     public void onPause() {
    303         super.onPause();
    304         mWifiP2pManager.stopPeerDiscovery(mChannel, null);
    305         getActivity().unregisterReceiver(mReceiver);
    306     }
    307 
    308     @Override
    309     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    310         int textId = mWifiP2pSearching ? R.string.wifi_p2p_menu_searching :
    311                 R.string.wifi_p2p_menu_search;
    312         menu.add(Menu.NONE, MENU_ID_SEARCH, 0, textId)
    313             .setEnabled(mWifiP2pEnabled)
    314             .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    315         menu.add(Menu.NONE, MENU_ID_RENAME, 0, R.string.wifi_p2p_menu_rename)
    316             .setEnabled(mWifiP2pEnabled)
    317             .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    318         super.onCreateOptionsMenu(menu, inflater);
    319     }
    320 
    321     @Override
    322     public void onPrepareOptionsMenu(Menu menu) {
    323         MenuItem searchMenu = menu.findItem(MENU_ID_SEARCH);
    324         MenuItem renameMenu = menu.findItem(MENU_ID_RENAME);
    325         if (mWifiP2pEnabled) {
    326             searchMenu.setEnabled(true);
    327             renameMenu.setEnabled(true);
    328         } else {
    329             searchMenu.setEnabled(false);
    330             renameMenu.setEnabled(false);
    331         }
    332 
    333         if (mWifiP2pSearching) {
    334             searchMenu.setTitle(R.string.wifi_p2p_menu_searching);
    335         } else {
    336             searchMenu.setTitle(R.string.wifi_p2p_menu_search);
    337         }
    338     }
    339 
    340     @Override
    341     public boolean onOptionsItemSelected(MenuItem item) {
    342         switch (item.getItemId()) {
    343             case MENU_ID_SEARCH:
    344                 startSearch();
    345                 return true;
    346             case MENU_ID_RENAME:
    347                 showDialog(DIALOG_RENAME);
    348                 return true;
    349         }
    350         return super.onOptionsItemSelected(item);
    351     }
    352 
    353     @Override
    354     public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
    355         if (preference instanceof WifiP2pPeer) {
    356             mSelectedWifiPeer = (WifiP2pPeer) preference;
    357             if (mSelectedWifiPeer.device.status == WifiP2pDevice.CONNECTED) {
    358                 showDialog(DIALOG_DISCONNECT);
    359             } else if (mSelectedWifiPeer.device.status == WifiP2pDevice.INVITED) {
    360                 showDialog(DIALOG_CANCEL_CONNECT);
    361             } else {
    362                 WifiP2pConfig config = new WifiP2pConfig();
    363                 config.deviceAddress = mSelectedWifiPeer.device.deviceAddress;
    364 
    365                 int forceWps = SystemProperties.getInt("wifidirect.wps", -1);
    366 
    367                 if (forceWps != -1) {
    368                     config.wps.setup = forceWps;
    369                 } else {
    370                     if (mSelectedWifiPeer.device.wpsPbcSupported()) {
    371                         config.wps.setup = WpsInfo.PBC;
    372                     } else if (mSelectedWifiPeer.device.wpsKeypadSupported()) {
    373                         config.wps.setup = WpsInfo.KEYPAD;
    374                     } else {
    375                         config.wps.setup = WpsInfo.DISPLAY;
    376                     }
    377                 }
    378 
    379                 mWifiP2pManager.connect(mChannel, config,
    380                         new WifiP2pManager.ActionListener() {
    381                             public void onSuccess() {
    382                                 if (DBG) Log.d(TAG, " connect success");
    383                             }
    384                             public void onFailure(int reason) {
    385                                 Log.e(TAG, " connect fail " + reason);
    386                                 Toast.makeText(getActivity(),
    387                                         R.string.wifi_p2p_failed_connect_message,
    388                                         Toast.LENGTH_SHORT).show();
    389                             }
    390                     });
    391             }
    392         } else if (preference instanceof WifiP2pPersistentGroup) {
    393             mSelectedGroup = (WifiP2pPersistentGroup) preference;
    394             showDialog(DIALOG_DELETE_GROUP);
    395         }
    396         return super.onPreferenceTreeClick(screen, preference);
    397     }
    398 
    399     @Override
    400     public Dialog onCreateDialog(int id) {
    401         if (id == DIALOG_DISCONNECT) {
    402             String deviceName = TextUtils.isEmpty(mSelectedWifiPeer.device.deviceName) ?
    403                     mSelectedWifiPeer.device.deviceAddress :
    404                     mSelectedWifiPeer.device.deviceName;
    405             String msg;
    406             if (mConnectedDevices > 1) {
    407                 msg = getActivity().getString(R.string.wifi_p2p_disconnect_multiple_message,
    408                         deviceName, mConnectedDevices - 1);
    409             } else {
    410                 msg = getActivity().getString(R.string.wifi_p2p_disconnect_message, deviceName);
    411             }
    412             AlertDialog dialog = new AlertDialog.Builder(getActivity())
    413                 .setTitle(R.string.wifi_p2p_disconnect_title)
    414                 .setMessage(msg)
    415                 .setPositiveButton(getActivity().getString(R.string.dlg_ok), mDisconnectListener)
    416                 .setNegativeButton(getActivity().getString(R.string.dlg_cancel), null)
    417                 .create();
    418             return dialog;
    419         } else if (id == DIALOG_CANCEL_CONNECT) {
    420             int stringId = R.string.wifi_p2p_cancel_connect_message;
    421             String deviceName = TextUtils.isEmpty(mSelectedWifiPeer.device.deviceName) ?
    422                     mSelectedWifiPeer.device.deviceAddress :
    423                     mSelectedWifiPeer.device.deviceName;
    424 
    425             AlertDialog dialog = new AlertDialog.Builder(getActivity())
    426                 .setTitle(R.string.wifi_p2p_cancel_connect_title)
    427                 .setMessage(getActivity().getString(stringId, deviceName))
    428                 .setPositiveButton(getActivity().getString(R.string.dlg_ok), mCancelConnectListener)
    429                 .setNegativeButton(getActivity().getString(R.string.dlg_cancel), null)
    430                 .create();
    431             return dialog;
    432         } else if (id == DIALOG_RENAME) {
    433             mDeviceNameText = new EditText(getActivity());
    434             mDeviceNameText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(30)});
    435             if (mSavedDeviceName != null) {
    436                 mDeviceNameText.setText(mSavedDeviceName);
    437                 mDeviceNameText.setSelection(mSavedDeviceName.length());
    438             } else if (mThisDevice != null && !TextUtils.isEmpty(mThisDevice.deviceName)) {
    439                 mDeviceNameText.setText(mThisDevice.deviceName);
    440                 mDeviceNameText.setSelection(0, mThisDevice.deviceName.length());
    441             }
    442             mSavedDeviceName = null;
    443             AlertDialog dialog = new AlertDialog.Builder(getActivity())
    444                 .setTitle(R.string.wifi_p2p_menu_rename)
    445                 .setView(mDeviceNameText)
    446                 .setPositiveButton(getActivity().getString(R.string.dlg_ok), mRenameListener)
    447                 .setNegativeButton(getActivity().getString(R.string.dlg_cancel), null)
    448                 .create();
    449             return dialog;
    450         } else if (id == DIALOG_DELETE_GROUP) {
    451             int stringId = R.string.wifi_p2p_delete_group_message;
    452 
    453             AlertDialog dialog = new AlertDialog.Builder(getActivity())
    454                 .setMessage(getActivity().getString(stringId))
    455                 .setPositiveButton(getActivity().getString(R.string.dlg_ok), mDeleteGroupListener)
    456                 .setNegativeButton(getActivity().getString(R.string.dlg_cancel), null)
    457                 .create();
    458             return dialog;
    459         }
    460         return null;
    461     }
    462 
    463     @Override
    464     public void onSaveInstanceState(Bundle outState) {
    465         if (mSelectedWifiPeer != null) {
    466             outState.putParcelable(SAVE_DIALOG_PEER, mSelectedWifiPeer.device);
    467         }
    468         if (mDeviceNameText != null) {
    469             outState.putString(SAVE_DEVICE_NAME, mDeviceNameText.getText().toString());
    470         }
    471     }
    472 
    473     private void handlePeersChanged() {
    474         mPeersGroup.removeAll();
    475 
    476         mConnectedDevices = 0;
    477         if (DBG) Log.d(TAG, "List of available peers");
    478         for (WifiP2pDevice peer: mPeers.getDeviceList()) {
    479             if (DBG) Log.d(TAG, "-> " + peer);
    480             mPeersGroup.addPreference(new WifiP2pPeer(getActivity(), peer));
    481             if (peer.status == WifiP2pDevice.CONNECTED) mConnectedDevices++;
    482         }
    483         if (DBG) Log.d(TAG, " mConnectedDevices " + mConnectedDevices);
    484     }
    485 
    486     public void onPersistentGroupInfoAvailable(WifiP2pGroupList groups) {
    487         mPersistentGroup.removeAll();
    488 
    489         for (WifiP2pGroup group: groups.getGroupList()) {
    490             if (DBG) Log.d(TAG, " group " + group);
    491             mPersistentGroup.addPreference(new WifiP2pPersistentGroup(getActivity(), group));
    492         }
    493     }
    494 
    495     public void onGroupInfoAvailable(WifiP2pGroup group) {
    496         if (DBG) Log.d(TAG, " group " + group);
    497         mConnectedGroup = group;
    498         updateDevicePref();
    499     }
    500 
    501     private void handleP2pStateChanged() {
    502         updateSearchMenu(false);
    503         if (mWifiP2pEnabled) {
    504             final PreferenceScreen preferenceScreen = getPreferenceScreen();
    505             preferenceScreen.removeAll();
    506 
    507             preferenceScreen.setOrderingAsAdded(true);
    508             preferenceScreen.addPreference(mThisDevicePref);
    509 
    510             mPeersGroup.setEnabled(true);
    511             preferenceScreen.addPreference(mPeersGroup);
    512 
    513             mPersistentGroup.setEnabled(true);
    514             preferenceScreen.addPreference(mPersistentGroup);
    515         }
    516     }
    517 
    518     private void updateSearchMenu(boolean searching) {
    519        mWifiP2pSearching = searching;
    520        Activity activity = getActivity();
    521        if (activity != null) activity.invalidateOptionsMenu();
    522     }
    523 
    524     private void startSearch() {
    525         if (mWifiP2pManager != null && !mWifiP2pSearching) {
    526             mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
    527                 public void onSuccess() {
    528                 }
    529                 public void onFailure(int reason) {
    530                     if (DBG) Log.d(TAG, " discover fail " + reason);
    531                 }
    532             });
    533         }
    534     }
    535 
    536     private void updateDevicePref() {
    537         if (mThisDevice != null) {
    538             if (TextUtils.isEmpty(mThisDevice.deviceName)) {
    539                 mThisDevicePref.setTitle(mThisDevice.deviceAddress);
    540             } else {
    541                 mThisDevicePref.setTitle(mThisDevice.deviceName);
    542             }
    543 
    544             mThisDevicePref.setPersistent(false);
    545             mThisDevicePref.setEnabled(true);
    546             mThisDevicePref.setSelectable(false);
    547         }
    548     }
    549 }
    550