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.appcompat_appcompat",
"com.google.android.material_material",
"kotlinx_coroutines_android",
],
required: [
"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.PlaybackState
import android.os.IBinder
import android.util.Log
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import com.eurekateam.fmradio.fragments.MainFragment
import com.eurekateam.fmradio.utils.Log
import java.io.File
@ -38,7 +38,7 @@ class FMRadioService : Service() {
return super.onStartCommand(intent, flags, startId)
}
if (intent != null) {
intent.action?.let { Log.i(TAG, it) }
intent.action?.let { Log.i(it) }
when (intent.action) {
BEGIN_BG_SERVICE -> {
mContext = this
@ -59,7 +59,7 @@ class FMRadioService : Service() {
}
ACTION_BEFORE -> {
mIsPlaying = true
Log.i(TAG, "onStartCommand: mCurrentIndex $mIndex")
Log.i("onStartCommand: mCurrentIndex $mIndex")
if (mIndex > 0)
mIndex -= 1
if (mIndex >= 1)
@ -68,7 +68,7 @@ class FMRadioService : Service() {
}
ACTION_NEXT -> {
mIsPlaying = true
Log.i(TAG, "onStartCommand: mCurrentIndex $mIndex")
Log.i("onStartCommand: mCurrentIndex $mIndex")
if (mIndex < mTracks.size - 1)
mIndex += 1
if (mIndex < mTracks.size - 1)
@ -78,8 +78,8 @@ class FMRadioService : Service() {
ACTION_QUIT -> {
mAudioManager = getSystemService(AUDIO_SERVICE) as AudioManager
mAudioManager.setParameters(FM_RADIO_OFF)
stopForeground(true)
mMediaSession.release()
stopSelf()
}
else -> {
throw IllegalAccessException("Unexpected Intent!")
@ -90,7 +90,7 @@ class FMRadioService : Service() {
mFilePath = mContext.filesDir
mNativeFMInterface = NativeFMInterface()
mTitle = "FM ${mTracks[mIndex].toFloat() / 1000} Mhz"
Log.i(TAG, "onStartCommand: mTitle=$mTitle")
Log.i("onStartCommand: mTitle=$mTitle")
mMediaSession = MediaSession(this, "FMRadio")
setPlaybackState()
sendMetaData("FM ${mTracks[mIndex].toFloat() / 1000}Mhz")
@ -178,4 +178,4 @@ class FMRadioService : Service() {
private const val TAG = "FMRadioService"
lateinit var mMediaSession : MediaSession
}
}
}

View file

@ -159,10 +159,6 @@ class MainActivity : AppCompatActivity() {
stopService(mIntent)
}
override fun onDestroy() {
super.onDestroy()
(getSystemService(Context.AUDIO_SERVICE) as AudioManager).setParameters(FM_RADIO_OFF)
}
companion object {
private const val PACKAGENAME = "com.eurekateam.fmradio"
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.media.AudioManager
import android.os.Bundle
import android.view.Display
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -21,6 +22,10 @@ import com.eurekateam.fmradio.NativeFMInterface
import com.eurekateam.fmradio.R
import com.google.android.material.floatingactionbutton.FloatingActionButton
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
@ -93,85 +98,100 @@ class MainFragment : Fragment(R.layout.fragment_main), View.OnClickListener,
setBackgroundColor(resources.getColor(android.R.color.system_accent1_700, requireContext().theme))
}
var mFMInit = false
if (mFreqCurrent == -1) {
mAudioManager.setParameters(FM_RADIO_OFF)
mUpdateEnableDisable(false, mRootView)
mFMInit = true
}
if (FileUtilities.checkIfExistFile(FileUtilities.mFMFreqFileName, requireContext())){
mFreqCurrent = FileUtilities.readFromFile(
FileUtilities.mFMFreqFileName,
requireContext()
).toInt()
mFMInterface.setFMFreq(fd, mFreqCurrent)
requireActivity().runOnUiThread {
mFMFreq.text = mCleanFormat.format(mFreqCurrent.toFloat() / 1000)
}
}
if (FileUtilities.checkIfExistFile(FileUtilities.mFMVolumeFileName, requireContext())){
mVolume = FileUtilities.readFromFile(
FileUtilities.mFMVolumeFileName,
requireContext()
).toInt()
mFMInterface.setFMVolume(fd, mVolume)
requireActivity().runOnUiThread {
mSeekBar.progress = mVolume
}
}
mSeekBar.min = 1
mSeekBar.max = 15
if (mVolume == -1) {
mVolume = 8
mFMInterface.setFMVolume(fd, mVolume)
requireActivity().runOnUiThread {
mSeekBar.progress = mVolume
}
}
if (FileUtilities.checkIfExistFile(FileUtilities.mHeadsetFileName, requireContext())){
mHeadset = true
mAudioManager.mode = AudioManager.MODE_IN_COMMUNICATION
mAudioManager.isSpeakerphoneOn = mHeadset
mAudioManager.setParameters(FM_RADIO_OFF)
mAudioManager.setParameters(FM_RADIO_ON)
mAudioManager.mode = AudioManager.MODE_NORMAL
if (mHeadset) {
mOutputSwitch.setImageIcon(
Icon.createWithResource(
requireContext(),
R.drawable.ic_volume_up
)
)
FileUtilities.createFile(FileUtilities.mHeadsetFileName, requireContext())
}
}
mFMInterface.setFMFreq(fd, mFMInterface.getFMLower(fd))
mRefreshTracks()
if (mFreqCurrent != -1){
mFMInterface.setFMFreq(fd, mFreqCurrent)
} else {
mFreqCurrent = mFMInterface.getFMLower(fd)
}
Thread {
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)
GlobalScope.launch {
withContext(Dispatchers.IO){
if (mFreqCurrent == -1) {
mAudioManager.setParameters(FM_RADIO_OFF)
withContext(Dispatchers.Main){
mUpdateEnableDisable(false, mRootView)
}
mFMInit = true
}
if (FileUtilities.checkIfExistFile(FileUtilities.mFMFreqFileName, requireContext())){
mFreqCurrent = FileUtilities.readFromFile(
FileUtilities.mFMFreqFileName,
requireContext()
).toInt()
mFMInterface.setFMFreq(fd, mFreqCurrent)
withContext(Dispatchers.Main) {
mFMFreq.text = mCleanFormat.format(mFreqCurrent.toFloat() / 1000)
}
}
if (FileUtilities.checkIfExistFile(FileUtilities.mFMVolumeFileName, requireContext())){
mVolume = FileUtilities.readFromFile(
FileUtilities.mFMVolumeFileName,
requireContext()
).toInt()
mFMInterface.setFMVolume(fd, mVolume)
withContext(Dispatchers.Main) {
mSeekBar.progress = mVolume
}
}
withContext(Dispatchers.Main){
mSeekBar.min = 1
mSeekBar.max = 15
}
withContext(Dispatchers.IO){
if (mVolume == -1) {
mVolume = 8
mFMInterface.setFMVolume(fd, mVolume)
withContext(Dispatchers.Main) {
mSeekBar.progress = mVolume
}
}
}
withContext(Dispatchers.IO){
if (FileUtilities.checkIfExistFile(FileUtilities.mHeadsetFileName, requireContext())){
mHeadset = true
mAudioManager.mode = AudioManager.MODE_IN_COMMUNICATION
mAudioManager.isSpeakerphoneOn = mHeadset
mAudioManager.setParameters(FM_RADIO_OFF)
mAudioManager.setParameters(FM_RADIO_ON)
mAudioManager.mode = AudioManager.MODE_NORMAL
withContext(Dispatchers.Main){
if (mHeadset) {
mOutputSwitch.setImageIcon(
Icon.createWithResource(
requireContext(),
R.drawable.ic_volume_up
)
)
FileUtilities.createFile(FileUtilities.mHeadsetFileName, requireContext())
}
}
}
}
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