From c38820a1107b23b1ada77b0355ac3ab915cc9ea3 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 9 Sep 2022 11:40:14 +0200 Subject: [PATCH 01/15] backup legacy downloads --- .../services/LegacyDownloadService.kt | 247 ++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 app/src/main/java/com/github/libretube/services/LegacyDownloadService.kt diff --git a/app/src/main/java/com/github/libretube/services/LegacyDownloadService.kt b/app/src/main/java/com/github/libretube/services/LegacyDownloadService.kt new file mode 100644 index 000000000..ef0aa603b --- /dev/null +++ b/app/src/main/java/com/github/libretube/services/LegacyDownloadService.kt @@ -0,0 +1,247 @@ +package com.github.libretube.services + +import android.app.DownloadManager +import android.app.PendingIntent +import android.app.Service +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.net.Uri +import android.os.Build +import android.os.Environment +import android.os.Environment.DIRECTORY_DOWNLOADS +import android.os.Environment.DIRECTORY_MOVIES +import android.os.Environment.DIRECTORY_MUSIC +import android.os.IBinder +import android.util.Log +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat +import com.github.libretube.Globals +import com.github.libretube.R +import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID +import com.github.libretube.constants.DOWNLOAD_FAILURE_NOTIFICATION_ID +import com.github.libretube.constants.DOWNLOAD_PENDING_NOTIFICATION_ID +import com.github.libretube.constants.DOWNLOAD_SUCCESS_NOTIFICATION_ID +import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.extensions.TAG +import com.github.libretube.obj.DownloadType +import com.github.libretube.util.PreferenceHelper +import java.io.File + +class LegacyDownloadService : Service() { + + private lateinit var notification: NotificationCompat.Builder + + private var downloadId: Long = -1 + private lateinit var videoName: String + private lateinit var videoUrl: String + private lateinit var audioUrl: String + private var downloadType: Int = 3 + + private lateinit var audioDir: File + private lateinit var videoDir: File + private lateinit var libretubeDir: File + private lateinit var tempDir: File + override fun onCreate() { + super.onCreate() + Globals.IS_DOWNLOAD_RUNNING = true + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + videoName = intent?.getStringExtra("videoName")!! + videoUrl = intent.getStringExtra("videoUrl")!! + audioUrl = intent.getStringExtra("audioUrl")!! + + downloadType = if (audioUrl != "") { + DownloadType.AUDIO + } else if (videoUrl != "") { + DownloadType.VIDEO + } else { + DownloadType.NONE + } + if (downloadType != DownloadType.NONE) { + downloadNotification(intent) + downloadManager() + } else { + onDestroy() + } + + return super.onStartCommand(intent, flags, startId) + } + + override fun onBind(intent: Intent?): IBinder? { + TODO("Not yet implemented") + } + + private fun downloadManager() { + // create folder for temporary files + tempDir = File( + applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS), + ".tmp" + ) + if (!tempDir.exists()) { + tempDir.mkdirs() + Log.e(TAG(), "Directory make") + } else { + tempDir.deleteRecursively() + tempDir.mkdirs() + Log.e(TAG(), "Directory already have") + } + + val downloadLocationPref = PreferenceHelper.getString(PreferenceKeys.DOWNLOAD_LOCATION, "") + val folderName = PreferenceHelper.getString(PreferenceKeys.DOWNLOAD_FOLDER, "LibreTube") + + val location = when (downloadLocationPref) { + "downloads" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) + "music" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_MUSIC) + "movies" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES) + "sdcard" -> Environment.getExternalStorageDirectory() + else -> Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) + } + + libretubeDir = File( + location, + folderName + ) + if (!libretubeDir.exists()) libretubeDir.mkdirs() + Log.i(TAG(), libretubeDir.toString()) + + // start download + try { + registerReceiver( + onDownloadComplete, + IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE) + ) + when (downloadType) { + DownloadType.VIDEO -> { + videoDir = File(libretubeDir, videoName) + downloadId = downloadManagerRequest( + getString(R.string.video), + getString(R.string.downloading), + videoUrl, + videoDir + ) + } + DownloadType.AUDIO -> { + audioDir = File(libretubeDir, videoName) + downloadId = downloadManagerRequest( + getString(R.string.audio), + getString(R.string.downloading), + audioUrl, + audioDir + ) + } + } + } catch (e: IllegalArgumentException) { + Log.e(TAG(), "download error $e") + downloadFailedNotification() + } + } + + 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 + if (downloadId == id) { + if (downloadType == DownloadType.MUX) { + downloadManagerRequest( + getString(R.string.audio), + getString(R.string.downloading), + audioUrl, + audioDir + ) + } else { + downloadSucceededNotification() + onDestroy() + } + } + } + } + + private fun downloadManagerRequest( + title: String, + descriptionText: String, + url: String, + fileDir: File + ): Long { + val request: DownloadManager.Request = + DownloadManager.Request(Uri.parse(url)) + .setTitle(title) // Title of the Download Notification + .setDescription(descriptionText) // Description of the Download Notification + .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // Visibility of the download Notification + .setDestinationUri(Uri.fromFile(fileDir)) + .setAllowedOverMetered(true) // Set if download is allowed on Mobile network + .setAllowedOverRoaming(true) // + val downloadManager: DownloadManager = + applicationContext.getSystemService(DOWNLOAD_SERVICE) as DownloadManager + return downloadManager.enqueue(request) + } + + private fun downloadNotification(intent: Intent) { + val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE) + } else { + PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT) + } + // Creating a notification and setting its various attributes + notification = + NotificationCompat.Builder(this@LegacyDownloadService, DOWNLOAD_CHANNEL_ID) + .setSmallIcon(R.drawable.ic_download) + .setContentTitle("LibreTube") + .setContentText(getString(R.string.downloading)) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setOngoing(true) + .setOnlyAlertOnce(true) + .setProgress(100, 0, true) + .setContentIntent(pendingIntent) + .setAutoCancel(true) + startForeground(DOWNLOAD_PENDING_NOTIFICATION_ID, notification.build()) + } + + private fun downloadFailedNotification() { + val builder = NotificationCompat.Builder(this@LegacyDownloadService, 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@LegacyDownloadService)) { + // notificationId is a unique int for each notification that you must define + notify(DOWNLOAD_FAILURE_NOTIFICATION_ID, builder.build()) + } + } + + private fun downloadSucceededNotification() { + Log.i(TAG(), "Download succeeded") + val builder = NotificationCompat.Builder(this@LegacyDownloadService, 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@LegacyDownloadService)) { + // notificationId is a unique int for each notification that you must define + notify(DOWNLOAD_SUCCESS_NOTIFICATION_ID, builder.build()) + } + } + + override fun onDestroy() { + try { + unregisterReceiver(onDownloadComplete) + } catch (e: Exception) { + } + + Globals.IS_DOWNLOAD_RUNNING = false + Log.d(TAG(), "dl finished!") + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + stopForeground(STOP_FOREGROUND_REMOVE) + } else { + @Suppress("DEPRECATION") + stopForeground(true) + } + + stopService(Intent(this@LegacyDownloadService, LegacyDownloadService::class.java)) + super.onDestroy() + } +} From 193c9b0e46be3f5b226a11c39d5aab0ca4cb4607 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 9 Sep 2022 13:11:54 +0200 Subject: [PATCH 02/15] change download destination --- .../libretube/services/DownloadService.kt | 101 ++++-------------- app/src/main/res/xml/advanced_settings.xml | 2 + 2 files changed, 20 insertions(+), 83 deletions(-) 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 8a2b24b26..16a27323a 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -1,7 +1,6 @@ package com.github.libretube.services import android.app.DownloadManager -import android.app.PendingIntent import android.app.Service import android.content.BroadcastReceiver import android.content.Context @@ -9,10 +8,6 @@ import android.content.Intent import android.content.IntentFilter import android.net.Uri import android.os.Build -import android.os.Environment -import android.os.Environment.DIRECTORY_DOWNLOADS -import android.os.Environment.DIRECTORY_MOVIES -import android.os.Environment.DIRECTORY_MUSIC import android.os.IBinder import android.util.Log import androidx.core.app.NotificationCompat @@ -21,12 +16,9 @@ import com.github.libretube.Globals import com.github.libretube.R import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID import com.github.libretube.constants.DOWNLOAD_FAILURE_NOTIFICATION_ID -import com.github.libretube.constants.DOWNLOAD_PENDING_NOTIFICATION_ID import com.github.libretube.constants.DOWNLOAD_SUCCESS_NOTIFICATION_ID -import com.github.libretube.constants.PreferenceKeys import com.github.libretube.extensions.TAG import com.github.libretube.obj.DownloadType -import com.github.libretube.util.PreferenceHelper import java.io.File class DownloadService : Service() { @@ -39,10 +31,8 @@ class DownloadService : Service() { private lateinit var audioUrl: String private var downloadType: Int = 3 - private lateinit var audioDir: File - private lateinit var videoDir: File - private lateinit var libretubeDir: File - private lateinit var tempDir: File + private lateinit var downloadDir: File + override fun onCreate() { super.onCreate() Globals.IS_DOWNLOAD_RUNNING = true @@ -61,7 +51,6 @@ class DownloadService : Service() { DownloadType.NONE } if (downloadType != DownloadType.NONE) { - downloadNotification(intent) downloadManager() } else { onDestroy() @@ -75,37 +64,12 @@ class DownloadService : Service() { } private fun downloadManager() { - // create folder for temporary files - tempDir = File( - applicationContext.getExternalFilesDir(DIRECTORY_DOWNLOADS), - ".tmp" + downloadDir = File( + this.getExternalFilesDir(null), + "video" ) - if (!tempDir.exists()) { - tempDir.mkdirs() - Log.e(TAG(), "Directory make") - } else { - tempDir.deleteRecursively() - tempDir.mkdirs() - Log.e(TAG(), "Directory already have") - } - val downloadLocationPref = PreferenceHelper.getString(PreferenceKeys.DOWNLOAD_LOCATION, "") - val folderName = PreferenceHelper.getString(PreferenceKeys.DOWNLOAD_FOLDER, "LibreTube") - - val location = when (downloadLocationPref) { - "downloads" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) - "music" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_MUSIC) - "movies" -> Environment.getExternalStoragePublicDirectory(DIRECTORY_MOVIES) - "sdcard" -> Environment.getExternalStorageDirectory() - else -> Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS) - } - - libretubeDir = File( - location, - folderName - ) - if (!libretubeDir.exists()) libretubeDir.mkdirs() - Log.i(TAG(), libretubeDir.toString()) + if (!downloadDir.exists()) downloadDir.mkdirs() // start download try { @@ -115,21 +79,23 @@ class DownloadService : Service() { ) when (downloadType) { DownloadType.VIDEO -> { - videoDir = File(libretubeDir, videoName) downloadId = downloadManagerRequest( getString(R.string.video), getString(R.string.downloading), videoUrl, - videoDir + Uri.fromFile( + File(downloadDir, videoName) + ) ) } DownloadType.AUDIO -> { - audioDir = File(libretubeDir, videoName) downloadId = downloadManagerRequest( getString(R.string.audio), getString(R.string.downloading), audioUrl, - audioDir + Uri.fromFile( + File(downloadDir, videoName) + ) ) } } @@ -144,19 +110,10 @@ class DownloadService : Service() { // 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 - if (downloadId == id) { - if (downloadType == DownloadType.MUX) { - downloadManagerRequest( - getString(R.string.audio), - getString(R.string.downloading), - audioUrl, - audioDir - ) - } else { - downloadSucceededNotification() - onDestroy() - } - } + if (downloadId != id) return + + downloadSucceededNotification() + onDestroy() } } @@ -164,14 +121,13 @@ class DownloadService : Service() { title: String, descriptionText: String, url: String, - fileDir: File + destination: Uri ): Long { val request: DownloadManager.Request = DownloadManager.Request(Uri.parse(url)) .setTitle(title) // Title of the Download Notification .setDescription(descriptionText) // Description of the Download Notification - .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // Visibility of the download Notification - .setDestinationUri(Uri.fromFile(fileDir)) + .setDestinationUri(destination) .setAllowedOverMetered(true) // Set if download is allowed on Mobile network .setAllowedOverRoaming(true) // val downloadManager: DownloadManager = @@ -179,27 +135,6 @@ class DownloadService : Service() { return downloadManager.enqueue(request) } - private fun downloadNotification(intent: Intent) { - val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE) - } else { - PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT) - } - // Creating a notification and setting its various attributes - notification = - NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID) - .setSmallIcon(R.drawable.ic_download) - .setContentTitle("LibreTube") - .setContentText(getString(R.string.downloading)) - .setPriority(NotificationCompat.PRIORITY_LOW) - .setOngoing(true) - .setOnlyAlertOnce(true) - .setProgress(100, 0, true) - .setContentIntent(pendingIntent) - .setAutoCancel(true) - startForeground(DOWNLOAD_PENDING_NOTIFICATION_ID, notification.build()) - } - private fun downloadFailedNotification() { val builder = NotificationCompat.Builder(this@DownloadService, DOWNLOAD_CHANNEL_ID) .setSmallIcon(R.drawable.ic_download) diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index 27a644e7e..05fdc54c5 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -2,6 +2,7 @@ + From 6bb065a87033e94f0fa4fbf4d8d58fcd4090168c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 9 Sep 2022 14:23:39 +0200 Subject: [PATCH 03/15] add download fragment --- .../libretube/adapters/DownloadsAdapter.kt | 38 +++++++++++++++++++ .../libretube/fragments/DownloadsFragment.kt | 38 +++++++++++++++++++ .../main/res/layout/downloaded_media_row.xml | 19 ++++++++++ .../main/res/layout/fragment_downloads.xml | 11 ++++++ app/src/main/res/layout/fragment_library.xml | 26 +++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt create mode 100644 app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt create mode 100644 app/src/main/res/layout/downloaded_media_row.xml create mode 100644 app/src/main/res/layout/fragment_downloads.xml diff --git a/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt new file mode 100644 index 000000000..978b87d62 --- /dev/null +++ b/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt @@ -0,0 +1,38 @@ +package com.github.libretube.adapters + +import android.annotation.SuppressLint +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.databinding.DownloadedMediaRowBinding +import java.io.File + +class DownloadsAdapter( + private val files: List +) : RecyclerView.Adapter() { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadsViewHolder { + val binding = DownloadedMediaRowBinding.inflate( + LayoutInflater.from(parent.context), + parent, + false + ) + return DownloadsViewHolder(binding) + } + + @SuppressLint("SetTextI18n") + override fun onBindViewHolder(holder: DownloadsViewHolder, position: Int) { + val file = files[position] + holder.binding.apply { + fileName.text = file.name + fileSize.text = "${file.length() / (1024 * 1024)} MiB" + } + } + + override fun getItemCount(): Int { + return files.size + } +} + +class DownloadsViewHolder( + val binding: DownloadedMediaRowBinding +) : RecyclerView.ViewHolder(binding.root) diff --git a/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt new file mode 100644 index 000000000..3722a78a0 --- /dev/null +++ b/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt @@ -0,0 +1,38 @@ +package layout + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.LinearLayoutManager +import com.github.libretube.adapters.DownloadsAdapter +import com.github.libretube.databinding.FragmentDownloadsBinding +import com.github.libretube.extensions.BaseFragment +import java.io.File + +class DownloadsFragment : BaseFragment() { + private lateinit var binding: FragmentDownloadsBinding + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + binding = FragmentDownloadsBinding.inflate(layoutInflater) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + val downloadDir = File( + context?.getExternalFilesDir(null), + "video" + ) + + binding.downloads.layoutManager = LinearLayoutManager(context) + binding.downloads.adapter = DownloadsAdapter( + downloadDir.listFiles()?.toList() ?: listOf() + ) + } +} diff --git a/app/src/main/res/layout/downloaded_media_row.xml b/app/src/main/res/layout/downloaded_media_row.xml new file mode 100644 index 000000000..6f11d1877 --- /dev/null +++ b/app/src/main/res/layout/downloaded_media_row.xml @@ -0,0 +1,19 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_downloads.xml b/app/src/main/res/layout/fragment_downloads.xml new file mode 100644 index 000000000..7ae172103 --- /dev/null +++ b/app/src/main/res/layout/fragment_downloads.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_library.xml b/app/src/main/res/layout/fragment_library.xml index 4287fa15e..dfa0b5e8d 100644 --- a/app/src/main/res/layout/fragment_library.xml +++ b/app/src/main/res/layout/fragment_library.xml @@ -73,6 +73,32 @@ + + + + + + + + Date: Fri, 9 Sep 2022 14:42:46 +0200 Subject: [PATCH 04/15] fix UI --- .../com/github/libretube/adapters/DownloadsAdapter.kt | 2 ++ .../github/libretube/fragments/DownloadsFragment.kt | 4 +++- .../com/github/libretube/fragments/LibraryFragment.kt | 10 ++++------ app/src/main/res/layout/downloaded_media_row.xml | 8 +++++--- app/src/main/res/layout/fragment_downloads.xml | 2 +- app/src/main/res/navigation/nav.xml | 9 +++++++-- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt index 978b87d62..632c895f1 100644 --- a/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt @@ -1,10 +1,12 @@ package com.github.libretube.adapters import android.annotation.SuppressLint +import android.util.Log import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.DownloadedMediaRowBinding +import com.github.libretube.extensions.TAG import java.io.File class DownloadsAdapter( diff --git a/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt index 3722a78a0..3461caf48 100644 --- a/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt @@ -1,6 +1,7 @@ -package layout +package com.github.libretube.fragments import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -8,6 +9,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import com.github.libretube.adapters.DownloadsAdapter import com.github.libretube.databinding.FragmentDownloadsBinding import com.github.libretube.extensions.BaseFragment +import com.github.libretube.extensions.TAG import java.io.File class DownloadsFragment : BaseFragment() { diff --git a/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt index b47b06ce5..b267f7aba 100644 --- a/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt @@ -30,12 +30,6 @@ class LibraryFragment : BaseFragment() { private lateinit var binding: FragmentLibraryBinding private val playerViewModel: PlayerViewModel by activityViewModels() - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - arguments?.let { - } - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -67,6 +61,10 @@ class LibraryFragment : BaseFragment() { } } + binding.downloads.setOnClickListener { + findNavController().navigate(R.id.downloadsFragment) + } + if (token != "") { binding.boogh.setImageResource(R.drawable.ic_list) binding.textLike.text = getString(R.string.emptyList) diff --git a/app/src/main/res/layout/downloaded_media_row.xml b/app/src/main/res/layout/downloaded_media_row.xml index 6f11d1877..8edbcb3e6 100644 --- a/app/src/main/res/layout/downloaded_media_row.xml +++ b/app/src/main/res/layout/downloaded_media_row.xml @@ -1,15 +1,17 @@ + android:paddingHorizontal="10dp" + android:paddingVertical="8dp"> + android:textSize="16sp" /> + android:layout_height="wrap_content"> + tools:layout="@layout/fragment_channel" /> + tools:layout="@layout/fragment_playlist" /> + \ No newline at end of file From 226350e21c2f6d7b60c5d88249502481eabf7717 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 9 Sep 2022 15:40:10 +0200 Subject: [PATCH 05/15] add offline player activity --- app/src/main/AndroidManifest.xml | 4 + .../activities/OfflinePlayerActivity.kt | 143 ++++++++++++++++++ .../libretube/adapters/DownloadsAdapter.kt | 11 +- .../github/libretube/constants/IntentData.kt | 1 + .../libretube/fragments/DownloadsFragment.kt | 2 - .../libretube/views/CustomExoPlayerView.kt | 4 +- .../res/layout/activity_offline_player.xml | 24 +++ 7 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/activities/OfflinePlayerActivity.kt create mode 100644 app/src/main/res/layout/activity_offline_player.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a962ce824..2375541e1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -42,6 +42,10 @@ android:name=".activities.CommunityActivity" android:label="@string/settings" /> + + + + + + + + + + + \ No newline at end of file From b623c628cb9a245921d407d6aac8b7b0843cc92c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 9 Sep 2022 15:56:23 +0200 Subject: [PATCH 06/15] bug fixes --- .../activities/OfflinePlayerActivity.kt | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) 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 2595af9e8..a1c2d4c26 100644 --- a/app/src/main/java/com/github/libretube/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/OfflinePlayerActivity.kt @@ -9,19 +9,13 @@ import android.view.WindowManager import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsControllerCompat -import com.github.libretube.BuildConfig 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.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem -import com.google.android.exoplayer2.source.DefaultMediaSourceFactory -import com.google.android.exoplayer2.source.MediaSource import com.google.android.exoplayer2.ui.StyledPlayerView -import com.google.android.exoplayer2.upstream.DataSource -import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory -import com.google.android.exoplayer2.util.Util import java.io.File class OfflinePlayerActivity : BaseActivity() { @@ -90,18 +84,10 @@ class OfflinePlayerActivity : BaseActivity() { } private fun setMediaSource(uri: Uri) { - val userAgent = Util.getUserAgent(applicationContext, BuildConfig.APPLICATION_ID) + val mediaItem = MediaItem + .fromUri(uri) - val dataSourceFactory: DataSource.Factory = - DefaultDataSourceFactory(applicationContext, userAgent) - - val videoItem: MediaItem = MediaItem.Builder() - .setUri(uri) - .build() - - val videoSource: MediaSource = - DefaultMediaSourceFactory(dataSourceFactory) - .createMediaSource(videoItem) + player.setMediaItem(mediaItem) /* val audioSource: MediaSource = @@ -112,8 +98,6 @@ class OfflinePlayerActivity : BaseActivity() { player.setMediaSource(mergeSource) */ - - player.setMediaSource(videoSource) } private fun hideSystemBars() { @@ -140,4 +124,9 @@ class OfflinePlayerActivity : BaseActivity() { WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()) } + + override fun onDestroy() { + player.release() + super.onDestroy() + } } From 600a99ff2813495b7e58e12bf339abdb0ac383fe Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 9 Sep 2022 16:02:16 +0200 Subject: [PATCH 07/15] fix deprecation --- .../libretube/activities/NoInternetActivity.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt b/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt index 60d9dcf56..8323a62a1 100644 --- a/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt @@ -2,6 +2,7 @@ package com.github.libretube.activities import android.content.Intent import android.os.Bundle +import androidx.activity.OnBackPressedCallback import com.github.libretube.R import com.github.libretube.databinding.ActivityNointernetBinding import com.github.libretube.extensions.BaseActivity @@ -29,10 +30,14 @@ class NoInternetActivity : BaseActivity() { startActivity(intent) } setContentView(binding.root) - } - override fun onBackPressed() { - finishAffinity() - super.onBackPressed() + onBackPressedDispatcher.addCallback( + this, + object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + finishAffinity() + } + } + ) } } From 462d247bd97de5ae22db1b9e505bbe12663d0d41 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 9 Sep 2022 16:27:34 +0200 Subject: [PATCH 08/15] make it accessible when offline --- .../activities/NoInternetActivity.kt | 9 ++ .../main/res/layout/activity_nointernet.xml | 89 +++++++++++-------- 2 files changed, 63 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt b/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt index 8323a62a1..da08387a2 100644 --- a/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt @@ -7,6 +7,7 @@ import com.github.libretube.R import com.github.libretube.databinding.ActivityNointernetBinding import com.github.libretube.extensions.BaseActivity import com.github.libretube.extensions.getStyledSnackBar +import com.github.libretube.fragments.DownloadsFragment import com.github.libretube.util.NetworkHelper import com.github.libretube.util.ThemeHelper @@ -29,6 +30,14 @@ class NoInternetActivity : BaseActivity() { val intent = Intent(this, SettingsActivity::class.java) startActivity(intent) } + + binding.downloads.setOnClickListener { + supportFragmentManager.beginTransaction() + .add(R.id.noInternet_container, DownloadsFragment()) + .addToBackStack(null) + .commit() + } + setContentView(binding.root) onBackPressedDispatcher.addCallback( diff --git a/app/src/main/res/layout/activity_nointernet.xml b/app/src/main/res/layout/activity_nointernet.xml index e131d0f7c..0fcc9d322 100644 --- a/app/src/main/res/layout/activity_nointernet.xml +++ b/app/src/main/res/layout/activity_nointernet.xml @@ -1,49 +1,68 @@ - + android:layout_height="match_parent" + android:background="?android:attr/colorBackground"> + android:src="@drawable/ic_settings" /> - - - + android:layout_gravity="center" + android:orientation="vertical"> -