Skip to content
Open
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
102 changes: 102 additions & 0 deletions aware-core/src/main/java/com/aware/TimeLatency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.aware;

import android.content.ContentValues;
import com.aware.utils.Aware_Sensor;

import java.util.ArrayList;
import java.util.List;

/**
* TimeLatency Sensor
*
* Tracks the time it takes for participants to answer questions.
* Records start time when a question appears and calculates duration
* when the question is answered.
*
*
*/
public class TimeLatency extends Aware_Sensor {

// List to store timing data for multiple questions
private List<ContentValues> data_values = new ArrayList<ContentValues>();

/**
* Records the start time when a question appears/becomes visible.
*
* @param questionId The unique identifier for the question
* @return ContentValues containing the question_id and start_time
*/
public ContentValues startTime(int questionId) {
// Get current timestamp in milliseconds
long beginningTime = System.currentTimeMillis();

// Create a new ContentValues object (like a dictionary) to store this question's data
ContentValues dataForStartingTime = new ContentValues();

// Store the question ID and start time
dataForStartingTime.put("question_id", questionId);
dataForStartingTime.put("start_time", beginningTime);
dataForStartingTime.put("starting_timestamp", beginningTime);

// Add this entry to our list so we can find it later
data_values.add(dataForStartingTime);

return dataForStartingTime;
}

/**
* Helper method to search for a question's data by its ID.
*
* @param questionId The unique identifier for the question to find
* @return ContentValues for the question if found, null otherwise
*/
private ContentValues searchingQuestion(int questionId) {
// Loop through all stored questions
for (int a = 0; a < data_values.size(); a++) {
// Get the ContentValues (dictionary) for this question
ContentValues data = data_values.get(a);

// Check if this is the question we're looking for
if (data.getAsInteger("question_id") == questionId) {
return data; // Found it! Return the data
}
}
// Question not found in the list
return null;
}

/**
* Records the end time when a question is answered and calculates the duration.
* Updates the existing entry with end_time and time_taken.
*
* @param questionId The unique identifier for the question that was answered
* @return ContentValues containing all timing data (start_time, end_time, time_taken)
* Returns null if the question was not found
*/
public ContentValues questionAnswered(int questionId) {
// Get current timestamp when question is answered
long timeCompleted = System.currentTimeMillis();

// Find the existing entry for this question
ContentValues dataForCompletedTime = searchingQuestion(questionId);

// Safety check: make sure we found the question
if (dataForCompletedTime == null) {
return null; // Question not found - can't calculate time
}

// Get the start time we stored earlier
long beginningTime = dataForCompletedTime.getAsLong("start_time");

// Calculate how long it took to answer (in milliseconds)
long timeTaken = timeCompleted - beginningTime;

// Update the existing entry with end time and duration
dataForCompletedTime.put("end_time", timeCompleted);
dataForCompletedTime.put("ending_timestamp", timeCompleted);
dataForCompletedTime.put("time_taken", timeTaken);

// Return the complete data (now has start_time, end_time, and time_taken)
return dataForCompletedTime;
}
}
67 changes: 67 additions & 0 deletions aware-tests/src/main/java/com/aware/TimeLatencyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.aware.tests;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.widget.Toast;
import com.aware.TimeLatency;

public class TestTimeLatency {

private TimeLatency timeLatency = new TimeLatency();
private int questionId = 1;

public void test(final Activity activity) {
// Call startTime() when question appears
timeLatency.startTime(questionId);

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Question " + questionId);
builder.setMessage("What is 2 + 2?");
builder.setCancelable(false);

builder.setPositiveButton("4", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showResult(activity, "4");
}
});

builder.setNegativeButton("5", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showResult(activity, "5");
}
});

builder.show();
}

private void showResult(final Activity activity, String answer) {
// Call questionAnswered() when user responds
ContentValues result = timeLatency.questionAnswered(questionId);

if (result == null) {
Toast.makeText(activity, "Error: Question not found", Toast.LENGTH_SHORT).show();
return;
}

long timeTaken = result.getAsLong("time_taken");
String message = String.format("Answer: %s\nTime: %.2f seconds", answer, timeTaken / 1000.0);

AlertDialog.Builder resultBuilder = new AlertDialog.Builder(activity);
resultBuilder.setTitle("Result");
resultBuilder.setMessage(message);
resultBuilder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
questionId++;
test(activity);
}
});
resultBuilder.setNegativeButton("Done", null);
resultBuilder.show();
}
}

8 changes: 8 additions & 0 deletions aware-tests/src/main/java/com/aware/tests/TestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public void onClick(View view) {
// }
// });

Button btnTimeLatency = findViewById(R.id.btn_test_timelatency);
btnTimeLatency.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TestTimeLatency testTimeLatency = new TestTimeLatency();
testTimeLatency.test(TestActivity.this);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE);
}
Expand Down
5 changes: 5 additions & 0 deletions aware-tests/src/main/res/layout/activity_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSentimental"/>
<Button
android:text="Test TimeLatency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_test_timelatency"/>
</LinearLayout>