-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f58bbf
commit 60ea15e
Showing
11 changed files
with
275 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
app/src/main/java/com/example/todo_app/LoginActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.example.todo_app; | ||
|
||
import androidx.activity.result.ActivityResult; | ||
import androidx.activity.result.ActivityResultCallback; | ||
import androidx.activity.result.ActivityResultLauncher; | ||
import androidx.activity.result.contract.ActivityResultContracts; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.Toast; | ||
|
||
import com.google.android.gms.auth.api.signin.GoogleSignIn; | ||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount; | ||
import com.google.android.gms.auth.api.signin.GoogleSignInClient; | ||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions; | ||
import com.google.android.gms.common.SignInButton; | ||
import com.google.android.gms.common.api.ApiException; | ||
import com.google.android.gms.tasks.Task; | ||
|
||
public class LoginActivity extends AppCompatActivity { | ||
GoogleSignInClient mGoogleSignInClient; | ||
Button signInButton; | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.loginactivity); | ||
|
||
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | ||
.requestEmail() | ||
.build(); | ||
|
||
mGoogleSignInClient = GoogleSignIn.getClient(this,gso); | ||
|
||
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this); | ||
|
||
signInButton = findViewById(R.id.Loginbutton); | ||
|
||
|
||
signInButton.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
Intent signInIntent = mGoogleSignInClient.getSignInIntent(); | ||
startactivityresult.launch(signInIntent); | ||
} | ||
}); | ||
} | ||
|
||
public void updateUI(GoogleSignInAccount account){ | ||
|
||
Button signoutButton=findViewById(R.id.sign_out_button); | ||
if(account!=null){ | ||
String personName = account.getDisplayName(); | ||
String personGivenName = account.getGivenName(); | ||
String personFamilyName = account.getFamilyName(); | ||
String personEmail = account.getEmail(); | ||
String personId = account.getId(); | ||
Uri personPhoto = account.getPhotoUrl(); | ||
final Intent i=new Intent(LoginActivity.this,MainActivity.class); | ||
startActivity(i); | ||
|
||
|
||
} | ||
else{ | ||
Toast.makeText(this, "log in failed! try again", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
|
||
ActivityResultLauncher<Intent> startactivityresult=registerForActivityResult( | ||
new ActivityResultContracts.StartActivityForResult(), | ||
new ActivityResultCallback<ActivityResult>() { | ||
@Override | ||
public void onActivityResult(ActivityResult result) { | ||
if(result.getResultCode()== Activity.RESULT_OK){ | ||
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(result.getData()); | ||
handleSignInResult(task); | ||
} | ||
} | ||
} | ||
); | ||
|
||
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) { | ||
try { | ||
GoogleSignInAccount account = completedTask.getResult(ApiException.class); | ||
updateUI(account); | ||
// Signed in successfully, show authenticated UI. | ||
Toast.makeText(this, "Congratulations ! successfully signed in.", Toast.LENGTH_SHORT).show(); | ||
|
||
} catch (ApiException e) { | ||
// The ApiException status code indicates the detailed failure reason. | ||
// Please refer to the GoogleSignInStatusCodes class reference for more information. | ||
Log.d("GOOGLE ERROR",e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.