handle the contact photo at inserting too

This commit is contained in:
tibbi 2017-12-25 23:06:20 +01:00
parent 4da25f25f2
commit 84be33dbbf
8 changed files with 122 additions and 20 deletions

View File

@ -60,6 +60,7 @@ class ContactActivity : SimpleActivity() {
private var currentContactPhotoPath = ""
private var lastPhotoIntentUri: Uri? = null
private var contact: Contact? = null
private var isSaving = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -321,6 +322,10 @@ class ContactActivity : SimpleActivity() {
}
private fun saveContact() {
if (isSaving) {
return
}
contact!!.apply {
firstName = contact_first_name.value
middleName = contact_middle_name.value
@ -330,11 +335,13 @@ class ContactActivity : SimpleActivity() {
emails = getFilledEmails()
source = contact_source.value
if (id == 0) {
insertNewContact()
} else {
updateContact()
}
Thread {
if (id == 0) {
insertNewContact()
} else {
updateContact()
}
}.start()
}
}
@ -369,6 +376,7 @@ class ContactActivity : SimpleActivity() {
}
private fun insertNewContact() {
isSaving = true
if (ContactsHelper(this@ContactActivity).insertContact(contact!!)) {
finish()
} else {
@ -377,6 +385,7 @@ class ContactActivity : SimpleActivity() {
}
private fun updateContact() {
isSaving = true
if (ContactsHelper(this@ContactActivity).updateContact(contact!!)) {
finish()
} else {

View File

@ -1,24 +1,33 @@
package com.simplemobiletools.contacts.helpers
import android.content.ContentProviderOperation
import android.content.ContentProviderResult
import android.content.ContentUris
import android.database.Cursor
import android.graphics.Bitmap
import android.net.Uri
import android.provider.ContactsContract
import android.provider.MediaStore
import android.util.SparseArray
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.Email
import com.simplemobiletools.contacts.models.PhoneNumber
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>()
@ -107,20 +116,6 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}.start()
}
fun doesSourceContainContacts(source: String): Boolean {
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val projection = arrayOf(ContactsContract.CommonDataKinds.Email.CONTACT_ID)
val selection = "${ContactsContract.RawContacts.ACCOUNT_NAME} = ?"
val selectionArgs = arrayOf(source)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
return (cursor?.moveToFirst() == true)
} finally {
cursor?.close()
}
}
private fun getEmails(contactId: Int? = null): SparseArray<ArrayList<Email>> {
val emails = SparseArray<ArrayList<Email>>()
val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI
@ -304,6 +299,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
fun insertContact(contact: Contact): Boolean {
return try {
activity.toast(R.string.inserting)
val operations = ArrayList<ContentProviderOperation>()
ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).apply {
withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
@ -343,7 +339,52 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
}
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
// photo (inspired by https://gist.github.com/slightfoot/5985900)
var fullSizePhotoData: ByteArray? = null
var scaledSizePhotoData: ByteArray?
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)
scaledSizePhotoData = bitmapToByteArray(scaledPhoto)
scaledPhoto.recycle()
fullSizePhotoData = bitmapToByteArray(bitmap)
bitmap.recycle()
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, scaledSizePhotoData)
operations.add(this.build())
}
}
val results: Array<ContentProviderResult>
try {
results = activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
} finally {
scaledSizePhotoData = null
}
// fullsize photo
if (contact.photoUri.isNotEmpty() && fullSizePhotoData != null) {
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
}
}
}
true
} catch (e: Exception) {
activity.showErrorToast(e)
@ -351,6 +392,17 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
}
private fun bitmapToByteArray(bitmap: Bitmap): ByteArray {
var baos: ByteArrayOutputStream? = null
try {
baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos)
return baos.toByteArray()
} finally {
baos?.close()
}
}
fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact))
fun deleteContacts(contacts: ArrayList<Contact>) {
@ -366,4 +418,33 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
activity.showErrorToast(e)
}
}
fun doesSourceContainContacts(source: String): Boolean {
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val projection = arrayOf(ContactsContract.CommonDataKinds.Email.CONTACT_ID)
val selection = "${ContactsContract.RawContacts.ACCOUNT_NAME} = ?"
val selectionArgs = arrayOf(source)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
return (cursor?.moveToFirst() == true)
} finally {
cursor?.close()
}
}
private fun getThumbnailSize(): Int {
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)
}
} finally {
cursor?.close()
}
return 0
}
}

View File

@ -2,6 +2,8 @@
<string name="app_name">Schlichte Kontakte</string>
<string name="app_launcher_name">Kontakte</string>
<string name="address">Adresse</string>
<string name="inserting">Inserting…</string>
<string name="updating">Updating…</string>
<string name="new_contact">Neuer Kontakt</string>
<string name="edit_contact">Kontakt bearbeiten</string>

View File

@ -2,6 +2,8 @@
<string name="app_name">심플 연락처</string>
<string name="app_launcher_name">연락처</string>
<string name="address">주소</string>
<string name="inserting">Inserting…</string>
<string name="updating">Updating…</string>
<string name="new_contact">새로운 연락처</string>
<string name="edit_contact">연락처 수정</string>

View File

@ -2,6 +2,8 @@
<string name="app_name">Simple Contacts</string>
<string name="app_launcher_name">Contactos</string>
<string name="address">Address</string>
<string name="inserting">Inserting…</string>
<string name="updating">Updating…</string>
<string name="new_contact">Novo contacto</string>
<string name="edit_contact">Editar contacto</string>

View File

@ -2,6 +2,8 @@
<string name="app_name">Simple Contacts</string>
<string name="app_launcher_name">Контакты</string>
<string name="address">Адрес</string>
<string name="inserting">Inserting…</string>
<string name="updating">Updating…</string>
<string name="new_contact">Новый контакт</string>
<string name="edit_contact">Редактировать контакт</string>

View File

@ -2,6 +2,8 @@
<string name="app_name">Jednoduché kontakty</string>
<string name="app_launcher_name">Kontakty</string>
<string name="address">Adresa</string>
<string name="inserting">Vytvára sa…</string>
<string name="updating">Upravuje sa…</string>
<string name="new_contact">Nový kontakt</string>
<string name="edit_contact">Upraviť kontakt</string>

View File

@ -2,6 +2,8 @@
<string name="app_name">Simple Contacts</string>
<string name="app_launcher_name">Contacts</string>
<string name="address">Address</string>
<string name="inserting">Inserting…</string>
<string name="updating">Updating…</string>
<string name="new_contact">New contact</string>
<string name="edit_contact">Edit contact</string>