Created
January 27, 2022 12:00
-
-
Save nurcinozer/e7f084386ba2c0f7f064cd59f053ae27 to your computer and use it in GitHub Desktop.
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
public class LoginActivity extends AppCompatActivity { | |
// AccountAuthService provides a set of APIs, including silentSignIn, getSignInIntent, and signOut. | |
private AccountAuthService mAuthService; | |
// Set HUAWEI ID sign-in authorization parameters. | |
private AccountAuthParams mAuthParam; | |
// Define the request code for signInIntent. | |
private static final int REQUEST_CODE_SIGN_IN = 1000; | |
// Define the log flag. | |
private static final String TAG = "Account"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
findViewById(R.id.challenge_silent_signin).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
silentSignInByHwId(); | |
} | |
}); | |
} | |
/** | |
* Silent sign-in: If a user has authorized your app and signed in, no authorization or sign-in screen will appear during subsequent sign-ins, and the user will directly sign in. | |
* After a successful silent sign-in, the HUAWEI ID information will be returned in the success event listener. | |
* If the user has not authorized your app or signed in, the silent sign-in will fail. In this case, your app will show the authorization or sign-in screen to the user. | |
*/ | |
private void silentSignInByHwId() { | |
mAuthParam = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM) | |
.setEmail() | |
.setIdToken() | |
.createParams(); | |
mAuthService = AccountAuthManager.getService(this, mAuthParam); | |
// Sign in with a HUAWEI ID silently. | |
Task<AuthAccount> task = mAuthService.silentSignIn(); | |
task.addOnSuccessListener(new OnSuccessListener<AuthAccount>() { | |
@Override | |
public void onSuccess(AuthAccount authAccount) { | |
dealWithResultOfSignIn(authAccount); | |
} | |
}); | |
task.addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(Exception e) { | |
Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_SHORT).show(); | |
if (e instanceof ApiException) { | |
ApiException apiException = (ApiException) e; | |
Intent signInIntent = mAuthService.getSignInIntent(); | |
signInIntent.putExtra(CommonConstant.RequestParams.IS_FULL_SCREEN, true); | |
startActivityForResult(signInIntent, REQUEST_CODE_SIGN_IN); | |
} | |
} | |
}); | |
} | |
private void dealWithResultOfSignIn(AuthAccount authAccount) { | |
if (authAccount.getIdToken() != null) { | |
Toast.makeText(this, "idToken:" + authAccount.getIdToken(), Toast.LENGTH_SHORT).show(); | |
Intent i = new Intent(getBaseContext(), MovieSearchActivity.class); | |
startActivity(i); | |
} | |
else { | |
Toast.makeText(this, "ID Token is not found!", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == REQUEST_CODE_SIGN_IN) { | |
Log.i(TAG, "onActivitResult of sigInInIntent, request code: " + REQUEST_CODE_SIGN_IN); | |
Task<AuthAccount> authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data); | |
if (authAccountTask.isSuccessful()) { | |
// The sign-in is successful, and the authAccount object that contains the HUAWEI ID information is obtained. | |
AuthAccount authAccount = authAccountTask.getResult(); | |
dealWithResultOfSignIn(authAccount); | |
Log.i(TAG, "onActivitResult of sigInInIntent, request code: " + REQUEST_CODE_SIGN_IN); | |
} else { | |
// The sign-in fails. Find the cause from the status code. For more information, please refer to Error Codes. | |
Log.e(TAG, "sign in failed : " +((ApiException)authAccountTask.getException()).getStatusCode()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment