From bd98619d4ff62125be92dc816bdd86f4d17eb8fe Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Wed, 23 Mar 2016 21:53:05 -0400 Subject: [PATCH] Fixed bug where keyboard would cover text input on the webview --- app/src/main/AndroidManifest.xml | 2 +- .../lightning/activity/BrowserActivity.java | 43 ++++++++++-- .../lightning/utils/KeyboardHelper.java | 67 +++++++++++++++++++ app/src/main/res/layout/browser_content.xml | 6 +- 4 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/acr/browser/lightning/utils/KeyboardHelper.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a1e9dfd..ebedf12 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -35,7 +35,7 @@ android:label="@string/app_name" android:launchMode="singleTask" android:theme="@style/Theme.LightTheme" - android:windowSoftInputMode="adjustResize|adjustPan"> + android:windowSoftInputMode="adjustResize"> diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index 4ab9db4..83c763e 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -17,6 +17,7 @@ import android.database.sqlite.SQLiteException; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.PorterDuff; +import android.graphics.Rect; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.media.MediaPlayer; @@ -116,6 +117,7 @@ import acr.browser.lightning.react.Observable; import acr.browser.lightning.react.Schedulers; import acr.browser.lightning.receiver.NetworkReceiver; import acr.browser.lightning.utils.DrawableUtils; +import acr.browser.lightning.utils.KeyboardHelper; import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.UrlUtils; @@ -154,7 +156,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements private ImageView mArrowImage; // Current tab view being displayed - private View mCurrentView; + @Nullable private View mCurrentView; // Full Screen Video Views private FrameLayout mFullscreenContainer; @@ -250,6 +252,17 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements initialize(); + KeyboardHelper keyboardHelper = new KeyboardHelper(mRoot); + keyboardHelper.registerKeyboardListener(new KeyboardHelper.KeyboardListener() { + @Override + public void keyboardVisibilityChanged(boolean visible) { + if (visible) { + setTabHeightForKeyboard(); + } else { + setTabHeight(); + } + } + }); } private synchronized void initialize() { @@ -603,7 +616,9 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements initializeTabHeight(); - if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { + // Sets the tab height correctly if the status bar is hidden + // Also ensures that tab is correct height on rotation mRoot.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) @Override @@ -1081,7 +1096,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements initializeToolbarHeight(newConfig); } - private void initializeToolbarHeight(@NonNull final Configuration configuration){ + private void initializeToolbarHeight(@NonNull final Configuration configuration) { // TODO externalize the dimensions doOnLayout(mUiLayout, new Runnable() { @Override @@ -1308,7 +1323,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements private int getSearchBarColor(int requestedColor, int defaultColor) { if (requestedColor == defaultColor) { - return mDarkTheme ? DrawableUtils.mixColor(0.25f, defaultColor, Color.WHITE): Color.WHITE; + return mDarkTheme ? DrawableUtils.mixColor(0.25f, defaultColor, Color.WHITE) : Color.WHITE; } else { return DrawableUtils.mixColor(0.25f, requestedColor, Color.WHITE); } @@ -1948,7 +1963,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements private void setTabHeight() { Log.d(TAG, "setTabHeight"); if (mRoot.getHeight() == 0) { - mRoot.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); + mRoot.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY); } Log.d(TAG, "UI Layout top: " + mUiLayout.getTop()); @@ -1957,12 +1972,28 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements int height = mRoot.getHeight() - mUiLayout.getTop(); mBrowserFrame.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height)); } else { - mBrowserFrame.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + params.weight = 1; + mBrowserFrame.setLayoutParams(params); } mBrowserFrame.requestLayout(); } + private void setTabHeightForKeyboard() { + doOnLayout(mUiLayout, new Runnable() { + @Override + public void run() { + Rect rect = new Rect(); + mRoot.getWindowVisibleDisplayFrame(rect); + + int height = rect.bottom - mUiLayout.getTop(); + mBrowserFrame.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height)); + mBrowserFrame.requestLayout(); + } + }); + } + /** * This method lets the search bar know that the page is currently loading * and that it should display the stop icon to indicate to the user that diff --git a/app/src/main/java/acr/browser/lightning/utils/KeyboardHelper.java b/app/src/main/java/acr/browser/lightning/utils/KeyboardHelper.java new file mode 100644 index 0000000..2bfb37f --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/utils/KeyboardHelper.java @@ -0,0 +1,67 @@ +package acr.browser.lightning.utils; + +import android.graphics.Rect; +import android.support.annotation.NonNull; +import android.view.View; +import android.view.ViewTreeObserver; + +public class KeyboardHelper { + + public interface KeyboardListener { + /** + * Called when the visibility of the keyboard changes. + * Parameter tells whether the keyboard has been shown + * or hidden. + * + * @param visible true if the keyboard has been shown, + * false otherwise. + */ + void keyboardVisibilityChanged(boolean visible); + } + + @NonNull private View mView; + private int mLastRight = -1; + private int mLastBottom = -1; + + /** + * Constructor + * + * @param view the view to listen on, should be + * the {@link android.R.id#content} view. + */ + public KeyboardHelper(@NonNull View view) { + mView = view; + } + + /** + * Registers a {@link KeyboardListener} to receive + * callbacks when the keyboard is shown for the specific + * view. The view used should be the content view as it + * will receive resize events from the system. + * + * @param listener the listener to register to receive events. + */ + public void registerKeyboardListener(@NonNull final KeyboardListener listener) { + mView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + Rect rect = new Rect(); + if (mLastBottom == -1) { + mLastBottom = rect.bottom; + } + if (mLastRight == -1) { + mLastRight = rect.right; + } + mView.getWindowVisibleDisplayFrame(rect); + if (mLastRight == rect.right && rect.bottom < mLastBottom) { + listener.keyboardVisibilityChanged(true); + } else if (mLastRight == rect.right && rect.bottom > mLastBottom) { + listener.keyboardVisibilityChanged(false); + } + mLastBottom = rect.bottom; + mLastRight = rect.right; + } + }); + } + +} diff --git a/app/src/main/res/layout/browser_content.xml b/app/src/main/res/layout/browser_content.xml index 3eb6969..d0839e6 100644 --- a/app/src/main/res/layout/browser_content.xml +++ b/app/src/main/res/layout/browser_content.xml @@ -1,7 +1,9 @@ - + android:background="@color/primary_color" + android:clipChildren="true"/>