Skip to content

Commit

Permalink
refactor: replace thread sleep with timer based solution
Browse files Browse the repository at this point in the history
  • Loading branch information
marcnause committed Nov 19, 2024
1 parent e451fa4 commit 9f416a1
Showing 1 changed file with 60 additions and 38 deletions.
98 changes: 60 additions & 38 deletions app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.pslab.sensors;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.text.Editable;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -31,8 +35,7 @@ abstract class AbstractSensorActivity extends AppCompatActivity {
private static final String KEY_TIMEGAP = TAG + "_timegap";
private static final String KEY_FLAG = TAG + "_flag";
private static final String KEY_STARTTIME = TAG + "_starttime";

private volatile boolean isRunning = true;
private static final String KEY_COUNTER = TAG + "_counter";

private int counter;
private ScienceLab scienceLab;
Expand All @@ -49,6 +52,9 @@ abstract class AbstractSensorActivity extends AppCompatActivity {
private EditText samplesEditBox;
private ImageButton playPauseButton;

private HandlerThread handlerThread;
private Handler handler;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -61,6 +67,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
timeGap = savedInstanceState.getInt(KEY_TIMEGAP);
flag = savedInstanceState.getInt(KEY_FLAG);
startTime = savedInstanceState.getLong(KEY_STARTTIME);
counter = savedInstanceState.getInt(KEY_COUNTER);
}

sensorDock = findViewById(R.id.sensor_control_dock_layout);
Expand All @@ -83,12 +90,21 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
sensorDock.setVisibility(View.VISIBLE);

scienceLab = ScienceLabCommon.scienceLab;
new Thread(getRunnable()).start();

handlerThread = new HandlerThread("MyHandlerThread");
handlerThread.start();
Looper looper = handlerThread.getLooper();
handler = new Handler(looper);

if (play) {
startTimerTask();
}
}

@Override
protected void onDestroy() {
isRunning = false;
play = false;
handlerThread.quit();
super.onDestroy();
}

Expand All @@ -104,32 +120,33 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
}

@NonNull
private Runnable getRunnable() {
private Runnable getTimerTask() {

return () -> {
while (isRunning) {
if (scienceLab.isConnected() && shouldPlay()) {
SensorDataFetch sensorDataFetch = getSensorDataFetch();
if (sensorDataFetch == null) {
Log.w(TAG, "No SensorDataFetch!");
} else {
sensorDataFetch.execute();
updateSensorDock();
}
if (flag == 0) {
startTime = System.currentTimeMillis();
flag = 1;
}
try {
Thread.sleep(timeGap);
} catch (InterruptedException e) {
Log.e(TAG, "Thread interrupted while waiting.");
}
if (play && scienceLab.isConnected() && shouldPlay()) {
if (flag == 0) {
startTime = System.currentTimeMillis();
flag = 1;
}
SensorDataFetch sensorDataFetch = getSensorDataFetch();
if (sensorDataFetch == null) {
Log.w(TAG, "No SensorDataFetch!");
} else {
sensorDataFetch.execute();
}
if (play) {
startTimerTask();
}
} else {
setPlayButton(false);
}
};
}

private void startTimerTask() {
handler.postDelayed(getTimerTask(), timeGap);
}

protected ScienceLab getScienceLab() {
return scienceLab;
}
Expand All @@ -141,23 +158,27 @@ private void setSensorDock() {

playPauseButton.setOnClickListener(v -> {
if (play && scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.circle_play_button);
play = false;
} else if (!scienceLab.isConnected()) {
playPauseButton.setImageResource(R.drawable.circle_play_button);
play = false;
} else {
playPauseButton.setImageResource(R.drawable.circle_pause_button);
play = true;
startTimerTask();
if (!indefiniteSamplesCheckBox.isChecked()) {
counter = Integer.parseInt(samplesEditBox.getText().toString());
Editable text = samplesEditBox.getText();
counter = text.length() == 0 ? 0 : Integer.parseInt(text.toString());
}
}
setPlayButton(play);
});

setPlayButton(play);

sensorDock.setVisibility(View.VISIBLE);

indefiniteSamplesCheckBox.setChecked(true);
samplesEditBox.setEnabled(false);
indefiniteSamplesCheckBox.setChecked(runIndefinitely);
samplesEditBox.setEnabled(!runIndefinitely);
samplesEditBox.setText(String.valueOf(counter));
indefiniteSamplesCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
runIndefinitely = true;
Expand Down Expand Up @@ -193,11 +214,15 @@ public void onStopTrackingTouch(SeekBar seekBar) {
});
}

private void setPlayButton(boolean isPlaying) {
playPauseButton.setImageResource(isPlaying ? R.drawable.circle_pause_button : R.drawable.circle_play_button);
}

protected boolean shouldPlay() {
if (play) {
if (indefiniteSamplesCheckBox.isChecked())
return true;
else if (counter >= 0) {
else if (counter > 0) {
counter--;
return true;
} else {
Expand All @@ -209,18 +234,15 @@ else if (counter >= 0) {
}
}

/**
* Get time of first start of sensor data capture.
*
* @return time in ms
*/
protected long getStartTime() {
return startTime;
}

protected void updateSensorDock() {
samplesEditBox.setText(String.valueOf(counter));
if (counter == 0 && !runIndefinitely) {
play = false;
playPauseButton.setImageResource(R.drawable.circle_play_button);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Expand Down

0 comments on commit 9f416a1

Please sign in to comment.