universal7885: FMRadio: Use kotlinx.cororutines library

* Background thread handle system
This commit is contained in:
roynatech2544 2022-02-28 01:17:46 +09:00
commit 0803fadf47
No known key found for this signature in database
GPG key ID: 9675C32163D88D30
4 changed files with 106 additions and 89 deletions

View file

@ -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",

View file

@ -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")

View file

@ -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"

View file

@ -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,18 +98,23 @@ 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
GlobalScope.launch {
withContext(Dispatchers.IO){
if (mFreqCurrent == -1) { if (mFreqCurrent == -1) {
mAudioManager.setParameters(FM_RADIO_OFF) mAudioManager.setParameters(FM_RADIO_OFF)
withContext(Dispatchers.Main){
mUpdateEnableDisable(false, mRootView) mUpdateEnableDisable(false, mRootView)
}
mFMInit = true mFMInit = true
} }
if (FileUtilities.checkIfExistFile(FileUtilities.mFMFreqFileName, requireContext())){ if (FileUtilities.checkIfExistFile(FileUtilities.mFMFreqFileName, requireContext())){
mFreqCurrent = FileUtilities.readFromFile( mFreqCurrent = FileUtilities.readFromFile(
FileUtilities.mFMFreqFileName, FileUtilities.mFMFreqFileName,
requireContext() requireContext()
).toInt() ).toInt()
mFMInterface.setFMFreq(fd, mFreqCurrent) mFMInterface.setFMFreq(fd, mFreqCurrent)
requireActivity().runOnUiThread { withContext(Dispatchers.Main) {
mFMFreq.text = mCleanFormat.format(mFreqCurrent.toFloat() / 1000) mFMFreq.text = mCleanFormat.format(mFreqCurrent.toFloat() / 1000)
} }
} }
@ -114,19 +124,24 @@ class MainFragment : Fragment(R.layout.fragment_main), View.OnClickListener,
requireContext() requireContext()
).toInt() ).toInt()
mFMInterface.setFMVolume(fd, mVolume) mFMInterface.setFMVolume(fd, mVolume)
requireActivity().runOnUiThread { withContext(Dispatchers.Main) {
mSeekBar.progress = mVolume mSeekBar.progress = mVolume
} }
} }
withContext(Dispatchers.Main){
mSeekBar.min = 1 mSeekBar.min = 1
mSeekBar.max = 15 mSeekBar.max = 15
}
withContext(Dispatchers.IO){
if (mVolume == -1) { if (mVolume == -1) {
mVolume = 8 mVolume = 8
mFMInterface.setFMVolume(fd, mVolume) mFMInterface.setFMVolume(fd, mVolume)
requireActivity().runOnUiThread { withContext(Dispatchers.Main) {
mSeekBar.progress = mVolume mSeekBar.progress = mVolume
} }
} }
}
withContext(Dispatchers.IO){
if (FileUtilities.checkIfExistFile(FileUtilities.mHeadsetFileName, requireContext())){ if (FileUtilities.checkIfExistFile(FileUtilities.mHeadsetFileName, requireContext())){
mHeadset = true mHeadset = true
mAudioManager.mode = AudioManager.MODE_IN_COMMUNICATION mAudioManager.mode = AudioManager.MODE_IN_COMMUNICATION
@ -134,6 +149,7 @@ class MainFragment : Fragment(R.layout.fragment_main), View.OnClickListener,
mAudioManager.setParameters(FM_RADIO_OFF) mAudioManager.setParameters(FM_RADIO_OFF)
mAudioManager.setParameters(FM_RADIO_ON) mAudioManager.setParameters(FM_RADIO_ON)
mAudioManager.mode = AudioManager.MODE_NORMAL mAudioManager.mode = AudioManager.MODE_NORMAL
withContext(Dispatchers.Main){
if (mHeadset) { if (mHeadset) {
mOutputSwitch.setImageIcon( mOutputSwitch.setImageIcon(
Icon.createWithResource( Icon.createWithResource(
@ -144,6 +160,8 @@ class MainFragment : Fragment(R.layout.fragment_main), View.OnClickListener,
FileUtilities.createFile(FileUtilities.mHeadsetFileName, requireContext()) FileUtilities.createFile(FileUtilities.mHeadsetFileName, requireContext())
} }
} }
}
}
mFMInterface.setFMFreq(fd, mFMInterface.getFMLower(fd)) mFMInterface.setFMFreq(fd, mFMInterface.getFMLower(fd))
mRefreshTracks() mRefreshTracks()
if (mFreqCurrent != -1){ if (mFreqCurrent != -1){
@ -151,7 +169,6 @@ class MainFragment : Fragment(R.layout.fragment_main), View.OnClickListener,
} else { } else {
mFreqCurrent = mFMInterface.getFMLower(fd) mFreqCurrent = mFMInterface.getFMLower(fd)
} }
Thread {
mFMInterface.setFMBoot(fd) mFMInterface.setFMBoot(fd)
if (!mFMInit && isAdded) { if (!mFMInit && isAdded) {
mFreqCurrent = mFMInterface.getFMFreq(fd).toInt() mFreqCurrent = mFMInterface.getFMFreq(fd).toInt()
@ -160,10 +177,10 @@ class MainFragment : Fragment(R.layout.fragment_main), View.OnClickListener,
mFMFreq.text = mCleanFormat.format( mFreqCurrent.toFloat() / 1000) mFMFreq.text = mCleanFormat.format( mFreqCurrent.toFloat() / 1000)
} }
} }
if (mTracks.isEmpty()) return@Thread if (mTracks.isEmpty())
if (mFreqCurrent == -1) if (mFreqCurrent == -1)
mFMInterface.setFMThread(fd, true) mFMInterface.setFMThread(fd, true)
}.start() withContext(Dispatchers.Main){
mFavButton.let { mFavButton.let {
if (mFavStats[mFreqCurrent] == null){ if (mFavStats[mFreqCurrent] == null){
mFavStats.putIfAbsent(mFreqCurrent, false) mFavStats.putIfAbsent(mFreqCurrent, false)
@ -174,6 +191,9 @@ class MainFragment : Fragment(R.layout.fragment_main), View.OnClickListener,
it.setImageDrawable(mStar) it.setImageDrawable(mStar)
} }
} }
}
}
}
return mRootView return mRootView
} }
private fun View.disable(mTextView : Boolean = false) { private fun View.disable(mTextView : Boolean = false) {