Java Code Examples for android.telephony.PhoneNumberUtils#formatNumber()

The following examples show how to use android.telephony.PhoneNumberUtils#formatNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VCardTelDisplayFormatter.java    From ZXing-Orient with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence format(CharSequence value, int index) {
  value = PhoneNumberUtils.formatNumber(value.toString());
  Map<String,Set<String>> metadata =
      metadataForIndex == null || metadataForIndex.size() <= index ? null : metadataForIndex.get(index);
  value = formatMetadata(value, metadata);
  return value;
}
 
Example 2
Source File: VCardTelDisplayFormatter.java    From zxingfragmentlib with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence format(CharSequence value, int index) {
  value = PhoneNumberUtils.formatNumber(value.toString());
  Map<String,Set<String>> metadata =
      metadataForIndex == null || metadataForIndex.size() <= index ? null : metadataForIndex.get(index);
  value = formatMetadata(value, metadata);
  return value;
}
 
Example 3
Source File: CountryCodePicker.java    From CountryCodePickerProject with Apache License 2.0 5 votes vote down vote up
/**
     * updates hint
     */
    private void updateHint() {
        if (editText_registeredCarrierNumber != null && hintExampleNumberEnabled) {
            String formattedNumber = "";
            Phonenumber.PhoneNumber exampleNumber = getPhoneUtil().getExampleNumberForType(getSelectedCountryNameCode(), getSelectedHintNumberType());
            if (exampleNumber != null) {
                formattedNumber = exampleNumber.getNationalNumber() + "";
//                Log.d(TAG, "updateHint: " + formattedNumber);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    formattedNumber = PhoneNumberUtils.formatNumber(getSelectedCountryCodeWithPlus() + formattedNumber, getSelectedCountryNameCode());
                } else {
                    formattedNumber = PhoneNumberUtils.formatNumber(getSelectedCountryCodeWithPlus() + formattedNumber);
                }
                if (formattedNumber != null) {
                    formattedNumber = formattedNumber.substring(getSelectedCountryCodeWithPlus().length()).trim();
                }
//                Log.d(TAG, "updateHint: after format " + formattedNumber + " " + selectionMemoryTag);
            } else {
//                Log.w(TAG, "updateHint: No example number found for this country (" + getSelectedCountryNameCode() + ") or this type (" + hintExampleNumberType.name() + ").");
            }

            //fallback to original hint
            if (formattedNumber == null) {
                formattedNumber = originalHint;
            }

            editText_registeredCarrierNumber.setHint(formattedNumber);
        }
    }
 
Example 4
Source File: SMSResultHandler.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
  SMSParsedResult smsResult = (SMSParsedResult) getResult();
  String[] rawNumbers = smsResult.getNumbers();
  String[] formattedNumbers = new String[rawNumbers.length];
  for (int i = 0; i < rawNumbers.length; i++) {
    formattedNumbers[i] = PhoneNumberUtils.formatNumber(rawNumbers[i]);
  }
  StringBuilder contents = new StringBuilder(50);
  ParsedResult.maybeAppend(formattedNumbers, contents);
  ParsedResult.maybeAppend(smsResult.getSubject(), contents);
  ParsedResult.maybeAppend(smsResult.getBody(), contents);
  return contents.toString();
}
 
Example 5
Source File: StoreActivity.java    From android-silent-ping-sms with GNU General Public License v3.0 5 votes vote down vote up
public static String formatNumber(String number) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return PhoneNumberUtils.formatNumber(number, Locale.getDefault().getCountry());
    } else {
        //Deprecated method
        //noinspection deprecation
        return PhoneNumberUtils.formatNumber(number);
    }
}
 
Example 6
Source File: SMSResultHandler.java    From reacteu-app with MIT License 5 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
  SMSParsedResult smsResult = (SMSParsedResult) getResult();
  StringBuilder contents = new StringBuilder(50);
  String[] rawNumbers = smsResult.getNumbers();
  String[] formattedNumbers = new String[rawNumbers.length];
  for (int i = 0; i < rawNumbers.length; i++) {
    formattedNumbers[i] = PhoneNumberUtils.formatNumber(rawNumbers[i]);
  }
  ParsedResult.maybeAppend(formattedNumbers, contents);
  ParsedResult.maybeAppend(smsResult.getSubject(), contents);
  ParsedResult.maybeAppend(smsResult.getBody(), contents);
  return contents.toString();
}
 
