Skip to content

Commit 7f00c22

Browse files
isheriffAndroid (Google) Code Review
authored andcommitted
Merge "Avoid duplicate dialogs leading to NPE" into ics-mr0
2 parents 451fa13 + bbe77ca commit 7f00c22

File tree

1 file changed

+101
-14
lines changed

1 file changed

+101
-14
lines changed

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

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
*/
8282
public class WifiP2pService extends IWifiP2pManager.Stub {
8383
private static final String TAG = "WifiP2pService";
84-
private static final boolean DBG = true;
84+
private static final boolean DBG = false;
8585
private static final String NETWORKTYPE = "WIFI_P2P";
8686

8787
private Context mContext;
@@ -131,12 +131,22 @@ public class WifiP2pService extends IWifiP2pManager.Stub {
131131
/* User rejected to disable Wi-Fi in order to enable p2p */
132132
private static final int WIFI_DISABLE_USER_REJECT = BASE + 5;
133133

134+
/* User accepted a group negotiation request */
135+
private static final int GROUP_NEGOTIATION_USER_ACCEPT = BASE + 6;
136+
/* User rejected a group negotiation request */
137+
private static final int GROUP_NEGOTIATION_USER_REJECT = BASE + 7;
138+
139+
/* User accepted a group invitation request */
140+
private static final int GROUP_INVITATION_USER_ACCEPT = BASE + 8;
141+
/* User rejected a group invitation request */
142+
private static final int GROUP_INVITATION_USER_REJECT = BASE + 9;
143+
134144
/* Airplane mode changed */
135-
private static final int AIRPLANE_MODE_CHANGED = BASE + 6;
145+
private static final int AIRPLANE_MODE_CHANGED = BASE + 10;
136146
/* Emergency callback mode */
137-
private static final int EMERGENCY_CALLBACK_MODE = BASE + 7;
138-
private static final int WPS_PBC = BASE + 8;
139-
private static final int WPS_PIN = BASE + 9;
147+
private static final int EMERGENCY_CALLBACK_MODE = BASE + 11;
148+
private static final int WPS_PBC = BASE + 12;
149+
private static final int WPS_PIN = BASE + 13;
140150

141151
private final boolean mP2pSupported;
142152

@@ -260,6 +270,10 @@ private class P2pStateMachine extends StateMachine {
260270
private P2pEnabledState mP2pEnabledState = new P2pEnabledState();
261271
// Inactive is when p2p is enabled with no connectivity
262272
private InactiveState mInactiveState = new InactiveState();
273+
private UserAuthorizingGroupNegotiationState mUserAuthorizingGroupNegotiationState
274+
= new UserAuthorizingGroupNegotiationState();
275+
private UserAuthorizingGroupInvitationState mUserAuthorizingGroupInvitationState
276+
= new UserAuthorizingGroupInvitationState();
263277
private GroupNegotiationState mGroupNegotiationState = new GroupNegotiationState();
264278
private GroupCreatedState mGroupCreatedState = new GroupCreatedState();
265279

@@ -290,6 +304,8 @@ private class P2pStateMachine extends StateMachine {
290304
addState(mP2pEnablingState, mDefaultState);
291305
addState(mP2pEnabledState, mDefaultState);
292306
addState(mInactiveState, mP2pEnabledState);
307+
addState(mUserAuthorizingGroupNegotiationState, mInactiveState);
308+
addState(mUserAuthorizingGroupInvitationState, mInactiveState);
293309
addState(mGroupNegotiationState, mP2pEnabledState);
294310
addState(mGroupCreatedState, mP2pEnabledState);
295311

@@ -379,6 +395,10 @@ public boolean processMessage(Message message) {
379395
// Ignore
380396
case WIFI_DISABLE_USER_ACCEPT:
381397
case WIFI_DISABLE_USER_REJECT:
398+
case GROUP_NEGOTIATION_USER_ACCEPT:
399+
case GROUP_NEGOTIATION_USER_REJECT:
400+
case GROUP_INVITATION_USER_ACCEPT:
401+
case GROUP_INVITATION_USER_REJECT:
382402
case GROUP_NEGOTIATION_TIMED_OUT:
383403
break;
384404
default:
@@ -747,6 +767,7 @@ public boolean processMessage(Message message) {
747767
case WifiMonitor.P2P_GO_NEGOTIATION_REQUEST_EVENT:
748768
mSavedGoNegotiationConfig = (WifiP2pConfig) message.obj;
749769
notifyP2pGoNegotationRequest(mSavedGoNegotiationConfig);
770+
transitionTo(mUserAuthorizingGroupNegotiationState);
750771
break;
751772
case WifiP2pManager.CREATE_GROUP:
752773
mPersistGroup = true;
@@ -761,6 +782,37 @@ public boolean processMessage(Message message) {
761782
case WifiMonitor.P2P_INVITATION_RECEIVED_EVENT:
762783
WifiP2pGroup group = (WifiP2pGroup) message.obj;
763784
notifyP2pInvitationReceived(group);
785+
transitionTo(mUserAuthorizingGroupInvitationState);
786+
break;
787+
default:
788+
return NOT_HANDLED;
789+
}
790+
return HANDLED;
791+
}
792+
}
793+
794+
class UserAuthorizingGroupNegotiationState extends State {
795+
@Override
796+
public void enter() {
797+
if (DBG) logd(getName());
798+
}
799+
800+
@Override
801+
public boolean processMessage(Message message) {
802+
if (DBG) logd(getName() + message.toString());
803+
switch (message.what) {
804+
case WifiMonitor.P2P_GO_NEGOTIATION_REQUEST_EVENT:
805+
case WifiMonitor.P2P_INVITATION_RECEIVED_EVENT:
806+
//Ignore additional connection requests
807+
break;
808+
case GROUP_NEGOTIATION_USER_ACCEPT:
809+
sendMessage(WifiP2pManager.CONNECT, mSavedGoNegotiationConfig);
810+
mSavedGoNegotiationConfig = null;
811+
break;
812+
case GROUP_NEGOTIATION_USER_REJECT:
813+
if (DBG) logd("User rejected incoming negotiation request");
814+
mSavedGoNegotiationConfig = null;
815+
transitionTo(mInactiveState);
764816
break;
765817
default:
766818
return NOT_HANDLED;
@@ -769,6 +821,40 @@ public boolean processMessage(Message message) {
769821
}
770822
}
771823

824+
class UserAuthorizingGroupInvitationState extends State {
825+
@Override
826+
public void enter() {
827+
if (DBG) logd(getName());
828+
}
829+
830+
@Override
831+
public boolean processMessage(Message message) {
832+
if (DBG) logd(getName() + message.toString());
833+
switch (message.what) {
834+
case WifiMonitor.P2P_GO_NEGOTIATION_REQUEST_EVENT:
835+
case WifiMonitor.P2P_INVITATION_RECEIVED_EVENT:
836+
//Ignore additional connection requests
837+
break;
838+
case GROUP_INVITATION_USER_ACCEPT:
839+
if (DBG) logd(getName() + " connect to invited group");
840+
WifiP2pConfig config = new WifiP2pConfig();
841+
config.deviceAddress = mSavedP2pGroup.getOwner().deviceAddress;
842+
sendMessage(WifiP2pManager.CONNECT, config);
843+
mSavedP2pGroup = null;
844+
break;
845+
case GROUP_INVITATION_USER_REJECT:
846+
if (DBG) logd("User rejected incoming invitation request");
847+
mSavedP2pGroup = null;
848+
transitionTo(mInactiveState);
849+
break;
850+
default:
851+
return NOT_HANDLED;
852+
}
853+
return HANDLED;
854+
}
855+
}
856+
857+
772858
class GroupNegotiationState extends State {
773859
@Override
774860
public void enter() {
@@ -1091,15 +1177,14 @@ public void onClick(DialogInterface dialog, int which) {
10911177
mSavedGoNegotiationConfig.wps.setup = WpsInfo.KEYPAD;
10921178
mSavedGoNegotiationConfig.wps.pin = pin.getText().toString();
10931179
}
1094-
sendMessage(WifiP2pManager.CONNECT, mSavedGoNegotiationConfig);
1095-
mSavedGoNegotiationConfig = null;
1180+
sendMessage(GROUP_NEGOTIATION_USER_ACCEPT);
10961181
}
10971182
})
10981183
.setNegativeButton(r.getString(R.string.cancel), new OnClickListener() {
10991184
@Override
11001185
public void onClick(DialogInterface dialog, int which) {
11011186
if (DBG) logd(getName() + " ignore connect");
1102-
mSavedGoNegotiationConfig = null;
1187+
sendMessage(GROUP_NEGOTIATION_USER_REJECT);
11031188
}
11041189
})
11051190
.create();
@@ -1180,14 +1265,16 @@ private void notifyP2pInvitationReceived(WifiP2pGroup group) {
11801265
.setView(textEntryView)
11811266
.setPositiveButton(r.getString(R.string.ok), new OnClickListener() {
11821267
public void onClick(DialogInterface dialog, int which) {
1183-
WifiP2pConfig config = new WifiP2pConfig();
1184-
config.deviceAddress = mSavedP2pGroup.getOwner().deviceAddress;
1185-
if (DBG) logd(getName() + " connect to invited group");
1186-
sendMessage(WifiP2pManager.CONNECT, config);
1187-
mSavedP2pGroup = null;
1268+
sendMessage(GROUP_INVITATION_USER_ACCEPT);
1269+
}
1270+
})
1271+
.setNegativeButton(r.getString(R.string.cancel), new OnClickListener() {
1272+
@Override
1273+
public void onClick(DialogInterface dialog, int which) {
1274+
if (DBG) logd(getName() + " ignore invite");
1275+
sendMessage(GROUP_INVITATION_USER_REJECT);
11881276
}
11891277
})
1190-
.setNegativeButton(r.getString(R.string.cancel), null)
11911278
.create();
11921279

11931280
pin.setVisibility(View.GONE);

0 commit comments

Comments
 (0)