|
@@ -1,13 +1,11 @@
|
1
|
|
-/*************************************************************
|
2
|
|
- * By: Coralys Cubero Rivera
|
3
|
|
- * Date: 2019
|
4
|
|
- *************************************************************/
|
5
|
|
-
|
6
|
1
|
package uprrp.tania.activities;
|
7
|
2
|
|
|
3
|
+import android.app.ProgressDialog;
|
|
4
|
+import android.content.Context;
|
8
|
5
|
import android.content.Intent;
|
9
|
6
|
import android.net.Uri;
|
10
|
7
|
import android.os.Bundle;
|
|
8
|
+import android.os.Handler;
|
11
|
9
|
import android.util.Log;
|
12
|
10
|
import android.view.View;
|
13
|
11
|
import android.widget.Button;
|
|
@@ -20,62 +18,127 @@ import com.google.android.gms.tasks.OnSuccessListener;
|
20
|
18
|
import com.google.firebase.iid.FirebaseInstanceId;
|
21
|
19
|
import com.google.firebase.iid.InstanceIdResult;
|
22
|
20
|
|
|
21
|
+import org.json.JSONException;
|
23
|
22
|
import org.json.JSONObject;
|
24
|
23
|
|
25
|
24
|
import uprrp.tania.R;
|
26
|
25
|
import uprrp.tania.SendActivateExperienceToServer;
|
|
26
|
+import uprrp.tania.URLEventListener;
|
27
|
27
|
|
28
|
28
|
public class ExperienceRegistrationActivity extends AppCompatActivity {
|
29
|
29
|
|
30
|
|
- String device_token;
|
31
|
|
- String id_experience;
|
|
30
|
+ private final String TAG = "ExperienceRegistrationActivity";
|
|
31
|
+ private String device_token;
|
|
32
|
+ private String id_experience;
|
|
33
|
+
|
32
|
34
|
@Override
|
33
|
35
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
36
|
+
|
|
37
|
+ // Start constructor and fetch UI components
|
34
|
38
|
super.onCreate(savedInstanceState);
|
35
|
39
|
setContentView(R.layout.activity_experience_registration);
|
|
40
|
+ final Button experienceRegistrationButton = findViewById(R.id.buttonEnterExperience);
|
36
|
41
|
|
37
|
|
- //Let's get the device's token
|
|
42
|
+ // Change all caps text to normal capitalization
|
|
43
|
+ // TODO: this is a workaround I found, any other acceptable solution is welcome
|
|
44
|
+ experienceRegistrationButton.setTransformationMethod(null);
|
|
45
|
+
|
|
46
|
+ // Request to Firebase...
|
38
|
47
|
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
|
39
|
48
|
@Override
|
40
|
49
|
public void onSuccess(InstanceIdResult instanceIdResult) {
|
41
|
|
- device_token = instanceIdResult.getToken();
|
|
50
|
+
|
|
51
|
+ // Get device's token
|
|
52
|
+ device_token = instanceIdResult.getToken();
|
|
53
|
+
|
|
54
|
+ // Set onClick listener on button
|
|
55
|
+ experienceRegistrationButton.setOnClickListener(new View.OnClickListener() {
|
|
56
|
+ @Override
|
|
57
|
+ public void onClick(View v) {
|
|
58
|
+
|
|
59
|
+ // Initiate progress dialog
|
|
60
|
+ // TODO: find substitute for this deprecated dialog box
|
|
61
|
+ final ProgressDialog progressDialog = ProgressDialog.show(ExperienceRegistrationActivity.this,
|
|
62
|
+ "Registering in Experience",
|
|
63
|
+ "This shouldn't take long");
|
|
64
|
+
|
|
65
|
+ if(!id_experience.equals("") && id_experience != null) {
|
|
66
|
+ try {
|
|
67
|
+
|
|
68
|
+ // Create JSON with details we'll send
|
|
69
|
+ JSONObject experienceCodeJSON = new JSONObject();
|
|
70
|
+ experienceCodeJSON.put("token", device_token);
|
|
71
|
+ experienceCodeJSON.put("id_experiencia", id_experience);
|
|
72
|
+ Log.d("EXPERIENCE CODE JSON", experienceCodeJSON.toString());
|
|
73
|
+
|
|
74
|
+ // Send registration request to server
|
|
75
|
+ SendActivateExperienceToServer experienceRegistrationTask = new SendActivateExperienceToServer(new URLEventListener() {
|
|
76
|
+ @Override
|
|
77
|
+ public void onSuccess() {
|
|
78
|
+ progressDialog.dismiss();
|
|
79
|
+ Context context = ExperienceRegistrationActivity.this;
|
|
80
|
+ Intent intent = new Intent(context, MainActivity.class);
|
|
81
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
82
|
+ context.startActivity(intent);
|
|
83
|
+ Toast.makeText(getApplicationContext(), "You have been registered!", Toast.LENGTH_LONG).show();
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ @Override
|
|
87
|
+ public void onFailure(Exception e) {
|
|
88
|
+ progressDialog.dismiss();
|
|
89
|
+ // TODO: restrict double registration (maybe in backend?)
|
|
90
|
+ Toast.makeText(ExperienceRegistrationActivity.this, "Oops! Something went wrong...", Toast.LENGTH_LONG).show();
|
|
91
|
+ Log.e(TAG, "Error occurred while sending registration request to server...");
|
|
92
|
+ e.printStackTrace();
|
|
93
|
+ }
|
|
94
|
+ });
|
|
95
|
+
|
|
96
|
+ experienceRegistrationTask.execute(experienceCodeJSON.toString());
|
|
97
|
+
|
|
98
|
+ // UNCOMMENT THIS FOR TESTING (REMEMBER TO COMMENT .execute() LINE)
|
|
99
|
+ /*
|
|
100
|
+ Handler handler = new Handler();
|
|
101
|
+ handler.postDelayed(new Runnable() {
|
|
102
|
+ public void run() {
|
|
103
|
+ progressDialog.dismiss();
|
|
104
|
+ if(false) { // Imitate success
|
|
105
|
+ Context context = ExperienceRegistrationActivity.this;
|
|
106
|
+ Intent intent = new Intent(context, MainActivity.class);
|
|
107
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
108
|
+ context.startActivity(intent);
|
|
109
|
+ Toast.makeText(getApplicationContext(), "You have been registered!", Toast.LENGTH_LONG).show();
|
|
110
|
+ } else { // Imitate failure
|
|
111
|
+ Toast.makeText(ExperienceRegistrationActivity.this, "Oops! Something went wrong...", Toast.LENGTH_LONG).show();
|
|
112
|
+ }
|
|
113
|
+ }
|
|
114
|
+ }, 5000);
|
|
115
|
+ */
|
|
116
|
+ // UNCOMMENT THIS FOR TESTING (REMEMBER TO COMMENT .execute() LINE)
|
|
117
|
+
|
|
118
|
+ } catch (JSONException e) {
|
|
119
|
+ progressDialog.dismiss();
|
|
120
|
+ Toast.makeText(ExperienceRegistrationActivity.this, "Oops! Something went wrong...", Toast.LENGTH_LONG).show();
|
|
121
|
+ Log.e(TAG, "Error occurred while preparing JSON for server");
|
|
122
|
+ e.printStackTrace();
|
|
123
|
+ }
|
|
124
|
+ } else {
|
|
125
|
+ progressDialog.dismiss();
|
|
126
|
+ Toast.makeText(ExperienceRegistrationActivity.this, "Invalid URL!", Toast.LENGTH_LONG).show();
|
|
127
|
+ Log.d(TAG, "Invalid query parameter found (" + id_experience + ")");
|
|
128
|
+ }
|
|
129
|
+ }
|
|
130
|
+ });
|
42
|
131
|
}
|
43
|
132
|
});
|
44
|
133
|
|
|
134
|
+ // Fetch experience ID from URL (only if we launched this Activity from Browser)
|
45
|
135
|
Intent intent = getIntent();
|
46
|
|
- if (Intent.ACTION_VIEW.equals(intent.getAction())) {
|
|
136
|
+ if(Intent.ACTION_VIEW.equals(intent.getAction())) {
|
47
|
137
|
Uri uri = intent.getData();
|
48
|
|
- assert uri != null;
|
|
138
|
+ assert uri != null; // TODO: figure out if this causes crashes...
|
49
|
139
|
id_experience = uri.getQueryParameter("id");
|
50
|
140
|
}
|
51
|
141
|
|
52
|
|
- final JSONObject experienceCodeJSON = new JSONObject();
|
53
|
|
-
|
54
|
|
- final Button enterExperience = findViewById(R.id.buttonEnterExperience);
|
55
|
|
- enterExperience.setOnClickListener(new View.OnClickListener() {
|
56
|
|
- @Override
|
57
|
|
- public void onClick(View v) {
|
58
|
|
- try {
|
59
|
|
- if (!id_experience.equals("")){
|
60
|
|
- experienceCodeJSON.put("token", device_token);
|
61
|
|
- experienceCodeJSON.put("id_experiencia", id_experience);
|
62
|
|
-
|
63
|
|
-
|
64
|
|
- Log.d("EXPERIENCE CODE JSON", experienceCodeJSON.toString());
|
65
|
|
-
|
66
|
|
- SendActivateExperienceToServer sendActivateExperienceToServer = new SendActivateExperienceToServer(getApplicationContext());
|
67
|
|
- sendActivateExperienceToServer.execute(experienceCodeJSON.toString());
|
68
|
|
-
|
69
|
|
- }
|
70
|
|
- else{
|
71
|
|
- Toast.makeText(ExperienceRegistrationActivity.this, "The password and confirmation must match!", Toast.LENGTH_LONG).show();
|
72
|
|
- }
|
73
|
|
-
|
74
|
|
-
|
75
|
|
- } catch (Exception e) {
|
76
|
|
- e.printStackTrace();
|
77
|
|
- }
|
78
|
|
- }
|
79
|
|
- });
|
80
|
142
|
}
|
|
143
|
+
|
81
|
144
|
}
|