Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Open sidebar
Wallet
sentinel-android
Commits
617d0db1
Verified
Commit
617d0db1
authored
Sep 02, 2020
by
Sarath
⚔
Browse files
Migrate websocket service to JobService
parent
bda84a34
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
176 additions
and
152 deletions
+176
-152
app/src/main/AndroidManifest.xml
app/src/main/AndroidManifest.xml
+4
-1
app/src/main/java/com/samourai/sentinel/BalanceActivity.java
app/src/main/java/com/samourai/sentinel/BalanceActivity.java
+99
-109
app/src/main/java/com/samourai/sentinel/MainActivity2.java
app/src/main/java/com/samourai/sentinel/MainActivity2.java
+1
-1
app/src/main/java/com/samourai/sentinel/ReceiveActivity.java
app/src/main/java/com/samourai/sentinel/ReceiveActivity.java
+3
-2
app/src/main/java/com/samourai/sentinel/network/dojo/Network.java
...main/java/com/samourai/sentinel/network/dojo/Network.java
+4
-4
app/src/main/java/com/samourai/sentinel/service/WebSocketService.java
.../java/com/samourai/sentinel/service/WebSocketService.java
+65
-35
No files found.
app/src/main/AndroidManifest.xml
View file @
617d0db1
...
...
@@ -7,6 +7,7 @@
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.CAMERA"
/>
<uses-permission
android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"
/>
<uses-permission
android:name=
"android.permission.VIBRATE"
/>
<uses-permission
android:name=
"android.permission.FOREGROUND_SERVICE"
/>
...
...
@@ -128,7 +129,9 @@
android:configChanges=
"keyboardHidden|orientation|screenSize"
/>
<service
android:enabled=
"true"
android:name=
"com.samourai.sentinel.service.WebSocketService"
/>
<service
android:enabled=
"true"
android:permission=
"android.permission.BIND_JOB_SERVICE"
android:name=
"com.samourai.sentinel.service.WebSocketService"
/>
<provider
android:name=
"com.samourai.sentinel.util.GenericFileProvider"
...
...
app/src/main/java/com/samourai/sentinel/BalanceActivity.java
View file @
617d0db1
This diff is collapsed.
Click to expand it.
app/src/main/java/com/samourai/sentinel/MainActivity2.java
View file @
617d0db1
...
...
@@ -66,7 +66,7 @@ public class MainActivity2 extends Activity {
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
if
(
AppUtil
.
getInstance
(
MainActivity2
.
this
.
getApplicationContext
()).
isServiceRunning
(
WebSocketService
.
class
))
{
stopService
(
new
Intent
(
MainActivity2
.
this
.
getApplicationContext
(),
WebSocketService
.
class
));
WebSocketService
.
startJob
(
getApplicationContext
(
));
}
}
...
...
app/src/main/java/com/samourai/sentinel/ReceiveActivity.java
View file @
617d0db1
...
...
@@ -441,9 +441,10 @@ public class ReceiveActivity extends Activity {
@Override
public
void
run
()
{
if
(
AppUtil
.
getInstance
(
ReceiveActivity
.
this
.
getApplicationContext
()).
isServiceRunning
(
WebSocketService
.
class
))
{
ReceiveActivity
.
this
.
stopService
(
new
Intent
(
ReceiveActivity
.
this
.
getApplicationContext
(),
WebSocketService
.
class
));
WebSocketService
.
stopJobs
(
getApplicationContext
(
));
}
ReceiveActivity
.
this
.
startService
(
new
Intent
(
ReceiveActivity
.
this
.
getApplicationContext
(),
WebSocketService
.
class
));
WebSocketService
.
startJob
(
getApplicationContext
());
}
}).
start
();
...
...
app/src/main/java/com/samourai/sentinel/network/dojo/Network.java
View file @
617d0db1
...
...
@@ -2,9 +2,11 @@ package com.samourai.sentinel.network.dojo;
import
android.content.Intent
;
import
android.os.Bundle
;
import
androidx.core.content.ContextCompat
;
import
androidx.appcompat.app.AlertDialog
;
import
androidx.appcompat.app.AppCompatActivity
;
import
android.view.MenuItem
;
import
android.view.View
;
import
android.widget.Button
;
...
...
@@ -118,13 +120,11 @@ public class Network extends AppCompatActivity {
// Toast.makeText(this,R.string.cannot_disable_tor_dojo,Toast.LENGTH_LONG).show();
// return;
// }
WebSocketService
.
stopJobs
(
getApplicationContext
());
stopTor
();
PrefsUtil
.
getInstance
(
getApplicationContext
()).
setValue
(
PrefsUtil
.
ENABLE_TOR
,
false
);
}
else
{
if
(
AppUtil
.
getInstance
(
this
.
getApplicationContext
()).
isServiceRunning
(
WebSocketService
.
class
))
{
stopService
(
new
Intent
(
this
.
getApplicationContext
(),
WebSocketService
.
class
));
}
WebSocketService
.
startJob
(
getApplicationContext
());
startTor
();
PrefsUtil
.
getInstance
(
getApplicationContext
()).
setValue
(
PrefsUtil
.
ENABLE_TOR
,
true
);
}
...
...
app/src/main/java/com/samourai/sentinel/service/WebSocketService.java
View file @
617d0db1
package
com.samourai.sentinel.service
;
import
android.app.Service
;
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.content.Intent
;
import
android.os.Handler
;
import
android.os.IBinder
;
import
com.invertedx.torservice.TorProxyManager
;
import
com.samourai.sentinel.SamouraiSentinel
;
import
com.samourai.sentinel.util.ReceiveLookAtUtil
;
import
com.samourai.sentinel.network.dojo.DojoUtil
;
import
com.samourai.sentinel.tor.TorManager
;
import
java.util.List
;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
android.util.Log
;
public
class
WebSocketService
extends
Service
{
public
class
WebSocketService
extends
Job
Service
{
private
Context
context
=
null
;
...
...
@@ -31,47 +35,58 @@ public class WebSocketService extends Service {
super
.
onCreate
();
}
@Override
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
return
START_STICKY
;
}
@Override
public
boolean
onStartJob
(
JobParameters
params
)
{
//
context
=
this
.
getApplicationContext
();
if
(
TorManager
.
getInstance
(
context
).
isConnected
()){
return
false
;
}
List
<
String
>
addrSubs
=
SamouraiSentinel
.
getInstance
(
WebSocketService
.
this
).
getAllAddrsSorted
();
addrs
=
addrSubs
.
toArray
(
new
String
[
addrSubs
.
size
()]);
if
(
addrs
.
length
==
0
)
{
return
;
if
(
addrs
.
length
==
0
)
{
}
else
{
webSocketHandler
=
new
WebSocketHandler
(
WebSocketService
.
this
,
addrs
);
connectToWebsocketIfNotConnected
();
timer
.
scheduleAtFixedRate
(
new
TimerTask
()
{
@Override
public
void
run
()
{
handler
.
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
connectToWebsocketIfNotConnected
();
}
});
}
},
5000
,
checkIfNotConnectedDelay
);
}
webSocketHandler
=
new
WebSocketHandler
(
WebSocketService
.
this
,
addrs
);
connectToWebsocketIfNotConnected
();
timer
.
scheduleAtFixedRate
(
new
TimerTask
()
{
@Override
public
void
run
()
{
handler
.
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
connectToWebsocketIfNotConnected
();
}
});
}
},
5000
,
checkIfNotConnectedDelay
);
}
@Override
public
int
onStartCommand
(
Intent
intent
,
int
flags
,
int
startId
)
{
return
START_STICKY
;
//Job is not completed so e
return
true
;
}
@Override
public
IBinder
onBind
(
Intent
intent
)
{
return
null
;
public
boolean
onStopJob
(
JobParameters
params
)
{
stop
();
return
true
;
}
public
void
connectToWebsocketIfNotConnected
()
{
public
void
connectToWebsocketIfNotConnected
()
{
try
{
if
(!
webSocketHandler
.
isConnected
())
{
if
(!
webSocketHandler
.
isConnected
())
{
webSocketHandler
.
start
();
}
}
catch
(
Exception
e
)
{
...
...
@@ -81,7 +96,7 @@ public class WebSocketService extends Service {
public
void
stop
()
{
try
{
if
(
webSocketHandler
!=
null
)
{
if
(
webSocketHandler
!=
null
)
{
webSocketHandler
.
stop
();
}
}
catch
(
Exception
e
)
{
...
...
@@ -90,10 +105,25 @@ public class WebSocketService extends Service {
}
@Override
public
void
onDestroy
()
{
public
void
onDestroy
()
{
stop
();
super
.
onDestroy
();
}
public
static
void
startJob
(
Context
mContext
)
{
JobScheduler
mJobScheduler
=
(
JobScheduler
)
mContext
.
getSystemService
(
Context
.
JOB_SCHEDULER_SERVICE
);
JobInfo
jobInfo
=
new
JobInfo
.
Builder
(
12
,
new
ComponentName
(
mContext
,
WebSocketService
.
class
))
.
setPersisted
(
true
)
.
setRequiredNetworkType
(
JobInfo
.
NETWORK_TYPE_ANY
)
.
build
();
if
(
mJobScheduler
!=
null
)
{
mJobScheduler
.
schedule
(
jobInfo
);
}
}
public
static
void
stopJobs
(
Context
mContext
)
{
JobScheduler
obScheduler
=
(
JobScheduler
)
mContext
.
getSystemService
(
Context
.
JOB_SCHEDULER_SERVICE
);
if
(
obScheduler
!=
null
)
{
obScheduler
.
cancelAll
();
}
}
}
\ No newline at end of file
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