android.provider.ContactsContract.PhoneLookup Java Examples

The following examples show how to use android.provider.ContactsContract.PhoneLookup. 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: NameLoader.java    From callmeter with GNU General Public License v3.0 7 votes vote down vote up
@Override
protected String doInBackground(final Void... params) {
    String ret = null;
    if (CallMeter.hasPermission(ctx, Manifest.permission.READ_CONTACTS)) {
        // resolve names only when permission is granted
        try {
            //noinspection ConstantConditions
            Cursor c = ctx.getContentResolver().query(
                    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, num),
                    new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
            if (c != null) {
                if (c.moveToFirst()) {
                    ret = c.getString(0);
                }
                c.close();
            }
        } catch (Exception e) {
            Log.e(TAG, "error loading name", e);
        }
    }
    return ret;
}
 
Example #2
Source File: ContactAccessor.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public boolean isSystemContact(Context context, String number) {
  Uri      uri        = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
  String[] projection = new String[]{PhoneLookup.DISPLAY_NAME, PhoneLookup.LOOKUP_KEY,
                                     PhoneLookup._ID, PhoneLookup.NUMBER};
  Cursor   cursor     = context.getContentResolver().query(uri, projection, null, null, null);

  try {
    if (cursor != null && cursor.moveToFirst()) {
      return true;
    }
  } finally {
    if (cursor != null) cursor.close();
  }

  return false;
}
 
Example #3
Source File: ContactIdentityManagerICS.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Uri getSelfIdentityUri() {
  String[] PROJECTION = new String[] {
      PhoneLookup.DISPLAY_NAME,
      PhoneLookup.LOOKUP_KEY,
      PhoneLookup._ID,
  };

  Cursor cursor = null;

  try {
    cursor = context.getContentResolver().query(ContactsContract.Profile.CONTENT_URI,
                                                  PROJECTION, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
      return Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
    }
  } finally {
    if (cursor != null)
      cursor.close();
  }

  return null;
}
 
Example #4
Source File: QuickContactBadge.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    // If contact has been assigned, mExtras should no longer be null, but do a null check
    // anyway just in case assignContactFromPhone or Email was called with a null bundle or
    // wasn't assigned previously.
    final Bundle extras = (mExtras == null) ? new Bundle() : mExtras;
    if (mContactUri != null) {
        QuickContact.showQuickContact(getContext(), QuickContactBadge.this, mContactUri,
                mExcludeMimes, mPrioritizedMimeType);
    } else if (mContactEmail != null && mQueryHandler != null) {
        extras.putString(EXTRA_URI_CONTENT, mContactEmail);
        mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP_AND_TRIGGER, extras,
                Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)),
                EMAIL_LOOKUP_PROJECTION, null, null, null);
    } else if (mContactPhone != null && mQueryHandler != null) {
        extras.putString(EXTRA_URI_CONTENT, mContactPhone);
        mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP_AND_TRIGGER, extras,
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
                PHONE_LOOKUP_PROJECTION, null, null, null);
    } else {
        // If a contact hasn't been assigned, don't react to click.
        return;
    }
}
 
Example #5
Source File: EventBroadcastReceiver.java    From deskcon-android with GNU General Public License v3.0 6 votes vote down vote up
public static String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactName;
}
 
Example #6
Source File: LogEntryAdapter.java    From emerald-dialer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onClick(View view) {
	if (view.getId() == R.id.contact_image) {
		String number = (String)view.getTag();
		if (null == number) {
			return;
		}
		Uri contactIdUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
		Cursor cursor = activityRef.get().getContentResolver().query(contactIdUri, new String[] {PhoneLookup.CONTACT_ID}, null, null, null);
		if (cursor == null || !cursor.moveToFirst()) {
			unknownNumberDialog(number);
			return;
		}
		String contactId = cursor.getString(0);
		cursor.close();
		Intent intent = new Intent(Intent.ACTION_VIEW);
		Uri uri = Uri.withAppendedPath(Contacts.CONTENT_URI, contactId);
		intent.setDataAndType(uri, "vnd.android.cursor.dir/contact");
		try {
			activityRef.get().startActivity(intent);
		} catch (ActivityNotFoundException e) {
			activityRef.get().showMissingContactsAppDialog();
		}
	}
}
 
