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