package design.nxn.login_template_01;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.auth.api.Auth;
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.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import java.text.BreakIterator;
import java.util.Objects;
import design.nxn.login_template_01.Tools.CustomViewPager;
import design.nxn.login_template_01.Tools.ViewPagerAdapter;
public class LoginActivity extends BaseActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
private static final String TAG = BaseActivity.class.getSimpleName();
private static final int RC_SIGN_IN = 420;
private GoogleApiClient mGoogleApiClient;
private ProgressDialog mProgressDialog;
private SignInButton btnSignIn;
private GoogleSignInClient mGoogleSignInClient;
private String personPhotoUrl;
GoogleSignInOptions gso;
BreakIterator mStatusTextView;
private static final int LOGIN_FRAGMENT = 0;
private static final int SIGNUP_FRAGMENT = 1;
private static final int RESET_PASSWORD_FRAGMENT = 2;
private Fragment newFragment;
private CustomViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnSignIn = (SignInButton)findViewById(R.id.sign_in_button);
initializeGPlusSettings();
initializeControls();
findViewById(R.id.sign_in_button).setOnClickListener(this);
viewPager = findViewById(R.id.viewpager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.setPagingEnabled(false);
changeFragment(LOGIN_FRAGMENT);
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
private void initializeControls() {
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
}
public void onStart() {
super.onStart();
GoogleSignInAccount account =
GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task =
GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleGPlusSignInResult(GoogleSignInResult result) {
Log.d(TAG,"handleSignInResult:" +result.isSuccess());
if (result.isSuccess()){
GoogleSignInAccount acct = result.getSignInAccount();
String personname = acct.getDisplayName();
personPhotoUrl = Objects.requireNonNull(acct.getPhotoUrl()).toString();
String personEmail = acct.getEmail();
Intent intent = new Intent(LoginActivity.this,SecondActivity.class);
intent.putExtra("personname",personname);
intent.putExtra("email",personEmail);
intent.putExtra("personphotoUrl",personPhotoUrl);
startActivity(intent);
finish();
}
}
private void handleSignInResult(Task<GoogleSignInAccount> task) {
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
}
catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
private void initializeGPlusSettings() {
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
// Build a GoogleSignInClient with the options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();
btnSignIn.setSize(SignInButton.SIZE_STANDARD);
}
private void changeFragment(int fragmentType) {
switch (fragmentType) {
case LOGIN_FRAGMENT:
viewPager.setCurrentItem(LOGIN_FRAGMENT);
break;
case SIGNUP_FRAGMENT:
viewPager.setCurrentItem(SIGNUP_FRAGMENT);
break;
case RESET_PASSWORD_FRAGMENT:
viewPager.setCurrentItem(RESET_PASSWORD_FRAGMENT);
break;
default:
viewPager.setCurrentItem(LOGIN_FRAGMENT);
break;
}
}
public void signUpClick(View view) {
changeFragment(SIGNUP_FRAGMENT);
}
public void signInClick(View view) {
changeFragment(LOGIN_FRAGMENT);
}
public void resetPasswordClick(View view) {
changeFragment(RESET_PASSWORD_FRAGMENT);
}
public void backClick(View view){
changeFragment(LOGIN_FRAGMENT);
}
@Override
public void onBackPressed() {
if (viewPager.getCurrentItem() == LOGIN_FRAGMENT)
super.onBackPressed();
else {
changeFragment(LOGIN_FRAGMENT);
}
}
public void logInButtonClicked() {
Toast.makeText(this, R.string.login_button_click, Toast.LENGTH_SHORT).show();
}
public void signUpButtonClicked() {
Toast.makeText(this, R.string.signup_button_click, Toast.LENGTH_SHORT).show();
}
public void resetPasswordButtonClicked() {
Toast.makeText(this, R.string.reset_password_button_clicked, Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sign_in_button:
signIn();
case R.id.logout:
signOut();
break;
}
}
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
}
private void revokeAccess() {
mGoogleSignInClient.revokeAccess()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void updateUI(GoogleSignInAccount account) {
if (account != null) {
mStatusTextView.setText(getString(R.string.signed_in_fmt, account.getDisplayName()));
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.logout).setVisibility(View.VISIBLE);
}
else {
mStatusTextView.setText(String.valueOf(R.string.signed_out));
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.logout).setVisibility(View.GONE);
}
}
}
答案 0 :(得分:0)
您正在设置点击此处
switch (view.getId()) {
case R.id.sign_in_button:
signIn();
根据此代码,您需要使用
btnSignIn.setOnClickListener(this);
而不是
findViewById(R.id.sign_in_button).setOnClickListener(this);