Example #7
Source File: OpenFitService.java    From OpenFit with MIT License 6 votes vote down vote up
public String getContactName(String phoneNumber) {
    if(phoneNumber == null){
        return null;
    }

    ContentResolver cr = this.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[] {PhoneLookup.DISPLAY_NAME}, null, null, null);
    if(cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }
    if(!cursor.isClosed()) {
        cursor.close();
    }
    return contactName;
}
 
Example #8
Source File: ContactBadge.java    From Android-ContactPicker with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    // If contact has been assigned, mExtras should no longer be null, but do a null check
    // anyway just in case assignContactFromPhone or Email was called with a null bundle or
    // wasn't assigned previously.
    final Bundle extras = (mExtras == null) ? new Bundle() : mExtras;
    if (mContactUri != null) {
        QuickContact.showQuickContact(getContext(), ContactBadge.this, mContactUri, QuickContact.MODE_LARGE, mExcludeMimes);
    } else if (mContactEmail != null && mQueryHandler != null) {
        extras.putString(Constants.EXTRA_URI_CONTENT, mContactEmail);
        mQueryHandler.startQuery(Constants.TOKEN_EMAIL_LOOKUP_AND_TRIGGER, extras,
                Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)),
                EMAIL_LOOKUP_PROJECTION, null, null, null, mContactQueryHandlerCallback);
    } else if (mContactPhone != null && mQueryHandler != null) {
        extras.putString(Constants.EXTRA_URI_CONTENT, mContactPhone);
        mQueryHandler.startQuery(Constants.TOKEN_PHONE_LOOKUP_AND_TRIGGER, extras,
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
                PHONE_LOOKUP_PROJECTION, null, null, null, mContactQueryHandlerCallback);
    } else {
        // If a contact hasn't been assigned, don't react to click.
        return;
    }
}
 
Example #9
Source File: ContactAccessor.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public Collection<ContactData> getContactsWithPush(Context context) {
  final ContentResolver resolver = context.getContentResolver();
  final String[] inProjection    = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME};

  final List<String>           registeredAddresses = Stream.of(DatabaseFactory.getRecipientDatabase(context).getRegistered())
                                                            .map(Recipient::resolved)
                                                            .filter(r -> r.getE164().isPresent())
                                                            .map(Recipient::requireE164)
                                                            .toList();
  final Collection<ContactData> lookupData          = new ArrayList<>(registeredAddresses.size());

  for (String registeredAddress : registeredAddresses) {
    Uri    uri          = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(registeredAddress));
    Cursor lookupCursor = resolver.query(uri, inProjection, null, null, null);

    try {
      if (lookupCursor != null && lookupCursor.moveToFirst()) {
        final ContactData contactData = new ContactData(lookupCursor.getLong(0), lookupCursor.getString(1));
        contactData.numbers.add(new NumberData("TextSecure", registeredAddress));
        lookupData.add(contactData);
      }
    } finally {
      if (lookupCursor != null)
        lookupCursor.close();
    }
  }

  return lookupData;
}
 
Example #10
Source File: ContactLookup.java    From zrtp-java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Search for a phone number on the Android AddressBook
 * 
 * @param number PhoneNumber
 */
public ContactLookup(String number) {
	this.name = number;
	this.number = number;

	if (!number.equals("")) {
		if (number.startsWith("+"))
			number = number.substring(1);
		Cursor c = ((AndroidPlatform) AndroidPlatform.getInstance()).getApplication()
				.getApplicationContext()
				.getContentResolver()
				.query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
						Uri.encode(number)),
						new String[] { PhoneLookup.DISPLAY_NAME }, null, null,
						null);
		
		if (c.getCount() > 0) {
			isOnUserAddressBook = true;
		}


		while (c.moveToNext())
			this.name = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
		
		c.close();
	}
	
	
}
 
