Skip to content

Commit

Permalink
Fixed audio player widgets not visible on third party widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamza417 committed Oct 2, 2024
1 parent df382a6 commit a789b09
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 60 deletions.
7 changes: 0 additions & 7 deletions .idea/dictionaries/hamza.xml

This file was deleted.

1 change: 1 addition & 0 deletions app/src/main/assets/html/changelogs.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ <h4>Bug Fixes</h4>
<li>Fixed <b>Batch Extract</b> crashing due to special characters in app name.</li>
<li>Fixed index issue in <b>Batch Extract</b>.</li>
<li>Fixed changes randomly not loading properly.</li>
<li>Fixed <b>Audio Player</b> state not visible on third party widgets.</li>
</ul>

<br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ object DevelopmentPreferences {
DevelopmentPreferencesModel.TYPE_BOOLEAN),

DevelopmentPreferencesModel("Add Bitmap To Metadata",
"Add bitmap to the metadata of MusicPlayerService. Useful for ROMs that shows Album Art on Lock Screen.",
"Add bitmap to the metadata of MusicPlayerService. " +
"Useful for ROMs that shows Album Art on Lock Screen or " +
"if widgets are not showing the album art.",
ADD_BITMAP_TO_METADATA,
DevelopmentPreferencesModel.TYPE_BOOLEAN),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class MediaButtonIntentReceiver : BroadcastReceiver() {
private const val MSG_HEADSET_DOUBLE_CLICK_TIMEOUT = 2
private const val DOUBLE_CLICK = 400
private var wakeLock: PowerManager.WakeLock? = null
private var mClickCounter = 0
private var mLastClickTime: Long = 0
private var clickCounter = 0
private var lastClickTime: Long = 0

@SuppressLint("HandlerLeak") // false alarm, handler is already static
private val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
private val handler: Handler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
when (msg.what) {
MSG_HEADSET_DOUBLE_CLICK_TIMEOUT -> {
Expand Down Expand Up @@ -90,19 +90,19 @@ class MediaButtonIntentReceiver : BroadcastReceiver() {
// The service may or may not be running, but we need to send it
// a command.
if (keycode == KeyEvent.KEYCODE_HEADSETHOOK || keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
mClickCounter = 0
if (eventTime - lastClickTime >= DOUBLE_CLICK) {
clickCounter = 0
}
mClickCounter++
if (DEBUG) Log.v(tag, "Got headset click, count = $mClickCounter")
mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT)
val msg: Message = mHandler.obtainMessage(
MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context)
val delay = if (mClickCounter < 3) DOUBLE_CLICK.toLong() else 0.toLong()
if (mClickCounter >= 3) {
mClickCounter = 0
clickCounter++
if (DEBUG) Log.v(tag, "Got headset click, count = $clickCounter")
handler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT)
val msg: Message = handler.obtainMessage(
MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, clickCounter, 0, context)
val delay = if (clickCounter < 3) DOUBLE_CLICK.toLong() else 0.toLong()
if (clickCounter >= 3) {
clickCounter = 0
}
mLastClickTime = eventTime
lastClickTime = eventTime
acquireWakeLockAndSendMessage(context, msg, delay)
} else {
startService(context, command)
Expand Down Expand Up @@ -141,11 +141,11 @@ class MediaButtonIntentReceiver : BroadcastReceiver() {
if (DEBUG) Log.v(tag, "Acquiring wake lock and sending " + msg.what)
// Make sure we don't indefinitely hold the wake lock under any circumstances
wakeLock!!.acquire(10000)
mHandler.sendMessageDelayed(msg, delay)
handler.sendMessageDelayed(msg, delay)
}

private fun releaseWakeLockIfHandlerIdle() {
if (mHandler.hasMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT)) {
if (handler.hasMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT)) {
if (DEBUG) Log.v(tag, "Handler still has messages pending, not releasing wake lock")
return
}
Expand Down
69 changes: 33 additions & 36 deletions app/src/main/java/app/simple/inure/services/AudioServicePager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ class AudioServicePager : Service(),
@Suppress("DEPRECATION")
stopForeground(true)
}

stopSelf()
}

Expand All @@ -304,49 +303,16 @@ class AudioServicePager : Service(),
ServiceConstants.actionQuitMusicServicePager -> {
quitService()
}

else -> {
/* no-op */
}
}
}
})

mediaSessionCompat!!.isActive = true
mediaSessionCompat!!.setMediaButtonReceiver(mediaButtonReceiverPendingIntent)
mediaControllerCompat = mediaSessionCompat!!.controller
// mediaMetadataCompat = mediaControllerCompat!!.metadata
}

private fun setPlaybackState(playbackState: Int) {
mediaSessionCompat?.setPlaybackState(
PlaybackStateCompat.Builder()
.setState(playbackState, mediaPlayer.currentPosition.toLong(), 1f)
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE or
PlaybackStateCompat.ACTION_PLAY or
PlaybackStateCompat.ACTION_PAUSE or
PlaybackStateCompat.ACTION_SKIP_TO_NEXT or
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS or
PlaybackStateCompat.ACTION_SEEK_TO or
PlaybackStateCompat.ACTION_STOP)
.addCustomAction(
PlaybackStateCompat.CustomAction.Builder(
ServiceConstants.actionQuitMusicServicePager,
"Close",
R.drawable.ic_close
).build())
.build())
}

private fun quitService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_REMOVE)
} else {
@Suppress("DEPRECATION")
stopForeground(true)
}

IntentHelper.sendLocalBroadcastIntent(ServiceConstants.actionQuitMusicServicePager, applicationContext)
stopSelf()
}

private fun setupMetadata() {
Expand All @@ -369,7 +335,6 @@ class AudioServicePager : Service(),
}

withContext(Dispatchers.Main) {
// setupMediaSession()
mediaSessionCompat?.setMetadata(mediaMetadataCompat)
createNotificationChannel()
setPlayingState()
Expand All @@ -382,6 +347,38 @@ class AudioServicePager : Service(),
}
}

private fun setPlaybackState(playbackState: Int) {
mediaSessionCompat?.setPlaybackState(
PlaybackStateCompat.Builder()
.setState(playbackState, mediaPlayer.currentPosition.toLong(), 1f)
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE or
PlaybackStateCompat.ACTION_PLAY or
PlaybackStateCompat.ACTION_PAUSE or
PlaybackStateCompat.ACTION_SKIP_TO_NEXT or
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS or
PlaybackStateCompat.ACTION_SEEK_TO or
PlaybackStateCompat.ACTION_STOP)
.addCustomAction(
PlaybackStateCompat.CustomAction.Builder(
ServiceConstants.actionQuitMusicServicePager,
"Close",
R.drawable.ic_close
).build())
.build())
}

private fun quitService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_REMOVE)
} else {
@Suppress("DEPRECATION")
stopForeground(true)
}

IntentHelper.sendLocalBroadcastIntent(ServiceConstants.actionQuitMusicServicePager, applicationContext)
stopSelf()
}

private fun requestAudioFocus(): Boolean {
val value: Int
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand Down

0 comments on commit a789b09

Please sign in to comment.