Commit c4fa2362 authored by Kristin Muterspaw's avatar Kristin Muterspaw
Browse files

SensorSampleService gets updated site, sector or spot from...

SensorSampleService gets updated site, sector or spot from SensorSampleActivity if the user changes it while bound to the service. Streaming button pulses when Field Day is streaming and writing to the database.
parent c7c1ba93
......@@ -17,6 +17,7 @@ import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.text.Editable;
......@@ -42,9 +43,6 @@ public class SensorSampleActivity extends Activity {
public LocationManager locationManager;
public LocationListener locationListener;
public static TextView longAndLat, satellites, accuracy, elevation;
public static double lastLongitude, lastLatitude, lastElevation;
public static int lastSatellites;
public static float lastAccuracy;
public static Resources res;
public static Spinner siteSpinner, sectorSpinner;
public static EditText spotET;
......@@ -55,12 +53,15 @@ public class SensorSampleActivity extends Activity {
public ArrayList<aSensor> listSensors;
public Boolean streaming = false, remote_db;
public Drawable streamOn, streamOff;
public Handler buttonHandler;
public Runnable buttonRunner;
public ImageButton exportButton, streamButton, sampleButton;
public static final String DEVICE = "BluetoothSensor Instance";
public static final String WHICH_SENSOR = "Which Sensor";
public static final String REMOTE_DB = "Remote DB Connected";
BluetoothSensor bluetoothSensor;
private SensorSampleService sampleService;
public int streamStatusId = 0;
private Context context;
private ArrayAdapter<aSensor> adapter;
......@@ -92,11 +93,15 @@ public class SensorSampleActivity extends Activity {
adapter.addAll(sensors);
adapter.notifyDataSetChanged();
} else if (SensorSampleService.ACTION_UPDATED_LIST.equals(intent.getAction())) {
Log.d("SensorSampleActivity", "Received Updated List Message!");
ArrayList<aSensor> sensors = intent.getParcelableArrayListExtra(SensorSampleService.SENSOR_LIST);
adapter.clear();
adapter.addAll(sensors);
adapter.notifyDataSetChanged();
} else if (SensorSampleService.ACTION_DISCONNECTED.equals(intent.getAction())){
buttonHandler.removeMessages(0);
buttonHandler.removeCallbacksAndMessages(buttonRunner);
streamStatusId = 0;
changeStreamButton();
}
}
};
......@@ -105,10 +110,7 @@ public class SensorSampleActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensorsample);
remote_db = getIntent().getBooleanExtra(REMOTE_DB, false);
initializeVariables();
context = this;
listSensors = new ArrayList<aSensor>();
Intent sensorServiceIntent = new Intent(this, SensorSampleService.class);
bindService(sensorServiceIntent, sampleServiceConnection, Context.BIND_AUTO_CREATE);
......@@ -139,8 +141,12 @@ public class SensorSampleActivity extends Activity {
@Override
public void onClick(View v) {
if (!streaming) {
streamButton.setImageDrawable(streamOn);
streaming = true;
streamStatusId = 1;
// This starts the handler that will change the streaming button so there's an
// indication that Field Day is actually streaming and writing to the database.
buttonHandler.postDelayed(buttonRunner, 350);
// Start the SensorSampleService supplying the type of sensor connected --
// bluetooth or built-in and the logging interval to use.
......@@ -150,9 +156,13 @@ public class SensorSampleActivity extends Activity {
i.putExtra(SensorSampleService.TRIP_NAME, tripName);
startService(i);
} else {
// Stop the SensorSampleService from writing to the database. Change the button
// for streaming to be off.
streamButton.setImageDrawable(streamOff);
streaming = false;
sampleService.stopSampling();
buttonHandler.removeMessages(0);
buttonHandler.removeCallbacksAndMessages(buttonRunner);
Intent i = new Intent(context, SensorSampleService.class);
stopService(i);
}
......@@ -298,9 +308,24 @@ public class SensorSampleActivity extends Activity {
logInterval = 1000 * 60;
break;
}
sampleService.onLoggingTimeChanged(logInterval);
Log.d("Changing Interval", "New Interval Time: " + String.valueOf(logInterval));
}
public void changeStreamButton() {
switch(streamStatusId){
case 0:
streamButton.setImageDrawable(streamOff);
streamStatusId = 1;
break;
case 1:
streamButton.setImageDrawable(streamOn);
streamStatusId = 0;
break;
}
}
public void exportDatabase() {
boolean success = db.copyDatabase("");
if (success){
......@@ -312,15 +337,10 @@ public class SensorSampleActivity extends Activity {
public void setLocation(Location location) {
if (location != null) {
lastLongitude = location.getLongitude();
lastLatitude = location.getLatitude();
lastElevation = location.getAltitude();
lastAccuracy = location.getAccuracy();
lastSatellites = location.getExtras().getInt("satellites");
longAndLat.setText(String.format(res.getString(R.string.latlong), lastLatitude, lastLongitude));
accuracy.setText(String.format(res.getString(R.string.accuracy), lastAccuracy));
satellites.setText(String.format(res.getString(R.string.satellite_count), lastSatellites));
elevation.setText(String.format(res.getString(R.string.elevation), lastElevation));
longAndLat.setText(String.format(res.getString(R.string.latlong), location.getLatitude(), location.getLongitude()));
accuracy.setText(String.format(res.getString(R.string.accuracy), location.getAccuracy()));
satellites.setText(String.format(res.getString(R.string.satellite_count), location.getExtras().getInt("satellites")));
elevation.setText(String.format(res.getString(R.string.elevation), location.getAltitude()));
}
}
......@@ -352,6 +372,17 @@ public class SensorSampleActivity extends Activity {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
tripName = settings.getString("trip", "");
context = this;
listSensors = new ArrayList<aSensor>();
remote_db = getIntent().getBooleanExtra(REMOTE_DB, false);
buttonHandler = new Handler();
buttonRunner = new Runnable() {
public void run() {
changeStreamButton();
buttonHandler.postDelayed(this, 350);
}
};
List<String> siteList = new ArrayList<String>();
List<String> sectorList = new ArrayList<String>();
......@@ -371,6 +402,7 @@ public class SensorSampleActivity extends Activity {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
site = parent.getItemAtPosition(position).toString();
sampleService.onSiteChanged(site);
}
@Override
......@@ -386,6 +418,7 @@ public class SensorSampleActivity extends Activity {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
sector = parent.getItemAtPosition(position).toString();
sampleService.onSectorChanged(sector);
}
@Override
......@@ -406,6 +439,7 @@ public class SensorSampleActivity extends Activity {
@Override
public void afterTextChanged(Editable s) {
spot = s.toString();
sampleService.onSpotChanged(spot);
}
});
}
......
......@@ -95,6 +95,7 @@ public class SensorSampleService extends Service implements SensorEventListener
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
btService = null;
dbHandler.removeCallbacksAndMessages(dbRunner);
broadcastUpdate(ACTION_DISCONNECTED);
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
btService.readMessage();
btHandler.postDelayed(btRunNames, 5000);
......@@ -102,10 +103,8 @@ public class SensorSampleService extends Service implements SensorEventListener
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
if (btService != null) {
if (BluetoothLeService.current_request.equals(BluetoothLeService.LIST_OF_SENSORS)) {
Log.d(TAG, "List of Sensors from device.");
parseResponse(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
} else if (BluetoothLeService.current_request.equals(BluetoothLeService.SENSOR_VALUES)){
Log.d(TAG, "Receiving Message");
parseResponse(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
}
}
......@@ -118,7 +117,6 @@ public class SensorSampleService extends Service implements SensorEventListener
final Intent intent = new Intent(action);
intent.putParcelableArrayListExtra(SENSOR_LIST, sensorList);
sendBroadcast(intent);
Log.d(TAG, "Sending Message Back to Activity.");
}
public class LocalBinder extends Binder {
......@@ -334,7 +332,7 @@ public class SensorSampleService extends Service implements SensorEventListener
sensor += c;
}
}
if (registered){
if (registered) {
if (writingToDB) {
writeToDB();
}
......@@ -387,6 +385,24 @@ public class SensorSampleService extends Service implements SensorEventListener
btHandler.removeCallbacksAndMessages(btRunNames);
}
public void onSpotChanged(String newSpot) {
spot = newSpot;
}
public void onSectorChanged(String newSector) {
sector = newSector;
}
public void onSiteChanged(String newSite) {
site = newSite;
}
public void onLoggingTimeChanged(long newLoggingInterval){
dbHandler.removeCallbacksAndMessages(dbRunner);
loggingInterval = newLoggingInterval;
dbHandler.postDelayed(dbRunner, loggingInterval);
}
public aSensor getSensor(String sensorID, Sensor sensor) {
for (aSensor s : sensorList) {
if (sensorType.equals("bluetooth")) {
......
......@@ -53,18 +53,18 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/site"
android:layout_alignEnd="@id/accuracy"
android:layout_below="@id/accuracy"
android:layout_alignStart="@id/accuracy" />
android:layout_alignStart="@id/accuracy"
android:layout_toStartOf="@+id/sector" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sector"
android:layout_below="@id/satellites"
android:layout_alignStart="@id/satellites"
android:layout_alignTop="@id/site"
android:layout_alignEnd="@id/satellites" />
android:layout_alignStart="@+id/interval"
android:layout_alignEnd="@+id/longandlat" />
<EditText
android:layout_width="wrap_content"
......
......@@ -53,18 +53,18 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/site"
android:layout_alignEnd="@id/accuracy"
android:layout_below="@id/accuracy"
android:layout_alignStart="@id/accuracy" />
android:layout_alignStart="@id/accuracy"
android:layout_toStartOf="@+id/sector" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sector"
android:layout_below="@id/satellites"
android:layout_alignStart="@id/satellites"
android:layout_alignTop="@id/site"
android:layout_alignEnd="@id/satellites" />
android:layout_alignStart="@+id/interval"
android:layout_alignEnd="@+id/longandlat" />
<EditText
android:layout_width="wrap_content"
......
......@@ -38,10 +38,10 @@
<string name="device">Device ID &#160;</string>
<!-- SensorSampleActivity Strings -->
<string name="satellite_count">Satellites: %1$d</string>
<string name="accuracy">Accuracy: %1$.0f</string>
<string name="satellite_count">Sat: %1$d</string>
<string name="accuracy">Acc: %1$.0f</string>
<string name="latlong">%1$.5f, %2$.5f</string>
<string name="elevation">Elevation: %1$.1f</string>
<string name="elevation">Elev: %1$.1f</string>
<string name="Satellites">Satellites</string>
<string name="LatLong">Latitude, Longitude</string>
<string name="Elevation">Elevation</string>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment