diff --git a/n_player/android/src/main/AndroidManifest.xml b/n_player/android/src/main/AndroidManifest.xml index f3a4107..5f20999 100644 --- a/n_player/android/src/main/AndroidManifest.xml +++ b/n_player/android/src/main/AndroidManifest.xml @@ -15,9 +15,14 @@ android:supportsRtl="true" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round"> + @@ -28,18 +33,6 @@ android:value="n_player" /> - - - - - - - diff --git a/n_player/android/src/main/java/com/enn3developer/n_music/DummyService.kt b/n_player/android/src/main/java/com/enn3developer/n_music/DummyService.kt deleted file mode 100644 index f115211..0000000 --- a/n_player/android/src/main/java/com/enn3developer/n_music/DummyService.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.enn3developer.n_music - -import android.app.Service -import android.content.Intent -import android.os.IBinder - -/** - * DummyService is a service that acts as a placeholder when setting up content intents in notification. - * It prevents the application from crashing when the intent is called - */ - -class DummyService : Service() { - override fun onBind(intent: Intent): IBinder? { - return null - } -} \ No newline at end of file diff --git a/n_player/android/src/main/java/com/enn3developer/n_music/MainActivity.kt b/n_player/android/src/main/java/com/enn3developer/n_music/MainActivity.kt index 2bee06b..803d3bf 100644 --- a/n_player/android/src/main/java/com/enn3developer/n_music/MainActivity.kt +++ b/n_player/android/src/main/java/com/enn3developer/n_music/MainActivity.kt @@ -58,6 +58,7 @@ class MainActivity : NativeActivity() { const val ASK_DIRECTORY = 0 const val ASK_FILE = 1 const val REQUEST_PERMISSION_CODE = 1 + const val ACTIONS = PlaybackState.ACTION_PLAY or PlaybackState.ACTION_PAUSE or PlaybackState.ACTION_SKIP_TO_NEXT or PlaybackState.ACTION_SKIP_TO_PREVIOUS or PlaybackState.ACTION_SEEK_TO } @SuppressLint("RestrictedApi") @@ -130,12 +131,13 @@ class MainActivity : NativeActivity() { mediaSession = MediaSession(applicationContext, TAG) val handler = Handler(Looper.getMainLooper()) handler.post { - mediaSession?.setCallback(MediaCallback(mediaSession!!, playback!!)) + mediaSession?.setCallback(MediaCallback(mediaSession!!, this)) } val bluetoothReceiver = IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY) applicationContext.registerReceiver(bluetoothBroadcastReceiver, bluetoothReceiver) playback = PlaybackState.Builder() - .setActions(PlaybackState.ACTION_PLAY or PlaybackState.ACTION_PAUSE or PlaybackState.ACTION_SKIP_TO_NEXT or PlaybackState.ACTION_SKIP_TO_PREVIOUS or PlaybackState.ACTION_SEEK_TO) + .setActions(ACTIONS) + .setActiveQueueItemId(ACTIONS) val channel = NotificationChannel( CHANNEL_ID, NOTIFICATION_NAME_SERVICE, @@ -148,8 +150,9 @@ class MainActivity : NativeActivity() { mediaSession?.setPlaybackState(playback?.build()) NotificationManagerCompat.from(applicationContext).createNotificationChannel(channel) notification = Notification.Builder(applicationContext, CHANNEL_ID).apply { - setSmallIcon(R.mipmap.ic_launcher_round) + setSmallIcon(R.drawable.ic_launcher_monochrome) style = Notification.MediaStyle().setMediaSession(mediaSession?.sessionToken) + setOngoing(true) } } @@ -186,9 +189,14 @@ class MainActivity : NativeActivity() { coverPath: String, songLength: Double ) { - val intent = Intent(applicationContext, DummyService::class.java).apply { - flags = Intent.FLAG_ACTIVITY_CLEAR_TOP + var intent = applicationContext.packageManager.getLaunchIntentForPackage(packageName) + + if (intent == null) { + intent = Intent(applicationContext, MainActivity::class.java) } + + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED + val pendingIntent = PendingIntent.getActivity( applicationContext, 0, intent, @@ -242,7 +250,6 @@ class MainActivity : NativeActivity() { val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.cancel(NOTIFICATION_ID) - stopService(Intent(this, DummyService::class.java)) super.onDestroy() } diff --git a/n_player/android/src/main/java/com/enn3developer/n_music/MediaCallback.kt b/n_player/android/src/main/java/com/enn3developer/n_music/MediaCallback.kt index 932c9bc..ab65791 100644 --- a/n_player/android/src/main/java/com/enn3developer/n_music/MediaCallback.kt +++ b/n_player/android/src/main/java/com/enn3developer/n_music/MediaCallback.kt @@ -4,9 +4,11 @@ import android.media.session.MediaSession import android.media.session.PlaybackState // Called in mediaSession callback when we interact with notification -class MediaCallback(private val mediaSession: MediaSession, - private val playback: PlaybackState.Builder +class MediaCallback( + private val mediaSession: MediaSession, + private val activity: MainActivity ) : MediaSession.Callback() { + private external fun TogglePause() private external fun PlayNext() private external fun PlayPrevious() @@ -14,24 +16,26 @@ class MediaCallback(private val mediaSession: MediaSession, override fun onPause() { TogglePause() - mediaSession.controller.playbackState?.position?.let { - playback.setState( - PlaybackState.STATE_PAUSED, - it, 1.0f - ) - } - mediaSession.setPlaybackState(playback.build()) + val position = mediaSession.controller.playbackState?.position ?: 0L + + activity.playback?.setState( + PlaybackState.STATE_PAUSED, + position, 1.0f + ) + + mediaSession.setPlaybackState(activity.playback?.build()) super.onPause() } override fun onPlay() { TogglePause() - mediaSession.controller.playbackState?.position?.let { - playback.setState( - PlaybackState.STATE_PLAYING, - it, 1.0f) - } - mediaSession.setPlaybackState(playback.build()) + val position = mediaSession.controller.playbackState?.position ?: 0L + + activity.playback?.setState( + PlaybackState.STATE_PLAYING, + position, 1.0f + ) + mediaSession.setPlaybackState(activity.playback?.build()) super.onPlay() } @@ -42,15 +46,15 @@ class MediaCallback(private val mediaSession: MediaSession, override fun onSkipToPrevious() { PlayPrevious() - playback.setState(PlaybackState.STATE_PLAYING, 0L, 1.0f) - mediaSession.setPlaybackState(playback.build()) + activity.playback?.setState(PlaybackState.STATE_PLAYING, 0L, 1.0f) + mediaSession.setPlaybackState(activity.playback?.build()) super.onSkipToPrevious() } override fun onSeekTo(pos: Long) { Seek((pos / 1000).toDouble()) - playback.setState(PlaybackState.STATE_PLAYING, pos, 1.0f) - mediaSession.setPlaybackState(playback.build()) + activity.playback?.setState(PlaybackState.STATE_PLAYING, pos, 1.0f) + mediaSession.setPlaybackState(activity.playback?.build()) super.onSeekTo(pos) } } \ No newline at end of file diff --git a/n_player/android/src/main/res/drawable/ic_launcher_monochrome.xml b/n_player/android/src/main/res/drawable/ic_launcher_monochrome.xml new file mode 100644 index 0000000..bcb7fdf --- /dev/null +++ b/n_player/android/src/main/res/drawable/ic_launcher_monochrome.xml @@ -0,0 +1,396 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/n_player/android/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/n_player/android/src/main/res/mipmap-anydpi-v26/ic_launcher.xml index 7353dbd..1413a31 100644 --- a/n_player/android/src/main/res/mipmap-anydpi-v26/ic_launcher.xml +++ b/n_player/android/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -2,4 +2,5 @@ + \ No newline at end of file diff --git a/n_player/android/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/n_player/android/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml index 7353dbd..1413a31 100644 --- a/n_player/android/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml +++ b/n_player/android/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -2,4 +2,5 @@ + \ No newline at end of file diff --git a/n_player/compile_rust.sh b/n_player/compile_rust.sh index f82b078..542e4d4 100644 --- a/n_player/compile_rust.sh +++ b/n_player/compile_rust.sh @@ -1,2 +1 @@ -source env.sh cargo ndk -t arm64-v8a -o android/src/main/jniLibs/ -p 30 build --package n_player --lib --no-default-features \ No newline at end of file diff --git a/n_player/compile_rust_release.sh b/n_player/compile_rust_release.sh index 6574106..6b90ccf 100644 --- a/n_player/compile_rust_release.sh +++ b/n_player/compile_rust_release.sh @@ -1,2 +1 @@ -source env.sh cargo ndk -t arm64-v8a -o android/src/main/jniLibs/ -p 30 build --package n_player --lib --no-default-features --release \ No newline at end of file