-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinomialTrialActivity.java
More file actions
203 lines (167 loc) · 8.18 KB
/
BinomialTrialActivity.java
File metadata and controls
203 lines (167 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
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;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.UUID;
import androidmads.library.qrgenearator.QRGContents;
import androidmads.library.qrgenearator.QRGSaver;
public class BinomialTrialActivity extends AppCompatActivity {
public static final String QRPath = "null";
Experiment exp;
TextView expDesc;
TextView userID;
TextView minTrials;
TextView result;
String longitude;
String latitude;
Date date;
SimpleDateFormat currentDate;
String formattedCurrentDate;
Button save;
Button generateQR;
ImageView binomialQRImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.binomial_trial);
Intent intent = getIntent();
exp = (Experiment) getIntent().getSerializableExtra("Experiment");
longitude = getIntent().getStringExtra("Longitude");
latitude = getIntent().getStringExtra("Latitude");
expDesc = findViewById(R.id.bTrialDescriptionView);
userID = findViewById(R.id.bTrialUserIDView);
minTrials = findViewById(R.id.bMinTrialView);
result = findViewById(R.id.bResultValue);
generateQR = findViewById(R.id.bTrialGenerateQRCodeButton);
date = Calendar.getInstance().getTime();
currentDate = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
formattedCurrentDate = currentDate.format(date);
expDesc.setText(exp.getDescription());
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();
binomialQRImage = findViewById(R.id.binomialQRImage);
generateQR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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);
binomialQRImage.setImageBitmap(bitmap);
Log.d("expdesc", exp.getDescription());
FirebaseFirestore db;
db = FirebaseFirestore.getInstance();
final CollectionReference collectionReference = db.collection("Barcodes");
HashMap<String, String> 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<Void>() {
@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();
}
}
});
}
public void successClicked(View target){
result.setText("Success");
}
public void failClicked(View target){
result.setText("Fail");
}
public void binomialSaveClicked(View target){
// we give the trial an ID using UUID and save the result in the database
if(result.getText().toString().equals("Fail") || result.getText().toString().equals("Success")) {
FirebaseFirestore db;
db = FirebaseFirestore.getInstance();
String id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
final CollectionReference collectionReference_1 = db.collection("Experiments");
final DocumentReference documentReference = collectionReference_1.document(exp.getDescription());
final CollectionReference collectionReference = documentReference.collection("Trials");
HashMap<String, String> data = new HashMap<>();
data.put("Trial-Result", result.getText().toString());
data.put("Experimenter ID", Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID));
data.put("Trial Date", formattedCurrentDate);
data.put("Location Latitude", latitude);
data.put("Location Longitude", longitude);
UUID Trial_id = UUID.randomUUID();
collectionReference
.document(Trial_id.toString())
.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@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());
}
});
Log.d("count", "Count: " + result.getText().toString());
}
finish();
}
}