diff --git a/app/src/main/java/com/mux/video/vod/demo/UploadExampleApp.kt b/app/src/main/java/com/mux/video/vod/demo/UploadExampleApp.kt index 9b700ea0..b603c1d4 100644 --- a/app/src/main/java/com/mux/video/vod/demo/UploadExampleApp.kt +++ b/app/src/main/java/com/mux/video/vod/demo/UploadExampleApp.kt @@ -4,7 +4,6 @@ import android.annotation.TargetApi import android.app.Application import android.app.NotificationChannel import android.app.NotificationManager -import android.content.Intent import android.os.Build import com.mux.video.upload.MuxUploadSdk import com.mux.video.upload.api.MuxUploadManager @@ -18,7 +17,7 @@ class UploadExampleApp : Application() { } MuxUploadSdk.initialize(this) if (MuxUploadManager.allUploadJobs().isNotEmpty()) { - UploadNotificationService.start(this) + UploadNotificationService.startCompat(this) } } diff --git a/app/src/main/java/com/mux/video/vod/demo/UploadNotificationService.kt b/app/src/main/java/com/mux/video/vod/demo/UploadNotificationService.kt index 0f4a347e..65eb6a4a 100644 --- a/app/src/main/java/com/mux/video/vod/demo/UploadNotificationService.kt +++ b/app/src/main/java/com/mux/video/vod/demo/UploadNotificationService.kt @@ -11,7 +11,6 @@ import android.os.Build import android.os.IBinder import android.util.Log import androidx.core.app.NotificationCompat -import androidx.core.app.NotificationManagerCompat import androidx.core.app.ServiceCompat import com.mux.video.upload.api.MuxUpload import com.mux.video.upload.api.MuxUploadManager @@ -30,11 +29,10 @@ class UploadNotificationService : Service() { private const val TAG = "BackgroundUploadService" const val ACTION_START = "start" - const val NOTIFICATION_PROGRESS = 200001 const val NOTIFICATION_FG = 200002 const val CHANNEL_UPLOAD_PROGRESS = "upload_progress" - fun start(context: Context) { + fun startCompat(context: Context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startImplApiO(context) } else { @@ -45,23 +43,21 @@ class UploadNotificationService : Service() { @TargetApi(Build.VERSION_CODES.O) private fun startImplApiO(context: Context) { val startIntent = Intent(context, UploadNotificationService::class.java) - startIntent.action = UploadNotificationService.ACTION_START + startIntent.action = ACTION_START context.startForegroundService(startIntent) } private fun startImplLegacy(context: Context) { val startIntent = Intent(context, UploadNotificationService::class.java) - startIntent.action = UploadNotificationService.ACTION_START + startIntent.action = ACTION_START context.startService(startIntent) } } private var uploadListListener: UploadListListener? = null - // uploads tracked by this Service, regardless of state. cleared when the service is destroyed private val uploadsByFile = mutableMapOf() - // todo - Create Notification Channels override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { val action = intent?.action if (action != ACTION_START) { @@ -90,14 +86,6 @@ class UploadNotificationService : Service() { @SuppressLint("InlinedApi", "MissingPermission") // inline use of FOREGROUND_SERVICE private fun notify(uploads: Collection) { - // todo - Manage foreground-iness: startForeground when there are running uploads, else don't - // todo - Two notification styles: 1 video and many videos - // todo - notification can't be swiped while in-progress (but provide cancel btns) - // todo - cancel notification if no uploads are running - // todo - show new notification for completion (clear count of completed uploads when both notifs are gone) - - // todo - hey wait we don't want to do notifications except by foreground svc - if (uploads.isEmpty()) { // only notify if there are uploads being tracked (in-progress or finished) return @@ -105,33 +93,27 @@ class UploadNotificationService : Service() { val uploadsInProgress = uploads.filter { it.isRunning } val uploadsCompleted = uploads.filter { it.isSuccessful } -// val uploadsPaused = uploads.filter { it.isPaused } val uploadsFailed = uploads.filter { it.error != null } - Log.d(TAG, "notify: uploadsInProgress: ${uploadsInProgress.size}") - Log.d(TAG, "notify: uploadsCompleted: ${uploadsCompleted.size}") -// Log.d(TAG, "notify: uploadsPaused: ${uploadsPaused.size}") - Log.d(TAG, "notify: uploadsFailed: ${uploadsFailed.size}") + Log.v(TAG, "notify: uploadsInProgress: ${uploadsInProgress.size}") + Log.v(TAG, "notify: uploadsCompleted: ${uploadsCompleted.size}") + Log.v(TAG, "notify: uploadsFailed: ${uploadsFailed.size}") val builder = NotificationCompat.Builder(this, CHANNEL_UPLOAD_PROGRESS) builder.setSmallIcon(R.drawable.ic_launcher) -// builder.setContentTitle(getString(R.string.app_name)) builder.setAutoCancel(false) builder.setOngoing(true) if (uploadsInProgress.isNotEmpty()) { - Log.i(TAG, "notifying progress") + Log.d(TAG, "notifying progress") if (uploadsInProgress.size == 1 && this.uploadsByFile.size == 1) { // Special case: A single upload in progress, with a single upload requested val upload = uploadsInProgress.first() val kbUploaded = (upload.currentProgress.bytesUploaded / 1024).toInt() val kbTotal = (upload.currentProgress.totalBytes / 1024).toInt() - Log.v(TAG, "upload progress: $kbUploaded of $kbTotal") - builder.setProgress(kbTotal, kbUploaded, false) builder.setContentTitle( -// builder.setContentText( resources.getQuantityString( R.plurals.notif_txt_uploading, 1, 1, 1 ) @@ -140,10 +122,9 @@ class UploadNotificationService : Service() { // Multiple uploads requested simultaneously so we batch them into one val totalKbUploaded = uploadsInProgress.sumOf { it.currentProgress.bytesUploaded / 1024 } val totalKb = uploadsInProgress.sumOf { it.currentProgress.totalBytes / 1024 } - Log.v(TAG, "upload progress: $totalKbUploaded of $totalKb") + builder.setProgress(totalKb.toInt(),totalKbUploaded.toInt(), false) builder.setContentTitle( -// builder.setContentText( resources.getQuantityString( R.plurals.notif_txt_uploading, uploads.size, @@ -153,7 +134,6 @@ class UploadNotificationService : Service() { } } else if (uploadsFailed.isNotEmpty()) { Log.i(TAG, "notifying Fail") -// builder.setContentText( builder.setContentTitle( resources.getQuantityString( R.plurals.notif_txt_failed, @@ -163,7 +143,6 @@ class UploadNotificationService : Service() { ) } else if (uploadsCompleted.isNotEmpty()) { Log.i(TAG, "notifying Complete") -// builder.setContentText( builder.setContentTitle( resources.getQuantityString( R.plurals.notif_txt_success, @@ -173,8 +152,6 @@ class UploadNotificationService : Service() { ) } -// NotificationManagerCompat.from(this).notify(NOTIFICATION_PROGRESS, builder.build()) - // always startForeground even if we're about to detach (to update the notification) ServiceCompat.startForeground( this, @@ -184,8 +161,9 @@ class UploadNotificationService : Service() { ) if (uploadsInProgress.isEmpty()) { - // we only need foreground while uploads are actually running + // we only need foreground/to even be running while uploads are actually running ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_DETACH) + stopSelf() } } diff --git a/app/src/main/java/com/mux/video/vod/demo/upload/viewmodel/CreateUploadViewModel.kt b/app/src/main/java/com/mux/video/vod/demo/upload/viewmodel/CreateUploadViewModel.kt index d2e9961a..ae710302 100644 --- a/app/src/main/java/com/mux/video/vod/demo/upload/viewmodel/CreateUploadViewModel.kt +++ b/app/src/main/java/com/mux/video/vod/demo/upload/viewmodel/CreateUploadViewModel.kt @@ -1,7 +1,6 @@ package com.mux.video.vod.demo.upload.viewmodel import android.app.Application -import android.content.Intent import android.database.Cursor import android.graphics.Bitmap import android.net.Uri @@ -69,7 +68,7 @@ class CreateUploadViewModel(private val app: Application) : AndroidViewModel(app // Force restart when creating brand new uploads (because we're making new Direct uploads) .start(forceRestart = true) - UploadNotificationService.start(app) + UploadNotificationService.startCompat(app) } }