Example usage for android.telephony PhoneNumberUtils isWellFormedSmsAddress

List of usage examples for android.telephony PhoneNumberUtils isWellFormedSmsAddress

Introduction

In this page you can find the example usage for android.telephony PhoneNumberUtils isWellFormedSmsAddress.

Prototype

public static boolean isWellFormedSmsAddress(String address) 

Source Link

Document

Return true iff the network portion of address is, as far as we can tell on the device, suitable for use as an SMS destination address.

Usage

From source file:Main.java

public static boolean isValidSmsOrEmail(String number) {
    return PhoneNumberUtils.isWellFormedSmsAddress(number) || isValidEmail(number);
}

From source file:org.cryptocall.ui.WizardActivity.java

/**
 * Checks if text of given EditText contains a valid international telephone number. If not an
 * error is set and the EditText gets the focus.
 * // ww  w.  ja  v a2 s .c  om
 * @param context
 * @param editText
 * @return true if valid telephone number
 */
private static boolean isEditTextValidTelephoneNumber(Context context, EditText editText) {
    boolean output = true;

    if (!PhoneNumberUtils.isWellFormedSmsAddress(editText.getText().toString())) {
        editText.setError(context.getString(R.string.wizard_error_blank));
        editText.requestFocus();
        output = false;
    } else {
        if (!editText.getText().toString().startsWith("+")
                || !PhoneNumberUtils.isGlobalPhoneNumber(editText.getText().toString())) {
            editText.setError(context.getString(R.string.wizard_error_not_international));
            editText.requestFocus();
            output = false;
        } else {
            editText.setError(null);
        }
    }

    return output;
}

From source file:org.pixmob.droidlink.ui.EventDetailsFragment.java

private void onComposeSMS() {
    if (number != null) {
        if (PhoneNumberUtils.isWellFormedSmsAddress(number)) {
            final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + number));
            startActivity(i);/*from www  . j av  a  2 s  .  co m*/
        }
    }
}

From source file:org.thoughtcrime.securesms.contacts.ContactAccessor.java

/***
 * If the code below looks shitty to you, that's because it was taken
 * directly from the Android source, where shitty code is all you get.
 */// ww w .j  a v  a2  s .  co m

public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
    final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + "," + Phone.TYPE;

    final String[] PROJECTION_PHONE = { Phone._ID, // 0
            Phone.CONTACT_ID, // 1
            Phone.TYPE, // 2
            Phone.NUMBER, // 3
            Phone.LABEL, // 4
            Phone.DISPLAY_NAME, // 5
    };

    String phone = "";
    String cons = null;

    if (constraint != null) {
        cons = constraint.toString();

        if (RecipientsAdapter.usefulAsDigits(cons)) {
            phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
            if (phone.equals(cons) && !PhoneNumberUtils.isWellFormedSmsAddress(phone)) {
                phone = "";
            } else {
                phone = phone.trim();
            }
        }
    }
    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
    String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE,
            Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS);

    Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER);

    if (phone.length() > 0) {
        ArrayList result = new ArrayList();
        result.add(Integer.valueOf(-1)); // ID
        result.add(Long.valueOf(-1)); // CONTACT_ID
        result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
        result.add(phone); // NUMBER

        /*
        * The "\u00A0" keeps Phone.getDisplayLabel() from deciding
        * to display the default label ("Home") next to the transformation
        * of the letters into numbers.
        */
        result.add("\u00A0"); // LABEL
        result.add(cons); // NAME

        ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
        wrap.add(result);

        ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);

        return new MergeCursor(new Cursor[] { translated, phoneCursor });
    } else {
        return phoneCursor;
    }
}

From source file:com.securecomcode.text.contacts.ContactAccessor.java

/***
 * If the code below looks shitty to you, that's because it was taken
 * directly from the Android source, where shitty code is all you get.
 *//*  w ww  .ja va 2s.c  om*/

