universal7885: FM-App: Switch to simple notification

- Also revert setting fm app as persistent
This commit is contained in:
roynatech2544 2022-11-04 01:07:52 +00:00
commit 7914ea45fd
3 changed files with 45 additions and 179 deletions

View file

@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:sharedUserId="android.uid.system" package="com.eurekateam.fmradio"> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:sharedUserId="android.uid.system" package="com.eurekateam.fmradio">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.Material3.DayNight.NoActionBar" android:persistent="true"> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.Material3.DayNight.NoActionBar">
<activity android:name=".MainActivity" android:exported="true"> <activity android:name=".MainActivity" android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
@ -9,12 +9,7 @@
</activity> </activity>
<service android:name=".FMRadioService" android:exported="false"> <service android:name=".FMRadioService" android:exported="false">
<intent-filter> <intent-filter>
<action android:name="com.eurekateam.fmradio.PLAY_FM"/> <action android:name="com.eurekateam.fmradio.STOP"/>
<action android:name="com.eurekateam.fmradio.NEXT"/>
<action android:name="com.eurekateam.fmradio.START"/>
<action android:name="com.eurekateam.fmradio.BEFORE"/>
<action android:name="com.eurekateam.fmradio.QUIT"/>
<action android:name="com.eurekateam.fmradio.OUTPUT"/>
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
</intent-filter> </intent-filter>
</service> </service>

View file

