Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
5b6a6782
Commit
5b6a6782
authored
Feb 12, 2020
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refine SubscriptionService coroutines
parent
79892751
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
17 deletions
+11
-17
core/src/main/java/com/github/shadowsocks/subscription/SubscriptionService.kt
...om/github/shadowsocks/subscription/SubscriptionService.kt
+11
-17
No files found.
core/src/main/java/com/github/shadowsocks/subscription/SubscriptionService.kt
View file @
5b6a6782
...
@@ -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
(
C
ancelReceiver
,
IntentFilter
(
Action
.
ABORT
),
"$packageName.SERVICE"
,
null
)
registerReceiver
(
c
ancelReceiver
,
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
(
C
ancelReceiver
)
if
(
receiverRegistered
)
unregisterReceiver
(
c
ancelReceiver
)
super
.
onDestroy
()
super
.
onDestroy
()
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment