Skip to content

Commit d3059ba

Browse files
committed
Fixed #14
1 parent cfe4f70 commit d3059ba

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
public class BinService extends Service implements StreamListener.Listener {
4343
public static final int START = 0;
4444
public static final int STOP = 1;
45+
public static final int STATUS = 2;
4546
public static final int NOTIFICATION_ID = 1;
4647
public static final String ACTION_START_SERVICE = "com.gianlu.aria2android.START_SERVICE";
4748
public static final String ACTION_STOP_SERVICE = "com.gianlu.aria2android.STOP_SERVICE";
@@ -69,6 +70,12 @@ public IBinder onBind(Intent intent) {
6970
return messenger.getBinder();
7071
}
7172

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+
7279
@TargetApi(Build.VERSION_CODES.O)
7380
private void createChannel() {
7481
NotificationChannel chan = new NotificationChannel(CHANNEL_ID, SERVICE_NAME, NotificationManager.IMPORTANCE_DEFAULT);
@@ -118,7 +125,7 @@ private void startBin(@NonNull StartConfig config) {
118125
dispatchBroadcast(Action.SERVER_START, null, null);
119126
}
120127

121-
private void ex(Exception ex) {
128+
private void ex(@NonNull Exception ex) {
122129
Logging.log(ex);
123130
dispatchBroadcast(Action.SERVER_EX, null, ex);
124131
}
@@ -200,6 +207,7 @@ public void unknownLogLine(@NonNull String line) {
200207
}
201208

202209
public enum Action {
210+
SERVER_STATUS,
203211
SERVER_START,
204212
SERVER_MSG,
205213
SERVER_EX,
@@ -236,6 +244,9 @@ public void handleMessage(Message msg) {
236244
case STOP:
237245
stopBin();
238246
break;
247+
case STATUS:
248+
dispatchStatus();
249+
break;
239250
default:
240251
super.handleMessage(msg);
241252
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class MainActivity extends ActivityWithDialog {
7373
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
7474
serviceMessenger = new Messenger(iBinder);
7575
checkIntentAction(getIntent());
76+
askForStatus();
7677
}
7778

7879
@Override
@@ -91,6 +92,23 @@ private static boolean isARM() {
9192
return false;
9293
}
9394

95+
@Override
96+
protected void onResume() {
97+
super.onResume();
98+
99+
askForStatus();
100+
}
101+
102+
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);
109+
}
110+
}
111+
94112
@Override
95113
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
96114
if (requestCode == STORAGE_ACCESS_CODE) {
@@ -558,6 +576,9 @@ public void onReceive(Context context, final Intent intent) {
558576
if (action != null && intent != null) {
559577
runOnUiThread(() -> {
560578
switch (action) {
579+
case SERVER_STATUS:
580+
updateUiStatus(intent.getBooleanExtra("on", false));
581+
break;
561582
case SERVER_START:
562583
addLog(new Logging.LogLine(Logging.LogLine.Type.INFO, getString(R.string.serverStarted)));
563584
break;

0 commit comments

Comments
 (0)