Wrapped OsdUtil Toast calls in a RunOnUIThread function to avoid occasional crashes when beeping. Closes #8

This commit is contained in:
Graham Jones
2016-07-22 18:36:40 +01:00
parent ffd731c85d
commit 4f71b024b5
7 changed files with 35 additions and 84 deletions

View File

@@ -96,7 +96,7 @@ public class MainActivity extends Activity {
// 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));
//int i = 5/0; // Force exception to test handler. //int i = 5/0; // Force exception to test handler.
mUtil = new OsdUtil(this); mUtil = new OsdUtil(this,serverStatusHandler);
mConnection = new SdServiceConnection(this); mConnection = new SdServiceConnection(this);
// Initialise the User Interface // Initialise the User Interface

View File

@@ -33,6 +33,7 @@ import android.content.ServiceConnection;
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.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
@@ -48,6 +49,7 @@ import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.RandomAccess; import java.util.RandomAccess;
import java.util.concurrent.RunnableFuture;
/** /**
* OsdUtil - OpenSeizureDetector Utilities * OsdUtil - OpenSeizureDetector Utilities
@@ -58,12 +60,22 @@ public class OsdUtil {
* 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 String TAG = "OsdUtil"; private String TAG = "OsdUtil";
public OsdUtil(Context context) { public OsdUtil(Context context, Handler handler) {
mContext = context; mContext = context;
mHandler = handler;
} }
/**
* used to make sure timers etc. run on UI thread
*/
public void runOnUiThread(Runnable runnable) {
mHandler.post(runnable);
}
public boolean isServerRunning() { public boolean isServerRunning() {
//Log.v(TAG,"isServerRunning()................"); //Log.v(TAG,"isServerRunning()................");
ActivityManager manager = ActivityManager manager =
@@ -187,10 +199,14 @@ public class OsdUtil {
* Display a Toast message on screen. * Display a Toast message on screen.
* @param msg - message to display. * @param msg - message to display.
*/ */
public void showToast(String msg) { public void showToast(final String msg) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(mContext, msg, Toast.makeText(mContext, msg,
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
} }
});
}
/** /**

View File

@@ -28,6 +28,7 @@ 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.os.Handler;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
@@ -42,6 +43,7 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference
private OsdUtil mUtil; private OsdUtil mUtil;
private boolean mPrefChanged = false; private boolean mPrefChanged = false;
private Context mContext; private Context mContext;
private Handler mHandler;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -51,9 +53,10 @@ public class PrefActivity extends PreferenceActivity implements SharedPreference
Thread.setDefaultUncaughtExceptionHandler(new OsdUncaughtExceptionHandler(PrefActivity.this)); Thread.setDefaultUncaughtExceptionHandler(new OsdUncaughtExceptionHandler(PrefActivity.this));
//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());
mContext = getApplicationContext(); mContext = getApplicationContext();
mUtil = new OsdUtil(mContext,mHandler);
} }
/** /**

View File

@@ -166,7 +166,7 @@ public class SdServer extends Service implements SdDataReceiver {
mHandler = new Handler(); mHandler = new Handler();
mUtil = new OsdUtil(getApplicationContext()); 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);

View File

@@ -62,7 +62,7 @@ public class StartupActivity extends Activity {
private Timer mUiTimer; private Timer mUiTimer;
private SdServiceConnection mConnection; private SdServiceConnection mConnection;
private boolean mStartedMainActivity = false; private boolean mStartedMainActivity = false;
final Handler mServerStatusHandler = new Handler(); // used to update ui from mUiTimer private Handler mHandler = new Handler(); // used to update ui from mUiTimer
@Override @Override
@@ -78,7 +78,9 @@ public class StartupActivity extends Activity {
setContentView(R.layout.startup_activity); setContentView(R.layout.startup_activity);
mUtil = new OsdUtil(this); 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.
@@ -160,7 +162,7 @@ public class StartupActivity extends Activity {
mUiTimer.schedule(new TimerTask() { mUiTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
mServerStatusHandler.post(serverStatusRunnable); mHandler.post(serverStatusRunnable);
//updateServerStatus(); //updateServerStatus();
} }
}, 0, 1000); }, 0, 1000);

View File

@@ -1,72 +0,0 @@
package uk.org.openseizuredetector.mkfilter;
/**
* A port of A.J Fisher's mkfilter utility to Java.
* Created by graham on 12/07/16.
*/
public class MkFilter {
private static int opt_be = 0x00001; /* -Be Bessel characteristic */
private static int opt_bu = 0x00002; /* -Bu Butterworth characteristic */
private static int opt_ch = 0x00004; /* -Ch Chebyshev characteristic */
private static int opt_re = 0x00008; /* -Re Resonator */
private static int opt_pi = 0x00010; /* -Pi proportional-integral */
private static int opt_lp = 0x00020; /* -Lp lowpass */
private static int opt_hp = 0x00040; /* -Hp highpass */
private static int opt_bp = 0x00080; /* -Bp bandpass */
private static int opt_bs = 0x00100; /* -Bs bandstop */
private static int opt_ap = 0x00200; /* -Ap allpass */
private static int opt_a = 0x00400; /* -a alpha value */
private static int opt_l = 0x00800; /* -l just list filter parameters */
private static int opt_o = 0x01000; /* -o order of filter */
private static int opt_p = 0x02000; /* -p specified poles only */
private static int opt_w = 0x04000; /* -w don't pre-warp */
private static int opt_z = 0x08000; /* -z use matched z-transform */
private static int opt_Z = 0x10000; /* -Z additional zero */
private static int maxpz = 25;
private int order;
public class complex {
double re;
double im;
complex(double r, double i) {
re = r;
im = i;
}
}
class pzrep {
complex[] poles, zeros;
int numpoles, numzeros;
pzrep() {
poles = new complex[maxpz];
zeros = new complex[maxpz];
}
}
MkFilter() {
order = 5;
}
public void test() {
pzrep pz = new pzrep();
pz.poles[0] = new complex(1, 1);
}
public void compute_s() {
/* Butterworth filter */
for (int i = 0; i < 2 * order; i++) {
double theta = (order & 1) ? (i * PI) / order : ((i + 0.5) * PI) / order;
choosepole(expj(theta));
}
}
}

View File

@@ -1,6 +1,7 @@
package uk.org.openseizuredetector; package uk.org.openseizuredetector;
import android.app.Activity; import android.app.Activity;
import android.os.Handler;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -24,7 +25,8 @@ public class OsdUtilTest {
@Test @Test
public void testStartServer() throws Exception { public void testStartServer() throws Exception {
//Activity a = new Activity(); //Activity a = new Activity();
OsdUtil util = new OsdUtil(null); Handler handler = new Handler();
OsdUtil util = new OsdUtil(null,handler);
assertThat(util.isServerRunning(), is(true)); assertThat(util.isServerRunning(), is(true));
assertThat(true, is (true)); assertThat(true, is (true));
//assertThat(true, is(false)); //assertThat(true, is(false));