EnterPhoneNumberActivity.java

  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.ui.util.ApiErrorDialogHelper;
 24import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
 25import io.michaelrocks.libphonenumber.android.NumberParseException;
 26import io.michaelrocks.libphonenumber.android.PhoneNumberUtil;
 27import io.michaelrocks.libphonenumber.android.Phonenumber;
 28
 29public class EnterPhoneNumberActivity extends XmppActivity implements QuickConversationsService.OnVerificationRequested {
 30
 31    private static final int REQUEST_CHOOSE_COUNTRY = 0x1234;
 32
 33    private ActivityEnterNumberBinding binding;
 34
 35    private String region = null;
 36    private final TextWatcher countryCodeTextWatcher = new TextWatcher() {
 37        @Override
 38        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 39
 40        }
 41
 42        @Override
 43        public void onTextChanged(CharSequence s, int start, int before, int count) {
 44
 45        }
 46
 47        @Override
 48        public void afterTextChanged(Editable editable) {
 49            final String text = editable.toString();
 50            try {
 51                final int oldCode = region != null ? PhoneNumberUtilWrapper.getInstance(EnterPhoneNumberActivity.this).getCountryCodeForRegion(region) : 0;
 52                final int code = Integer.parseInt(text);
 53                if (oldCode != code) {
 54                    region = PhoneNumberUtilWrapper.getInstance(EnterPhoneNumberActivity.this).getRegionCodeForCountryCode(code);
 55                }
 56                if ("ZZ".equals(region)) {
 57                    binding.country.setText(TextUtils.isEmpty(text) ? R.string.choose_a_country : R.string.invalid_country_code);
 58                } else {
 59                    binding.number.requestFocus();
 60                    binding.country.setText(PhoneNumberUtilWrapper.getCountryForCode(region));
 61                }
 62            } catch (NumberFormatException e) {
 63                binding.country.setText(TextUtils.isEmpty(text) ? R.string.choose_a_country : R.string.invalid_country_code);
 64            }
 65        }
 66    };
 67    private boolean requestingVerification = false;
 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        this.binding.progressBar.setVisibility(requesting ? View.VISIBLE : View.GONE);
163        this.binding.progressBar.setIndeterminate(requesting);
164    }
165
166    @Override
167    public void onActivityResult(int requestCode, int resultCode, final Intent data) {
168        super.onActivityResult(requestCode, resultCode, data);
169        if (resultCode == RESULT_OK && requestCode == REQUEST_CHOOSE_COUNTRY) {
170            String region = data.getStringExtra("region");
171            if (region != null) {
172                this.region = region;
173                final int countryCode = PhoneNumberUtilWrapper.getInstance(this).getCountryCodeForRegion(region);
174                this.binding.countryCode.setText(String.valueOf(countryCode));
175            }
176        }
177    }
178
179    @Override
180    public void onVerificationRequestFailed(int code) {
181        runOnUiThread(() -> {
182            setRequestingVerificationState(false);
183            ApiErrorDialogHelper.create(this, code).show();
184        });
185    }
186
187    @Override
188    public void onVerificationRequested() {
189        runOnUiThread(() -> {
190            startActivity(new Intent(this, VerifyActivity.class));
191            finish();
192        });
193    }
194
195    @Override
196    public void onVerificationRequestedRetryAt(long timestamp) {
197        runOnUiThread(() -> {
198            Intent intent = new Intent(this, VerifyActivity.class);
199            intent.putExtra(VerifyActivity.EXTRA_RETRY_SMS_AFTER, timestamp);
200            startActivity(intent);
201            finish();
202        });
203    }
204}