Expose wifi p2p toggle switch to user

We will not show the Wifi p2p settings to user but the turn on/off capability
is exposed to the user and applications will be expected to let user turn on
and turn off p2p at launch.

Remove the compound button integration from the settings page.

Bug: 5242230
Change-Id: I54ec307304256b051e85201b8bb829d0e5544bfc
diff --git a/res/xml/wireless_settings.xml b/res/xml/wireless_settings.xml
index 81929d0..1e71863 100644
--- a/res/xml/wireless_settings.xml
+++ b/res/xml/wireless_settings.xml
@@ -45,6 +45,11 @@
         android:summary="@string/ndef_push_settings_summary" >
     </PreferenceScreen>
 
+    <CheckBoxPreference
+        android:key="toggle_wifi_p2p"
+        android:title="@string/wifi_p2p_settings_title"
+        android:persistent="false" />
+
     <PreferenceScreen
         android:fragment="com.android.settings.wifi.p2p.WifiP2pSettings"
         android:key="wifi_p2p_settings"
diff --git a/src/com/android/settings/WirelessSettings.java b/src/com/android/settings/WirelessSettings.java
index e1fc882..14ec06b 100644
--- a/src/com/android/settings/WirelessSettings.java
+++ b/src/com/android/settings/WirelessSettings.java
@@ -36,6 +36,7 @@
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.telephony.TelephonyProperties;
 import com.android.settings.nfc.NfcEnabler;
