diff --git a/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java b/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java index 289ff6b..36f3a69 100644 --- a/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java +++ b/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java @@ -107,6 +107,8 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac private boolean mLogData = true; private boolean mPermissionsRequested = false; + private static int mNbound = 0; + public OsdUtil(Context context, Handler handler) { mContext = context; mHandler = handler; @@ -190,7 +192,7 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac * Stop the SdServer service */ public void stopServer() { - Log.d(TAG, "OsdUtil.stopServer() - stopping Server..."); + Log.d(TAG, "OsdUtil.stopServer() - stopping Server... - mNbound=" + mNbound); writeToSysLogFile("stopserver() - stopping server"); // then send an Intent to stop the service. @@ -209,6 +211,8 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac writeToSysLogFile("bindToServer() - binding to SdServer"); Intent intent = new Intent(sdServiceConnection.mContext, SdServer.class); 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 { activity.unbindService(sdServiceConnection); sdServiceConnection.mBound = false; + mNbound = mNbound - 1; + Log.i(TAG,"OsdUtil.unBindFromServer() - mNbound = "+mNbound); } catch (Exception ex) { Log.e(TAG, "unbindFromServer() - error unbinding service - " + ex.toString()); writeToSysLogFile("unbindFromServer() - error unbinding service - " +ex.toString()); + Log.i(TAG,"OsdUtil.unBindFromServer() - mNbound = "+mNbound); } } else { Log.i(TAG, "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. */ public void writeToLogFile(String fname, String msgStr) { - Log.v(TAG, "writeToLogFile(" + fname + "," + msgStr + ")"); + //Log.v(TAG, "writeToLogFile(" + fname + "," + msgStr + ")"); //showToast("Logging " + msgStr); Time tnow = new Time(Time.getCurrentTimezone()); tnow.setToNow(); @@ -356,7 +363,7 @@ public class OsdUtil implements ActivityCompat.OnRequestPermissionsResultCallbac + "/" + fname, true); if (msgStr != null) { String dateTimeStr = tnow.format("%Y-%m-%d %H:%M:%S"); - Log.v(TAG, "writing msgStr"); + //Log.v(TAG, "writing msgStr"); of.append(dateTimeStr + ", " + tnow.toMillis(true) + ", " + msgStr + "
\n"); diff --git a/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java b/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java index 9de08da..430161c 100644 --- a/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/PrefActivity.java @@ -117,8 +117,16 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { Log.i(TAG, "SharedPreference " + s + " Changed."); - //mUtil.showToast("Shared Preference " + s + " Changed."); + mUtil.showToast("Setting " + s + " Changed - restarting server"); 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() { super.onStop(); mUtil.writeToSysLogFile("PrefActvity.onStop()"); - Log.i(TAG, "onStop. mPrefChanged=" + mPrefChanged); + /*Log.i(TAG, "onStop. mPrefChanged=" + mPrefChanged); if (mPrefChanged) { Log.i(TAG,"PrefActivity.onStop() - settings changed - restarting server"); mUtil.writeToSysLogFile("PrefActvity.onStop() - settings changed - re-starting server...."); mUtil.showToast("Settings Changed - re-starting OpenSeizureDetector...."); - Intent intent = new Intent(getApplicationContext(), StartupActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); - finish(); + mUtil.stopServer(); + // Wait 5 seconds to give the server chance to shutdown. + mHandler.postDelayed(new Runnable() { + 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); + } + */ } /** diff --git a/app/src/main/java/uk/org/openseizuredetector/SdServer.java b/app/src/main/java/uk/org/openseizuredetector/SdServer.java index 82321b0..7b86cb6 100644 --- a/app/src/main/java/uk/org/openseizuredetector/SdServer.java +++ b/app/src/main/java/uk/org/openseizuredetector/SdServer.java @@ -986,7 +986,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei } else { Log.v(TAG, "NetworkBroadcastReceiver - Wifi Connected"); mUtil.writeToSysLogFile("Network State Changed - Wifi Connected"); - mUtil.showToast("Network State Changed - Wifi Connected"); + //mUtil.showToast("Network State Changed - Wifi Connected"); } } else { Log.v(TAG, "NetworkBroadcastReceiver - No Active Network");