diff --git a/app/build.gradle b/app/build.gradle index 4d12c00..2af19b3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -89,8 +89,8 @@ dependencies { implementation "androidx.work:work-runtime-ktx:$work_version" - implementation "com.github.yausername.youtubedl-android:library:0.10.1" - implementation "com.github.yausername.youtubedl-android:ffmpeg:0.10.1" + implementation "com.github.yausername.youtubedl-android:library:0.10.2" + implementation "com.github.yausername.youtubedl-android:ffmpeg:0.10.2" implementation "com.squareup.picasso:picasso:$picassoVersion" diff --git a/app/src/main/java/com/yausername/dvd/ui/SettingsFragment.kt b/app/src/main/java/com/yausername/dvd/ui/SettingsFragment.kt index ffa1d26..b6911b2 100644 --- a/app/src/main/java/com/yausername/dvd/ui/SettingsFragment.kt +++ b/app/src/main/java/com/yausername/dvd/ui/SettingsFragment.kt @@ -5,12 +5,19 @@ import android.os.Build import android.os.Bundle import android.view.Menu import android.view.View +import android.widget.Toast import androidx.appcompat.app.AppCompatDelegate import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager +import androidx.work.ExistingWorkPolicy +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.WorkInfo +import androidx.work.WorkManager import com.yausername.dvd.R +import com.yausername.dvd.work.YoutubeDLUpdateWorker +import com.yausername.youtubedl_android.YoutubeDL class SettingsFragment : PreferenceFragmentCompat() { @@ -21,21 +28,30 @@ class SettingsFragment : PreferenceFragmentCompat() { themePreference?.let { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { it.entries = arrayOf("Light", "Dark", "Set by Battery Saver"); - it.entryValues = arrayOf(AppCompatDelegate.MODE_NIGHT_NO.toString(), AppCompatDelegate.MODE_NIGHT_YES.toString(), AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY.toString()); - } else{ + it.entryValues = arrayOf( + AppCompatDelegate.MODE_NIGHT_NO.toString(), + AppCompatDelegate.MODE_NIGHT_YES.toString(), + AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY.toString() + ); + } else { it.entries = arrayOf("Light", "Dark", "System Default"); - it.entryValues = arrayOf(AppCompatDelegate.MODE_NIGHT_NO.toString(), AppCompatDelegate.MODE_NIGHT_YES.toString(), AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM.toString()); + it.entryValues = arrayOf( + AppCompatDelegate.MODE_NIGHT_NO.toString(), + AppCompatDelegate.MODE_NIGHT_YES.toString(), + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM.toString() + ); } } - themePreference?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue -> - AppCompatDelegate.setDefaultNightMode(newValue.toString().toInt()) - true - } + themePreference?.onPreferenceChangeListener = + Preference.OnPreferenceChangeListener { preference, newValue -> + AppCompatDelegate.setDefaultNightMode(newValue.toString().toInt()) + true + } val downloadLocationPref: Preference? = findPreference("downloadLocation") val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context) - downloadLocationPref?.setSummary(sharedPrefs.getString("downloadLocation", "Set default download location")) downloadLocationPref?.let { + it.summary = sharedPrefs.getString("downloadLocation", "Set default download location") it.onPreferenceClickListener = Preference.OnPreferenceClickListener { val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) i.addCategory(Intent.CATEGORY_DEFAULT) @@ -43,6 +59,46 @@ class SettingsFragment : PreferenceFragmentCompat() { true } } + + val updateYoutubeDLPref: Preference? = findPreference("updateYoutubeDL") + updateYoutubeDLPref?.let { + it.summary = YoutubeDL.getInstance().version(requireContext().applicationContext) + it.onPreferenceClickListener = Preference.OnPreferenceClickListener { + updateYoutubeDL() + true + } + } + } + + private fun updateYoutubeDL() { + val workTag = "youtube-dl-update" + val workManager = WorkManager.getInstance(requireContext().applicationContext) + val state = + workManager.getWorkInfosByTag(workTag).get()?.getOrNull(0)?.state + val running = state === WorkInfo.State.RUNNING || state === WorkInfo.State.ENQUEUED + if (running) { + Toast.makeText( + context, + "update is already in progress", + Toast.LENGTH_SHORT + ).show() + return + } + val workRequest = OneTimeWorkRequestBuilder() + .addTag(workTag) + .build() + + workManager.enqueueUniqueWork( + workTag, + ExistingWorkPolicy.KEEP, + workRequest + ) + + Toast.makeText( + context, + "Update queued. Check notification for progress", + Toast.LENGTH_SHORT + ).show() } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -66,10 +122,13 @@ class SettingsFragment : PreferenceFragmentCompat() { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { 6969 -> { - val path = data!!.data!!.toString() - val editor = PreferenceManager.getDefaultSharedPreferences(context).edit() - editor.putString("downloadLocation", path).apply() - findPreference("downloadLocation")?.setSummary(path) + data?.data?.let { + val path = it.toString() + val editor = PreferenceManager.getDefaultSharedPreferences(context).edit() + editor.putString("downloadLocation", path).apply() + findPreference("downloadLocation")?.setSummary(path) + } + } } } diff --git a/app/src/main/java/com/yausername/dvd/work/YoutubeDLUpdateWorker.kt b/app/src/main/java/com/yausername/dvd/work/YoutubeDLUpdateWorker.kt new file mode 100644 index 0000000..cdf2e2e --- /dev/null +++ b/app/src/main/java/com/yausername/dvd/work/YoutubeDLUpdateWorker.kt @@ -0,0 +1,72 @@ +package com.yausername.dvd.work + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import android.os.Build +import android.widget.Toast +import androidx.core.app.NotificationCompat +import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo +import androidx.work.WorkerParameters +import com.yausername.dvd.R +import com.yausername.youtubedl_android.YoutubeDL +import com.yausername.youtubedl_android.YoutubeDLUpdater +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + + +class YoutubeDLUpdateWorker(appContext: Context, params: WorkerParameters) : + CoroutineWorker(appContext, params) { + + private val notificationManager = + appContext.getSystemService(Context.NOTIFICATION_SERVICE) as + NotificationManager + + + override suspend fun doWork(): Result { + + createNotificationChannel() + val notificationId = id.hashCode() + val notification = NotificationCompat.Builder(applicationContext, + channelId + ) + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("Updating youtube-dl") + .build() + + val foregroundInfo = ForegroundInfo(notificationId, notification) + setForeground(foregroundInfo) + + val result = YoutubeDL.getInstance().updateYoutubeDL(applicationContext) + if (result == YoutubeDLUpdater.UpdateStatus.ALREADY_UP_TO_DATE) { + withContext(Dispatchers.Main){ + Toast.makeText(applicationContext, "already up to date", Toast.LENGTH_SHORT).show(); + } + } + return Result.success() + } + + private fun createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + var notificationChannel = + notificationManager?.getNotificationChannel(channelId) + if (notificationChannel == null) { + notificationChannel = NotificationChannel( + channelId, + channelName, NotificationManager.IMPORTANCE_LOW + ) + notificationChannel.description = + channelDescription + notificationManager?.createNotificationChannel(notificationChannel) + } + } + } + + companion object { + const val channelName = "youtube-dl update" + const val channelDescription = "youtube-dl update" + const val channelId = "youtube-dl update" + } +} + diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 9f35339..39362ce 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -14,6 +14,13 @@ app:iconSpaceReserved="false"> + + +