Example 7
Source File: SMSResultHandler.java    From android-apps with MIT License 5 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
  SMSParsedResult smsResult = (SMSParsedResult) getResult();
  StringBuilder contents = new StringBuilder(50);
  String[] rawNumbers = smsResult.getNumbers();
  String[] formattedNumbers = new String[rawNumbers.length];
  for (int i = 0; i < rawNumbers.length; i++) {
    formattedNumbers[i] = PhoneNumberUtils.formatNumber(rawNumbers[i]);
  }
  ParsedResult.maybeAppend(formattedNumbers, contents);
  ParsedResult.maybeAppend(smsResult.getSubject(), contents);
  ParsedResult.maybeAppend(smsResult.getBody(), contents);
  return contents.toString();
}
 
Example 8
Source File: SMSResultHandler.java    From android-quick-response-code with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
    SMSParsedResult smsResult = (SMSParsedResult) getResult();
    StringBuilder contents = new StringBuilder(50);
    String[] rawNumbers = smsResult.getNumbers();
    String[] formattedNumbers = new String[rawNumbers.length];
    for (int i = 0; i < rawNumbers.length; i++) {
        formattedNumbers[i] = PhoneNumberUtils.formatNumber(rawNumbers[i]);
    }
    ParsedResult.maybeAppend(formattedNumbers, contents);
    ParsedResult.maybeAppend(smsResult.getSubject(), contents);
    ParsedResult.maybeAppend(smsResult.getBody(), contents);
    return contents.toString();
}
 
Example 9
Source File: SMSResultHandler.java    From ZXing-Standalone-library with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
  SMSParsedResult smsResult = (SMSParsedResult) getResult();
  String[] rawNumbers = smsResult.getNumbers();
  String[] formattedNumbers = new String[rawNumbers.length];
  for (int i = 0; i < rawNumbers.length; i++) {
    formattedNumbers[i] = PhoneNumberUtils.formatNumber(rawNumbers[i]);
  }
  StringBuilder contents = new StringBuilder(50);
  ParsedResult.maybeAppend(formattedNumbers, contents);
  ParsedResult.maybeAppend(smsResult.getSubject(), contents);
  ParsedResult.maybeAppend(smsResult.getBody(), contents);
  return contents.toString();
}
 
Example 10
Source File: VCardTelDisplayFormatter.java    From barcodescanner-lib-aar with MIT License 5 votes vote down vote up
@Override
public CharSequence format(CharSequence value, int index) {
  value = PhoneNumberUtils.formatNumber(value.toString());
  Map<String,Set<String>> metadata =
      metadataForIndex == null || metadataForIndex.size() <= index ? null : metadataForIndex.get(index);
  value = formatMetadata(value, metadata);
  return value;
}
 
