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

Bench sample works, new dropdown list adapter for dropdown menus because the...

Bench sample works, new dropdown list adapter for dropdown menus because the list is List<Pair<String,String>> so the android simple one doesn't work. If Field Day can't find a latitude, longitude, etc for the bench sampling trip, site, sector and spot then it doesn't write to the database and informs the user it can't find the geo information.
parent 7da25ca7
......@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -2,27 +2,48 @@ package fieldscience.cs.earlham.edu.fieldday;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.Pair;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Map;
public class BenchSampleActivity extends Activity {
private static final String TAG = BenchSampleActivity.class.getSimpleName();
public static final String REMOTE_DB = "Remote DB Connected";
private static final String table = "fieldday_reading";
Spinner siteSpinner, sectorSpinner, spotSpinner, platformSpinner, sensorSpinner;
EditText sensorValue, sensorQuality, readingNotes;
EditText valueET, qualityET, notesET;
ReadingsDatabase db;
String site, sector, spot, platform, sensor;
String site, sector, spot, platform, sensorName, sensorValue, sensorQuality, tripName, readingNotes;
boolean remote_db;
List<String> siteList, sectorList, spotList, platformList, sensorList;
public static final String REMOTE_DB = "Remote DB Connected";
List<String> spotList, platformList;
List<Pair<String, String>> siteList, sectorList, sensorList;
Pair<String, String> sitePair, sectorPair, sensorPair;
Button recordButton;
private DropdownListAdapter siteAdapter, sectorAdapter, sensorAdapter;
private aSensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState){
......@@ -30,6 +51,8 @@ public class BenchSampleActivity extends Activity {
setContentView(R.layout.activity_benchsample);
db = ReadingsDatabase.getInstance(this);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
tripName = settings.getString("trip", "");
siteList = new ArrayList<>();
sectorList = new ArrayList<>();
......@@ -38,23 +61,25 @@ public class BenchSampleActivity extends Activity {
sensorList = new ArrayList<>();
remote_db = getIntent().getBooleanExtra(REMOTE_DB, false);
if (remote_db){
siteList = db.getSites(null);
}
siteList.add(0, "Site");
readingNotes = "";
recordButton = (Button) findViewById(R.id.recordButton);
// Site spinner, site adapter
if (remote_db) { siteList = db.getSites(null); }
siteList.add(0, new Pair<>("Site", "0"));
siteSpinner = (Spinner) findViewById(R.id.selectSite);
final ArrayAdapter<String> siteAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, siteList);
siteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
siteAdapter = new DropdownListAdapter(this);
siteAdapter.addItems(siteList);
siteSpinner.setAdapter(siteAdapter);
// Sector spinner, sector adapter
sectorSpinner = (Spinner) findViewById(R.id.selectSector);
sectorList.add(0, "Sector");
final ArrayAdapter<String> sectorAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, sectorList);
sectorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sectorAdapter = new DropdownListAdapter(this);
sectorList.add(0, new Pair<>("Sector", "0"));
sectorAdapter.addItems(sectorList);
sectorSpinner.setAdapter(sectorAdapter);
// Spot spinner, spot adapter
spotSpinner = (Spinner) findViewById(R.id.selectSpot);
spotList.add(0, "Spot");
final ArrayAdapter<String> spotAdapter = new ArrayAdapter<>(this,
......@@ -62,29 +87,30 @@ public class BenchSampleActivity extends Activity {
spotAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spotSpinner.setAdapter(spotAdapter);
// Platform spinner, platform adapter
platformSpinner = (Spinner) findViewById(R.id.selectPlatform);
platformList.add(0, "Platform");
if (remote_db){
platformList.addAll(db.getPlatforms());
}
if (remote_db) { platformList.addAll(db.getPlatforms()); }
ArrayAdapter<String> platformAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, platformList);
platformAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
platformSpinner.setAdapter(platformAdapter);
// Sensor spinner, sensor adapter
sensorSpinner = (Spinner) findViewById(R.id.selectSensor);
sensorList.add(0, "Sensor");
final ArrayAdapter<String> sensorAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, sensorList);
sensorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sensorAdapter = new DropdownListAdapter(this);
sensorList.add(0, new Pair<>("Sensor", "0"));
sensorAdapter.addItems(sensorList);
sensorSpinner.setAdapter(sensorAdapter);
siteSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != 0) {
site = parent.getItemAtPosition(position).toString();
sectorList.addAll(db.getSectors(site));
sitePair = (Pair<String, String>) siteAdapter.getItem(position);
site = sitePair.first;
sector = null;
sectorAdapter.addItems(db.getSectors(site));
}
}
......@@ -98,7 +124,10 @@ public class BenchSampleActivity extends Activity {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != 0) {
sector = parent.getItemAtPosition(position).toString();
sectorPair = (Pair<String, String>) sectorAdapter.getItem(position);
sector = sectorPair.first;
Log.d(TAG, "Site: " + site + " Sector: " + sector);
spot = null;
spotList.addAll(db.getSpots(site, sector));
}
}
......@@ -128,8 +157,25 @@ public class BenchSampleActivity extends Activity {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != 0) {
platform = parent.getItemAtPosition(position).toString();
Log.d(TAG, platform);
sensorList.addAll(db.getSensors(platform));
sensorName = null;
sensorAdapter.addItems(db.getSensors(platform));
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
sensorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != 0) {
sensorPair = (Pair<String,String>) sensorAdapter.getItem(position);
sensorName = sensorPair.first;
sensor = new aSensor(sensorPair.second, sensorPair.first);
sensor.setPlatform(platform);
}
}
......@@ -139,11 +185,139 @@ public class BenchSampleActivity extends Activity {
}
});
sensorValue = (EditText) findViewById(R.id.sensorValue);
sensorValue.requestFocus();
sensorQuality = (EditText) findViewById(R.id.sensorQuality);
sensorQuality.requestFocus();
readingNotes = (EditText) findViewById(R.id.readingNotes);
readingNotes.requestFocus();
notesET = (EditText) findViewById(R.id.readingNotes);
notesET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
readingNotes = s.toString();
}
});
valueET = (EditText) findViewById(R.id.sensorValue);
valueET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
sensorValue = s.toString();
Log.d(TAG, "Sensor Value: " + sensorValue);
if (!sensorValue.equals("")){
sensor.setLastValue(new float[] {Float.parseFloat(sensorValue)}, 1);
}
}
});
qualityET = (EditText) findViewById(R.id.sensorQuality);
qualityET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
sensorQuality = s.toString();
if (!sensorQuality.equals("")) {
sensor.setLastValueQuality(Float.parseFloat(sensorQuality));
}
}
});
recordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkFields()) {
recordSample();
} else {
makeToast("Make sure you've entered or selected all of the appropriate information" +
" for this sample.");
}
}
});
}
public boolean checkFields() {
if ((site == null) || (sector == null) || (spot == null) || (platform == null) ||
(sensor == null) || (sensorValue == null) || (sensorQuality == null)) {
return false;
} else {
return true;
}
}
public void makeToast(String message) {
Toast t = Toast.makeText(this, message, Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
t.show();
}
public void recordSample() {
Map<String, Float> geo = new HashMap<>();
if (!(geo = findGeoInfo()).isEmpty()) {
double[] geoInfo = new double[] {
geo.get("latitude"), geo.get("longitude"), geo.get("elevation")
};
boolean success = db.addReading(sensor, sensor.getLastValues()[0], sensor.getLastValueQuality(), getTimestamp(),
site, sector, spot, geoInfo, tripName, geo.get("accuracy"), Math.round(geo.get("satellites")), readingNotes, "", table);
if (success) {
makeToast("success!");
} else {
makeToast("not successful");
}
}
}
public String getTimestamp(){
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
return s.format(new Date());
}
public Map<String, Float> findGeoInfo() {
Map<String,Float> geo = new HashMap<>();
Cursor c = db.query(ReadingsDatabase.T_READING,
"siteID = ? and sectorID = ? and spotID = ?",
new String[] {
((Pair<String,String>) (siteAdapter.getItem(siteAdapter.getIndexOf(sitePair)))).second,
((Pair<String,String>) (sectorAdapter.getItem(sectorAdapter.getIndexOf(sectorPair)))).second,
spot }
);
c.moveToFirst();
if (c.getCount() == 0) {
makeToast("Unfortunately, I can't find a latitude and longitude for this trip, site, sector and spot you selected.");
} else {
geo.put("latitude", c.getFloat(c.getColumnIndex("latitude")));
geo.put("longitude", c.getFloat(c.getColumnIndex("longitude")));
geo.put("elevation", c.getFloat(c.getColumnIndex("elevation")));
geo.put("accuracy", c.getFloat(c.getColumnIndex("accuracy")));
geo.put("satellites", (float) (c.getInt(c.getColumnIndex("satellites"))));
}
c.close();
return geo;
}
}
package fieldscience.cs.earlham.edu.fieldday;
import android.app.Activity;
import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class DropdownListAdapter extends BaseAdapter {
private ArrayList<Pair<String, String>> dropdownList;
private Context context;
public DropdownListAdapter(Context context) {
super();
dropdownList = new ArrayList<>();
this.context = context;
}
public void addItems(List<Pair<String, String>> items) {
dropdownList.addAll(items);
}
public void addItem(Pair<String, String> item) {
dropdownList.add(item);
}
public int getIndexOf(Pair<String, String> item){
return dropdownList.indexOf(item);
}
@Override
public int getCount(){
return dropdownList.size();
}
@Override
public Object getItem(int position){
return dropdownList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem holder;
Pair<String, String> item = (Pair<String, String>) getItem(position);
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.dropdown_item, parent, false);
holder = new ViewHolderItem();
holder.itemName = (TextView) view.findViewById(R.id.itemName);
view.setTag(holder);
} else {
holder = (ViewHolderItem) view.getTag();
}
holder.itemName.setText(item.first); // the String
return view;
}
public class ViewHolderItem {
TextView itemName;
ViewHolderItem() {
// empty constructor
}
}
}
......@@ -9,6 +9,7 @@ import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;
import android.util.Pair;
import java.io.File;
import java.io.FileInputStream;
......@@ -444,35 +445,36 @@ public class ReadingsDatabase extends SQLiteOpenHelper {
return values;
}
public List<String> getSectors(String site){
public List<Pair<String, String>> getSectors(String site){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = null;
List<String> values = new ArrayList<>();
List<Pair<String, String>> values = new ArrayList<>();
if (site != null){
c = db.query(T_SITE, new String[]{C_SITEID}, "siteName = ?", new String[]{site},
null, null, null);
c.moveToFirst();
int id = c.getInt(c.getColumnIndex(C_SITEID));
c.close();
c = db.query(T_SECTOR, new String[]{C_SECTORNAME}, "siteID = ?", new String[]{Integer.toString(id)},
c = db.query(T_SECTOR, new String[]{C_SECTORNAME, C_SECTORID}, "siteID = ?", new String[]{Integer.toString(id)},
null, null, null);
} else {
c = db.query(T_SECTOR, new String[]{C_SECTORNAME}, null, null, null, null, null);
c = db.query(T_SECTOR, new String[]{C_SECTORNAME, C_SECTORID}, null, null, null, null, null);
}
while (c.moveToNext()) {
values.add(c.getString(c.getColumnIndex("sectorName")));
values.add(Pair.create(
c.getString(c.getColumnIndex(C_SECTORNAME)), Integer.toString(c.getInt(c.getColumnIndex(C_SECTORID)))));
}
c.close();
return values;
}
public List<String> getSites(String trip){
public List<Pair<String, String>> getSites(String trip){
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {"siteName"};
Cursor c = db.query(T_SITE, columns, null, null, null, null, null);
List<String> values = new ArrayList<String>();
Cursor c = db.query(T_SITE, new String[]{C_SITENAME, C_SITEID}, null, null, null, null, null);
List<Pair<String, String>> values = new ArrayList<>();
while (c.moveToNext()) {
values.add(c.getString(c.getColumnIndex("siteName")));
values.add(Pair.create(
c.getString(c.getColumnIndex(C_SITENAME)), Integer.toString(c.getInt(c.getColumnIndex(C_SITEID)))));
}
c.close();
return values;
......@@ -489,9 +491,9 @@ public class ReadingsDatabase extends SQLiteOpenHelper {
return values;
}
public List<String> getSensors(String platform){
public List<Pair<String,String>> getSensors(String platform){
SQLiteDatabase db = this.getReadableDatabase();
List<String> values = new ArrayList<>();
List<Pair<String,String>> values = new ArrayList<>();
Cursor c = null;
if (platform != null){
c = db.query(T_PLATFORM, new String[]{C_PLATFORMID}, "platformName = ?", new String[]{platform},
......@@ -500,14 +502,15 @@ public class ReadingsDatabase extends SQLiteOpenHelper {
String platformId = c.getString(c.getColumnIndex(C_PLATFORMID));
Log.d(TAG, platformId);
c.close();
c = db.query(T_SENSOR, new String[]{C_SENSORTYPE}, "platformID = ?", new String[]{platformId},
c = db.query(T_SENSOR, new String[]{C_SENSORTYPE, C_SENSORID}, "platformID = ?", new String[]{platformId},
null, null, null);
} else {
c = db.query(T_SENSOR, new String[]{C_SENSORTYPE}, null, null, null, null, null);
c = db.query(T_SENSOR, new String[]{C_SENSORTYPE, C_SENSORID}, null, null, null, null, null);
}
while (c.moveToNext()){
Log.d(TAG, Integer.toString(c.getCount()));
values.add(c.getString(c.getColumnIndex("sensorType")));
values.add(Pair.create(
c.getString(c.getColumnIndex("sensorType")), c.getString(c.getColumnIndex("sensorID"))));
}
c.close();
return values;
......
......@@ -23,6 +23,7 @@ import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.Pair;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
......@@ -384,8 +385,8 @@ public class SensorSampleActivity extends Activity {
}
};
List<String> siteList = new ArrayList<String>();
List<String> sectorList = new ArrayList<String>();
List<Pair<String, String>> siteList = new ArrayList<>();
List<Pair<String, String>> sectorList = new ArrayList<>();
if (remote_db){
siteList = db.getSites(null);
sectorList = db.getSectors(null);
......@@ -395,14 +396,13 @@ public class SensorSampleActivity extends Activity {
}
ArrayAdapter<String> siteAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, siteList);
siteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final DropdownListAdapter siteAdapter = new DropdownListAdapter(this);
siteAdapter.addItems(siteList);
siteSpinner.setAdapter(siteAdapter);
siteSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
site = parent.getItemAtPosition(position).toString();
site = ((Pair<String, String>) siteAdapter.getItem(position)).first;
if (sampleService != null) {
sampleService.onSiteChanged(site);
}
......@@ -414,14 +414,13 @@ public class SensorSampleActivity extends Activity {
}
});
ArrayAdapter<String> sectorAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sectorList);
sectorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final DropdownListAdapter sectorAdapter = new DropdownListAdapter(this);
sectorAdapter.addItems(sectorList);
sectorSpinner.setAdapter(sectorAdapter);
sectorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
sector = parent.getItemAtPosition(position).toString();
sector = ((Pair<String, String>) sectorAdapter.getItem(position)).first;
if (sampleService != null) {
sampleService.onSectorChanged(sector);
}
......