am d52e0c79: Merge "Add support for changing p2p device name" into jb-dev

* commit 'd52e0c791f83d16cb5de32b6a9a1d785aa56b454':
  Add support for changing p2p device name
This commit is contained in:
Irfan Sheriff
2012-04-27 09:57:44 -07:00
committed by Android Git Automerger
3 changed files with 79 additions and 9 deletions

View File

@@ -3269,6 +3269,12 @@ public final class Settings {
*/
public static final String WIFI_FREQUENCY_BAND = "wifi_frequency_band";
/**
* The Wi-Fi peer-to-peer device name
* @hide
*/
public static final String WIFI_P2P_DEVICE_NAME = "wifi_p2p_device_name";
/**
* Maximum amount of time in milliseconds to hold a wakelock while waiting for mobile
* data connectivity to be established after a disconnect from Wi-Fi.

View File

@@ -381,6 +381,13 @@ public class WifiP2pManager {
/** @hide */
public static final int RESPONSE_SERVICE = BASE + 50;
/** @hide */
public static final int SET_DEVICE_NAME = BASE + 51;
/** @hide */
public static final int SET_DEVICE_NAME_FAILED = BASE + 52;
/** @hide */
public static final int SET_DEVICE_NAME_SUCCEEDED = BASE + 53;
/**
* Create a new WifiP2pManager instance. Applications use
* {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
@@ -603,6 +610,7 @@ public class WifiP2pManager {
case WifiP2pManager.ADD_SERVICE_REQUEST_FAILED:
case WifiP2pManager.REMOVE_SERVICE_REQUEST_FAILED:
case WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED:
case WifiP2pManager.SET_DEVICE_NAME_FAILED:
if (listener != null) {
((ActionListener) listener).onFailure(message.arg1);
}
@@ -621,6 +629,7 @@ public class WifiP2pManager {
case WifiP2pManager.ADD_SERVICE_REQUEST_SUCCEEDED:
case WifiP2pManager.REMOVE_SERVICE_REQUEST_SUCCEEDED:
case WifiP2pManager.CLEAR_SERVICE_REQUESTS_SUCCEEDED:
case WifiP2pManager.SET_DEVICE_NAME_SUCCEEDED:
if (listener != null) {
((ActionListener) listener).onSuccess();
}
@@ -1103,6 +1112,21 @@ public class WifiP2pManager {
c.mAsyncChannel.sendMessage(REQUEST_GROUP_INFO, 0, c.putListener(listener));
}
/**
* Set p2p device name.
* @hide
* @param c is the channel created at {@link #initialize}
* @param listener for callback when group info is available. Can be null.
*/
public void setDeviceName(Channel c, String devName, ActionListener listener) {
checkChannel(c);
WifiP2pDevice d = new WifiP2pDevice();
d.deviceName = devName;
c.mAsyncChannel.sendMessage(SET_DEVICE_NAME, 0, c.putListener(listener), d);
}
/**
* Get a reference to WifiP2pService handler. This is used to establish
* an AsyncChannel communication with WifiService

View File

@@ -184,7 +184,6 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
mThisDevice.primaryDeviceType = mContext.getResources().getString(
com.android.internal.R.string.config_wifi_p2p_device_type);
mThisDevice.deviceName = getDefaultDeviceName();
mP2pStateMachine = new P2pStateMachine(TAG, mP2pSupported);
mP2pStateMachine.start();
@@ -205,14 +204,6 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
"WifiP2pService");
}
/* We use the 4 digits of the ANDROID_ID to have a friendly
* default that has low likelihood of collision with a peer */
private String getDefaultDeviceName() {
String id = Settings.Secure.getString(mContext.getContentResolver(),
Settings.Secure.ANDROID_ID);
return "Android_" + id.substring(0,4);
}
/**
* Get a reference to handler. This is used by a client to establish
* an AsyncChannel communication with WifiP2pService
@@ -376,6 +367,10 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED,
WifiP2pManager.BUSY);
break;
case WifiP2pManager.SET_DEVICE_NAME:
replyToMessage(message, WifiP2pManager.SET_DEVICE_NAME_FAILED,
WifiP2pManager.BUSY);
break;
case WifiP2pManager.REQUEST_PEERS:
replyToMessage(message, WifiP2pManager.RESPONSE_PEERS, mPeers);
break;
@@ -474,6 +469,10 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED,
WifiP2pManager.P2P_UNSUPPORTED);
break;
case WifiP2pManager.SET_DEVICE_NAME:
replyToMessage(message, WifiP2pManager.SET_DEVICE_NAME_FAILED,
WifiP2pManager.P2P_UNSUPPORTED);
break;
default:
return NOT_HANDLED;
}
@@ -583,6 +582,16 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
mWifiNative.closeSupplicantConnection();
transitionTo(mP2pDisablingState);
break;
case WifiP2pManager.SET_DEVICE_NAME:
WifiP2pDevice d = (WifiP2pDevice) message.obj;
if (d != null && setAndPersistDeviceName(d.deviceName)) {
if (DBG) logd("set device name " + d.deviceName);
replyToMessage(message, WifiP2pManager.SET_DEVICE_NAME_SUCCEEDED);
} else {
replyToMessage(message, WifiP2pManager.SET_DEVICE_NAME_FAILED,
WifiP2pManager.ERROR);
}
break;
case WifiP2pManager.DISCOVER_PEERS:
// do not send service discovery request while normal find operation.
clearSupplicantServiceRequest();
@@ -1412,8 +1421,39 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
}
}
private String getPersistedDeviceName() {
String deviceName = Settings.Secure.getString(mContext.getContentResolver(),
Settings.Secure.WIFI_P2P_DEVICE_NAME);
if (deviceName == null) {
/* We use the 4 digits of the ANDROID_ID to have a friendly
* default that has low likelihood of collision with a peer */
String id = Settings.Secure.getString(mContext.getContentResolver(),
Settings.Secure.ANDROID_ID);
return "Android_" + id.substring(0,4);
}
return deviceName;
}
private boolean setAndPersistDeviceName(String devName) {
if (devName == null) return false;
if (!mWifiNative.setDeviceName(devName)) {
loge("Failed to set device name " + devName);
return false;
}
mThisDevice.deviceName = devName;
mWifiNative.setP2pSsidPostfix("-" + mThisDevice.deviceName);
Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.WIFI_P2P_DEVICE_NAME, devName);
sendThisDeviceChangedBroadcast();
return true;
}
private void initializeP2pSettings() {
mWifiNative.setPersistentReconnect(true);
mThisDevice.deviceName = getPersistedDeviceName();
mWifiNative.setDeviceName(mThisDevice.deviceName);
//DIRECT-XY-DEVICENAME (XY is randomly generated)
mWifiNative.setP2pSsidPostfix("-" + mThisDevice.deviceName);