diff --git a/app/src/androidTest/java/com/example/quantify/ExperimentTest.java b/app/src/androidTest/java/com/example/quantify/ExperimentTest.java new file mode 100644 index 0000000..cd236b5 --- /dev/null +++ b/app/src/androidTest/java/com/example/quantify/ExperimentTest.java @@ -0,0 +1,155 @@ +package com.example.quantify; + +import android.app.Activity; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ListView; +import android.widget.Spinner; +import android.widget.TextView; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ActivityTestRule; +import com.robotium.solo.Solo; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * VERY IMPORTANT NOTE + * MANY OF THE TEST CASES ARE BUILT ON SPECIFIC DATA SAVED ON DATABASE AT THE TIME OF TESTING + * SO THEY MIGHT NOT WORK/GIVE ERROR LATER ON. ALSO MANY OF THE TEST CASES ARE BUILT ON + * ROBOTIUM SCREEN COORDINATES THAT HELPED TO REAL PLACE WHICH WAS BEING TESTED SO RUNNING THEM + * LATER WILL GIVE THE SAME ERROR. TO USE THESE SPECIFIC TEST CASES, KNOW WHAT YOU ARE DOING. + */ + +public class ExperimentTest { + + private Solo solo; + + @Rule + public ActivityTestRule rule = + new ActivityTestRule<>(MainActivity.class, true, true); + + /** + * Runs before all tests and creates solo instance. + * @throws Exception + */ + @Before + public void setUp() throws Exception{ + solo = new Solo(InstrumentationRegistry.getInstrumentation(),rule.getActivity()); + } + + /** + * Gets the Activity + * @throws Exception + */ + @Test + public void start() throws Exception{ + Activity activity = rule.getActivity(); + } + + /** + * This test checks if has anything on the screen at start (should be nothing as new user), + * then creates an experiment. Then checks again if he has the experiment now or not. + */ + @Test + public void publish() { + solo.assertCurrentActivity("Wrong Activity", MainActivity.class); + + Activity activity = (Activity)solo.getCurrentActivity(); + ListView listview = (ListView)solo.getView(R.id.exp_list); + int countPersonalExp = listview.getAdapter().getCount(); + + // The list should be empty + assertEquals(0, countPersonalExp); + + // Now that we are good upto this point, next, create an experiment. + solo.clickOnView(solo.getView(R.id.floatingActionButton)); + solo.enterText((EditText) solo.getView(R.id.exp_desc_fragment), "UI Testing"); + solo.clearEditText((EditText) solo.getView(R.id.exp_desc_fragment)); //Clear the EditText + solo.enterText((EditText) solo.getView(R.id.exp_min_trials), "5"); + solo.clearEditText((EditText) solo.getView(R.id.exp_min_trials)); //Clear the EditText + + // Selecting which experiment type is it + solo.clickOnView(solo.getView(R.id.exp_type_fragment, 0)); + solo.scrollToTop(); + solo.clickOnView(solo.getView(TextView.class, 0)); // We choose you Binomial! + + //solo.clickOnButton("OK"); + solo.clickOnScreen(866, 1661); + // Do we return back to main activity? + solo.assertCurrentActivity("Wrong Activity, need Main activity", MainActivity.class); + // Check if our experiment is created + countPersonalExp = listview.getAdapter().getCount(); + assertTrue(solo.waitForText("UI Testing", 1, 2000)); + } + + @Test + public void unpublish() { + solo.clickOnScreen(755, 780); + ListView listview = (ListView)solo.getView(R.id.exp_list); + int countPersonalExp = listview.getAdapter().getCount(); + // The experiment should be gone + assertEquals(0, countPersonalExp); + + } + + @Test + public void end() { + solo.clickOnScreen(289, 766); + solo.clickOnText("UI Testing"); + solo.clickOnScreen(366, 1343); + // the result is already 3 and it should not change + assertTrue(solo.waitForText("3", 1, 2000)); + } + + @Test + public void subscribe() { + solo.clickOnScreen(808, 285); + ListView listview = (ListView)solo.getView(R.id.exp_list); + + // this subscribes the second row of all experiments + //String listValue = listview.getItemAtPosition(1).toString(); + solo.clickLongOnScreen(459, 872); + + // lets see if we can find the subscribed experiment + solo.clickOnScreen(607, 2005); + assertTrue(solo.waitForText("HaloPhoneTest", 1, 2000)); + + } + + @Test + public void addTrials() { + solo.clickOnScreen(808, 285); // all experiment tab + solo.clickOnScreen(521, 1560); // experiment from list + solo.clickOnScreen(848, 1355); // proceed + + solo.clickOnScreen(541, 1688); // start + solo.clickOnScreen(366, 1349); // success + solo.clickOnScreen(550, 1563); // save + + // previous ans was 4, there is one place it should be 5 + assertTrue(solo.searchText("1")); + + } + + + + /** + * Closes the activity after each test + * @throws Exception + */ + @After + public void tearDown() throws Exception { + solo.finishOpenedActivities(); + } + +} + diff --git a/app/src/androidTest/java/com/example/quantify/MainActivityTest_2.java b/app/src/androidTest/java/com/example/quantify/MainActivityTest_2.java index 3823cf6..5556d1c 100644 --- a/app/src/androidTest/java/com/example/quantify/MainActivityTest_2.java +++ b/app/src/androidTest/java/com/example/quantify/MainActivityTest_2.java @@ -16,7 +16,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -public class MainActivityTest_2 { +public class MainActivityTest_2 { private Solo solo; @Rule public ActivityTestRule rule = @@ -56,8 +56,8 @@ public void checkList(){ //solo.clickOnButton("Add New"); //select Add New //Get view for EditText and enter the info solo.enterText((EditText) solo.getView(R.id.exp_desc_fragment), "FunctionTest1"); - solo.enterText((EditText) solo.getView(R.id.exp_user_fragment), "Halo_test_robot"); - solo.enterText((EditText) solo.getView(R.id.exp_status_fragment), "RUNNING"); + //solo.enterText((EditText) solo.getView(R.id.exp_user_fragment), "Halo_test_robot"); + //solo.enterText((EditText) solo.getView(R.id.exp_status_fragment), "RUNNING"); solo.enterText((EditText) solo.getView(R.id.exp_type_fragment), "Binomial"); //can't find the OK button in AlertDialog solo.clickOnText("Ok"); diff --git a/app/src/androidTest/java/com/example/quantify/SubscribedTest.java b/app/src/androidTest/java/com/example/quantify/SubscribedTest.java new file mode 100644 index 0000000..4797f23 --- /dev/null +++ b/app/src/androidTest/java/com/example/quantify/SubscribedTest.java @@ -0,0 +1,29 @@ +package com.example.quantify; + + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ActivityTestRule; + +import com.robotium.solo.Solo; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class SubscribedTest { + private Solo solo; + + @Rule + public ActivityTestRule rule = + new ActivityTestRule<>(SubscribedActivity.class, true, true); + + @Before + public void setUp() { + solo = new Solo(InstrumentationRegistry.getInstrumentation(), rule.getActivity()); + } + + @Test + public void start() { + SubscribedActivity activity = rule.getActivity(); + } +} diff --git a/app/src/androidTest/java/com/example/quantify/TrialTest.java b/app/src/androidTest/java/com/example/quantify/TrialTest.java new file mode 100644 index 0000000..e2959a8 --- /dev/null +++ b/app/src/androidTest/java/com/example/quantify/TrialTest.java @@ -0,0 +1,29 @@ +package com.example.quantify; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ActivityTestRule; + +import com.robotium.solo.Solo; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class TrialTest { + private Solo solo; + + @Rule + public ActivityTestRule rule = + new ActivityTestRule<>(Trial.class, true, true); + + @Before + public void setUp() { + solo = new Solo(InstrumentationRegistry.getInstrumentation(), rule.getActivity()); + } + + @Test + public void start() { + Trial activity = rule.getActivity(); + } +} + diff --git a/app/src/androidTest/java/com/example/quantify/UserProfileTest.java b/app/src/androidTest/java/com/example/quantify/UserProfileTest.java new file mode 100644 index 0000000..28cb50b --- /dev/null +++ b/app/src/androidTest/java/com/example/quantify/UserProfileTest.java @@ -0,0 +1,42 @@ +package com.example.quantify; + +import android.app.Activity; +import android.widget.EditText; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ActivityTestRule; + +import com.robotium.solo.Solo; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +public class UserProfileTest { + private Solo solo; + + @Rule + public ActivityTestRule rule = + new ActivityTestRule<>(com.example.quantify.ShowUserProfile.class, true, true); + + @Before + public void setUp() { + solo = new Solo(InstrumentationRegistry.getInstrumentation(), rule.getActivity()); + } + + @Test + public void start() { + com.example.quantify.ShowUserProfile activity = rule.getActivity(); + } + + /*@Test + public void checkList() { + solo.assertCurrentActivity("Wrong activity", ShowUserProfile.class); + solo.clickOnButton("Phone"); + solo.enterText((EditText) solo.getView(R.id.edit_text), "+1-202-555-0157"); + solo.clickOnButton("OK"); + }*/ +} + + + diff --git a/app/src/main/java/com/example/quantify/BinomialTrialActivity.java b/app/src/main/java/com/example/quantify/BinomialTrialActivity.java index 425f199..9b537dc 100644 --- a/app/src/main/java/com/example/quantify/BinomialTrialActivity.java +++ b/app/src/main/java/com/example/quantify/BinomialTrialActivity.java @@ -2,27 +2,19 @@ import android.content.Intent; import android.graphics.Bitmap; -import android.graphics.Point; -import android.hardware.display.DisplayManager; -import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.Settings; -import android.text.format.DateFormat; import android.util.Log; -import android.view.Display; import android.view.View; -import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; -import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; -import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.firebase.firestore.CollectionReference; @@ -32,7 +24,6 @@ import com.google.zxing.WriterException; import com.journeyapps.barcodescanner.BarcodeEncoder; -import java.lang.reflect.Array; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @@ -41,7 +32,6 @@ import java.util.UUID; import androidmads.library.qrgenearator.QRGContents; -import androidmads.library.qrgenearator.QRGEncoder; import androidmads.library.qrgenearator.QRGSaver; public class BinomialTrialActivity extends AppCompatActivity { @@ -63,7 +53,7 @@ public class BinomialTrialActivity extends AppCompatActivity { Button save; Button generateQR; - ImageView imageView; + ImageView binomialQRImage; @Override protected void onCreate(Bundle savedInstanceState) { @@ -94,15 +84,51 @@ protected void onCreate(Bundle savedInstanceState) { String savePath = Environment.getExternalStorageDirectory().getPath() + "/QRCode/"; String TAG = "GenerateQRCode"; UUID thisExperimentID = exp.getExperimentID(); - imageView = findViewById(R.id.imageView); + binomialQRImage = findViewById(R.id.binomialQRImage); generateQR.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - String inputValue = thisExperimentID.toString() + ";" + result.getText().toString(); + if (result.getText().toString().equals("_____")) { + Toast.makeText(getApplicationContext(), "The Trial result is empty, please enter result.", Toast.LENGTH_LONG).show(); + return; + } + + String inputValue = thisExperimentID.toString() + ";" + exp.getDescription() + ";" + result.getText().toString(); try{ BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); Bitmap bitmap = barcodeEncoder.encodeBitmap(inputValue, BarcodeFormat.QR_CODE,400,400); - imageView.setImageBitmap(bitmap); + binomialQRImage.setImageBitmap(bitmap); + + Log.d("expdesc", exp.getDescription()); + + FirebaseFirestore db; + db = FirebaseFirestore.getInstance(); + final CollectionReference collectionReference = db.collection("Barcodes"); + + HashMap data = new HashMap<>(); + data.put("Associate Exp", thisExperimentID.toString()); + data.put("Experiment desc", exp.getDescription()); + data.put("Result", result.getText().toString()); + data.put("Type", "Binomial Trials"); + + collectionReference + .document(inputValue) + .set(data) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + // These are a method which gets executed when the task is succeeded + Log.d("TAG", "Data has been added successfully!"); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + // These are a method which gets executed if there’s any problem + Log.d("TAG", "Data could not be added!" + e.toString()); + } + }); + boolean save; String result; try { diff --git a/app/src/main/java/com/example/quantify/CountTrialActivity.java b/app/src/main/java/com/example/quantify/CountTrialActivity.java index 710e84d..acdc74e 100644 --- a/app/src/main/java/com/example/quantify/CountTrialActivity.java +++ b/app/src/main/java/com/example/quantify/CountTrialActivity.java @@ -1,13 +1,17 @@ package com.example.quantify; import android.content.Intent; +import android.graphics.Bitmap; import android.os.Bundle; +import android.os.Environment; import android.provider.Settings; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -17,6 +21,9 @@ import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.FirebaseFirestore; +import com.google.zxing.BarcodeFormat; +import com.google.zxing.WriterException; +import com.journeyapps.barcodescanner.BarcodeEncoder; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -25,6 +32,9 @@ import java.util.Locale; import java.util.UUID; +import androidmads.library.qrgenearator.QRGContents; +import androidmads.library.qrgenearator.QRGSaver; + public class CountTrialActivity extends AppCompatActivity { Experiment exp; @@ -40,8 +50,10 @@ public class CountTrialActivity extends AppCompatActivity { Date date; SimpleDateFormat currentDate; String formattedCurrentDate; + ImageView countQRImage; Button save; + Button generateQR; @Override protected void onCreate(Bundle savedInstanceState) { @@ -58,6 +70,7 @@ protected void onCreate(Bundle savedInstanceState) { userID = findViewById(R.id.cTrialUserIDView); minTrials = findViewById(R.id.cMinTrialView); editCount = findViewById(R.id.countEdit); + generateQR = findViewById(R.id.cTrialGenerateQRCodeButton); date = Calendar.getInstance().getTime(); currentDate = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()); @@ -67,7 +80,69 @@ protected void onCreate(Bundle savedInstanceState) { userID.setText(exp.getExperimentID().toString()); minTrials.setText(exp.getMinTrials().toString()); - + //generate the QR code with save feature not working + String savePath = Environment.getExternalStorageDirectory().getPath() + "/QRCode/"; + String TAG = "GenerateQRCode"; + UUID thisExperimentID = exp.getExperimentID(); + countQRImage = findViewById(R.id.countQRImage); + generateQR.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (editCount.getText().toString().isEmpty()) { + Toast.makeText(getApplicationContext(), "The Trial result is empty, please enter result.", Toast.LENGTH_LONG).show(); + return; + } + + String inputValue = thisExperimentID.toString() + ";" + exp.getDescription() + ";" + editCount.getText().toString(); + + try{ + BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); + Bitmap bitmap = barcodeEncoder.encodeBitmap(inputValue, BarcodeFormat.QR_CODE,400,400); + countQRImage.setImageBitmap(bitmap); + + FirebaseFirestore db; + db = FirebaseFirestore.getInstance(); + final CollectionReference collectionReference = db.collection("Barcodes"); + + HashMap data = new HashMap<>(); + data.put("Associate Exp", thisExperimentID.toString()); + data.put("Experiment desc", exp.getDescription()); + data.put("Result", editCount.getText().toString()); + data.put("Type", "Count-based Tests"); + + collectionReference + .document(inputValue) + .set(data) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + // These are a method which gets executed when the task is succeeded + Log.d("TAG", "Data has been added successfully!"); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + // These are a method which gets executed if there’s any problem + Log.d("TAG", "Data could not be added!" + e.toString()); + } + }); + + boolean save; + String result; + try { + save = QRGSaver.save(savePath, "Test1", bitmap, QRGContents.ImageType.IMAGE_PNG); + String realPath = savePath.toString() + "Test1"; + result = save ? "Image Saved" : "Image Not Saved"; + Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); + } catch (Exception e) { + e.printStackTrace(); + } + } catch (WriterException e) { + e.printStackTrace(); + } + } + }); } diff --git a/app/src/main/java/com/example/quantify/MainActivity.java b/app/src/main/java/com/example/quantify/MainActivity.java index e059164..12e9c9a 100644 --- a/app/src/main/java/com/example/quantify/MainActivity.java +++ b/app/src/main/java/com/example/quantify/MainActivity.java @@ -1,9 +1,15 @@ package com.example.quantify; +import android.Manifest; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.pm.PackageManager; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.os.Build; import android.os.Bundle; import android.provider.Settings; @@ -23,6 +29,7 @@ import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +import androidx.core.app.ActivityCompat; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.tasks.OnCompleteListener; @@ -47,8 +54,12 @@ import org.json.JSONException; import org.json.JSONObject; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; import java.util.HashMap; +import java.util.Locale; import java.util.UUID; @@ -70,8 +81,13 @@ public class MainActivity extends AppCompatActivity { FloatingActionButton floatingActionButton; String id; - private double latitude; - private double longitude; + String latitude; + String longitude; + + LocationManager locationManager; + LocationListener locationListener; + + private int tabPos = 0; @@ -101,6 +117,22 @@ protected void onCreate(Bundle savedInstanceState) { // initially, we see the owner view experimentList.setAdapter(ownerExperimentAdapter); + locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); + + locationListener = new UserLocationListenerMain(); + if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + return; + } + locationManager.requestLocationUpdates( + LocationManager.GPS_PROVIDER, 5000, 10, locationListener); + FirebaseFirestore db; db = FirebaseFirestore.getInstance(); final CollectionReference collectionReference = db.collection("Experiments"); @@ -207,9 +239,9 @@ public boolean onMenuItemClick(MenuItem item) { break; - case R.id.more: + /*case R.id.more: // Handle more icon press - break; + break;*/ } return false; @@ -413,7 +445,7 @@ public boolean onMenuItemClick(MenuItem item) { break; - case R.id.question_answer: + /*case R.id.question_answer:*/ // Handle question_answer icon press case R.id.qr_code: @@ -437,7 +469,6 @@ public boolean onMenuItemClick(MenuItem item) { return false; } }); - } @Override @@ -451,56 +482,121 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten if (intentResult.getContents() != null) { //when result content is not null //initialize alert dialog -<<<<<<< HEAD - - String barcodeInfo = intentResult.getContents(); - - FirebaseFirestore db; - db = FirebaseFirestore.getInstance(); - final CollectionReference barcodeCollection = db.collection("Barcodes"); - - - barcodeCollection.addSnapshotListener(new EventListener() { -======= - AlertDialog.Builder builder = new AlertDialog.Builder( - MainActivity.this - ); - //set title - builder.setTitle("Result"); - //set message - builder.setMessage(intentResult.getContents()); - - //set positive button - builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { ->>>>>>> 7d54a6cfb4e5bc054a0fbafe3aab1dd5a739b210 - @Override - public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) { - for (QueryDocumentSnapshot doc : queryDocumentSnapshots) { - String barcodeID = doc.getId(); - - //recognize code - if (barcodeInfo.equals(barcodeID)){ - addBarcodeTrialResult(barcodeID); - return; - } + if (intentResult.getContents().contains(";")){ + AlertDialog.Builder builder = new AlertDialog.Builder( + MainActivity.this + ); + String trialResultString; + String experimentIDString; + String experimentDesc; + UUID experimentID; + //set title + builder.setTitle("Notification"); + //set message + builder.setMessage("Your trial has been added."); + experimentIDString = intentResult.getContents().split(";")[0]; + trialResultString = intentResult.getContents().split(";")[2]; + experimentDesc = intentResult.getContents().split(";")[1]; + experimentID = UUID.fromString(experimentIDString); - } - // new barcode - initBarcode(barcodeInfo); + FirebaseFirestore db; + db = FirebaseFirestore.getInstance(); + //whereEqualTo() is from + //https://stackoverflow.com/questions/53332471/checking-if-a-document-exists-in-a-firestore-collection/53332591#53332591 + db.collection("Experiments").whereEqualTo("Experiment ID", experimentID) + .limit(1).get() + .addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + //Toast.makeText(getApplicationContext(),experimentDesc, Toast.LENGTH_SHORT).show(); + //Log.d("docsize", experimentDesc); + //task.getResult().getDocuments().get(0).getId(); + + final CollectionReference collectionReference_1 = db.collection("Experiments"); + final DocumentReference documentReference = collectionReference_1.document(experimentDesc); + final CollectionReference collectionReference = documentReference.collection("Trials"); + + Date date; + SimpleDateFormat currentDate; + String formattedCurrentDate; + + date = Calendar.getInstance().getTime(); + currentDate = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()); + formattedCurrentDate = currentDate.format(date); + String experimenterID = id; + + HashMap data = new HashMap<>(); + data.put("Experimenter ID", experimenterID); + data.put("Location Latitude", latitude); + data.put("Location Longitude", longitude); + data.put("Trial Date", formattedCurrentDate); + data.put("Trial-Result", trialResultString); + + UUID trialID = UUID.randomUUID(); + collectionReference + .document(trialID.toString()) + .set(data) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + // These are a method which gets executed when the task is succeeded + Log.d("TAG", "Data has been added successfully!"); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + // These are a method which gets executed if there’s any problem + Log.d("TAG", "Data could not be added!" + e.toString()); + } + }); + } + } + }); - } - }); -<<<<<<< HEAD -======= - //show alert dialog - builder.show(); + //set positive button + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int which) { + //dismiss dialog + dialogInterface.dismiss(); + } + }); + //show alert dialog + builder.show(); + return; + + } - // check firebase -// String codeInfo = intentResult.getContents(); ->>>>>>> 7d54a6cfb4e5bc054a0fbafe3aab1dd5a739b210 + else { + String barcodeInfo = intentResult.getContents(); + FirebaseFirestore db; + db = FirebaseFirestore.getInstance(); + //final CollectionReference barcodeCollection = db.collection("Barcodes"); + DocumentReference docRef = db.collection("Barcodes").document(barcodeInfo); + docRef.get().addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + DocumentSnapshot document = task.getResult(); + if (document.exists()) { +// Log.d(TAG, "DocumentSnapshot data: " + document.getData()); + addBarcodeTrialResult(document); + } else { +// Log.d(TAG, "No such document"); + initBarcode(barcodeInfo); + } + } else { +// Log.d(TAG, "get failed with ", task.getException()); + } + } + }); + } + }else { //when result content is null //display toast @@ -508,11 +604,106 @@ public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable Fi ,"OOPS... You did not scan anything", Toast.LENGTH_SHORT) .show(); } - } + } + - private void addBarcodeTrialResult(String barcode){ - Toast.makeText(MainActivity.this, "bar code is: " + barcode, Toast.LENGTH_SHORT).show(); - return; + private void addBarcodeTrialResult(DocumentSnapshot doc){ +// Toast.makeText(MainActivity.this, "bar code is: " + barcode, Toast.LENGTH_SHORT).show(); + AlertDialog.Builder builder = new AlertDialog.Builder( + MainActivity.this + ); + //set title + builder.setTitle("Notification"); + //set message + builder.setMessage("Your trial has been added."); + + String trialResultString; + String experimentIDString; + //final String[] expestring = new String[1]; + String experimentDesc; + UUID experimentID; + + + + + trialResultString = doc.getString("Result"); + experimentDesc = doc.getString("Associate Exp"); + + Log.d("OnComplete", "aaa"); + + FirebaseFirestore db; + db = FirebaseFirestore.getInstance(); + + + experimentIDString = doc.getString("Exp ID"); + Log.d("Fail test", experimentIDString); + experimentID = UUID.fromString(experimentIDString); + + //whereEqualTo() is from + //https://stackoverflow.com/questions/53332471/checking-if-a-document-exists-in-a-firestore-collection/53332591#53332591 + db.collection("Experiments").whereEqualTo("Experiment ID", experimentID) + .limit(1).get() + .addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (task.isSuccessful()) { + //Toast.makeText(getApplicationContext(),experimentDesc, Toast.LENGTH_SHORT).show(); + //Log.d("docsize", experimentDesc); + //task.getResult().getDocuments().get(0).getId(); + + final CollectionReference collectionReference_1 = db.collection("Experiments"); + final DocumentReference documentReference = collectionReference_1.document(experimentDesc); + final CollectionReference collectionReference = documentReference.collection("Trials"); + + Date date; + SimpleDateFormat currentDate; + String formattedCurrentDate; + + date = Calendar.getInstance().getTime(); + currentDate = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()); + formattedCurrentDate = currentDate.format(date); + String experimenterID = id; + + HashMap data = new HashMap<>(); + data.put("Experimenter ID", experimenterID); + data.put("Location Latitude", latitude); + data.put("Location Longitude", longitude); + data.put("Trial Date", formattedCurrentDate); + data.put("Trial-Result", trialResultString); + + UUID trialID = UUID.randomUUID(); + collectionReference + .document(trialID.toString()) + .set(data) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + // These are a method which gets executed when the task is succeeded + Log.d("TAG", "Data has been added successfully!"); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + // These are a method which gets executed if there’s any problem + Log.d("TAG", "Data could not be added!" + e.toString()); + } + }); + } + } + }); + + + //set positive button + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int which) { + //dismiss dialog + dialogInterface.dismiss(); + } + }); + //show alert dialog + builder.show(); } private void initBarcode(String barcode){ @@ -542,12 +733,12 @@ public void onClick(DialogInterface dialog, int which) { String num_result = expMinTrials.getText().toString(); final String[] result = new String[1]; - FirebaseFirestore db; - db = FirebaseFirestore.getInstance(); - final CollectionReference coll = db.collection("Experiments"); - final DocumentReference doc = coll.document(exp_description); + FirebaseFirestore dbdbdb; + dbdbdb = FirebaseFirestore.getInstance(); + final CollectionReference coll = dbdbdb.collection("Experiments"); + final DocumentReference docdoc = coll.document(exp_description); - doc.get().addOnCompleteListener(new OnCompleteListener() { + docdoc.get().addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { @@ -593,20 +784,24 @@ public void onComplete(@NonNull Task task) { result[0] = num_result; } - final CollectionReference coll = db.collection("Barcodes"); + FirebaseFirestore dbc; + dbc = FirebaseFirestore.getInstance(); + + final CollectionReference coll_bar = dbc.collection("Barcodes"); HashMap data = new HashMap<>(); if (exp_description.length() > 0) { data.put("Associate Exp", exp_description); data.put("Result", result[0]); data.put("Type", exp_type); + data.put("Exp ID", document.getString("Experiment ID")); } else{ Toast.makeText(MainActivity.this, "Unable to create experiment.\nDescription empty!", Toast.LENGTH_SHORT).show(); return; } - coll + coll_bar .document(barcode) .set(data) .addOnSuccessListener(new OnSuccessListener() { @@ -624,22 +819,55 @@ public void onFailure(@NonNull Exception e) { } }); - return; } else { Log.d("TAG", "Document does not exist!"); Toast.makeText(MainActivity.this, "No such experiment exist!", Toast.LENGTH_SHORT).show(); - return; } + return; } else { Log.d("TAG", "Failed with: ", task.getException()); } } }); - - } }); adb.show(); return; } -} \ No newline at end of file + + class UserLocationListenerMain implements LocationListener { + @Override + public void onLocationChanged(Location loc) { + + longitude = loc.getLongitude()+""; + Log.v("longitude", longitude); + latitude = loc.getLatitude() + ""; + Log.d("latitude", latitude); + + String final_location = longitude + ", " + latitude; + + + } + + @Override + public void onProviderDisabled(String provider) {} + + @Override + public void onProviderEnabled(String provider) {} + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) {} + + } + + + + + + +} + + + + + diff --git a/app/src/main/java/com/example/quantify/MeasurementTrialActivity.java b/app/src/main/java/com/example/quantify/MeasurementTrialActivity.java index fb13be9..4ba5d51 100644 --- a/app/src/main/java/com/example/quantify/MeasurementTrialActivity.java +++ b/app/src/main/java/com/example/quantify/MeasurementTrialActivity.java @@ -1,13 +1,17 @@ package com.example.quantify; import android.content.Intent; +import android.graphics.Bitmap; import android.os.Bundle; +import android.os.Environment; import android.provider.Settings; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -17,6 +21,9 @@ import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.FirebaseFirestore; +import com.google.zxing.BarcodeFormat; +import com.google.zxing.WriterException; +import com.journeyapps.barcodescanner.BarcodeEncoder; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -25,6 +32,9 @@ import java.util.Locale; import java.util.UUID; +import androidmads.library.qrgenearator.QRGContents; +import androidmads.library.qrgenearator.QRGSaver; + public class MeasurementTrialActivity extends AppCompatActivity { Experiment exp; @@ -42,6 +52,9 @@ public class MeasurementTrialActivity extends AppCompatActivity { String formattedCurrentDate; Button save; + Button generateQR; + + ImageView MQRImage; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,6 +70,7 @@ protected void onCreate(Bundle savedInstanceState) { userID = findViewById(R.id.mTrialUserIDView); minTrials = findViewById(R.id.mMinTrialView); editCount = findViewById(R.id.measurementEdit); + generateQR = findViewById(R.id.mTrialGenerateQRCodeButton); date = Calendar.getInstance().getTime(); currentDate = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()); @@ -66,7 +80,69 @@ protected void onCreate(Bundle savedInstanceState) { userID.setText(exp.getExperimentID().toString()); minTrials.setText(exp.getMinTrials().toString()); - + //generate the QR code with save feature not working + String savePath = Environment.getExternalStorageDirectory().getPath() + "/QRCode/"; + String TAG = "GenerateQRCode"; + UUID thisExperimentID = exp.getExperimentID(); + MQRImage = findViewById(R.id.MQRImage); + generateQR.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (editCount.getText().toString().isEmpty()) { + Toast.makeText(getApplicationContext(), "The Trial result is empty, please enter result.", Toast.LENGTH_LONG).show(); + return; + } + + String inputValue = thisExperimentID.toString() + ";" + exp.getDescription() + ";" + editCount.getText().toString(); + + try{ + BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); + Bitmap bitmap = barcodeEncoder.encodeBitmap(inputValue, BarcodeFormat.QR_CODE,400,400); + MQRImage.setImageBitmap(bitmap); + + FirebaseFirestore db; + db = FirebaseFirestore.getInstance(); + final CollectionReference collectionReference = db.collection("Barcodes"); + + HashMap data = new HashMap<>(); + data.put("Associate Exp", thisExperimentID.toString()); + data.put("Experiment desc", exp.getDescription()); + data.put("Result", editCount.getText().toString()); + data.put("Type", "Measurement Trials"); + + collectionReference + .document(inputValue) + .set(data) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + // These are a method which gets executed when the task is succeeded + Log.d("TAG", "Data has been added successfully!"); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + // These are a method which gets executed if there’s any problem + Log.d("TAG", "Data could not be added!" + e.toString()); + } + }); + + boolean save; + String result; + try { + save = QRGSaver.save(savePath, "Test1", bitmap, QRGContents.ImageType.IMAGE_PNG); + String realPath = savePath.toString() + "Test1"; + result = save ? "Image Saved" : "Image Not Saved"; + Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); + } catch (Exception e) { + e.printStackTrace(); + } + } catch (WriterException e) { + e.printStackTrace(); + } + } + }); } diff --git a/app/src/main/java/com/example/quantify/NonNegativeCountTrialActivity.java b/app/src/main/java/com/example/quantify/NonNegativeCountTrialActivity.java index bbdeeaa..e6b2d78 100644 --- a/app/src/main/java/com/example/quantify/NonNegativeCountTrialActivity.java +++ b/app/src/main/java/com/example/quantify/NonNegativeCountTrialActivity.java @@ -1,12 +1,15 @@ package com.example.quantify; import android.content.Intent; +import android.graphics.Bitmap; import android.os.Bundle; +import android.os.Environment; import android.provider.Settings; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; @@ -18,6 +21,9 @@ import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.FirebaseFirestore; +import com.google.zxing.BarcodeFormat; +import com.google.zxing.WriterException; +import com.journeyapps.barcodescanner.BarcodeEncoder; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -26,6 +32,9 @@ import java.util.Locale; import java.util.UUID; +import androidmads.library.qrgenearator.QRGContents; +import androidmads.library.qrgenearator.QRGSaver; + public class NonNegativeCountTrialActivity extends AppCompatActivity { Experiment exp; @@ -43,6 +52,9 @@ public class NonNegativeCountTrialActivity extends AppCompatActivity { String formattedCurrentDate; Button save; + Button generateQR; + + ImageView NNCQRImage; @Override protected void onCreate(Bundle savedInstanceState) { @@ -58,6 +70,7 @@ protected void onCreate(Bundle savedInstanceState) { userID = findViewById(R.id.nTrialUserIDView); minTrials = findViewById(R.id.nMinTrialView); editCount = findViewById(R.id.nonNegEdit); + generateQR = findViewById(R.id.nTrialGenerateQRCodeButton); date = Calendar.getInstance().getTime(); currentDate = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()); @@ -67,7 +80,69 @@ protected void onCreate(Bundle savedInstanceState) { userID.setText(exp.getExperimentID().toString()); minTrials.setText(exp.getMinTrials().toString()); - + //generate the QR code with save feature not working + String savePath = Environment.getExternalStorageDirectory().getPath() + "/QRCode/"; + String TAG = "GenerateQRCode"; + UUID thisExperimentID = exp.getExperimentID(); + NNCQRImage = findViewById(R.id.NNCQRImage); + generateQR.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (editCount.getText().toString().isEmpty()) { + Toast.makeText(getApplicationContext(), "The Trial result is empty, please enter result.", Toast.LENGTH_LONG).show(); + return; + } + + String inputValue = thisExperimentID.toString() + ";" + exp.getDescription() + ";" + editCount.getText().toString(); + + try{ + BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); + Bitmap bitmap = barcodeEncoder.encodeBitmap(inputValue, BarcodeFormat.QR_CODE,400,400); + NNCQRImage.setImageBitmap(bitmap); + + FirebaseFirestore db; + db = FirebaseFirestore.getInstance(); + final CollectionReference collectionReference = db.collection("Barcodes"); + + HashMap data = new HashMap<>(); + data.put("Associate Exp", thisExperimentID.toString()); + data.put("Experiment desc", exp.getDescription()); + data.put("Result", editCount.getText().toString()); + data.put("Type", "Non-negative Integer Counts"); + + collectionReference + .document(inputValue) + .set(data) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + // These are a method which gets executed when the task is succeeded + Log.d("TAG", "Data has been added successfully!"); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + // These are a method which gets executed if there’s any problem + Log.d("TAG", "Data could not be added!" + e.toString()); + } + }); + + boolean save; + String result; + try { + save = QRGSaver.save(savePath, "Test1", bitmap, QRGContents.ImageType.IMAGE_PNG); + String realPath = savePath.toString() + "Test1"; + result = save ? "Image Saved" : "Image Not Saved"; + Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); + } catch (Exception e) { + e.printStackTrace(); + } + } catch (WriterException e) { + e.printStackTrace(); + } + } + }); } diff --git a/app/src/main/java/com/example/quantify/QuestionForumList.java b/app/src/main/java/com/example/quantify/QuestionForumList.java index 1dc4f72..6815db5 100644 --- a/app/src/main/java/com/example/quantify/QuestionForumList.java +++ b/app/src/main/java/com/example/quantify/QuestionForumList.java @@ -114,6 +114,8 @@ public void onItemClick(AdapterView parent, View view, int position, long id) */ public void arrangeListWithDocument(String path, List list) { + List user = new ArrayList<>(); + TextView textSuggestion = (TextView)findViewById(R.id.addQuestionReferenceText); // Firebase connection FirebaseFirestore db = FirebaseFirestore.getInstance(); diff --git a/app/src/main/java/com/example/quantify/ResultList.java b/app/src/main/java/com/example/quantify/ResultList.java index 02bd73d..b4ac632 100644 --- a/app/src/main/java/com/example/quantify/ResultList.java +++ b/app/src/main/java/com/example/quantify/ResultList.java @@ -37,10 +37,9 @@ public class ResultList extends ArrayAdapter { ArrayList result_date_list; ArrayList result_count_list; - int success; - int failure; + ArrayList booleanResults; // zero index is fail. one index is success - public ResultList(Context context, ArrayList trials, ArrayList ignoredTrials, ArrayList Trial_list, ArrayList Count_list, ArrayList result_date_list, ArrayList result_count_list, int success, int failure){ + public ResultList(Context context, ArrayList trials, ArrayList ignoredTrials, ArrayList Trial_list, ArrayList Count_list, ArrayList result_date_list, ArrayList result_count_list, ArrayList booleanResults){ super(context, 0, trials); this.trials = trials; this.ignoredTrials = ignoredTrials; @@ -48,8 +47,7 @@ public ResultList(Context context, ArrayList trials, ArrayList ign this.Count_list = Count_list; this.result_date_list = result_date_list; this.result_count_list = result_count_list; - this.success = success; - this.failure = failure; + this.booleanResults = booleanResults; this.context = context; } @@ -87,8 +85,8 @@ public boolean onLongClick(View v) { Count_list.clear(); result_date_list.clear(); result_count_list.clear(); - success = 0; - failure = 0; + booleanResults.set(0,0); // zero index is fail + booleanResults.set(1,0); // one index is success for(int counter = 0; counter < trials.size(); counter++){ String Trial_result = trials.get(counter).getResult(); @@ -110,9 +108,20 @@ public boolean onLongClick(View v) { result_date_list.add(Result_date); result_count_list.add(1); } - } + if (Trial_result.equals("Success")) { + booleanResults.set(1, booleanResults.get(1)+1); + Log.d("TAG", "BOOM"); + } + + else if (Trial_result.equals("Fail")) { + booleanResults.set(0, booleanResults.get(0)+1); + Log.d("TAG", "BOOM2"); + } + } + Log.d("success",String.valueOf(booleanResults.get(1))); + Log.d("failure",String.valueOf(booleanResults.get(0))); } notifyDataSetChanged(); return false; diff --git a/app/src/main/java/com/example/quantify/ResultsToIgnoreActivity.java b/app/src/main/java/com/example/quantify/ResultsToIgnoreActivity.java index 0773f70..4eae7b2 100644 --- a/app/src/main/java/com/example/quantify/ResultsToIgnoreActivity.java +++ b/app/src/main/java/com/example/quantify/ResultsToIgnoreActivity.java @@ -41,8 +41,7 @@ public class ResultsToIgnoreActivity extends AppCompatActivity { ArrayList result_date_list; ArrayList result_count_list; - int success; - int failure; + ArrayList booleanResults; // zero index is fail. one index is success @Override protected void onCreate(Bundle savedInstanceState) { @@ -69,10 +68,11 @@ protected void onCreate(Bundle savedInstanceState) { result_date_list = new ArrayList<>(); result_count_list = new ArrayList<>(); - success = 0; - failure = 0; + booleanResults = new ArrayList(); + booleanResults.add(0); + booleanResults.add(0); - trialAdapter = new ResultList(ResultsToIgnoreActivity.this, trialDataList, ignoredTrials, Trial_list, Count_list, result_date_list, result_count_list, success, failure); + trialAdapter = new ResultList(ResultsToIgnoreActivity.this, trialDataList, ignoredTrials, Trial_list, Count_list, result_date_list, result_count_list, booleanResults); trialList.setAdapter(trialAdapter); @@ -95,8 +95,8 @@ public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable Fi Count_list.clear(); result_date_list.clear(); result_count_list.clear(); - success = 0; - failure = 0; + booleanResults.set(0,0); // zero index is fail + booleanResults.set(1,0); // one index is success for (QueryDocumentSnapshot doc : queryDocumentSnapshots) { String experimenter_id = (String) doc.getData().get("Experimenter ID"); @@ -112,18 +112,18 @@ public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable Fi trialDataList.add(new_trial); if (Trial_result.equals("Success")) { - success++; + booleanResults.set(1, booleanResults.get(1)+1); Log.d("TAG", "BOOM"); } if (Trial_result.equals("Fail")) { - failure++; + booleanResults.set(0, booleanResults.get(0)+1); Log.d("TAG", "BOOM2"); } } } - Log.d("ignored", String.valueOf(failure)); + Log.d("fail", String.valueOf(booleanResults.get(0))); Log.d("ignored", ignoredTrials.toString()); trialAdapter.notifyDataSetChanged(); @@ -158,8 +158,11 @@ public void createMyHistogram(View target){ if(Count_list.size() > 0 && Trial_list.size() > 0) { if (exp.getType().equals("Binomial Trials")) { Intent intent_1 = new Intent(this, BinomialHistogramActivity.class); - intent_1.putExtra("success", success); - intent_1.putExtra("fail", failure); + intent_1.putExtra("success", booleanResults.get(1)); + intent_1.putExtra("fail", booleanResults.get(0)); + Log.d("success",String.valueOf(booleanResults.get(1))); + Log.d("failure",String.valueOf(booleanResults.get(0))); + this.startActivity(intent_1); } else { Intent intent_1 = new Intent(this, OtherHistogramActivity.class); @@ -186,5 +189,6 @@ public void myResultsOverTimeOtherClicked(View target){ else{ Toast.makeText(this, "No trials available", Toast.LENGTH_SHORT).show(); } + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/quantify/SearchActivity.java b/app/src/main/java/com/example/quantify/SearchActivity.java index 1026aa7..1ac74f7 100644 --- a/app/src/main/java/com/example/quantify/SearchActivity.java +++ b/app/src/main/java/com/example/quantify/SearchActivity.java @@ -68,21 +68,28 @@ protected void onCreate(Bundle savedInstanceState) { public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) { dataList.clear(); for (QueryDocumentSnapshot doc : queryDocumentSnapshots) { - //Log.d("TAG", String.valueOf(doc.getData().get("Province Name"))); - UUID experiment_id = UUID.fromString((String) doc.getData().get("Experiment ID")); - String experiment_description = doc.getId(); - String experiment_username = (String) doc.getData().get("Experiment User"); - String experiment_status = (String) doc.getData().get("Experiment Status"); - String experiment_type = (String) doc.getData().get("Experiment Type"); - String experiment_location = (String) doc.getData().get("Experiment Location"); - Integer experiment_min_trials = 1; - try { - experiment_min_trials = Integer.valueOf((String) doc.getData().get("Min Trials")); - } catch (Exception e) { - experiment_min_trials = 0; + if(doc.getData().get("Experiment ID") != null + && doc.getData().get("Experiment User")!= null + && doc.getData().get("Experiment Status")!= null + && doc.getData().get("Experiment Type")!= null + && doc.getData().get("Experiment Location")!= null + && doc.getData().get("Min Trials")!= null) { + //Log.d("TAG", String.valueOf(doc.getData().get("Province Name"))); + UUID experiment_id = UUID.fromString((String) doc.getData().get("Experiment ID")); + String experiment_description = doc.getId(); + String experiment_username = (String) doc.getData().get("Experiment User"); + String experiment_status = (String) doc.getData().get("Experiment Status"); + String experiment_type = (String) doc.getData().get("Experiment Type"); + String experiment_location = (String) doc.getData().get("Experiment Location"); + Integer experiment_min_trials = 1; + try { + experiment_min_trials = Integer.valueOf((String) doc.getData().get("Min Trials")); + } catch (Exception e) { + experiment_min_trials = 0; + } + Log.d("TAG", experiment_username); + dataList.add(new Experiment(experiment_id, experiment_description, experiment_username, experiment_status, experiment_type, experiment_min_trials, experiment_location)); // Adding the cities and provinces from FireStore } - Log.d("TAG", experiment_username); - dataList.add(new Experiment(experiment_id, experiment_description, experiment_username, experiment_status, experiment_type, experiment_min_trials, experiment_location)); // Adding the cities and provinces from FireStore } } @@ -102,14 +109,14 @@ public boolean onCreateOptionsMenu(Menu menu) { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { - Toast.makeText(SearchActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show(); + //Toast.makeText(SearchActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show(); searchExps(query); return false; } @Override public boolean onQueryTextChange(String newText) { - Toast.makeText(SearchActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show(); + //Toast.makeText(SearchActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show(); searchExps(newText); return false; } diff --git a/app/src/main/java/com/example/quantify/Trial.java b/app/src/main/java/com/example/quantify/Trial.java index 5ac147d..77db41f 100644 --- a/app/src/main/java/com/example/quantify/Trial.java +++ b/app/src/main/java/com/example/quantify/Trial.java @@ -1,6 +1,8 @@ package com.example.quantify; -public class Trial { +import android.app.Activity; + +public class Trial extends Activity { private String experimenterID; private String Date; private String result; diff --git a/app/src/main/java/com/example/quantify/TrialIntermediateActivity.java b/app/src/main/java/com/example/quantify/TrialIntermediateActivity.java index fd7ddf5..99d1695 100644 --- a/app/src/main/java/com/example/quantify/TrialIntermediateActivity.java +++ b/app/src/main/java/com/example/quantify/TrialIntermediateActivity.java @@ -28,8 +28,7 @@ import com.google.firebase.firestore.QuerySnapshot; import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +import java.util.Collections; public class TrialIntermediateActivity extends AppCompatActivity { @@ -52,15 +51,21 @@ public class TrialIntermediateActivity extends AppCompatActivity { ArrayList experimentIDList; TextView mean; + TextView median; + TextView Q1; + TextView Q3; + TextView sd; Button start; ArrayList Trial_list; ArrayList Count_list; + ArrayList Complete_Trial_list; ArrayList result_date_list; ArrayList result_count_list; - + int length; + double sum; @Override protected void onCreate(Bundle savedInstanceState) { @@ -77,6 +82,10 @@ protected void onCreate(Bundle savedInstanceState) { locationView = findViewById(R.id.locationView); mean = findViewById(R.id.mean_value); + median = findViewById(R.id.medianValue); + Q1 = findViewById(R.id.quartile1View); + Q3 = findViewById(R.id.quartile3View); + sd = findViewById(R.id.StdDevValue); start = findViewById(R.id.startButton); @@ -129,13 +138,19 @@ protected void onCreate(Bundle savedInstanceState) { // if the number is not unique, increment count Trial_list = new ArrayList(); Count_list = new ArrayList(); + Complete_Trial_list = new ArrayList(); result_date_list = new ArrayList<>(); result_count_list = new ArrayList<>(); + + collectionReference.addSnapshotListener(new EventListener() { @Override public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) { + length = 0; + sum = 0; + Complete_Trial_list.clear(); Count_list.clear(); Trial_list.clear(); result_count_list.clear(); @@ -148,15 +163,20 @@ public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable Fi if (doc.getData().get("Trial-Result") != null) { String Trial_id = doc.getId(); String Trial_result = (String) doc.getData().get("Trial-Result"); + Complete_Trial_list.add(Double.parseDouble(Trial_result)); // Log.d("TAG", Result_date); if (Trial_list.contains(Trial_result)){ int index = Trial_list.indexOf(Trial_result); Count_list.set(index, Count_list.get(index) + 1); + sum = sum + Double.parseDouble(Trial_result); + length = length + 1; } else { Trial_list.add(Trial_result); Count_list.add(1); + sum = sum + Double.parseDouble(Trial_result); + length = length + 1; } if(doc.getData().get("Trial Date") != null) { @@ -184,18 +204,64 @@ public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable Fi Log.d("TAG", (String) result_count_list.toString()); Log.d("longitudeList", longitudeList.toString()); Log.d("latitudeList", latitudeList.toString()); + Log.d("LENGTH", String.valueOf(length)); + + if (Complete_Trial_list.size() > 0){ + double mean_v = calculateMean(sum, length); + mean.setText(String.valueOf(mean_v)); + Collections.sort(Complete_Trial_list); + double median_v = calculateMedian(Complete_Trial_list); + double Q1_v = calculateQ1(Complete_Trial_list); + double Q3_v = calculateQ3(Complete_Trial_list); + double sd_v = calculateSD(Complete_Trial_list, mean_v); + + Q1.setText(String.valueOf(Q1_v)); + median.setText(String.valueOf(median_v)); + Q3.setText(String.valueOf(Q3_v)); + sd.setText(String.valueOf(sd_v)); + Log.d("Sorted", Complete_Trial_list.toString()); + } + + + } + }); } - public double calculateMean() { + public double calculateMedian(ArrayList CompleteList){ + int position = CompleteList.size()/2; + Log.d("Q2 position", String.valueOf(position)); + return CompleteList.get(position); + } + + public double calculateQ1(ArrayList CompleteList){ + int position = CompleteList.size()/4; + Log.d("Q1 position", String.valueOf(position)); + return CompleteList.get(position); + } + + public double calculateQ3(ArrayList CompleteList){ + Log.d("LENGTH OF LIST", String.valueOf(CompleteList.size())); + int position = (CompleteList.size()*3)/4; + Log.d("Q3 position", String.valueOf(position)); + return CompleteList.get(position); + } - double sum = 0; - for (int counter = 0; counter < Trial_list.size(); counter++) { - sum += Double.parseDouble(Trial_list.get(counter).toString()); + public double calculateSD(ArrayList CompleteList, double mean){ + double sum_square = 0; + double sd; + for (int counter=0; counter + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 127fc4d..0af3ea1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -18,7 +18,6 @@ android:background="@color/shrine_pink_100" app:layout_scrollFlags="scroll|enterAlways|snap" app:menu="@menu/top_app_bar" - app:navigationIcon="@drawable/ic_menu_24dp" app:title="@string/app_name" app:titleTextColor="@color/shrine_pink_900" /> diff --git a/app/src/main/res/layout/activity_question_details.xml b/app/src/main/res/layout/activity_question_details.xml index acaeb76..c81c839 100644 --- a/app/src/main/res/layout/activity_question_details.xml +++ b/app/src/main/res/layout/activity_question_details.xml @@ -23,14 +23,15 @@