Example #11
Source File: RecipientProvider.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
private @NonNull RecipientDetails getIndividualRecipientDetails(Context context, long recipientId, @NonNull String number) {
  Optional<RecipientsPreferences> preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientsPreferences(new long[]{recipientId});
  MaterialColor                   color       = preferences.isPresent() ? preferences.get().getColor() : null;
  Uri                             uri         = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

  try (Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION, null, null, null)) {
    if (cursor != null && cursor.moveToFirst()) {
      final String resultNumber = cursor.getString(3);
      if (resultNumber != null) {
        Uri          contactUri   = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
        String       name         = resultNumber.equals(cursor.getString(0)) ? null : cursor.getString(0);
        ContactPhoto contactPhoto = ContactPhotoFactory.getContactPhoto(context,
                                                                        Uri.withAppendedPath(Contacts.CONTENT_URI, cursor.getLong(2) + ""),
                                                                        name);

        return new RecipientDetails(cursor.getString(0), resultNumber, contactUri, contactPhoto, color);
      } else {
        Log.w(TAG, "resultNumber is null");
      }
    }
  } catch (SecurityException se) {
    Log.w(TAG, se);
  }

  if (STATIC_DETAILS.containsKey(number)) return STATIC_DETAILS.get(number);
  else                                    return new RecipientDetails(null, number, null, ContactPhotoFactory.getDefaultContactPhoto(null), color);
}
 
Example #12
Source File: AsyncContactImageLoader.java    From emerald-dialer with GNU General Public License v3.0 5 votes vote down vote up
Drawable loadImageForNumber(String number) {
	if (null == number) {
		return mDefaultDrawable;
	}
	Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
	Cursor cursor = mContext.getContentResolver().query(uri, new String[] {PhoneLookup.LOOKUP_KEY}, null, null, null);
	if (cursor == null || !cursor.moveToFirst()) {
		return mDefaultDrawable;
	}
	String lookupKey = cursor.getString(0);
	cursor.close();
	return loadImageForContact(lookupKey);
	
}
 
Example #13
Source File: ContactUtils.java    From call_manage with MIT License 5 votes vote down vote up
/**
 * Opens a contact in the default contacts app by a given number
 *
 * @param activity
 * @param number
 */
public static void openContactByNumber(Activity activity, String number) {
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] projection = new String[]{PhoneLookup._ID};
    Cursor cursor = activity.getContentResolver().query(uri, projection, null, null, null);

    // if cursor isn't empty, take the first result
    if (cursor != null && cursor.moveToNext()) {
        Long id = cursor.getLong(0);
        openContactById(activity, id);
    }
    cursor.close(); // close the mf cursor
}
 
Example #14
Source File: PhoneUtilEclair.java    From DumbphoneAssistant with GNU General Public License v2.0 4 votes vote down vote up
public Uri retrieveContactUri(Contact contact) {
    String lookupKey;
    Long contactId;
    Cursor result = null;
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.RawContacts.CONTACT_ID };
    String selection;
    String[] selectionArgs;

    // at first try to resolve with contacts id
    if (contact.getId() != null) {
        selection = PhoneLookup._ID + "=?";
        selectionArgs = new String[] { contact.getId() };
        result = resolver.query(uri, projection, selection, selectionArgs, null);
        // check if unique result
        if (result.getCount() != 1) {
            result.close();
            result = null;
        }
    }
    
    // if no contact id or no result, try alternate method
    if (result == null) {
        selection = ContactsContract.Contacts.DISPLAY_NAME + " = '?' AND "
                + ContactsContract.CommonDataKinds.Phone.NUMBER + " = '?'"
        ;
        selectionArgs = new String[] { contact.getName(), contact.getNumber() };
        result = resolver.query(uri, projection, selection, selectionArgs, null);
        // check if unique result
        if (result.getCount() != 1) {
            result.close();
            result = null;
        }
    }
            
    // check for result
    if (result == null) {
        return null;
    }
    
    // get results
    result.moveToNext();
    lookupKey = result.getString(0);
    contactId = result.getLong(1);
    result.close();

    // create contact URI
    return ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
}
 
