Search in sources :

Example 6 with Contact

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

the class ThreadActivity method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_thread, menu);
    String id = null;
    // Hide Show contact option if contact not in contact
    try {
        Contact contact = ContactUtilSingleton.getInstance().getContact(this, address);
        id = contact.getId();
    } catch (ReadContactPermissionException e) {
        e.printStackTrace();
    }
    if (id == null) {
        MenuItem item = menu.findItem(R.id.showContact);
        item.setVisible(false);
    }
    return true;
}
Also used : ReadContactPermissionException(dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException) MenuItem(android.view.MenuItem) Contact(dev.sagar.smsblocker.tech.beans.Contact)

Example 7 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact 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 8 with Contact

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

the class ContactUtilSingleton method getPictureUri.

/**
 * This method gives URI of Picture of an contact
 * @param context Context of activity
 * @param phoneNo Contact's Phone Number
 * @return URI of Phone number
 */
public Uri getPictureUri(Context context, String phoneNo) throws ReadContactPermissionException {
    final String methodName = "getPictureUri()";
    log.justEntered(methodName);
    Contact contact = getContact(context, phoneNo);
    /*//Caching
        if(contactMap.containsKey(phoneNo)){
            contact = contactMap.get(phoneNo);
        }else {

            //Actual Procedure
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo));
            ContentResolver contentResolver = context.getContentResolver();
            String[] projection = {
                    ContactsContract.PhoneLookup.DISPLAY_NAME,
                    ContactsContract.PhoneLookup._ID,
                    ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI,
                    ContactsContract.CommonDataKinds.Phone.NUMBER
            };

            //String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + "= ?";
            String selection = "";
            //String selectionArg[] = {phoneNo};
            String selectionArg[] = {};
            String mOrder = "";

            Cursor cursor = null;
            cursor = contentResolver.query(uri, projection, selection, selectionArg, mOrder);

            //If Contact is Not Found
            if (cursor == null || cursor.getCount() == 0) {
                contact.setNumber(phoneNo);
                contact.setSubscriptionId(null);
                contact.setDisplayName(null);
                contact.setPhotoThumbnail(null);

                log.info(methodName, "Contact Not Found: " + phoneNo);
            }
            //If contact is Found
            else{
                log.info(methodName, "Found Contacts: " + cursor.getCount());
                cursor.moveToNext();

                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 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: "+phoneNo);
                }

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

        }

        contactMap.put(phoneNo, contact);*/
    log.returning(methodName);
    return contact.getPhotoThumbnail();
}
Also used : Contact(dev.sagar.smsblocker.tech.beans.Contact)

Example 9 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact 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 10 with Contact

use of dev.sagar.smsblocker.tech.beans.Contact 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

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