Search in sources :

Example 6 with ReadContactPermissionException

use of dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException in project SMSBlocker by sagarpawardev.

the class RVStarredSMSAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(SMSViewHolder holder, int position) {
    final String methodName = "onBindViewHolder(SMSViewHolder, int)";
    log.justEntered(methodName);
    // Adi changes Start
    holder.tvBody.setTypeface(myFont);
    holder.tvFrom.setTypeface(myFont);
    holder.tvTime.setTypeface(myFont);
    // Adi changes End
    SMS sms = smses.get(position);
    String address = sms.getAddress();
    Contact contact = null;
    try {
        contact = ContactUtilSingleton.getInstance().getContact(context, sms.getAddress());
    } catch (ReadContactPermissionException e) {
        e.printStackTrace();
    }
    String displayName = contact.getDisplayName();
    if (displayName == null)
        displayName = address;
    long tm = sms.getDateTime();
    String socialDate = DateUtilSingleton.getInstance().socialFormat(tm);
    holder.tvTime.setText(socialDate);
    holder.tvFrom.setText(displayName);
    holder.tvBody.setText(sms.getBody());
    // Setting User Image
    Uri dpUri = contact.getPhotoThumbnail();
    if (dpUri != null) {
        holder.dpView.setPictureSrc(dpUri);
    } else {
        if (!displayName.equals(address)) {
            String c = String.valueOf(displayName.charAt(0));
            holder.dpView.setLetterText(c);
        } else {
            String str = context.getResources().getString(R.string.hash);
            holder.dpView.setLetterText(str);
        }
    }
    log.returning(methodName);
}
Also used : ReadContactPermissionException(dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException) SMS(dev.sagar.smsblocker.tech.beans.SMS) Uri(android.net.Uri) Contact(dev.sagar.smsblocker.tech.beans.Contact)

Example 7 with ReadContactPermissionException

use of dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException in project SMSBlocker by sagarpawardev.

the class ContactUtilSingleton method getAllContacts.

/**
 * This method returns list of all contacts present in Device
 * @param context Context of activity
 * @return List of all contacts
 */
public ArrayList<Contact> getAllContacts(Context context) throws ReadContactPermissionException {
    final String methodName = "getAllContacts()";
    log.info(methodName, "Just Entered..");
    // 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.HAS_PHONE_NUMBER +" > 1"
    "";
    String[] selectionArg = {};
    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);
    }
    if (!cursor.isClosed())
        cursor.close();
    log.returning(methodName);
    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 8 with ReadContactPermissionException

use of dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException in project SMSBlocker by sagarpawardev.

the class ContactUtilSingleton method getContactOrDefault.

public Contact getContactOrDefault(Context context, String phoneNumber) {
    final String methodName = "getContactOrDefault()";
    log.justEntered(methodName);
    Contact contact = null;
    try {
        // TODO Default more data here like Uri, Thumbnail Uri
        contact = getContact(context, phoneNumber);
        String name = contact.getDisplayName() == null ? contact.getNumber() : contact.getDisplayName();
        contact.setDisplayName(name);
    } catch (ReadContactPermissionException e) {
        log.error(methodName, "Got null from contacts so defaulting phoneNu");
        contact = new Contact();
        contact.setId(null);
        String formatAddress = PhoneUtilsSingleton.getInstance().formatNumber(context, phoneNumber);
        contact.setDisplayName(formatAddress);
        contact.setPhoto(null);
        contact.setPhotoThumbnail(null);
        contact.setNumber(phoneNumber);
    }
    log.returning(methodName);
    return contact;
}
Also used : ReadContactPermissionException(dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException) Contact(dev.sagar.smsblocker.tech.beans.Contact)

Aggregations

ReadContactPermissionException (dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException)8 Contact (dev.sagar.smsblocker.tech.beans.Contact)7 Uri (android.net.Uri)6 ContentResolver (android.content.ContentResolver)3 Cursor (android.database.Cursor)3 ArrayList (java.util.ArrayList)2 Intent (android.content.Intent)1 Drawable (android.graphics.drawable.Drawable)1 MenuItem (android.view.MenuItem)1 SMS (dev.sagar.smsblocker.tech.beans.SMS)1