use of com.onesignal.sdktest.callback.SMSUpdateCallback in project OneSignal-Android-SDK by OneSignal.
the class Dialog method createUpdateAlertDialog.
/**
* Create an AlertDialog for when the user updates a single value field
* Click OK to verify and update the field being updated
*/
public void createUpdateAlertDialog(final String content, final ProfileUtil.FieldType field, final UpdateAlertDialogCallback callback) {
View updateAlertDialogView = layoutInflater.inflate(R.layout.update_alert_dialog_layout, null, false);
final TextInputLayout updateAlertDialogTextInputLayout = updateAlertDialogView.findViewById(R.id.update_alert_dialog_text_input_layout);
final EditText updateAlertDialogEditText = updateAlertDialogView.findViewById(R.id.update_alert_dialog_edit_text);
final ProgressBar updateAlertDialogProgressBar = updateAlertDialogView.findViewById(R.id.update_alert_dialog_progress_bar);
String hintTitle = "New " + field.getTitle();
updateAlertDialogTextInputLayout.setHint(hintTitle);
updateAlertDialogEditText.setText(content);
font.applyFont(updateAlertDialogTextInputLayout, font.saralaBold);
font.applyFont(updateAlertDialogEditText, font.saralaBold);
final CustomAlertDialogBuilder updateAlertDialog = new CustomAlertDialogBuilder(context, updateAlertDialogView);
updateAlertDialog.setView(updateAlertDialogView);
updateAlertDialog.setIsCancelable(true);
updateAlertDialog.setCanceledOnTouchOutside(false);
updateAlertDialog.setPositiveButton(Text.BUTTON_UPDATE, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
toggleUpdateAlertDialogAttributes(true);
final String newContent = updateAlertDialogEditText.getText().toString().trim();
if (newContent.equals(content)) {
InterfaceUtil.hideKeyboardFrom(context, updateAlertDialogEditText);
toggleUpdateAlertDialogAttributes(false);
dialog.dismiss();
} else if (ProfileUtil.isContentValid(field, updateAlertDialogTextInputLayout)) {
InterfaceUtil.hideKeyboardFrom(context, updateAlertDialogEditText);
switch(field) {
case APP_ID:
updateAppId(dialog, newContent);
break;
case EMAIL:
updateEmail(dialog, newContent);
break;
case SMS:
updateSMsNumber(dialog, newContent);
break;
case EXTERNAL_USER_ID:
updateExternalUserId(dialog, newContent);
break;
}
} else {
toggleUpdateAlertDialogAttributes(false);
}
}
private void toggleUpdateAlertDialogAttributes(boolean disableAttributes) {
int progressVisibility = disableAttributes ? View.VISIBLE : View.GONE;
updateAlertDialogProgressBar.setVisibility(progressVisibility);
int buttonVisibility = disableAttributes ? View.GONE : View.VISIBLE;
updateAlertDialog.getPositiveButtonElement().setVisibility(buttonVisibility);
updateAlertDialog.getNegativeButtonElement().setVisibility(buttonVisibility);
updateAlertDialog.getPositiveButtonElement().setEnabled(!disableAttributes);
updateAlertDialog.getNegativeButtonElement().setEnabled(!disableAttributes);
updateAlertDialog.setIsCancelable(!disableAttributes);
}
/**
* Handles changing the app id for the SDK by reinitializing and caching
*/
private void updateAppId(DialogInterface dialog, String appId) {
OneSignal.setAppId(appId);
SharedPreferenceUtil.cacheOneSignalAppId(context, appId);
toggleUpdateAlertDialogAttributes(false);
dialog.dismiss();
callback.onSuccess(appId);
}
/**
* Updates the email attached to the device and caches
*/
private void updateEmail(final DialogInterface dialog, final String email) {
currentUser.setEmail(email, new EmailUpdateCallback() {
@Override
public void onSuccess() {
SharedPreferenceUtil.cacheUserEmail(context, email);
Log.d(Tag.DEBUG, Text.EMAIL_SET_SUCCESSFULLY);
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
toggleUpdateAlertDialogAttributes(false);
dialog.dismiss();
callback.onSuccess(email);
}
});
}
@Override
public void onFailure() {
Log.d(Tag.ERROR, Text.EMAIL_SET_FAILURE);
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
toggleUpdateAlertDialogAttributes(false);
dialog.dismiss();
callback.onFailure();
}
});
}
});
}
/**
* Updates the SMS number attached to the device and caches
*/
private void updateSMsNumber(final DialogInterface dialog, final String smsNumber) {
currentUser.setSMSNumber(smsNumber, new SMSUpdateCallback() {
@Override
public void onSuccess() {
SharedPreferenceUtil.cacheUserSMSNumber(context, smsNumber);
Log.d(Tag.DEBUG, Text.SMS_SET_SUCCESSFULLY);
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
toggleUpdateAlertDialogAttributes(false);
dialog.dismiss();
callback.onSuccess(smsNumber);
}
});
}
@Override
public void onFailure() {
Log.d(Tag.ERROR, Text.SMS_SET_FAILURE);
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
toggleUpdateAlertDialogAttributes(false);
dialog.dismiss();
callback.onFailure();
}
});
}
});
}
/**
* Set external id attached to the user/email of the device
*/
private void updateExternalUserId(final DialogInterface dialog, final String externalUserId) {
OneSignal.setExternalUserId(externalUserId, new OneSignal.OSExternalUserIdUpdateCompletionHandler() {
@Override
public void onSuccess(JSONObject results) {
// Default success to false until we know push came back successful
boolean successful = false;
// Check push exists with success status and success status is true
if (isExternalUserIdPushSuccessful(results)) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "Push channel external user id set successfully");
SharedPreferenceUtil.cacheUserExternalUserId(context, externalUserId);
successful = true;
}
// Check email exists with success status and success status is true
if (isExternalUserIdEmailSuccessful(results)) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "Email channel external user id set successfully");
}
// We could eventually check email also but not important for now
if (successful)
callback.onSuccess(externalUserId);
else
callback.onFailure();
toggleUpdateAlertDialogAttributes(false);
dialog.dismiss();
}
@Override
public void onFailure(OneSignal.ExternalIdError error) {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.VERBOSE, "External user id set failed with error: " + error);
callback.onFailure();
dialog.dismiss();
}
/**
* Parse the results of the external user id completion callback and make sure push.success = true
*/
private boolean isExternalUserIdPushSuccessful(JSONObject status) {
boolean successful = false;
try {
if (!status.has("push"))
return false;
JSONObject pushStatus = status.getJSONObject("push");
if (!pushStatus.has("success"))
return false;
successful = pushStatus.getBoolean("success");
} catch (JSONException e) {
e.printStackTrace();
}
return successful;
}
/**
* Parse the results of the external user id completion callback and make sure email.success = true
*/
private boolean isExternalUserIdEmailSuccessful(JSONObject status) {
boolean successful = false;
try {
if (!status.has("email"))
return false;
JSONObject emailStatus = status.getJSONObject("email");
if (!emailStatus.has("success"))
return false;
successful = emailStatus.getBoolean("success");
} catch (JSONException e) {
e.printStackTrace();
}
return successful;
}
});
}
}).setNegativeButton(Text.BUTTON_CANCEL, null);
updateAlertDialog.show();
updateAlertDialogEditText.requestFocus();
}
Aggregations