Merge "Add wifi direct settings UI"

This commit is contained in:
Irfan Sheriff
2011-12-01 17:23:41 -08:00
committed by Android (Google) Code Review
6 changed files with 205 additions and 308 deletions

View File

@@ -1375,23 +1375,25 @@
<!-- Wi-Fi p2p / Wi-Fi Direct settings -->
<!-- Used in the 1st-level settings screen to launch Wi-fi Direct settings [CHAR LIMIT=25] -->
<string name="wifi_p2p_settings_title">Wi-Fi direct</string>
<!-- Summary for Wi-fi Direct settings item in the 1st-level settings screen [CHAR LIMIT=50]-->
<string name="wifi_p2p_settings_summary">Set up peer-to-peer connectivity</string>
<string name="wifi_p2p_settings_title">Wi-Fi Direct</string>
<string name="wifi_p2p_device_info">Device information</string>
<string name="wifi_p2p_wps_setup">Wi-Fi protected setup</string>
<string name="wifi_p2p_wps_pin">Type PIN</string>
<string name="wifi_p2p_persist_network">Remember this connection</string>
<!-- Menu option to discover peers-->
<string name="wifi_p2p_menu_search">Search</string>
<!-- Menu option to create a group-->
<string name="wifi_p2p_menu_create_group">Create group</string>
<!-- Menu option to remove a group-->
<string name="wifi_p2p_menu_remove_group">Remove group</string>
<!-- Menu option to Wi-Fi p2p advanced settings -->
<string name="wifi_p2p_menu_advanced">Advanced</string>
<string name="wifi_p2p_menu_search">SEARCH FOR DEVICES</string>
<!-- Menu option when discovery is in progress-->
<string name="wifi_p2p_menu_searching">SEARCHING</string>
<!-- Menu option to Rename-->
<string name="wifi_p2p_menu_rename">Rename device</string>
<!-- Title for available p2p devices -->
<string name="wifi_p2p_available_devices">Available devices</string>
<string name="wifi_p2p_peer_devices">PEER DEVICES</string>
<!-- Toast text for a failed connection initiation -->
<string name="wifi_p2p_failed_connect_message">Connect failed</string>
<!-- Title for disconnect dialog -->
<string name="wifi_p2p_disconnect_title">Disconnect?</string>
<!-- Message test for disconnection from one device-->
<string name="wifi_p2p_disconnect_message">Touching Disconnect will end your connection with <xliff:g id="peer_name">%1$s</xliff:g></string>
<!-- Message test for disconnection from multiple devices-->
<string name="wifi_p2p_disconnect_multiple_message">Touching Disconnect will end your connection with <xliff:g id="peer_name">%1$s</xliff:g> and <xliff:g id="peer_count">%2$s</xliff:g> other devices</string>
<!-- Wifi AP settings-->
<!-- Label for wifi tether checkbox. Toggles Access Point on/off -->

View File

@@ -54,17 +54,10 @@
android:key="android_beam_settings"
android:title="@string/android_beam_settings_title" />
<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"
android:title="@string/wifi_p2p_settings_title"
android:summary="@string/wifi_p2p_settings_summary">
</PreferenceScreen>
android:title="@string/wifi_p2p_settings_title" />
<PreferenceScreen
android:key="mobile_network_settings"

View File

@@ -37,7 +37,6 @@ 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 {
@@ -46,7 +45,6 @@ public class WirelessSettings extends SettingsPreferenceFragment {
private static final String KEY_WIMAX_SETTINGS = "wimax_settings";
private static final String KEY_ANDROID_BEAM_SETTINGS = "android_beam_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";
@@ -60,8 +58,6 @@ 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
@@ -102,8 +98,6 @@ public class WirelessSettings extends SettingsPreferenceFragment {
CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
PreferenceScreen androidBeam = (PreferenceScreen) findPreference(KEY_ANDROID_BEAM_SETTINGS);
CheckBoxPreference wifiP2p = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI_P2P);
mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
mNfcEnabler = new NfcEnabler(activity, nfc, androidBeam);
@@ -156,11 +150,8 @@ public class WirelessSettings extends SettingsPreferenceFragment {
WifiP2pManager p2p = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)) {
getPreferenceScreen().removePreference(wifiP2p);
} else {
mWifiP2pEnabler = new WifiP2pEnabler(activity, wifiP2p);
}
getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
}
// Enable Proxy selector settings if allowed.
Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
@@ -189,10 +180,6 @@ public class WirelessSettings extends SettingsPreferenceFragment {
if (mNfcEnabler != null) {
mNfcEnabler.resume();
}
if (mWifiP2pEnabler != null) {
mWifiP2pEnabler.resume();
}
}
@Override
@@ -203,10 +190,6 @@ public class WirelessSettings extends SettingsPreferenceFragment {
if (mNfcEnabler != null) {
mNfcEnabler.pause();
}
if (mWifiP2pEnabler != null) {
mWifiP2pEnabler.pause();
}
}
@Override

