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

The following examples show how to use android.telephony.PhoneNumberUtils#convertKeypadLettersToDigits() . 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: UnicodeDialerKeyListener.java    From FamilyChat with Apache License 2.0 6 votes vote down vote up
@Override
public CharSequence filter(CharSequence source, int start, int end,
                           Spanned dest, int dstart, int dend)
{
    final String converted = PhoneNumberUtils.convertKeypadLettersToDigits(replaceUnicodeDigits(source.toString()));
    // PhoneNumberUtils.replaceUnicodeDigits performs a character for character replacement,
    // so we can assume that start and end positions should remain unchanged.
    CharSequence result = super.filter(converted, start, end, dest, dstart, dend);
    if (result == null)
    {
        if (source.equals(converted))
        {
            // There was no conversion or filtering performed. Just return null according to
            // the behavior of DialerKeyListener.
            return null;
        } else
        {
            // filter returns null if the charsequence is to be returned unchanged/unfiltered.
            // But in this case we do want to return a modified character string (even if
            // none of the characters in the modified string are filtered). So if
            // result == null we return the unfiltered but converted numeric string instead.
            return converted.subSequence(start, end);
        }
    }
    return result;
}
 
Example 2
Source File: ContactAccessor.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
/***
 * 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.
 */

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;
  }
}
 
Example 3
Source File: OutgoingCall.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(Context aContext, Intent intent) {
    context = aContext;

       // No need to check permission here as we are always fired with correct permission
       
    String action = intent.getAction();
	String number = getResultData();
	//String full_number = intent.getStringExtra("android.phone.extra.ORIGINAL_URI");
	// Escape if no number
	if (number == null) {
		return;
	}

	
	// If emergency number transmit as if we were not there
	if(PhoneNumberUtils.isEmergencyNumber(number)) {
		ignoreNext = "";
		setResultData(number);
		return;
	}
	
	if(!Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) {
	    Log.e(THIS_FILE, "Not launching with correct action ! Do not process");
	    setResultData(number);
           return;
	}
	
	prefsWrapper = new PreferencesProviderWrapper(context);
	// If we already passed the outgoing call receiver or we are not integrated, do as if we were not there
	if ( ignoreNext.equalsIgnoreCase(number) || 
	        !prefsWrapper.getPreferenceBooleanValue(SipConfigManager.INTEGRATE_WITH_DIALER, true) || 
			action == null) {
		
		Log.d(THIS_FILE, "Our selector disabled, or Mobile chosen in our selector, send to tel");
		ignoreNext = "";
		setResultData(number);
		return;
	}
	
	
	// If this is an outgoing call with a valid number
	if (action.equals(Intent.ACTION_NEW_OUTGOING_CALL) ) {

        //Compute remote apps that could receive the outgoing call itnent through our api
        Map<String, String> potentialHandlers = CallHandlerPlugin.getAvailableCallHandlers(context);
        Log.d(THIS_FILE, "We have " + potentialHandlers.size() + " potential handlers");
        
	    
		// If sip is there or there is at least 2 call handlers (if only one we assume that's the embed gsm one !)
		if(prefsWrapper.isValidConnectionForOutgoing() || potentialHandlers.size() > 1) {
			// Just to be sure of what is incoming : sanitize phone number (in case of it was not properly done by dialer
			// Or by a third party app
			number = PhoneNumberUtils.convertKeypadLettersToDigits(number);
            number = PhoneNumberUtils.stripSeparators(number);
            
            
            // We can now check that the number that we want to call can be managed by something different than gsm plugin
            // Note that this is now possible because we cache filters.
            if(gsmCallHandlerId == null) {
                gsmCallHandlerId = CallHandlerPlugin.getAccountIdForCallHandler(aContext, (new ComponentName(aContext, com.csipsimple.plugins.telephony.CallHandler.class)).flattenToString());
            }
            if(gsmCallHandlerId != SipProfile.INVALID_ID) {
                if(Filter.isMustCallNumber(aContext, gsmCallHandlerId, number)) {
                    Log.d(THIS_FILE, "Filtering to force pass number along");
                    // Pass the call to pstn handle
                    setResultData(Filter.rewritePhoneNumber(aContext, gsmCallHandlerId, number));
                    return;
                }
            }
			
			// Launch activity to choose what to do with this call
			Intent outgoingCallChooserIntent = new Intent(Intent.ACTION_CALL);
			// Add csipsimple protocol :)
			outgoingCallChooserIntent.setData(SipUri.forgeSipUri(SipManager.PROTOCOL_SIP, number));
			outgoingCallChooserIntent.setClassName(context, OutgoingCallChooser.class.getName());
			outgoingCallChooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			Log.d(THIS_FILE, "Start outgoing call chooser for CSipSimple");
			context.startActivity(outgoingCallChooserIntent);
			// We will treat this by ourselves
			setResultData(null);
			return;
		}
	}
	
	
	
	Log.d(THIS_FILE, "Can't use SIP, pass number along");
	// Pass the call to pstn handle
	setResultData(number);
	return;
}
 
Example 4
Source File: ContactAccessor.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
/***
 * 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.
 */

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(-1);                    // ID
    result.add((long) -1);             // CONTACT_ID
    result.add(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;
  }
}