|
| 1 | +package org.apache.cordova.statusbar; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.graphics.Rect; |
| 5 | +import android.view.View; |
| 6 | +import android.view.ViewTreeObserver; |
| 7 | +import android.widget.FrameLayout; |
| 8 | + |
| 9 | +public class StatusBarViewHelper { |
| 10 | + |
| 11 | + // For more information, see https://issuetracker.google.com/issues/36911528 |
| 12 | + // To use this class, simply invoke assistActivity() on an Activity that already has its content view set. |
| 13 | + |
| 14 | + //This solution was based off of |
| 15 | + //https://stackoverflow.com/questions/7417123/android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visible/19494006#answer-42261118 |
| 16 | + |
| 17 | + private View mChildOfContent; |
| 18 | + private int usableHeightPrevious; |
| 19 | + private FrameLayout.LayoutParams frameLayoutParams; |
| 20 | + private Activity activity; |
| 21 | + private StatusBar statusbar; |
| 22 | + private static final String TAG = "StatusBarViewHelper"; |
| 23 | + |
| 24 | + static void assist(Activity activity, StatusBar statusbar) { |
| 25 | + new StatusBarViewHelper(activity, statusbar); |
| 26 | + } |
| 27 | + |
| 28 | + private StatusBarViewHelper(Activity a, StatusBar b) { |
| 29 | + activity = a; |
| 30 | + statusbar = b; |
| 31 | + |
| 32 | + FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); |
| 33 | + mChildOfContent = content.getChildAt(0); |
| 34 | + |
| 35 | + mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { |
| 36 | + public void onGlobalLayout() { |
| 37 | + possiblyResizeChildOfContent(); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams(); |
| 42 | + } |
| 43 | + |
| 44 | + private void possiblyResizeChildOfContent() { |
| 45 | + int usableHeightNow = computeUsableHeight(); |
| 46 | + if (usableHeightNow != usableHeightPrevious) { |
| 47 | + frameLayoutParams.height = usableHeightNow; |
| 48 | + mChildOfContent.requestLayout(); |
| 49 | + usableHeightPrevious = usableHeightNow; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private boolean _isStatusBarVisible() { |
| 54 | + return statusbar.isVisible(); |
| 55 | + } |
| 56 | + |
| 57 | + private int computeUsableHeight() { |
| 58 | + Rect r = new Rect(); |
| 59 | + mChildOfContent.getWindowVisibleDisplayFrame(r); |
| 60 | + int uiOptions = activity.getWindow().getDecorView().getSystemUiVisibility(); |
| 61 | + boolean isFullscreen = ((uiOptions | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) == uiOptions); |
| 62 | + boolean isStatusBarVisible = this._isStatusBarVisible(); |
| 63 | + |
| 64 | + int usableHeight = r.bottom; |
| 65 | + |
| 66 | + // This handles both overlayed status bars and reserved spaces for cutouts when the |
| 67 | + // status bar is hidden |
| 68 | + if (!isFullscreen || (isFullscreen && !isStatusBarVisible)) { |
| 69 | + usableHeight = usableHeight - r.top; |
| 70 | + } |
| 71 | + |
| 72 | + return usableHeight; |
| 73 | + } |
| 74 | +} |
0 commit comments