Fixed problem with server not shutting down properly after changing settings, resulting in multiple versions of the datasource running.

This commit is contained in:
Graham Jones
2019-01-27 20:19:41 +00:00
parent 0bc18d5d0b
commit 02b2451a57
3 changed files with 34 additions and 11 deletions

View File

@@ -107,6 +107,8 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac
private boolean mLogData = true; private boolean mLogData = true;
private boolean mPermissionsRequested = false; private boolean mPermissionsRequested = false;
private static int mNbound = 0;
public OsdUtil(Context context, Handler handler) { public OsdUtil(Context context, Handler handler) {
mContext = context; mContext = context;
mHandler = handler; mHandler = handler;
@@ -190,7 +192,7 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac
* Stop the SdServer service * Stop the SdServer service
*/ */
public void stopServer() { public void stopServer() {
Log.d(TAG, "OsdUtil.stopServer() - stopping Server..."); Log.d(TAG, "OsdUtil.stopServer() - stopping Server... - mNbound=" + mNbound);
writeToSysLogFile("stopserver() - stopping server"); writeToSysLogFile("stopserver() - stopping server");
// then send an Intent to stop the service. // then send an Intent to stop the service.
@@ -209,6 +211,8 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac
writeToSysLogFile("bindToServer() - binding to SdServer"); writeToSysLogFile("bindToServer() - binding to SdServer");
Intent intent = new Intent(sdServiceConnection.mContext, SdServer.class); Intent intent = new Intent(sdServiceConnection.mContext, SdServer.class);
activity.bindService(intent, sdServiceConnection, Context.BIND_AUTO_CREATE); activity.bindService(intent, sdServiceConnection, Context.BIND_AUTO_CREATE);
mNbound = mNbound + 1;
Log.i(TAG,"OsdUtil.bindToServer() - mNbound = "+mNbound);
} }
/** /**
@@ -222,14 +226,17 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac
try { try {
activity.unbindService(sdServiceConnection); activity.unbindService(sdServiceConnection);
sdServiceConnection.mBound = false; sdServiceConnection.mBound = false;
mNbound = mNbound - 1;
Log.i(TAG,"OsdUtil.unBindFromServer() - mNbound = "+mNbound);
} catch (Exception ex) { } catch (Exception ex) {
Log.e(TAG, "unbindFromServer() - error unbinding service - " + ex.toString()); Log.e(TAG, "unbindFromServer() - error unbinding service - " + ex.toString());
writeToSysLogFile("unbindFromServer() - error unbinding service - " +ex.toString()); writeToSysLogFile("unbindFromServer() - error unbinding service - " +ex.toString());
Log.i(TAG,"OsdUtil.unBindFromServer() - mNbound = "+mNbound);
} }
} else { } else {
Log.i(TAG, "unbindFromServer() - not bound to server - ignoring"); Log.i(TAG, "unbindFromServer() - not bound to server - ignoring");
writeToSysLogFile("unbindFromServer() - not bound to server - ignoring"); writeToSysLogFile("unbindFromServer() - not bound to server - ignoring");
Log.i(TAG,"OsdUtil.unBindFromServer() - mNbound = "+mNbound);
} }
} }
@@ -337,7 +344,7 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac
* in which case writes to alarm log file. * in which case writes to alarm log file.
*/ */
public void writeToLogFile(String fname, String msgStr) { public void writeToLogFile(String fname, String msgStr) {
Log.v(TAG, "writeToLogFile(" + fname + "," + msgStr + ")"); //Log.v(TAG, "writeToLogFile(" + fname + "," + msgStr + ")");
//showToast("Logging " + msgStr); //showToast("Logging " + msgStr);
Time tnow = new Time(Time.getCurrentTimezone()); Time tnow = new Time(Time.getCurrentTimezone());
tnow.setToNow(); tnow.setToNow();
@@ -356,7 +363,7 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac
+ "/" + fname, true); + "/" + fname, true);
if (msgStr != null) { if (msgStr != null) {
String dateTimeStr = tnow.format("%Y-%m-%d %H:%M:%S"); String dateTimeStr = tnow.format("%Y-%m-%d %H:%M:%S");
Log.v(TAG, "writing msgStr"); //Log.v(TAG, "writing msgStr");
of.append(dateTimeStr + ", " of.append(dateTimeStr + ", "
+ tnow.toMillis(true) + ", " + tnow.toMillis(true) + ", "
+ msgStr + "<br/>\n"); + msgStr + "<br/>\n");

View File

@@ -117,8 +117,16 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Log.i(TAG, "SharedPreference " + s + " Changed."); Log.i(TAG, "SharedPreference " + s + " Changed.");
//mUtil.showToast("Shared Preference " + s + " Changed."); mUtil.showToast("Setting " + s + " Changed - restarting server");
mPrefChanged = true; mPrefChanged = true;
mUtil.stopServer();
// Wait 0.1 second to give the server chance to shutdown, then re-start it
mHandler.postDelayed(new Runnable() {
public void run() {
mUtil.startServer();
}
}, 100);
} }
@@ -146,16 +154,24 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
mUtil.writeToSysLogFile("PrefActvity.onStop()"); mUtil.writeToSysLogFile("PrefActvity.onStop()");
Log.i(TAG, "onStop. mPrefChanged=" + mPrefChanged); /*Log.i(TAG, "onStop. mPrefChanged=" + mPrefChanged);
if (mPrefChanged) { if (mPrefChanged) {
Log.i(TAG,"PrefActivity.onStop() - settings changed - restarting server"); Log.i(TAG,"PrefActivity.onStop() - settings changed - restarting server");
mUtil.writeToSysLogFile("PrefActvity.onStop() - settings changed - re-starting server...."); mUtil.writeToSysLogFile("PrefActvity.onStop() - settings changed - re-starting server....");
mUtil.showToast("Settings Changed - re-starting OpenSeizureDetector...."); mUtil.showToast("Settings Changed - re-starting OpenSeizureDetector....");
Intent intent = new Intent(getApplicationContext(), StartupActivity.class); mUtil.stopServer();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); // Wait 5 seconds to give the server chance to shutdown.
startActivity(intent); mHandler.postDelayed(new Runnable() {
finish(); public void run() {
Intent intent = new Intent(getApplicationContext(), StartupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}, 5000);
} }
*/
} }
/** /**

View File

@@ -986,7 +986,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei
} else { } else {
Log.v(TAG, "NetworkBroadcastReceiver - Wifi Connected"); Log.v(TAG, "NetworkBroadcastReceiver - Wifi Connected");
mUtil.writeToSysLogFile("Network State Changed - Wifi Connected"); mUtil.writeToSysLogFile("Network State Changed - Wifi Connected");
mUtil.showToast("Network State Changed - Wifi Connected"); //mUtil.showToast("Network State Changed - Wifi Connected");
} }
} else { } else {
Log.v(TAG, "NetworkBroadcastReceiver - No Active Network"); Log.v(TAG, "NetworkBroadcastReceiver - No Active Network");