2019-04-20 05:29:45 +00:00
|
|
|
package code.name.monkey.retromusic.activities
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2019-11-30 11:12:47 +00:00
|
|
|
import android.app.Activity
|
|
|
|
import android.app.Service
|
|
|
|
import android.content.ActivityNotFoundException
|
|
|
|
import android.content.Intent
|
2018-12-25 14:58:47 +00:00
|
|
|
import android.content.res.ColorStateList
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.speech.RecognizerIntent
|
2019-11-30 11:12:47 +00:00
|
|
|
import android.text.Editable
|
|
|
|
import android.text.TextWatcher
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.view.View
|
2019-04-05 10:45:09 +00:00
|
|
|
import android.view.inputmethod.InputMethodManager
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.widget.TextView.BufferType
|
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.appcompat.widget.SearchView.OnQueryTextListener
|
2019-11-30 11:12:47 +00:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import androidx.transition.TransitionManager
|
2018-12-25 14:58:47 +00:00
|
|
|
import code.name.monkey.appthemehelper.ThemeStore
|
2019-12-05 18:18:54 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.ATHUtil
|
2019-11-30 11:12:47 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.ColorUtil
|
|
|
|
import code.name.monkey.appthemehelper.util.MaterialValueHelper
|
|
|
|
import code.name.monkey.retromusic.App
|
|
|
|
import code.name.monkey.retromusic.R
|
2019-04-20 05:29:45 +00:00
|
|
|
import code.name.monkey.retromusic.activities.base.AbsMusicServiceActivity
|
|
|
|
import code.name.monkey.retromusic.adapter.SearchAdapter
|
2019-11-30 11:12:47 +00:00
|
|
|
import code.name.monkey.retromusic.mvp.presenter.SearchPresenter
|
|
|
|
import code.name.monkey.retromusic.mvp.presenter.SearchView
|
|
|
|
import code.name.monkey.retromusic.util.RetroUtil
|
|
|
|
import com.google.android.material.textfield.TextInputEditText
|
2020-02-26 16:49:24 +00:00
|
|
|
import kotlinx.android.synthetic.main.activity_search.*
|
|
|
|
import java.util.*
|
2019-09-04 19:30:24 +00:00
|
|
|
import javax.inject.Inject
|
2020-02-26 16:49:24 +00:00
|
|
|
import kotlin.collections.ArrayList
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2019-09-04 19:30:24 +00:00
|
|
|
class SearchActivity : AbsMusicServiceActivity(), OnQueryTextListener, TextWatcher, SearchView {
|
2019-11-30 11:12:47 +00:00
|
|
|
@Inject
|
|
|
|
lateinit var searchPresenter: SearchPresenter
|
|
|
|
|
|
|
|
private var searchAdapter: SearchAdapter? = null
|
|
|
|
private var query: String? = null
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
setDrawUnderStatusBar()
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_search)
|
|
|
|
setStatusbarColorAuto()
|
2019-12-05 18:18:54 +00:00
|
|
|
setNavigationbarColorAuto()
|
2019-11-30 11:12:47 +00:00
|
|
|
setTaskDescriptionColorAuto()
|
|
|
|
setLightNavigationBar(true)
|
|
|
|
|
2019-12-05 18:18:54 +00:00
|
|
|
App.musicComponent.inject(this)
|
|
|
|
searchPresenter.attachView(this)
|
|
|
|
|
2019-11-30 11:12:47 +00:00
|
|
|
setupRecyclerView()
|
|
|
|
setUpToolBar()
|
|
|
|
setupSearchView()
|
|
|
|
|
|
|
|
if (intent.getBooleanExtra(EXTRA_SHOW_MIC, false)) {
|
|
|
|
startMicSearch()
|
|
|
|
}
|
|
|
|
|
|
|
|
back.setOnClickListener { onBackPressed() }
|
|
|
|
voiceSearch.setOnClickListener { startMicSearch() }
|
|
|
|
clearText.setOnClickListener { searchView.clearText() }
|
2020-02-26 16:49:24 +00:00
|
|
|
searchContainer.backgroundTintList =
|
|
|
|
ColorStateList.valueOf(ATHUtil.resolveColor(this, R.attr.colorSurface))
|
2019-11-30 11:12:47 +00:00
|
|
|
|
|
|
|
keyboardPopup.setOnClickListener {
|
|
|
|
val inputManager = getSystemService(Service.INPUT_METHOD_SERVICE) as InputMethodManager
|
|
|
|
inputManager.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyboardPopup.backgroundTintList = ColorStateList.valueOf(ThemeStore.accentColor(this))
|
|
|
|
ColorStateList.valueOf(
|
2020-02-26 16:49:24 +00:00
|
|
|
MaterialValueHelper.getPrimaryTextColor(
|
|
|
|
this,
|
|
|
|
ColorUtil.isColorLight(ThemeStore.accentColor(this))
|
|
|
|
)
|
2019-11-30 11:12:47 +00:00
|
|
|
).apply {
|
|
|
|
keyboardPopup.setTextColor(this)
|
|
|
|
keyboardPopup.iconTint = this
|
|
|
|
}
|
|
|
|
if (savedInstanceState != null) {
|
2019-12-06 17:13:09 +00:00
|
|
|
query = savedInstanceState.getString(QUERY)
|
2019-11-30 11:12:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setupRecyclerView() {
|
|
|
|
searchAdapter = SearchAdapter(this, emptyList())
|
|
|
|
searchAdapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
|
|
|
override fun onChanged() {
|
|
|
|
super.onChanged()
|
|
|
|
empty.visibility = if (searchAdapter!!.itemCount < 1) View.VISIBLE else View.GONE
|
|
|
|
}
|
|
|
|
})
|
|
|
|
recyclerView.apply {
|
|
|
|
layoutManager = LinearLayoutManager(this@SearchActivity)
|
|
|
|
adapter = searchAdapter
|
|
|
|
}
|
|
|
|
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
|
|
|
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
|
|
|
super.onScrolled(recyclerView, dx, dy)
|
|
|
|
if (dy > 0) {
|
|
|
|
keyboardPopup.shrink()
|
|
|
|
} else if (dy < 0) {
|
|
|
|
keyboardPopup.extend()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setupSearchView() {
|
|
|
|
searchView.addTextChangedListener(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
searchPresenter.detachView()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSaveInstanceState(outState: Bundle) {
|
|
|
|
super.onSaveInstanceState(outState)
|
|
|
|
outState.putString(QUERY, query)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpToolBar() {
|
|
|
|
title = null
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun search(query: String) {
|
|
|
|
this.query = query
|
|
|
|
TransitionManager.beginDelayedTransition(appBarLayout)
|
|
|
|
voiceSearch.visibility = if (query.isNotEmpty()) View.GONE else View.VISIBLE
|
|
|
|
clearText.visibility = if (query.isNotEmpty()) View.VISIBLE else View.GONE
|
|
|
|
searchPresenter.search(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onMediaStoreChanged() {
|
|
|
|
super.onMediaStoreChanged()
|
|
|
|
query?.let { search(it) }
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onQueryTextSubmit(query: String): Boolean {
|
|
|
|
hideSoftKeyboard()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onQueryTextChange(newText: String): Boolean {
|
|
|
|
search(newText)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun hideSoftKeyboard() {
|
|
|
|
RetroUtil.hideSoftKeyboard(this@SearchActivity)
|
|
|
|
if (searchView != null) {
|
|
|
|
searchView.clearFocus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun showEmptyView() {
|
|
|
|
searchAdapter?.swapDataSet(ArrayList())
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun showData(data: MutableList<Any>) {
|
|
|
|
searchAdapter?.swapDataSet(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
when (requestCode) {
|
|
|
|
REQ_CODE_SPEECH_INPUT -> {
|
|
|
|
if (resultCode == Activity.RESULT_OK && null != data) {
|
2020-02-26 16:49:24 +00:00
|
|
|
val result: ArrayList<String>? =
|
|
|
|
data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
|
2019-11-30 11:12:47 +00:00
|
|
|
query = result?.get(0)
|
|
|
|
searchView.setText(query, BufferType.EDITABLE)
|
|
|
|
searchPresenter.search(query!!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun startMicSearch() {
|
|
|
|
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
|
|
|
|
intent.putExtra(
|
2019-12-30 11:01:50 +00:00
|
|
|
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
|
|
|
|
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
|
2019-11-30 11:12:47 +00:00
|
|
|
)
|
|
|
|
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
|
|
|
|
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt))
|
|
|
|
try {
|
|
|
|
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT)
|
|
|
|
} catch (e: ActivityNotFoundException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
Toast.makeText(this, getString(R.string.speech_not_supported), Toast.LENGTH_SHORT)
|
2019-12-30 11:01:50 +00:00
|
|
|
.show()
|
2019-11-30 11:12:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onTextChanged(newText: CharSequence, start: Int, before: Int, count: Int) {
|
|
|
|
search(newText.toString())
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun afterTextChanged(s: Editable) {
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
val TAG: String = SearchActivity::class.java.simpleName
|
|
|
|
|
|
|
|
const val EXTRA_SHOW_MIC = "extra_show_mic"
|
|
|
|
const val QUERY: String = "query"
|
|
|
|
|
|
|
|
private const val REQ_CODE_SPEECH_INPUT = 9002
|
|
|
|
}
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
2019-11-30 11:12:47 +00:00
|
|
|
|
|
|
|
fun TextInputEditText.clearText() {
|
|
|
|
text = null
|
|
|
|
}
|