Commit 5b6a6782 authored by Mygod's avatar Mygod

Refine SubscriptionService coroutines

parent 79892751
...@@ -35,16 +35,14 @@ import androidx.annotation.RequiresApi ...@@ -35,16 +35,14 @@ import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import com.crashlytics.android.Crashlytics
import com.github.shadowsocks.Core import com.github.shadowsocks.Core
import com.github.shadowsocks.Core.app import com.github.shadowsocks.Core.app
import com.github.shadowsocks.core.R import com.github.shadowsocks.core.R
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager import com.github.shadowsocks.database.ProfileManager
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Action import com.github.shadowsocks.utils.*
import com.github.shadowsocks.utils.asIterable
import com.github.shadowsocks.utils.readableMessage
import com.github.shadowsocks.utils.useCancellable
import com.google.gson.JsonStreamParser import com.google.gson.JsonStreamParser
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.io.File import java.io.File
...@@ -53,11 +51,10 @@ import java.io.InputStream ...@@ -53,11 +51,10 @@ import java.io.InputStream
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
class SubscriptionService : Service() { class SubscriptionService : Service(), CoroutineScope {
companion object { companion object {
private const val NOTIFICATION_CHANNEL = "service-subscription" private const val NOTIFICATION_CHANNEL = "service-subscription"
private const val NOTIFICATION_ID = 2 private const val NOTIFICATION_ID = 2
private var worker: Job? = null
val idle = MutableLiveData<Boolean>(true) val idle = MutableLiveData<Boolean>(true)
...@@ -65,12 +62,9 @@ class SubscriptionService : Service() { ...@@ -65,12 +62,9 @@ class SubscriptionService : Service() {
app.getText(R.string.service_subscription), NotificationManager.IMPORTANCE_LOW) app.getText(R.string.service_subscription), NotificationManager.IMPORTANCE_LOW)
} }
private object CancelReceiver : BroadcastReceiver() { override val coroutineContext = SupervisorJob() + CoroutineExceptionHandler { _, t -> printLog(t) }
override fun onReceive(context: Context?, intent: Intent?) { private var worker: Job? = null
worker?.cancel() private val cancelReceiver = broadcastReceiver { _, _ -> worker?.cancel() }
}
}
private var counter = 0 private var counter = 0
private var receiverRegistered = false private var receiverRegistered = false
...@@ -80,10 +74,10 @@ class SubscriptionService : Service() { ...@@ -80,10 +74,10 @@ class SubscriptionService : Service() {
if (worker == null) { if (worker == null) {
idle.value = false idle.value = false
if (!receiverRegistered) { if (!receiverRegistered) {
registerReceiver(CancelReceiver, IntentFilter(Action.ABORT), "$packageName.SERVICE", null) registerReceiver(cancelReceiver, IntentFilter(Action.ABORT), "$packageName.SERVICE", null)
receiverRegistered = true receiverRegistered = true
} }
worker = GlobalScope.launch { worker = launch {
val urls = Subscription.instance.urls val urls = Subscription.instance.urls
val notification = NotificationCompat.Builder(this@SubscriptionService, NOTIFICATION_CHANNEL).apply { val notification = NotificationCompat.Builder(this@SubscriptionService, NOTIFICATION_CHANNEL).apply {
color = ContextCompat.getColor(this@SubscriptionService, R.color.material_primary_500) color = ContextCompat.getColor(this@SubscriptionService, R.color.material_primary_500)
...@@ -145,7 +139,7 @@ class SubscriptionService : Service() { ...@@ -145,7 +139,7 @@ class SubscriptionService : Service() {
return tempFile return tempFile
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
GlobalScope.launch(Dispatchers.Main) { launch(Dispatchers.Main) {
Toast.makeText(this@SubscriptionService, e.readableMessage, Toast.LENGTH_LONG).show() Toast.makeText(this@SubscriptionService, e.readableMessage, Toast.LENGTH_LONG).show()
} }
if (!tempFile.delete()) tempFile.deleteOnExit() if (!tempFile.delete()) tempFile.deleteOnExit()
...@@ -212,8 +206,8 @@ class SubscriptionService : Service() { ...@@ -212,8 +206,8 @@ class SubscriptionService : Service() {
} }
override fun onDestroy() { override fun onDestroy() {
worker?.cancel() cancel()
if (receiverRegistered) unregisterReceiver(CancelReceiver) if (receiverRegistered) unregisterReceiver(cancelReceiver)
super.onDestroy() super.onDestroy()
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment