Created System Log file to help to trace system start-up/shut-down issues. Moved pebble specific code from OsdUtil to SdDataSoruce Pebble. Moved generic code for logging from SdDataSourcePebble to OsdUtil

This commit is contained in:
Graham Jones
2016-07-24 20:53:52 +01:00
parent f30d8b869d
commit 0178f249c1
7 changed files with 251 additions and 119 deletions

View File

@@ -170,7 +170,7 @@ public class MainActivity extends Activity {
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");
mUtil.startPebbleApp(); mConnection.mSdServer.mSdDataSource.startPebbleApp();
return true; return true;
case R.id.action_instal_watch_app: case R.id.action_instal_watch_app:
Log.v(TAG, "action_install_watch_app"); Log.v(TAG, "action_install_watch_app");

View File

@@ -30,12 +30,14 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager;
import android.text.format.Time; import android.text.format.Time;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
@@ -60,18 +62,51 @@ import java.util.concurrent.RunnableFuture;
* Deals with starting and stopping the background service and binding to it to receive data. * Deals with starting and stopping the background service and binding to it to receive data.
*/ */
public class OsdUtil { public class OsdUtil {
private final String SYSLOG = "SysLog";
private final String ALARMLOG = "AlarmLog";
private final String DATALOG = "DataLog";
/** /**
* Based on http://stackoverflow.com/questions/7440473/android-how-to-check-if-the-intent-service-is-still-running-or-has-stopped-running * Based on http://stackoverflow.com/questions/7440473/android-how-to-check-if-the-intent-service-is-still-running-or-has-stopped-running
*/ */
private Context mContext; private Context mContext;
private Handler mHandler; private Handler mHandler;
private String TAG = "OsdUtil"; private String TAG = "OsdUtil";
private boolean mLogAlarms = true;
private boolean mLogSystem = true;
private boolean mLogData = true;
public OsdUtil(Context context, Handler handler) { public OsdUtil(Context context, Handler handler) {
mContext = context; mContext = context;
mHandler = handler; mHandler = handler;
updatePrefs();
writeToSysLogFile("OsdUtil() - initialised");
} }
/**
* updatePrefs() - update basic settings from the SharedPreferences
* - defined in res/xml/prefs.xml
*/
public void updatePrefs() {
Log.v(TAG, "updatePrefs()");
SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(mContext);
try {
mLogAlarms = SP.getBoolean("LogAlarms", true);
Log.v(TAG, "updatePrefs() - mLogAlarms = " + mLogAlarms);
mLogData = SP.getBoolean("LogData", false);
Log.v(TAG, "updatePrefs() - mLogData = " + mLogData);
mLogSystem = SP.getBoolean("LogSystem", true);
Log.v(TAG, "updatePrefs() - mLogSystem = " + mLogSystem);
} catch (Exception ex) {
Log.v(TAG, "updatePrefs() - Problem parsing preferences!");
showToast("Problem Parsing Preferences - Something won't work - Please go back to Settings and correct it!");
}
}
/** /**
* used to make sure timers etc. run on UI thread * used to make sure timers etc. run on UI thread
*/ */
@@ -102,6 +137,8 @@ public class OsdUtil {
*/ */
public void startServer() { public void startServer() {
// Start the server // Start the server
Log.v(TAG,"startServer()");
writeToSysLogFile("startServer() - starting server");
Intent sdServerIntent; Intent sdServerIntent;
sdServerIntent = new Intent(mContext, SdServer.class); sdServerIntent = new Intent(mContext, SdServer.class);
sdServerIntent.setData(Uri.parse("Start")); sdServerIntent.setData(Uri.parse("Start"));
@@ -113,6 +150,7 @@ public class OsdUtil {
*/ */
public void stopServer() { public void stopServer() {
Log.v(TAG, "stopping Server..."); Log.v(TAG, "stopping Server...");
writeToSysLogFile("stopserver() - stopping server");
// then send an Intent to stop the service. // then send an Intent to stop the service.
Intent sdServerIntent; Intent sdServerIntent;
@@ -127,6 +165,7 @@ public class OsdUtil {
*/ */
public void bindToServer(Activity activity, SdServiceConnection sdServiceConnection) { public void bindToServer(Activity activity, SdServiceConnection sdServiceConnection) {
Log.v(TAG, "bindToServer() - binding to SdServer"); Log.v(TAG, "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);
} }
@@ -138,14 +177,18 @@ public class OsdUtil {
// unbind this activity from the service if it is bound. // unbind this activity from the service if it is bound.
if (sdServiceConnection.mBound) { if (sdServiceConnection.mBound) {
Log.v(TAG, "unbindFromServer() - unbinding"); Log.v(TAG, "unbindFromServer() - unbinding");
writeToSysLogFile("unbindFromServer() - unbinding");
try { try {
activity.unbindService(sdServiceConnection); activity.unbindService(sdServiceConnection);
sdServiceConnection.mBound = false; sdServiceConnection.mBound = false;
} 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());
} }
} else { } else {
Log.v(TAG, "unbindFromServer() - not bound to server - ignoring"); Log.v(TAG, "unbindFromServer() - not bound to server - ignoring");
writeToSysLogFile("unbindFromServer() - not bound to server - ignoring");
} }
} }
@@ -215,46 +258,36 @@ public class OsdUtil {
/** /**
* Open Pebble or Pebble Time app. If it is not installed, open Play store so the user can install it. * Write a message to the system log file, provided mLogSystem is true.
* @param msgStr
*/ */
public void startPebbleApp() { public void writeToSysLogFile(String msgStr) {
// first try to launch the original pebble app if (mLogSystem)
Intent pebbleAppIntent; writeToLogFile(SYSLOG,msgStr);
PackageManager pm = mContext.getPackageManager(); else
try { Log.v(TAG,"writeToSysLogFile - mLogSystem False so not writing");
pebbleAppIntent = pm.getLaunchIntentForPackage("com.getpebble.android");
mContext.startActivity(pebbleAppIntent);
} catch (Exception ex1) {
// and if original pebble app fails, try Pebble Time app...
Log.v(TAG, "exception starting original pebble App - trying pebble time..." + ex1.toString());
try {
pebbleAppIntent = pm.getLaunchIntentForPackage("com.getpebble.android.basalt");
mContext.startActivity(pebbleAppIntent);
} catch (Exception ex2) {
// and if that fails, open play store so the user can install it:
Log.v(TAG, "exception starting Pebble Time App." + ex2.toString());
this.showToast("Error Launching Pebble or Pebble Time App - Please make sure it is installed...");
final String appPackageName = "com.getpebble.android.basalt";
try {
// try using play store app.
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
// and if play store app is not installed, use browser to open app page.
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}
} }
/** /**
* Install the OpenSeizureDetector watch app onto the watch. * Write a message to the alarm log file, provided mLogAlarms is true.
* based on https://forums.getpebble.com/discussion/13128/install-watch-app-pebble-store-from-android-companion-app * @param msgStr
*/ */
public void installOsdWatchApp() { public void writeToAlarmLogFile(String msgStr) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("pebble://appstore/54d28a43e4d94c043f000008")); if (mLogAlarms)
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); writeToLogFile(ALARMLOG,msgStr);
mContext.startActivity(myIntent); else
Log.v(TAG,"writeToAlarmLogFile - mLogAlarms False so not writing");
}
/**
* Write a message to the data log file, provided mLogData is true.
* @param msgStr
*/
public void writeToDataLogFile(String msgStr) {
if (mLogData)
writeToLogFile(DATALOG,msgStr);
else
Log.v(TAG,"writeToDataLogFile - mLogData False so not writing");
} }
@@ -264,7 +297,7 @@ public class OsdUtil {
*/ */
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();
String dateStr = tnow.format("%Y-%m-%d"); String dateStr = tnow.format("%Y-%m-%d");
@@ -278,7 +311,9 @@ public class OsdUtil {
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+" : "+msgStr+"<br/>\n"); of.append(dateTimeStr+", "
+tnow.toMillis(true)+", "
+msgStr+"<br/>\n");
} }
of.close(); of.close();
} catch (Exception ex) { } catch (Exception ex) {

View File

@@ -24,6 +24,7 @@
package uk.org.openseizuredetector; package uk.org.openseizuredetector;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
@@ -39,13 +40,17 @@ interface SdDataReceiver {
public abstract class SdDataSource { public abstract class SdDataSource {
public SdData mSdData; public SdData mSdData;
public String mName = "undefined"; public String mName = "undefined";
protected OsdUtil mUtil;
protected Context mContext; protected Context mContext;
protected Handler mHandler;
protected SdDataReceiver mSdDataReceiver; protected SdDataReceiver mSdDataReceiver;
private String TAG = "SdDataSource"; private String TAG = "SdDataSource";
public SdDataSource(Context context, SdDataReceiver sdDataReceiver) { public SdDataSource(Context context, Handler handler, SdDataReceiver sdDataReceiver) {
Log.v(TAG, "SdDataSource() Constructor"); Log.v(TAG, "SdDataSource() Constructor");
mContext = context; mContext = context;
mHandler = handler;
mUtil = new OsdUtil(mContext, mHandler);
mSdDataReceiver = sdDataReceiver; mSdDataReceiver = sdDataReceiver;
mSdData = new SdData(); mSdData = new SdData();
} }
@@ -78,6 +83,8 @@ public abstract class SdDataSource {
*/ */
public void installWatchApp() { Log.v(TAG,"installWatchApp"); } public void installWatchApp() { Log.v(TAG,"installWatchApp"); }
public void startPebbleApp() { Log.v(TAG,"startPebbleApp()"); }
/** /**
* Display a Toast message on screen. * Display a Toast message on screen.
* @param msg - message to display. * @param msg - message to display.

View File

@@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.format.Time; import android.text.format.Time;
import android.util.Log; import android.util.Log;
@@ -32,14 +33,15 @@ public class SdDataSourceNetwork extends SdDataSource {
private int ALARM_STATE_NETFAULT = 7; private int ALARM_STATE_NETFAULT = 7;
public SdDataSourceNetwork(Context context, SdDataReceiver sdDataReceiver) { public SdDataSourceNetwork(Context context, Handler handler, SdDataReceiver sdDataReceiver) {
super(context,sdDataReceiver); super(context, handler, sdDataReceiver);
mName = "Network"; mName = "Network";
} }
@Override public void start() { @Override public void start() {
// Update preferences. // Update preferences.
Log.v(TAG,"start(): calling updatePrefs()"); Log.v(TAG,"start(): calling updatePrefs()");
mUtil.writeToSysLogFile("SdDataSourceNetwork().start()");
updatePrefs(); updatePrefs();
// Start timer to retrieve seizure detector data regularly. // Start timer to retrieve seizure detector data regularly.
@@ -62,6 +64,7 @@ public class SdDataSourceNetwork extends SdDataSource {
} }
@Override public void stop() { @Override public void stop() {
mUtil.writeToSysLogFile("SdDataSourceNetwork().stop()");
// Stop the data update timer // Stop the data update timer
if (mDataUpdateTimer !=null) { if (mDataUpdateTimer !=null) {
Log.v(TAG,"stop(): cancelling status timer"); Log.v(TAG,"stop(): cancelling status timer");
@@ -80,6 +83,7 @@ public class SdDataSourceNetwork extends SdDataSource {
*/ */
public void updatePrefs() { public void updatePrefs() {
Log.v(TAG, "updatePrefs()"); Log.v(TAG, "updatePrefs()");
mUtil.writeToSysLogFile("SdDataSourceNetwork().updatePrefs()");
SharedPreferences SP = PreferenceManager SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(mContext); .getDefaultSharedPreferences(mContext);
mServerIP = SP.getString("ServerIP","192.168.1.175"); mServerIP = SP.getString("ServerIP","192.168.1.175");
@@ -90,14 +94,14 @@ public class SdDataSourceNetwork extends SdDataSource {
Log.v(TAG,"updatePrefs() - mDataUpdatePeriod = "+mDataUpdatePeriod); Log.v(TAG,"updatePrefs() - mDataUpdatePeriod = "+mDataUpdatePeriod);
} catch (Exception ex) { } catch (Exception ex) {
Log.v(TAG,"updatePrefs() - Problem parsing preferences!"); Log.v(TAG,"updatePrefs() - Problem parsing preferences!");
mUtil.writeToSysLogFile("SdDataSourceNetwork().updatePrefs() - " +ex.toString());
showToast("Problem Parsing Preferences - Something won't work"); showToast("Problem Parsing Preferences - Something won't work");
} }
} }
/** /**
* Retrive the current Seizure Detector Data from the server. * Retrive the current Seizure Detector Data from the server.
* Uses teh DownloadSdDataTask class to download the data in the * Uses the DownloadSdDataTask class to download the data in the
* background. The data is processed in DownloadSdDataTask.onPostExecute(). * background. The data is processed in DownloadSdDataTask.onPostExecute().
*/ */
public void downloadSdData() { public void downloadSdData() {

View File

@@ -26,21 +26,17 @@ package uk.org.openseizuredetector;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.format.Time; import android.text.format.Time;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.getpebble.android.kit.Constants;
import com.getpebble.android.kit.PebbleKit; import com.getpebble.android.kit.PebbleKit;
import com.getpebble.android.kit.util.PebbleDictionary; import com.getpebble.android.kit.util.PebbleDictionary;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@@ -49,10 +45,6 @@ import java.io.OutputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.UUID; import java.util.UUID;
@@ -149,8 +141,9 @@ public class SdDataSourcePebble extends SdDataSource {
private double[] rawData = new double[MAX_RAW_DATA]; private double[] rawData = new double[MAX_RAW_DATA];
private int nRawData = 0; private int nRawData = 0;
public SdDataSourcePebble(Context context, SdDataReceiver sdDataReceiver) { public SdDataSourcePebble(Context context, Handler handler,
super(context, sdDataReceiver); SdDataReceiver sdDataReceiver) {
super(context, handler, sdDataReceiver);
mName = "Pebble"; mName = "Pebble";
// Set default settings from XML files (mContext is set by super(). // Set default settings from XML files (mContext is set by super().
PreferenceManager.setDefaultValues(mContext, PreferenceManager.setDefaultValues(mContext,
@@ -164,6 +157,7 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
public void start() { public void start() {
Log.v(TAG, "start()"); Log.v(TAG, "start()");
mUtil.writeToSysLogFile("SdDataSourcePebble.start()");
updatePrefs(); updatePrefs();
startPebbleServer(); startPebbleServer();
// Start timer to check status of pebble regularly. // Start timer to check status of pebble regularly.
@@ -171,7 +165,8 @@ public class SdDataSourcePebble extends SdDataSource {
// use a timer to check the status of the pebble app on the same frequency // use a timer to check the status of the pebble app on the same frequency
// as we get app data. // as we get app data.
if (mStatusTimer == null) { if (mStatusTimer == null) {
Log.v(TAG, "onCreate(): starting status timer"); Log.v(TAG, "start(): starting status timer");
mUtil.writeToSysLogFile("SdDataSourcePebble.start() - starting status timer");
mStatusTimer = new Timer(); mStatusTimer = new Timer();
mStatusTimer.schedule(new TimerTask() { mStatusTimer.schedule(new TimerTask() {
@Override @Override
@@ -180,26 +175,28 @@ public class SdDataSourcePebble extends SdDataSource {
} }
}, 0, mDataUpdatePeriod * 1000); }, 0, mDataUpdatePeriod * 1000);
} else { } else {
Log.v(TAG, "onCreate(): status timer already running."); Log.v(TAG, "start(): status timer already running.");
mUtil.writeToSysLogFile("SdDataSourcePebble.start() - status timer already running??");
} }
// make sure we get some data when we first start. // make sure we get some data when we first start.
getPebbleData(); getPebbleData();
// Start timer to retrieve pebble settings regularly. // Start timer to retrieve pebble settings regularly.
getPebbleSdSettings(); getPebbleSdSettings();
if (mSettingsTimer == null) { if (mSettingsTimer == null) {
Log.v(TAG, "onCreate(): starting settings timer"); Log.v(TAG, "start(): starting settings timer");
mUtil.writeToSysLogFile("SdDataSourcePebble.start() - starting settings timer");
mSettingsTimer = new Timer(); mSettingsTimer = new Timer();
mSettingsTimer.schedule(new TimerTask() { mSettingsTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
mUtil.writeToSysLogFile("SdDataSourcePebble.mSettingsTimer timed out.");
getPebbleSdSettings(); getPebbleSdSettings();
} }
}, 0, 1000 * (mDataUpdatePeriod + 60)); // ask for settings less frequently than we get data }, 0, 1000 * (mDataUpdatePeriod + 60)); // ask for settings less frequently than we get data
} else { } else {
Log.v(TAG, "onCreate(): settings timer already running."); Log.v(TAG, "start(): settings timer already running.");
mUtil.writeToSysLogFile("SdDataSourcePebble.start() - settings timer already running??");
} }
} }
/** /**
@@ -207,30 +204,33 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
public void stop() { public void stop() {
Log.v(TAG, "stop()"); Log.v(TAG, "stop()");
mUtil.writeToSysLogFile("SdDataSourcePebble.stop()");
try { try {
// Stop the status timer // Stop the status timer
if (mStatusTimer != null) { if (mStatusTimer != null) {
Log.v(TAG, "onDestroy(): cancelling status timer"); Log.v(TAG, "stop(): cancelling status timer");
mUtil.writeToSysLogFile("SdDataSourcePebble.stop() - cancelling status timer");
mStatusTimer.cancel(); mStatusTimer.cancel();
mStatusTimer.purge(); mStatusTimer.purge();
mStatusTimer = null; mStatusTimer = null;
} }
// Stop the settings timer // Stop the settings timer
if (mSettingsTimer != null) { if (mSettingsTimer != null) {
Log.v(TAG, "onDestroy(): cancelling settings timer"); Log.v(TAG, "stop(): cancelling settings timer");
mUtil.writeToSysLogFile("SdDataSourcePebble.stop() - cancelling settings timer");
mSettingsTimer.cancel(); mSettingsTimer.cancel();
mSettingsTimer.purge(); mSettingsTimer.purge();
mSettingsTimer = null; mSettingsTimer = null;
} }
// Stop pebble message handler. // Stop pebble message handler.
Log.v(TAG, "onDestroy(): stopping pebble server"); Log.v(TAG, "stop(): stopping pebble server");
mUtil.writeToSysLogFile("SdDataSourcePebble.stop() - stopping pebble server");
stopPebbleServer(); stopPebbleServer();
} catch (Exception e) { } catch (Exception e) {
Log.v(TAG, "Error in stop() - " + e.toString()); Log.v(TAG, "Error in stop() - " + e.toString());
mUtil.writeToSysLogFile("SdDataSourcePebble.stop() - error - "+e.toString());
} }
} }
/** /**
@@ -239,6 +239,7 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
public void updatePrefs() { public void updatePrefs() {
Log.v(TAG, "updatePrefs()"); Log.v(TAG, "updatePrefs()");
mUtil.writeToSysLogFile("SdDataSourcePebble.updatePrefs()");
SharedPreferences SP = PreferenceManager SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(mContext); .getDefaultSharedPreferences(mContext);
try { try {
@@ -337,6 +338,7 @@ public class SdDataSourcePebble extends SdDataSource {
} catch (Exception ex) { } catch (Exception ex) {
Log.v(TAG, "updatePrefs() - Problem parsing preferences!"); Log.v(TAG, "updatePrefs() - Problem parsing preferences!");
mUtil.writeToSysLogFile("SdDataSourcePebble.updatePrefs() - ERROR "+ex.toString());
Toast toast = Toast.makeText(mContext, "Problem Parsing Preferences - Something won't work - Please go back to Settings and correct it!", Toast.LENGTH_SHORT); Toast toast = Toast.makeText(mContext, "Problem Parsing Preferences - Something won't work - Please go back to Settings and correct it!", Toast.LENGTH_SHORT);
toast.show(); toast.show();
} }
@@ -349,6 +351,7 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
private void startPebbleServer() { private void startPebbleServer() {
Log.v(TAG, "StartPebbleServer()"); Log.v(TAG, "StartPebbleServer()");
mUtil.writeToSysLogFile("SdDataSourcePebble.startPebbleServer()");
final Handler handler = new Handler(); final Handler handler = new Handler();
msgDataHandler = new PebbleKit.PebbleDataReceiver(SD_UUID) { msgDataHandler = new PebbleKit.PebbleDataReceiver(SD_UUID) {
@Override @Override
@@ -457,6 +460,7 @@ public class SdDataSourcePebble extends SdDataSource {
public void stopPebbleServer() { public void stopPebbleServer() {
Log.v(TAG, "stopPebbleServer(): Stopping Pebble Server"); Log.v(TAG, "stopPebbleServer(): Stopping Pebble Server");
Log.v(TAG, "stopPebbleServer(): msgDataHandler = " + msgDataHandler.toString()); Log.v(TAG, "stopPebbleServer(): msgDataHandler = " + msgDataHandler.toString());
mUtil.writeToSysLogFile("SdDataSourcePebble.stopPebbleServer()");
try { try {
mContext.unregisterReceiver(msgDataHandler); mContext.unregisterReceiver(msgDataHandler);
} catch (Exception e) { } catch (Exception e) {
@@ -469,17 +473,12 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
public void startWatchApp() { public void startWatchApp() {
Log.v(TAG, "startWatchApp() - closing app first"); Log.v(TAG, "startWatchApp() - closing app first");
mUtil.writeToSysLogFile("SdDataSourcePebble.startWatchApp() - closing app first");
// first close the watch app if it is running. // first close the watch app if it is running.
PebbleKit.closeAppOnPebble(mContext, SD_UUID); PebbleKit.closeAppOnPebble(mContext, SD_UUID);
// then start it after a 1 second delay.
//final Handler handler = new Handler();
//handler.postDelayed(new Runnable() {
// @Override
// public void run() {
Log.v(TAG, "startWatchApp() - starting watch app..."); Log.v(TAG, "startWatchApp() - starting watch app...");
mUtil.writeToSysLogFile("SdDataSourcePebble.startWatchApp() - starting watch app");
PebbleKit.startAppOnPebble(mContext, SD_UUID); PebbleKit.startAppOnPebble(mContext, SD_UUID);
// }
//}, 1000);
} }
/** /**
@@ -487,6 +486,7 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
public void stopWatchApp() { public void stopWatchApp() {
Log.v(TAG, "stopWatchApp()"); Log.v(TAG, "stopWatchApp()");
mUtil.writeToSysLogFile("SdDataSourcePebble.stopWatchApp()");
PebbleKit.closeAppOnPebble(mContext, SD_UUID); PebbleKit.closeAppOnPebble(mContext, SD_UUID);
} }
@@ -498,8 +498,10 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
public void getPebbleSdSettings() { public void getPebbleSdSettings() {
Log.v(TAG, "getPebbleSdSettings() - sending required settings to pebble"); Log.v(TAG, "getPebbleSdSettings() - sending required settings to pebble");
mUtil.writeToSysLogFile("SdDataSourcePebble.getPebbleSdSettings() - send settings first");
sendPebbleSdSettings(); sendPebbleSdSettings();
Log.v(TAG, "getPebbleSdSettings() - requesting settings from pebble"); Log.v(TAG, "getPebbleSdSettings() - requesting settings from pebble");
mUtil.writeToSysLogFile("SdDataSourcePebble.getPebbleSdSettings() - and request settings from pebble");
PebbleDictionary data = new PebbleDictionary(); PebbleDictionary data = new PebbleDictionary();
data.addUint8(KEY_SETTINGS, (byte) 1); data.addUint8(KEY_SETTINGS, (byte) 1);
PebbleKit.sendDataToPebble( PebbleKit.sendDataToPebble(
@@ -514,6 +516,8 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
public void sendPebbleSdSettings() { public void sendPebbleSdSettings() {
Log.v(TAG, "sendPebblSdSettings() - preparing settings dictionary.. mSampleFreq=" + mSampleFreq); Log.v(TAG, "sendPebblSdSettings() - preparing settings dictionary.. mSampleFreq=" + mSampleFreq);
mUtil.writeToSysLogFile("SdDataSourcePebble.sendPebbleSdSettings()");
// Watch Settings // Watch Settings
final PebbleDictionary setDict = new PebbleDictionary(); final PebbleDictionary setDict = new PebbleDictionary();
setDict.addInt16(KEY_DEBUG, mDebug); setDict.addInt16(KEY_DEBUG, mDebug);
@@ -613,6 +617,7 @@ public class SdDataSourcePebble extends SdDataSource {
*/ */
public void getPebbleData() { public void getPebbleData() {
Log.v(TAG, "getPebbleData() - requesting data from pebble"); Log.v(TAG, "getPebbleData() - requesting data from pebble");
mUtil.writeToSysLogFile("SdDataSourcePebble.getPebbleData() - requesting data from pebble");
PebbleDictionary data = new PebbleDictionary(); PebbleDictionary data = new PebbleDictionary();
data.addUint8(KEY_DATA_TYPE, (byte) 1); data.addUint8(KEY_DATA_TYPE, (byte) 1);
PebbleKit.sendDataToPebble( PebbleKit.sendDataToPebble(
@@ -646,6 +651,7 @@ public class SdDataSourcePebble extends SdDataSource {
Log.v(TAG, "getPebbleStatus() - tdiff = " + tdiff); Log.v(TAG, "getPebbleStatus() - tdiff = " + tdiff);
mSdData.pebbleAppRunning = false; mSdData.pebbleAppRunning = false;
Log.v(TAG, "getPebbleStatus() - Pebble App Not Running - Attempting to Re-Start"); Log.v(TAG, "getPebbleStatus() - Pebble App Not Running - Attempting to Re-Start");
mUtil.writeToSysLogFile("SdDataSourcePebble.getPebbleStatus() - Pebble App not Running - Attempting to Re-Start");
startWatchApp(); startWatchApp();
//mPebbleStatusTime = tnow; // set status time to now so we do not re-start app repeatedly. //mPebbleStatusTime = tnow; // set status time to now so we do not re-start app repeatedly.
getPebbleSdSettings(); getPebbleSdSettings();
@@ -677,7 +683,11 @@ public class SdDataSourcePebble extends SdDataSource {
} }
} }
/**
* analyseRawData() - called when raw data is received.
* FIXME - this does not do anything at the moment so raw data is
* ignored!
*/
private void analyseRawData() { private void analyseRawData() {
Log.v(TAG,"analyserawData()"); Log.v(TAG,"analyserawData()");
//DoubleFFT_1D fft = new DoubleFFT_1D(MAX_RAW_DATA); //DoubleFFT_1D fft = new DoubleFFT_1D(MAX_RAW_DATA);
@@ -694,6 +704,7 @@ public class SdDataSourcePebble extends SdDataSource {
@Override @Override
public void installWatchApp() { public void installWatchApp() {
Log.v(TAG, "SdDataSourcePebble.installWatchApp()"); Log.v(TAG, "SdDataSourcePebble.installWatchApp()");
mUtil.writeToSysLogFile("SdDataSourcePebble.installWatchApp()");
final String WATCHAPP_FILENAME = "pebble_sd.pbw"; final String WATCHAPP_FILENAME = "pebble_sd.pbw";
try { try {
@@ -713,10 +724,65 @@ public class SdDataSourcePebble extends SdDataSource {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent); mContext.startActivity(intent);
} catch (IOException e) { } catch (IOException e) {
mUtil.writeToSysLogFile("SdDataSourcePebble.installWatchApp() - app install failed"+e.toString());
Toast.makeText(mContext, "App install failed: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); Toast.makeText(mContext, "App install failed: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
} }
} }
/**
* Install the OpenSeizureDetector watch app onto the watch from Pebble AppStore
* based on https://forums.getpebble.com/discussion/13128/install-watch-app-pebble-store-from-android-companion-app
*/
public void installWatchAppFromPebbleAppStore() {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("pebble://appstore/54d28a43e4d94c043f000008"));
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(myIntent);
}
/**
* Open Pebble or Pebble Time app. If it is not installed, open Play store so the user can install it.
*/
@Override
public void startPebbleApp() {
mUtil.writeToSysLogFile("SdDataSourcePebble.startPebbleApp()");
// first try to launch the original pebble app
Intent pebbleAppIntent;
PackageManager pm = mContext.getPackageManager();
try {
pebbleAppIntent = pm.getLaunchIntentForPackage("com.getpebble.android");
mContext.startActivity(pebbleAppIntent);
} catch (Exception ex1) {
// and if original pebble app fails, try Pebble Time app...
Log.v(TAG, "exception starting original pebble App - trying pebble time..." + ex1.toString());
mUtil.writeToSysLogFile("SdDataSourcePebble.startPebbleApp() - Error starting original pebble app - trying Pebble Time App instead");
try {
pebbleAppIntent = pm.getLaunchIntentForPackage("com.getpebble.android.basalt");
mContext.startActivity(pebbleAppIntent);
} catch (Exception ex2) {
// and if that fails, open play store so the user can install it:
Log.v(TAG, "exception starting Pebble Time App." + ex2.toString());
mUtil.writeToSysLogFile("SdDataSourcePebble.startPebbleApp() - Error starting Pebble Time App - Is it installed?");
this.showToast("Error Launching Pebble or Pebble Time App - Please make sure it is installed...");
final String appPackageName = "com.getpebble.android.basalt";
try {
mUtil.writeToSysLogFile("SdDataSourcePebble.startPebbleApp() - Opening Play Store to install Pebble App");
// try using play store app.
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
// and if play store app is not installed, use browser to open app page.
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}
}
} }

View File

@@ -79,7 +79,6 @@ import org.json.JSONArray;
public class SdServer extends Service implements SdDataReceiver { public class SdServer extends Service implements SdDataReceiver {
// Notification ID // Notification ID
private int NOTIFICATION_ID = 1; private int NOTIFICATION_ID = 1;
private final String SYSLOG = "SysLog";
private NotificationManager mNM; private NotificationManager mNM;
@@ -131,10 +130,9 @@ public class SdServer extends Service implements SdDataReceiver {
*/ */
public SdServer() { public SdServer() {
super(); super();
Log.v(TAG, "SdServer Created");
mSdData = new SdData(); mSdData = new SdData();
mToneGenerator = new ToneGenerator(AudioManager.STREAM_ALARM, 100); mToneGenerator = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
Log.v(TAG, "SdServer Created");
} }
@@ -145,7 +143,7 @@ public class SdServer extends Service implements SdDataReceiver {
} }
/** /**
* used to make suer timers run on UI thread * used to make sure timers run on UI thread
*/ */
private void runOnUiThread(Runnable runnable) { private void runOnUiThread(Runnable runnable) {
mHandler.post(runnable); mHandler.post(runnable);
@@ -158,6 +156,9 @@ public class SdServer extends Service implements SdDataReceiver {
@Override @Override
public void onCreate() { public void onCreate() {
Log.v(TAG, "onCreate()"); Log.v(TAG, "onCreate()");
mHandler = new Handler();
mUtil = new OsdUtil(getApplicationContext(),mHandler);
mUtil.writeToSysLogFile("SdServer.onCreate()");
// Set our custom uncaught exception handler to report issues. // Set our custom uncaught exception handler to report issues.
Thread.setDefaultUncaughtExceptionHandler( Thread.setDefaultUncaughtExceptionHandler(
@@ -165,9 +166,6 @@ public class SdServer extends Service implements SdDataReceiver {
//int i = 5/0; // Force exception to test handler. //int i = 5/0; // Force exception to test handler.
mHandler = new Handler();
mUtil = new OsdUtil(getApplicationContext(),mHandler);
// Create a wake lock, but don't use it until the service is started. // Create a wake lock, but don't use it until the service is started.
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
@@ -182,6 +180,7 @@ public class SdServer extends Service implements SdDataReceiver {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "onStartCommand() - SdServer service starting"); Log.v(TAG, "onStartCommand() - SdServer service starting");
mUtil.writeToSysLogFile("SdServer.onStartCommand()");
// Update preferences. // Update preferences.
Log.v(TAG, "onStartCommand() - calling updatePrefs()"); Log.v(TAG, "onStartCommand() - calling updatePrefs()");
@@ -191,23 +190,28 @@ public class SdServer extends Service implements SdDataReceiver {
switch (mSdDataSourceName) { switch (mSdDataSourceName) {
case "Pebble": case "Pebble":
Log.v(TAG, "Selecting Pebble DataSource"); Log.v(TAG, "Selecting Pebble DataSource");
mSdDataSource = new SdDataSourcePebble(this.getApplicationContext(), this); mUtil.writeToSysLogFile("SdServer.onStartCommand() - creating SdDataSourcePebble");
mSdDataSource = new SdDataSourcePebble(this.getApplicationContext(), mHandler, this);
break; break;
case "Network": case "Network":
Log.v(TAG, "Selecting Network DataSource"); Log.v(TAG, "Selecting Network DataSource");
mSdDataSource = new SdDataSourceNetwork(this.getApplicationContext(), this); mUtil.writeToSysLogFile("SdServer.onStartCommand() - creating SdDataSourceNetwork");
mSdDataSource = new SdDataSourceNetwork(this.getApplicationContext(), mHandler, this);
break; break;
default: default:
Log.v(TAG, "Datasource " + mSdDataSourceName + " not recognised - Exiting"); Log.v(TAG, "Datasource " + mSdDataSourceName + " not recognised - Exiting");
mUtil.writeToSysLogFile("SdServer.onStartCommand() - Datasource "+mSdDataSourceName+" not recognised - exiting");
mUtil.showToast("Datasource " + mSdDataSourceName + " not recognised - Exiting"); mUtil.showToast("Datasource " + mSdDataSourceName + " not recognised - Exiting");
return 1; return 1;
} }
mUtil.writeToSysLogFile("SdServer.onStartCommand() - starting SdDataSource");
mSdDataSource.start(); mSdDataSource.start();
// Display a notification icon in the status bar of the phone to // Display a notification icon in the status bar of the phone to
// show the service is running. // show the service is running.
Log.v(TAG, "showing Notification"); Log.v(TAG, "showing Notification");
mUtil.writeToSysLogFile("SdServer.onStartCommand() - showing Notification");
showNotification(0); showNotification(0);
// Record last time we sent an SMS so we can limit rate of SMS // Record last time we sent an SMS so we can limit rate of SMS
@@ -217,7 +221,8 @@ public class SdServer extends Service implements SdDataReceiver {
// Start timer to log data regularly.. // Start timer to log data regularly..
if (dataLogTimer == null) { if (dataLogTimer == null) {
Log.v(TAG, "onCreate(): starting dataLog timer"); Log.v(TAG, "onStartCommand(): starting dataLog timer");
mUtil.writeToSysLogFile("SdServer.onStartCommand() - starting dataLog timer");
dataLogTimer = new Timer(); dataLogTimer = new Timer();
dataLogTimer.schedule(new TimerTask() { dataLogTimer.schedule(new TimerTask() {
@Override @Override
@@ -226,19 +231,23 @@ public class SdServer extends Service implements SdDataReceiver {
} }
}, 0, 1000 * 60); }, 0, 1000 * 60);
} else { } else {
Log.v(TAG, "onCreate(): dataLog timer already running."); Log.v(TAG, "onStartCommand(): dataLog timer already running.");
mUtil.writeToSysLogFile("SdServer.onStartCommand() - dataLog timer already running???");
} }
// Start the web server // Start the web server
mUtil.writeToSysLogFile("SdServer.onStartCommand() - starting web server");
startWebServer(); startWebServer();
// Apply the wake-lock to prevent CPU sleeping (very battery intensive!) // Apply the wake-lock to prevent CPU sleeping (very battery intensive!)
if (mWakeLock != null) { if (mWakeLock != null) {
mWakeLock.acquire(); mWakeLock.acquire();
Log.v(TAG, "Applied Wake Lock to prevent device sleeping"); Log.v(TAG, "Applied Wake Lock to prevent device sleeping");
mUtil.writeToSysLogFile("SdServer.onStartCommand() - applying wake lock");
} else { } else {
Log.d(TAG, "mmm...mWakeLock is null, so not aquiring lock. This shouldn't happen!"); Log.d(TAG, "mmm...mWakeLock is null, so not aquiring lock. This shouldn't happen!");
mUtil.writeToSysLogFile("SdServer.onStartCommand() - mWakeLock is not null - this shouldn't happen???");
} }
return START_STICKY; return START_STICKY;
@@ -247,6 +256,7 @@ public class SdServer extends Service implements SdDataReceiver {
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.v(TAG, "onDestroy(): SdServer Service stopping"); Log.v(TAG, "onDestroy(): SdServer Service stopping");
mUtil.writeToSysLogFile("SdServer.onDestroy() - releasing wakelock");
// release the wake lock to allow CPU to sleep and reduce // release the wake lock to allow CPU to sleep and reduce
// battery drain. // battery drain.
if (mWakeLock != null) { if (mWakeLock != null) {
@@ -255,22 +265,26 @@ public class SdServer extends Service implements SdDataReceiver {
Log.v(TAG, "Released Wake Lock to allow device to sleep."); Log.v(TAG, "Released Wake Lock to allow device to sleep.");
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error Releasing Wakelock - " + e.toString()); Log.e(TAG, "Error Releasing Wakelock - " + e.toString());
mUtil.writeToSysLogFile("SdServer.onDestroy() - Error releasing wakelock.");
mUtil.showToast("Error Releasing Wakelock"); mUtil.showToast("Error Releasing Wakelock");
} }
} else { } else {
Log.d(TAG, "mmm...mWakeLock is null, so not releasing lock. This shouldn't happen!"); Log.d(TAG, "mmm...mWakeLock is null, so not releasing lock. This shouldn't happen!");
mUtil.writeToSysLogFile("SdServer.onDestroy() - mWakeLock is null so not releasing lock - this Shouldn't happen???");
} }
if (mSdDataSource != null) { if (mSdDataSource != null) {
Log.v(TAG, "stopping mSdDataSource"); Log.v(TAG, "stopping mSdDataSource");
mUtil.writeToSysLogFile("SdServer.onDestroy() - stopping mSdDataSource");
mSdDataSource.stop(); mSdDataSource.stop();
} else { } else {
Log.e(TAG, "ERROR - mSdDataSource is null - why????"); Log.e(TAG, "ERROR - mSdDataSource is null - why????");
mUtil.writeToSysLogFile("SdServer.onDestroy() - mSdDataSource is null - why???");
} }
// Stop the data update timer // Stop the Cancel Audible timer
if (mCancelAudibleTimer != null) { if (mCancelAudibleTimer != null) {
Log.v(TAG, "stop(): cancelling Cancel_Audible timer"); Log.v(TAG, "onDestroy(): cancelling Cancel_Audible timer");
mCancelAudibleTimer.cancel(); mCancelAudibleTimer.cancel();
//mCancelAudibleTimer.purge(); //mCancelAudibleTimer.purge();
mCancelAudibleTimer = null; mCancelAudibleTimer = null;
@@ -280,18 +294,23 @@ public class SdServer extends Service implements SdDataReceiver {
try { try {
// Cancel the notification. // Cancel the notification.
Log.v(TAG, "onDestroy(): cancelling notification"); Log.v(TAG, "onDestroy(): cancelling notification");
mUtil.writeToSysLogFile("SdServer.onDestroy - cancelling notification");
mNM.cancel(NOTIFICATION_ID); mNM.cancel(NOTIFICATION_ID);
// Stop web server // Stop web server
Log.v(TAG, "onDestroy(): stopping web server"); Log.v(TAG, "onDestroy(): stopping web server");
mUtil.writeToSysLogFile("SdServer.onDestroy() - stopping Web Server");
stopWebServer(); stopWebServer();
// stop this service. // stop this service.
Log.v(TAG, "onDestroy(): calling stopSelf()"); Log.v(TAG, "onDestroy(): calling stopSelf()");
mUtil.writeToSysLogFile("SdServer.onDestroy() - stopping self");
stopSelf(); stopSelf();
} catch (Exception e) { } catch (Exception e) {
Log.v(TAG, "Error in onDestroy() - " + e.toString()); Log.v(TAG, "Error in onDestroy() - " + e.toString());
mUtil.writeToSysLogFile("SdServer.onDestroy() -error "+e.toString());
} }
mUtil.writeToSysLogFile("SdServer.onDestroy() - releasing mToneGenerator");
mToneGenerator.release(); mToneGenerator.release();
mToneGenerator = null; mToneGenerator = null;
} }
@@ -337,6 +356,7 @@ public class SdServer extends Service implements SdDataReceiver {
// Show the main activity on the user's screen. // Show the main activity on the user's screen.
private void showMainActivity() { private void showMainActivity() {
Log.v(TAG, "showMainActivity()"); Log.v(TAG, "showMainActivity()");
mUtil.writeToSysLogFile("SdServer.showMainActivity()");
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1); List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
@@ -344,6 +364,7 @@ public class SdServer extends Service implements SdDataReceiver {
if (componentInfo.getPackageName().equals("uk.org.openseizuredetector")) { if (componentInfo.getPackageName().equals("uk.org.openseizuredetector")) {
Log.v(TAG,"showMainActivity(): OpenSeizureDetector Activity is already shown on top - not doing anything"); Log.v(TAG,"showMainActivity(): OpenSeizureDetector Activity is already shown on top - not doing anything");
mUtil.writeToSysLogFile("SdServer.showMainActivity - Activity is already shown on top, not doing anything");
} else { } else {
Log.v(TAG,"showMainActivity(): Showing Main Activity"); Log.v(TAG,"showMainActivity(): Showing Main Activity");
Intent i = new Intent(getApplicationContext(), MainActivity.class); Intent i = new Intent(getApplicationContext(), MainActivity.class);
@@ -615,9 +636,9 @@ public class SdServer extends Service implements SdDataReceiver {
*/ */
protected void startWebServer() { protected void startWebServer() {
Log.v(TAG, "startWebServer()"); Log.v(TAG, "startWebServer()");
mUtil.writeToLogFile(SYSLOG,"Start Web Server.\n"); mUtil.writeToSysLogFile("SdServer.Start Web Server.");
if (webServer == null) { if (webServer == null) {
webServer = new SdWebServer(getApplicationContext(), getDataStorageDir(), mSdData); webServer = new SdWebServer(getApplicationContext(), mUtil.getDataStorageDir(), mSdData);
try { try {
webServer.start(); webServer.start();
} catch (IOException ioe) { } catch (IOException ioe) {
@@ -633,7 +654,7 @@ public class SdServer extends Service implements SdDataReceiver {
* Stop the web server - FIXME - doesn't seem to do anything! * Stop the web server - FIXME - doesn't seem to do anything!
*/ */
protected void stopWebServer() { protected void stopWebServer() {
Log.v(TAG, "stopWebServer()"); Log.v(TAG, "SdServer.stopWebServer()");
if (webServer != null) { if (webServer != null) {
webServer.stop(); webServer.stop();
if (webServer.isAlive()) { if (webServer.isAlive()) {
@@ -662,6 +683,8 @@ public class SdServer extends Service implements SdDataReceiver {
*/ */
public void updatePrefs() { public void updatePrefs() {
Log.v(TAG, "updatePrefs()"); Log.v(TAG, "updatePrefs()");
mUtil.writeToSysLogFile("SdServer.updatePrefs()");
SharedPreferences SP = PreferenceManager SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext()); .getDefaultSharedPreferences(getBaseContext());
try { try {
@@ -700,31 +723,13 @@ public class SdServer extends Service implements SdDataReceiver {
} catch (Exception ex) { } catch (Exception ex) {
Log.v(TAG, "updatePrefs() - Problem parsing preferences!"); Log.v(TAG, "updatePrefs() - Problem parsing preferences!");
mUtil.writeToSysLogFile("SdServer.updatePrefs() - Error "+ex.toString());
Toast toast = Toast.makeText(getApplicationContext(), "Problem Parsing Preferences - Something won't work - Please go back to Settings and correct it!", Toast.LENGTH_SHORT); Toast toast = Toast.makeText(getApplicationContext(), "Problem Parsing Preferences - Something won't work - Please go back to Settings and correct it!", Toast.LENGTH_SHORT);
toast.show(); toast.show();
} }
} }
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
public File getDataStorageDir() {
// Get the directory for the user's public pictures directory.
File file =
new File(Environment.getExternalStorageDirectory()
, "OpenSeizureDetector");
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
return file;
}
/** /**
* Write data to SD card alarm log * Write data to SD card alarm log
@@ -759,9 +764,9 @@ public class SdServer extends Service implements SdDataReceiver {
fname = fname + "_" + dateStr + ".txt"; fname = fname + "_" + dateStr + ".txt";
// Open output directory on SD Card. // Open output directory on SD Card.
if (isExternalStorageWritable()) { if (mUtil.isExternalStorageWritable()) {
try { try {
FileWriter of = new FileWriter(getDataStorageDir().toString() FileWriter of = new FileWriter(mUtil.getDataStorageDir().toString()
+ "/" + fname, true); + "/" + fname, true);
if (mSdData != null) { if (mSdData != null) {
Log.v(TAG, "writing mSdData.toString()"); Log.v(TAG, "writing mSdData.toString()");

View File

@@ -71,16 +71,19 @@ public class StartupActivity extends Activity {
// Set our custom uncaught exception handler to report issues. // Set our custom uncaught exception handler to report issues.
Thread.setDefaultUncaughtExceptionHandler(new OsdUncaughtExceptionHandler(StartupActivity.this)); Thread.setDefaultUncaughtExceptionHandler(new OsdUncaughtExceptionHandler(StartupActivity.this));
//int i = 5/0; // Force exception to test handler.
mHandler = new Handler();
mUtil = new OsdUtil(this,mHandler);
mUtil.writeToSysLogFile("");
mUtil.writeToSysLogFile("*******************************");
mUtil.writeToSysLogFile("* StartUpActivity Started *");
mUtil.writeToSysLogFile("*******************************");
// Force the screen to stay on when the app is running // Force the screen to stay on when the app is running
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.startup_activity); setContentView(R.layout.startup_activity);
mHandler = new Handler();
mUtil = new OsdUtil(this,mHandler);
// Read the default settings from the xml preferences files, so we do // Read the default settings from the xml preferences files, so we do
// not have to use the hard coded ones in the java files. // not have to use the hard coded ones in the java files.
@@ -95,12 +98,14 @@ public class StartupActivity extends Activity {
public void onClick(View view) { public void onClick(View view) {
Log.v(TAG, "settings button clicked"); Log.v(TAG, "settings button clicked");
try { try {
mUtil.writeToSysLogFile("Starting Settings Activity");
Intent intent = new Intent( Intent intent = new Intent(
StartupActivity.this, StartupActivity.this,
PrefActivity.class); PrefActivity.class);
startActivity(intent); startActivity(intent);
} catch (Exception ex) { } catch (Exception ex) {
Log.v(TAG, "exception starting settings activity " + ex.toString()); Log.v(TAG, "exception starting settings activity " + ex.toString());
mUtil.writeToSysLogFile("ERROR Starting Settings Activity");
} }
} }
@@ -111,7 +116,8 @@ public class StartupActivity extends Activity {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Log.v(TAG, "pebble button clicked"); Log.v(TAG, "pebble button clicked");
mUtil.startPebbleApp(); mUtil.writeToSysLogFile("Starting Pebble Phone App");
mConnection.mSdServer.mSdDataSource.startPebbleApp();
} }
}); });
@@ -120,7 +126,7 @@ public class StartupActivity extends Activity {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
Log.v(TAG, "install Osd Watch App button clicked"); Log.v(TAG, "install Osd Watch App button clicked");
//mUtil.installOsdWatchApp(); mUtil.writeToSysLogFile("Installing Watch App");
mConnection.mSdServer.mSdDataSource.installWatchApp(); mConnection.mSdServer.mSdDataSource.installWatchApp();
} }
}); });
@@ -130,6 +136,8 @@ public class StartupActivity extends Activity {
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
mUtil.writeToSysLogFile("StartupActivity.onStart()");
// Display the DataSource name // Display the DataSource name
SharedPreferences SP = PreferenceManager SharedPreferences SP = PreferenceManager
@@ -149,11 +157,14 @@ public class StartupActivity extends Activity {
if (mUtil.isServerRunning()) { if (mUtil.isServerRunning()) {
Log.v(TAG, "onStart() - server running - stopping it"); Log.v(TAG, "onStart() - server running - stopping it");
mUtil.writeToSysLogFile("StartupActivity.onStart() - server already running - stopping it.");
mUtil.stopServer(); mUtil.stopServer();
} }
mUtil.writeToSysLogFile("StartupActivity.onStart() - starting server");
mUtil.startServer(); mUtil.startServer();
// Bind to the service. // Bind to the service.
mUtil.writeToSysLogFile("StartupActivity.onStart() - binding to server");
mConnection = new SdServiceConnection(this); mConnection = new SdServiceConnection(this);
mUtil.bindToServer(this, mConnection); mUtil.bindToServer(this, mConnection);
@@ -171,8 +182,9 @@ public class StartupActivity extends Activity {
@Override @Override
protected void onStop() { protected void onStop() {
Log.v(TAG, "onStop()");
super.onStop(); super.onStop();
Log.v(TAG, "onStop()");
mUtil.writeToSysLogFile("StartupActivity.onStop() - unbinding from server");
mUtil.unbindFromServer(this, mConnection); mUtil.unbindFromServer(this, mConnection);
mUiTimer.cancel(); mUiTimer.cancel();
} }
@@ -300,6 +312,7 @@ public class StartupActivity extends Activity {
if (allOk) { if (allOk) {
if (!mStartedMainActivity) { if (!mStartedMainActivity) {
Log.v(TAG, "starting main activity..."); Log.v(TAG, "starting main activity...");
mUtil.writeToSysLogFile("StartupActivity.serverStatusRunnable - all checks ok - starting main activity.");
try { try {
Intent intent = new Intent( Intent intent = new Intent(
getApplicationContext(), getApplicationContext(),
@@ -311,9 +324,11 @@ public class StartupActivity extends Activity {
} catch (Exception ex) { } catch (Exception ex) {
mStartedMainActivity = false; mStartedMainActivity = false;
Log.v(TAG, "exception starting main activity " + ex.toString()); Log.v(TAG, "exception starting main activity " + ex.toString());
mUtil.writeToSysLogFile("StartupActivity.serverStatusRunnable - exception starting main activity "+ex.toString());
} }
} else { } else {
Log.v(TAG,"allOk, but already started MainActivity so not doing anything"); Log.v(TAG,"allOk, but already started MainActivity so not doing anything");
mUtil.writeToSysLogFile("StartupActivity.serverStatusRunnable - allOk, but already started MainActivity so not doing anything");
} }
} }
} }