From c4cc06f23607368a54aa67a06f350c8d6afd9254 Mon Sep 17 00:00:00 2001 From: casey langen Date: Mon, 21 Aug 2017 18:21:19 -0700 Subject: [PATCH] Fixed playback notification for Android O --- .../remote/playback/SystemService.kt | 35 ++++++++++++++----- .../casey/musikcube/remote/util/Debouncer.kt | 2 +- src/musikdroid/build.gradle | 2 +- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/playback/SystemService.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/playback/SystemService.kt index e65d450f8..b6cb13803 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/playback/SystemService.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/playback/SystemService.kt @@ -1,7 +1,6 @@ package io.casey.musikcube.remote.playback -import android.app.PendingIntent -import android.app.Service +import android.app.* import android.content.* import android.graphics.Bitmap import android.media.AudioManager @@ -28,6 +27,7 @@ import io.casey.musikcube.remote.util.Debouncer import io.casey.musikcube.remote.util.Strings import io.casey.musikcube.remote.websocket.Prefs import android.support.v4.app.NotificationCompat.Action as NotifAction +import android.os.Build /** * a service used to interact with all of the system media-related components -- notifications, @@ -36,19 +36,36 @@ import android.support.v4.app.NotificationCompat.Action as NotifAction */ class SystemService : Service() { private val handler = Handler() - private var prefs: SharedPreferences? = null private var playback: StreamingPlaybackService? = null private var wakeLock: PowerManager.WakeLock? = null - private var powerManager: PowerManager? = null private var mediaSession: MediaSessionCompat? = null private var headsetHookPressCount = 0 + private lateinit var powerManager: PowerManager + private lateinit var prefs: SharedPreferences + private var albumArtModel = AlbumArtModel.empty() private var albumArt: Bitmap? = null private var albumArtRequest: SimpleTarget? = null override fun onCreate() { super.onCreate() + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channel = NotificationChannel( + NOTIFICATION_CHANNEL, + NOTIFICATION_CHANNEL, + NotificationManager.IMPORTANCE_LOW) + + channel.enableVibration(false) + channel.setSound(null, null) + channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC + + val nm = getSystemService(Service.NOTIFICATION_SERVICE) as NotificationManager + nm.deleteNotificationChannel(NOTIFICATION_CHANNEL) + nm.createNotificationChannel(channel) + } + prefs = getSharedPreferences(Prefs.NAME, Context.MODE_PRIVATE) powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager registerReceivers() @@ -90,7 +107,7 @@ class SystemService : Service() { } if (wakeLock == null) { - wakeLock = powerManager?.newWakeLock( + wakeLock = powerManager.newWakeLock( PowerManager.PARTIAL_WAKE_LOCK, "StreamingPlaybackService") wakeLock?.setReferenceCounted(false) @@ -221,8 +238,8 @@ class SystemService : Service() { private fun updateMetadata(title: String, artist: String, album: String, image: Bitmap?, duration: Int) { var currentImage = image - val albumArtEnabledInSettings = this.prefs?.getBoolean( - Prefs.Key.ALBUM_ART_ENABLED, Prefs.Default.ALBUM_ART_ENABLED) ?: Prefs.Default.ALBUM_ART_ENABLED + val albumArtEnabledInSettings = this.prefs.getBoolean( + Prefs.Key.ALBUM_ART_ENABLED, Prefs.Default.ALBUM_ART_ENABLED) if (albumArtEnabledInSettings) { if ("-" != artist && "-" != album && !albumArtModel.matches(artist, album)) { @@ -255,13 +272,12 @@ class SystemService : Service() { val contentIntent = PendingIntent.getActivity( applicationContext, 1, MainActivity.getStartIntent(this), 0) - val notification = NotificationCompat.Builder(this, "musikdroid") + val notification = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setContentText(artist + " - " + album) .setContentIntent(contentIntent) .setUsesChronometer(false) - //.setLargeIcon(albumArtBitmap)) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setOngoing(true) @@ -461,6 +477,7 @@ class SystemService : Service() { companion object { private val TAG = "SystemService" private val NOTIFICATION_ID = 0xdeadbeef.toInt() + private val NOTIFICATION_CHANNEL = "musikdroid" private val HEADSET_HOOK_DEBOUNCE_MS = 500L private val ACTION_NOTIFICATION_PLAY = "io.casey.musikcube.remote.NOTIFICATION_PLAY" private val ACTION_NOTIFICATION_PAUSE = "io.casey.musikcube.remote.NOTIFICATION_PAUSE" diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Debouncer.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Debouncer.kt index f576d1eec..dee98cd92 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Debouncer.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Debouncer.kt @@ -24,7 +24,7 @@ abstract class Debouncer(private val delay: Long) { protected abstract fun onDebounced(last: T?) - private val deferred = object : Runnable { + private val deferred:Runnable = object : Runnable { override fun run() { onDebounced(last) } diff --git a/src/musikdroid/build.gradle b/src/musikdroid/build.gradle index 282f71384..9217054e3 100644 --- a/src/musikdroid/build.gradle +++ b/src/musikdroid/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.1.3-2' + ext.kotlin_version = '1.1.4-2' repositories { jcenter() }