Commit f277bafe authored by aurimas's avatar aurimas Committed by Commit bot

Update showKeyboard method to be more aggressive.

Keyboard does not show up in certain cases (like landscape phones on JB devices)
by simply using InputMethodManager.SHOW_IMPLICIT.

BUG=411854

Review URL: https://codereview.chromium.org/620263002

Cr-Commit-Position: refs/heads/master@{#297944}
parent 32759c4b
...@@ -8,12 +8,15 @@ import android.content.Context; ...@@ -8,12 +8,15 @@ import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Utility functions for common Android UI tasks. * Utility functions for common Android UI tasks.
* This class is not supposed to be instantiated. * This class is not supposed to be instantiated.
...@@ -21,6 +24,9 @@ import android.view.inputmethod.InputMethodManager; ...@@ -21,6 +24,9 @@ import android.view.inputmethod.InputMethodManager;
public class UiUtils { public class UiUtils {
private static final String TAG = "UiUtils"; private static final String TAG = "UiUtils";
private static final int KEYBOARD_RETRY_ATTEMPTS = 10;
private static final long KEYBOARD_RETRY_DELAY_MS = 100;
/** /**
* Guards this class from being instantiated. * Guards this class from being instantiated.
*/ */
...@@ -59,12 +65,29 @@ public class UiUtils { ...@@ -59,12 +65,29 @@ public class UiUtils {
* Shows the software keyboard if necessary. * Shows the software keyboard if necessary.
* @param view The currently focused {@link View}, which would receive soft keyboard input. * @param view The currently focused {@link View}, which would receive soft keyboard input.
*/ */
public static void showKeyboard(View view) { public static void showKeyboard(final View view) {
InputMethodManager imm = final Handler handler = new Handler();
(InputMethodManager) view.getContext().getSystemService( final AtomicInteger attempt = new AtomicInteger();
Context.INPUT_METHOD_SERVICE); Runnable openRunnable = new Runnable() {
// Only shows soft keyboard if there isn't an open physical keyboard. @Override
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); public void run() {
// Not passing InputMethodManager.SHOW_IMPLICIT as it does not trigger the
// keyboard in landscape mode.
InputMethodManager imm =
(InputMethodManager) view.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
try {
imm.showSoftInput(view, 0);
} catch (IllegalArgumentException e) {
if (attempt.incrementAndGet() <= KEYBOARD_RETRY_ATTEMPTS) {
handler.postDelayed(this, KEYBOARD_RETRY_DELAY_MS);
} else {
Log.e(TAG, "Unable to open keyboard. Giving up.", e);
}
}
}
};
openRunnable.run();
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment