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