+import com.android.settings.wifi.p2p.WifiP2pEnabler;
 
 public class WirelessSettings extends SettingsPreferenceFragment {
 
@@ -43,6 +44,7 @@
     private static final String KEY_TOGGLE_NFC = "toggle_nfc";
     private static final String KEY_NDEF_PUSH_SETTINGS = "ndef_push_settings";
     private static final String KEY_VPN_SETTINGS = "vpn_settings";
+    private static final String KEY_TOGGLE_WIFI_P2P = "toggle_wifi_p2p";
     private static final String KEY_WIFI_P2P_SETTINGS = "wifi_p2p_settings";
     private static final String KEY_TETHER_SETTINGS = "tether_settings";
     private static final String KEY_PROXY_SETTINGS = "proxy_settings";
@@ -58,6 +60,8 @@
     private NfcEnabler mNfcEnabler;
     private NfcAdapter mNfcAdapter;
 
+    private WifiP2pEnabler mWifiP2pEnabler;
+
     /**
      * Invoked on each preference click in this hierarchy, overrides
      * PreferenceActivity's implementation.  Used to make sure we track the
@@ -98,6 +102,8 @@
         CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
         PreferenceScreen ndefPush = (PreferenceScreen) findPreference(KEY_NDEF_PUSH_SETTINGS);
 
+        CheckBoxPreference wifiP2p = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI_P2P);
+
         mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
         mNfcEnabler = new NfcEnabler(activity, nfc, ndefPush);
 
@@ -133,6 +139,15 @@
             getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
         }
 
+        WifiP2pManager p2p = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
+
+        if (!p2p.isP2pSupported()) {
+            getPreferenceScreen().removePreference(wifiP2p);
+        } else {
+            mWifiP2pEnabler = new WifiP2pEnabler(activity, wifiP2p);
+        }
+
+        //Settings is used for debug alone
         if (!WIFI_P2P_DEBUG) {
             getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
         }
@@ -186,6 +201,10 @@
         if (mNfcEnabler != null) {
             mNfcEnabler.resume();
         }
+
+        if (mWifiP2pEnabler != null) {
+            mWifiP2pEnabler.resume();
+        }
     }
 
     @Override
@@ -196,6 +215,10 @@
         if (mNfcEnabler != null) {
             mNfcEnabler.pause();
         }
+
+        if (mWifiP2pEnabler != null) {
+            mWifiP2pEnabler.pause();
+        }
     }
 
     @Override
diff --git a/src/com/android/settings/wifi/p2p/WifiP2pEnabler.java b/src/com/android/settings/wifi/p2p/WifiP2pEnabler.java
index 608aa02..b812d97 100644
--- a/src/com/android/settings/wifi/p2p/WifiP2pEnabler.java
+++ b/src/com/android/settings/wifi/p2p/WifiP2pEnabler.java
@@ -25,21 +25,19 @@
 import android.net.wifi.p2p.WifiP2pManager;
 import android.os.Handler;
 import android.os.Message;
+import android.preference.CheckBoxPreference;
 import android.preference.Preference;
 import android.provider.Settings;
 import android.util.Log;
-import android.widget.CompoundButton;
-import android.widget.Switch;
 
 /**
  * WifiP2pEnabler is a helper to manage the Wifi p2p on/off
  */
-public class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener {
+public class WifiP2pEnabler implements Preference.OnPreferenceChangeListener {
     private static final String TAG = "WifiP2pEnabler";
 
     private final Context mContext;
-    private Switch mSwitch;
-    private int mWifiP2pState;
+    private final CheckBoxPreference mCheckBox;
     private final IntentFilter mIntentFilter;
     private final Handler mHandler = new WifiP2pHandler();
     private WifiP2pManager mWifiP2pManager;
@@ -57,9 +55,9 @@
         }
     };
 
-    public WifiP2pEnabler(Context context, Switch switch_) {
+    public WifiP2pEnabler(Context context, CheckBoxPreference checkBox) {
         mContext = context;
-        mSwitch = switch_;
+        mCheckBox = checkBox;
 
         mWifiP2pManager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
         if (mWifiP2pManager != null) {
@@ -68,7 +66,7 @@
                 //Failure to set up connection
                 Log.e(TAG, "Failed to set up connection with wifi p2p service");
                 mWifiP2pManager = null;
-                mSwitch.setEnabled(false);
+                mCheckBox.setEnabled(false);
             }
         } else {
             Log.e(TAG, "mWifiP2pManager is null!");
@@ -80,48 +78,39 @@
     public void resume() {
         if (mWifiP2pManager == null) return;
         mContext.registerReceiver(mReceiver, mIntentFilter);
-        mSwitch.setOnCheckedChangeListener(this);
+        mCheckBox.setOnPreferenceChangeListener(this);
     }
 
     public void pause() {
         if (mWifiP2pManager == null) return;
         mContext.unregisterReceiver(mReceiver);
-        mSwitch.setOnCheckedChangeListener(null);
+        mCheckBox.setOnPreferenceChangeListener(null);
     }
 
-    public void setSwitch(Switch switch_) {
-        if (mSwitch == switch_) return;
-        mSwitch.setOnCheckedChangeListener(null);
-        mSwitch = switch_;
-        mSwitch.setOnCheckedChangeListener(this);
+    public boolean onPreferenceChange(Preference preference, Object value) {
 
-        mSwitch.setChecked(mWifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED);
-    }
+        if (mWifiP2pManager == null) return false;
 
-    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-
-        if (mWifiP2pManager == null) return;
-
-        if (isChecked) {
+        mCheckBox.setEnabled(false);
+        final boolean enable = (Boolean) value;
+        if (enable) {
             mWifiP2pManager.enableP2p(mChannel);
         } else {
             mWifiP2pManager.disableP2p(mChannel);
         }
+        return false;
     }
 
     private void handleP2pStateChanged(int state) {
-        mSwitch.setEnabled(true);
+        mCheckBox.setEnabled(true);
         switch (state) {
             case WifiP2pManager.WIFI_P2P_STATE_ENABLED:
-                mWifiP2pState = WifiP2pManager.WIFI_P2P_STATE_ENABLED;
-                mSwitch.setChecked(true);
+                mCheckBox.setChecked(true);
                 break;
             case WifiP2pManager.WIFI_P2P_STATE_DISABLED:
-                mWifiP2pState = WifiP2pManager.WIFI_P2P_STATE_DISABLED;
-                mSwitch.setChecked(false);
+                mCheckBox.setChecked(false);
                 break;
             default:
-                mWifiP2pState = WifiP2pManager.WIFI_P2P_STATE_DISABLED;
                 Log.e(TAG,"Unhandled wifi state " + state);
                 break;
         }
@@ -135,10 +124,10 @@
                     //Failure to set up connection
                     Log.e(TAG, "Lost connection with wifi p2p service");
                     mWifiP2pManager = null;
-                    mSwitch.setEnabled(false);
+                    mCheckBox.setEnabled(false);
                     break;
                 case WifiP2pManager.ENABLE_P2P_FAILED:
-                    mSwitch.setEnabled(true);
+                    mCheckBox.setEnabled(true);
                     break;
                 default:
                     //Ignore
diff --git a/src/com/android/settings/wifi/p2p/WifiP2pSettings.java b/src/com/android/settings/wifi/p2p/WifiP2pSettings.java
index 0656689..d832012 100644
--- a/src/com/android/settings/wifi/p2p/WifiP2pSettings.java
+++ b/src/com/android/settings/wifi/p2p/WifiP2pSettings.java
@@ -42,7 +42,6 @@
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
-import android.widget.Switch;
 
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
@@ -66,7 +65,6 @@
     private final Handler mHandler = new WifiP2pHandler();
     private WifiP2pManager mWifiP2pManager;
     private WifiP2pManager.Channel mChannel;
-    private WifiP2pEnabler mWifiP2pEnabler;
     private WifiP2pDialog mConnectDialog;
     private OnClickListener mConnectListener;
     private OnClickListener mDisconnectListener;
@@ -121,25 +119,6 @@
             Log.e(TAG, "mWifiP2pManager is null !");
         }
 
-        Switch actionBarSwitch = new Switch(activity);
-
-        if (activity instanceof PreferenceActivity) {
-            PreferenceActivity preferenceActivity = (PreferenceActivity) activity;
-            if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) {
-                final int padding = activity.getResources().getDimensionPixelSize(
-                        R.dimen.action_bar_switch_padding);
-                actionBarSwitch.setPadding(0, 0, padding, 0);
-                activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
-                        ActionBar.DISPLAY_SHOW_CUSTOM);
-                activity.getActionBar().setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
-                            ActionBar.LayoutParams.WRAP_CONTENT,
-                            ActionBar.LayoutParams.WRAP_CONTENT,
-                            Gravity.CENTER_VERTICAL | Gravity.RIGHT));
-            }
-        }
-
-        mWifiP2pEnabler = new WifiP2pEnabler(activity, actionBarSwitch);
-
         //connect dialog listener
         mConnectListener = new OnClickListener() {
             @Override
@@ -171,9 +150,6 @@
     public void onResume() {
         super.onResume();
         getActivity().registerReceiver(mReceiver, mIntentFilter);
-        if (mWifiP2pEnabler != null) {
-            mWifiP2pEnabler.resume();
-        }
 
         if (mWifiP2pManager != null) mWifiP2pManager.discoverPeers(mChannel);
     }
@@ -181,9 +157,6 @@
     @Override
     public void onPause() {
         super.onPause();
-        if (mWifiP2pEnabler != null) {
-            mWifiP2pEnabler.pause();
-        }
         getActivity().unregisterReceiver(mReceiver);
     }