2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.view
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.content.Context
|
2019-02-15 02:51:22 +01:00
|
|
|
import androidx.core.view.inputmethod.EditorInfoCompat
|
|
|
|
import androidx.core.view.inputmethod.InputConnectionCompat
|
|
|
|
import androidx.appcompat.widget.AppCompatEditText
|
2018-01-04 19:52:25 +01:00
|
|
|
import android.util.AttributeSet
|
|
|
|
import android.view.MotionEvent
|
2018-01-21 23:18:15 +01:00
|
|
|
import android.view.inputmethod.EditorInfo
|
|
|
|
import android.view.inputmethod.InputConnection
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.util.LogCategory
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
class MyEditText : AppCompatEditText {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private val log = LogCategory("MyEditText")
|
|
|
|
}
|
|
|
|
|
|
|
|
private var mOnSelectionChangeListener : OnSelectionChangeListener? = null
|
|
|
|
|
|
|
|
constructor(context : Context) : super(context)
|
|
|
|
constructor(context : Context, attrs : AttributeSet) : super(context, attrs)
|
2018-01-21 23:18:15 +01:00
|
|
|
constructor(context : Context, attrs : AttributeSet, defStyleAttr : Int) : super(
|
|
|
|
context,
|
|
|
|
attrs,
|
|
|
|
defStyleAttr
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// 選択範囲変更イベントをコールバックに渡す
|
|
|
|
|
|
|
|
interface OnSelectionChangeListener {
|
|
|
|
fun onSelectionChanged(selStart : Int, selEnd : Int)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setOnSelectionChangeListener(listener : OnSelectionChangeListener) {
|
|
|
|
mOnSelectionChangeListener = listener
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSelectionChanged(selStart : Int, selEnd : Int) {
|
|
|
|
super.onSelectionChanged(selStart, selEnd)
|
|
|
|
mOnSelectionChangeListener?.onSelectionChanged(selStart, selEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
// Android 6.0 でのクラッシュ対応
|
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
|
|
|
override fun onTouchEvent(event : MotionEvent) : Boolean {
|
|
|
|
return try {
|
|
|
|
super.onTouchEvent(event)
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
false
|
|
|
|
// java.lang.NullPointerException:
|
|
|
|
// at android.widget.Editor$SelectionModifierCursorController.onTouchEvent (Editor.java:4889)
|
|
|
|
// at android.widget.Editor.onTouchEvent (Editor.java:1223)
|
|
|
|
// at android.widget.TextView.onTouchEvent (TextView.java:8304)
|
|
|
|
// at android.view.View.dispatchTouchEvent (View.java:9303)
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 23:18:15 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
// IMEから画像を送られてくることがあるらしい
|
|
|
|
|
|
|
|
var commitContentListener :InputConnectionCompat.OnCommitContentListener? = null
|
|
|
|
var contentMineTypeArray :Array<String>? = null
|
|
|
|
|
|
|
|
override fun onCreateInputConnection(outAttrs : EditorInfo?) : InputConnection {
|
|
|
|
|
|
|
|
log.d("onCreateInputConnection: listener=${commitContentListener}")
|
|
|
|
|
|
|
|
val super_ic = super.onCreateInputConnection(outAttrs)
|
|
|
|
|
|
|
|
val listener = commitContentListener
|
|
|
|
val mimeArray = contentMineTypeArray
|
|
|
|
return if( listener == null || mimeArray ==null || outAttrs == null ){
|
|
|
|
super_ic
|
|
|
|
}else{
|
|
|
|
EditorInfoCompat.setContentMimeTypes(outAttrs, mimeArray)
|
|
|
|
InputConnectionCompat.createWrapper(super_ic,outAttrs,listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|