88import androidx .annotation .NonNull ;
99import androidx .fragment .app .Fragment ;
1010
11+ import android .util .Log ;
1112import android .util .Patterns ;
1213import android .view .LayoutInflater ;
1314import android .view .View ;
1415import android .view .ViewGroup ;
1516import android .widget .Button ;
1617import android .widget .EditText ;
18+ import android .widget .ImageView ;
1719import android .widget .RadioButton ;
1820import android .widget .RadioGroup ;
1921import android .widget .Toast ;
22+
23+ import com .google .android .gms .auth .api .Auth ;
24+ import com .google .android .gms .auth .api .signin .GoogleSignIn ;
25+ import com .google .android .gms .auth .api .signin .GoogleSignInAccount ;
26+ import com .google .android .gms .auth .api .signin .GoogleSignInClient ;
27+ import com .google .android .gms .auth .api .signin .GoogleSignInOptions ;
28+ import com .google .android .gms .auth .api .signin .GoogleSignInResult ;
29+ import com .google .android .gms .common .SignInButton ;
30+ import com .google .android .gms .common .api .ApiException ;
31+ import com .google .android .gms .common .api .GoogleApiClient ;
2032import com .google .android .gms .tasks .OnCompleteListener ;
2133import com .google .android .gms .tasks .Task ;
34+ import com .google .firebase .auth .AuthCredential ;
2235import com .google .firebase .auth .AuthResult ;
2336import com .google .firebase .auth .FirebaseAuth ;
2437import com .google .firebase .auth .FirebaseAuthUserCollisionException ;
2538import com .google .firebase .auth .FirebaseUser ;
39+ import com .google .firebase .auth .GoogleAuthProvider ;
2640import com .google .firebase .database .DatabaseReference ;
2741import com .google .firebase .database .FirebaseDatabase ;
42+ import com .release .virtualinstructor .channel_yoga_list .yoga_channel1_list ;
43+
44+ import java .util .concurrent .Executor ;
45+
46+ import static android .content .ContentValues .TAG ;
2847
2948
3049public class register extends Fragment {
3150
3251
52+ private static final int RC_SIGN_IN = 123 ;
3353 private DatabaseReference databaseReference ;
54+ private GoogleSignInClient mGoogleSignInClient ;
55+ //private GoogleApiClient googleApiClient;
56+ private SignInButton signInButton ;
57+ private String idToken ;
58+ private FirebaseAuth firebaseAuth ;
59+ private FirebaseAuth .AuthStateListener authStateListener ;
60+ private String name ;
61+ private String email ;
62+
63+ // @Override
64+ // public void onStart(){
65+ // super.onStart();
66+ // GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(getActivity());
67+ // if(account!=null){
68+ // Intent i = new Intent(getActivity(), meditation_fragment.class);
69+ // startActivity(i);
70+ // }
71+ // }
72+
73+ @ Override
74+ public void onStart () {
75+ super .onStart ();
76+ if (authStateListener != null ){
77+ FirebaseAuth .getInstance ().signOut ();
78+ }
79+ firebaseAuth .addAuthStateListener (authStateListener );
80+ }
81+
82+ @ Override
83+ public void onStop () {
84+ super .onStop ();
85+ if (authStateListener != null ){
86+ firebaseAuth .removeAuthStateListener (authStateListener );
87+ }
88+ }
3489
3590 @ Override
3691 public View onCreateView (LayoutInflater inflater , ViewGroup container ,
@@ -53,6 +108,23 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
53108 final FirebaseAuth mAuth = FirebaseAuth .getInstance ();
54109 //final FirebaseDatabase database = FirebaseDatabase.getInstance();
55110
111+ createRequest ();
112+ v .findViewById (R .id .googleSignIn ).setOnClickListener (new View .OnClickListener () {
113+ @ Override
114+ public void onClick (View v ) {
115+ signIn ();
116+ }
117+ });
118+ signInButton = v .findViewById (R .id .sign_in_button );
119+ signInButton .setOnClickListener (new View .OnClickListener () {
120+ @ Override
121+ public void onClick (View view ) {
122+ // Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
123+ // startActivityForResult(intent,RC_SIGN_IN);
124+ Intent signInIntent = mGoogleSignInClient .getSignInIntent ();
125+ startActivityForResult (signInIntent , RC_SIGN_IN );
126+ }
127+ });
56128
57129 register .setOnClickListener (new View .OnClickListener () {
58130 @ Override
@@ -138,5 +210,150 @@ public void onComplete(@NonNull Task<AuthResult> task) {
138210 return v ;
139211 }
140212
213+ private void createRequest () {
214+ // Configure Google Sign In
215+ // Configure sign-in to request the user's ID, email address, and basic
216+ // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
217+ GoogleSignInOptions gso = new GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
218+ .requestEmail ()
219+ .build ();
220+
221+ // googleApiClient=new GoogleApiClient.Builder(getActivity())
222+ // .enableAutoManage(getActivity(), this)
223+ // .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
224+ // .build();
225+ // Build a GoogleSignInClient with the options specified by gso.
226+ mGoogleSignInClient = GoogleSignIn .getClient (getActivity (), gso );
227+ }
228+
229+ private void signIn () {
230+ Intent signInIntent = mGoogleSignInClient .getSignInIntent ();
231+ startActivityForResult (signInIntent , RC_SIGN_IN );
232+ // Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
233+ // startActivityForResult(intent,RC_SIGN_IN);
234+ }
235+
236+ @ Override
237+ public void onActivityResult (int requestCode , int resultCode , Intent data ) {
238+ super .onActivityResult (requestCode , resultCode , data );
239+
240+ // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
241+ if (requestCode == RC_SIGN_IN ) {
242+ // The Task returned from this call is always completed, no need to attach
243+ // a listener.
244+ //Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
245+ GoogleSignInResult result = Auth .GoogleSignInApi .getSignInResultFromIntent (data );
246+ // handleSignInResult(task);
247+ handleSignInResult (result );
248+ }
249+ }
250+
251+ private void handleSignInResult (GoogleSignInResult result /*Task<GoogleSignInAccount> completedTask*/ ) {
252+ // try {
253+ // GoogleSignInAccount account = completedTask.getResult(ApiException.class);
254+ //
255+ // // Signed in successfully, show authenticated UI.
256+ // // updateUI(account);
257+ // Intent i = new Intent(getActivity(), meditation_fragment.class);
258+ // startActivity(i);
259+ //
260+ // } catch (ApiException e) {
261+ // // The ApiException status code indicates the detailed failure reason.
262+ // // Please refer to the GoogleSignInStatusCodes class reference for more information.
263+ // Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
264+ // // updateUI(null);
265+ // Toast.makeText(getActivity(), "Sorry auth failed.", Toast.LENGTH_SHORT).show();
266+ // }
267+
268+ if (result .isSuccess ()){
269+ GoogleSignInAccount account = result .getSignInAccount ();
270+ idToken = account .getIdToken ();
271+ name = account .getDisplayName ();
272+ email = account .getEmail ();
273+ // you can store user data to SharedPreference
274+ AuthCredential credential = GoogleAuthProvider .getCredential (idToken , null );
275+ firebaseAuthWithGoogle (credential );
276+ }else {
277+ // Google Sign In failed, update UI appropriately
278+ Log .e (TAG , "Login Unsuccessful. " +result );
279+ Toast .makeText (getActivity (), "Login Unsuccessful" , Toast .LENGTH_SHORT ).show ();
280+ }
281+ }
282+
283+ private void firebaseAuthWithGoogle (AuthCredential credential ){
284+
285+ firebaseAuth .signInWithCredential (credential )
286+ .addOnCompleteListener ((Executor ) this , new OnCompleteListener <AuthResult >() {
287+ @ Override
288+ public void onComplete (@ NonNull Task <AuthResult > task ) {
289+ Log .d (TAG , "signInWithCredential:onComplete:" + task .isSuccessful ());
290+ if (task .isSuccessful ()){
291+ Toast .makeText (getActivity (), "Login successful" , Toast .LENGTH_SHORT ).show ();
292+ gotoProfile ();
293+ }else {
294+ Log .w (TAG , "signInWithCredential" + task .getException ().getMessage ());
295+ task .getException ().printStackTrace ();
296+ Toast .makeText (getActivity (), "Authentication failed." , Toast .LENGTH_SHORT ).show ();
297+ }
298+
299+ }
300+ });
301+ }
302+
303+ private void gotoProfile (){
304+ Intent intent = new Intent (getActivity (), meditation_fragment .class );
305+ intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK );
306+ startActivity (intent );
307+ }
141308
309+ // @Override
310+ // public void onActivityResult(int requestCode, int resultCode, Intent data) {
311+ // super.onActivityResult(requestCode, resultCode, data);
312+ //
313+ //
314+ // // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
315+ // if (requestCode == RC_SIGN_IN) {
316+ // Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
317+ // try {
318+ // // Google Sign In was successful, authenticate with Firebase
319+ // GoogleSignInAccount account = (GoogleSignInAccount) task.getResult(ApiException.class);
320+ // firebaseAuthWithGoogle(account);
321+ // } catch (ApiException e) {
322+ // // Google Sign In failed, update UI appropriately
323+ // // ...
324+ // //Toast.makeText(getActivity(), "Google ", Toast.LENGTH_SHORT).show();
325+ // Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
326+ // } catch (Throwable throwable) {
327+ // throwable.printStackTrace();
328+ // }
329+ // }
330+ // }
331+ //
332+ //
333+ // private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
334+ //
335+ //
336+ // AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
337+ // mAuth.signInWithCredential(credential)
338+ // .addOnCompleteListener(this, new OnCompleteListener() {
339+ // @Override
340+ // public void onComplete(@NonNull Task task) {
341+ // if (task.isSuccessful()) {
342+ // // Sign in success, update UI with the signed-in user's information
343+ // FirebaseUser user = mAuth.getCurrentUser();
344+ // Intent intent = new Intent(getApplicationContext(),Profile.class);
345+ // startActivity(intent);
346+ //
347+ //
348+ // } else {
349+ // Toast.makeText(MainActivity.this, "Sorry auth failed.", Toast.LENGTH_SHORT).show();
350+ //
351+ //
352+ // }
353+ //
354+ //
355+ // // ...
356+ // }
357+ // });
358+ // }
142359}
0 commit comments