11package org .leyfer .thesis .touchlogger_dirty .activity ;
22
33import android .Manifest ;
4+ import android .app .PendingIntent ;
45import android .content .ClipData ;
56import android .content .ClipboardManager ;
67import android .content .DialogInterface ;
8+ import android .content .Intent ;
79import android .content .pm .PackageManager ;
810import android .os .Build ;
911import android .os .Bundle ;
2022import android .widget .TextView ;
2123import android .widget .Toast ;
2224
25+ import org .greenrobot .eventbus .EventBus ;
26+ import org .greenrobot .eventbus .Subscribe ;
27+ import org .greenrobot .eventbus .ThreadMode ;
2328import org .leyfer .thesis .touchlogger_dirty .BuildConfig ;
2429import org .leyfer .thesis .touchlogger_dirty .R ;
2530import 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 ;
2633import org .leyfer .thesis .touchlogger_dirty .exception .ManualInstallationException ;
34+ import org .leyfer .thesis .touchlogger_dirty .status_control .StatusController ;
2735import org .leyfer .thesis .touchlogger_dirty .utils .file .FileUtils ;
2836
2937import 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}
0 commit comments