Skip to content

Commit

Permalink
Stop the service also
Browse files Browse the repository at this point in the history
  • Loading branch information
daytime-em committed Jun 12, 2024
1 parent d6f43c4 commit 175d704
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 36 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/com/mux/video/vod/demo/UploadExampleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +17,7 @@ class UploadExampleApp : Application() {
}
MuxUploadSdk.initialize(this)
if (MuxUploadManager.allUploadJobs().isNotEmpty()) {
UploadNotificationService.start(this)
UploadNotificationService.startCompat(this)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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<String, MuxUpload>()

// todo - Create Notification Channels
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val action = intent?.action
if (action != ACTION_START) {
Expand Down Expand Up @@ -90,48 +86,34 @@ class UploadNotificationService : Service() {

@SuppressLint("InlinedApi", "MissingPermission") // inline use of FOREGROUND_SERVICE
private fun notify(uploads: Collection<MuxUpload>) {
// 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
}

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
)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 175d704

Please sign in to comment.