Example 11
Source File: QRCodeEncoder.java    From Study_Android_Demo with Apache License 2.0 4 votes vote down vote up
private void encodeQRCodeContents(Intent intent, String type) {
  switch (type) {
    case Contents.Type.TEXT:
      String textData = intent.getStringExtra(Intents.Encode.DATA);
      if (textData != null && !textData.isEmpty()) {
        contents = textData;
        displayContents = textData;
        title = activity.getString(R.string.contents_text);
      }
      break;

    case Contents.Type.EMAIL:
      String emailData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (emailData != null) {
        contents = "mailto:" + emailData;
        displayContents = emailData;
        title = activity.getString(R.string.contents_email);
      }
      break;

    case Contents.Type.PHONE:
      String phoneData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (phoneData != null) {
        contents = "tel:" + phoneData;
        displayContents = PhoneNumberUtils.formatNumber(phoneData);
        title = activity.getString(R.string.contents_phone);
      }
      break;

    case Contents.Type.SMS:
      String smsData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (smsData != null) {
        contents = "sms:" + smsData;
        displayContents = PhoneNumberUtils.formatNumber(smsData);
        title = activity.getString(R.string.contents_sms);
      }
      break;

    case Contents.Type.CONTACT:
      Bundle contactBundle = intent.getBundleExtra(Intents.Encode.DATA);
      if (contactBundle != null) {

        String name = contactBundle.getString(ContactsContract.Intents.Insert.NAME);
        String organization = contactBundle.getString(ContactsContract.Intents.Insert.COMPANY);
        String address = contactBundle.getString(ContactsContract.Intents.Insert.POSTAL);
        List<String> phones = getAllBundleValues(contactBundle, Contents.PHONE_KEYS);
        List<String> phoneTypes = getAllBundleValues(contactBundle, Contents.PHONE_TYPE_KEYS);
        List<String> emails = getAllBundleValues(contactBundle, Contents.EMAIL_KEYS);
        String url = contactBundle.getString(Contents.URL_KEY);
        List<String> urls = url == null ? null : Collections.singletonList(url);
        String note = contactBundle.getString(Contents.NOTE_KEY);

        ContactEncoder encoder = useVCard ? new VCardContactEncoder() : new MECARDContactEncoder();
        String[] encoded = encoder.encode(Collections.singletonList(name),
                                          organization,
                                          Collections.singletonList(address),
                                          phones,
                                          phoneTypes,
                                          emails,
                                          urls,
                                          note);
        // Make sure we've encoded at least one field.
        if (!encoded[1].isEmpty()) {
          contents = encoded[0];
          displayContents = encoded[1];
          title = activity.getString(R.string.contents_contact);
        }

      }
      break;

    case Contents.Type.LOCATION:
      Bundle locationBundle = intent.getBundleExtra(Intents.Encode.DATA);
      if (locationBundle != null) {
        // These must use Bundle.getFloat(), not getDouble(), it's part of the API.
        float latitude = locationBundle.getFloat("LAT", Float.MAX_VALUE);
        float longitude = locationBundle.getFloat("LONG", Float.MAX_VALUE);
        if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
          contents = "geo:" + latitude + ',' + longitude;
          displayContents = latitude + "," + longitude;
          title = activity.getString(R.string.contents_location);
        }
      }
      break;
  }
}
 
Example 12
Source File: TelResultHandler.java    From Study_Android_Demo with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
  String contents = getResult().getDisplayResult();
  contents = contents.replace("\r", "");
  return PhoneNumberUtils.formatNumber(contents);
}
 
Example 13
Source File: QRCodeEncoder.java    From ZXing-Standalone-library with Apache License 2.0 4 votes vote down vote up
private void encodeQRCodeContents(Intent intent, String type) {
  switch (type) {
    case Contents.Type.TEXT:
      String textData = intent.getStringExtra(Intents.Encode.DATA);
      if (textData != null && !textData.isEmpty()) {
        contents = textData;
        displayContents = textData;
        title = activity.getString(R.string.contents_text);
      }
      break;

    case Contents.Type.EMAIL:
      String emailData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (emailData != null) {
        contents = "mailto:" + emailData;
        displayContents = emailData;
        title = activity.getString(R.string.contents_email);
      }
      break;

    case Contents.Type.PHONE:
      String phoneData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (phoneData != null) {
        contents = "tel:" + phoneData;
        displayContents = PhoneNumberUtils.formatNumber(phoneData);
        title = activity.getString(R.string.contents_phone);
      }
      break;

    case Contents.Type.SMS:
      String smsData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (smsData != null) {
        contents = "sms:" + smsData;
        displayContents = PhoneNumberUtils.formatNumber(smsData);
        title = activity.getString(R.string.contents_sms);
      }
      break;

    case Contents.Type.CONTACT:
      Bundle contactBundle = intent.getBundleExtra(Intents.Encode.DATA);
      if (contactBundle != null) {

        String name = contactBundle.getString(ContactsContract.Intents.Insert.NAME);
        String organization = contactBundle.getString(ContactsContract.Intents.Insert.COMPANY);
        String address = contactBundle.getString(ContactsContract.Intents.Insert.POSTAL);
        List<String> phones = getAllBundleValues(contactBundle, Contents.PHONE_KEYS);
        List<String> phoneTypes = getAllBundleValues(contactBundle, Contents.PHONE_TYPE_KEYS);
        List<String> emails = getAllBundleValues(contactBundle, Contents.EMAIL_KEYS);
        String url = contactBundle.getString(Contents.URL_KEY);
        List<String> urls = url == null ? null : Collections.singletonList(url);
        String note = contactBundle.getString(Contents.NOTE_KEY);

        ContactEncoder encoder = useVCard ? new VCardContactEncoder() : new MECARDContactEncoder();
        String[] encoded = encoder.encode(Collections.singletonList(name),
                                          organization,
                                          Collections.singletonList(address),
                                          phones,
                                          phoneTypes,
                                          emails,
                                          urls,
                                          note);
        // Make sure we've encoded at least one field.
        if (!encoded[1].isEmpty()) {
          contents = encoded[0];
          displayContents = encoded[1];
          title = activity.getString(R.string.contents_contact);
        }

      }
      break;

    case Contents.Type.LOCATION:
      Bundle locationBundle = intent.getBundleExtra(Intents.Encode.DATA);
      if (locationBundle != null) {
        // These must use Bundle.getFloat(), not getDouble(), it's part of the API.
        float latitude = locationBundle.getFloat("LAT", Float.MAX_VALUE);
        float longitude = locationBundle.getFloat("LONG", Float.MAX_VALUE);
        if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
          contents = "geo:" + latitude + ',' + longitude;
          displayContents = latitude + "," + longitude;
          title = activity.getString(R.string.contents_location);
        }
      }
      break;
  }
}
 
