From cf46afdc86aa07b8d16f692b57099c49737c8d76 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 10 Sep 2022 11:51:50 +0200 Subject: [PATCH] cleanup --- .../activities/OfflinePlayerActivity.kt | 13 ++---- .../libretube/services/DownloadService.kt | 46 ++++++++----------- .../github/libretube/util/DownloadHelper.kt | 24 ++++++++++ 3 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/util/DownloadHelper.kt diff --git a/app/src/main/java/com/github/libretube/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/activities/OfflinePlayerActivity.kt index 1d00a3378..077e036ad 100644 --- a/app/src/main/java/com/github/libretube/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/OfflinePlayerActivity.kt @@ -13,6 +13,7 @@ import com.github.libretube.constants.IntentData import com.github.libretube.databinding.ActivityOfflinePlayerBinding import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.extensions.BaseActivity +import com.github.libretube.util.DownloadHelper import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.source.MergingMediaSource @@ -69,20 +70,13 @@ class OfflinePlayerActivity : BaseActivity() { } private fun playVideo() { - val videoDownloadDir = File( - getExternalFilesDir(null), - "video" - ) - + val videoDownloadDir = DownloadHelper.getVideoDir(this) val videoFile = File( videoDownloadDir, fileName ) - val audioDownloadDir = File( - getExternalFilesDir(null), - "audio" - ) + val audioDownloadDir = DownloadHelper.getAudioDir(this) val audioFile = File( audioDownloadDir, fileName @@ -129,6 +123,7 @@ class OfflinePlayerActivity : BaseActivity() { } } + @Suppress("DEPRECATION") private fun hideSystemBars() { window?.decorView?.systemUiVisibility = ( View.SYSTEM_UI_FLAG_LAYOUT_STABLE diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index a8380883f..082fe4852 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -7,7 +7,6 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.net.Uri -import android.os.Build import android.os.IBinder import android.util.Log import androidx.core.app.NotificationCompat @@ -19,6 +18,7 @@ import com.github.libretube.constants.DOWNLOAD_FAILURE_NOTIFICATION_ID import com.github.libretube.constants.DOWNLOAD_SUCCESS_NOTIFICATION_ID import com.github.libretube.constants.DownloadType import com.github.libretube.extensions.TAG +import com.github.libretube.util.DownloadHelper import java.io.File class DownloadService : Service() { @@ -28,8 +28,6 @@ class DownloadService : Service() { private lateinit var audioUrl: String private var downloadType: Int = 3 - private lateinit var videoDownloadDir: File - private lateinit var audioDownloadDir: File private var videoDownloadId: Long? = null private var audioDownloadId: Long? = null @@ -64,19 +62,14 @@ class DownloadService : Service() { } private fun downloadManager() { - videoDownloadDir = File( - this.getExternalFilesDir(null), - "video" - ) + // initialize and create the directories to download into - if (!videoDownloadDir.exists()) videoDownloadDir.mkdirs() + val videoDownloadDir = DownloadHelper.getVideoDir(this) + val audioDownloadDir = DownloadHelper.getAudioDir(this) - audioDownloadDir = File( - this.getExternalFilesDir(null), - "audio" - ) - - if (!audioDownloadDir.exists()) audioDownloadDir.mkdirs() + listOf(videoDownloadDir, audioDownloadDir).forEach { + if (!it.exists()) it.mkdir() + } // start download try { @@ -113,9 +106,13 @@ class DownloadService : Service() { private val onDownloadComplete: BroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { // Fetching the download id received with the broadcast - val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) // Checking if the received broadcast is for our enqueued download by matching download id - when (id) { + when ( + intent.getLongExtra( + DownloadManager.EXTRA_DOWNLOAD_ID, + -1 + ) + ) { videoDownloadId -> videoDownloadId = null audioDownloadId -> audioDownloadId = null } @@ -147,13 +144,13 @@ class DownloadService : Service() { } private fun downloadFailedNotification() { - val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID) + val builder = NotificationCompat.Builder(this, DOWNLOAD_CHANNEL_ID) .setSmallIcon(R.drawable.ic_download) .setContentTitle(resources.getString(R.string.downloadfailed)) .setContentText(getString(R.string.fail)) .setPriority(NotificationCompat.PRIORITY_HIGH) - with(NotificationManagerCompat.from(this@DownloadService)) { + with(NotificationManagerCompat.from(this)) { // notificationId is a unique int for each notification that you must define notify(DOWNLOAD_FAILURE_NOTIFICATION_ID, builder.build()) } @@ -161,13 +158,13 @@ class DownloadService : Service() { private fun downloadSucceededNotification() { Log.i(TAG(), "Download succeeded") - val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID) + val builder = NotificationCompat.Builder(this, DOWNLOAD_CHANNEL_ID) .setSmallIcon(R.drawable.ic_download) .setContentTitle(resources.getString(R.string.success)) .setContentText(getString(R.string.downloadsucceeded)) .setPriority(NotificationCompat.PRIORITY_HIGH) - with(NotificationManagerCompat.from(this@DownloadService)) { + with(NotificationManagerCompat.from(this)) { // notificationId is a unique int for each notification that you must define notify(DOWNLOAD_SUCCESS_NOTIFICATION_ID, builder.build()) } @@ -181,14 +178,7 @@ class DownloadService : Service() { Globals.IS_DOWNLOAD_RUNNING = false - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - stopForeground(STOP_FOREGROUND_REMOVE) - } else { - @Suppress("DEPRECATION") - stopForeground(true) - } - - stopService(Intent(this@DownloadService, DownloadService::class.java)) + stopService(Intent(this, DownloadService::class.java)) super.onDestroy() } } diff --git a/app/src/main/java/com/github/libretube/util/DownloadHelper.kt b/app/src/main/java/com/github/libretube/util/DownloadHelper.kt new file mode 100644 index 000000000..79dbcaded --- /dev/null +++ b/app/src/main/java/com/github/libretube/util/DownloadHelper.kt @@ -0,0 +1,24 @@ +package com.github.libretube.util + +import android.content.Context +import java.io.File + +object DownloadHelper { + private fun getOfflineStorageDir(context: Context): File { + return context.getExternalFilesDir(null)!! + } + + fun getVideoDir(context: Context): File { + return File( + getOfflineStorageDir(context), + "video" + ) + } + + fun getAudioDir(context: Context): File { + return File( + getOfflineStorageDir(context), + "audio" + ) + } +}