Search in sources :

Example 1 with PhoneNumberFormattingTextWatcher

use of android.telephony.PhoneNumberFormattingTextWatcher in project actor-platform by actorapp.

the class SignPhoneFragment method initView.

private void initView(View v) {
    ((TextView) v.findViewById(R.id.phone_sign_hint)).setTextColor(ActorSDK.sharedActor().style.getTextSecondaryColor());
    countrySelectButton = (Button) v.findViewById(R.id.button_country_select);
    countrySelectButton.setTextColor(ActorSDK.sharedActor().style.getMainColor());
    onClick(countrySelectButton, new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            keyboardHelper.setImeVisibility(phoneNumberEditText, false);
            startActivityForResult(new Intent(getActivity(), PickCountryActivity.class), REQUEST_COUNTRY);
        }
    });
    countryCodeEditText = (EditText) v.findViewById(R.id.tv_country_code);
    countryCodeEditText.setTextColor(ActorSDK.sharedActor().style.getTextPrimaryColor());
    countryCodeEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            final Activity a = getActivity();
            if (a != null) {
                final String str = s.toString();
                if (str.length() == 4 && countryDb != null) {
                    if (countryDb.getCountryByPhoneCode(str) != null) {
                        focusPhone();
                    } else if (countryDb.getCountryByPhoneCode(str.substring(0, 3)) != null) {
                        countryCodeEditText.setText(str.substring(0, 3));
                        phoneNumberEditText.setText(str.substring(3, 4));
                        focusPhone();
                        return;
                    } else if (countryDb.getCountryByPhoneCode(str.substring(0, 2)) != null) {
                        countryCodeEditText.setText(str.substring(0, 2));
                        phoneNumberEditText.setText(str.substring(2, 4));
                        focusPhone();
                        return;
                    } else if (countryDb.getCountryByPhoneCode(str.substring(0, 1)) != null) {
                        countryCodeEditText.setText(str.substring(0, 1));
                        phoneNumberEditText.setText(str.substring(1, 4));
                        focusPhone();
                        return;
                    }
                }
                if (!ignoreNextCodeChange) {
                    if (TextUtils.isEmpty(s)) {
                        countrySelectButton.setText(R.string.auth_phone_country_title);
                    } else {
                        if (countryDb != null) {
                            final Country country = countryDb.getCountryByPhoneCode(s.toString());
                            if (country == null) {
                                countrySelectButton.setText(R.string.auth_phone_error_invalid_country);
                            } else {
                                setCountryName(country);
                            }
                        }
                    }
                } else {
                    ignoreNextCodeChange = false;
                }
            }
        }
    });
    phoneNumberEditText = (BackspaceKeyEditText) v.findViewById(R.id.tv_phone_number);
    phoneNumberEditText.setTextColor(ActorSDK.sharedActor().style.getTextPrimaryColor());
    phoneNumberEditText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
    phoneNumberEditText.setBackspaceListener(new BackspaceKeyEditText.BackspacePressListener() {

        @Override
        public boolean onBackspacePressed() {
            if (phoneNumberEditText.getText().length() == 0) {
                focusCode();
                return false;
            } else {
                return true;
            }
        }
    });
    phoneNumberEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
            if (id == EditorInfo.IME_ACTION_GO) {
                requestCode();
                return true;
            }
            return false;
        }
    });
    phoneNumberEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    countryCodeEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                focusPhone();
                return true;
            }
            return false;
        }
    });
    String savedCountryCode = messenger().getPreferences().getString("auth_county_code");
    countryCodeEditText.setText(savedCountryCode);
    String savedPhoneNumber = messenger().getPreferences().getString("auth_phone_number");
    phoneNumberEditText.setText(savedPhoneNumber);
    v.findViewById(R.id.button_why).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(getActivity()).setMessage(R.string.auth_phone_why_description).setPositiveButton(R.string.auth_phone_why_done, null).show().setCanceledOnTouchOutside(true);
        }
    });
    TextView switchToEmail = (TextView) v.findViewById(R.id.button_switch_to_email);
    switchToEmail.setTextColor(ActorSDK.sharedActor().style.getMainColor());
    onClick(switchToEmail, new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switchToEmail();
        }
    });
    if ((ActorSDK.sharedActor().getAuthType() & AuthActivity.AUTH_TYPE_EMAIL) == AuthActivity.AUTH_TYPE_EMAIL) {
        switchToEmail.setVisibility(View.VISIBLE);
    } else {
        switchToEmail.setVisibility(View.GONE);
    }
    Button singIn = (Button) v.findViewById(R.id.button_sign_in);
    singIn.setTextColor(ActorSDK.sharedActor().style.getTextSecondaryColor());
    onClick(singIn, new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startSignIn();
        }
    });
    onClick(v, R.id.button_continue, new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            requestCode();
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) Activity(android.app.Activity) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) KeyEvent(android.view.KeyEvent) Button(android.widget.Button) PhoneNumberFormattingTextWatcher(android.telephony.PhoneNumberFormattingTextWatcher) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) Country(im.actor.sdk.util.country.Country) TextView(android.widget.TextView) PhoneNumberFormattingTextWatcher(android.telephony.PhoneNumberFormattingTextWatcher)

Aggregations

Activity (android.app.Activity)1 AlertDialog (android.app.AlertDialog)1 Intent (android.content.Intent)1 PhoneNumberFormattingTextWatcher (android.telephony.PhoneNumberFormattingTextWatcher)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 KeyEvent (android.view.KeyEvent)1 View (android.view.View)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 Country (im.actor.sdk.util.country.Country)1