Corrected starting and stopping of uiTimer so it stops properly when mainActivity closed.

This commit is contained in:
Graham Jones
2019-01-09 07:02:30 +00:00
parent 55cc65847d
commit 86ad2bdfc0

View File

@@ -25,10 +25,8 @@
package uk.org.openseizuredetector; package uk.org.openseizuredetector;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@@ -57,17 +55,11 @@ import java.util.TimerTask;
//MPAndroidChart //MPAndroidChart
import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.LargeValueFormatter;
import com.github.mikephil.charting.utils.ValueFormatter; import com.github.mikephil.charting.utils.ValueFormatter;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@@ -86,6 +78,7 @@ public class MainActivity extends AppCompatActivity {
final Handler serverStatusHandler = new Handler(); final Handler serverStatusHandler = new Handler();
Messenger messenger = new Messenger(new ResponseHandler()); Messenger messenger = new Messenger(new ResponseHandler());
Timer mUiTimer;
/** /**
* Called when the activity is first created. * Called when the activity is first created.
@@ -93,6 +86,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Log.v(TAG,"onCreate()");
// Set our custom uncaught exception handler to report issues. // Set our custom uncaught exception handler to report issues.
Thread.setDefaultUncaughtExceptionHandler(new OsdUncaughtExceptionHandler(MainActivity.this)); Thread.setDefaultUncaughtExceptionHandler(new OsdUncaughtExceptionHandler(MainActivity.this));
@@ -151,15 +145,6 @@ public class MainActivity extends AppCompatActivity {
}); });
// start timer to refresh user interface every second.
Timer uiTimer = new Timer();
uiTimer.schedule(new TimerTask() {
@Override
public void run() {
updateServerStatus();
}
}, 0, 1000);
} }
/** /**
@@ -167,7 +152,7 @@ public class MainActivity extends AppCompatActivity {
*/ */
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
Log.v(TAG, "onCreateOptionsMenue()"); Log.v(TAG, "onCreateOptionsMenu()");
getMenuInflater().inflate(R.menu.main_activity_actions, menu); getMenuInflater().inflate(R.menu.main_activity_actions, menu);
mOptionsMenu = menu; mOptionsMenu = menu;
return true; return true;
@@ -175,7 +160,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
Log.v(TAG, "Menu Option " + item.getItemId() + " selected"); Log.v(TAG, "onOptionsItemSelected() : " + item.getItemId() + " selected");
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_launch_pebble_app: case R.id.action_launch_pebble_app:
Log.v(TAG, "action_launch_pebble_app"); Log.v(TAG, "action_launch_pebble_app");
@@ -272,6 +257,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
Log.v(TAG,"onCStart()");
mUtil.writeToSysLogFile("MainActivity.onStart()"); mUtil.writeToSysLogFile("MainActivity.onStart()");
SharedPreferences SP = PreferenceManager SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext()); .getDefaultSharedPreferences(getBaseContext());
@@ -283,21 +269,35 @@ public class MainActivity extends AppCompatActivity {
String versionName = mUtil.getAppVersionName(); String versionName = mUtil.getAppVersionName();
tv.setText("OpenSeizureDetector Android App Version " + versionName); tv.setText("OpenSeizureDetector Android App Version " + versionName);
Log.v(TAG,"onStart() - binding to server");
mUtil.writeToSysLogFile("MainActivity.onStart - Binding to Server"); mUtil.writeToSysLogFile("MainActivity.onStart - Binding to Server");
mUtil.bindToServer(this, mConnection); mUtil.bindToServer(this, mConnection);
// start timer to refresh user interface every second.
mUiTimer = new Timer();
mUiTimer.schedule(new TimerTask() {
@Override
public void run() {
updateServerStatus();
}
}, 0, 1000);
} }
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
Log.v(TAG,"onStop() - unbinding from server");
mUtil.writeToSysLogFile("MainActivity.onStop()"); mUtil.writeToSysLogFile("MainActivity.onStop()");
mUtil.unbindFromServer(this, mConnection); mUtil.unbindFromServer(this, mConnection);
mUiTimer.cancel();
} }
private void startServer() { private void startServer() {
mUtil.writeToSysLogFile("MainActivity.startServer()"); mUtil.writeToSysLogFile("MainActivity.startServer()");
Log.v(TAG, "starting Server..."); Log.v(TAG, "startServer(): starting Server...");
mUtil.startServer(); mUtil.startServer();
// Change the action bar icon to show the option to stop the service. // Change the action bar icon to show the option to stop the service.
if (mOptionsMenu != null) { if (mOptionsMenu != null) {
@@ -312,7 +312,7 @@ public class MainActivity extends AppCompatActivity {
private void stopServer() { private void stopServer() {
mUtil.writeToSysLogFile("MainActivity.stopServer()"); mUtil.writeToSysLogFile("MainActivity.stopServer()");
Log.v(TAG, "stopping Server..."); Log.v(TAG, "stopServer(): stopping Server...");
mUtil.stopServer(); mUtil.stopServer();
// Change the action bar icon to show the option to start the service. // Change the action bar icon to show the option to start the service.
if (mOptionsMenu != null) { if (mOptionsMenu != null) {
@@ -326,7 +326,7 @@ public class MainActivity extends AppCompatActivity {
/* /*
* updateServerStatus - called by the uiTimer timer periodically. * updateServerStatus - called by the mUiTimer timer periodically.
* requests the ui to be updated by calling serverStatusRunnable. * requests the ui to be updated by calling serverStatusRunnable.
*/ */
private void updateServerStatus() { private void updateServerStatus() {
@@ -339,6 +339,8 @@ public class MainActivity extends AppCompatActivity {
*/ */
final Runnable serverStatusRunnable = new Runnable() { final Runnable serverStatusRunnable = new Runnable() {
public void run() { public void run() {
Log.v(TAG,"serverStatusRunnable()");
TextView tv; TextView tv;
if (mUtil.isServerRunning()) { if (mUtil.isServerRunning()) {
tv = (TextView) findViewById(R.id.serverStatusTv); tv = (TextView) findViewById(R.id.serverStatusTv);
@@ -631,12 +633,14 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
Log.v(TAG,"onPause()");
mUtil.writeToSysLogFile("MainActivity.onPause()"); mUtil.writeToSysLogFile("MainActivity.onPause()");
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Log.v(TAG,"onResume()");
mUtil.writeToSysLogFile("MainActivity.onResume()"); mUtil.writeToSysLogFile("MainActivity.onResume()");
} }