2016-08-28 06:24:48 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.view
|
|
|
|
|
|
|
|
import android.content.Context
|
2017-04-17 11:28:22 +02:00
|
|
|
import android.support.v13.view.inputmethod.EditorInfoCompat
|
|
|
|
import android.support.v13.view.inputmethod.InputConnectionCompat
|
|
|
|
import android.support.v13.view.inputmethod.InputContentInfoCompat
|
|
|
|
import android.support.v4.os.BuildCompat
|
2016-08-28 06:24:48 +02:00
|
|
|
import android.text.InputType
|
|
|
|
import android.text.Selection
|
|
|
|
import android.text.method.ArrowKeyMovementMethod
|
|
|
|
import android.text.method.MovementMethod
|
|
|
|
import android.util.AttributeSet
|
2017-04-17 11:28:22 +02:00
|
|
|
import android.view.inputmethod.EditorInfo
|
|
|
|
import android.view.inputmethod.InputConnection
|
2016-08-28 06:24:48 +02:00
|
|
|
import android.widget.AdapterView
|
2017-03-02 10:23:34 +01:00
|
|
|
import com.bumptech.glide.Glide
|
2016-12-18 16:16:03 +01:00
|
|
|
import org.mariotaku.chameleon.view.ChameleonMultiAutoCompleteTextView
|
2017-04-17 11:28:22 +02:00
|
|
|
import org.mariotaku.ktextension.contains
|
2016-08-28 06:24:48 +02:00
|
|
|
import org.mariotaku.twidere.adapter.ComposeAutoCompleteAdapter
|
2017-05-13 08:19:23 +02:00
|
|
|
import org.mariotaku.twidere.extension.setupEmojiFactory
|
2016-08-28 06:24:48 +02:00
|
|
|
import org.mariotaku.twidere.model.UserKey
|
|
|
|
import org.mariotaku.twidere.util.widget.StatusTextTokenizer
|
|
|
|
|
2017-04-17 11:28:22 +02:00
|
|
|
|
2017-02-03 15:12:43 +01:00
|
|
|
class ComposeEditText(
|
2016-12-18 06:21:24 +01:00
|
|
|
context: Context,
|
|
|
|
attrs: AttributeSet? = null
|
2016-12-18 16:16:03 +01:00
|
|
|
) : ChameleonMultiAutoCompleteTextView(context, attrs) {
|
2016-08-28 06:24:48 +02:00
|
|
|
|
|
|
|
private var adapter: ComposeAutoCompleteAdapter? = null
|
2017-04-17 11:28:22 +02:00
|
|
|
var imageInputListener: ((InputContentInfoCompat) -> Unit)? = null
|
2016-08-28 06:24:48 +02:00
|
|
|
var accountKey: UserKey? = null
|
|
|
|
set(value) {
|
|
|
|
field = value
|
|
|
|
updateAccountKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
init {
|
2017-05-13 08:19:23 +02:00
|
|
|
setupEmojiFactory()
|
2016-08-28 06:24:48 +02:00
|
|
|
setTokenizer(StatusTextTokenizer())
|
|
|
|
onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
|
|
|
|
removeIMESuggestions()
|
|
|
|
}
|
|
|
|
// HACK: remove AUTO_COMPLETE flag to force IME show auto completion
|
|
|
|
setRawInputType(inputType and InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE.inv())
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getDefaultMovementMethod(): MovementMethod {
|
|
|
|
return ArrowKeyMovementMethod.getInstance()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow()
|
|
|
|
if (!isInEditMode && adapter == null) {
|
2017-03-02 10:23:34 +01:00
|
|
|
adapter = ComposeAutoCompleteAdapter(context, Glide.with(context))
|
2016-08-28 06:24:48 +02:00
|
|
|
}
|
2017-02-03 15:12:43 +01:00
|
|
|
setAdapter(adapter)
|
2016-08-28 06:24:48 +02:00
|
|
|
updateAccountKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow()
|
|
|
|
adapter?.closeCursor()
|
|
|
|
adapter = null
|
|
|
|
}
|
|
|
|
|
2017-02-03 15:12:43 +01:00
|
|
|
override fun onTextContextMenuItem(id: Int): Boolean {
|
|
|
|
try {
|
|
|
|
return super.onTextContextMenuItem(id)
|
|
|
|
} catch (e: AbstractMethodError) {
|
|
|
|
// http://crashes.to/s/69acd0ea0de
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 11:28:22 +02:00
|
|
|
override fun onCreateInputConnection(editorInfo: EditorInfo?): InputConnection? {
|
|
|
|
if (editorInfo == null) return null
|
|
|
|
val ic = super.onCreateInputConnection(editorInfo) ?: return null
|
|
|
|
EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("image/*"))
|
|
|
|
|
|
|
|
val callback = InputConnectionCompat.OnCommitContentListener { inputContentInfo, flags, _ ->
|
|
|
|
// read and display inputContentInfo asynchronously
|
|
|
|
if (BuildCompat.isAtLeastNMR1() && InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION in flags) {
|
|
|
|
try {
|
|
|
|
inputContentInfo.requestPermission()
|
|
|
|
} catch (e: Exception) {
|
|
|
|
return@OnCommitContentListener false // return false if failed
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// read and display inputContentInfo asynchronously.
|
|
|
|
// call inputContentInfo.releasePermission() as needed.
|
|
|
|
imageInputListener?.invoke(inputContentInfo)
|
|
|
|
return@OnCommitContentListener true
|
|
|
|
}
|
|
|
|
return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
|
|
|
|
}
|
|
|
|
|
2016-08-28 06:24:48 +02:00
|
|
|
private fun updateAccountKey() {
|
|
|
|
adapter?.accountKey = accountKey
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun removeIMESuggestions() {
|
|
|
|
val selectionEnd = selectionEnd
|
|
|
|
val selectionStart = selectionStart
|
|
|
|
Selection.removeSelection(text)
|
|
|
|
setSelection(selectionStart, selectionEnd)
|
|
|
|
}
|
|
|
|
}
|