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