public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
    final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + ","
            + Contacts.Data.IS_SUPER_PRIMARY + " DESC," + Phone.TYPE;

    final String[] PROJECTION_PHONE = { Phone._ID, // 0
            Phone.CONTACT_ID, // 1
            Phone.TYPE, // 2
            Phone.NUMBER, // 3
            Phone.LABEL, // 4
            Phone.DISPLAY_NAME, // 5
    };

    String phone = "";
    String cons = null;

    if (constraint != null) {
        cons = constraint.toString();

        if (RecipientsAdapter.usefulAsDigits(cons)) {
            phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
            if (phone.equals(cons) && !PhoneNumberUtils.isWellFormedSmsAddress(phone)) {
                phone = "";
            } else {
                phone = phone.trim();
            }
        }
    }
    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
    String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE,
            Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS);

    Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER);

    if (phone.length() > 0) {
        ArrayList result = new ArrayList();
        result.add(Integer.valueOf(-1)); // ID
        result.add(Long.valueOf(-1)); // CONTACT_ID
        result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
        result.add(phone); // NUMBER

        /*
        * The "\u00A0" keeps Phone.getDisplayLabel() from deciding
        * to display the default label ("Home") next to the transformation
        * of the letters into numbers.
        */
        result.add("\u00A0"); // LABEL
        result.add(cons); // NAME

        ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
        wrap.add(result);

        ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);

        return new MergeCursor(new Cursor[] { translated, phoneCursor });
    } else {
        return phoneCursor;
    }
}

From source file:org.kontalk.ui.NumberValidation.java

private boolean checkInput(boolean importing) {
    String phoneStr;//from  w  w  w.j a v  a 2 s  . c om

    // check name first
    if (!importing) {
        mName = mNameText.getText().toString().trim();
        if (mName.length() == 0) {
            error(R.string.msg_no_name);
            return false;
        }
    }

    String phoneInput = mPhone.getText().toString();
    // if the user entered a phone number use it even when importing for backward compatibility
    if (!importing || !phoneInput.isEmpty()) {
        PhoneNumberUtil util = PhoneNumberUtil.getInstance();
        CountryCode cc = (CountryCode) mCountryCode.getSelectedItem();
        if (!BuildConfig.DEBUG) {
            PhoneNumber phone;
            try {
                phone = util.parse(phoneInput, cc.regionCode);
                // autoselect correct country if user entered country code too
                if (phone.hasCountryCode()) {
                    CountryCode ccLookup = new CountryCode();
                    ccLookup.regionCode = util.getRegionCodeForNumber(phone);
                    ccLookup.countryCode = phone.getCountryCode();
                    int position = ((CountryCodesAdapter) mCountryCode.getAdapter()).getPositionForId(ccLookup);
                    if (position >= 0) {
                        mCountryCode.setSelection(position);
                        cc = (CountryCode) mCountryCode.getItemAtPosition(position);
                    }
                }
                // handle special cases
                NumberValidator.handleSpecialCases(phone);
                if (!util.isValidNumberForRegion(phone, cc.regionCode)
                        && !NumberValidator.isSpecialNumber(phone))
                    throw new NumberParseException(ErrorType.INVALID_COUNTRY_CODE,
                            "invalid number for region " + cc.regionCode);
            } catch (NumberParseException e1) {
                error(R.string.msg_invalid_number);
                return false;
            }

            // check phone number format
            phoneStr = util.format(phone, PhoneNumberFormat.E164);
            if (!PhoneNumberUtils.isWellFormedSmsAddress(phoneStr)) {
                Log.i(TAG, "not a well formed SMS address");
            }
        } else {
            phoneStr = String.format(Locale.US, "+%d%s", cc.countryCode, mPhone.getText().toString());
        }

        // phone is null - invalid number
        if (phoneStr == null) {
            Toast.makeText(this, R.string.warn_invalid_number, Toast.LENGTH_SHORT).show();
            return false;
        }

        Log.v(TAG, "Using phone number to register: " + phoneStr);
        mPhoneNumber = phoneStr;
    } else {
        // we will use the data from the imported key
        mName = null;
        mPhoneNumber = null;
    }

    return true;
}

From source file:com.android.messaging.util.PhoneUtils.java

/**
 * Returns whether the given destination is valid for sending SMS/MMS message.
 */// www . j  a v  a 2s.  co m
