Skip to content

Commit d52e0c7

Browse files
isheriffAndroid (Google) Code Review
authored andcommitted
Merge "Add support for changing p2p device name" into jb-dev
2 parents c098198 + 2bdefca commit d52e0c7

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

core/java/android/provider/Settings.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,6 +3269,12 @@ public static final String getBluetoothInputDevicePriorityKey(String address) {
32693269
*/
32703270
public static final String WIFI_FREQUENCY_BAND = "wifi_frequency_band";
32713271

3272+
/**
3273+
* The Wi-Fi peer-to-peer device name
3274+
* @hide
3275+
*/
3276+
public static final String WIFI_P2P_DEVICE_NAME = "wifi_p2p_device_name";
3277+
32723278
/**
32733279
* Maximum amount of time in milliseconds to hold a wakelock while waiting for mobile
32743280
* data connectivity to be established after a disconnect from Wi-Fi.

wifi/java/android/net/wifi/p2p/WifiP2pManager.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ public class WifiP2pManager {
381381
/** @hide */
382382
public static final int RESPONSE_SERVICE = BASE + 50;
383383

384+
/** @hide */
385+
public static final int SET_DEVICE_NAME = BASE + 51;
386+
/** @hide */
387+
public static final int SET_DEVICE_NAME_FAILED = BASE + 52;
388+
/** @hide */
389+
public static final int SET_DEVICE_NAME_SUCCEEDED = BASE + 53;
390+
384391
/**
385392
* Create a new WifiP2pManager instance. Applications use
386393
* {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
@@ -603,6 +610,7 @@ public void handleMessage(Message message) {
603610
case WifiP2pManager.ADD_SERVICE_REQUEST_FAILED:
604611
case WifiP2pManager.REMOVE_SERVICE_REQUEST_FAILED:
605612
case WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED:
613+
case WifiP2pManager.SET_DEVICE_NAME_FAILED:
606614
if (listener != null) {
607615
((ActionListener) listener).onFailure(message.arg1);
608616
}
@@ -621,6 +629,7 @@ public void handleMessage(Message message) {
621629
case WifiP2pManager.ADD_SERVICE_REQUEST_SUCCEEDED:
622630
case WifiP2pManager.REMOVE_SERVICE_REQUEST_SUCCEEDED:
623631
case WifiP2pManager.CLEAR_SERVICE_REQUESTS_SUCCEEDED:
632+
case WifiP2pManager.SET_DEVICE_NAME_SUCCEEDED:
624633
if (listener != null) {
625634
((ActionListener) listener).onSuccess();
626635
}
@@ -1103,6 +1112,21 @@ public void requestGroupInfo(Channel c, GroupInfoListener listener) {
11031112
c.mAsyncChannel.sendMessage(REQUEST_GROUP_INFO, 0, c.putListener(listener));
11041113
}
11051114

1115+
/**
1116+
* Set p2p device name.
1117+
* @hide
1118+
* @param c is the channel created at {@link #initialize}
1119+
* @param listener for callback when group info is available. Can be null.
1120+
*/
1121+
public void setDeviceName(Channel c, String devName, ActionListener listener) {
1122+
checkChannel(c);
1123+
WifiP2pDevice d = new WifiP2pDevice();
1124+
d.deviceName = devName;
1125+
c.mAsyncChannel.sendMessage(SET_DEVICE_NAME, 0, c.putListener(listener), d);
1126+
}
1127+
1128+
1129+
11061130
/**
11071131
* Get a reference to WifiP2pService handler. This is used to establish
11081132
* an AsyncChannel communication with WifiService

wifi/java/android/net/wifi/p2p/WifiP2pService.java

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public WifiP2pService(Context context) {
184184

185185
mThisDevice.primaryDeviceType = mContext.getResources().getString(
186186
com.android.internal.R.string.config_wifi_p2p_device_type);
187-
mThisDevice.deviceName = getDefaultDeviceName();
188187

189188
mP2pStateMachine = new P2pStateMachine(TAG, mP2pSupported);
190189
mP2pStateMachine.start();
@@ -205,14 +204,6 @@ private void enforceChangePermission() {
205204
"WifiP2pService");
206205
}
207206

208-
/* We use the 4 digits of the ANDROID_ID to have a friendly
209-
* default that has low likelihood of collision with a peer */
210-
private String getDefaultDeviceName() {
211-
String id = Settings.Secure.getString(mContext.getContentResolver(),
212-
Settings.Secure.ANDROID_ID);
213-
return "Android_" + id.substring(0,4);
214-
}
215-
216207
/**
217208
* Get a reference to handler. This is used by a client to establish
218209
* an AsyncChannel communication with WifiP2pService
@@ -376,6 +367,10 @@ public boolean processMessage(Message message) {
376367
WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED,
377368
WifiP2pManager.BUSY);
378369
break;
370+
case WifiP2pManager.SET_DEVICE_NAME:
371+
replyToMessage(message, WifiP2pManager.SET_DEVICE_NAME_FAILED,
372+
WifiP2pManager.BUSY);
373+
break;
379374
case WifiP2pManager.REQUEST_PEERS:
380375
replyToMessage(message, WifiP2pManager.RESPONSE_PEERS, mPeers);
381376
break;
@@ -474,6 +469,10 @@ public boolean processMessage(Message message) {
474469
WifiP2pManager.CLEAR_SERVICE_REQUESTS_FAILED,
475470
WifiP2pManager.P2P_UNSUPPORTED);
476471
break;
472+
case WifiP2pManager.SET_DEVICE_NAME:
473+
replyToMessage(message, WifiP2pManager.SET_DEVICE_NAME_FAILED,
474+
WifiP2pManager.P2P_UNSUPPORTED);
475+
break;
477476
default:
478477
return NOT_HANDLED;
479478
}
@@ -583,6 +582,16 @@ public boolean processMessage(Message message) {
583582
mWifiNative.closeSupplicantConnection();
584583
transitionTo(mP2pDisablingState);
585584
break;
585+
case WifiP2pManager.SET_DEVICE_NAME:
586+
WifiP2pDevice d = (WifiP2pDevice) message.obj;
587+
if (d != null && setAndPersistDeviceName(d.deviceName)) {
588+
if (DBG) logd("set device name " + d.deviceName);
589+
replyToMessage(message, WifiP2pManager.SET_DEVICE_NAME_SUCCEEDED);
590+
} else {
591+
replyToMessage(message, WifiP2pManager.SET_DEVICE_NAME_FAILED,
592+
WifiP2pManager.ERROR);
593+
}
594+
break;
586595
case WifiP2pManager.DISCOVER_PEERS:
587596
// do not send service discovery request while normal find operation.
588597
clearSupplicantServiceRequest();
@@ -1412,8 +1421,39 @@ private void p2pConnectWithPinDisplay(WifiP2pConfig config, boolean join) {
14121421
}
14131422
}
14141423

1424+
private String getPersistedDeviceName() {
1425+
String deviceName = Settings.Secure.getString(mContext.getContentResolver(),
1426+
Settings.Secure.WIFI_P2P_DEVICE_NAME);
1427+
if (deviceName == null) {
1428+
/* We use the 4 digits of the ANDROID_ID to have a friendly
1429+
* default that has low likelihood of collision with a peer */
1430+
String id = Settings.Secure.getString(mContext.getContentResolver(),
1431+
Settings.Secure.ANDROID_ID);
1432+
return "Android_" + id.substring(0,4);
1433+
}
1434+
return deviceName;
1435+
}
1436+
1437+
private boolean setAndPersistDeviceName(String devName) {
1438+
if (devName == null) return false;
1439+
1440+
if (!mWifiNative.setDeviceName(devName)) {
1441+
loge("Failed to set device name " + devName);
1442+
return false;
1443+
}
1444+
1445+
mThisDevice.deviceName = devName;
1446+
mWifiNative.setP2pSsidPostfix("-" + mThisDevice.deviceName);
1447+
1448+
Settings.Secure.putString(mContext.getContentResolver(),
1449+
Settings.Secure.WIFI_P2P_DEVICE_NAME, devName);
1450+
sendThisDeviceChangedBroadcast();
1451+
return true;
1452+
}
1453+
14151454
private void initializeP2pSettings() {
14161455
mWifiNative.setPersistentReconnect(true);
1456+
mThisDevice.deviceName = getPersistedDeviceName();
14171457
mWifiNative.setDeviceName(mThisDevice.deviceName);
14181458
//DIRECT-XY-DEVICENAME (XY is randomly generated)
14191459
mWifiNative.setP2pSsidPostfix("-" + mThisDevice.deviceName);

0 commit comments

Comments
 (0)