View File

@@ -1,132 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.wifi.p2p;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import com.android.settings.R;
/**
* Dialog to setup a p2p connection
*/
public class WifiP2pDialog extends AlertDialog implements AdapterView.OnItemSelectedListener {
static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
private final DialogInterface.OnClickListener mListener;
private View mView;
private TextView mDeviceName;
private TextView mDeviceAddress;
/* These values come from "wifi_p2p_wps_setup" resource array */
private static final int WPS_PBC = 0;
private static final int WPS_KEYPAD = 1;
private static final int WPS_DISPLAY = 2;
private int mWpsSetupIndex = WPS_PBC; //default is pbc
WifiP2pDevice mDevice;
public WifiP2pDialog(Context context, DialogInterface.OnClickListener listener,
WifiP2pDevice device) {
super(context);
mListener = listener;
mDevice = device;
}
public WifiP2pConfig getConfig() {
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = mDeviceAddress.getText().toString();
config.wps = new WpsInfo();
switch (mWpsSetupIndex) {
case WPS_PBC:
config.wps.setup = WpsInfo.PBC;
break;
case WPS_KEYPAD:
config.wps.setup = WpsInfo.KEYPAD;
config.wps.pin = ((TextView) mView.findViewById(R.id.wps_pin)).
getText().toString();
break;
case WPS_DISPLAY:
config.wps.setup = WpsInfo.DISPLAY;
break;
default:
config.wps.setup = WpsInfo.PBC;
break;
}
return config;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
mView = getLayoutInflater().inflate(R.layout.wifi_p2p_dialog, null);
Spinner mWpsSetup = ((Spinner) mView.findViewById(R.id.wps_setup));
setView(mView);
setInverseBackgroundForced(true);
Context context = getContext();
setTitle(R.string.wifi_p2p_settings_title);
mDeviceName = (TextView) mView.findViewById(R.id.device_name);
mDeviceAddress = (TextView) mView.findViewById(R.id.device_address);
setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_connect), mListener);
setButton(DialogInterface.BUTTON_NEGATIVE,
context.getString(R.string.wifi_cancel), mListener);
if (mDevice != null) {
mDeviceName.setText(mDevice.deviceName);
mDeviceAddress.setText(mDevice.deviceAddress);
mWpsSetup.setSelection(mWpsSetupIndex); //keep pbc as default
}
mWpsSetup.setOnItemSelectedListener(this);
super.onCreate(savedInstanceState);
}
@Override
public void onItemSelected(AdapterView<?> 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) {
}
}

View File

@@ -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;
}
}
}

View File

@@ -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) {
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);
if (mPeersGroup == null) {
mPeersGroup = new PreferenceCategory(getActivity());
} else {
mPeersGroup.removeAll();
}
mThisDevicePref = new Preference(getActivity());
preferenceScreen.addPreference(mThisDevicePref);
mPeersGroup.setTitle(R.string.wifi_p2p_available_devices);
mPeersGroup = new PreferenceCategory(getActivity());
mPeersGroup.setTitle(R.string.wifi_p2p_peer_devices);
mPeersGroup.setEnabled(true);
preferenceScreen.addPreference(mPeersGroup);
mPeers = peers;
for (WifiP2pDevice peer: peers.getDeviceList()) {
mPeersGroup.addPreference(new WifiP2pPeer(getActivity(), peer));
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
}
}