use of dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException 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);
}
}
use of dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException 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;
}
use of dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException 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;
}
use of dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException 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;
}
use of dev.sagar.smsblocker.tech.exceptions.ReadContactPermissionException in project SMSBlocker by sagarpawardev.
the class ThreadActivity method updateActionBar.
private void updateActionBar() {
final String methodName = "updateActionBar()";
log.justEntered(methodName);
String contact = null;
Uri dpUri = null;
try {
contact = contactUtil.getContactName(this, address);
dpUri = contactUtil.getPictureUri(this, address);
} catch (ReadContactPermissionException e) {
e.printStackTrace();
contact = address;
}
log.info(methodName, "Setting contactName: " + contact);
toolbar.setTitle(contact);
if (contact != null && !contact.equals(address)) {
toolbar.setSubtitle(address);
} else
toolbar.setSubtitle(null);
try {
log.info(methodName, "Setting Contact picture in action bar");
Drawable drawable = PictureUtilSingleton.getInstance().getPictureThumbDrawable(this, dpUri);
log.info(methodName, "Received picture: " + drawable);
getSupportActionBar().setLogo(drawable);
} catch (NullPointerException e) {
log.info(methodName, "No picture for Contact..");
}
log.returning(methodName);
}
Aggregations