Added functionality to switch data sources (network data source does not work yet though!).

This commit is contained in:
Graham Jones
2015-11-22 07:21:51 +00:00
parent 856b3c5d3d
commit ce30cb53cb
8 changed files with 59 additions and 5 deletions

View File

@@ -36,6 +36,7 @@ import android.net.Uri;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast;
import org.apache.http.conn.util.InetAddressUtils; import org.apache.http.conn.util.InetAddressUtils;
@@ -177,6 +178,13 @@ public class OsdUtil {
return null; return null;
} }
/**
* Display a Toast message on screen.
* @param msg - message to display.
*/
public void showToast(String msg) {
Toast.makeText(mContext, msg,
Toast.LENGTH_LONG).show();
}
} }

View File

@@ -37,6 +37,7 @@ interface SdDataReceiver {
*/ */
public abstract class SdDataSource { public abstract class SdDataSource {
public SdData mSdData; public SdData mSdData;
public String mName = "undefined";
protected Context mContext; protected Context mContext;
protected SdDataReceiver mSdDataReceiver; protected SdDataReceiver mSdDataReceiver;
private String TAG = "SdDataSource"; private String TAG = "SdDataSource";

View File

@@ -0,0 +1,14 @@
package uk.org.openseizuredetector;
import android.content.Context;
/**
* Created by graham on 22/11/15.
*/
public class SdDataSourceNetwork extends SdDataSource {
public SdDataSourceNetwork(Context context, SdDataReceiver sdDataReceiver) {
super(context,sdDataReceiver);
mName = "Network";
}
}

View File

@@ -97,6 +97,7 @@ public class SdDataSourcePebble extends SdDataSource {
public SdDataSourcePebble(Context context, SdDataReceiver sdDataReceiver) { public SdDataSourcePebble(Context context, SdDataReceiver sdDataReceiver) {
super(context,sdDataReceiver); super(context,sdDataReceiver);
mName = "Pebble";
} }

View File

@@ -84,6 +84,7 @@ public class SdServer extends Service implements SdDataReceiver {
private WakeLock mWakeLock = null; private WakeLock mWakeLock = null;
public SdDataSource mSdDataSource; public SdDataSource mSdDataSource;
public SdData mSdData; public SdData mSdData;
private String mSdDataSourceName = "undefined"; // The name of the data soruce specified in the preferences.
private boolean mLatchAlarms = false; private boolean mLatchAlarms = false;
private boolean mCancelAudible = false; private boolean mCancelAudible = false;
private boolean mAudibleAlarm = false; private boolean mAudibleAlarm = false;
@@ -96,6 +97,7 @@ public class SdServer extends Service implements SdDataReceiver {
private boolean mLogAlarms = true; private boolean mLogAlarms = true;
private boolean mLogData = false; private boolean mLogData = false;
private File mOutFile; private File mOutFile;
private OsdUtil mUtil;
private final IBinder mBinder = new SdBinder(); private final IBinder mBinder = new SdBinder();
@@ -134,6 +136,7 @@ public class SdServer extends Service implements SdDataReceiver {
public void onCreate() { public void onCreate() {
Log.v(TAG, "onCreate()"); Log.v(TAG, "onCreate()");
mUtil = new OsdUtil(getApplicationContext());
// 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);
@@ -152,7 +155,22 @@ public class SdServer extends Service implements SdDataReceiver {
// Update preferences. // Update preferences.
Log.v(TAG, "onStartCommand() - calling updatePrefs()"); Log.v(TAG, "onStartCommand() - calling updatePrefs()");
updatePrefs(); updatePrefs();
Log.v(TAG, "onStartCommand: Datasource =" + mSdDataSourceName);
switch (mSdDataSourceName) {
case "Pebble":
Log.v(TAG,"Selecting Pebble DataSource");
mSdDataSource = new SdDataSourcePebble(this.getApplicationContext(), this); mSdDataSource = new SdDataSourcePebble(this.getApplicationContext(), this);
break;
case "Network":
Log.v(TAG, "Selecting Network DataSource");
mSdDataSource = new SdDataSourceNetwork(this.getApplicationContext(),this);
break;
default:
Log.v(TAG, "Datasource " + mSdDataSourceName + " not recognised - Exiting");
mUtil.showToast("Datasource " + mSdDataSourceName + " not recognised - Exiting");
return 1;
}
mSdDataSource.start(); mSdDataSource.start();
@@ -491,6 +509,8 @@ public class SdServer extends Service implements SdDataReceiver {
SharedPreferences SP = PreferenceManager SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext()); .getDefaultSharedPreferences(getBaseContext());
try { try {
mSdDataSourceName = SP.getString("DataSource","undefined");
Log.v(TAG,"updatePrefs() - DataSource = "+mSdDataSourceName);
mLatchAlarms = SP.getBoolean("LatchAlarms", false); mLatchAlarms = SP.getBoolean("LatchAlarms", false);
Log.v(TAG, "updatePrefs() - mLatchAlarms = " + mLatchAlarms); Log.v(TAG, "updatePrefs() - mLatchAlarms = " + mLatchAlarms);
mAudibleFaultWarning = SP.getBoolean("AudibleFaultWarning", true); mAudibleFaultWarning = SP.getBoolean("AudibleFaultWarning", true);

View File

@@ -26,11 +26,13 @@ package uk.org.openseizuredetector;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
@@ -101,6 +103,14 @@ public class StartupActivity extends Activity {
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
// Display the DataSource name
SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());;
String dataSourceName = SP.getString("DataSource","undefined");
TextView tv = (TextView)findViewById(R.id.dataSourceTextView);
tv.setText("DataSource = "+dataSourceName);
if (mUtil.isServerRunning()) { if (mUtil.isServerRunning()) {
Log.v(TAG, "onStart() - server running - stopping it"); Log.v(TAG, "onStart() - server running - stopping it");
mUtil.stopServer(); mUtil.stopServer();

View File

@@ -26,7 +26,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Starting......" android:text="Starting......"
android:id="@+id/textView3" android:id="@+id/dataSourceTextView"
android:layout_gravity="center_horizontal" /> android:layout_gravity="center_horizontal" />
<LinearLayout <LinearLayout

View File

@@ -5,8 +5,8 @@
<item>"Network"</item> <item>"Network"</item>
</string-array> </string-array>
<string-array name="datasource_list_values"> <string-array name="datasource_list_values">
<item>"pebble"</item> <item>"Pebble"</item>
<item>"network"</item> <item>"Network"</item>
</string-array> </string-array>
</resources> </resources>