Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f38534f4 authored by Irfan Sheriff's avatar Irfan Sheriff Committed by Android (Google) Code Review
Browse files

Merge "Expose wifi p2p toggle switch to user"

parents ca6d987a 1610a74a
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -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"
+23 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.widget.Switch;
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 @@ public class WirelessSettings extends SettingsPreferenceFragment {
    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 @@ public class WirelessSettings extends SettingsPreferenceFragment {
    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 @@ public class WirelessSettings extends SettingsPreferenceFragment {
        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 @@ public class WirelessSettings extends SettingsPreferenceFragment {
            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 @@ public class WirelessSettings extends SettingsPreferenceFragment {
        if (mNfcEnabler != null) {
            mNfcEnabler.resume();
        }

        if (mWifiP2pEnabler != null) {
            mWifiP2pEnabler.resume();
        }
    }

    @Override
@@ -196,6 +215,10 @@ public class WirelessSettings extends SettingsPreferenceFragment {
        if (mNfcEnabler != null) {
            mNfcEnabler.pause();
        }

        if (mWifiP2pEnabler != null) {
            mWifiP2pEnabler.pause();
        }
    }

    @Override
+19 −30
Original line number Diff line number Diff line
@@ -25,21 +25,19 @@ import android.content.IntentFilter;
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 class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener {
        }
    };

    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 @@ public class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener {
                //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 class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener {
    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);
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (mWifiP2pManager == null) return;
        if (mWifiP2pManager == null) return false;

        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 @@ public class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener {
                    //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
+0 −27
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ import android.view.Gravity;
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 @@ public class WifiP2pSettings extends SettingsPreferenceFragment {
    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 @@ public class WifiP2pSettings extends SettingsPreferenceFragment {
            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 class WifiP2pSettings extends SettingsPreferenceFragment {
    public void onResume() {
        super.onResume();
        getActivity().registerReceiver(mReceiver, mIntentFilter);
        if (mWifiP2pEnabler != null) {
            mWifiP2pEnabler.resume();
        }

        if (mWifiP2pManager != null) mWifiP2pManager.discoverPeers(mChannel);
    }
@@ -181,9 +157,6 @@ public class WifiP2pSettings extends SettingsPreferenceFragment {
    @Override
    public void onPause() {
        super.onPause();
        if (mWifiP2pEnabler != null) {
            mWifiP2pEnabler.pause();
        }
        getActivity().unregisterReceiver(mReceiver);
    }