Search in sources :

Example 1 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact in project SMSBlocker by sagarpawardev.

the class ThreadActivity method showContact.

public void showContact() {
    String contactID = null;
    try {
        Contact contact = ContactUtilSingleton.getInstance().getContact(this, address);
        contactID = contact.getId();
    } catch (ReadContactPermissionException e) {
        e.printStackTrace();
    }
    if (contactID != null) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
        intent.setData(uri);
        startActivity(intent);
    }
}
Also used : ReadContactPermissionException(dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException) Intent(android.content.Intent) Uri(android.net.Uri) Contact(dev.sagar.smsblocker.tech.beans.Contact)

Example 2 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact in project SMSBlocker by sagarpawardev.

the class RVNewThreadAdapter_Contacts method contactsOnBindViewHolder.

private void contactsOnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    final String methodName = "contactsOnBindViewHolder()";
    log.justEntered(methodName);
    ContactViewHolder holder = (ContactViewHolder) viewHolder;
    Contact contact = contacts.get(position);
    String displayName = contact.getDisplayName();
    String phoneNo = contact.getNumber();
    String id = contact.getId();
    holder.tvId.setText(id);
    holder.tvName.setText(displayName);
    holder.tvNumber.setText(phoneNo);
    // Adi changes Start
    Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/VarelaRound-Regular.ttf");
    holder.tvName.setTypeface(myFont, Typeface.BOLD);
    holder.tvNumber.setTypeface(myFont);
    // Adi changes End
    log.returning(methodName);
}
Also used : Typeface(android.graphics.Typeface) Contact(dev.sagar.smsblocker.tech.beans.Contact)

Example 3 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact in project SMSBlocker by sagarpawardev.

the class ContactUtilSingleton method searchContacts.

/**
 * This method returns a list of contacts based on the String pattern
 * @param context Context of activity
 * @param searchStr search String could be partial phone number or contact name
 * @return List of matched contacts
 */
