1package eu.siacs.conversations.ui;
2
3import android.app.AlertDialog;
4import android.content.Intent;
5import android.databinding.DataBindingUtil;
6import android.os.Bundle;
7import android.support.v7.app.AppCompatActivity;
8import android.support.v7.widget.Toolbar;
9import android.text.Editable;
10import android.text.Html;
11import android.text.TextUtils;
12import android.text.TextWatcher;
13import android.util.Log;
14import android.view.KeyEvent;
15import android.view.View;
16import android.widget.TextView;
17
18import eu.siacs.conversations.Config;
19import eu.siacs.conversations.R;
20import eu.siacs.conversations.databinding.ActivityEnterNumberBinding;
21import eu.siacs.conversations.services.QuickConversationsService;
22import eu.siacs.conversations.ui.drawable.TextDrawable;
23import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
24import io.michaelrocks.libphonenumber.android.NumberParseException;
25import io.michaelrocks.libphonenumber.android.PhoneNumberUtil;
26import io.michaelrocks.libphonenumber.android.Phonenumber;
27
28public class EnterPhoneNumberActivity extends XmppActivity implements QuickConversationsService.OnVerificationRequested {
29
30 private static final int REQUEST_CHOOSE_COUNTRY = 0x1234;
31
32 private ActivityEnterNumberBinding binding;
33
34 private String region = null;
35 private boolean requestingVerification = false;
36
37 private final TextWatcher countryCodeTextWatcher = new TextWatcher() {
38 @Override
39 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
40
41 }
42
43 @Override
44 public void onTextChanged(CharSequence s, int start, int before, int count) {
45
46 }
47
48 @Override
49 public void afterTextChanged(Editable editable) {
50 final String text = editable.toString();
51 try {
52 final int oldCode = region != null ? PhoneNumberUtilWrapper.getInstance(EnterPhoneNumberActivity.this).getCountryCodeForRegion(region) : 0;
53 final int code = Integer.parseInt(text);
54 if (oldCode != code) {
55 region = PhoneNumberUtilWrapper.getInstance(EnterPhoneNumberActivity.this).getRegionCodeForCountryCode(code);
56 }
57 if ("ZZ".equals(region)) {
58 binding.country.setText(TextUtils.isEmpty(text) ? R.string.choose_a_country : R.string.invalid_country_code);
59 } else {
60 binding.number.requestFocus();
61 binding.country.setText(PhoneNumberUtilWrapper.getCountryForCode(region));
62 }
63 } catch (NumberFormatException e) {
64 binding.country.setText(TextUtils.isEmpty(text) ? R.string.choose_a_country : R.string.invalid_country_code);
65 }
66 }
67 };
68
69 @Override
70 protected void refreshUiReal() {
71
72 }
73
74 @Override
75 void onBackendConnected() {
76 xmppConnectionService.getQuickConversationsService().addOnVerificationRequestedListener(this);
77 }
78
79 @Override
80 protected void onCreate(final Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
82
83 String region = savedInstanceState != null ? savedInstanceState.getString("region") : null;
84 boolean requestingVerification = savedInstanceState != null && savedInstanceState.getBoolean("requesting_verification", false);
85 if (region != null) {
86 this.region = region;
87 } else {
88 this.region = PhoneNumberUtilWrapper.getUserCountry(this);
89 }
90
91 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_enter_number);
92 this.binding.countryCode.setCompoundDrawables(new TextDrawable(this.binding.countryCode, "+"), null, null, null);
93 this.binding.country.setOnClickListener(this::onSelectCountryClick);
94 this.binding.next.setOnClickListener(this::onNextClick);
95 setSupportActionBar((Toolbar) this.binding.toolbar);
96 this.binding.countryCode.addTextChangedListener(this.countryCodeTextWatcher);
97 this.binding.countryCode.setText(String.valueOf(PhoneNumberUtilWrapper.getInstance(this).getCountryCodeForRegion(this.region)));
98 setRequestingVerificationState(requestingVerification);
99 }
100
101 @Override
102 public void onSaveInstanceState(Bundle savedInstanceState) {
103 if (this.region != null) {
104 savedInstanceState.putString("region", this.region);
105 }
106 savedInstanceState.putBoolean("requesting_verification", this.requestingVerification);
107 super.onSaveInstanceState(savedInstanceState);
108 }
109
110 @Override
111 public void onStop() {
112 if (xmppConnectionService != null) {
113 xmppConnectionService.getQuickConversationsService().removeOnVerificationRequestedListener(this);
114 }
115 super.onStop();
116 }
117
118 private void onNextClick(View v) {
119 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
120 try {
121 final Editable number = this.binding.number.getText();
122 final String input = number.toString();
123 final Phonenumber.PhoneNumber phoneNumber = PhoneNumberUtilWrapper.getInstance(this).parse(input, region);
124 this.binding.countryCode.setText(String.valueOf(phoneNumber.getCountryCode()));
125 number.clear();
126 number.append(String.valueOf(phoneNumber.getNationalNumber()));
127 final String formattedPhoneNumber = PhoneNumberUtilWrapper.getInstance(this).format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
128
129 if (PhoneNumberUtilWrapper.getInstance(this).isValidNumber(phoneNumber)) {
130 builder.setMessage(Html.fromHtml(getString(R.string.we_will_be_verifying, formattedPhoneNumber)));
131 builder.setNegativeButton(R.string.edit, null);
132 builder.setPositiveButton(R.string.ok, (dialog, which) -> onPhoneNumberEntered(phoneNumber));
133 } else {
134 builder.setMessage(getString(R.string.not_a_valid_phone_number, formattedPhoneNumber));
135 builder.setPositiveButton(R.string.ok, null);
136 }
137 Log.d(Config.LOGTAG, phoneNumber.toString());
138 } catch (NumberParseException e) {
139 builder.setMessage(R.string.please_enter_your_phone_number);
140 builder.setPositiveButton(R.string.ok, null);
141 }
142 builder.create().show();
143 }
144
145 private void onSelectCountryClick(View view) {
146 Intent intent = new Intent(this, ChooseCountryActivity.class);
147 startActivityForResult(intent, REQUEST_CHOOSE_COUNTRY);
148 }
149
150 private void onPhoneNumberEntered(Phonenumber.PhoneNumber phoneNumber) {
151 setRequestingVerificationState(true);
152 xmppConnectionService.getQuickConversationsService().requestVerification(phoneNumber);
153 }
154
155 private void setRequestingVerificationState(boolean requesting) {
156 this.requestingVerification = requesting;
157 this.binding.countryCode.setEnabled(!requesting);
158 this.binding.country.setEnabled(!requesting);
159 this.binding.number.setEnabled(!requesting);
160 this.binding.next.setEnabled(!requesting);
161 this.binding.next.setText(requesting ? R.string.requesting_sms : R.string.next);
162 }
163
164 @Override
165 public void onActivityResult(int requestCode, int resultCode, final Intent data) {
166 super.onActivityResult(requestCode, resultCode, data);
167 if (resultCode == RESULT_OK && requestCode == REQUEST_CHOOSE_COUNTRY) {
168 String region = data.getStringExtra("region");
169 if (region != null) {
170 this.region = region;
171 final int countryCode = PhoneNumberUtilWrapper.getInstance(this).getCountryCodeForRegion(region);
172 this.binding.countryCode.setText(String.valueOf(countryCode));
173 }
174 }
175 }
176
177 @Override
178 public void onVerificationRequestFailed(int code) {
179 runOnUiThread(()->{
180 setRequestingVerificationState(false);
181 });
182 }
183
184 @Override
185 public void onVerificationRequested() {
186 runOnUiThread(() -> {
187 startActivity(new Intent(this, VerifyActivity.class));
188 finish();
189 });
190 }
191
192 @Override
193 public void onVerificationRequestedRetryAt(long timestamp) {
194 runOnUiThread(() -> {
195 Intent intent = new Intent(this, VerifyActivity.class);
196 intent.putExtra(VerifyActivity.EXTRA_RETRY_SMS_AFTER, timestamp);
197 startActivity(intent);
198 finish();
199 });
200 }
201}