From 8c2ac71b34a00a863b2c3ddcd5b5dae1d9bd6a25 Mon Sep 17 00:00:00 2001 From: Irfan Sheriff Date: Mon, 28 Nov 2011 15:10:35 -0800 Subject: [PATCH] Add wifi direct settings UI - Use switch for toggling - Follow string and UI suggestions from UX folks The goal is to keep the interaction minimal - PBC is used when possible and keypad/display are used only if really necessary Bug: 5332330 Change-Id: I83e91ad3a393c143e70f1f2b6a842b95eacde404 --- res/values/strings.xml | 28 +- res/xml/wireless_settings.xml | 9 +- .../android/settings/WirelessSettings.java | 19 +- .../settings/wifi/p2p/WifiP2pDialog.java | 132 --------- .../settings/wifi/p2p/WifiP2pEnabler.java | 47 ++- .../settings/wifi/p2p/WifiP2pSettings.java | 278 ++++++++++-------- 6 files changed, 205 insertions(+), 308 deletions(-) delete mode 100644 src/com/android/settings/wifi/p2p/WifiP2pDialog.java diff --git a/res/values/strings.xml b/res/values/strings.xml index b9fe1b48ace..ab30bf31f05 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1375,23 +1375,25 @@ - Wi-Fi direct - - Set up peer-to-peer connectivity + Wi-Fi Direct Device information - Wi-Fi protected setup - Type PIN Remember this connection - Search - - Create group - - Remove group - - Advanced + SEARCH FOR DEVICES + + SEARCHING + + Rename device - Available devices + PEER DEVICES + + Connect failed + + Disconnect? + + Touching Disconnect will end your connection with %1$s + + Touching Disconnect will end your connection with %1$s and %2$s other devices diff --git a/res/xml/wireless_settings.xml b/res/xml/wireless_settings.xml index 20ffd8eb7d1..336682d8e9e 100644 --- a/res/xml/wireless_settings.xml +++ b/res/xml/wireless_settings.xml @@ -54,17 +54,10 @@ android:key="android_beam_settings" android:title="@string/android_beam_settings_title" /> - - - + android:title="@string/wifi_p2p_settings_title" /> parent, View view, int position, long id) { - mWpsSetupIndex = position; - - if (mWpsSetupIndex == WPS_KEYPAD) { - mView.findViewById(R.id.wps_pin_entry).setVisibility(View.VISIBLE); - } else { - mView.findViewById(R.id.wps_pin_entry).setVisibility(View.GONE); - } - return; - } - - @Override - public void onNothingSelected(AdapterView parent) { - } - -} diff --git a/src/com/android/settings/wifi/p2p/WifiP2pEnabler.java b/src/com/android/settings/wifi/p2p/WifiP2pEnabler.java index 0747d64edf5..cde44cdfb4c 100644 --- a/src/com/android/settings/wifi/p2p/WifiP2pEnabler.java +++ b/src/com/android/settings/wifi/p2p/WifiP2pEnabler.java @@ -27,18 +27,21 @@ import android.os.Message; import android.preference.CheckBoxPreference; import android.preference.Preference; import android.provider.Settings; +import android.widget.CompoundButton; +import android.widget.Switch; import android.util.Log; /** * WifiP2pEnabler is a helper to manage the Wifi p2p on/off */ -public class WifiP2pEnabler implements Preference.OnPreferenceChangeListener { +public class WifiP2pEnabler implements CompoundButton.OnCheckedChangeListener { private static final String TAG = "WifiP2pEnabler"; private final Context mContext; - private final CheckBoxPreference mCheckBox; private final IntentFilter mIntentFilter; + private Switch mSwitch; private WifiP2pManager mWifiP2pManager; + private boolean mStateMachineEvent; private WifiP2pManager.Channel mChannel; private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @@ -53,9 +56,9 @@ public class WifiP2pEnabler implements Preference.OnPreferenceChangeListener { } }; - public WifiP2pEnabler(Context context, CheckBoxPreference checkBox) { + public WifiP2pEnabler(Context context, Switch switch_) { mContext = context; - mCheckBox = checkBox; + mSwitch = switch_; mWifiP2pManager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE); if (mWifiP2pManager != null) { @@ -64,7 +67,7 @@ public class WifiP2pEnabler implements Preference.OnPreferenceChangeListener { //Failure to set up connection Log.e(TAG, "Failed to set up connection with wifi p2p service"); mWifiP2pManager = null; - mCheckBox.setEnabled(false); + mSwitch.setEnabled(false); } } else { Log.e(TAG, "mWifiP2pManager is null!"); @@ -76,42 +79,54 @@ public class WifiP2pEnabler implements Preference.OnPreferenceChangeListener { public void resume() { if (mWifiP2pManager == null) return; mContext.registerReceiver(mReceiver, mIntentFilter); - mCheckBox.setOnPreferenceChangeListener(this); + mSwitch.setOnCheckedChangeListener(this); } public void pause() { if (mWifiP2pManager == null) return; mContext.unregisterReceiver(mReceiver); - mCheckBox.setOnPreferenceChangeListener(null); + mSwitch.setOnCheckedChangeListener(null); } - public boolean onPreferenceChange(Preference preference, Object value) { + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (mWifiP2pManager == null) return false; + if (mStateMachineEvent) return; - mCheckBox.setEnabled(false); - final boolean enable = (Boolean) value; - if (enable) { + if (mWifiP2pManager == null) return; + + mSwitch.setEnabled(false); + if (isChecked) { mWifiP2pManager.enableP2p(mChannel); } else { mWifiP2pManager.disableP2p(mChannel); } - return false; } private void handleP2pStateChanged(int state) { - mCheckBox.setEnabled(true); + setSwitchChecked(true); switch (state) { case WifiP2pManager.WIFI_P2P_STATE_ENABLED: - mCheckBox.setChecked(true); + setSwitchChecked(true); + mSwitch.setEnabled(true); break; case WifiP2pManager.WIFI_P2P_STATE_DISABLED: - mCheckBox.setChecked(false); + setSwitchChecked(false); + mSwitch.setEnabled(true); break; default: + mSwitch.setEnabled(false); + setSwitchChecked(false); Log.e(TAG,"Unhandled wifi state " + state); break; } } + private void setSwitchChecked(boolean checked) { + if (checked != mSwitch.isChecked()) { + mStateMachineEvent = true; + mSwitch.setChecked(checked); + mStateMachineEvent = false; + } + } + } diff --git a/src/com/android/settings/wifi/p2p/WifiP2pSettings.java b/src/com/android/settings/wifi/p2p/WifiP2pSettings.java index f6588b9c62a..f09e4eb4534 100644 --- a/src/com/android/settings/wifi/p2p/WifiP2pSettings.java +++ b/src/com/android/settings/wifi/p2p/WifiP2pSettings.java @@ -32,8 +32,9 @@ import android.net.wifi.p2p.WifiP2pDevice; import android.net.wifi.p2p.WifiP2pDeviceList; import android.net.wifi.p2p.WifiP2pManager; import android.net.wifi.p2p.WifiP2pManager.PeerListListener; +import android.net.wifi.WpsInfo; import android.os.Bundle; -import android.os.Message; +import android.os.Handler; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceCategory; @@ -45,6 +46,8 @@ import android.view.Gravity; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import android.widget.Switch; +import android.widget.Toast; import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; @@ -60,25 +63,27 @@ public class WifiP2pSettings extends SettingsPreferenceFragment implements PeerListListener { private static final String TAG = "WifiP2pSettings"; + private static final boolean DBG = false; private static final int MENU_ID_SEARCH = Menu.FIRST; - private static final int MENU_ID_CREATE_GROUP = Menu.FIRST + 1; - private static final int MENU_ID_REMOVE_GROUP = Menu.FIRST + 2; - private static final int MENU_ID_ADVANCED = Menu.FIRST +3; - + private static final int MENU_ID_RENAME = Menu.FIRST + 1; private final IntentFilter mIntentFilter = new IntentFilter(); private WifiP2pManager mWifiP2pManager; private WifiP2pManager.Channel mChannel; - private WifiP2pDialog mConnectDialog; - private OnClickListener mConnectListener; private OnClickListener mDisconnectListener; private WifiP2pPeer mSelectedWifiPeer; + private WifiP2pEnabler mWifiP2pEnabler; + private boolean mWifiP2pEnabled; + private boolean mWifiP2pSearching; + private int mConnectedDevices; + + private Handler mUiHandler; + private PreferenceGroup mPeersGroup; private Preference mThisDevicePref; - private static final int DIALOG_CONNECT = 1; - private static final int DIALOG_DISCONNECT = 2; + private static final int DIALOG_DISCONNECT = 1; private WifiP2pDevice mThisDevice; private WifiP2pDeviceList mPeers = new WifiP2pDeviceList(); @@ -89,7 +94,9 @@ public class WifiP2pSettings extends SettingsPreferenceFragment String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { - //TODO: nothing right now + mWifiP2pEnabled = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, + WifiP2pManager.WIFI_P2P_STATE_DISABLED) == WifiP2pManager.WIFI_P2P_STATE_ENABLED; + handleP2pStateChanged(); } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { if (mWifiP2pManager != null) { mWifiP2pManager.requestPeers(mChannel, WifiP2pSettings.this); @@ -99,12 +106,12 @@ public class WifiP2pSettings extends SettingsPreferenceFragment NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra( WifiP2pManager.EXTRA_NETWORK_INFO); if (networkInfo.isConnected()) { - Log.d(TAG, "Connected"); + if (DBG) Log.d(TAG, "Connected"); } } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { mThisDevice = (WifiP2pDevice) intent.getParcelableExtra( WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); - Log.d(TAG, "Update device info: " + mThisDevice); + if (DBG) Log.d(TAG, "Update device info: " + mThisDevice); updateDevicePref(); } } @@ -120,6 +127,8 @@ public class WifiP2pSettings extends SettingsPreferenceFragment mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); + mUiHandler = new Handler(); + final Activity activity = getActivity(); mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); if (mWifiP2pManager != null) { @@ -133,27 +142,6 @@ public class WifiP2pSettings extends SettingsPreferenceFragment Log.e(TAG, "mWifiP2pManager is null !"); } - //connect dialog listener - mConnectListener = new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if (which == DialogInterface.BUTTON_POSITIVE) { - WifiP2pConfig config = mConnectDialog.getConfig(); - if (mWifiP2pManager != null) { - mWifiP2pManager.connect(mChannel, config, - new WifiP2pManager.ActionListener() { - public void onSuccess() { - Log.d(TAG, " connect success"); - } - public void onFailure(int reason) { - Log.d(TAG, " connect fail " + reason); - } - }); - } - } - } - }; - //disconnect dialog listener mDisconnectListener = new OnClickListener() { @Override @@ -162,96 +150,101 @@ public class WifiP2pSettings extends SettingsPreferenceFragment if (mWifiP2pManager != null) { mWifiP2pManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() { public void onSuccess() { - Log.d(TAG, " remove group success"); + if (DBG) Log.d(TAG, " remove group success"); } public void onFailure(int reason) { - Log.d(TAG, " remove group fail " + reason); + if (DBG) Log.d(TAG, " remove group fail " + reason); } }); } } } }; + + 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); + + final PreferenceScreen preferenceScreen = getPreferenceScreen(); + preferenceScreen.removeAll(); + + preferenceScreen.setOrderingAsAdded(true); + mThisDevicePref = new Preference(getActivity()); + preferenceScreen.addPreference(mThisDevicePref); + setHasOptionsMenu(true); } @Override public void onResume() { super.onResume(); + mWifiP2pEnabler.resume(); getActivity().registerReceiver(mReceiver, mIntentFilter); - - if (mWifiP2pManager != null) { - mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() { - public void onSuccess() { - Log.d(TAG, " discover success"); - } - public void onFailure(int reason) { - Log.d(TAG, " discover fail " + reason); - } - }); - } + startSearch(); } @Override public void onPause() { super.onPause(); + mWifiP2pEnabler.pause(); getActivity().unregisterReceiver(mReceiver); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - menu.add(Menu.NONE, MENU_ID_SEARCH, 0, R.string.wifi_p2p_menu_search) + int textId = mWifiP2pSearching ? R.string.wifi_p2p_menu_searching : + R.string.wifi_p2p_menu_search; + menu.add(Menu.NONE, MENU_ID_SEARCH, 0, textId) + .setEnabled(mWifiP2pEnabled) .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - menu.add(Menu.NONE, MENU_ID_CREATE_GROUP, 0, R.string.wifi_p2p_menu_create_group) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - menu.add(Menu.NONE, MENU_ID_REMOVE_GROUP, 0, R.string.wifi_p2p_menu_remove_group) - .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); - menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_p2p_menu_advanced) + menu.add(Menu.NONE, MENU_ID_RENAME, 0, R.string.wifi_p2p_menu_rename) + .setEnabled(mWifiP2pEnabled) .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); super.onCreateOptionsMenu(menu, inflater); } + @Override + public void onPrepareOptionsMenu(Menu menu) { + MenuItem searchMenu = menu.findItem(MENU_ID_SEARCH); + MenuItem renameMenu = menu.findItem(MENU_ID_RENAME); + if (mWifiP2pEnabled) { + searchMenu.setEnabled(true); + renameMenu.setEnabled(true); + } else { + searchMenu.setEnabled(false); + renameMenu.setEnabled(false); + } + + if (mWifiP2pSearching) { + searchMenu.setTitle(R.string.wifi_p2p_menu_searching); + } else { + searchMenu.setTitle(R.string.wifi_p2p_menu_search); + } + } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_ID_SEARCH: - if (mWifiP2pManager != null) { - mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() { - public void onSuccess() { - Log.d(TAG, " discover success"); - } - public void onFailure(int reason) { - Log.d(TAG, " discover fail " + reason); - } - }); - } + startSearch(); return true; - case MENU_ID_CREATE_GROUP: - if (mWifiP2pManager != null) { - mWifiP2pManager.createGroup(mChannel, new WifiP2pManager.ActionListener() { - public void onSuccess() { - Log.d(TAG, " create group success"); - } - public void onFailure(int reason) { - Log.d(TAG, " create group fail " + reason); - } - }); - } - return true; - case MENU_ID_REMOVE_GROUP: - if (mWifiP2pManager != null) { - mWifiP2pManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() { - public void onSuccess() { - Log.d(TAG, " remove group success"); - } - public void onFailure(int reason) { - Log.d(TAG, " remove group fail " + reason); - } - }); - } - return true; - case MENU_ID_ADVANCED: - //TODO: add advanced settings for p2p + case MENU_ID_RENAME: + //TODO: handle rename return true; } return super.onOptionsItemSelected(item); @@ -264,7 +257,27 @@ public class WifiP2pSettings extends SettingsPreferenceFragment if (mSelectedWifiPeer.device.status == WifiP2pDevice.CONNECTED) { showDialog(DIALOG_DISCONNECT); } else { - showDialog(DIALOG_CONNECT); + WifiP2pConfig config = new WifiP2pConfig(); + config.deviceAddress = mSelectedWifiPeer.device.deviceAddress; + if (mSelectedWifiPeer.device.wpsPbcSupported()) { + config.wps.setup = WpsInfo.PBC; + } else if (mSelectedWifiPeer.device.wpsKeypadSupported()) { + config.wps.setup = WpsInfo.KEYPAD; + } else { + config.wps.setup = WpsInfo.DISPLAY; + } + mWifiP2pManager.connect(mChannel, config, + new WifiP2pManager.ActionListener() { + public void onSuccess() { + if (DBG) Log.d(TAG, " connect success"); + } + public void onFailure(int reason) { + Log.e(TAG, " connect fail " + reason); + Toast.makeText(getActivity(), + R.string.wifi_p2p_failed_connect_message, + Toast.LENGTH_SHORT).show(); + } + }); } } return super.onPreferenceTreeClick(screen, preference); @@ -272,14 +285,15 @@ public class WifiP2pSettings extends SettingsPreferenceFragment @Override public Dialog onCreateDialog(int id) { - if (id == DIALOG_CONNECT) { - mConnectDialog = new WifiP2pDialog(getActivity(), mConnectListener, - mSelectedWifiPeer.device); - return mConnectDialog; - } else if (id == DIALOG_DISCONNECT) { + if (id == DIALOG_DISCONNECT) { + int stringId = (mConnectedDevices > 1) ? R.string.wifi_p2p_disconnect_multiple_message : + R.string.wifi_p2p_disconnect_message; + String deviceName = TextUtils.isEmpty(mSelectedWifiPeer.device.deviceName) ? + mSelectedWifiPeer.device.deviceAddress : + mSelectedWifiPeer.device.deviceName; AlertDialog dialog = new AlertDialog.Builder(getActivity()) - .setTitle("Disconnect ?") - .setMessage("Do you want to disconnect ?") + .setTitle(R.string.wifi_p2p_disconnect_title) + .setMessage(getActivity().getString(stringId, deviceName)) .setPositiveButton(getActivity().getString(R.string.dlg_ok), mDisconnectListener) .setNegativeButton(getActivity().getString(R.string.dlg_cancel), null) .create(); @@ -289,33 +303,61 @@ public class WifiP2pSettings extends SettingsPreferenceFragment } public void onPeersAvailable(WifiP2pDeviceList peers) { - - final PreferenceScreen preferenceScreen = getPreferenceScreen(); - preferenceScreen.removeAll(); - - preferenceScreen.setOrderingAsAdded(true); - - if (mPeersGroup == null) { - mPeersGroup = new PreferenceCategory(getActivity()); - } else { - mPeersGroup.removeAll(); - } - - preferenceScreen.addPreference(mThisDevicePref); - - mPeersGroup.setTitle(R.string.wifi_p2p_available_devices); - mPeersGroup.setEnabled(true); - preferenceScreen.addPreference(mPeersGroup); + mPeersGroup.removeAll(); mPeers = peers; + mConnectedDevices = 0; for (WifiP2pDevice peer: peers.getDeviceList()) { mPeersGroup.addPreference(new WifiP2pPeer(getActivity(), peer)); + if (peer.status == WifiP2pDevice.CONNECTED) mConnectedDevices++; + } + } + + private void handleP2pStateChanged() { + updateSearchMenu(false); + if (mWifiP2pEnabled) { + final PreferenceScreen preferenceScreen = getPreferenceScreen(); + preferenceScreen.removeAll(); + + preferenceScreen.setOrderingAsAdded(true); + mThisDevicePref = new Preference(getActivity()); + preferenceScreen.addPreference(mThisDevicePref); + + mPeersGroup = new PreferenceCategory(getActivity()); + mPeersGroup.setTitle(R.string.wifi_p2p_peer_devices); + mPeersGroup.setEnabled(true); + preferenceScreen.addPreference(mPeersGroup); + + startSearch(); + } + } + + private void updateSearchMenu(boolean searching) { + mWifiP2pSearching = searching; + Activity activity = getActivity(); + if (activity != null) activity.invalidateOptionsMenu(); + } + + private void startSearch() { + if (mWifiP2pManager != null && !mWifiP2pSearching) { + mWifiP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() { + public void onSuccess() { + updateSearchMenu(true); + //Allow 20s to discover devices + mUiHandler.postDelayed(new Runnable() { + public void run() { + updateSearchMenu(false); + }}, 20000); + } + public void onFailure(int reason) { + if (DBG) Log.d(TAG, " discover fail " + reason); + updateSearchMenu(false); + } + }); } } private void updateDevicePref() { - mThisDevicePref = new Preference(getActivity()); - if (mThisDevice != null) { if (TextUtils.isEmpty(mThisDevice.deviceName)) { mThisDevicePref.setTitle(mThisDevice.deviceAddress); @@ -323,15 +365,9 @@ public class WifiP2pSettings extends SettingsPreferenceFragment mThisDevicePref.setTitle(mThisDevice.deviceName); } - if (mThisDevice.status == WifiP2pDevice.CONNECTED) { - String[] statusArray = getActivity().getResources().getStringArray( - R.array.wifi_p2p_status); - mThisDevicePref.setSummary(statusArray[mThisDevice.status]); - } mThisDevicePref.setPersistent(false); mThisDevicePref.setEnabled(true); mThisDevicePref.setSelectable(false); } - onPeersAvailable(mPeers); //update UI } }