Fix for UiDevice press button operations wait for idle

This cl#299334 attempted to address a missing wait for idle
on the UiDevice main pressXXX operation since issues related to
reliability were surfacing. These are pressHome, pressBack
and pressMenu. In error, I added the waitForIdle after and
not before the operation where it should've been in the first place.

Operations should never count on other operations to guarantee
waitForIdle for them. Each operation should ensure its own device
idle before performing any action. The existing implementation
unfortunately didn't improve the reliability and issues remained.

This fix should put these pressXXX commands inline with all the
UiAutomator APIs where everything  performs a waitForIdle _before_
performing any action.

Change-Id: I76d7ec50fb822d41a0be4808a0d3e61bdde17935
diff --git a/uiautomator/library/core-src/com/android/uiautomator/core/UiDevice.java b/uiautomator/library/core-src/com/android/uiautomator/core/UiDevice.java
index de7e760..5a2f3a7 100644
--- a/uiautomator/library/core-src/com/android/uiautomator/core/UiDevice.java
+++ b/uiautomator/library/core-src/com/android/uiautomator/core/UiDevice.java
@@ -186,11 +186,10 @@
      */
     public boolean pressMenu() {
         Tracer.trace();
-        boolean ret = mUiAutomationBridge.getInteractionController().sendKeyAndWaitForEvent(
+        waitForIdle();
+        return mUiAutomationBridge.getInteractionController().sendKeyAndWaitForEvent(
                 KeyEvent.KEYCODE_MENU, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
                 KEY_PRESS_EVENT_TIMEOUT);
-        waitForIdle();
-        return ret;
     }
 
     /**
@@ -200,11 +199,10 @@
      */
     public boolean pressBack() {
         Tracer.trace();
-        boolean ret = mUiAutomationBridge.getInteractionController().sendKeyAndWaitForEvent(
+        waitForIdle();
+        return mUiAutomationBridge.getInteractionController().sendKeyAndWaitForEvent(
                 KeyEvent.KEYCODE_BACK, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
                 KEY_PRESS_EVENT_TIMEOUT);
-        waitForIdle();
-        return ret;
     }
 
     /**
@@ -214,11 +212,10 @@
      */
     public boolean pressHome() {
         Tracer.trace();
-        boolean ret = mUiAutomationBridge.getInteractionController().sendKeyAndWaitForEvent(
+        waitForIdle();
+        return mUiAutomationBridge.getInteractionController().sendKeyAndWaitForEvent(
                 KeyEvent.KEYCODE_HOME, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
                 KEY_PRESS_EVENT_TIMEOUT);
-        waitForIdle();
-        return ret;
     }
 
     /**
@@ -310,9 +307,8 @@
      */
     public boolean pressKeyCode(int keyCode) {
         Tracer.trace(keyCode);
-        boolean ret = mUiAutomationBridge.getInteractionController().sendKey(keyCode, 0);
         waitForIdle();
-        return ret;
+        return mUiAutomationBridge.getInteractionController().sendKey(keyCode, 0);
     }
 
     /**
@@ -326,9 +322,8 @@
      */
     public boolean pressKeyCode(int keyCode, int metaState) {
         Tracer.trace(keyCode, metaState);
-        boolean ret = mUiAutomationBridge.getInteractionController().sendKey(keyCode, metaState);
         waitForIdle();
-        return ret;
+        return mUiAutomationBridge.getInteractionController().sendKey(keyCode, metaState);
     }
 
     /**
@@ -340,9 +335,8 @@
      */
     public boolean pressRecentApps() throws RemoteException {
         Tracer.trace();
-        boolean ret = getAutomatorBridge().getInteractionController().toggleRecentApps();
         waitForIdle();
-        return ret;
+        return getAutomatorBridge().getInteractionController().toggleRecentApps();
     }
 
     /**
@@ -353,9 +347,8 @@
      */
     public boolean openNotification() {
         Tracer.trace();
-        boolean ret =  getAutomatorBridge().getInteractionController().openNotification();
         waitForIdle();
-        return ret;
+        return  getAutomatorBridge().getInteractionController().openNotification();
     }
 
     /**
@@ -366,9 +359,8 @@
      */
     public boolean openQuickSettings() {
         Tracer.trace();
-        boolean ret = getAutomatorBridge().getInteractionController().openQuickSettings();
         waitForIdle();
-        return ret;
+        return getAutomatorBridge().getInteractionController().openQuickSettings();
     }
 
     /**
@@ -627,6 +619,7 @@
      */
     public boolean isNaturalOrientation() {
         Tracer.trace();
+        waitForIdle();
         int ret = mUiAutomationBridge.getRotation();
         return ret == UiAutomation.ROTATION_FREEZE_0 ||
                 ret == UiAutomation.ROTATION_FREEZE_180;
@@ -676,7 +669,7 @@
     public void setOrientationLeft() throws RemoteException {
         Tracer.trace();
         getAutomatorBridge().getInteractionController().setRotationLeft();
-        waitForIdle();
+        waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
     }
 
     /**
@@ -691,7 +684,7 @@
     public void setOrientationRight() throws RemoteException {
         Tracer.trace();
         getAutomatorBridge().getInteractionController().setRotationRight();
-        waitForIdle();
+        waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
     }
 
     /**
@@ -706,7 +699,7 @@
     public void setOrientationNatural() throws RemoteException {
         Tracer.trace();
         getAutomatorBridge().getInteractionController().setRotationNatural();
-        waitForIdle();
+        waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
     }
 
     /**