Skip to content

Commit d8544a5

Browse files
author
Jeff Brown
committed
Copy all mutable state before sending to clients.
This resolves an issue with ConcurrentModificationException or inconsistent data being perceived by clients. Fixed an NPE in the WifiP2pDeviceList copy constructor. Bug: 7133752 Change-Id: I37a4d004f7b1ca21d490f131039d81695db2ba42
1 parent 21c7153 commit d8544a5

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

wifi/java/android/net/wifi/p2p/WifiP2pDeviceList.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@
3535
*/
3636
public class WifiP2pDeviceList implements Parcelable {
3737

38-
private HashMap<String, WifiP2pDevice> mDevices;
38+
private final HashMap<String, WifiP2pDevice> mDevices = new HashMap<String, WifiP2pDevice>();
3939

4040
public WifiP2pDeviceList() {
41-
mDevices = new HashMap<String, WifiP2pDevice>();
4241
}
4342

4443
/** copy constructor */
@@ -52,7 +51,6 @@ public WifiP2pDeviceList(WifiP2pDeviceList source) {
5251

5352
/** @hide */
5453
public WifiP2pDeviceList(ArrayList<WifiP2pDevice> devices) {
55-
mDevices = new HashMap<String, WifiP2pDevice>();
5654
for (WifiP2pDevice device : devices) {
5755
if (device.deviceAddress != null) {
5856
mDevices.put(device.deviceAddress, device);

wifi/java/android/net/wifi/p2p/WifiP2pGroupList.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package android.net.wifi.p2p;
1717

1818
import java.util.Collection;
19+
import java.util.Map;
1920

2021
import android.os.Parcel;
2122
import android.os.Parcelable;
@@ -32,19 +33,20 @@ public class WifiP2pGroupList implements Parcelable {
3233

3334
private static final int CREDENTIAL_MAX_NUM = 32;
3435

35-
private LruCache<Integer, WifiP2pGroup> mGroups;
36-
private GroupDeleteListener mListener;
36+
private final LruCache<Integer, WifiP2pGroup> mGroups;
37+
private final GroupDeleteListener mListener;
38+
3739
private boolean isClearCalled = false;
3840

3941
public interface GroupDeleteListener {
4042
public void onDeleteGroup(int netId);
4143
}
4244

4345
WifiP2pGroupList() {
44-
this(null);
46+
this(null, null);
4547
}
4648

47-
WifiP2pGroupList(GroupDeleteListener listener) {
49+
WifiP2pGroupList(WifiP2pGroupList source, GroupDeleteListener listener) {
4850
mListener = listener;
4951
mGroups = new LruCache<Integer, WifiP2pGroup>(CREDENTIAL_MAX_NUM) {
5052
@Override
@@ -55,6 +57,12 @@ protected void entryRemoved(boolean evicted, Integer netId,
5557
}
5658
}
5759
};
60+
61+
if (source != null) {
62+
for (Map.Entry<Integer, WifiP2pGroup> item : source.mGroups.snapshot().entrySet()) {
63+
mGroups.put(item.getKey(), item.getValue());
64+
}
65+
}
5866
}
5967

6068
/**

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ private class P2pStateMachine extends StateMachine {
359359
private WifiNative mWifiNative = new WifiNative(mInterface);
360360
private WifiMonitor mWifiMonitor = new WifiMonitor(this, mWifiNative);
361361

362-
private WifiP2pDeviceList mPeers = new WifiP2pDeviceList();
363-
private WifiP2pGroupList mGroups = new WifiP2pGroupList(
362+
private final WifiP2pDeviceList mPeers = new WifiP2pDeviceList();
363+
private final WifiP2pGroupList mGroups = new WifiP2pGroupList(null,
364364
new GroupDeleteListener() {
365365
@Override
366366
public void onDeleteGroup(int netId) {
@@ -370,7 +370,7 @@ public void onDeleteGroup(int netId) {
370370
sendP2pPersistentGroupsChangedBroadcast();
371371
}
372372
});
373-
private WifiP2pInfo mWifiP2pInfo = new WifiP2pInfo();
373+
private final WifiP2pInfo mWifiP2pInfo = new WifiP2pInfo();
374374
private WifiP2pGroup mGroup;
375375

376376
// Saved WifiP2pConfig for a peer connection
@@ -501,17 +501,20 @@ public boolean processMessage(Message message) {
501501
WifiP2pManager.BUSY);
502502
break;
503503
case WifiP2pManager.REQUEST_PEERS:
504-
replyToMessage(message, WifiP2pManager.RESPONSE_PEERS, mPeers);
504+
replyToMessage(message, WifiP2pManager.RESPONSE_PEERS,
505+
new WifiP2pDeviceList(mPeers));
505506
break;
506507
case WifiP2pManager.REQUEST_CONNECTION_INFO:
507-
replyToMessage(message, WifiP2pManager.RESPONSE_CONNECTION_INFO, mWifiP2pInfo);
508+
replyToMessage(message, WifiP2pManager.RESPONSE_CONNECTION_INFO,
509+
new WifiP2pInfo(mWifiP2pInfo));
508510
break;
509511
case WifiP2pManager.REQUEST_GROUP_INFO:
510-
replyToMessage(message, WifiP2pManager.RESPONSE_GROUP_INFO, mGroup);
512+
replyToMessage(message, WifiP2pManager.RESPONSE_GROUP_INFO,
513+
mGroup != null ? new WifiP2pGroup(mGroup) : null);
511514
break;
512515
case WifiP2pManager.REQUEST_PERSISTENT_GROUP_INFO:
513516
replyToMessage(message, WifiP2pManager.RESPONSE_PERSISTENT_GROUP_INFO,
514-
mGroups);
517+
new WifiP2pGroupList(mGroups, null));
515518
break;
516519
case WifiP2pManager.SET_DIALOG_LISTENER:
517520
String appPkgName = (String)message.getData().getString(

0 commit comments

Comments
 (0)