Consistently report keyboard open|close events in GameActivity.

Fixes: 399154889
Test: local

Change-Id: Iec92e67949dd4c9b845f667880bea6615d3c9f2e

diff --git a/game-activity/src/main/java/com/google/androidgamesdk/GameActivity.java b/game-activity/src/main/java/com/google/androidgamesdk/GameActivity.java
index 2607db2..d9ec96c 100644
--- a/game-activity/src/main/java/com/google/androidgamesdk/GameActivity.java
+++ b/game-activity/src/main/java/com/google/androidgamesdk/GameActivity.java
@@ -75,6 +75,8 @@
 
   private EditorInfo imeEditorInfo;
 
+  private boolean softwareKeyboardVisible = false;
+
   /**
    * The SurfaceView used by default for displaying the game and getting a text input.
    * You can override its creation in `onCreateSurfaceView`.
@@ -481,7 +483,12 @@
 
   @Override
   public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
-    this.mSurfaceView.mInputConnection.onApplyWindowInsets(v, insets);
+    this.onImeInsetsChanged(insets.getInsets(WindowInsetsCompat.Type.ime()));
+    boolean keyboardVisible = isSoftwareKeyboardVisible(insets);
+    if (keyboardVisible != softwareKeyboardVisible) {
+      softwareKeyboardVisible = keyboardVisible;
+      onSoftwareKeyboardVisibilityChanged(keyboardVisible);
+    }
     onWindowInsetsChangedNative(mNativeHandle);
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
       // Pass through to the view - we don't want to handle the insets, just observe them.
@@ -609,4 +616,12 @@
       mInputConnection.setEditorInfo(e);
     }
   }
+
+  private boolean isSoftwareKeyboardVisible(WindowInsetsCompat insets) {
+    if (insets == null) {
+      return false;
+    }
+
+    return insets.isVisible(WindowInsetsCompat.Type.ime());
+  }
 }
diff --git a/game-text-input/src/main/java/com/google/androidgamesdk/gametextinput/InputConnection.java b/game-text-input/src/main/java/com/google/androidgamesdk/gametextinput/InputConnection.java
index a890475..c70a297 100644
--- a/game-text-input/src/main/java/com/google/androidgamesdk/gametextinput/InputConnection.java
+++ b/game-text-input/src/main/java/com/google/androidgamesdk/gametextinput/InputConnection.java
@@ -117,9 +117,6 @@
     targetView.setOnKeyListener(this);
     // Apply EditorInfo settings
     this.setEditorInfo(settings.mEditorInfo);
-
-    ViewCompat.setOnApplyWindowInsetsListener(
-        targetView, (v, insets) -> onApplyWindowInsets(v, insets));
   }
 
   /**
@@ -148,6 +145,7 @@
    */
   public final void setSoftKeyboardActive(boolean active, int flags) {
     Log.d(TAG, "setSoftKeyboardActive, active: " + active);
+
     this.mSoftKeyboardActive = active;
     if (active) {
       this.targetView.setFocusableInTouchMode(true);
@@ -531,36 +529,6 @@
   }
 
   /**
-   * This function is called whenever software keyboard (IME) changes its visible dimensions.
-   *
-   * @param v main application View
-   * @param insets insets of the software keyboard (IME)
-   * @return this function should return original insets object unless it wants to modify insets.
-   */
-  public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
-    Log.d(TAG, "onApplyWindowInsets: " + this.isSoftwareKeyboardVisible());
-
-    Listener listener = this.listener;
-    if (listener != null) {
-      listener.onImeInsetsChanged(insets.getInsets(WindowInsetsCompat.Type.ime()));
-    }
-
-    boolean visible = this.isSoftwareKeyboardVisible();
-    if (visible == this.mSoftKeyboardActive) {
-      return insets;
-    }
-
-    this.mSoftKeyboardActive = visible;
-    imm.restartInput(targetView);
-
-    if (listener != null) {
-      listener.onSoftwareKeyboardVisibilityChanged(visible);
-    }
-
-    return insets;
-  }
-
-  /**
    * Get the current IME insets.
    *
    * @return The current IME insets