use of org.edx.mobile.task.RegisterTask in project edx-app-android by edx.
the class RegisterActivity method createAccount.
private void createAccount() {
boolean hasError = false;
// prepare query (POST body)
Bundle parameters = new Bundle();
for (IRegistrationFieldView v : mFieldViews) {
if (v.isValidInput()) {
if (v.hasValue()) {
// we submit the field only if it provides a value
parameters.putString(v.getField().getName(), v.getCurrentValue().getAsString());
}
} else {
if (!hasError) {
// this is the first input field with error,
// so focus on it after showing the popup
showErrorPopup(v.getOnErrorFocusView());
}
hasError = true;
}
}
// set honor_code and terms_of_service to true
parameters.putString("honor_code", "true");
parameters.putString("terms_of_service", "true");
// set parameter required by social registration
final String access_token = loginPrefs.getSocialLoginAccessToken();
final String provider = loginPrefs.getSocialLoginProvider();
boolean fromSocialNet = !TextUtils.isEmpty(access_token);
if (fromSocialNet) {
parameters.putString("access_token", access_token);
parameters.putString("provider", provider);
parameters.putString("client_id", environment.getConfig().getOAuthClientId());
}
// do NOT proceed if validations are failed
if (hasError) {
return;
}
// Send analytics event for Create Account button click
final String appVersion = String.format("%s v%s", getString(R.string.android), BuildConfig.VERSION_NAME);
environment.getAnalyticsRegistry().trackCreateAccountClicked(appVersion, provider);
showProgress();
final SocialFactory.SOCIAL_SOURCE_TYPE backsourceType = SocialFactory.SOCIAL_SOURCE_TYPE.fromString(provider);
final RegisterTask task = new RegisterTask(this, parameters, access_token, backsourceType) {
@Override
public void onSuccess(AuthResponse auth) {
environment.getAnalyticsRegistry().trackRegistrationSuccess(appVersion, provider);
onUserLoginSuccess(auth.profile);
}
@Override
public void onException(Exception ex) {
hideProgress();
if (ex instanceof LoginAPI.RegistrationException) {
final FormFieldMessageBody messageBody = ((LoginAPI.RegistrationException) ex).getFormErrorBody();
boolean errorShown = false;
for (String key : messageBody.keySet()) {
if (key == null)
continue;
for (IRegistrationFieldView fieldView : mFieldViews) {
if (key.equalsIgnoreCase(fieldView.getField().getName())) {
List<RegisterResponseFieldError> error = messageBody.get(key);
showErrorOnField(error, fieldView);
if (!errorShown) {
// this is the first input field with error,
// so focus on it after showing the popup
showErrorPopup(fieldView.getOnErrorFocusView());
errorShown = true;
}
break;
}
}
}
if (errorShown) {
// Return here to avoid falling back to the generic error handler.
return;
}
}
// If app version is un-supported
if (ex instanceof HttpStatusException && ((HttpStatusException) ex).getStatusCode() == HttpStatus.UPGRADE_REQUIRED) {
RegisterActivity.this.showAlertDialog(null, getString(R.string.app_version_unsupported_register_msg), getString(R.string.label_update), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AppStoreUtils.openAppInAppStore(RegisterActivity.this);
}
}, getString(android.R.string.cancel), null);
} else {
RegisterActivity.this.showAlertDialog(null, ErrorUtils.getErrorMessage(ex, RegisterActivity.this));
}
}
};
task.execute();
}
Aggregations