Verified Commit 617d0db1 authored by Sarath's avatar Sarath
Browse files

Migrate websocket service to JobService

parent bda84a34
......@@ -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"
......
......@@ -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());
}
}
......
......@@ -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();
......
......@@ -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);
}
......
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 JobService {
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
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