diff --git a/.idea/modules.xml b/.idea/modules.xml index 4b8941c..f23dbfa 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/app/src/main/java/edu/nau/li_840a_interface/GraphManager.java b/app/src/main/java/edu/nau/li_840a_interface/GraphManager.java index d415a0c..bffdff5 100644 --- a/app/src/main/java/edu/nau/li_840a_interface/GraphManager.java +++ b/app/src/main/java/edu/nau/li_840a_interface/GraphManager.java @@ -6,14 +6,19 @@ import android.media.RingtoneManager; import android.media.Ringtone; import android.net.Uri; +import android.util.Log; import android.view.View; import android.widget.TextView; +import android.widget.Toast; + import com.jjoe64.graphview.GraphView; import java.util.ArrayList; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; +import java.util.concurrent.TimeUnit; + public class GraphManager implements Runnable { @@ -35,6 +40,7 @@ public class GraphManager implements Runnable private TextView finalbutton; private TextView warnDisplay; private String warnString; + public Boolean runningTest = false; private long startTime; private long lastruntime; private long nowruntime; @@ -49,6 +55,7 @@ public class GraphManager implements Runnable private String countdown; public boolean countdownNotified; + /////////////// // CONSTANTS // /////////////// @@ -62,6 +69,7 @@ public class GraphManager implements Runnable public GraphManager(Activity activity, GraphView[] graphIds, TextView textIds[]) { + Date time; // The activity is required for updating items on the UI thread, so we save it here as a @@ -112,7 +120,6 @@ public GraphManager(Activity activity, GraphView[] graphIds, TextView textIds[]) */ public void run() { - String data; Date time; long currentTime; @@ -120,25 +127,67 @@ public void run() // GET INSTRUMENT data = this.getData(); + + + + while (data == null){ // Wait for the first data to arrive to get instrument try { Thread.sleep(SLEEP_TIME); } catch (Exception exception) {} - data = this.getData(); + + if(this.getData() == null){ + Log.e("Data: ", "Most assuredly null"); + }else{ + Log.e("Data was not null", "So annoying"); + Log.e("Data: ", this.getData()); + } + + + if(this.getData() == "I like candy"){ + this.runningTest = true; + Log.e("Set runningTest to", "true"); + + + } + + if(this.getData() == "I hate candy"){ + this.runningTest = false; + Log.e("Set runningTest to", "false"); + + + } + + if(this.runningTest){ + data = this.mockData(); + }else{ + data = this.getData(); + } + } - // In case the instrument is just connected, there might be some gibberish in the data buffer... we drop the first string and use the second one to identify instrument (and fields in future versions) - while (!(data.substring(0,1).equals("<"))){ // Wait for the first data to arrive to get instrument - try { Thread.sleep(SLEEP_TIME); } catch (Exception exception) {} - data = this.getData(); + Log.e("Mocked data?", data); + + if(!runningTest){ + // In case the instrument is just connected, there might be some gibberish in the data buffer... we drop the first string and use the second one to identify instrument (and fields in future versions) + while (!(data.substring(0,1).equals("<"))){ // Wait for the first data to arrive to get instrument + try { Thread.sleep(SLEEP_TIME); + } catch (Exception exception) { + + } + data = this.getData(); + + } } + try { instrument = data.substring(data.lastIndexOf('/') + 1).toUpperCase(); // Use the last element in datastring, not the first - instrument = instrument.substring(0, instrument.length() - 1); // seems to be more stable if incomplete strings are received. - } catch (Exception exception) { instrument="unknown";} + instrument = instrument.substring(0, instrument.length() - 1); // seems to be more stable if incomplete strings are received. + } catch (Exception exception) { + instrument="unknown";} activity.runOnUiThread(new Runnable() { public void run() { - instrumentDisplay.setText(instrument); + instrumentDisplay.setText(instrument); } }); // END GET INSTRUMENT @@ -147,10 +196,28 @@ public void run() { // Loops until the screen is deconstructed while (running) { + if(runningTest){ + Log.e("Still running", "test"); + try{ + TimeUnit.MILLISECONDS.sleep(500); + newDataAvailable = true; + Log.e("newDataAvailable", "true"); + }catch (Exception e){ + Log.w("Couldn't sleep", e); + } + } // The following section is runs every time soon after new data is available, (depending on the settings of the LICOR, default 0.5s and SLEEP_TIME) if (newDataAvailable) { + + // Get the latest data from the instrument - data = this.getData(); + if(runningTest){ + data = this.mockData(); + }else{ + data = this.getData(); + } + Log.e("Got here", "Line 217"); + // Get the current time time = new Date(); @@ -162,6 +229,7 @@ public void run() { nowruntime = (timeDiff/1000)*1000; if(lastruntime", data.split("")[1].split("")[0]); output[0] = stringToFloat(data.split("")[1].split("")[0]); } catch(Exception exception) diff --git a/app/src/main/java/edu/nau/li_840a_interface/dataMocker.java b/app/src/main/java/edu/nau/li_840a_interface/dataMocker.java new file mode 100644 index 0000000..40a0dda --- /dev/null +++ b/app/src/main/java/edu/nau/li_840a_interface/dataMocker.java @@ -0,0 +1,57 @@ +package edu.nau.li_840a_interface; +import java.util.Random; +import android.os.Handler; +import android.os.Binder; +import android.os.IBinder; + +public class dataMocker { + + static final String li820_start = ""; + static final String li820_end = ""; + static final String data_start = ""; + static final String data_end = ""; + static final String celltemp_start = ""; + static final String celltemp_end = ""; + static final String cellpress_start = ""; + static final String cellpress_end = ""; + static final String c02_start = ""; + static final String c02_end = ""; + static final String c02abs_start = ""; + static final String c02abs_end = ""; + static final String ivolt_start = ""; + static final String ivolt_end = ""; + static final String raw_start = ""; + static final String raw_end = ""; + + private Handler mHandler; + + /* Example Licor XML + ```4.7040696e17.8869514e15.9971117e2 + 8.6043262e-21.5730590e12360202,2642891``` + */ + + public void setHandler(Handler mHandler) { + this.mHandler = mHandler; + } + + public static String generateXML(){ + String mock_xml = ""; + int counter = 0; + Random rand = new Random(); + int n = rand.nextInt(50) + 1; //50 is the maximum and the 1 is our minimum + + while(counter < n){ + mock_xml += li820_start + data_start + celltemp_start + "4.7040696e1" + celltemp_end + cellpress_start + + "7.8869514e1" + cellpress_end + c02_start + "5.9971117e2" + c02_end + c02abs_start + "8.6043262e-2" + + c02abs_end + ivolt_start + "1.5730590e1" + ivolt_end + raw_start + "2360202,2642891" + raw_end + data_end + li820_end; + counter++; + } + return mock_xml; + } + + public class DMBinder extends Binder { + public dataMocker getService() { + return dataMocker.this; + } + } +} diff --git a/app/src/main/java/edu/nau/li_840a_interface/graphScreen.java b/app/src/main/java/edu/nau/li_840a_interface/graphScreen.java index 4efab6b..9163ef0 100644 --- a/app/src/main/java/edu/nau/li_840a_interface/graphScreen.java +++ b/app/src/main/java/edu/nau/li_840a_interface/graphScreen.java @@ -47,6 +47,7 @@ public class graphScreen extends AppCompatActivity { public static int NOT_CONNECTED = 0; // Where? public static int USB = 1; // Where? public static int BLUETOOTH = 2; // Where? + public static int DATAMOCKER = 3; // CONNECTION STATUS public static int device_connection=NOT_CONNECTED; // Is a Sensor connected? public static boolean bt_established=false; @@ -150,6 +151,7 @@ public void setNotConnected(){ button.setText("Connect"); device_connection=NOT_CONNECTED; bt_established=false; + manager.runningTest = false; } public void setConnected(int connection){ @@ -163,6 +165,12 @@ public void setConnected(int connection){ button.setText("Disconnect USB"); device_connection=USB; } + if (connection==DATAMOCKER){ + button.setText("Disconnect DataMocker"); + device_connection=DATAMOCKER; + manager.updateData("I like candy"); + manager.runningTest = true; + } } public void connectDevice() { @@ -182,6 +190,15 @@ public void connectDevice() { Resources res = getResources(); builder.setIcon(res.getDrawable(R.drawable.dog)); + + builder.setNeutralButton("On a test leash\n(Data Mocker)", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + // Data Mocker Functionality + setConnected(DATAMOCKER); + } + }); + // If the yes button is pressed builder.setPositiveButton("On a leash\n(USB)", new DialogInterface.OnClickListener() { @Override @@ -235,8 +252,8 @@ public void showCO2(View view) { graphIds[0].setLayoutParams(new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT)); + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT)); graphIds[1].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); graphIds[2].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); graphIds[3].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); @@ -251,8 +268,8 @@ public void showH2O(View view) { graphIds[1].setLayoutParams(new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT)); + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT)); graphIds[2].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); graphIds[3].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); graphIds[0].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); @@ -266,8 +283,8 @@ public void showTemp(View view) { graphIds[2].setLayoutParams(new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT)); + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT)); graphIds[3].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); graphIds[0].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); graphIds[1].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); @@ -281,8 +298,8 @@ public void showPres(View view) { graphIds[3].setLayoutParams(new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.MATCH_PARENT)); + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT)); graphIds[0].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); graphIds[1].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); graphIds[2].setLayoutParams(new LinearLayout.LayoutParams(0, 0)); @@ -478,41 +495,41 @@ public void onClick(DialogInterface dialogInterface, int i) { */ public void finalize(View view) { - if (!loggingstopped ) { - AlertDialog.Builder alert = new AlertDialog.Builder(this); - alert.setTitle("Log duration"); - alert.setMessage("Intended log duration (in seconds):"); - - // Set an EditText view to get user input - final EditText seconds = new EditText(this); - seconds.setRawInputType(InputType.TYPE_CLASS_NUMBER); - seconds.setMaxLines(1); - seconds.setImeOptions(EditorInfo.IME_ACTION_DONE); - alert.setView(seconds); - - alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - String value = seconds.getText().toString(); - if (!value.equals("")) { - logduration = Integer.parseInt(value) * 1000; + if (!loggingstopped ) { + AlertDialog.Builder alert = new AlertDialog.Builder(this); + alert.setTitle("Log duration"); + alert.setMessage("Intended log duration (in seconds):"); + + // Set an EditText view to get user input + final EditText seconds = new EditText(this); + seconds.setRawInputType(InputType.TYPE_CLASS_NUMBER); + seconds.setMaxLines(1); + seconds.setImeOptions(EditorInfo.IME_ACTION_DONE); + alert.setView(seconds); + + alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + String value = seconds.getText().toString(); + if (!value.equals("")) { + logduration = Integer.parseInt(value) * 1000; + } + countdown_label = String.format("Log duration\n%d:%02d", logduration / (60 * 1000) % 60, logduration / 1000 % 60); + textIds[7].setText(countdown_label); + return; } - countdown_label = String.format("Log duration\n%d:%02d", logduration / (60 * 1000) % 60, logduration / 1000 % 60); - textIds[7].setText(countdown_label); - return; - } - }); + }); - alert.setNegativeButton("Cancel", - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - // TODO Auto-generated method stub - return; - } - }); - alert.show(); + alert.setNegativeButton("Cancel", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + // TODO Auto-generated method stub + return; + } + }); + alert.show(); - return; - } + return; + } loggingstopped=false; String reading; @@ -592,10 +609,10 @@ public void onClick(DialogInterface dialog, int which) { // Construct the CSV file content metaString = "Instrument,Operator Name,Site Name,Sample ID,Temperature,Comments,Time and Date,Longitude," + - "Latitude,Elevation,R Squared,Regression Slope,Standard Error,X Start Range,X End Range,AppVersion\n" + - metaInstrument + "," +metaOpName + "," + metaSite + "," + metaSampleId + "," + metaTemp + "," + - metaComments + "," + metaTime + "," + metaLong + "," + metaLat + "," + - metaElevation + "," + rSquared + "," + regSlope + "," + stdError + "," + + "Latitude,Elevation,R Squared,Regression Slope,Standard Error,X Start Range,X End Range,AppVersion\n" + + metaInstrument + "," +metaOpName + "," + metaSite + "," + metaSampleId + "," + metaTemp + "," + + metaComments + "," + metaTime + "," + metaLong + "," + metaLat + "," + + metaElevation + "," + rSquared + "," + regSlope + "," + stdError + "," + df.format(firstSecond) + "," + df.format(lastSecond) + "," + getString(R.string.version_name); @@ -667,7 +684,7 @@ public void onReceive(Context context, Intent intent) { Toast.makeText(getBaseContext(), "Device does not support Bluetooth", Toast.LENGTH_SHORT).show(); break; } - // ADD BT_BROADCASTS HERE + // ADD BT_BROADCASTS HERE } else if (device_connection == USB) { switch (intent.getAction()) { @@ -703,16 +720,16 @@ else if (device_connection == USB) { private BTService btService; private UsbService usbService; - // private graphScreen.MyHandler mHandler; - private graphScreen.MyBTHandler mbtHandler; - private graphScreen.MyUSBHandler musbHandler; + // private graphScreen.MyHandler mHandler; + private graphScreen.MyBTHandler mbtHandler; + private graphScreen.MyUSBHandler musbHandler; private final ServiceConnection btConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { - btService = ((BTService.BTBinder) arg1).getService(); - btService.setHandler(mbtHandler); + btService = ((BTService.BTBinder) arg1).getService(); + btService.setHandler(mbtHandler); } @Override public void onServiceDisconnected(ComponentName arg0) { @@ -724,8 +741,8 @@ public void onServiceDisconnected(ComponentName arg0) { private final ServiceConnection usbConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { - usbService = ((UsbService.UsbBinder) arg1).getService(); - usbService.setHandler(musbHandler); + usbService = ((UsbService.UsbBinder) arg1).getService(); + usbService.setHandler(musbHandler); } @Override public void onServiceDisconnected(ComponentName arg0) { @@ -770,16 +787,16 @@ private void startBTService(Class service, ServiceConnection serviceConnectio private void startUSBService(Class service, ServiceConnection serviceConnection, Bundle extras) { if (!UsbService.SERVICE_CONNECTED ) { - Intent startService = new Intent(this, service); - if (extras != null && !extras.isEmpty()) { - Set keys = extras.keySet(); - for (String key : keys) { - String extra = extras.getString(key); - startService.putExtra(key, extra); - } + Intent startService = new Intent(this, service); + if (extras != null && !extras.isEmpty()) { + Set keys = extras.keySet(); + for (String key : keys) { + String extra = extras.getString(key); + startService.putExtra(key, extra); } - startService(startService); } + startService(startService); + } Intent bindingIntent = new Intent(this, service); bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE); } diff --git a/app/src/main/java/edu/nau/li_840a_interface/metaData.java b/app/src/main/java/edu/nau/li_840a_interface/metaData.java index 37cfc87..fa0d0c5 100644 --- a/app/src/main/java/edu/nau/li_840a_interface/metaData.java +++ b/app/src/main/java/edu/nau/li_840a_interface/metaData.java @@ -380,27 +380,23 @@ private void setPic() { int photoH = bmOptions.outHeight; // Handling Error so it only accepts Portrait Images // If statement accepts Portrait Images Images - if (photoW < photoH) { + // Determine how much to scale down the image - // Determine how much to scale down the image + int scaleFactor = Math.max(photoW / targetW, photoH / targetH); - int scaleFactor = Math.min(photoW / targetW, photoH / targetH); + // Decode the image file into a Bitmap sized to fill the View + bmOptions.inJustDecodeBounds = false; + bmOptions.inSampleSize = scaleFactor; + bmOptions.inPurgeable = true; - // Decode the image file into a Bitmap sized to fill the View - bmOptions.inJustDecodeBounds = false; - bmOptions.inSampleSize = scaleFactor; - bmOptions.inPurgeable = true; + image = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); + //Toast.makeText(metaData.this, "Unscaled: " + image.getByteCount(), Toast.LENGTH_SHORT).show(); + //scaleImage(); + //Toast.makeText(metaData.this, "Scaled: " + image.getByteCount(), Toast.LENGTH_SHORT).show(); + imagePreview.setImageBitmap(image); - image = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); - //Toast.makeText(metaData.this, "Unscaled: " + image.getByteCount(), Toast.LENGTH_SHORT).show(); - //scaleImage(); - //Toast.makeText(metaData.this, "Scaled: " + image.getByteCount(), Toast.LENGTH_SHORT).show(); - imagePreview.setImageBitmap(image); - } else { - Toast.makeText(metaData.this, "Please change the orientation and take a new photo!", Toast.LENGTH_LONG).show(); - } } diff --git a/build.gradle b/build.gradle index 72aeec4..c935b4f 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.2.0' + classpath 'com.android.tools.build:gradle:3.2.1' // NOTE: Do not place your application dependencies here; they belong