Fix Automatic update subscriptions

This commit is contained in:
2dust committed 2023-11-15 15:27:53 +08:00
1 parent 29848053a4
commit f25c0cc890
6 files changed
+40 -49

No files matched your search

@@ -1,29 +1,24 @@
package com.v2ray.ang.service
import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.extension.toast
import com.v2ray.ang.util.AngConfigManager
import com.v2ray.ang.util.MmkvManager
import com.v2ray.ang.util.Utils
import kotlinx.coroutines.delay
object SubscriptionUpdater {
const val notificationChannel = "update-subscription-channel"
const val notificationChannel = "subscription_update_channel"
class UpdateTask(context: Context, params: WorkerParameters) :
CoroutineWorker(context, params) {
@@ -31,23 +26,20 @@ object SubscriptionUpdater {
private val notificationManager = NotificationManagerCompat.from(applicationContext)
private val notification =
NotificationCompat.Builder(applicationContext, notificationChannel)
.setWhen(0)
.setTicker("Update")
.setContentTitle("Subscription Update")
.setContentTitle(context.getString(R.string.title_pref_auto_update_subscription))
.setSmallIcon(R.drawable.ic_stat_name)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(NotificationCompat.PRIORITY_MIN)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
@SuppressLint("MissingPermission")
override suspend fun doWork(): Result {
Log.d(AppConfig.ANG_PACKAGE, "start updating subscriptions")
Log.d(AppConfig.ANG_PACKAGE, "subscription automatic update starting")
val subs = MmkvManager.decodeSubscriptions().filter { it.second.autoUpdate }
for (i in subs) {
val subscription = i.second
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -61,7 +53,10 @@ object SubscriptionUpdater {
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(3, notification.build())
Log.d(AppConfig.ANG_PACKAGE, "update: ${subscription.remarks} subscription")
Log.d(
AppConfig.ANG_PACKAGE,
"subscription automatic update: ---${subscription.remarks}"
)
val configs = Utils.getUrlContentWithCustomUserAgent(subscription.url)
importBatchConfig(configs, i.first)
notification.setContentText("Updating ${subscription.remarks}")
@@ -72,10 +67,9 @@ object SubscriptionUpdater {
}
fun importBatchConfig(server: String?, subid: String = "") {
val append = subid.isEmpty()
val append = subid.isNullOrEmpty()
var count = AngConfigManager.importBatchConfig(server, subid, append)
val count = AngConfigManager.importBatchConfig(server, subid, append)
if (count <= 0) {
AngConfigManager.importBatchConfig(Utils.decode(server!!), subid, append)
}
@@ -243,11 +243,7 @@ class SettingsActivity : BaseActivity() {
.apply {
setInitialDelay(interval, TimeUnit.MINUTES)
}
.setConstraints(
Constraints(
NetworkType.CONNECTED,
)
).build()
.build()
)
}