Example 14
Source File: TelResultHandler.java    From android-quick-response-code with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
    String contents = getResult().getDisplayResult();
    contents = contents.replace("\r", "");
    return PhoneNumberUtils.formatNumber(contents);
}
 
Example 15
Source File: QRCodeEncoder.java    From weex with Apache License 2.0 4 votes vote down vote up
private void encodeQRCodeContents(Intent intent, String type) {
  switch (type) {
    case Contents.Type.TEXT:
      String textData = intent.getStringExtra(Intents.Encode.DATA);
      if (textData != null && !textData.isEmpty()) {
        contents = textData;
        displayContents = textData;
        title = activity.getString(R.string.contents_text);
      }
      break;

    case Contents.Type.EMAIL:
      String emailData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (emailData != null) {
        contents = "mailto:" + emailData;
        displayContents = emailData;
        title = activity.getString(R.string.contents_email);
      }
      break;

    case Contents.Type.PHONE:
      String phoneData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (phoneData != null) {
        contents = "tel:" + phoneData;
        displayContents = PhoneNumberUtils.formatNumber(phoneData);
        title = activity.getString(R.string.contents_phone);
      }
      break;

    case Contents.Type.SMS:
      String smsData = ContactEncoder.trim(intent.getStringExtra(Intents.Encode.DATA));
      if (smsData != null) {
        contents = "sms:" + smsData;
        displayContents = PhoneNumberUtils.formatNumber(smsData);
        title = activity.getString(R.string.contents_sms);
      }
      break;

    case Contents.Type.CONTACT:
      Bundle contactBundle = intent.getBundleExtra(Intents.Encode.DATA);
      if (contactBundle != null) {

        String name = contactBundle.getString(ContactsContract.Intents.Insert.NAME);
        String organization = contactBundle.getString(ContactsContract.Intents.Insert.COMPANY);
        String address = contactBundle.getString(ContactsContract.Intents.Insert.POSTAL);
        List<String> phones = getAllBundleValues(contactBundle, Contents.PHONE_KEYS);
        List<String> phoneTypes = getAllBundleValues(contactBundle, Contents.PHONE_TYPE_KEYS);
        List<String> emails = getAllBundleValues(contactBundle, Contents.EMAIL_KEYS);
        String url = contactBundle.getString(Contents.URL_KEY);
        List<String> urls = url == null ? null : Collections.singletonList(url);
        String note = contactBundle.getString(Contents.NOTE_KEY);

        ContactEncoder encoder = useVCard ? new VCardContactEncoder() : new MECARDContactEncoder();
        String[] encoded = encoder.encode(Collections.singletonList(name),
                                          organization,
                                          Collections.singletonList(address),
                                          phones,
                                          phoneTypes,
                                          emails,
                                          urls,
                                          note);
        // Make sure we've encoded at least one field.
        if (!encoded[1].isEmpty()) {
          contents = encoded[0];
          displayContents = encoded[1];
          title = activity.getString(R.string.contents_contact);
        }

      }
      break;

    case Contents.Type.LOCATION:
      Bundle locationBundle = intent.getBundleExtra(Intents.Encode.DATA);
      if (locationBundle != null) {
        // These must use Bundle.getFloat(), not getDouble(), it's part of the API.
        float latitude = locationBundle.getFloat("LAT", Float.MAX_VALUE);
        float longitude = locationBundle.getFloat("LONG", Float.MAX_VALUE);
        if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
          contents = "geo:" + latitude + ',' + longitude;
          displayContents = latitude + "," + longitude;
          title = activity.getString(R.string.contents_location);
        }
      }
      break;
  }
}
 
