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
940e4e30
Commit
940e4e30
authored
Jan 12, 2020
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow subscription service to be aborted
parent
20e4023c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
5 deletions
+28
-5
core/src/main/java/com/github/shadowsocks/bg/ServiceNotification.kt
...ain/java/com/github/shadowsocks/bg/ServiceNotification.kt
+2
-2
core/src/main/java/com/github/shadowsocks/subscription/SubscriptionService.kt
...om/github/shadowsocks/subscription/SubscriptionService.kt
+25
-3
core/src/main/java/com/github/shadowsocks/utils/Constants.kt
core/src/main/java/com/github/shadowsocks/utils/Constants.kt
+1
-0
No files found.
core/src/main/java/com/github/shadowsocks/bg/ServiceNotification.kt
View file @
940e4e30
...
...
@@ -84,8 +84,8 @@ class ServiceNotification(private val service: BaseService.Interface, profileNam
service
as
Context
val
closeAction
=
NotificationCompat
.
Action
.
Builder
(
R
.
drawable
.
ic_navigation_close
,
service
.
get
String
(
R
.
string
.
stop
),
PendingIntent
.
getBroadcast
(
service
,
0
,
Intent
(
Action
.
CLOSE
),
0
)).
apply
{
service
.
get
Text
(
R
.
string
.
stop
),
PendingIntent
.
getBroadcast
(
service
,
0
,
Intent
(
Action
.
CLOSE
)
.
setPackage
(
service
.
packageName
)
,
0
)).
apply
{
setShowsUserInterface
(
false
)
}.
build
()
if
(
Build
.
VERSION
.
SDK_INT
<
24
)
builder
.
addAction
(
closeAction
)
else
builder
.
addInvisibleAction
(
closeAction
)
...
...
core/src/main/java/com/github/shadowsocks/subscription/SubscriptionService.kt
View file @
940e4e30
...
...
@@ -22,8 +22,12 @@ package com.github.shadowsocks.subscription
import
android.app.NotificationChannel
import
android.app.NotificationManager
import
android.app.PendingIntent
import
android.app.Service
import
android.content.BroadcastReceiver
import
android.content.Context
import
android.content.Intent
import
android.content.IntentFilter
import
android.os.IBinder
import
android.util.Log
import
android.widget.Toast
...
...
@@ -58,16 +62,33 @@ class SubscriptionService : Service() {
app
.
getText
(
R
.
string
.
service_subscription
),
NotificationManager
.
IMPORTANCE_LOW
)
}
private
object
CancelReceiver
:
BroadcastReceiver
()
{
override
fun
onReceive
(
context
:
Context
?,
intent
:
Intent
?)
{
worker
?.
cancel
()
}
}
private
var
counter
=
0
private
var
receiverRegistered
=
false
override
fun
onBind
(
intent
:
Intent
?):
IBinder
?
=
null
override
fun
onStartCommand
(
intent
:
Intent
?,
flags
:
Int
,
startId
:
Int
):
Int
{
if
(
worker
==
null
)
{
idle
.
value
=
false
if
(!
receiverRegistered
)
registerReceiver
(
CancelReceiver
,
IntentFilter
(
Action
.
ABORT
))
worker
=
GlobalScope
.
launch
{
val
urls
=
Subscription
.
instance
.
urls
val
notification
=
NotificationCompat
.
Builder
(
this
@SubscriptionService
,
NOTIFICATION_CHANNEL
).
apply
{
color
=
ContextCompat
.
getColor
(
this
@SubscriptionService
,
R
.
color
.
material_primary_500
)
priority
=
NotificationCompat
.
PRIORITY_LOW
addAction
(
NotificationCompat
.
Action
.
Builder
(
R
.
drawable
.
ic_navigation_close
,
getText
(
R
.
string
.
stop
),
PendingIntent
.
getBroadcast
(
this
@SubscriptionService
,
0
,
Intent
(
Action
.
ABORT
).
setPackage
(
packageName
),
0
)).
apply
{
setShowsUserInterface
(
false
)
}.
build
())
setCategory
(
NotificationCompat
.
CATEGORY_PROGRESS
)
setContentTitle
(
getString
(
R
.
string
.
service_subscription_working
))
setOngoing
(
true
)
...
...
@@ -78,12 +99,13 @@ class SubscriptionService : Service() {
Core
.
notification
.
notify
(
NOTIFICATION_ID
,
notification
.
build
())
counter
=
0
val
workers
=
urls
.
asIterable
().
map
{
url
->
async
(
Dispatchers
.
IO
)
{
work
(
url
,
urls
.
size
(),
notification
)
}
async
(
Dispatchers
.
IO
)
{
fetchJson
(
url
,
urls
.
size
(),
notification
)
}
}
try
{
val
localJsons
=
workers
.
awaitAll
()
withContext
(
Dispatchers
.
Main
)
{
Core
.
notification
.
notify
(
NOTIFICATION_ID
,
notification
.
apply
{
setContentTitle
(
getText
(
R
.
string
.
service_subscription_finishing
))
setProgress
(
0
,
0
,
true
)
}.
build
())
createProfilesFromSubscription
(
localJsons
.
asSequence
().
filterNotNull
().
map
{
it
.
inputStream
()
})
...
...
@@ -108,8 +130,7 @@ class SubscriptionService : Service() {
return
START_NOT_STICKY
}
private
var
counter
=
0
private
suspend
fun
work
(
url
:
URL
,
max
:
Int
,
notification
:
NotificationCompat
.
Builder
):
File
?
{
private
suspend
fun
fetchJson
(
url
:
URL
,
max
:
Int
,
notification
:
NotificationCompat
.
Builder
):
File
?
{
val
tempFile
=
File
.
createTempFile
(
"subscription-"
,
".json"
,
cacheDir
)
try
{
(
url
.
openConnection
()
as
HttpURLConnection
).
useCancellable
{
...
...
@@ -185,6 +206,7 @@ class SubscriptionService : Service() {
override
fun
onDestroy
()
{
worker
?.
cancel
()
if
(
receiverRegistered
)
unregisterReceiver
(
CancelReceiver
)
super
.
onDestroy
()
}
}
core/src/main/java/com/github/shadowsocks/utils/Constants.kt
View file @
940e4e30
...
...
@@ -79,6 +79,7 @@ object Action {
const
val
SERVICE
=
"com.github.shadowsocks.SERVICE"
const
val
CLOSE
=
"com.github.shadowsocks.CLOSE"
const
val
RELOAD
=
"com.github.shadowsocks.RELOAD"
const
val
ABORT
=
"com.github.shadowsocks.ABORT"
const
val
EXTRA_PROFILE_ID
=
"com.github.shadowsocks.EXTRA_PROFILE_ID"
}
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