2
2
3
3
import android .app .Activity ;
4
4
import android .app .ProgressDialog ;
5
+ import android .content .Context ;
5
6
import android .content .Intent ;
6
7
import android .content .pm .ActivityInfo ;
7
8
import android .graphics .drawable .AnimationDrawable ;
23
24
import ai .labomatic .data .local .UsersDatabaseHandler ;
24
25
25
26
import ai .labomatic .R ;
27
+ import ai .labomatic .ui .BarcodeReader .BarcodeCaptureActivity ;
26
28
import ai .labomatic .ui .NavigationMenu ;
29
+ import ai .labomatic .util .Initializer ;
27
30
28
31
import com .google .android .gms .tasks .OnCompleteListener ;
29
32
import com .google .android .gms .tasks .Task ;
32
35
import com .google .firebase .auth .FirebaseAuthUserCollisionException ;
33
36
import com .google .firebase .auth .FirebaseUser ;
34
37
38
+ import net .igenius .mqttservice .MQTTServiceReceiver ;
39
+
35
40
import java .util .List ;
36
41
37
42
/**
@@ -64,6 +69,10 @@ public class LoginActivity extends Activity {
64
69
// Firebase
65
70
private FirebaseAuth mAuth ;
66
71
72
+ // Authentication states
73
+ private boolean firebaseAuthentication = false ;
74
+ private boolean mqttAuthentication = false ;
75
+
67
76
@ Override
68
77
protected void onCreate (Bundle savedInstanceState ) {
69
78
super .onCreate (savedInstanceState );
@@ -223,11 +232,18 @@ public void onStart(){
223
232
anim .start ();
224
233
}
225
234
235
+ @ Override
236
+ public void onResume (){
237
+ super .onResume ();
238
+ receiver .register (this );
239
+ }
240
+
226
241
@ Override
227
242
public void onPause (){
228
243
super .onPause ();
229
244
if (anim != null && anim .isRunning ())
230
245
anim .stop ();
246
+ receiver .unregister (this );
231
247
}
232
248
233
249
public boolean validatePassword (String password ){
@@ -325,10 +341,10 @@ public void onComplete(@NonNull Task<AuthResult> task) {
325
341
// Sign in success, update UI with the signed-in user's information
326
342
Log .i (TAG , "signInWithEmail:success" );
327
343
FirebaseUser user = mAuth .getCurrentUser ();
328
- progressDialog . dismiss ();
329
- Intent menuActivity = new Intent ( LoginActivity . this ,
330
- NavigationMenu . class );
331
- startActivity ( menuActivity );
344
+ // Change firebase auth variable to true
345
+ firebaseAuthentication = true ;
346
+ // try to start the next activity
347
+ validateConnections ( );
332
348
} else {
333
349
// If sign in fails, display a message to the user.
334
350
Log .e (TAG , "signInWithEmail:failure" , task .getException ());
@@ -355,15 +371,15 @@ public void onComplete(@NonNull Task<AuthResult> task) {
355
371
FirebaseUser user = mAuth .getCurrentUser ();
356
372
// Save in local database
357
373
db .createUser (new User (email , password ));
358
- // Kill progressDialog
359
- progressDialog . dismiss ( );
374
+ // If creating user was successful, then sign in
375
+ firebaseSignInWithEmailAndPassword ( email + emailExtension , password );
360
376
} else {
361
377
if (task .getException () instanceof FirebaseAuthUserCollisionException ) {
362
378
showToast ("User with this email already exist." );
363
379
// Save in local database
364
380
db .createUser (new User (email , password ));
365
- // Kill progressDialog
366
- progressDialog . dismiss ( );
381
+ // User already exists, so save in db and sign in
382
+ firebaseSignInWithEmailAndPassword ( email + emailExtension , password );
367
383
} else {
368
384
// If sign in fails, display a message to the user.
369
385
Log .e (TAG , "createUserWithEmail:failure" , task .getException ());
@@ -377,6 +393,64 @@ public void onComplete(@NonNull Task<AuthResult> task) {
377
393
});
378
394
}
379
395
396
+ /**
397
+ * Check mqtt and firebase are connected and start the next activity.
398
+ * */
399
+ public void validateConnections (){
400
+ if (mqttAuthentication == true && firebaseAuthentication == true ){
401
+ // If both variables are set to true, then start the next activity
402
+ progressDialog .dismiss ();
403
+ Intent intent = new Intent (LoginActivity .this , NavigationMenu .class );
404
+ startActivity (intent );
405
+ } else {
406
+ Log .i (TAG , "both services are not connected yet" );
407
+ }
408
+ }
409
+
410
+ // MQTT receiver
411
+ private MQTTServiceReceiver receiver =
412
+ new MQTTServiceReceiver () {
413
+ private static final String TAG = "Receiver" ;
414
+ @ Override
415
+ public void onSubscriptionSuccessful (Context context , String requestId , String topic ) {
416
+ Log .i (TAG , "Subscribed to " + topic );
417
+ }
418
+ @ Override
419
+ public void onSubscriptionError (Context context , String requestId , String topic , Exception exception ) {
420
+ Log .i (TAG , "Can't subscribe to " + topic , exception );
421
+ }
422
+ @ Override
423
+ public void onPublishSuccessful (Context context , String requestId , String topic ) {
424
+ Log .i (TAG , "Successfully published on topic: " + topic );
425
+ }
426
+ @ Override
427
+ public void onMessageArrived (Context context , String topic , byte [] payload ) {
428
+ Log .i (TAG , "New message on " + topic + ": " + new String (payload ));
429
+ }
430
+
431
+ @ Override
432
+ public void onConnectionSuccessful (Context context , String requestId ) {
433
+ // Feedback
434
+ showToast ("Connected" );
435
+ Log .i (TAG , "Connected!" );
436
+ // Change mqtt auth variable to true
437
+ mqttAuthentication = true ;
438
+ // Try to move to the next activity
439
+ validateConnections ();
440
+ }
441
+
442
+ @ Override
443
+ public void onException (Context context , String requestId , Exception exception ) {
444
+ exception .printStackTrace ();
445
+ Log .i (TAG , requestId + " exception" );
446
+ }
447
+
448
+ @ Override
449
+ public void onConnectionStatus (Context context , boolean connected ) {
450
+
451
+ }
452
+ };
453
+
380
454
public void showToast (String message ){
381
455
Toast .makeText (LoginActivity .this , message , Toast .LENGTH_SHORT ).show ();
382
456
}
0 commit comments