add back the old RemoteControlReceiver too for lower Android versions
This commit is contained in:
parent
82c1b8f9c6
commit
d97d0da6d8
2 changed files with 75 additions and 0 deletions
|
@ -134,6 +134,16 @@
|
|||
android:resource="@xml/widget_info"/>
|
||||
</receiver>
|
||||
|
||||
<receiver
|
||||
android:name=".receivers.RemoteControlReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
<intent-filter android:priority="999">
|
||||
<action android:name="android.intent.action.MEDIA_BUTTON"/>
|
||||
<category android:name="android.intent.category.DEFAULT"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<receiver
|
||||
android:name=".receivers.HeadsetPlugReceiver">
|
||||
<intent-filter>
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package com.simplemobiletools.musicplayer.receivers
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Handler
|
||||
import android.view.KeyEvent
|
||||
import com.simplemobiletools.musicplayer.extensions.config
|
||||
import com.simplemobiletools.musicplayer.extensions.sendIntent
|
||||
import com.simplemobiletools.musicplayer.helpers.NEXT
|
||||
import com.simplemobiletools.musicplayer.helpers.PLAYPAUSE
|
||||
import com.simplemobiletools.musicplayer.helpers.PREVIOUS
|
||||
|
||||
class RemoteControlReceiver : BroadcastReceiver() {
|
||||
|
||||
companion object {
|
||||
private const val MAX_CLICK_DURATION = 700
|
||||
|
||||
private var mContext: Context? = null
|
||||
private val mHandler = Handler()
|
||||
|
||||
private var mClicksCnt = 0
|
||||
|
||||
private val runnable = Runnable {
|
||||
if (mClicksCnt == 0)
|
||||
return@Runnable
|
||||
|
||||
mContext!!.sendIntent(
|
||||
when (mClicksCnt) {
|
||||
1 -> PLAYPAUSE
|
||||
2 -> NEXT
|
||||
else -> PREVIOUS
|
||||
}
|
||||
)
|
||||
mClicksCnt = 0
|
||||
}
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
mContext = context
|
||||
if (intent.action == Intent.ACTION_MEDIA_BUTTON) {
|
||||
val swapPrevNext = context.config.swapPrevNext
|
||||
val intentNext = if (swapPrevNext) PREVIOUS else NEXT
|
||||
val intentPrevious = if (swapPrevNext) NEXT else PREVIOUS
|
||||
val event = intent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)
|
||||
if (event.action == KeyEvent.ACTION_UP) {
|
||||
when (event.keyCode) {
|
||||
KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_MEDIA_PAUSE -> context.sendIntent(PLAYPAUSE)
|
||||
KeyEvent.KEYCODE_MEDIA_PREVIOUS -> context.sendIntent(intentPrevious)
|
||||
KeyEvent.KEYCODE_MEDIA_NEXT -> context.sendIntent(intentNext)
|
||||
KeyEvent.KEYCODE_HEADSETHOOK -> {
|
||||
mClicksCnt++
|
||||
|
||||
mHandler.removeCallbacks(runnable)
|
||||
if (mClicksCnt >= 3) {
|
||||
mHandler.post(runnable)
|
||||
} else {
|
||||
mHandler.postDelayed(runnable, MAX_CLICK_DURATION.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue