4
4
import android .os .Bundle ;
5
5
import android .support .annotation .NonNull ;
6
6
import android .support .v7 .app .AppCompatActivity ;
7
- import android .util .Log ;
8
7
import android .view .View ;
9
8
import android .widget .Button ;
10
9
import android .widget .TextView ;
20
19
import com .google .android .gms .common .api .PendingResult ;
21
20
import com .google .android .gms .common .api .ResultCallback ;
22
21
import com .google .android .gms .common .api .Status ;
22
+ import com .google .firebase .auth .FirebaseAuth ;
23
+ import com .google .firebase .auth .FirebaseUser ;
24
+
25
+ import timber .log .Timber ;
23
26
24
27
public class MainActivity
25
28
extends AppCompatActivity
@@ -29,6 +32,8 @@ public class MainActivity
29
32
private Button signOutButton ;
30
33
TextView statusTextView ;
31
34
GoogleApiClient mGoogleApiClient ;
35
+ FirebaseAuth firebaseAuth ;
36
+ FirebaseAuth .AuthStateListener authStateListener ;
32
37
private static final String TAG = "SignInActivity" ;
33
38
private static final int RC_SIGN_IN = 9001 ;
34
39
@@ -37,6 +42,19 @@ protected void onCreate(Bundle savedInstanceState) {
37
42
super .onCreate (savedInstanceState );
38
43
setContentView (R .layout .activity_main );
39
44
45
+ firebaseAuth = FirebaseAuth .getInstance ();
46
+ authStateListener = new FirebaseAuth .AuthStateListener () {
47
+ @ Override
48
+ public void onAuthStateChanged (@ NonNull FirebaseAuth firebaseAuth ) {
49
+ FirebaseUser currentUser = firebaseAuth .getCurrentUser ();
50
+ if (currentUser != null ) {
51
+ Timber .d ("Current loggedin user is %s" , currentUser .getDisplayName ());
52
+ } else {
53
+ Timber .d ("User signed out.." );
54
+ }
55
+ }
56
+ };
57
+
40
58
GoogleSignInOptions gso = new GoogleSignInOptions
41
59
.Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
42
60
.requestEmail ()
@@ -55,6 +73,20 @@ protected void onCreate(Bundle savedInstanceState) {
55
73
signOutButton .setOnClickListener (this );
56
74
}
57
75
76
+ @ Override
77
+ protected void onStart () {
78
+ super .onStart ();
79
+ firebaseAuth .addAuthStateListener (authStateListener );
80
+ }
81
+
82
+ @ Override
83
+ protected void onStop () {
84
+ super .onStop ();
85
+ if (firebaseAuth != null ) {
86
+ firebaseAuth .removeAuthStateListener (authStateListener );
87
+ }
88
+ }
89
+
58
90
@ Override
59
91
public void onClick (View v ) {
60
92
switch (v .getId ()){
@@ -65,15 +97,13 @@ public void onClick(View v) {
65
97
signOut ();
66
98
break ;
67
99
default :
68
- Log .d (MainActivity . class . getCanonicalName (), "Unknown id clicked : " + v .getId ());
100
+ Timber .d ("Unknown id clicked : %d" , v .getId ());
69
101
break ;
70
-
71
-
72
102
}
73
103
}
74
104
75
105
private void signOut () {
76
- Log .d (MainActivity . class . getCanonicalName (), "Signing out..." );
106
+ Timber .d ("Signing out..." );
77
107
PendingResult <Status > statusPendingResult = Auth .GoogleSignInApi .signOut (mGoogleApiClient );
78
108
statusPendingResult .setResultCallback (new ResultCallback <Status >() {
79
109
@ Override
@@ -100,7 +130,7 @@ private void signIn() {
100
130
@ Override
101
131
public void onConnectionFailed (@ NonNull ConnectionResult connectionResult ) {
102
132
// An unresolvable error has occurred and Google APIs won't be available.
103
- Log .d (TAG , "onConnectionFailed:" + connectionResult );
133
+ Timber .d ( "onConnectionFailed: %s" , connectionResult . getErrorMessage () );
104
134
}
105
135
106
136
@ Override
0 commit comments