mirror of
https://github.com/chararomandroid/android_device_samsung_a20
synced 2026-08-23 04:25:33 -04:00
universal7885: FMRadio: Use kotlinx.cororutines library
* Background thread handle system
This commit is contained in:
parent
308a5d49c8
commit
0803fadf47
4 changed files with 106 additions and 89 deletions
|
|
@ -11,6 +11,7 @@ android_app {
|
||||||
"androidx.core_core",
|
"androidx.core_core",
|
||||||
"androidx.appcompat_appcompat",
|
"androidx.appcompat_appcompat",
|
||||||
"com.google.android.material_material",
|
"com.google.android.material_material",
|
||||||
|
"kotlinx_coroutines_android",
|
||||||
],
|
],
|
||||||
required: [
|
required: [
|
||||||
"privapp_whitelist_com.eurekateam.fmradio.xml",
|
"privapp_whitelist_com.eurekateam.fmradio.xml",
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,10 @@ import android.media.MediaMetadata
|
||||||
import android.media.session.MediaSession
|
import android.media.session.MediaSession
|
||||||
import android.media.session.PlaybackState
|
import android.media.session.PlaybackState
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.util.Log
|
|
||||||
import androidx.core.content.res.ResourcesCompat
|
import androidx.core.content.res.ResourcesCompat
|
||||||
import androidx.core.graphics.drawable.toBitmap
|
import androidx.core.graphics.drawable.toBitmap
|
||||||
import com.eurekateam.fmradio.fragments.MainFragment
|
import com.eurekateam.fmradio.fragments.MainFragment
|
||||||
|
import com.eurekateam.fmradio.utils.Log
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -38,7 +38,7 @@ class FMRadioService : Service() {
|
||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
intent.action?.let { Log.i(TAG, it) }
|
intent.action?.let { Log.i(it) }
|
||||||
when (intent.action) {
|
when (intent.action) {
|
||||||
BEGIN_BG_SERVICE -> {
|
BEGIN_BG_SERVICE -> {
|
||||||
mContext = this
|
mContext = this
|
||||||
|
|
@ -59,7 +59,7 @@ class FMRadioService : Service() {
|
||||||
}
|
}
|
||||||
ACTION_BEFORE -> {
|
ACTION_BEFORE -> {
|
||||||
mIsPlaying = true
|
mIsPlaying = true
|
||||||
Log.i(TAG, "onStartCommand: mCurrentIndex $mIndex")
|
Log.i("onStartCommand: mCurrentIndex $mIndex")
|
||||||
if (mIndex > 0)
|
if (mIndex > 0)
|
||||||
mIndex -= 1
|
mIndex -= 1
|
||||||
if (mIndex >= 1)
|
if (mIndex >= 1)
|
||||||
|
|
@ -68,7 +68,7 @@ class FMRadioService : Service() {
|
||||||
}
|
}
|
||||||
ACTION_NEXT -> {
|
ACTION_NEXT -> {
|
||||||
mIsPlaying = true
|
mIsPlaying = true
|
||||||
Log.i(TAG, "onStartCommand: mCurrentIndex $mIndex")
|
Log.i("onStartCommand: mCurrentIndex $mIndex")
|
||||||
if (mIndex < mTracks.size - 1)
|
if (mIndex < mTracks.size - 1)
|
||||||
mIndex += 1
|
mIndex += 1
|
||||||
if (mIndex < mTracks.size - 1)
|
if (mIndex < mTracks.size - 1)
|
||||||
|
|
@ -78,8 +78,8 @@ class FMRadioService : Service() {
|
||||||
ACTION_QUIT -> {
|
ACTION_QUIT -> {
|
||||||
mAudioManager = getSystemService(AUDIO_SERVICE) as AudioManager
|
mAudioManager = getSystemService(AUDIO_SERVICE) as AudioManager
|
||||||
mAudioManager.setParameters(FM_RADIO_OFF)
|
mAudioManager.setParameters(FM_RADIO_OFF)
|
||||||
stopForeground(true)
|
|
||||||
mMediaSession.release()
|
mMediaSession.release()
|
||||||
|
stopSelf()
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
throw IllegalAccessException("Unexpected Intent!")
|
throw IllegalAccessException("Unexpected Intent!")
|
||||||
|
|
@ -90,7 +90,7 @@ class FMRadioService : Service() {
|
||||||
mFilePath = mContext.filesDir
|
mFilePath = mContext.filesDir
|
||||||
mNativeFMInterface = NativeFMInterface()
|
mNativeFMInterface = NativeFMInterface()
|
||||||
mTitle = "FM ${mTracks[mIndex].toFloat() / 1000} Mhz"
|
mTitle = "FM ${mTracks[mIndex].toFloat() / 1000} Mhz"
|
||||||
Log.i(TAG, "onStartCommand: mTitle=$mTitle")
|
Log.i("onStartCommand: mTitle=$mTitle")
|
||||||
mMediaSession = MediaSession(this, "FMRadio")
|
mMediaSession = MediaSession(this, "FMRadio")
|
||||||
setPlaybackState()
|
setPlaybackState()
|
||||||
sendMetaData("FM ${mTracks[mIndex].toFloat() / 1000}Mhz")
|
sendMetaData("FM ${mTracks[mIndex].toFloat() / 1000}Mhz")
|
||||||
|
|
@ -178,4 +178,4 @@ class FMRadioService : Service() {
|
||||||
private const val TAG = "FMRadioService"
|
private const val TAG = "FMRadioService"
|
||||||
lateinit var mMediaSession : MediaSession
|
lateinit var mMediaSession : MediaSession
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -159,10 +159,6 @@ class MainActivity : AppCompatActivity() {
|
||||||
stopService(mIntent)
|
stopService(mIntent)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
super.onDestroy()
|
|
||||||
(getSystemService(Context.AUDIO_SERVICE) as AudioManager).setParameters(FM_RADIO_OFF)
|
|
||||||
}
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val PACKAGENAME = "com.eurekateam.fmradio"
|
private const val PACKAGENAME = "com.eurekateam.fmradio"
|
||||||
private const val BEGIN_BG_SERVICE = "$PACKAGENAME.STARTBG"
|
private const val BEGIN_BG_SERVICE = "$PACKAGENAME.STARTBG"
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import android.graphics.drawable.Drawable
|
||||||
import android.graphics.drawable.Icon
|
import android.graphics.drawable.Icon
|
||||||
import android.media.AudioManager
|
import android.media.AudioManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.view.Display
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
|
@ -21,6 +22,10 @@ import com.eurekateam.fmradio.NativeFMInterface
|
||||||
import com.eurekateam.fmradio.R
|
import com.eurekateam.fmradio.R
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||||
import com.google.android.material.textview.MaterialTextView
|
import com.google.android.material.textview.MaterialTextView
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.text.DecimalFormat
|
import java.text.DecimalFormat
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -93,85 +98,100 @@ class MainFragment : Fragment(R.layout.fragment_main), View.OnClickListener,
|
||||||
setBackgroundColor(resources.getColor(android.R.color.system_accent1_700, requireContext().theme))
|
setBackgroundColor(resources.getColor(android.R.color.system_accent1_700, requireContext().theme))
|
||||||
}
|
}
|
||||||
var mFMInit = false
|
var mFMInit = false
|
||||||
if (mFreqCurrent == -1) {
|
GlobalScope.launch {
|
||||||
mAudioManager.setParameters(FM_RADIO_OFF)
|
withContext(Dispatchers.IO){
|
||||||
mUpdateEnableDisable(false, mRootView)
|
if (mFreqCurrent == -1) {
|
||||||
mFMInit = true
|
mAudioManager.setParameters(FM_RADIO_OFF)
|
||||||
}
|
withContext(Dispatchers.Main){
|
||||||
if (FileUtilities.checkIfExistFile(FileUtilities.mFMFreqFileName, requireContext())){
|
mUpdateEnableDisable(false, mRootView)
|
||||||
mFreqCurrent = FileUtilities.readFromFile(
|
}
|
||||||
FileUtilities.mFMFreqFileName,
|
mFMInit = true
|
||||||
requireContext()
|
}
|
||||||
).toInt()
|
|
||||||
mFMInterface.setFMFreq(fd, mFreqCurrent)
|
if (FileUtilities.checkIfExistFile(FileUtilities.mFMFreqFileName, requireContext())){
|
||||||
requireActivity().runOnUiThread {
|
mFreqCurrent = FileUtilities.readFromFile(
|
||||||
mFMFreq.text = mCleanFormat.format(mFreqCurrent.toFloat() / 1000)
|
FileUtilities.mFMFreqFileName,
|
||||||
}
|
requireContext()
|
||||||
}
|
).toInt()
|
||||||
if (FileUtilities.checkIfExistFile(FileUtilities.mFMVolumeFileName, requireContext())){
|
mFMInterface.setFMFreq(fd, mFreqCurrent)
|
||||||
mVolume = FileUtilities.readFromFile(
|
withContext(Dispatchers.Main) {
|
||||||
FileUtilities.mFMVolumeFileName,
|
mFMFreq.text = mCleanFormat.format(mFreqCurrent.toFloat() / 1000)
|
||||||
requireContext()
|
}
|
||||||
).toInt()
|
}
|
||||||
mFMInterface.setFMVolume(fd, mVolume)
|
if (FileUtilities.checkIfExistFile(FileUtilities.mFMVolumeFileName, requireContext())){
|
||||||
requireActivity().runOnUiThread {
|
mVolume = FileUtilities.readFromFile(
|
||||||
mSeekBar.progress = mVolume
|
FileUtilities.mFMVolumeFileName,
|
||||||
}
|
requireContext()
|
||||||
}
|
).toInt()
|
||||||
mSeekBar.min = 1
|
mFMInterface.setFMVolume(fd, mVolume)
|
||||||
mSeekBar.max = 15
|
withContext(Dispatchers.Main) {
|
||||||
if (mVolume == -1) {
|
mSeekBar.progress = mVolume
|
||||||
mVolume = 8
|
}
|
||||||
mFMInterface.setFMVolume(fd, mVolume)
|
}
|
||||||
requireActivity().runOnUiThread {
|
withContext(Dispatchers.Main){
|
||||||
mSeekBar.progress = mVolume
|
mSeekBar.min = 1
|
||||||
}
|
mSeekBar.max = 15
|
||||||
}
|
}
|
||||||
if (FileUtilities.checkIfExistFile(FileUtilities.mHeadsetFileName, requireContext())){
|
withContext(Dispatchers.IO){
|
||||||
mHeadset = true
|
if (mVolume == -1) {
|
||||||
mAudioManager.mode = AudioManager.MODE_IN_COMMUNICATION
|
mVolume = 8
|
||||||
mAudioManager.isSpeakerphoneOn = mHeadset
|
mFMInterface.setFMVolume(fd, mVolume)
|
||||||
mAudioManager.setParameters(FM_RADIO_OFF)
|
withContext(Dispatchers.Main) {
|
||||||
mAudioManager.setParameters(FM_RADIO_ON)
|
mSeekBar.progress = mVolume
|
||||||
mAudioManager.mode = AudioManager.MODE_NORMAL
|
}
|
||||||
if (mHeadset) {
|
}
|
||||||
mOutputSwitch.setImageIcon(
|
}
|
||||||
Icon.createWithResource(
|
withContext(Dispatchers.IO){
|
||||||
requireContext(),
|
if (FileUtilities.checkIfExistFile(FileUtilities.mHeadsetFileName, requireContext())){
|
||||||
R.drawable.ic_volume_up
|
mHeadset = true
|
||||||
)
|
mAudioManager.mode = AudioManager.MODE_IN_COMMUNICATION
|
||||||
)
|
mAudioManager.isSpeakerphoneOn = mHeadset
|
||||||
FileUtilities.createFile(FileUtilities.mHeadsetFileName, requireContext())
|
mAudioManager.setParameters(FM_RADIO_OFF)
|
||||||
}
|
mAudioManager.setParameters(FM_RADIO_ON)
|
||||||
}
|
mAudioManager.mode = AudioManager.MODE_NORMAL
|
||||||
mFMInterface.setFMFreq(fd, mFMInterface.getFMLower(fd))
|
withContext(Dispatchers.Main){
|
||||||
mRefreshTracks()
|
if (mHeadset) {
|
||||||
if (mFreqCurrent != -1){
|
mOutputSwitch.setImageIcon(
|
||||||
mFMInterface.setFMFreq(fd, mFreqCurrent)
|
Icon.createWithResource(
|
||||||
} else {
|
requireContext(),
|
||||||
mFreqCurrent = mFMInterface.getFMLower(fd)
|
R.drawable.ic_volume_up
|
||||||
}
|
)
|
||||||
Thread {
|
)
|
||||||
mFMInterface.setFMBoot(fd)
|
FileUtilities.createFile(FileUtilities.mHeadsetFileName, requireContext())
|
||||||
if (!mFMInit && isAdded) {
|
}
|
||||||
mFreqCurrent = mFMInterface.getFMFreq(fd).toInt()
|
}
|
||||||
mFMInterface.setFMFreq(fd, mFreqCurrent)
|
}
|
||||||
requireActivity().mainExecutor.execute {
|
}
|
||||||
mFMFreq.text = mCleanFormat.format( mFreqCurrent.toFloat() / 1000)
|
mFMInterface.setFMFreq(fd, mFMInterface.getFMLower(fd))
|
||||||
|
mRefreshTracks()
|
||||||
|
if (mFreqCurrent != -1){
|
||||||
|
mFMInterface.setFMFreq(fd, mFreqCurrent)
|
||||||
|
} else {
|
||||||
|
mFreqCurrent = mFMInterface.getFMLower(fd)
|
||||||
|
}
|
||||||
|
mFMInterface.setFMBoot(fd)
|
||||||
|
if (!mFMInit && isAdded) {
|
||||||
|
mFreqCurrent = mFMInterface.getFMFreq(fd).toInt()
|
||||||
|
mFMInterface.setFMFreq(fd, mFreqCurrent)
|
||||||
|
requireActivity().mainExecutor.execute {
|
||||||
|
mFMFreq.text = mCleanFormat.format( mFreqCurrent.toFloat() / 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (mTracks.isEmpty())
|
||||||
|
if (mFreqCurrent == -1)
|
||||||
|
mFMInterface.setFMThread(fd, true)
|
||||||
|
withContext(Dispatchers.Main){
|
||||||
|
mFavButton.let {
|
||||||
|
if (mFavStats[mFreqCurrent] == null){
|
||||||
|
mFavStats.putIfAbsent(mFreqCurrent, false)
|
||||||
|
}
|
||||||
|
if (mFavStats[mFreqCurrent]!!){
|
||||||
|
it.setImageDrawable(mStarFilled)
|
||||||
|
}else {
|
||||||
|
it.setImageDrawable(mStar)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (mTracks.isEmpty()) return@Thread
|
|
||||||
if (mFreqCurrent == -1)
|
|
||||||
mFMInterface.setFMThread(fd, true)
|
|
||||||
}.start()
|
|
||||||
mFavButton.let {
|
|
||||||
if (mFavStats[mFreqCurrent] == null){
|
|
||||||
mFavStats.putIfAbsent(mFreqCurrent, false)
|
|
||||||
}
|
|
||||||
if (mFavStats[mFreqCurrent]!!){
|
|
||||||
it.setImageDrawable(mStarFilled)
|
|
||||||
}else {
|
|
||||||
it.setImageDrawable(mStar)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mRootView
|
return mRootView
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue