properly handle photo adding and removing at contact update

This commit is contained in:
tibbi 2017-12-26 19:51:48 +01:00
parent 51312dee1c
commit 70b011a697
6 changed files with 90 additions and 25 deletions

View File

@ -36,7 +36,7 @@ ext {
}
dependencies {
implementation 'com.simplemobiletools:commons:3.4.2'
implementation 'com.simplemobiletools:commons:3.4.5'
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion"

View File

@ -34,8 +34,7 @@ import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.sendEmailIntent
import com.simplemobiletools.contacts.extensions.sendSMSIntent
import com.simplemobiletools.contacts.extensions.tryStartCall
import com.simplemobiletools.contacts.helpers.CONTACT_ID
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.*
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.Email
import com.simplemobiletools.contacts.models.PhoneNumber
@ -327,6 +326,8 @@ class ContactActivity : SimpleActivity() {
}
contact!!.apply {
val oldPhotoUri = photoUri
firstName = contact_first_name.value
middleName = contact_middle_name.value
surname = contact_surname.value
@ -339,7 +340,8 @@ class ContactActivity : SimpleActivity() {
if (id == 0) {
insertNewContact()
} else {
updateContact()
val photoUpdateStatus = getPhotoUpdateStatus(oldPhotoUri, photoUri)
updateContact(photoUpdateStatus)
}
}.start()
}
@ -384,15 +386,27 @@ class ContactActivity : SimpleActivity() {
}
}
private fun updateContact() {
private fun updateContact(photoUpdateStatus: Int) {
isSaving = true
if (ContactsHelper(this@ContactActivity).updateContact(contact!!)) {
if (ContactsHelper(this@ContactActivity).updateContact(contact!!, photoUpdateStatus)) {
finish()
} else {
toast(R.string.unknown_error_occurred)
}
}
private fun getPhotoUpdateStatus(oldUri: String, newUri: String): Int {
return if (oldUri.isEmpty() && newUri.isNotEmpty()) {
PHOTO_ADDED
} else if (oldUri.isNotEmpty() && newUri.isEmpty()) {
PHOTO_REMOVED
} else if (oldUri != newUri) {
PHOTO_CHANGED
} else {
PHOTO_UNCHANGED
}
}
private fun addNewPhoneNumberField() {
layoutInflater.inflate(R.layout.item_phone_number, contact_numbers_holder, false).apply {
updateTextColors(this as ViewGroup)

View File

@ -8,6 +8,7 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.signature.ObjectKey
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
@ -118,6 +119,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
if (contact.photoUri.isNotEmpty()) {
val options = RequestOptions()
.signature(ObjectKey(contact.photoUri))
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.error(contactDrawable)
.centerCrop()

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.contacts.helpers
import android.content.Context
import com.simplemobiletools.commons.helpers.BaseConfig
import com.simplemobiletools.commons.helpers.SORTING
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
class Config(context: Context) : BaseConfig(context) {

View File

@ -5,5 +5,10 @@ val CALL_CONTACT_ON_CLICK = "call_contact_on_click"
val DISPLAY_CONTACT_SOURCES = "display_contact_sources"
val START_NAME_WITH_SURNAME = "start_name_with_surname"
val SORTING = "sorting"
val CONTACT_ID = "contact_id"
// contact photo changes
val PHOTO_ADDED = 1
val PHOTO_REMOVED = 2
val PHOTO_CHANGED = 3
val PHOTO_UNCHANGED = 4

View File

@ -27,7 +27,6 @@ import com.simplemobiletools.contacts.overloads.times
import java.io.ByteArrayOutputStream
import java.util.*
class ContactsHelper(val activity: BaseSimpleActivity) {
fun getContactSources(callback: (ArrayList<String>) -> Unit) {
val accounts = HashSet<String>()
@ -74,7 +73,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID)
val id = cursor.getIntValue(ContactsContract.Data.RAW_CONTACT_ID)
val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
val surname = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
@ -192,7 +191,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val uri = ContactsContract.Data.CONTENT_URI
val projection = getContactProjection()
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID} = ?"
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, id.toString())
var cursor: Cursor? = null
try {
@ -215,7 +214,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
private fun getContactProjection() = arrayOf(
ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID,
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
@ -238,11 +237,12 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return sort
}
fun updateContact(contact: Contact): Boolean {
fun updateContact(contact: Contact, photoUpdateStatus: Int): Boolean {
return try {
activity.toast(R.string.updating)
val operations = ArrayList<ContentProviderOperation>()
ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).apply {
val selection = "${ContactsContract.Data.CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?"
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?"
val selectionArgs = arrayOf(contact.id.toString(), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
withSelection(selection, selectionArgs)
withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contact.firstName)
@ -289,6 +289,12 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
}
// photo
when (photoUpdateStatus) {
PHOTO_ADDED -> addPhoto(contact, operations)
PHOTO_REMOVED -> removePhoto(contact, operations)
PHOTO_CHANGED -> {}
}
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
true
} catch (e: Exception) {
@ -297,6 +303,48 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
}
private fun addPhoto(contact: Contact, operations: ArrayList<ContentProviderOperation>): ArrayList<ContentProviderOperation> {
if (contact.photoUri.isNotEmpty()) {
val photoUri = Uri.parse(contact.photoUri)
val bitmap = MediaStore.Images.Media.getBitmap(activity.contentResolver, photoUri)
val thumbnailSize = getThumbnailSize()
val scaledPhoto = Bitmap.createScaledBitmap(bitmap, thumbnailSize, thumbnailSize, false)
val scaledSizePhotoData = bitmapToByteArray(scaledPhoto)
scaledPhoto.recycle()
val fullSizePhotoData = bitmapToByteArray(bitmap)
bitmap.recycle()
bitmap.recycle()
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
withValue(ContactsContract.Data.RAW_CONTACT_ID, contact.id)
withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, scaledSizePhotoData)
operations.add(this.build())
}
val baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contact.id.toLong())
val displayPhotoUri = Uri.withAppendedPath(baseUri, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
val fileDescriptor = activity.contentResolver.openAssetFileDescriptor(displayPhotoUri, "rw")
val photoStream = fileDescriptor.createOutputStream()
photoStream.write(fullSizePhotoData)
photoStream.close()
fileDescriptor.close()
}
return operations
}
private fun removePhoto(contact: Contact, operations: ArrayList<ContentProviderOperation>): ArrayList<ContentProviderOperation> {
ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI).apply {
val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?"
val selectionArgs = arrayOf(contact.id.toString(), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
withSelection(selection, selectionArgs)
operations.add(this.build())
}
return operations
}
fun insertContact(contact: Contact): Boolean {
return try {
activity.toast(R.string.inserting)
@ -373,16 +421,11 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val rawContactId = ContentUris.parseId(results[0].uri)
val baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId)
val displayPhotoUri = Uri.withAppendedPath(baseUri, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
val photoStream = activity.contentResolver.openAssetFileDescriptor(displayPhotoUri, "rw").createOutputStream()
photoStream.use {
var bufferSize = 16 * 1024
var offset = 0
while (offset < fullSizePhotoData.size) {
bufferSize = Math.min(bufferSize, fullSizePhotoData.size - offset)
photoStream.write(fullSizePhotoData, offset, bufferSize)
offset += bufferSize
}
}
val fileDescriptor = activity.contentResolver.openAssetFileDescriptor(displayPhotoUri, "rw")
val photoStream = fileDescriptor.createOutputStream()
photoStream.write(fullSizePhotoData)
photoStream.close()
fileDescriptor.close()
}
true
@ -434,10 +477,10 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
private fun getThumbnailSize(): Int {
val uri = ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI
val projection = arrayOf(ContactsContract.DisplayPhoto.THUMBNAIL_MAX_DIM)
var cursor: Cursor? = null
try {
val uri = ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI
val projection = arrayOf(ContactsContract.DisplayPhoto.THUMBNAIL_MAX_DIM)
cursor = activity.contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
return cursor.getIntValue(ContactsContract.DisplayPhoto.THUMBNAIL_MAX_DIM)