@ -1,181 +1,60 @@
package com.eurekateam.fmradio package com.eurekateam.fmradio
import android.app.* import android.app.Service
import android.app.PendingIntent
import android.app.Notification
import android.app.NotificationManager
import android.app.NotificationChannel
import androidx.core.app.NotificationCompat
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.drawable.Icon
import android.media.AudioManager
import android.media.MediaMetadata
import android.media.session.MediaSession
import android.media.session.PlaybackState
import android.os.IBinder import android.os.IBinder
import androidx.preference.PreferenceManager
import com.eurekateam.fmradio.enums.PlayState import com.eurekateam.fmradio.NativeFMInterface
import com.eurekateam.fmradio.enums.PowerState import com.eurekateam.fmradio.R
import com.eurekateam.fmradio.utils.Log
import vendor.eureka.hardware.fmradio.SetType
import vendor.eureka.hardware.fmradio.GetType import vendor.eureka.hardware.fmradio.GetType
class FMRadioService : Service() { import java.text.DecimalFormat
private lateinit var mContext: Context
private lateinit var mAudioManager: AudioManager
private val mNativeFMInterface = NativeFMInterface()
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i("--- FMRadio Background (IN) ---")
mAudioManager = getSystemService(AUDIO_SERVICE) as AudioManager
if (intent != null) {
intent.action?.let { Log.i(it) }
when (intent.action) {
ACTION_PLAY -> {
if (mPlayState == PlayState.STATE_PLAYING) {
mPlayState = PlayState.STATE_STOPPED
mAudioManager.setParameters(PowerState.FM_POWER_OFF.mAudioParam)
} else if (mPlayState == PlayState.STATE_STOPPED) {
mPlayState = PlayState.STATE_PLAYING
mAudioManager.setParameters(PowerState.FM_POWER_ON.mAudioParam)
}
}
ACTION_BEFORE -> {
mPlayState = PlayState.STATE_PLAYING
mNativeFMInterface.mDefaultCtl.getValue(GetType.GET_TYPE_FM_BEFORE_CHANNEL)
}
ACTION_NEXT -> {
mPlayState = PlayState.STATE_PLAYING
mNativeFMInterface.mDefaultCtl.getValue(GetType.GET_TYPE_FM_NEXT_CHANNEL)
}
ACTION_QUIT -> {
mAudioManager = getSystemService(AUDIO_SERVICE) as AudioManager
Log.i("--- FMRadio Background (OUT) ---")
mNativeFMInterface.mDevCtl.setValue(SetType.SET_TYPE_FM_THREAD, 0)
mNativeFMInterface.mDevCtl.close()
mAudioManager.setParameters(PowerState.FM_POWER_OFF.mAudioParam)
mMediaSession.release()
stopSelf()
}
ACTION_OUTPUT -> {
var mSpeaker = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("speaker", false)
mSpeaker = !mSpeaker
changeOutputDevice(mSpeaker)
PreferenceManager.getDefaultSharedPreferences(mContext).edit().putBoolean("speaker", mSpeaker).apply()
}
ACTION_START -> {
mMediaSession = MediaSession(this, "FMRadio")
setPlaybackState()
mMediaSession.isActive = true
mContext = this
}
}
}
sendMetaData("FM ${mNativeFMInterface.mDefaultCtl.getValue(GetType.GET_TYPE_FM_FREQ).toFloat() / 1000} Mhz")
startForeground(51, pushNotification())
Log.i("--- FMRadio Background (OUT) ---")
return START_STICKY
}
private fun setPlaybackState() {
val state = PlaybackState.Builder()
.setActions(
PlaybackState.ACTION_PLAY or PlaybackState.ACTION_SKIP_TO_NEXT
or PlaybackState.ACTION_PAUSE or PlaybackState.ACTION_SKIP_TO_PREVIOUS
or PlaybackState.ACTION_STOP or PlaybackState.ACTION_PLAY_PAUSE
)
.build()
mMediaSession.setPlaybackState(state)
}
private fun sendMetaData(mTitle: String) {
val metadata = MediaMetadata.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, mTitle)
.putString(MediaMetadata.METADATA_KEY_ARTIST, resources.getString(R.string.app_name))
.putBitmap(
MediaMetadata.METADATA_KEY_ALBUM_ART,
BitmapFactory.decodeResource(mContext.resources, R.drawable.ic_radio)
)
.build()
mMediaSession.setMetadata(metadata)
}
private fun pushNotification(): Notification { class FMRadioService : Service() {
private val mFMInterface = NativeFMInterface()
private val mCleanFormat = DecimalFormat("0.#")
private fun pushNotification(mContext: Context): Notification {
val nm = mContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager val nm = mContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel( val channel = NotificationChannel(
mContext.packageName, mContext.packageName,
"FM Radio Playing", "FM Radio",
NotificationManager.IMPORTANCE_HIGH NotificationManager.IMPORTANCE_DEFAULT
) )
nm.createNotificationChannel(channel) nm.createNotificationChannel(channel)
val builder = Notification.Builder(mContext, mContext.packageName) val builder = NotificationCompat.Builder(mContext, mContext.packageName)
// Create a MediaStyle object and supply your media session token to it. val notificationIntent = Intent(mContext, FMRadioService::class.java)
val mediaStyle = Notification.MediaStyle().setMediaSession(mMediaSession.sessionToken) val contentIntent = PendingIntent.getActivity(
builder.setSmallIcon(R.drawable.ic_radio) mContext,
builder.setOngoing(true) 50,
builder.setColor(resources.getColor(android.R.color.system_accent1_200, mContext.theme)) notificationIntent,
builder.setColorized(true) PendingIntent.FLAG_IMMUTABLE
builder.style = mediaStyle )
builder.setOnlyAlertOnce(true) val mCurrFreq = mFMInterface.mDefaultCtl.getValue(GetType.GET_TYPE_FM_FREQ)
builder.setChannelId(mContext.packageName)
val togglePlay = PendingIntent.getService(mContext, 0, Intent(ACTION_PLAY), PendingIntent.FLAG_IMMUTABLE)
val forward = PendingIntent.getService(mContext, 0, Intent(ACTION_NEXT), PendingIntent.FLAG_IMMUTABLE)
val rewind = PendingIntent.getService(mContext, 0, Intent(ACTION_BEFORE), PendingIntent.FLAG_IMMUTABLE)
val close = PendingIntent.getService(mContext, 0, Intent(ACTION_QUIT), PendingIntent.FLAG_IMMUTABLE)
val output = PendingIntent.getService(mContext, 0, Intent(ACTION_OUTPUT), PendingIntent.FLAG_IMMUTABLE)
val mOutputAction: Notification.Action = Notification.Action.Builder(
if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("speaker", false)) {
Icon.createWithResource(this, R.drawable.ic_volume_up)
} else {
Icon.createWithResource(this, R.drawable.ic_headphones)
},
"Output Configuration",
output
).build()
val mPausePlayAction: Notification.Action = Notification.Action.Builder(
when (mPlayState) {
PlayState.STATE_PLAYING -> {
Icon.createWithResource(this, R.drawable.ic_pause)
}
PlayState.STATE_STOPPED -> {
Icon.createWithResource(this, R.drawable.ic_play)
}
},
"Start/Stop",
togglePlay
).build() builder.setContentIntent(contentIntent)
val mRewindAction: Notification.Action = Notification.Action.Builder( builder.setOngoing(true)
Icon.createWithResource(this, R.drawable.ic_rewind), builder.setSmallIcon(R.drawable.ic_radio)
"Rewind", builder.setContentTitle("FMRadio is running - ${mCleanFormat.format(mCurrFreq.toFloat() / 1000)} Mhz")
rewind builder.setChannelId(mContext.packageName)
).build()
val mForwardAction: Notification.Action = Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_forward),
"Forward",
forward
).build()
val mCloseAction: Notification.Action = Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_close),
"Close",
close
).build()
builder.addAction(mOutputAction)
builder.addAction(mRewindAction)
builder.addAction(mPausePlayAction)
builder.addAction(mForwardAction)
builder.addAction(mCloseAction)
return builder.build() return builder.build()
} }
companion object { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
private const val PACKAGENAME = "com.eurekateam.fmradio" if (intent?.action == "com.eurekateam.fmradio.STOP") {
private const val ACTION_START = "$PACKAGENAME.START" stopSelf()
private const val ACTION_PLAY = "$PACKAGENAME.PLAY_FM" return super.onStartCommand(intent, flags, startId)
private const val ACTION_NEXT = "$PACKAGENAME.NEXT" } else {
private const val ACTION_BEFORE = "$PACKAGENAME.BEFORE" startForeground(50, pushNotification(this))
private const val ACTION_QUIT = "$PACKAGENAME.QUIT" return START_STICKY
private const val ACTION_OUTPUT = "$PACKAGENAME.OUTPUT" }
private var mPlayState: PlayState = PlayState.STATE_PLAYING }
lateinit var mMediaSession: MediaSession override fun onBind(intent: Intent?): IBinder? {
} return null
private fun changeOutputDevice(speaker: Boolean) {
mNativeFMInterface.mDevCtl.setValue(SetType.SET_TYPE_FM_SPEAKER_ROUTE, if (speaker) 1 else 0)
} }
} }

View file

@ -170,22 +170,14 @@ class MainActivity : AppCompatActivity() {
super.onPause() super.onPause()
Log.i("Application onPause") Log.i("Application onPause")
if (mFMInterface.mDefaultCtl.getValue(GetType.GET_TYPE_FM_MUTEX_LOCKED) == 0) { if (mFMInterface.mDefaultCtl.getValue(GetType.GET_TYPE_FM_MUTEX_LOCKED) == 0) {
mIntent.action = ACTION_START mIntent.action = null
startService(mIntent) startService(mIntent)
mStartedService = true
} }
} }
override fun onRestart() { override fun onRestart() {
super.onRestart() super.onRestart()
if (mStartedService) { mIntent.action = "com.eurekateam.fmradio.STOP"
stopService(mIntent) startService(mIntent)
mStartedService = false
}
}
companion object {
private const val ACTION_START = "com.eurekateam.fmradio.START"
private var mStartedService = false
} }
} }