Skip to content

Commit d2bb774

Browse files
committed
Added Google sign-in
1 parent 19764ca commit d2bb774

File tree

13 files changed

+311
-18
lines changed

13 files changed

+311
-18
lines changed

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ dependencies {
2727
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
2828
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
2929
implementation 'com.google.firebase:firebase-auth:19.3.2'
30+
// implementation 'com.google.firebase:firebase-core:19.3.2'
3031
implementation 'com.google.firebase:firebase-database:19.3.1'
3132
testImplementation 'junit:junit:4.12'
3233
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
3334
implementation 'com.google.android.material:material:1.1.0-alpha10'
3435
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
3536
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5'
3637
implementation "androidx.cardview:cardview:1.0.0"
37-
38+
implementation 'com.google.android.gms:play-services-auth:19.0.0'
3839

3940
}

app/release/app-release.apk

338 KB
Binary file not shown.

app/src/main/java/com/release/virtualinstructor/register.java

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,84 @@
88
import androidx.annotation.NonNull;
99
import androidx.fragment.app.Fragment;
1010

11+
import android.util.Log;
1112
import android.util.Patterns;
1213
import android.view.LayoutInflater;
1314
import android.view.View;
1415
import android.view.ViewGroup;
1516
import android.widget.Button;
1617
import android.widget.EditText;
18+
import android.widget.ImageView;
1719
import android.widget.RadioButton;
1820
import android.widget.RadioGroup;
1921
import 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;
2032
import com.google.android.gms.tasks.OnCompleteListener;
2133
import com.google.android.gms.tasks.Task;
34+
import com.google.firebase.auth.AuthCredential;
2235
import com.google.firebase.auth.AuthResult;
2336
import com.google.firebase.auth.FirebaseAuth;
2437
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
2538
import com.google.firebase.auth.FirebaseUser;
39+
import com.google.firebase.auth.GoogleAuthProvider;
2640
import com.google.firebase.database.DatabaseReference;
2741
import 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

3049
public 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
}

app/src/main/java/com/release/virtualinstructor/spalsh_activity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protected void onCreate(Bundle savedInstanceState) {
1818
setContentView(R.layout.activity_spalsh_activity);
1919

2020
timer = new Timer();
21+
2122
timer.schedule(new TimerTask() {
2223
@Override
2324
public void run() {

app/src/main/res/layout/activity_channel1_med_list.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
android:id="@+id/med1_text"
8282
android:layout_width="wrap_content"
8383
android:layout_height="wrap_content"
84-
android:text="Msin building"
84+
android:text="Main building"
8585
android:textSize="20sp"
8686
android:fontFamily="@font/century_gothic"
8787
android:layout_centerVertical="true"

app/src/main/res/layout/activity_main_login.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@
8888
android:text="Register"
8989
android:textSize="20sp"
9090
android:fontFamily="@font/candara"
91-
android:background="@drawable/checked_bg"
91+
android:background="@drawable/unchecked_bg"
9292
android:paddingStart="20sp"
9393
android:layout_marginEnd="15sp"
94-
android:textColor="@color/white"
94+
android:textColor="@color/colorPrimary"
9595
android:paddingEnd="20sp"
9696
android:layout_toStartOf="@id/center_line"
9797
android:textAllCaps="false"
@@ -143,8 +143,8 @@
143143
android:textSize="20sp"
144144
android:layout_marginStart="15sp"
145145
android:layout_toEndOf="@id/center_line"
146-
android:textColor="@color/colorPrimary"
147-
android:background="@drawable/unchecked_bg"
146+
android:textColor="@color/white"
147+
android:background="@drawable/checked_bg"
148148
android:fontFamily="@font/candara"
149149
android:text="Log IN"
150150
android:visibility="gone"

0 commit comments

Comments
 (0)