From 677fde9c1be61409a450fab3001b323fad427e36 Mon Sep 17 00:00:00 2001 From: WebManDev Date: Sat, 17 Jan 2026 17:18:20 -0500 Subject: [PATCH] added timeLatency --- .../src/main/java/com/aware/TimeLatency.java | 102 ++++++++++++++++++ .../main/java/com/aware/TimeLatencyTest.java | 67 ++++++++++++ .../java/com/aware/tests/TestActivity.java | 8 ++ .../src/main/res/layout/activity_test.xml | 5 + 4 files changed, 182 insertions(+) create mode 100644 aware-core/src/main/java/com/aware/TimeLatency.java create mode 100644 aware-tests/src/main/java/com/aware/TimeLatencyTest.java diff --git a/aware-core/src/main/java/com/aware/TimeLatency.java b/aware-core/src/main/java/com/aware/TimeLatency.java new file mode 100644 index 0000000..f2357af --- /dev/null +++ b/aware-core/src/main/java/com/aware/TimeLatency.java @@ -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 data_values = new ArrayList(); + + /** + * 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; + } +} diff --git a/aware-tests/src/main/java/com/aware/TimeLatencyTest.java b/aware-tests/src/main/java/com/aware/TimeLatencyTest.java new file mode 100644 index 0000000..a2eba78 --- /dev/null +++ b/aware-tests/src/main/java/com/aware/TimeLatencyTest.java @@ -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(); + } +} + diff --git a/aware-tests/src/main/java/com/aware/tests/TestActivity.java b/aware-tests/src/main/java/com/aware/tests/TestActivity.java index 15d24d9..53095cc 100644 --- a/aware-tests/src/main/java/com/aware/tests/TestActivity.java +++ b/aware-tests/src/main/java/com/aware/tests/TestActivity.java @@ -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); } diff --git a/aware-tests/src/main/res/layout/activity_test.xml b/aware-tests/src/main/res/layout/activity_test.xml index 4a48b69..fcda37f 100644 --- a/aware-tests/src/main/res/layout/activity_test.xml +++ b/aware-tests/src/main/res/layout/activity_test.xml @@ -33,4 +33,9 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnSentimental"/> +