Skip to content

Commit 8844862

Browse files
committed
Add control interface on main activity.
1 parent 314e088 commit 8844862

File tree

2 files changed

+114
-8
lines changed

2 files changed

+114
-8
lines changed

app/src/main/java/org/leyfer/thesis/touchlogger_dirty/activity/MainActivity.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.leyfer.thesis.touchlogger_dirty.activity;
22

33
import android.Manifest;
4+
import android.app.PendingIntent;
45
import android.content.ClipData;
56
import android.content.ClipboardManager;
67
import android.content.DialogInterface;
8+
import android.content.Intent;
79
import android.content.pm.PackageManager;
810
import android.os.Build;
911
import android.os.Bundle;
@@ -20,10 +22,16 @@
2022
import android.widget.TextView;
2123
import android.widget.Toast;
2224

25+
import org.greenrobot.eventbus.EventBus;
26+
import org.greenrobot.eventbus.Subscribe;
27+
import org.greenrobot.eventbus.ThreadMode;
2328
import org.leyfer.thesis.touchlogger_dirty.BuildConfig;
2429
import org.leyfer.thesis.touchlogger_dirty.R;
2530
import org.leyfer.thesis.touchlogger_dirty.dialog.ErrorAlertDialog;
31+
import org.leyfer.thesis.touchlogger_dirty.event.PauseEvent;
32+
import org.leyfer.thesis.touchlogger_dirty.event.StatusEvent;
2633
import org.leyfer.thesis.touchlogger_dirty.exception.ManualInstallationException;
34+
import org.leyfer.thesis.touchlogger_dirty.status_control.StatusController;
2735
import org.leyfer.thesis.touchlogger_dirty.utils.file.FileUtils;
2836

2937
import java.io.File;
@@ -44,6 +52,11 @@ public class MainActivity extends AppCompatActivity {
4452
private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
4553
private Handler mHandler;
4654

55+
private TextView statusTv;
56+
private Button togglePauseButton;
57+
58+
private StatusEvent.Status currentStatus = StatusEvent.Status.STATUS_OFFLINE;
59+
4760
@Override
4861
protected void onCreate(Bundle savedInstanceState) {
4962
super.onCreate(savedInstanceState);
@@ -59,6 +72,11 @@ protected void onCreate(Bundle savedInstanceState) {
5972
hello.setText(R.string.science);
6073
TextView version = findViewById(R.id.version);
6174
version.setText(getString(R.string.version, BuildConfig.VERSION_NAME));
75+
statusTv = findViewById(R.id.status_text);
76+
togglePauseButton = findViewById(R.id.toggle_pause);
77+
78+
setOffline(getString(R.string.offline));
79+
onResumed();
6280

6381
mHandler = new Handler(getMainLooper());
6482

@@ -275,4 +293,72 @@ public void onCancel(DialogInterface dialog) {
275293

276294
errorAlertDialog.show();
277295
}
296+
297+
@Override
298+
protected void onPause() {
299+
EventBus.getDefault().unregister(this);
300+
super.onPause();
301+
}
302+
303+
@Override
304+
protected void onResume() {
305+
super.onResume();
306+
EventBus.getDefault().register(this);
307+
}
308+
309+
private void onPaused() {
310+
togglePauseButton.setText(R.string.action_resume);
311+
togglePauseButton.setOnClickListener(new PausedCliciListener());
312+
}
313+
314+
private void onResumed() {
315+
togglePauseButton.setText(R.string.action_pause);
316+
togglePauseButton.setOnClickListener(new ResumedClickListener());
317+
}
318+
319+
private void setOnline(String status) {
320+
statusTv.setText(getString(R.string.payload_status, status));
321+
}
322+
323+
private void setOffline(String status) {
324+
statusTv.setText(getString(R.string.payload_status, status));
325+
}
326+
327+
@Subscribe(threadMode = ThreadMode.MAIN)
328+
public void onStatusEvent(StatusEvent statusEvent) {
329+
if (statusEvent.getStatus() == StatusEvent.Status.STATUS_ONLINE) {
330+
if (currentStatus != StatusEvent.Status.STATUS_ONLINE) {
331+
setOnline(statusEvent.getStatusString());
332+
}
333+
} else if (statusEvent.getStatus() == StatusEvent.Status.STATUS_OFFLINE) {
334+
if (currentStatus != StatusEvent.Status.STATUS_OFFLINE) {
335+
setOffline(statusEvent.getStatusString());
336+
}
337+
}
338+
}
339+
340+
@Subscribe(threadMode = ThreadMode.MAIN)
341+
public void onPausedEvent(PauseEvent event) {
342+
if (event.isPaused()) {
343+
onPaused();
344+
} else {
345+
onResumed();
346+
}
347+
}
348+
349+
private class PausedCliciListener implements View.OnClickListener {
350+
@Override
351+
public void onClick(View view) {
352+
Intent intent = StatusController.ControlActionReceiver.getResumeIntent();
353+
sendBroadcast(intent);
354+
}
355+
}
356+
357+
private class ResumedClickListener implements View.OnClickListener {
358+
@Override
359+
public void onClick(View view) {
360+
Intent intent = StatusController.ControlActionReceiver.getPauseIntent();
361+
sendBroadcast(intent);
362+
}
363+
}
278364
}

app/src/main/res/layout/activity_main.xml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,50 @@
66
android:layout_height="match_parent"
77
tools:context="org.leyfer.thesis.touchlogger_dirty.activity.MainActivity">
88

9+
<TextView
10+
android:id="@+id/status_text"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:layout_alignParentTop="true"
14+
android:layout_marginLeft="8dp"
15+
android:layout_marginStart="8dp"
16+
android:layout_marginTop="8dp"
17+
android:text="Hello World!"/>
18+
19+
<Button
20+
android:id="@+id/toggle_pause"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:layout_below="@id/status_text"
24+
android:layout_marginLeft="8dp"
25+
android:layout_marginStart="8dp"
26+
android:layout_marginTop="8dp"
27+
android:text="@string/action_pause"/>
28+
929
<TextView
1030
android:id="@+id/sample_text"
1131
android:layout_width="wrap_content"
1232
android:layout_height="wrap_content"
13-
android:text="Hello World!"
33+
android:layout_centerHorizontal="true"
1434
android:layout_centerVertical="true"
15-
android:layout_centerHorizontal="true"/>
35+
android:text="Hello World!"/>
1636

1737
<TextView
18-
android:layout_below="@id/sample_text"
19-
android:layout_marginBottom="8dp"
2038
android:id="@+id/version"
2139
android:layout_width="wrap_content"
2240
android:layout_height="wrap_content"
23-
android:text="Hello World!"
41+
android:layout_below="@id/sample_text"
42+
android:layout_centerHorizontal="true"
2443
android:layout_centerVertical="true"
25-
android:layout_centerHorizontal="true"/>
44+
android:layout_marginBottom="8dp"
45+
android:text="Hello World!"/>
2646

2747
<Button
2848
android:id="@+id/button"
2949
android:layout_width="match_parent"
3050
android:layout_height="wrap_content"
31-
android:text="@string/button_action_text"
3251
android:layout_alignParentBottom="true"
33-
android:layout_centerHorizontal="true"/>
52+
android:layout_centerHorizontal="true"
53+
android:text="@string/button_action_text"/>
3454

3555
</RelativeLayout>

0 commit comments

Comments
 (0)