This commit is contained in:
Bnyro 2022-06-07 12:42:59 +02:00
parent 533f69637b
commit 583a96d54f
14 changed files with 157 additions and 50 deletions

View File

@ -5,6 +5,7 @@ import android.app.PendingIntent
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.DOWNLOAD_SERVICE
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
@ -15,6 +16,10 @@ import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.core.net.toFile
import androidx.core.net.toUri
import androidx.preference.PreferenceManager
import com.arthenica.ffmpegkit.FFmpegKit
import java.io.File
@ -44,8 +49,9 @@ class DownloadService : Service() {
videoId = intent?.getStringExtra("videoId")!!
videoUrl = intent.getStringExtra("videoUrl")!!
audioUrl = intent.getStringExtra("audioUrl")!!
extension = intent.getStringExtra("extension")!!
duration = intent.getIntExtra("duration", 1)
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
extension = sharedPreferences.getString("video_format", ".mp4")!!
downloadType = if (audioUrl != "" && videoUrl != "") "mux"
else if (audioUrl != "") "audio"
else if (videoUrl != "") "video"
@ -81,11 +87,22 @@ class DownloadService : Service() {
}
// create LibreTube folder in Downloads
/*
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val downloadsDirectory = sharedPreferences.getString("download_directory_path", "")
Log.i(TAG, downloadsDirectory!!)
libretubeDir = if (downloadsDirectory == "") File(
Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS),
"LibreTube"
)
else File(downloadsDirectory)
*/
libretubeDir = File(
Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS),
"LibreTube"
)
if (!libretubeDir.exists()) libretubeDir.mkdirs()
Log.i(TAG, libretubeDir.toString())
// start download
try {

View File

@ -6,7 +6,7 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.github.libretube.preferences.SettingsFragment
import com.github.libretube.preferences.MainSettings
import com.github.libretube.util.ThemeHelper
import com.google.android.material.color.DynamicColors
@ -14,7 +14,7 @@ var isCurrentViewMainSettings = true
var requireMainActivityRestart = false
class SettingsActivity : AppCompatActivity() {
val TAG = "SettingsActivity"
override fun onCreate(savedInstanceState: Bundle?) {
DynamicColors.applyToActivityIfAvailable(this)
ThemeHelper().updateTheme(this)
@ -31,7 +31,7 @@ class SettingsActivity : AppCompatActivity() {
if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.replace(R.id.settings, MainSettings())
.commit()
}
}
@ -54,7 +54,7 @@ class SettingsActivity : AppCompatActivity() {
isCurrentViewMainSettings = true
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.replace(R.id.settings, MainSettings())
.commit()
}
}

View File

@ -13,8 +13,6 @@ import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Spinner
import android.widget.TextView
import androidx.core.app.ActivityCompat
@ -29,13 +27,8 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
class DownloadDialog : DialogFragment() {
private val TAG = "DownloadDialog"
var streams: Streams = Streams()
var vidName = arrayListOf<String>()
var vidUrl = arrayListOf<String>()
var audioName = arrayListOf<String>()
var audioUrl = arrayListOf<String>()
var selectedVideo = 0
var selectedAudio = 0
var extension = ".mp4"
var duration = 0
private lateinit var videoId: String
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
@ -142,19 +135,13 @@ class DownloadDialog : DialogFragment() {
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
val radioGroup = view.findViewById<RadioGroup>(R.id.radioGp)
radioGroup.setOnCheckedChangeListener { group, checkedId ->
val radio: RadioButton = view.findViewById(checkedId)
extension = radio.text.toString()
Log.d(TAG, extension)
}
view.findViewById<Button>(R.id.download).setOnClickListener {
val intent = Intent(context, DownloadService::class.java)
intent.putExtra("videoId", videoId)
intent.putExtra("videoUrl", vidUrl[selectedVideo])
intent.putExtra("audioUrl", audioUrl[selectedAudio])
intent.putExtra("duration", duration)
intent.putExtra("extension", extension)
// intent.putExtra("command","-y -i ${response.videoStreams[which].url} -i ${response.audioStreams!![0].url} -c copy ${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)}/${videoId}.mkv")
context?.startService(intent)
dismiss()

View File

@ -11,14 +11,13 @@ import com.github.libretube.requireMainActivityRestart
import com.github.libretube.util.ThemeHelper
class AppearanceSettings : PreferenceFragmentCompat() {
private val TAG = "CustomizationSettings"
private val TAG = "AppearanceSettings"
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.appearance_settings, rootKey)
val themeToggle = findPreference<ListPreference>("theme_togglee")
themeToggle?.setOnPreferenceChangeListener { _, _ ->
val refresh = Intent(context, SettingsActivity::class.java)
startActivity(refresh)
activity?.recreate()
requireMainActivityRestart = true
true
}
@ -26,8 +25,7 @@ class AppearanceSettings : PreferenceFragmentCompat() {
val accentColor = findPreference<Preference>("accent_color")
accentColor?.setOnPreferenceChangeListener { _, _ ->
requireMainActivityRestart = true
val refresh = Intent(context, SettingsActivity::class.java)
startActivity(refresh)
activity?.recreate()
true
}

View File

@ -0,0 +1,44 @@
package com.github.libretube.preferences
import android.content.Intent
import android.os.Bundle
import android.provider.DocumentsContract
import androidx.core.net.toUri
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.github.libretube.R
class DownloadSettings : PreferenceFragmentCompat() {
val TAG = "DownloadSettings"
private val directoryRequestCode = 9999
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.download_settings, rootKey)
// doesn't work yet
val directory = findPreference<Preference>("download_directory")
directory?.setOnPreferenceClickListener {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
val pickerInitialUri = sharedPreferences.getString("download_directory_path", "")?.toUri()
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
startActivityForResult(Intent.createChooser(intent, "Choose directory"), directoryRequestCode)
true
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
directoryRequestCode -> {
val directoryUri = data?.data
// save selected download directory to the shared preferences
val sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(requireContext())
sharedPreferences.edit().putString("download_directory_path", directoryUri.toString())
.apply()
}
}
}
}

View File

@ -37,8 +37,8 @@ import org.json.JSONObject
import org.json.JSONTokener
import retrofit2.HttpException
class SettingsFragment : PreferenceFragmentCompat() {
val TAG = "Settings"
class MainSettings : PreferenceFragmentCompat() {
val TAG = "SettingsFragment"
companion object {
lateinit var getContent: ActivityResultLauncher<String>
@ -165,6 +165,13 @@ class SettingsFragment : PreferenceFragmentCompat() {
true
}
val downloads = findPreference<Preference>("downloads")
downloads?.setOnPreferenceClickListener {
val newFragment = DownloadSettings()
navigateSettings(newFragment)
true
}
val history = findPreference<Preference>("history")
history?.setOnPreferenceClickListener {
val newFragment = HistorySettings()

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal" >
<path
android:fillColor="@android:color/white"
android:pathData="M19,9h-4V3H9v6H5l7,7 7,-7zM5,18v2h14v-2H5z" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?android:attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M20,6h-8l-2,-2L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,8c0,-1.1 -0.9,-2 -2,-2zM20,18L4,18L4,8h16v10z" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?android:attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M17,10.5V7c0,-0.55 -0.45,-1 -1,-1H4c-0.55,0 -1,0.45 -1,1v10c0,0.55 0.45,1 1,1h12c0.55,0 1,-0.45 1,-1v-3.5l4,4v-11l-4,4z" />
</vector>

View File

@ -26,29 +26,6 @@
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<RadioGroup
android:id="@+id/radioGp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:visibility="gone">
<RadioButton
android:id="@+id/mkv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".mkv"
android:layout_marginRight="16dp"
android:checked="true" />
<RadioButton
android:id="@+id/mp4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=".mp4" />
</RadioGroup>
<Button
android:id="@+id/download"
android:layout_width="wrap_content"
@ -61,4 +38,5 @@
android:padding="8dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp" />
</LinearLayout>

View File

@ -599,4 +599,15 @@
<item>IconFlame</item>
<item>IconBird</item>
</string-array>
<string-array name="videoFormats">
<item>MP4</item>
<item>MKV</item>
</string-array>
<string-array name="videoFormatsValues">
<item>.mp4</item>
<item>.mkv</item>
</string-array>
</resources>

View File

@ -120,4 +120,9 @@
<string name="update_available_text">There is a new update available. Click okay to become redirected to the update page on GitHub.</string>
<string name="appearance">Appearance</string>
<string name="app_behavior">App Behavior</string>
<string name="downloads">Downloads</string>
<string name="video_format">Video format</string>
<string name="video_format_summary">The video format downloaded videos get converted to (only applies if both, audio and video, are downloaded).</string>
<string name="download_directory">Download directory</string>
<string name="download_directory_summary">The directory where your downloaded media gets stored.</string>
</resources>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/downloads">
<ListPreference
app:key="video_format"
app:title="@string/video_format"
app:entries="@array/videoFormats"
app:entryValues="@array/videoFormatsValues"
app:defaultValue=".mp4"
app:summary="@string/video_format_summary"
app:icon="@drawable/ic_videocam" />
<!--
<Preference
app:key="download_directory"
app:title="@string/download_directory"
app:summary="@string/download_directory_summary"
app:icon="@drawable/ic_folder" />
-->
</PreferenceCategory>
</androidx.preference.PreferenceScreen>

View File

@ -56,7 +56,7 @@
<PreferenceCategory app:title="@string/customization">
<Preference
<androidx.preference.Preference
app:key="appearance"
app:title="@string/appearance"
android:icon="@drawable/ic_color" />
@ -67,6 +67,11 @@
app:summary="@string/sponsorblock_summary"
android:icon="@drawable/ic_block" />
<androidx.preference.Preference
app:title="@string/downloads"
app:key="downloads"
android:icon="@drawable/ic_file_download" />
<androidx.preference.Preference
app:key="history"
app:title="@string/history"