Example 16
Source File: TelResultHandler.java    From android-apps with MIT License 4 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
  String contents = getResult().getDisplayResult();
  contents = contents.replace("\r", "");
  return PhoneNumberUtils.formatNumber(contents);
}
 
Example 17
Source File: LogEntryAdapter.java    From emerald-dialer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void bindView(View view, Context context, Cursor cursor) {
	final LogEntryCache viewCache = (LogEntryCache) view.getTag();
	if (viewCache == null) {
		return;
	}
	String name = cursor.getString(COLUMN_NAME);
	String phoneNumber = cursor.getString(COLUMN_NUMBER);
	if (phoneNumber == null) {
		phoneNumber = "";
	}
	String formattedNumber = PhoneNumberUtils.formatNumber(phoneNumber, Locale.getDefault().getCountry());
	if (!TextUtils.isEmpty(name)) {
		viewCache.contactName.setText(name);
		viewCache.phoneNumber.setText(formattedNumber);
	} else if (!TextUtils.isEmpty(formattedNumber)) {
		viewCache.contactName.setText(formattedNumber);
		viewCache.phoneNumber.setText("");
	} else {
		viewCache.contactName.setText("no number");
		viewCache.phoneNumber.setText("");
	}
	long date = cursor.getLong(COLUMN_DATE);
	viewCache.callDate.setText(DateUtils.formatSameDayTime(date, System.currentTimeMillis(), DateFormat.MEDIUM, DateFormat.SHORT));

	int id = cursor.getInt(COLUMN_TYPE);
	int callTypeDrawableId = 0;
	switch (id) {
		case Calls.INCOMING_TYPE:
			callTypeDrawableId = callReceivedDrawableId;
			break;
		case Calls.OUTGOING_TYPE:
			callTypeDrawableId = callMadeDrawableId;
			break;
		case Calls.MISSED_TYPE:
			callTypeDrawableId = R.drawable.ic_call_missed;
			break;
	}
	if (callTypeDrawableId != 0) {
		viewCache.callTypeImage.setImageDrawable(context.getResources().getDrawable(callTypeDrawableId, context.getTheme()));
	}
	viewCache.contactImage.setTag(phoneNumber); // set a tag for the callback to be able to check, so we don't set the contact image of a reused view
	Drawable d = mAsyncContactImageLoader.loadDrawableForNumber(phoneNumber, new ImageCallback() {
		
		@Override
		public void imageLoaded(Drawable imageDrawable, String number) {
			if (TextUtils.equals(number, (String)viewCache.contactImage.getTag())) {
				viewCache.contactImage.setImageDrawable(imageDrawable);
			}
		}
	});
	viewCache.contactImage.setImageDrawable(d);
	if (phoneNumber.length() == 0) {
		return;
	}
	viewCache.contactImage.setOnClickListener(this);
	
}
 
Example 18
Source File: TelResultHandler.java    From barterli_android with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getDisplayContents() {
    String contents = getResult().getDisplayResult();
    contents = contents.replace("\r", "");
    return PhoneNumberUtils.formatNumber(contents);
}
 
Example 19
Source File: RecipientPreferenceActivity.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private @NonNull String formatRecipient(@NonNull Recipient recipient) {
  if      (recipient.getE164().isPresent())  return PhoneNumberUtils.formatNumber(recipient.requireE164());
  else if (recipient.getEmail().isPresent()) return recipient.requireEmail();
  else                                       return "";
}
 
Example 20
Source File: LollipopUtil.java    From appinventor-extensions with Apache License 2.0 2 votes vote down vote up
/**
 * Format a phone number based on the number's country code, falling
 * back to the format defined by the user's current locale. This is
 * to replace calling {@link PhoneNumberUtils#formatNumber(String)},
 * which was deprecated in the LOLLIPOP release.
 *
 * @see PhoneNumberUtils#formatNumber(String, String)
 * @param number The phone number to be formatted
 * @return The phone number, formatted based on the country code or
 * user's locale.
 */
public static String formatNumber(String number) {
  return PhoneNumberUtils.formatNumber(number, Locale.getDefault().getCountry());
}