@@ -120,6 +120,12 @@ final class WifiDisplayController implements DumpUtils.Dump {
120120 // or are not trying to connect.
121121 private WifiP2pDevice mConnectingDevice ;
122122
123+ // The device from which we are currently disconnecting.
124+ private WifiP2pDevice mDisconnectingDevice ;
125+
126+ // The device to which we were previously trying to connect and are now canceling.
127+ private WifiP2pDevice mCancelingDevice ;
128+
123129 // The device to which we are currently connected, which means we have an active P2P group.
124130 private WifiP2pDevice mConnectedDevice ;
125131
@@ -186,6 +192,7 @@ private void updateSettings() {
186192 updateWfdEnableState ();
187193 }
188194
195+ @ Override
189196 public void dump (PrintWriter pw ) {
190197 pw .println ("mWifiDisplayOnSetting=" + mWifiDisplayOnSetting );
191198 pw .println ("mWifiP2pEnabled=" + mWifiP2pEnabled );
@@ -196,6 +203,8 @@ public void dump(PrintWriter pw) {
196203 pw .println ("mDiscoverPeersRetriesLeft=" + mDiscoverPeersRetriesLeft );
197204 pw .println ("mDesiredDevice=" + describeWifiP2pDevice (mDesiredDevice ));
198205 pw .println ("mConnectingDisplay=" + describeWifiP2pDevice (mConnectingDevice ));
206+ pw .println ("mDisconnectingDisplay=" + describeWifiP2pDevice (mDisconnectingDevice ));
207+ pw .println ("mCancelingDisplay=" + describeWifiP2pDevice (mCancelingDevice ));
199208 pw .println ("mConnectedDevice=" + describeWifiP2pDevice (mConnectedDevice ));
200209 pw .println ("mConnectionRetriesLeft=" + mConnectionRetriesLeft );
201210 pw .println ("mRemoteDisplay=" + mRemoteDisplay );
@@ -384,7 +393,9 @@ private void handleScanFinished() {
384393 final int count = mAvailableWifiDisplayPeers .size ();
385394 final WifiDisplay [] displays = WifiDisplay .CREATOR .newArray (count );
386395 for (int i = 0 ; i < count ; i ++) {
387- displays [i ] = createWifiDisplay (mAvailableWifiDisplayPeers .get (i ));
396+ WifiP2pDevice device = mAvailableWifiDisplayPeers .get (i );
397+ displays [i ] = createWifiDisplay (device );
398+ updateDesiredDevice (device );
388399 }
389400
390401 mHandler .post (new Runnable () {
@@ -395,6 +406,23 @@ public void run() {
395406 });
396407 }
397408
409+ private void updateDesiredDevice (WifiP2pDevice device ) {
410+ // Handle the case where the device to which we are connecting or connected
411+ // may have been renamed or reported different properties in the latest scan.
412+ final String address = device .deviceAddress ;
413+ if (mDesiredDevice != null && mDesiredDevice .deviceAddress .equals (address )) {
414+ if (DEBUG ) {
415+ Slog .d (TAG , "updateDesiredDevice: new information "
416+ + describeWifiP2pDevice (device ));
417+ }
418+ mDesiredDevice .update (device );
419+ if (mAdvertisedDisplay != null
420+ && mAdvertisedDisplay .getDeviceAddress ().equals (address )) {
421+ readvertiseDisplay (createWifiDisplay (mDesiredDevice ));
422+ }
423+ }
424+ }
425+
398426 private void connect (final WifiP2pDevice device ) {
399427 if (mDesiredDevice != null
400428 && !mDesiredDevice .deviceAddress .equals (device .deviceAddress )) {
@@ -459,12 +487,17 @@ private void updateConnection() {
459487 }
460488
461489 // Step 2. Before we try to connect to a new device, disconnect from the old one.
490+ if (mDisconnectingDevice != null ) {
491+ return ; // wait for asynchronous callback
492+ }
462493 if (mConnectedDevice != null && mConnectedDevice != mDesiredDevice ) {
463494 Slog .i (TAG , "Disconnecting from Wifi display: " + mConnectedDevice .deviceName );
495+ mDisconnectingDevice = mConnectedDevice ;
496+ mConnectedDevice = null ;
464497
465498 unadvertiseDisplay ();
466499
467- final WifiP2pDevice oldDevice = mConnectedDevice ;
500+ final WifiP2pDevice oldDevice = mDisconnectingDevice ;
468501 mWifiP2pManager .removeGroup (mWifiP2pChannel , new ActionListener () {
469502 @ Override
470503 public void onSuccess () {
@@ -480,8 +513,8 @@ public void onFailure(int reason) {
480513 }
481514
482515 private void next () {
483- if (mConnectedDevice == oldDevice ) {
484- mConnectedDevice = null ;
516+ if (mDisconnectingDevice == oldDevice ) {
517+ mDisconnectingDevice = null ;
485518 updateConnection ();
486519 }
487520 }
@@ -491,13 +524,18 @@ private void next() {
491524
492525 // Step 3. Before we try to connect to a new device, stop trying to connect
493526 // to the old one.
527+ if (mCancelingDevice != null ) {
528+ return ; // wait for asynchronous callback
529+ }
494530 if (mConnectingDevice != null && mConnectingDevice != mDesiredDevice ) {
495531 Slog .i (TAG , "Canceling connection to Wifi display: " + mConnectingDevice .deviceName );
532+ mCancelingDevice = mConnectingDevice ;
533+ mConnectingDevice = null ;
496534
497535 unadvertiseDisplay ();
498536 mHandler .removeCallbacks (mConnectionTimeout );
499537
500- final WifiP2pDevice oldDevice = mConnectingDevice ;
538+ final WifiP2pDevice oldDevice = mCancelingDevice ;
501539 mWifiP2pManager .cancelConnect (mWifiP2pChannel , new ActionListener () {
502540 @ Override
503541 public void onSuccess () {
@@ -513,8 +551,8 @@ public void onFailure(int reason) {
513551 }
514552
515553 private void next () {
516- if (mConnectingDevice == oldDevice ) {
517- mConnectingDevice = null ;
554+ if (mCancelingDevice == oldDevice ) {
555+ mCancelingDevice = null ;
518556 updateConnection ();
519557 }
520558 }
@@ -763,13 +801,17 @@ private void advertiseDisplay(final WifiDisplay display,
763801 public void run () {
764802 if (oldSurface != null && surface != oldSurface ) {
765803 mListener .onDisplayDisconnected ();
766- } else if (oldDisplay != null && !Objects . equal (display , oldDisplay )) {
804+ } else if (oldDisplay != null && !oldDisplay . hasSameAddress (display )) {
767805 mListener .onDisplayConnectionFailed ();
768806 }
769807
770808 if (display != null ) {
771- if (!Objects . equal ( display , oldDisplay )) {
809+ if (!display . hasSameAddress ( oldDisplay )) {
772810 mListener .onDisplayConnecting (display );
811+ } else if (!display .equals (oldDisplay )) {
812+ // The address is the same but some other property such as the
813+ // name must have changed.
814+ mListener .onDisplayChanged (display );
773815 }
774816 if (surface != null && surface != oldSurface ) {
775817 mListener .onDisplayConnected (display , surface , width , height , flags );
@@ -784,6 +826,12 @@ private void unadvertiseDisplay() {
784826 advertiseDisplay (null , null , 0 , 0 , 0 );
785827 }
786828
829+ private void readvertiseDisplay (WifiDisplay display ) {
830+ advertiseDisplay (display , mAdvertisedDisplaySurface ,
831+ mAdvertisedDisplayWidth , mAdvertisedDisplayHeight ,
832+ mAdvertisedDisplayFlags );
833+ }
834+
787835 private static Inet4Address getInterfaceAddress (WifiP2pGroup info ) {
788836 NetworkInterface iface ;
789837 try {
@@ -885,6 +933,7 @@ public interface Listener {
885933
886934 void onDisplayConnecting (WifiDisplay display );
887935 void onDisplayConnectionFailed ();
936+ void onDisplayChanged (WifiDisplay display );
888937 void onDisplayConnected (WifiDisplay display ,
889938 Surface surface , int width , int height , int flags );
890939 void onDisplayDisconnected ();
0 commit comments