Skip to content

Commit f7dde3d

Browse files
committed
Move status control logic to separate class, route control events through eventbus.
1 parent 37a8d54 commit f7dde3d

File tree

7 files changed

+185
-67
lines changed

7 files changed

+185
-67
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.leyfer.thesis.touchlogger_dirty.event;
2+
3+
public class PauseEvent extends Event {
4+
private final boolean isPaused;
5+
6+
public PauseEvent(boolean isPaused) {
7+
this.isPaused = isPaused;
8+
}
9+
10+
public boolean isPaused() {
11+
return isPaused;
12+
}
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.leyfer.thesis.touchlogger_dirty.event;
2+
3+
public class StatusEvent extends Event {
4+
5+
public Status getStatus() {
6+
return status;
7+
}
8+
9+
public enum Status {
10+
STATUS_ONLINE,
11+
STATUS_OFFLINE
12+
}
13+
14+
private final Status status;
15+
private final String statusString;
16+
17+
public StatusEvent(Status status, String statusString) {
18+
this.status = status;
19+
this.statusString = statusString;
20+
}
21+
22+
public String getStatusString() {
23+
return statusString;
24+
}
25+
}

app/src/main/java/org/leyfer/thesis/touchlogger_dirty/notification/ControlNotification.java

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
import android.app.NotificationChannel;
55
import android.app.NotificationManager;
66
import android.app.PendingIntent;
7-
import android.content.BroadcastReceiver;
87
import android.content.Context;
9-
import android.content.Intent;
10-
import android.content.IntentFilter;
118
import android.os.Build;
129
import android.support.annotation.RequiresApi;
1310
import android.support.v4.app.NotificationCompat;
14-
import android.util.Log;
1511

12+
import org.greenrobot.eventbus.Subscribe;
13+
import org.greenrobot.eventbus.ThreadMode;
1614
import org.leyfer.thesis.touchlogger_dirty.R;
17-
import org.leyfer.thesis.touchlogger_dirty.activity.MainActivity;
15+
import org.leyfer.thesis.touchlogger_dirty.event.PauseEvent;
16+
import org.leyfer.thesis.touchlogger_dirty.event.StatusEvent;
17+
18+
import static org.leyfer.thesis.touchlogger_dirty.status_control.StatusController.ControlActionReceiver.getPauseIntent;
19+
import static org.leyfer.thesis.touchlogger_dirty.status_control.StatusController.ControlActionReceiver.getResumeIntent;
1820

1921
public class ControlNotification {
2022
public static final int GESTURE_CONTROLLER_NOTIFICATION_ID = 0x7357;
2123

22-
private static final String STATUS_ONLINE = "online";
23-
private static final String STATUS_OFFLINE = "offline";
2424
private static final String CHANNEL_ID = "ControlNotification_channel_1";
2525

2626
private final Context context;
@@ -50,7 +50,7 @@ public ControlNotification(Context context) {
5050
}
5151

5252
builder = new NotificationCompat.Builder(context, CHANNEL_ID);
53-
setOfflineUi();
53+
setStatus(context.getString(R.string.offline));
5454
onResumedUi();
5555
updateNotification();
5656
}
@@ -71,38 +71,21 @@ private void updateNotification() {
7171
notificationManager.notify(GESTURE_CONTROLLER_NOTIFICATION_ID, getNotification());
7272
}
7373

74-
private static Intent getPauseIntent() {
75-
return new Intent(NotificationActionReceiver.ACTION_PAUSE);
76-
}
77-
78-
private static Intent getResumeIntent() {
79-
return new Intent(NotificationActionReceiver.ACTION_RESUME);
80-
}
81-
8274
public Notification getNotification() {
8375
return builder.build();
8476
}
8577

86-
private void setStatus(String status) {
78+
public void setStatus(String status) {
8779
builder.setContentTitle(context.getString(R.string.payload_status, status));
8880
}
8981

90-
private void setOnlineUi() {
91-
setStatus(STATUS_ONLINE);
92-
}
93-
94-
public void setOnline() {
95-
setOnlineUi();
82+
public void setOnline(String statusString) {
83+
setStatus(statusString);
9684
updateNotification();
9785
}
9886

99-
private void setOfflineUi() {
100-
setStatus(STATUS_OFFLINE);
101-
102-
}
103-
104-
public void setOffline() {
105-
setOfflineUi();
87+
public void setOffline(String statusString) {
88+
setStatus(statusString);
10689
updateNotification();
10790
}
10891

@@ -128,37 +111,21 @@ public void onResumed() {
128111
updateNotification();
129112
}
130113

131-
public abstract static class NotificationActionReceiver extends BroadcastReceiver {
132-
private static final String ACTION_PAUSE = "notification_action_receiver_pause";
133-
private static final String ACTION_RESUME = "notification_action_receiver_resume";
134-
135-
@Override
136-
public void onReceive(Context context, Intent intent) {
137-
if (intent.getAction() != null) {
138-
if (intent.getAction().equals(ACTION_PAUSE)) {
139-
Log.d(MainActivity.TAG, "Pausing gesture service");
140-
onPause();
141-
} else if (intent.getAction().equals(ACTION_RESUME)) {
142-
Log.d(MainActivity.TAG, "Resuming gesture service");
143-
onResume();
144-
} else {
145-
Log.e(MainActivity.TAG, String.format("Invalid action string: %s!",
146-
intent.getAction()));
147-
}
148-
} else {
149-
Log.e(MainActivity.TAG, "No action string!");
150-
}
114+
@Subscribe(threadMode = ThreadMode.MAIN)
115+
public void onStatusEvent(StatusEvent statusEvent) {
116+
if (statusEvent.getStatus() == StatusEvent.Status.STATUS_ONLINE) {
117+
setOnline(statusEvent.getStatusString());
118+
} else if (statusEvent.getStatus() == StatusEvent.Status.STATUS_OFFLINE) {
119+
setOffline(statusEvent.getStatusString());
151120
}
121+
}
152122

153-
public IntentFilter getIntentFilter() {
154-
IntentFilter intentFilter = new IntentFilter();
155-
intentFilter.addAction(ACTION_PAUSE);
156-
intentFilter.addAction(ACTION_RESUME);
157-
return intentFilter;
123+
@Subscribe(threadMode = ThreadMode.MAIN)
124+
public void onPausedEvent(PauseEvent event) {
125+
if (event.isPaused()) {
126+
onPaused();
127+
} else {
128+
onResumed();
158129
}
159-
160-
public abstract void onPause();
161-
162-
public abstract void onResume();
163130
}
164131
}

app/src/main/java/org/leyfer/thesis/touchlogger_dirty/service/GestureBuilderService.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
import org.greenrobot.eventbus.EventBus;
1010
import org.leyfer.thesis.touchlogger_dirty.InputDataReader;
11+
import org.leyfer.thesis.touchlogger_dirty.R;
1112
import org.leyfer.thesis.touchlogger_dirty.activity.MainActivity;
1213
import org.leyfer.thesis.touchlogger_dirty.event.NewMatchingFileEvent;
1314
import org.leyfer.thesis.touchlogger_dirty.notification.ControlNotification;
15+
import org.leyfer.thesis.touchlogger_dirty.status_control.StatusController;
1416
import org.leyfer.thesis.touchlogger_dirty.utils.Config;
1517
import org.leyfer.thesis.touchlogger_dirty.utils.HeartBeatSender;
1618
import org.leyfer.thesis.touchlogger_dirty.utils.writer.Command;
@@ -27,9 +29,10 @@ public class GestureBuilderService extends IntentService {
2729

2830
private InputDataReader inputDataReader;
2931
private ControlNotification controlNotification;
32+
private StatusController statusController;
3033
private ControlWriter controlWriter;
3134
private Handler commandHandler;
32-
private ControlNotification.NotificationActionReceiver notificationReceiver;
35+
private StatusController.ControlActionReceiver notificationReceiver;
3336

3437
public GestureBuilderService() {
3538
super("GestureBuilderService");
@@ -62,13 +65,18 @@ Config.TOUCH_DATA_FILE_BASE_NAME, INPUT_DATA_DIR, getApplicationContext()
6265
new HeartBeatSender(
6366
Config.HEARTBEAT_INTERVAL_MS, new HeartbeatCommand(), controlWriter).start();
6467

65-
controlNotification = new ControlNotification(getApplicationContext());
68+
controlNotification = new ControlNotification(this);
69+
EventBus.getDefault().register(controlNotification);
6670
startForeground(GESTURE_CONTROLLER_NOTIFICATION_ID, controlNotification.getNotification());
6771

72+
statusController = new StatusController(getString(R.string.online),
73+
getString(R.string.offline));
74+
6875
commandHandler = new Handler(getMainLooper());
6976

7077
notificationReceiver = new NotificationReceiver(new PauseCommand(), new ResumeCommand());
71-
registerReceiver(notificationReceiver, notificationReceiver.getIntentFilter());
78+
registerReceiver(notificationReceiver,
79+
StatusController.ControlActionReceiver.getIntentFilter());
7280

7381
EventBus.getDefault().register(inputDataReader);
7482
Log.d(MainActivity.TAG, String.format("Registered! Overall: %b",
@@ -88,9 +96,10 @@ public void onDestroy() {
8896
EventBus.getDefault().hasSubscriberForEvent(NewMatchingFileEvent.class)));
8997
inputDataReader.stop();
9098
}
99+
EventBus.getDefault().unregister(controlNotification);
91100
}
92101

93-
private class NotificationReceiver extends ControlNotification.NotificationActionReceiver {
102+
private class NotificationReceiver extends StatusController.ControlActionReceiver {
94103
private final Command onPauseCommand;
95104
private final Command onResumeCommand;
96105

@@ -129,7 +138,7 @@ public void onSuccess() {
129138
commandHandler.post(new Runnable() {
130139
@Override
131140
public void run() {
132-
controlNotification.onResumed();
141+
statusController.onResumed();
133142
}
134143
});
135144
} else {
@@ -155,7 +164,7 @@ public void onSuccess() {
155164
commandHandler.post(new Runnable() {
156165
@Override
157166
public void run() {
158-
controlNotification.onPaused();
167+
statusController.onPaused();
159168
}
160169
});
161170
} else {
@@ -185,7 +194,7 @@ public void onSuccess() {
185194
commandHandler.post(new Runnable() {
186195
@Override
187196
public void run() {
188-
controlNotification.setOnline();
197+
statusController.setOnline();
189198
}
190199
});
191200
} else {
@@ -205,7 +214,7 @@ public void onFailure() {
205214
commandHandler.post(new Runnable() {
206215
@Override
207216
public void run() {
208-
controlNotification.setOffline();
217+
statusController.setOffline();
209218
}
210219
});
211220
} else {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.leyfer.thesis.touchlogger_dirty.status_control;
2+
3+
public interface Controllable {
4+
void setOnline();
5+
6+
void setOffline();
7+
8+
void onPaused();
9+
10+
void onResumed();
11+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.leyfer.thesis.touchlogger_dirty.status_control;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.IntentFilter;
7+
import android.util.Log;
8+
9+
import org.greenrobot.eventbus.EventBus;
10+
import org.leyfer.thesis.touchlogger_dirty.activity.MainActivity;
11+
import org.leyfer.thesis.touchlogger_dirty.event.PauseEvent;
12+
import org.leyfer.thesis.touchlogger_dirty.event.StatusEvent;
13+
14+
public class StatusController implements Controllable {
15+
16+
private final String statusOnlineString;
17+
private final String statusOfflineString;
18+
private final EventBus eventBus;
19+
20+
public StatusController(String statusOnlineString, String statusOfflineString) {
21+
this.statusOnlineString = statusOnlineString;
22+
this.statusOfflineString = statusOfflineString;
23+
eventBus = EventBus.getDefault();
24+
}
25+
26+
@Override
27+
public void setOnline() {
28+
eventBus.post(new StatusEvent(StatusEvent.Status.STATUS_ONLINE, statusOnlineString));
29+
}
30+
31+
@Override
32+
public void setOffline() {
33+
eventBus.post(new StatusEvent(StatusEvent.Status.STATUS_OFFLINE, statusOfflineString));
34+
}
35+
36+
@Override
37+
public void onPaused() {
38+
eventBus.post(new PauseEvent(true));
39+
}
40+
41+
@Override
42+
public void onResumed() {
43+
eventBus.post(new PauseEvent(false));
44+
}
45+
46+
public abstract static class ControlActionReceiver extends BroadcastReceiver {
47+
private static final String ACTION_PAUSE = "notification_action_receiver_pause";
48+
private static final String ACTION_RESUME = "notification_action_receiver_resume";
49+
50+
@Override
51+
public void onReceive(Context context, Intent intent) {
52+
if (intent.getAction() != null) {
53+
switch (intent.getAction()) {
54+
case ACTION_PAUSE:
55+
Log.d(MainActivity.TAG, "Pausing gesture service");
56+
onPause();
57+
break;
58+
case ACTION_RESUME:
59+
Log.d(MainActivity.TAG, "Resuming gesture service");
60+
onResume();
61+
break;
62+
default:
63+
Log.e(MainActivity.TAG, String.format("Invalid action string: %s!",
64+
intent.getAction()));
65+
break;
66+
}
67+
} else {
68+
Log.e(MainActivity.TAG, "No action string!");
69+
}
70+
}
71+
72+
public static IntentFilter getIntentFilter() {
73+
IntentFilter intentFilter = new IntentFilter();
74+
intentFilter.addAction(ACTION_PAUSE);
75+
intentFilter.addAction(ACTION_RESUME);
76+
return intentFilter;
77+
}
78+
79+
public static Intent getPauseIntent() {
80+
return new Intent(ControlActionReceiver.ACTION_PAUSE);
81+
}
82+
83+
public static Intent getResumeIntent() {
84+
return new Intent(ControlActionReceiver.ACTION_RESUME);
85+
}
86+
87+
public abstract void onPause();
88+
89+
public abstract void onResume();
90+
}
91+
}

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@
3434
<string name="manual_7">./exec_payload</string>
3535
<string name="version">Version: %1$s</string>
3636
<string name="stack_trace_copied_to_clipboard">Stack trace copied to clipboard</string>
37+
<string name="online">online</string>
38+
<string name="offline">offline</string>
3739
</resources>

0 commit comments

Comments
 (0)