PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/activities/SearchActivity.kt

221 lines
7.5 KiB
Kotlin
Raw Normal View History

2019-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.activities
2018-12-06 08:52:57 +00:00
import android.app.Activity
import android.app.SearchManager
2019-04-05 10:45:09 +00:00
import android.app.Service
2018-12-06 08:52:57 +00:00
import android.content.ActivityNotFoundException
import android.content.Context
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
import android.text.Editable
import android.text.TextWatcher
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
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
2018-12-25 14:58:47 +00:00
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.ColorUtil
2019-04-05 10:45:09 +00:00
import code.name.monkey.appthemehelper.util.MaterialValueHelper
2019-09-04 19:30:24 +00:00
import code.name.monkey.retromusic.App
2018-12-06 08:52:57 +00:00
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
import code.name.monkey.retromusic.mvp.presenter.SearchPresenter
2019-09-04 19:30:24 +00:00
import code.name.monkey.retromusic.mvp.presenter.SearchView
2019-06-08 13:42:57 +00:00
import code.name.monkey.retromusic.util.RetroColorUtil
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.util.RetroUtil
import kotlinx.android.synthetic.main.activity_search.*
import java.util.*
2019-09-04 19:30:24 +00:00
import javax.inject.Inject
2019-09-20 19:15:00 +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 {
@Inject
lateinit var searchPresenter: SearchPresenter
2018-12-06 08:52:57 +00:00
private var searchAdapter: SearchAdapter? = null
private var query: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
setDrawUnderStatusBar()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
2019-09-20 19:15:00 +00:00
App.musicComponent.inject(this)
2019-09-04 19:30:24 +00:00
searchPresenter.attachView(this)
2018-12-06 08:52:57 +00:00
setStatusbarColorAuto()
setNavigationbarColorAuto()
setTaskDescriptionColorAuto()
setLightNavigationBar(true)
setupRecyclerView()
setUpToolBar()
setupSearchView()
2019-06-04 04:00:11 +00:00
if (intent.getBooleanExtra(EXTRA_SHOW_MIC, false)) {
2018-12-06 08:52:57 +00:00
startMicSearch()
}
2019-06-04 04:00:11 +00:00
2018-12-08 03:33:02 +00:00
back.setOnClickListener { onBackPressed() }
2018-12-06 08:52:57 +00:00
voiceSearch.setOnClickListener { startMicSearch() }
2018-12-25 14:58:47 +00:00
2019-06-08 13:42:57 +00:00
searchContainer.setCardBackgroundColor(RetroColorUtil.toolbarColor(this))
2018-12-25 14:58:47 +00:00
2019-04-05 10:45:09 +00:00
keyboardPopup.setOnClickListener {
val inputManager = getSystemService(Service.INPUT_METHOD_SERVICE) as InputMethodManager
2019-07-30 20:20:01 +00:00
inputManager.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT)
2019-04-05 10:45:09 +00:00
}
keyboardPopup.backgroundTintList = ColorStateList.valueOf(ThemeStore.accentColor(this))
ColorStateList.valueOf(MaterialValueHelper.getPrimaryTextColor(this, ColorUtil.isColorLight(ThemeStore.accentColor(this)))).apply {
keyboardPopup.setTextColor(this)
keyboardPopup.iconTint = this
}
2018-12-06 08:52:57 +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
}
2019-04-05 10:45:09 +00:00
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
2019-09-17 19:36:13 +00:00
keyboardPopup.shrink()
2019-04-05 10:45:09 +00:00
} else if (dy < 0) {
2019-09-17 19:36:13 +00:00
keyboardPopup.extend()
2019-04-05 10:45:09 +00:00
}
}
})
2018-12-06 08:52:57 +00:00
}
private fun setupSearchView() {
getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchView.addTextChangedListener(this)
}
override fun onResume() {
super.onResume()
searchPresenter.search(query)
2018-12-06 08:52:57 +00:00
}
override fun onDestroy() {
super.onDestroy()
2019-09-04 19:30:24 +00:00
searchPresenter.detachView()
2018-12-06 08:52:57 +00:00
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(QUERY, query)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
searchPresenter.search(savedInstanceState.getString(QUERY, ""))
2018-12-06 08:52:57 +00:00
}
private fun setUpToolBar() {
title = null
2019-04-05 10:45:09 +00:00
appBarLayout.setBackgroundColor(ThemeStore.primaryColor(this))
2018-12-06 08:52:57 +00:00
}
private fun search(query: String) {
this.query = query.trim { it <= ' ' }
voiceSearch.visibility = if (query.isNotEmpty()) View.GONE else View.VISIBLE
searchPresenter.search(query)
2018-12-06 08:52:57 +00:00
}
override fun onMediaStoreChanged() {
super.onMediaStoreChanged()
searchPresenter.search(query!!)
2018-12-06 08:52:57 +00:00
}
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() {
2019-09-20 19:15:00 +00:00
searchAdapter?.swapDataSet(ArrayList())
2018-12-06 08:52:57 +00:00
}
2019-09-20 19:15:00 +00:00
override fun showData(data: MutableList<Any>) {
searchAdapter?.swapDataSet(data)
2018-12-06 08:52:57 +00:00
}
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) {
2019-09-20 19:15:00 +00:00
val result: ArrayList<String>? = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
query = result?.get(0)
2018-12-06 08:52:57 +00:00
searchView.setText(query, BufferType.EDITABLE)
searchPresenter.search(query!!)
2018-12-06 08:52:57 +00:00
}
}
}
}
private fun startMicSearch() {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
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).show()
}
}
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
2019-06-04 04:00:11 +00:00
const val EXTRA_SHOW_MIC = "extra_show_mic"
2018-12-06 08:52:57 +00:00
const val QUERY: String = "query"
2019-06-04 04:00:11 +00:00
2018-12-06 08:52:57 +00:00
private const val REQ_CODE_SPEECH_INPUT = 9002
}
}