OverKeyboardPopupWindow.kt 4.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
/**
 * Copyright 2015 YA LLC
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package chat.rocket.android.widget.emoji

import android.content.Context
import android.graphics.Point
import android.graphics.Rect
import android.os.Build
import android.view.*
import android.widget.PopupWindow
import chat.rocket.android.BuildConfig
import chat.rocket.android.R

/**
 * Base class to create popup window that appears over software keyboard.
 */
abstract class OverKeyboardPopupWindow(val context: Context, private val rootView: View) : PopupWindow(context), ViewTreeObserver.OnGlobalLayoutListener {

    /**
     * @return keyboard height in pixels
     */
    var keyboardHeight = 0
        private set
    private var pendingOpen = false
    /**
     * @return Returns true if the soft keyboard is open, false otherwise.
     */
    var isKeyboardOpen = false
        private set

    private var keyboardHideListener: OnKeyboardHideListener? = null

    interface OnKeyboardHideListener {
        fun onKeyboardHide()
    }

    init {
        setBackgroundDrawable(null)
        if (BuildConfig.VERSION_CODE >= Build.VERSION_CODES.LOLLIPOP) {
            elevation = 0f
        }
        val view = onCreateView(LayoutInflater.from(context))
        onViewCreated(view)
        contentView = view
        softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE

        // Default size
        setSize(this.context.resources.getDimensionPixelSize(R.dimen.supposed_keyboard_height),
                WindowManager.LayoutParams.MATCH_PARENT)
        setSizeForSoftKeyboard()
    }

    fun setKeyboardHideListener(keyboardHideListener: OnKeyboardHideListener) {
        this.keyboardHideListener = keyboardHideListener
    }

    /**
     * Manually set the popup window size
     *
     * @param width  Width of the popup
     * @param height Height of the popup
     */
    fun setSize(width: Int, height: Int) {
        setWidth(width)
        setHeight(height)
    }

    /**
     * Call this function to resize the emoji popup according to your soft keyboard size
     */
    fun setSizeForSoftKeyboard() {
        val viewTreeObserver = rootView.viewTreeObserver
        viewTreeObserver.addOnGlobalLayoutListener(this)
    }

    override fun onGlobalLayout() {
        val r = Rect()
        rootView.getWindowVisibleDisplayFrame(r)

        val screenHeight = calculateScreenHeight()
        var heightDifference = screenHeight - (r.bottom - r.top)

        val resources = context.resources
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            heightDifference -= resources.getDimensionPixelSize(resourceId)
        }

        if (heightDifference > 100) {
            keyboardHeight = heightDifference
            setSize(WindowManager.LayoutParams.MATCH_PARENT, keyboardHeight)

            isKeyboardOpen = true
            if (pendingOpen) {
                showAtBottom()
                pendingOpen = false
            }
        } else {
            if (isKeyboardOpen && keyboardHideListener != null) {
                keyboardHideListener!!.onKeyboardHide()
            }
            isKeyboardOpen = false
        }
    }

    private fun calculateScreenHeight(): Int {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.getDefaultDisplay()
        val size = Point()
        display.getSize(size)
        return size.y
    }

    /**
     * Use this function to show the popup.
     * NOTE: Since, the soft keyboard sizes are variable on different android devices, the
     * library needs you to open the soft keyboard at least once before calling this function.
     * If that is not possible see showAtBottomPending() function.
     */
    fun showAtBottom() {
        showAtLocation(rootView, Gravity.BOTTOM, 0, 0)
    }

    /**
     * Use this function when the soft keyboard has not been opened yet. This
     * will show the popup after the keyboard is up next time.
     * Generally, you will be calling InputMethodManager.showSoftInput function after
     * calling this function.
     */
    fun showAtBottomPending() {
        if (isKeyboardOpen) {
            showAtBottom()
        } else {
            pendingOpen = true
        }
    }

    abstract fun onCreateView(inflater: LayoutInflater): View

    abstract fun onViewCreated(view: View)


}