Merge "Expose wifi p2p toggle switch to user"
This commit is contained in:
committed by
Android (Google) Code Review
commit
f38534f4b5
@@ -45,6 +45,11 @@
|
|||||||
android:summary="@string/ndef_push_settings_summary" >
|
android:summary="@string/ndef_push_settings_summary" >
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
|
||||||
|
<CheckBoxPreference
|
||||||
|
android:key="toggle_wifi_p2p"
|
||||||
|
android:title="@string/wifi_p2p_settings_title"
|
||||||
|
android:persistent="false" />
|
||||||
|
|
||||||
<PreferenceScreen
|
<PreferenceScreen
|
||||||
android:fragment="com.android.settings.wifi.p2p.WifiP2pSettings"
|
android:fragment="com.android.settings.wifi.p2p.WifiP2pSettings"
|
||||||
android:key="wifi_p2p_settings"
|
android:key="wifi_p2p_settings"
|
||||||
|
@@ -36,6 +36,7 @@ import android.widget.Switch;
|
|||||||
import com.android.internal.telephony.TelephonyIntents;
|
import com.android.internal.telephony.TelephonyIntents;
|
||||||
import com.android.internal.telephony.TelephonyProperties;
|
import com.android.internal.telephony.TelephonyProperties;
|
||||||
import com.android.settings.nfc.NfcEnabler;
|
import com.android.settings.nfc.NfcEnabler;
|
||||||
|
import com.android.settings.wifi.p2p.WifiP2pEnabler;
|
||||||
|
|
||||||
public class WirelessSettings extends SettingsPreferenceFragment {
|
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_TOGGLE_NFC = "toggle_nfc";
|
||||||
private static final String KEY_NDEF_PUSH_SETTINGS = "ndef_push_settings";
|
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_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_WIFI_P2P_SETTINGS = "wifi_p2p_settings";
|
||||||
private static final String KEY_TETHER_SETTINGS = "tether_settings";
|
private static final String KEY_TETHER_SETTINGS = "tether_settings";
|
||||||
private static final String KEY_PROXY_SETTINGS = "proxy_settings";
|
private static final String KEY_PROXY_SETTINGS = "proxy_settings";
|
||||||
@@ -58,6 +60,8 @@ public class WirelessSettings extends SettingsPreferenceFragment {
|
|||||||
private NfcEnabler mNfcEnabler;
|
private NfcEnabler mNfcEnabler;
|
||||||
private NfcAdapter mNfcAdapter;
|
private NfcAdapter mNfcAdapter;
|
||||||
|
|
||||||
|
private WifiP2pEnabler mWifiP2pEnabler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invoked on each preference click in this hierarchy, overrides
|
* Invoked on each preference click in this hierarchy, overrides
|
||||||
* PreferenceActivity's implementation. Used to make sure we track the
|
* 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);
|
CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
|
||||||
PreferenceScreen ndefPush = (PreferenceScreen) findPreference(KEY_NDEF_PUSH_SETTINGS);
|
PreferenceScreen ndefPush = (PreferenceScreen) findPreference(KEY_NDEF_PUSH_SETTINGS);
|
||||||
|
|
||||||
|
CheckBoxPreference wifiP2p = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI_P2P);
|
||||||
|
|
||||||
mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
|
mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
|
||||||
mNfcEnabler = new NfcEnabler(activity, nfc, ndefPush);
|
mNfcEnabler = new NfcEnabler(activity, nfc, ndefPush);
|
||||||
|
|
||||||
@@ -133,6 +139,15 @@ public class WirelessSettings extends SettingsPreferenceFragment {
|
|||||||
getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
|
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) {
|
if (!WIFI_P2P_DEBUG) {
|
||||||
getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
|
getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
|
||||||
}
|
}
|
||||||
@@ -186,6 +201,10 @@ public class WirelessSettings extends SettingsPreferenceFragment {
|
|||||||
if (mNfcEnabler != null) {
|
if (mNfcEnabler != null) {
|
||||||
mNfcEnabler.resume();
|
mNfcEnabler.resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mWifiP2pEnabler != null) {
|
||||||
|
mWifiP2pEnabler.resume();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -196,6 +215,10 @@ public class WirelessSettings extends SettingsPreferenceFragment {
|
|||||||
if (mNfcEnabler != null) {
|
if (mNfcEnabler != null) {
|
||||||
mNfcEnabler.pause();
|
mNfcEnabler.pause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mWifiP2pEnabler != null) {
|
||||||
|
mWifiP2pEnabler.pause();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -25,21 +25,19 @@ import android.content.IntentFilter;
|
|||||||
import android.net.wifi.p2p.WifiP2pManager;
|
import android.net.wifi.p2p.WifiP2pManager;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
|
import android.preference.CheckBoxPreference;
|
||||||
import android.preference.Preference;
|
import android.preference.Preference;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.CompoundButton;
|
|
||||||
import android.widget.Switch;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* WifiP2pEnabler is a helper to manage the Wifi p2p on/off
|
* 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 static final String TAG = "WifiP2pEnabler";
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private Switch mSwitch;
|
private final CheckBoxPreference mCheckBox;
|
||||||
private int mWifiP2pState;
|
|
||||||
private final IntentFilter mIntentFilter;
|
private final IntentFilter mIntentFilter;
|
||||||
private final Handler mHandler = new WifiP2pHandler();
|
private final Handler mHandler = new WifiP2pHandler();
|
||||||
private WifiP2pManager mWifiP2pManager;
|
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;
|
mContext = context;
|
||||||
mSwitch = switch_;
|
mCheckBox = checkBox;
|
||||||
|
|
||||||
mWifiP2pManager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
|
mWifiP2pManager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);
|
||||||
if (mWifiP2pManager != null) {
|
if (mWifiP2pManager != null) {
|
||||||
@@ -68,7 +66,7 @@ public class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener {
|
|||||||
//Failure to set up connection
|
//Failure to set up connection
|
||||||
Log.e(TAG, "Failed to set up connection with wifi p2p service");
|
Log.e(TAG, "Failed to set up connection with wifi p2p service");
|
||||||
mWifiP2pManager = null;
|
mWifiP2pManager = null;
|
||||||
mSwitch.setEnabled(false);
|
mCheckBox.setEnabled(false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "mWifiP2pManager is null!");
|
Log.e(TAG, "mWifiP2pManager is null!");
|
||||||
@@ -80,48 +78,39 @@ public class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener {
|
|||||||
public void resume() {
|
public void resume() {
|
||||||
if (mWifiP2pManager == null) return;
|
if (mWifiP2pManager == null) return;
|
||||||
mContext.registerReceiver(mReceiver, mIntentFilter);
|
mContext.registerReceiver(mReceiver, mIntentFilter);
|
||||||
mSwitch.setOnCheckedChangeListener(this);
|
mCheckBox.setOnPreferenceChangeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pause() {
|
public void pause() {
|
||||||
if (mWifiP2pManager == null) return;
|
if (mWifiP2pManager == null) return;
|
||||||
mContext.unregisterReceiver(mReceiver);
|
mContext.unregisterReceiver(mReceiver);
|
||||||
mSwitch.setOnCheckedChangeListener(null);
|
mCheckBox.setOnPreferenceChangeListener(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSwitch(Switch switch_) {
|
public boolean onPreferenceChange(Preference preference, Object value) {
|
||||||
if (mSwitch == switch_) return;
|
|
||||||
mSwitch.setOnCheckedChangeListener(null);
|
|
||||||
mSwitch = switch_;
|
|
||||||
mSwitch.setOnCheckedChangeListener(this);
|
|
||||||
|
|
||||||
mSwitch.setChecked(mWifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED);
|
if (mWifiP2pManager == null) return false;
|
||||||
}
|
|
||||||
|
|
||||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
mCheckBox.setEnabled(false);
|
||||||
|
final boolean enable = (Boolean) value;
|
||||||
if (mWifiP2pManager == null) return;
|
if (enable) {
|
||||||
|
|
||||||
if (isChecked) {
|
|
||||||
mWifiP2pManager.enableP2p(mChannel);
|
mWifiP2pManager.enableP2p(mChannel);
|
||||||
} else {
|
} else {
|
||||||
mWifiP2pManager.disableP2p(mChannel);
|
mWifiP2pManager.disableP2p(mChannel);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleP2pStateChanged(int state) {
|
private void handleP2pStateChanged(int state) {
|
||||||
mSwitch.setEnabled(true);
|
mCheckBox.setEnabled(true);
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case WifiP2pManager.WIFI_P2P_STATE_ENABLED:
|
case WifiP2pManager.WIFI_P2P_STATE_ENABLED:
|
||||||
mWifiP2pState = WifiP2pManager.WIFI_P2P_STATE_ENABLED;
|
mCheckBox.setChecked(true);
|
||||||
mSwitch.setChecked(true);
|
|
||||||
break;
|
break;
|
||||||
case WifiP2pManager.WIFI_P2P_STATE_DISABLED:
|
case WifiP2pManager.WIFI_P2P_STATE_DISABLED:
|
||||||
mWifiP2pState = WifiP2pManager.WIFI_P2P_STATE_DISABLED;
|
mCheckBox.setChecked(false);
|
||||||
mSwitch.setChecked(false);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
mWifiP2pState = WifiP2pManager.WIFI_P2P_STATE_DISABLED;
|
|
||||||
Log.e(TAG,"Unhandled wifi state " + state);
|
Log.e(TAG,"Unhandled wifi state " + state);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -135,10 +124,10 @@ public class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener {
|
|||||||
//Failure to set up connection
|
//Failure to set up connection
|
||||||
Log.e(TAG, "Lost connection with wifi p2p service");
|
Log.e(TAG, "Lost connection with wifi p2p service");
|
||||||
mWifiP2pManager = null;
|
mWifiP2pManager = null;
|
||||||
mSwitch.setEnabled(false);
|
mCheckBox.setEnabled(false);
|
||||||
break;
|
break;
|
||||||
case WifiP2pManager.ENABLE_P2P_FAILED:
|
case WifiP2pManager.ENABLE_P2P_FAILED:
|
||||||
mSwitch.setEnabled(true);
|
mCheckBox.setEnabled(true);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
//Ignore
|
//Ignore
|
||||||
|
@@ -42,7 +42,6 @@ import android.view.Gravity;
|
|||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.widget.Switch;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
@@ -66,7 +65,6 @@ public class WifiP2pSettings extends SettingsPreferenceFragment {
|
|||||||
private final Handler mHandler = new WifiP2pHandler();
|
private final Handler mHandler = new WifiP2pHandler();
|
||||||
private WifiP2pManager mWifiP2pManager;
|
private WifiP2pManager mWifiP2pManager;
|
||||||
private WifiP2pManager.Channel mChannel;
|
private WifiP2pManager.Channel mChannel;
|
||||||
private WifiP2pEnabler mWifiP2pEnabler;
|
|
||||||
private WifiP2pDialog mConnectDialog;
|
private WifiP2pDialog mConnectDialog;
|
||||||
private OnClickListener mConnectListener;
|
private OnClickListener mConnectListener;
|
||||||
private OnClickListener mDisconnectListener;
|
private OnClickListener mDisconnectListener;
|
||||||
@@ -121,25 +119,6 @@ public class WifiP2pSettings extends SettingsPreferenceFragment {
|
|||||||
Log.e(TAG, "mWifiP2pManager is null !");
|
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
|
//connect dialog listener
|
||||||
mConnectListener = new OnClickListener() {
|
mConnectListener = new OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
@@ -171,9 +150,6 @@ public class WifiP2pSettings extends SettingsPreferenceFragment {
|
|||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
getActivity().registerReceiver(mReceiver, mIntentFilter);
|
getActivity().registerReceiver(mReceiver, mIntentFilter);
|
||||||
if (mWifiP2pEnabler != null) {
|
|
||||||
mWifiP2pEnabler.resume();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mWifiP2pManager != null) mWifiP2pManager.discoverPeers(mChannel);
|
if (mWifiP2pManager != null) mWifiP2pManager.discoverPeers(mChannel);
|
||||||
}
|
}
|
||||||
@@ -181,9 +157,6 @@ public class WifiP2pSettings extends SettingsPreferenceFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
if (mWifiP2pEnabler != null) {
|
|
||||||
mWifiP2pEnabler.pause();
|
|
||||||
}
|
|
||||||
getActivity().unregisterReceiver(mReceiver);
|
getActivity().unregisterReceiver(mReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user