Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/src/main/java/io/pslab/activity/LuxMeterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public Fragment getSensorFragment() {
@Override
public void getDataFromDataLogger() {
if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean(KEY_LOG)) {
playingData = true;
//playingData = true;
viewingData = true;
recordedLuxData = LocalDataLog.with()
.getBlockOfLuxRecords(getIntent().getExtras().getLong(DATA_BLOCK));
String title = titleFormat.format(recordedLuxData.get(0).getTime());
Expand Down
147 changes: 102 additions & 45 deletions app/src/main/java/io/pslab/fragment/LuxMeterDataFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -88,7 +89,6 @@ private enum LUX_SENSOR {INBUILT_SENSOR, BH1750_SENSOR, TSL2561_SENSOR}
private LuxData sensorData;
private float currentMin = 10000;
private float currentMax = 0;
private boolean playComplete = false;
private YAxis y;
private Unbinder unbinder;
private long previousTimeElapsed = (System.currentTimeMillis() - startTime) / updatePeriod;
Expand Down Expand Up @@ -130,7 +130,12 @@ public void onResume() {
recordedLuxArray = new ArrayList<>();
resetInstrumentData();
playRecordedData();

} else if (luxSensor.viewingData) {
sensorLabel.setText(getResources().getString(R.string.recorder));
recordedLuxArray = new ArrayList<>();
resetInstrumentData();
plotAllRecordedData();
// TODO: Display the whole graph with filled data
} else if (!luxSensor.isRecording) {
updateGraphs();
sum = 0;
Expand Down Expand Up @@ -158,6 +163,41 @@ public void onDestroyView() {
unbinder.unbind();
}

private void plotAllRecordedData() {
recordedLuxArray.addAll(luxSensor.recordedLuxData);
if (recordedLuxArray.size() != 0) {
for (LuxData d: recordedLuxArray) {
if (currentMax < d.getLux()) {
currentMax = d.getLux();
}
if (currentMin > d.getLux()) {
currentMin = d.getLux();
}
Entry entry = new Entry((float) (d.getTime() - d.getBlock()) / 1000, d.getLux());
entries.add(entry);
sum += entry.getY();
}
y.setAxisMaximum(currentMax);
y.setAxisMinimum(currentMin);
y.setLabelCount(10);
statMax.setText(String.valueOf(currentMax));
statMin.setText(String.valueOf(currentMin));
statMean.setText(String.format(Locale.getDefault(), "%.2f", (sum / recordedLuxArray.size())));

LineDataSet dataSet = new LineDataSet(entries, getString(R.string.lux));
dataSet.setDrawCircles(false);
dataSet.setDrawValues(false);
dataSet.setLineWidth(2);
LineData data = new LineData(dataSet);

mChart.setData(data);
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(80);
mChart.moveViewToX(data.getEntryCount());
mChart.invalidate();
}
}

private void playRecordedData() {
recordedLuxArray.addAll(luxSensor.recordedLuxData);
try {
Expand Down Expand Up @@ -187,60 +227,77 @@ public void run() {
handler.post(new Runnable() {
@Override
public void run() {
try {
playComplete = false;
LuxData d = recordedLuxArray.get(turns);
turns++;
if (currentMax < d.getLux()) {
currentMax = d.getLux();
statMax.setText(String.valueOf(d.getLux()));
if (luxSensor.playingData) {
try {
LuxData d = recordedLuxArray.get(turns);
turns++;
if (currentMax < d.getLux()) {
currentMax = d.getLux();
statMax.setText(String.valueOf(d.getLux()));
}
if (currentMin > d.getLux()) {
currentMin = d.getLux();
statMin.setText(String.valueOf(d.getLux()));
}
y.setAxisMaximum(currentMax);
y.setAxisMinimum(currentMin);
y.setLabelCount(10);
lightMeter.setWithTremble(false);
lightMeter.setSpeedAt(d.getLux());

Entry entry = new Entry((float) (d.getTime() - d.getBlock()) / 1000, d.getLux());
entries.add(entry);
count++;
sum += entry.getY();
statMean.setText(String.format(Locale.getDefault(), "%.2f", (sum / count)));

LineDataSet dataSet = new LineDataSet(entries, getString(R.string.lux));
dataSet.setDrawCircles(false);
dataSet.setDrawValues(false);
dataSet.setLineWidth(2);
LineData data = new LineData(dataSet);

mChart.setData(data);
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(80);
mChart.moveViewToX(data.getEntryCount());
mChart.invalidate();
} catch (IndexOutOfBoundsException e) {
graphTimer.cancel();
graphTimer = null;
turns = 0;
luxSensor.playingData = false;
luxSensor.startedPlay = false;
luxSensor.invalidateOptionsMenu();
}
if (currentMin > d.getLux()) {
currentMin = d.getLux();
statMin.setText(String.valueOf(d.getLux()));
}
y.setAxisMaximum(currentMax);
y.setAxisMinimum(currentMin);
y.setLabelCount(10);
lightMeter.setWithTremble(false);
lightMeter.setSpeedAt(d.getLux());

Entry entry = new Entry((float) (d.getTime() - d.getBlock()) / 1000, d.getLux());
entries.add(entry);
count++;
sum += entry.getY();
statMean.setText(String.format(Locale.getDefault(), "%.2f", (sum / count)));

LineDataSet dataSet = new LineDataSet(entries, getString(R.string.lux));
dataSet.setDrawCircles(false);
dataSet.setDrawValues(false);
dataSet.setLineWidth(2);
LineData data = new LineData(dataSet);

mChart.setData(data);
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(80);
mChart.moveViewToX(data.getEntryCount());
mChart.invalidate();
} catch (IndexOutOfBoundsException e) {
graphTimer.cancel();
playComplete = true;
}
}
});
}
}, 0, timeGap);
}

public void saveGraph() {
if (playComplete) {
mChart.saveToGallery(luxSensor.dateFormat.format(recordedLuxArray.get(0).getTime()), 100);
Toast.makeText(getActivity(), "Graph saved to gallery", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Wait until the play is complete", Toast.LENGTH_LONG).show();
public void playData() {
resetInstrumentData();
luxSensor.startedPlay = true;
try {
if (recordedLuxArray.size() > 1) {
LuxData i = recordedLuxArray.get(1);
long timeGap = i.getTime() - i.getBlock();
processRecordedData(timeGap);
} else {
processRecordedData(0);
}
} catch (IllegalArgumentException e) {
Toast.makeText(getActivity(),
getActivity().getResources().getString(R.string.no_data_fetched), Toast.LENGTH_SHORT).show();
}
}

public void saveGraph() {
// Todo: Implement export here
}

private void setupInstruments() {
lightMeter.setMaxSpeed(10000);

Expand Down
49 changes: 45 additions & 4 deletions app/src/main/java/io/pslab/models/PSLabSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -29,6 +32,8 @@

import org.json.JSONArray;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;

Expand Down Expand Up @@ -61,6 +66,8 @@ public abstract class PSLabSensor extends AppCompatActivity {
public boolean checkGPSOnResume = false;
public boolean writeHeaderToFile = true;
public boolean playingData = false;
public boolean viewingData = false;
public boolean startedPlay = false;

public CoordinatorLayout sensorParentView;
public BottomSheetBehavior bottomSheetBehavior;
Expand Down Expand Up @@ -240,12 +247,13 @@ private void fillUpFragment() {
}

private void setUpMenu(Menu menu) {
if (playingData) {
if (playingData || viewingData) {
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setVisible(false);
}
}
menu.findItem(R.id.save_graph).setVisible(playingData);
menu.findItem(R.id.save_graph).setVisible(viewingData || playingData);
menu.findItem(R.id.play_data).setVisible(viewingData || playingData);
}

@Override
Expand All @@ -260,6 +268,8 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem record = menu.findItem(R.id.record_data);
record.setIcon(isRecording ? R.drawable.ic_record_stop_white : R.drawable.ic_record_white);
MenuItem play = menu.findItem(R.id.play_data);
play.setIcon(playingData ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp);
return super.onPrepareOptionsMenu(menu);
}

Expand All @@ -286,13 +296,44 @@ public boolean onOptionsItemSelected(MenuItem item) {
dataRecordingCycle();
} else {
stopRecordSensorData();
CustomSnackBar.showSnackBar(sensorParentView,
getString(R.string.data_recording_stopped), null, null);
final File logDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSVLogger.CSV_DIRECTORY + File.separator + getSensorName());
String logLocation;
try {
logLocation = getString(R.string.log_saved_directory) + logDirectory.getCanonicalPath();
} catch (IOException e) {
// This message wouldn't appear in usual cases. Added in order to handle ex:
logLocation = getString(R.string.log_saved_failed);
}
CustomSnackBar.showSnackBar(sensorParentView, logLocation, null, null);
CustomSnackBar.snackbar.setDuration(Snackbar.LENGTH_INDEFINITE);
CustomSnackBar.snackbar.setAction(getString(R.string.open), new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri selectedUri = Uri.parse(logDirectory.getAbsolutePath());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
if (intent.resolveActivityInfo(getPackageManager(), 0) != null) {
startActivity(intent);
}
}
});
isRecording = false;
prepareMarkers();
}
invalidateOptionsMenu();
break;
case R.id.play_data:
playingData = !playingData;
if (!startedPlay) {
if (getSensorFragment() instanceof LuxMeterDataFragment) {
((LuxMeterDataFragment) getSupportFragmentManager()
.findFragmentByTag(getSensorName())).playData();
}
}
invalidateOptionsMenu();
break;
case R.id.show_map:
if (psLabPermission.checkPermissions(PSLabSensor.this,
PSLabPermission.MAP_PERMISSION)) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/pslab/others/CSVLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CSVLogger {
private String category;
SimpleDateFormat TIME;

private static final String CSV_DIRECTORY = "PSLab";
public static final String CSV_DIRECTORY = "PSLab";

/**
* Constructor initiate logger with a category folder
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/io/pslab/others/CustomSnackBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
public class CustomSnackBar {

public static Snackbar snackbar;

public static void showSnackBar(@NonNull CoordinatorLayout holderLayout, @NonNull String displayText,
String actionText, View.OnClickListener clickListener){
Snackbar snackbar = Snackbar.make(holderLayout,displayText,Snackbar.LENGTH_LONG)
String actionText, View.OnClickListener clickListener) {
snackbar = Snackbar.make(holderLayout, displayText, Snackbar.LENGTH_LONG)
.setAction(actionText, clickListener);

snackbar.setActionTextColor(ContextCompat.getColor(holderLayout.getContext(), R.color.colorPrimary));
View sbView = snackbar.getView();
TextView textView = sbView.findViewById(android.support.design.R.id.snackbar_text);
TextView textView = sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbar.show();
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_pause_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M6,19h4L10,5L6,5v14zM14,5v14h4L18,5h-4z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_play_arrow_white_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M8,5v14l11,-7z"/>
</vector>
Loading