mirror of https://github.com/deniscerri/ytdlnis
stuff
parent
025ec65c27
commit
6b89d3e3fd
@ -0,0 +1,30 @@
|
||||
package com.deniscerri.ytdlnis.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import androidx.work.Constraints
|
||||
import androidx.work.ExistingWorkPolicy
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import com.deniscerri.ytdlnis.work.DownloadWorker
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class AlarmStartReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(p0: Context?, p1: Intent?) {
|
||||
Log.e("assa", "Start")
|
||||
val workConstraints = Constraints.Builder()
|
||||
val workRequest = OneTimeWorkRequestBuilder<DownloadWorker>()
|
||||
.addTag("download")
|
||||
.setConstraints(workConstraints.build())
|
||||
.setInitialDelay(1000L, TimeUnit.MILLISECONDS)
|
||||
|
||||
WorkManager.getInstance(p0!!).enqueueUniqueWork(
|
||||
System.currentTimeMillis().toString(),
|
||||
ExistingWorkPolicy.REPLACE,
|
||||
workRequest.build()
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package com.deniscerri.ytdlnis.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.deniscerri.ytdlnis.database.DBManager
|
||||
import com.deniscerri.ytdlnis.database.repository.DownloadRepository
|
||||
import com.deniscerri.ytdlnis.util.NotificationUtil
|
||||
import com.deniscerri.ytdlnis.work.AlarmScheduler
|
||||
import com.yausername.youtubedl_android.YoutubeDL
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Locale
|
||||
|
||||
class AlarmStopReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(p0: Context?, p1: Intent?) {
|
||||
val dbManager = DBManager.getInstance(p0!!)
|
||||
val ytdl = YoutubeDL.getInstance()
|
||||
val notificationUtil = NotificationUtil(p0)
|
||||
val preferences = PreferenceManager.getDefaultSharedPreferences(p0)
|
||||
val alarmScheduler = AlarmScheduler(p0)
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
runCatching {
|
||||
Log.e("assa", "assssssssssssssssssssssssssssss")
|
||||
val active = dbManager.downloadDao.getActiveDownloadsList()
|
||||
|
||||
val startingTime = preferences.getString("schedule_start", "00:00")!!
|
||||
val sTime = Calendar.getInstance()
|
||||
sTime.set(Calendar.HOUR_OF_DAY, startingTime.split(":")[0].toInt())
|
||||
sTime.set(Calendar.MINUTE, startingTime.split(":")[1].toInt())
|
||||
sTime.set(Calendar.SECOND, 0)
|
||||
val time = alarmScheduler.calculateNextTime(sTime)
|
||||
|
||||
active.forEach {
|
||||
ytdl.destroyProcessById(it.id.toString())
|
||||
notificationUtil.cancelDownloadNotification(it.id.toInt())
|
||||
it.status = DownloadRepository.Status.Queued.toString()
|
||||
dbManager.downloadDao.update(it)
|
||||
}
|
||||
|
||||
AlarmScheduler(p0).schedule()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,45 +1,70 @@
|
||||
package com.deniscerri.ytdlnis.ui.more.settings
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context.POWER_SERVICE
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.PowerManager
|
||||
import android.provider.Settings
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.SeekBarPreference
|
||||
import com.deniscerri.ytdlnis.MainActivity
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.preference.SwitchPreferenceCompat
|
||||
import com.deniscerri.ytdlnis.R
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.deniscerri.ytdlnis.receiver.AlarmStartReceiver
|
||||
import com.deniscerri.ytdlnis.receiver.AlarmStopReceiver
|
||||
import com.deniscerri.ytdlnis.util.UiUtil
|
||||
import com.deniscerri.ytdlnis.work.AlarmScheduler
|
||||
import java.util.Calendar
|
||||
|
||||
|
||||
class DownloadSettingsFragment : BaseSettingsFragment() {
|
||||
override val title: Int = R.string.downloads
|
||||
|
||||
private var concurrentDownloads: SeekBarPreference? = null
|
||||
private var ignoreBatteryOptimization: Preference? = null
|
||||
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.downloading_preferences, rootKey)
|
||||
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
|
||||
val useScheduler = findPreference<SwitchPreferenceCompat>("use_scheduler")
|
||||
val scheduleStart = findPreference<Preference>("schedule_start")
|
||||
scheduleStart?.summary = preferences.getString("schedule_start", "00:00")
|
||||
val scheduleEnd = findPreference<Preference>("schedule_end")
|
||||
scheduleEnd?.summary = preferences.getString("schedule_end", "05:00")
|
||||
val scheduler = AlarmScheduler(requireContext())
|
||||
|
||||
useScheduler?.setOnPreferenceChangeListener { preference, newValue ->
|
||||
if (newValue as Boolean){
|
||||
scheduler.schedule()
|
||||
}else{
|
||||
scheduler.cancel()
|
||||
//start worker if there are leftover downloads waiting for scheduler
|
||||
val intent = Intent(context, AlarmStartReceiver::class.java)
|
||||
requireActivity().sendBroadcast(intent)
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
scheduleStart?.setOnPreferenceClickListener {
|
||||
UiUtil.showTimePicker(parentFragmentManager){
|
||||
val hr = it.get(Calendar.HOUR_OF_DAY)
|
||||
val mn = it.get(Calendar.MINUTE)
|
||||
val formattedTime = String.format("%02d", hr) + ":" + String.format("%02d", mn)
|
||||
preferences.edit().putString("schedule_start",formattedTime).apply()
|
||||
scheduleStart.summary = formattedTime
|
||||
|
||||
ignoreBatteryOptimization = findPreference("ignore_battery")
|
||||
val packageName: String = requireContext().packageName
|
||||
val pm = requireContext().applicationContext.getSystemService(POWER_SERVICE) as PowerManager
|
||||
if (pm.isIgnoringBatteryOptimizations(packageName)) {
|
||||
ignoreBatteryOptimization!!.isVisible = false
|
||||
scheduler.schedule()
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
ignoreBatteryOptimization = findPreference("ignore_battery")
|
||||
ignoreBatteryOptimization!!.onPreferenceClickListener =
|
||||
Preference.OnPreferenceClickListener {
|
||||
val intent = Intent()
|
||||
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||
intent.data = Uri.parse("package:" + requireContext().packageName)
|
||||
startActivity(intent)
|
||||
true
|
||||
scheduleEnd?.setOnPreferenceClickListener {
|
||||
UiUtil.showTimePicker(parentFragmentManager){
|
||||
val hr = it.get(Calendar.HOUR_OF_DAY)
|
||||
val mn = it.get(Calendar.MINUTE)
|
||||
val formattedTime = String.format("%02d", hr) + ":" + String.format("%02d", mn)
|
||||
preferences.edit().putString("schedule_end",formattedTime).apply()
|
||||
scheduleEnd.summary = formattedTime
|
||||
|
||||
scheduler.schedule()
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,116 @@
|
||||
package com.deniscerri.ytdlnis.work
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.deniscerri.ytdlnis.receiver.AlarmStartReceiver
|
||||
import com.deniscerri.ytdlnis.receiver.AlarmStopReceiver
|
||||
import java.util.Calendar
|
||||
|
||||
class AlarmScheduler(private val context: Context) {
|
||||
|
||||
private val alarmManager = context.getSystemService(AlarmManager::class.java)
|
||||
private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
|
||||
fun schedule() {
|
||||
cancel()
|
||||
|
||||
//schedule starting alarm
|
||||
val startingTime = preferences.getString("schedule_start", "00:00")!!
|
||||
val sTime = Calendar.getInstance()
|
||||
sTime.set(Calendar.HOUR_OF_DAY, startingTime.split(":")[0].toInt())
|
||||
sTime.set(Calendar.MINUTE, startingTime.split(":")[1].toInt())
|
||||
sTime.set(Calendar.SECOND, 0)
|
||||
val time = calculateNextTime(sTime)
|
||||
|
||||
val intent = Intent(context, AlarmStartReceiver::class.java)
|
||||
alarmManager.setExactAndAllowWhileIdle(
|
||||
AlarmManager.RTC,
|
||||
time.timeInMillis,
|
||||
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE )
|
||||
)
|
||||
|
||||
//schedule closing alarm
|
||||
val endingTime = preferences.getString("schedule_end", "05:00")!!
|
||||
val eTime = Calendar.getInstance()
|
||||
eTime.set(Calendar.HOUR_OF_DAY, endingTime.split(":")[0].toInt())
|
||||
eTime.set(Calendar.MINUTE, endingTime.split(":")[1].toInt())
|
||||
sTime.set(Calendar.SECOND, 0)
|
||||
val calendar = calculateNextTime(eTime)
|
||||
|
||||
|
||||
val intent2 = Intent(context, AlarmStopReceiver::class.java)
|
||||
alarmManager.setExactAndAllowWhileIdle(
|
||||
AlarmManager.RTC,
|
||||
calendar.timeInMillis + 60000,
|
||||
PendingIntent.getBroadcast(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE )
|
||||
)
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
alarmManager.cancel(
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
0,
|
||||
Intent(context, AlarmStartReceiver::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
|
||||
alarmManager.cancel(
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
1,
|
||||
Intent(context, AlarmStopReceiver::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun calculateNextTime(c: Calendar) : Calendar {
|
||||
val calendar = Calendar.getInstance()
|
||||
if (c.get(Calendar.HOUR_OF_DAY) < calendar.get(Calendar.HOUR_OF_DAY)){
|
||||
c.add(Calendar.DATE, 1)
|
||||
}else if (
|
||||
c.get(Calendar.HOUR_OF_DAY) == calendar.get(Calendar.HOUR_OF_DAY) &&
|
||||
c.get(Calendar.MINUTE) < calendar.get(Calendar.MINUTE)
|
||||
){
|
||||
c.add(Calendar.DATE, 1)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun isDuringTheScheduledTime(): Boolean{
|
||||
val now = Calendar.getInstance()
|
||||
val currentHour = now.get(Calendar.HOUR_OF_DAY)
|
||||
val currentMinute = now.get(Calendar.MINUTE)
|
||||
|
||||
val startingTime = preferences.getString("schedule_start", "00:00")!!
|
||||
val startingHour = startingTime.split(":")[0].toInt()
|
||||
val startingMinute = startingTime.split(":")[1].toInt()
|
||||
|
||||
val endingTime = preferences.getString("schedule_end", "05:00")!!
|
||||
var endingHour = endingTime.split(":")[0].toInt()
|
||||
if (endingHour < 12 && endingHour < startingHour){
|
||||
endingHour += 24
|
||||
}
|
||||
val endingMinute = endingTime.split(":")[1].toInt()
|
||||
|
||||
if (currentHour in startingHour..endingHour){
|
||||
if (currentHour == endingHour){
|
||||
if (currentMinute > endingMinute) {
|
||||
return false
|
||||
}
|
||||
}else if(currentHour == startingHour){
|
||||
if (currentMinute < startingMinute){
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="?android:colorAccent" android:pathData="M21,12.4V7l-4,-4H5C3.89,3 3,3.9 3,5v14c0,1.1 0.89,2 2,2h7.4L21,12.4zM15,15c0,1.66 -1.34,3 -3,3s-3,-1.34 -3,-3s1.34,-3 3,-3S15,13.34 15,15zM6,6h9v4H6V6zM19.99,16.25l1.77,1.77L16.77,23H15v-1.77L19.99,16.25zM23.25,16.51l-0.85,0.85l-1.77,-1.77l0.85,-0.85c0.2,-0.2 0.51,-0.2 0.71,0l1.06,1.06C23.45,16 23.45,16.32 23.25,16.51z"/>
|
||||
</vector>
|
||||
Loading…
Reference in New Issue