public static boolean isValidSmsMmsDestination(final String destination) {
    return PhoneNumberUtils.isWellFormedSmsAddress(destination) || MmsSmsUtils.isEmailAddress(destination);
}

From source file:com.silentcircle.contacts.calllognew.CallLogAdapter.java

/***
 * Binds click handlers and intents to the voicemail, details and callback action buttons.
 *
 * @param views  The call log item views.
 *///from   w w w .  jav  a2  s. co m
private void bindActionButtons(CallLogListItemViews views) {
    boolean canPlaceCallToNumber = PhoneNumberUtilsWrapper.canPlaceCallsTo(views.number,
            views.numberPresentation);

    boolean canSendMessages = !TextUtils.isEmpty(views.assertedId) && Utilities.isUriNumber(views.assertedId);

    // Set return call intent, otherwise null.
    if (canPlaceCallToNumber) {
        // Sets the primary action to call the number.
        views.callBackButtonView.setTag(IntentProvider.getReturnCallIntentProvider(views.number));
        views.callBackButtonView.setVisibility(View.VISIBLE);
        views.callBackButtonView.setOnClickListener(mActionListener);
    } else {
        // Number is not callable, so hide button.
        views.callBackButtonView.setTag(null);
        views.callBackButtonView.setVisibility(View.GONE);
    }

    if (canSendMessages) {
        // Sets the primary action to call the number.
        views.writeBackButtonView.setTag(IntentProvider.getReturnMessagingIntentProvider(views.assertedId));
        views.writeBackButtonView.setVisibility(View.VISIBLE);
        views.writeBackButtonView.setOnClickListener(mActionListener);
    } else {
        // Number is not callable, so hide button.
        views.writeBackButtonView.setTag(null);
        views.writeBackButtonView.setVisibility(View.GONE);
    }
    // If one of the calls had video capabilities, show the video call button.
    //        if (CallUtil.isVideoEnabled(mContext) && canPlaceCallToNumber &&
    //                views.phoneCallDetailsViews.callTypeIcons.isVideoShown()) {
    //            views.videoCallButtonView.setTag(
    //                    IntentProvider.getReturnVideoCallIntentProvider(views.number));
    //            views.videoCallButtonView.setVisibility(View.VISIBLE);
    //            views.videoCallButtonView.setOnClickListener(mActionListener);
    //        } else {
    //            views.videoCallButtonView.setTag(null);
    //            views.videoCallButtonView.setVisibility(View.GONE);
    //        }

    // For voicemail calls, show the "VOICEMAIL" action button; hide otherwise.
    //        if (views.callType == Calls.VOICEMAIL_TYPE) {
    //            views.voicemailButtonView.setOnClickListener(mActionListener);
    //            views.voicemailButtonView.setTag(
    //                    IntentProvider.getPlayVoicemailIntentProvider(views.rowId, views.voicemailUri));
    //            views.voicemailButtonView.setVisibility(View.VISIBLE);
    //
    //            views.detailsButtonView.setVisibility(View.GONE);
    //        }
    //        else {
    //            views.voicemailButtonView.setTag(null);
    //            views.voicemailButtonView.setVisibility(View.GONE);

    views.detailsButtonView.setOnClickListener(mActionListener);
    views.detailsButtonView
            .setTag(IntentProvider.getCallDetailIntentProvider(views.rowId, views.callIds, null));

    // check to see whether invite button should be displayed
    // if sipaddress empty (not an SC user) and number valid then allow invite
    if (TextUtils.isEmpty(views.assertedId) && !TextUtils.isEmpty(views.number)
            && PhoneNumberUtils.isWellFormedSmsAddress(views.number)) {
        // Sets the primary action to call the number.
        views.inviteButtonView.setTag(IntentProvider.getInviteIntentProvider(views.number));
        views.inviteButtonView.setVisibility(View.VISIBLE);
        views.inviteButtonView.setOnClickListener(mActionListener);
    } else {
        // Number is not callable, so hide button.
        views.inviteButtonView.setTag(null);
        views.inviteButtonView.setVisibility(View.GONE);
    }

    mCallLogViewsHelper.setActionContentDescriptions(views);
}