-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathPasteInputEditText.kt
More file actions
74 lines (59 loc) · 2.42 KB
/
PasteInputEditText.kt
File metadata and controls
74 lines (59 loc) · 2.42 KB
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
package com.mattermost.pasteinputtext
import android.annotation.SuppressLint
import android.os.Build
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import androidx.core.view.inputmethod.EditorInfoCompat
import androidx.core.view.inputmethod.InputConnectionCompat
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.views.textinput.ReactEditText
import java.lang.Exception
@SuppressLint("ViewConstructor")
class PasteInputEditText(context: ThemedReactContext) : ReactEditText(context) {
private lateinit var mOnPasteListener: IPasteInputListener
private lateinit var mPasteEventDispatcher: EventDispatcher
private var mDisabledCopyPaste: Boolean = false
private var mSelectionWatcher: SelectionWatcher? = null
interface SelectionWatcher {
fun onSelectionChanged(selStart: Int, selEnd: Int)
}
fun setSelectionWatcher(selectionWatcher: SelectionWatcher?) {
mSelectionWatcher = selectionWatcher
}
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
super.onSelectionChanged(selStart, selEnd)
mSelectionWatcher?.onSelectionChanged(selStart, selEnd)
}
fun setDisableCopyPaste(disabled: Boolean) {
this.mDisabledCopyPaste = disabled
}
fun setOnPasteListener(listener: IPasteInputListener, event: EventDispatcher?) {
mOnPasteListener = listener
if (event != null) {
mPasteEventDispatcher = event
}
}
fun getOnPasteListener() : IPasteInputListener {
return mOnPasteListener
}
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
val ic = super.onCreateInputConnection(outAttrs)
EditorInfoCompat.setContentMimeTypes(outAttrs, arrayOf("*/*"))
val callback = InputConnectionCompat.OnCommitContentListener { inputContentInfo, flags, _ ->
val lacksPermission = (flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && lacksPermission) {
try {
inputContentInfo.requestPermission()
} catch (e: Exception) {
return@OnCommitContentListener false
}
}
if (!mDisabledCopyPaste) {
getOnPasteListener().onPaste(inputContentInfo.contentUri, mPasteEventDispatcher)
}
true
}
return InputConnectionCompat.createWrapper(ic!!, outAttrs, callback)
}
}