Example #15
Source File: PhoneUtilEclair.java    From DumbphoneAssistant with GNU General Public License v2.0 4 votes vote down vote up
public ArrayList<Contact> get() {
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] {
            PhoneLookup._ID,
            PhoneLookup.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.TYPE,
            ContactsContract.CommonDataKinds.Phone.LABEL,
            ContactsContract.CommonDataKinds.Phone.NUMBER
    };
    String[] simTypesQueryParts = new String[simTypes.length];
    Arrays.fill(simTypesQueryParts, ContactsContract.RawContacts.ACCOUNT_TYPE + " <> ?");
    String simTypesQuery = TextUtils.join(" AND ", simTypesQueryParts);
    String selection = ContactsContract.RawContacts.ACCOUNT_TYPE + " IS NULL OR (" + simTypesQuery + ")";
    String[] selectionArgs = simTypes;

    Cursor results = resolver.query(
            uri,
            projection,
            selection,
            selectionArgs,
            null
    );

    // create array of Phone contacts and fill it
    final ArrayList<Contact> phoneContacts = new ArrayList<>();
    int indexId = results.getColumnIndex(PhoneLookup._ID);
    int indexName = results.getColumnIndex(PhoneLookup.DISPLAY_NAME);
    int indexType = results.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    int indexLabel = results.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
    int indexNumber = results.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    while (results.moveToNext()) {
        int type = results.getInt(indexType);
        String custom = results.getString(indexLabel);
        final Contact phoneContact = new Contact(
                results.getString(indexId),
                results.getString(indexName),
                results.getString(indexNumber),
                (String) Phone.getTypeLabel(this.activity.getResources(), type, custom)
        );
        phoneContacts.add(phoneContact);
    }
    results.close();
    return phoneContacts;
}
 
Example #16
Source File: ContactBadge.java    From Android-ContactPicker with Apache License 2.0 3 votes vote down vote up
/**
 * Assign a contact based on a phone number. This should only be used when the contact's URI is
 * not available, as an extra query will have to be performed to lookup the URI based on the
 * phone number.
 *
 * @param phoneNumber The phone number of the contact.
 * @param lazyLookup  If this is true, the lookup query will not be performed  until this view is
 *                    clicked.
 * @param extras      A bundle of extras to populate the contact edit page with if the contact is not
 *                    found and the user chooses to add the phone number to an existing contact or
 *                    create a new contact. Uses the same string constants as those found in
 *                    {@link android.provider.ContactsContract.Intents.Insert}
 */
public void assignContactFromPhone(String phoneNumber, boolean lazyLookup, Bundle extras) {
    mContactPhone = phoneNumber;
    mExtras = extras;
    if (!lazyLookup && mQueryHandler != null) {
        mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP, null,
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
                PHONE_LOOKUP_PROJECTION, null, null, null, mContactQueryHandlerCallback);
    } else {
        mContactUri = null;
        onContactUriChanged();
    }
}
 
Example #17
Source File: QuickContactBadge.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Assign a contact based on a phone number. This should only be used when
 * the contact's URI is not available, as an extra query will have to be
 * performed to lookup the URI based on the phone number.
 *
 * @param phoneNumber The phone number of the contact.
 * @param lazyLookup If this is true, the lookup query will not be performed
 * until this view is clicked.
 * @param extras A bundle of extras to populate the contact edit page with if the contact
 * is not found and the user chooses to add the phone number to an existing contact or
 * create a new contact. Uses the same string constants as those found in
 * {@link android.provider.ContactsContract.Intents.Insert}
 */
public void assignContactFromPhone(String phoneNumber, boolean lazyLookup, Bundle extras) {
    mContactPhone = phoneNumber;
    mExtras = extras;
    if (!lazyLookup && mQueryHandler != null) {
        mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP, null,
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone),
                PHONE_LOOKUP_PROJECTION, null, null, null);
    } else {
        mContactUri = null;
        onContactUriChanged();
    }
}