Skip to content

Commit 69faf34

Browse files
committed
Process is on when leaving Activity
1 parent d3059ba commit 69faf34

3 files changed

Lines changed: 94 additions & 102 deletions

File tree

app/src/main/java/com/gianlu/aria2android/Aria2/BinService.java

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.gianlu.aria2android.Aria2;
22

3-
import android.annotation.SuppressLint;
43
import android.annotation.TargetApi;
54
import android.app.Notification;
65
import android.app.NotificationChannel;
@@ -40,8 +39,6 @@
4039
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
4140

4241
public class BinService extends Service implements StreamListener.Listener {
43-
public static final int START = 0;
44-
public static final int STOP = 1;
4542
public static final int STATUS = 2;
4643
public static final int NOTIFICATION_ID = 1;
4744
public static final String ACTION_START_SERVICE = "com.gianlu.aria2android.START_SERVICE";
@@ -56,34 +53,6 @@ public class BinService extends Service implements StreamListener.Listener {
5653
private PerformanceMonitor performanceMonitor;
5754
private ShortcutManager shortcutManager;
5855

59-
@Nullable
60-
@Override
61-
public IBinder onBind(Intent intent) {
62-
if (messenger == null) {
63-
serviceThread.start();
64-
broadcastManager = LocalBroadcastManager.getInstance(this);
65-
messenger = new Messenger(new LocalHandler());
66-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1)
67-
shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
68-
}
69-
70-
return messenger.getBinder();
71-
}
72-
73-
private void dispatchStatus() {
74-
Intent intent = new Intent(Action.SERVER_STATUS.toString());
75-
intent.putExtra("on", process != null);
76-
broadcastManager.sendBroadcast(intent);
77-
}
78-
79-
@TargetApi(Build.VERSION_CODES.O)
80-
private void createChannel() {
81-
NotificationChannel chan = new NotificationChannel(CHANNEL_ID, SERVICE_NAME, NotificationManager.IMPORTANCE_DEFAULT);
82-
chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
83-
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
84-
if (service != null) service.createNotificationChannel(chan);
85-
}
86-
8756
private void startBin(@NonNull StartConfig config) {
8857
String cmd = BinUtils.createCommandLine(this, config);
8958
try {
@@ -125,6 +94,47 @@ private void startBin(@NonNull StartConfig config) {
12594
dispatchBroadcast(Action.SERVER_START, null, null);
12695
}
12796

97+
@Override
98+
public int onStartCommand(Intent intent, int flags, int startId) {
99+
if (Objects.equals(intent.getAction(), ACTION_START_SERVICE)) {
100+
startBin((StartConfig) intent.getSerializableExtra("config"));
101+
return super.onStartCommand(intent, flags, startId);
102+
} else if (Objects.equals(intent.getAction(), ACTION_STOP_SERVICE)) {
103+
stopBin();
104+
}
105+
106+
stopSelf();
107+
return START_NOT_STICKY;
108+
}
109+
110+
@Nullable
111+
@Override
112+
public IBinder onBind(Intent intent) {
113+
if (messenger == null) {
114+
serviceThread.start();
115+
broadcastManager = LocalBroadcastManager.getInstance(this);
116+
messenger = new Messenger(new LocalHandler(this));
117+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1)
118+
shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
119+
}
120+
121+
return messenger.getBinder();
122+
}
123+
124+
public void dispatchStatus() {
125+
Intent intent = new Intent(Action.SERVER_STATUS.toString());
126+
intent.putExtra("on", process != null);
127+
broadcastManager.sendBroadcast(intent);
128+
}
129+
130+
@TargetApi(Build.VERSION_CODES.O)
131+
private void createChannel() {
132+
NotificationChannel chan = new NotificationChannel(CHANNEL_ID, SERVICE_NAME, NotificationManager.IMPORTANCE_DEFAULT);
133+
chan.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
134+
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
135+
if (service != null) service.createNotificationChannel(chan);
136+
}
137+
128138
private void ex(@NonNull Exception ex) {
129139
Logging.log(ex);
130140
dispatchBroadcast(Action.SERVER_EX, null, ex);
@@ -228,24 +238,19 @@ public String toString() {
228238
}
229239
}
230240

231-
@SuppressLint("HandlerLeak")
232-
private class LocalHandler extends Handler {
241+
private static class LocalHandler extends Handler {
242+
private final BinService service;
233243

234-
LocalHandler() {
235-
super(serviceThread.getLooper());
244+
LocalHandler(@NonNull BinService service) {
245+
super(service.serviceThread.getLooper());
246+
this.service = service;
236247
}
237248

238249
@Override
239250
public void handleMessage(Message msg) {
240251
switch (msg.what) {
241-
case START:
242-
startBin((StartConfig) msg.obj);
243-
break;
244-
case STOP:
245-
stopBin();
246-
break;
247252
case STATUS:
248-
dispatchStatus();
253+
service.dispatchStatus();
249254
break;
250255
default:
251256
super.handleMessage(msg);
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package com.gianlu.aria2android;
22

33
import android.content.BroadcastReceiver;
4-
import android.content.ComponentName;
54
import android.content.Context;
65
import android.content.Intent;
7-
import android.content.ServiceConnection;
8-
import android.os.IBinder;
9-
import android.os.Message;
10-
import android.os.Messenger;
11-
import android.os.RemoteException;
126

137
import com.gianlu.aria2android.Aria2.BinService;
148
import com.gianlu.aria2android.Aria2.StartConfig;
@@ -24,21 +18,14 @@ public void onReceive(final Context context, Intent intent) {
2418
return;
2519

2620
if (Prefs.getBoolean(PK.START_AT_BOOT)) {
27-
context.getApplicationContext().bindService(new Intent(context, BinService.class), new ServiceConnection() {
28-
@Override
29-
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
30-
Messenger messenger = new Messenger(iBinder);
31-
try {
32-
messenger.send(Message.obtain(null, BinService.START, StartConfig.fromPrefs()));
33-
} catch (RemoteException | JSONException ex) {
34-
Logging.log(ex);
35-
}
36-
}
37-
38-
@Override
39-
public void onServiceDisconnected(ComponentName componentName) {
40-
}
41-
}, Context.BIND_AUTO_CREATE);
21+
try {
22+
context.getApplicationContext()
23+
.startActivity(new Intent(context, BinService.class)
24+
.setAction(BinService.ACTION_START_SERVICE)
25+
.putExtra("config", StartConfig.fromPrefs()));
26+
} catch (JSONException ex) {
27+
Logging.log(ex);
28+
}
4229
}
4330
}
4431
}

app/src/main/java/com/gianlu/aria2android/MainActivity.java

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,13 @@
6060
public class MainActivity extends ActivityWithDialog {
6161
private static final int STORAGE_ACCESS_CODE = 1;
6262
private static final String ACTION_START_STOP_RECEIVED = "com.gianlu.aria2android.START_STOP_RECEIVED";
63-
private boolean isRunning;
63+
private volatile boolean isRunning;
6464
private ServiceBroadcastReceiver receiver;
6565
private Messenger serviceMessenger;
66-
private ToggleButton toggleServer;
67-
private MaterialEditTextPreference outputPath;
68-
private MaterialPreferenceCategory generalCategory;
69-
private MaterialPreferenceCategory rpcCategory;
70-
private MaterialPreferenceCategory notificationsCategory;
7166
private final ServiceConnection serviceConnection = new ServiceConnection() {
7267
@Override
7368
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
7469
serviceMessenger = new Messenger(iBinder);
75-
checkIntentAction(getIntent());
7670
askForStatus();
7771
}
7872

@@ -81,6 +75,11 @@ public void onServiceDisconnected(ComponentName componentName) {
8175
serviceMessenger = null;
8276
}
8377
};
78+
private ToggleButton toggleServer;
79+
private MaterialEditTextPreference outputPath;
80+
private MaterialPreferenceCategory generalCategory;
81+
private MaterialPreferenceCategory rpcCategory;
82+
private MaterialPreferenceCategory notificationsCategory;
8483
private LinearLayout logsContainer;
8584
private MessageView logsMessage;
8685

@@ -95,17 +94,16 @@ private static boolean isARM() {
9594
@Override
9695
protected void onResume() {
9796
super.onResume();
98-
9997
askForStatus();
10098
}
10199

102100
private void askForStatus() {
103-
if (serviceMessenger == null) return;
104-
105-
try {
106-
serviceMessenger.send(Message.obtain(null, BinService.STATUS, null));
107-
} catch (RemoteException ex) {
108-
Logging.log(ex);
101+
if (serviceMessenger != null) {
102+
try {
103+
serviceMessenger.send(Message.obtain(null, BinService.STATUS, null));
104+
} catch (RemoteException ex) {
105+
Logging.log(ex);
106+
}
109107
}
110108
}
111109

@@ -333,6 +331,7 @@ protected void onCreate(Bundle savedInstanceState) {
333331
version.setText(BinUtils.binVersion(this));
334332

335333
backwardCompatibility();
334+
checkIntentAction(getIntent());
336335
}
337336

338337
@Override
@@ -374,17 +373,6 @@ private void backwardCompatibility() {
374373
}
375374
}
376375

377-
@Override
378-
protected void onDestroy() {
379-
try {
380-
unbindService(serviceConnection);
381-
} catch (IllegalArgumentException ex) {
382-
Logging.log(ex);
383-
}
384-
385-
super.onDestroy();
386-
}
387-
388376
private boolean startService() {
389377
Prefs.putLong(PK.CURRENT_SESSION_START, System.currentTimeMillis());
390378
AnalyticsApplication.sendAnalytics(Utils.ACTION_TURN_ON);
@@ -430,22 +418,20 @@ public void askRationale(@NonNull AlertDialog.Builder builder) {
430418
receiver = new ServiceBroadcastReceiver();
431419
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
432420
try {
433-
if (serviceMessenger != null) {
434-
serviceMessenger.send(Message.obtain(null, BinService.START, StartConfig.fromPrefs()));
435-
return true;
436-
} else {
437-
bindService(new Intent(this, BinService.class), serviceConnection, BIND_AUTO_CREATE);
438-
return false;
439-
}
421+
bindService(new Intent(this, BinService.class), serviceConnection, BIND_AUTO_CREATE);
422+
startService(new Intent(this, BinService.class)
423+
.setAction(BinService.ACTION_START_SERVICE)
424+
.putExtra("config", StartConfig.fromPrefs()));
425+
return true;
440426
} catch (JSONException ex) {
441427
Toaster.with(this).message(R.string.failedLoadingOptions).ex(ex).show();
442428
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
443429
return false;
444-
} catch (RemoteException ex) {
430+
} /*catch (RemoteException ex) {
445431
Toaster.with(this).message(R.string.failedStarting).ex(ex).show();
446432
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
447433
return false;
448-
}
434+
} */
449435
}
450436

451437
@Override
@@ -454,16 +440,29 @@ protected void onStart() {
454440
bindService(new Intent(this, BinService.class), serviceConnection, BIND_AUTO_CREATE);
455441
}
456442

443+
@Override
444+
protected void onDestroy() {
445+
super.onDestroy();
446+
447+
try {
448+
unbindService(serviceConnection);
449+
} catch (IllegalArgumentException ex) {
450+
Logging.log(ex);
451+
}
452+
}
453+
457454
private boolean stopService() {
458455
if (serviceMessenger == null) return true;
459456

460457
try {
461-
serviceMessenger.send(Message.obtain(null, BinService.STOP, null));
462-
} catch (RemoteException ex) {
463-
Toaster.with(this).message(R.string.failedStopping).ex(ex).show();
464-
return false;
458+
unbindService(serviceConnection);
459+
} catch (IllegalArgumentException ex) {
460+
Logging.log(ex);
465461
}
466462

463+
startService(new Intent(this, BinService.class)
464+
.setAction(BinService.ACTION_STOP_SERVICE));
465+
467466
Bundle bundle = null;
468467
if (Prefs.getLong(PK.CURRENT_SESSION_START, -1) != -1) {
469468
bundle = new Bundle();
@@ -572,11 +571,12 @@ private class ServiceBroadcastReceiver extends BroadcastReceiver {
572571

573572
@Override
574573
public void onReceive(Context context, final Intent intent) {
575-
final BinService.Action action = BinService.Action.find(intent);
574+
BinService.Action action = BinService.Action.find(intent);
576575
if (action != null && intent != null) {
577576
runOnUiThread(() -> {
578577
switch (action) {
579578
case SERVER_STATUS:
579+
System.out.println("UPDATE!!");
580580
updateUiStatus(intent.getBooleanExtra("on", false));
581581
break;
582582
case SERVER_START:

0 commit comments

Comments
 (0)