public ArrayList<Contact> searchContacts(Context context, String searchStr) throws ReadContactPermissionException {
    final String methodName = "searchContacts()";
    log.justEntered(methodName);
    // Checking Permission
    boolean hasReadContactPermission = PermissionUtilSingleton.getInstance().hasPermission(context, READ_CONTACTS);
    if (!hasReadContactPermission) {
        throw new ReadContactPermissionException();
    }
    ContentResolver contentResolver = context.getContentResolver();
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI };
    String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ? OR " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE ?";
    String[] selectionArg = { "%" + searchStr + "%", "%" + searchStr + "%" };
    String mOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
    Cursor cursor = contentResolver.query(uri, projection, selection, selectionArg, mOrder);
    log.info(methodName, "Reading Contacts...");
    ArrayList<Contact> contacts = new ArrayList<>();
    if (cursor == null) {
        log.error(methodName, "Received Cursor: " + null);
        return contacts;
    }
    while (cursor.moveToNext()) {
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        if (number == null) {
            log.info(methodName, name + " Does not have a number and its id is: " + id);
            continue;
        }
        Contact contact = new Contact();
        contact.setDisplayName(name);
        contact.setId(id);
        contact.setNumber(number);
        String image_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
        if (image_uri != null) {
            Uri dpUri = Uri.parse(image_uri);
            contact.setPhotoThumbnail(dpUri);
        } else {
            log.info(methodName, number + " Does Not have picture");
        }
        contacts.add(contact);
    }
    cursor.close();
    log.info(methodName, "Returning..");
    return contacts;
}
Also used : ReadContactPermissionException(dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Contact(dev.sagar.smsblocker.tech.beans.Contact)

Example 4 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact in project SMSBlocker by sagarpawardev.

the class ContactUtilSingleton method getContactName.

/**
 * This method converts a phone number to Contract name
 * @param context Context of activity
 * @param phoneNumber Contact's Phone Number
 * @return Name of Contact from Phone Number
 */
public String getContactName(Context context, String phoneNumber) throws ReadContactPermissionException {
    final String methodName = "getContactName()";
    log.info(methodName, "Just Entered...");
    Contact contact = getContact(context, phoneNumber);
    String name = contact.getDisplayName();
    if (name == null) {
        name = phoneNumber;
    }
    /*//Caching
        if(contactMap.containsKey(phoneNumber)) {
            String name = contactMap.get(phoneNumber).getDisplayName();
            return name;
        }

        //Check Permissions if donot have permission return behave like you don't have contact
        boolean hasContactPerm = PermissionUtilSingleton.getInstance().hasPermission(context, READ_CONTACTS);
        if(!hasContactPerm){
            return phoneNumber;
        }

        Contact contact = new Contact();

        //Actual Procedure
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        String projection[] = {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.PHOTO_URI
        };
        Cursor cursor = contentResolver.query(uri, projection, null, null, null);
        if (cursor == null) {
            log.error(methodName, "Nothing in Cursor for "+phoneNumber);
            contact.setPhotoThumbnail(null);
            contact.setDisplayName(null);
            contact.setSubscriptionId(null);
            contact.setNumber(phoneNumber);
        }
        else {
            String contactName = phoneNumber;

            if (cursor.moveToFirst()) {
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
                String thumb_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI));
                String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                contact.setNumber(number);
                contact.setSubscriptionId(id);
                contact.setDisplayName(contactName);
                if(thumb_uri != null){
                    Uri imgUri = Uri.parse(thumb_uri);
                    contact.setPhotoThumbnail(imgUri);
                    log.info(methodName, "Found Profile ficture...");
                }
                else{
                    log.info(methodName, "No picture for phone number: "+phoneNumber);
                }
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }
        }

        contactMap.put(phoneNumber, contact);
        String name = contact.getDisplayName();
        if(name == null){
            name = phoneNumber;
        }*/
    log.returning(methodName);
    return name;
}
Also used : Contact(dev.sagar.smsblocker.tech.beans.Contact)

Example 5 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact in project SMSBlocker by sagarpawardev.

the class ContactUtilSingleton method getContact.

public Contact getContact(Context context, String phoneNumber) throws ReadContactPermissionException {
    final String methodName = "getContact(Context, String)";
    log.justEntered(methodName);
    Contact result = new Contact();
    phoneNumber = PhoneUtilsSingleton.getInstance().formatNumber(context, phoneNumber);
    // Caching
    log.info(methodName, "Checking in cache");
    if (contactMap.containsKey(phoneNumber)) {
        log.info(methodName, phoneNumber + " Found in cache ");
        result = contactMap.get(phoneNumber);
    } else {
        // Check Permissions if donot have permission return behave like you don't have contact
        boolean hasContactPerm = PermissionUtilSingleton.getInstance().hasPermission(context, READ_CONTACTS);
        if (!hasContactPerm) {
            log.info(methodName, "No permissions for is given");
            throw new ReadContactPermissionException();
        }
        // Actual Procedure
        log.info(methodName, "Reading Content provider for contact");
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        String[] projection = { ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI, ContactsContract.PhoneLookup.PHOTO_URI };
        Cursor cursor = contentResolver.query(uri, projection, null, null, null);
        if (cursor == null || cursor.getCount() == 0) {
            log.error(methodName, "Nothing in Cursor for " + phoneNumber);
            result.setPhotoThumbnail(null);
            result.setId(null);
            result.setDisplayName(null);
            result.setPhoto(null);
            result.setNumber(phoneNumber);
        } else {
            log.info(methodName, "Cursor Size: " + cursor.getCount());
            if (cursor.moveToFirst()) {
                String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
                String thumb_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI));
                String photo_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
                String number = phoneNumber;
                log.info(methodName, "Received contact data for: " + number);
                result.setNumber(number);
                result.setId(id);
                result.setDisplayName(contactName);
                if (thumb_uri != null) {
                    Uri imgUri = Uri.parse(thumb_uri);
                    result.setPhotoThumbnail(imgUri);
                    log.info(methodName, "Found Profile picture...");
                } else {
                    log.info(methodName, "No picture for phone number: " + phoneNumber);
                }
                if (photo_uri != null) {
                    Uri imgUri = Uri.parse(photo_uri);
                    result.setPhoto(imgUri);
                    log.info(methodName, "Found Profile picture thumbnail...");
                } else {
                    log.info(methodName, "No Thumbnail picture for phone number: " + phoneNumber);
                }
            }
            if (!cursor.isClosed()) {
                cursor.close();
            }
        }
        // Cache Data
        contactMap.put(phoneNumber, result);
    }
    log.returning(methodName);
    return result;
}
Also used : ReadContactPermissionException(dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException) Cursor(android.database.Cursor) Uri(android.net.Uri) Contact(dev.sagar.smsblocker.tech.beans.Contact) ContentResolver(android.content.ContentResolver)

Aggregations

Contact (dev.sagar.smsblocker.tech.beans.Contact)11 ReadContactPermissionException (dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException)7 Uri (android.net.Uri)5 ContentResolver (android.content.ContentResolver)3 Cursor (android.database.Cursor)3 Intent (android.content.Intent)2 ArrayList (java.util.ArrayList)2 Notification (android.app.Notification)1 PendingIntent (android.app.PendingIntent)1 Typeface (android.graphics.Typeface)1 Bundle (android.os.Bundle)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)1 MenuItem (android.view.MenuItem)1 SMS (dev.sagar.smsblocker.tech.beans.SMS)1