-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SignUpConfirm.java
278 lines (235 loc) · 10.6 KB
/
SignUpConfirm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2013-2016 Amazon.com,
* Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the
* License. A copy of the License is located at
*
* http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, express or implied. See the License
* for the specific language governing permissions and
* limitations under the License.
*/
package com.amazonaws.youruserpools;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUserCodeDeliveryDetails;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.GenericHandler;
import com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.VerificationHandler;
import com.amazonaws.youruserpools.CognitoYourUserPoolsDemo.R;
public class SignUpConfirm extends AppCompatActivity {
private EditText username;
private EditText confCode;
private Button confirm;
private TextView reqCode;
private String userName;
private AlertDialog userDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up_confirm);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
TextView main_title = (TextView) findViewById(R.id.confirm_toolbar_title);
main_title.setText("Confirm");
init();
}
private void init() {
Bundle extras = getIntent().getExtras();
if (extras !=null) {
if(extras.containsKey("name")) {
userName = extras.getString("name");
username = (EditText) findViewById(R.id.editTextConfirmUserId);
username.setText(userName);
confCode = (EditText) findViewById(R.id.editTextConfirmCode);
confCode.requestFocus();
if(extras.containsKey("destination")) {
String dest = extras.getString("destination");
String delMed = extras.getString("deliveryMed");
TextView screenSubtext = (TextView) findViewById(R.id.textViewConfirmSubtext_1);
if(dest != null && delMed != null && dest.length() > 0 && delMed.length() > 0) {
screenSubtext.setText("A confirmation code was sent to "+dest+" via "+delMed);
}
else {
screenSubtext.setText("A confirmation code was sent");
}
}
}
else {
TextView screenSubtext = (TextView) findViewById(R.id.textViewConfirmSubtext_1);
screenSubtext.setText("Request for a confirmation code or confirm with the code you already have.");
}
}
username = (EditText) findViewById(R.id.editTextConfirmUserId);
username.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.length() == 0) {
TextView label = (TextView) findViewById(R.id.textViewConfirmUserIdLabel);
label.setText(username.getHint());
username.setBackground(getDrawable(R.drawable.text_border_selector));
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
TextView label = (TextView) findViewById(R.id.textViewConfirmUserIdMessage);
label.setText(" ");
}
@Override
public void afterTextChanged(Editable s) {
if(s.length() == 0) {
TextView label = (TextView) findViewById(R.id.textViewConfirmUserIdLabel);
label.setText("");
}
}
});
confCode = (EditText) findViewById(R.id.editTextConfirmCode);
confCode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.length() == 0) {
TextView label = (TextView) findViewById(R.id.textViewConfirmCodeLabel);
label.setText(confCode.getHint());
confCode.setBackground(getDrawable(R.drawable.text_border_selector));
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
TextView label = (TextView) findViewById(R.id.textViewConfirmCodeMessage);
label.setText(" ");
}
@Override
public void afterTextChanged(Editable s) {
if(s.length() == 0) {
TextView label = (TextView) findViewById(R.id.textViewConfirmCodeLabel);
label.setText("");
}
}
});
confirm = (Button) findViewById(R.id.confirm_button);
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendConfCode();
}
});
reqCode = (TextView) findViewById(R.id.resend_confirm_req);
reqCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
reqConfCode();
}
});
}
private void sendConfCode() {
userName = username.getText().toString();
String confirmCode = confCode.getText().toString();
if(userName == null || userName.length() < 1) {
TextView label = (TextView) findViewById(R.id.textViewConfirmUserIdMessage);
label.setText(username.getHint()+" cannot be empty");
username.setBackground(getDrawable(R.drawable.text_border_error));
return;
}
if(confirmCode == null || confirmCode.length() < 1) {
TextView label = (TextView) findViewById(R.id.textViewConfirmCodeMessage);
label.setText(confCode.getHint()+" cannot be empty");
confCode.setBackground(getDrawable(R.drawable.text_border_error));
return;
}
AppHelper.getPool().getUser(userName).confirmSignUpInBackground(confirmCode, true, confHandler);
}
private void reqConfCode() {
userName = username.getText().toString();
if(userName == null || userName.length() < 1) {
TextView label = (TextView) findViewById(R.id.textViewConfirmUserIdMessage);
label.setText(username.getHint()+" cannot be empty");
username.setBackground(getDrawable(R.drawable.text_border_error));
return;
}
AppHelper.getPool().getUser(userName).resendConfirmationCodeInBackground(resendConfCodeHandler);
}
GenericHandler confHandler = new GenericHandler() {
@Override
public void onSuccess() {
showDialogMessage("Success!",userName+" has been confirmed!", true);
}
@Override
public void onFailure(Exception exception) {
TextView label = (TextView) findViewById(R.id.textViewConfirmUserIdMessage);
label.setText("Confirmation failed!");
username.setBackground(getDrawable(R.drawable.text_border_error));
label = (TextView) findViewById(R.id.textViewConfirmCodeMessage);
label.setText("Confirmation failed!");
confCode.setBackground(getDrawable(R.drawable.text_border_error));
showDialogMessage("Confirmation failed", AppHelper.formatException(exception), false);
}
};
VerificationHandler resendConfCodeHandler = new VerificationHandler() {
@Override
public void onSuccess(CognitoUserCodeDeliveryDetails cognitoUserCodeDeliveryDetails) {
TextView mainTitle = (TextView) findViewById(R.id.textViewConfirmTitle);
mainTitle.setText("Confirm your account");
confCode = (EditText) findViewById(R.id.editTextConfirmCode);
confCode.requestFocus();
showDialogMessage("Confirmation code sent.","Code sent to "+cognitoUserCodeDeliveryDetails.getDestination()+" via "+cognitoUserCodeDeliveryDetails.getDeliveryMedium()+".", false);
}
@Override
public void onFailure(Exception exception) {
TextView label = (TextView) findViewById(R.id.textViewConfirmUserIdMessage);
label.setText("Confirmation code resend failed");
username.setBackground(getDrawable(R.drawable.text_border_error));
showDialogMessage("Confirmation code request has failed", AppHelper.formatException(exception), false);
}
};
private void showDialogMessage(String title, String body, final boolean exitActivity) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title).setMessage(body).setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
userDialog.dismiss();
if(exitActivity) {
exit();
}
} catch (Exception e) {
exit();
}
}
});
userDialog = builder.create();
userDialog.show();
}
private void exit() {
Intent intent = new Intent();
if(userName == null)
userName = "";
intent.putExtra("name",userName);
setResult(RESULT_OK, intent);
finish();
}
}