Skip to content

Commit

Permalink
added youtube-dl update setting
Browse files Browse the repository at this point in the history
  • Loading branch information
yausername committed Jun 16, 2020
1 parent 0379396 commit ee5c2df
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
83 changes: 71 additions & 12 deletions app/src/main/java/com/yausername/dvd/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -21,28 +28,77 @@ 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)
startActivityForResult(Intent.createChooser(i, "Choose directory"), 6969)
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<YoutubeDLUpdateWorker>()
.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?) {
Expand All @@ -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<Preference>("downloadLocation")?.setSummary(path)
data?.data?.let {
val path = it.toString()
val editor = PreferenceManager.getDefaultSharedPreferences(context).edit()
editor.putString("downloadLocation", path).apply()
findPreference<Preference>("downloadLocation")?.setSummary(path)
}

}
}
}
Expand Down
72 changes: 72 additions & 0 deletions app/src/main/java/com/yausername/dvd/work/YoutubeDLUpdateWorker.kt
Original file line number Diff line number Diff line change
@@ -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"
}
}

7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
app:iconSpaceReserved="false">
</Preference>

<Preference
android:title="Update youtube-dl"
android:key="updateYoutubeDL"
app:persistent="false"
app:iconSpaceReserved="false">
</Preference>

<Preference
android:title="About"
app:summary="https://github.com/yausername/dvd"
Expand Down

0 comments on commit ee5c2df

Please sign in to comment.