mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
New look based on the SparkleDNS concept: a dark theme, a floating bottom panel with Home, Activity and Settings, and pages for the blocklists, apps and About. - Home: big ON/OFF switch, the current server, 24-hour stats and chart - Activity: requests, blocked, average time and cache share for 24 hours or 7 days, company dominance, and the live log with search and filter - Blocklists: HaGeZi Pro preset next to Pro++ and TIF, lists can be switched off without deleting them, automatic updates (daily or weekly, optionally Wi-Fi only) through JobScheduler - Settings: server list, custom server, server test and per-app DNS - Hourly numbers for the charts are kept on the device, without domains The server list now ships with the app; it is no longer downloaded from the repository.
67 lines
2.6 KiB
Kotlin
67 lines
2.6 KiB
Kotlin
package cc.skysparkle.sparkledns
|
|
|
|
import android.app.job.JobInfo
|
|
import android.app.job.JobParameters
|
|
import android.app.job.JobScheduler
|
|
import android.app.job.JobService
|
|
import android.content.ComponentName
|
|
import android.content.Context
|
|
import android.util.Log
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
enum class UpdateInterval(val days: Long) { OFF(0), DAILY(1), WEEKLY(7) }
|
|
|
|
// Downloads newer versions of the user's blocklists in the background. Uses the system
|
|
// JobScheduler, so the app needs no extra library and nothing runs while the conditions are not met.
|
|
object AutoUpdate {
|
|
private const val JOB_ID = 4201
|
|
|
|
// JobInfo.networkType is deprecated in favour of NetworkRequest, which needs API 28
|
|
@Suppress("DEPRECATION")
|
|
fun apply(context: Context) {
|
|
val prefs = Prefs(context)
|
|
val scheduler = context.getSystemService(JobScheduler::class.java) ?: return
|
|
val interval = prefs.updateInterval
|
|
if (interval == UpdateInterval.OFF) {
|
|
scheduler.cancel(JOB_ID)
|
|
return
|
|
}
|
|
val period = TimeUnit.DAYS.toMillis(interval.days)
|
|
val network = if (prefs.updateWifiOnly) JobInfo.NETWORK_TYPE_UNMETERED else JobInfo.NETWORK_TYPE_ANY
|
|
// Scheduling the same job again would start its period over, so an unchanged one is kept
|
|
val existing = scheduler.getPendingJob(JOB_ID)
|
|
if (existing != null && existing.intervalMillis == period && existing.networkType == network) return
|
|
|
|
val job = JobInfo.Builder(JOB_ID, ComponentName(context, BlocklistUpdateJob::class.java))
|
|
.setPeriodic(period)
|
|
.setRequiredNetworkType(network)
|
|
.setRequiresBatteryNotLow(true)
|
|
.setPersisted(true)
|
|
.build()
|
|
try {
|
|
scheduler.schedule(job)
|
|
} catch (e: RuntimeException) {
|
|
Log.w("SparkleDNS", "Cannot schedule blocklist updates", e)
|
|
}
|
|
}
|
|
}
|
|
|
|
class BlocklistUpdateJob : JobService() {
|
|
override fun onStartJob(params: JobParameters): Boolean {
|
|
Thread({
|
|
val ok = try {
|
|
Blocklist.updateAllNow(applicationContext)
|
|
} catch (e: Exception) {
|
|
Log.w("SparkleDNS", "Blocklist update failed", e)
|
|
false
|
|
}
|
|
// A failed run is retried later with the system's back-off
|
|
jobFinished(params, !ok)
|
|
}, "blocklist-update").start()
|
|
return true
|
|
}
|
|
|
|
// Downloads cannot be cut short halfway; the next run picks up where this one stopped
|
|
override fun onStopJob(params: JobParameters): Boolean = true
|
|
}
|