1
1
package com.google.samples.quickstart.canonical
2
2
3
+ import android.content.Context
4
+ import android.content.Intent
3
5
import androidx.appcompat.app.AppCompatActivity
4
6
import android.os.Bundle
7
+ import android.util.AttributeSet
8
+ import android.util.Log
9
+ import android.view.View
10
+ import android.widget.Button
5
11
import androidx.fragment.app.FragmentTransaction
12
+ import com.google.android.gms.auth.api.signin.GoogleSignIn
13
+ import com.google.android.gms.auth.api.signin.GoogleSignInClient
14
+ import com.google.android.gms.auth.api.signin.GoogleSignInOptions
15
+ import com.google.android.gms.common.SignInButton
16
+ import com.google.android.gms.common.api.ApiException
6
17
import com.google.android.material.bottomnavigation.BottomNavigationView
18
+ import com.google.android.material.snackbar.Snackbar
19
+ import com.google.firebase.auth.FirebaseAuth
20
+ import com.google.firebase.auth.GoogleAuthProvider
7
21
8
22
class MainActivity : AppCompatActivity () {
23
+ private lateinit var mGoogleSignInClient: GoogleSignInClient
24
+ private lateinit var auth: FirebaseAuth
9
25
10
- override fun onCreate (savedInstanceState : Bundle ? ) {
11
- super .onCreate(savedInstanceState)
12
- setContentView(R .layout.activity_main)
26
+ private fun googleSignInInit () {
27
+ val gso = GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
28
+ .requestIdToken(getString(R .string.default_web_client_id))
29
+ .requestEmail()
30
+ .build()
31
+
32
+ mGoogleSignInClient = GoogleSignIn .getClient(this , gso)
33
+ }
13
34
35
+ private fun signIn () {
36
+ auth = FirebaseAuth .getInstance()
37
+ val signInIntent = mGoogleSignInClient.signInIntent
38
+ startActivityForResult(signInIntent, RC_SIGN_IN )
39
+ }
40
+
41
+ private fun setupNavigationBar () {
14
42
val runFragment = RunFragment ()
15
43
supportFragmentManager
16
44
.beginTransaction()
@@ -57,6 +85,78 @@ class MainActivity : AppCompatActivity() {
57
85
}
58
86
}
59
87
88
+ override fun onCreate (savedInstanceState : Bundle ? ) {
89
+ super .onCreate(savedInstanceState)
90
+ setContentView(R .layout.activity_main)
91
+
92
+ googleSignInInit()
93
+ setupNavigationBar()
94
+ }
95
+
96
+ override fun onStart () {
97
+ super .onStart()
98
+ val account = GoogleSignIn .getLastSignedInAccount(this )
99
+ if (account == null ) {
100
+ setContentView(R .layout.login_in_page)
101
+
102
+ findViewById<SignInButton >(R .id.sign_in_button).setOnClickListener {
103
+ signIn()
104
+ }
105
+
106
+ }
107
+ }
108
+
109
+ override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
110
+ super .onActivityResult(requestCode, resultCode, data)
111
+
112
+ // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
113
+ if (requestCode == RC_SIGN_IN ) {
114
+ val task = GoogleSignIn .getSignedInAccountFromIntent(data)
115
+ if (task.isSuccessful) {
116
+ try {
117
+ // Google Sign In was successful, authenticate with Firebase
118
+ val account = task.getResult(ApiException ::class .java)!!
119
+ Log .d(" MainActivity-Login" , " firebaseAuthWithGoogle:" + account.id)
120
+ firebaseAuthWithGoogle(account.idToken!! )
121
+ setContentView(R .layout.activity_main)
122
+ setupNavigationBar()
123
+ } catch (e: ApiException ) {
124
+ // Google Sign In failed, update UI appropriately
125
+ Log .w(" MainActivity-Login" , " Google sign in failed" , e)
126
+ // ...
127
+ }
128
+ } else {
129
+ Log .w(" MainActivity-Login" , " Google sign in unsuccessful" )
130
+ }
131
+
132
+ }
133
+ }
134
+
135
+ private fun firebaseAuthWithGoogle (idToken : String ) {
136
+ val credential = GoogleAuthProvider .getCredential(idToken, null )
137
+ auth.signInWithCredential(credential)
138
+ .addOnCompleteListener(this ) { task ->
139
+ if (task.isSuccessful) {
140
+ // Sign in success, update UI with the signed-in user's information
141
+ Log .d(" firebaseAuthWithGoogle" , " signInWithCredential:success" )
142
+ val user = auth.currentUser
143
+ } else {
144
+ // If sign in fails, display a message to the user.
145
+ Log .w(" firebaseAuthWithGoogle" , " signInWithCredential:failure" , task.exception)
146
+ // ...
147
+ }
148
+
149
+ // ...
150
+ }
151
+ }
152
+
153
+
154
+
155
+ companion object {
156
+ private const val RC_SIGN_IN = 0
157
+ }
158
+
159
+
60
160
61
161
62
162
}
0 commit comments