/** * Send generic SMS * * @param phoneNumber * @param message */ public void sendSms(Context context, String phoneNumber, String message) { if (PhoneNumberUtils.isWellFormedSmsAddress(phoneNumber)) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; PendingIntent sentPI = PendingIntent.getBroadcast(context.getApplicationContext(), 0, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(context.getApplicationContext(), 0, new Intent(DELIVERED), 0); /* receive when the SMS has been sent */ context.getApplicationContext().registerReceiver(SmsSendReceiver, new IntentFilter(SENT)); /* receive when the SMS has been delivered */ context .getApplicationContext() .registerReceiver(SmsDeliveredReceiver, new IntentFilter(DELIVERED)); Log.d(Constants.TAG, "Sending SMS to " + phoneNumber + " with message: " + message); // TODO: make it a preference to switch between data sms and normal sms SmsManager smsManager = SmsManager.getDefault(); smsManager.sendDataMessage( phoneNumber, null, Constants.DATA_SMS_PORT, message.getBytes(), sentPI, deliveredPI); // smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } else { mCryptoCallService.sendUpdateUiToHandler(R.string.status_destination_invalid); } }
@VisibleForTesting static boolean isNumberAddress(String number) { if (number.contains("@")) return false; if (GroupUtil.isEncodedGroup(number)) return false; final String networkNumber = PhoneNumberUtils.extractNetworkPortion(number); if (TextUtils.isEmpty(networkNumber)) return false; if (networkNumber.length() < 3) return false; return PhoneNumberUtils.isWellFormedSmsAddress(number); }
private boolean checkInput() { mPhoneNumber = null; String phoneStr = null; // check name first mName = mNameText.getText().toString().trim(); if (mName.length() == 0) { error(R.string.title_no_name, R.string.msg_no_name); return false; } PhoneNumberUtil util = PhoneNumberUtil.getInstance(); CountryCode cc = (CountryCode) mCountryCode.getSelectedItem(); if (!BuildConfig.DEBUG) { PhoneNumber phone; try { phone = util.parse(mPhone.getText().toString(), cc.regionCode); if (!util.isValidNumberForRegion(phone, cc.regionCode)) { throw new NumberParseException( ErrorType.INVALID_COUNTRY_CODE, "invalid number for region " + cc.regionCode); } } catch (NumberParseException e1) { error(R.string.title_invalid_number, R.string.msg_invalid_number); return false; } // check phone number format if (phone != null) { 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; return true; }
// Some received sms's have addresses such as "OakfieldCPS" or "T-Mobile". This // function will attempt to identify these and return true. If the number contains // 3 or more digits, such as "jello123", this function will return false. // Some countries have 3 digits shortcodes and we have to identify them as numbers. // http://en.wikipedia.org/wiki/Short_code // Examples of input/output for this function: // "Jello123" -> false [3 digits, it is considered to be the phone number "123"] // "T-Mobile" -> true [it is considered to be the address "T-Mobile"] // "Mobile1" -> true [1 digit, it is considered to be the address "Mobile1"] // "Dogs77" -> true [2 digits, it is considered to be the address "Dogs77"] // "****1" -> true [1 digits, it is considered to be the address "****1"] // "#4#5#6#" -> true [it is considered to be the address "#4#5#6#"] // "AB12" -> true [2 digits, it is considered to be the address "AB12"] // "12" -> true [2 digits, it is considered to be the address "12"] private boolean isAlphaNumber(String number) { // TODO: PhoneNumberUtils.isWellFormedSmsAddress() only check if the number is a valid // GSM SMS address. If the address contains a dialable char, it considers it a well // formed SMS addr. CDMA doesn't work that way and has a different parser for SMS // address (see CdmaSmsAddress.parse(String address)). We should definitely fix this!!! if (!PhoneNumberUtils.isWellFormedSmsAddress(number)) { // The example "T-Mobile" will exit here because there are no numbers. return true; // we're not an sms address, consider it an alpha number } if (MessageUtils.isAlias(number)) { return true; } number = PhoneNumberUtils.extractNetworkPortion(number); if (TextUtils.isEmpty(number)) { return true; // there are no digits whatsoever in the number } // At this point, anything like "Mobile1" or "Dogs77" will be stripped down to // "1" and "77". "#4#5#6#" remains as "#4#5#6#" at this point. return number.length() < 3; }