Fixed issues with contact creation & removal

This commit is contained in:
Sylvain Berfini 2020-04-05 18:57:04 +02:00
parent 69618141f2
commit 4c30e385f0
6 changed files with 59 additions and 8 deletions

View file

@ -84,6 +84,7 @@ class ContactEditorFragment : Fragment() {
binding.setSaveChangesClickListener {
val savedContact = viewModel.save()
if (savedContact is NativeContact) {
savedContact.syncValuesFromAndroidContact(requireContext())
val deepLink = "linphone-android://contact/view/${savedContact.nativeId}"
Log.i("[Contact Editor] Displaying contact, starting deep link: $deepLink")
findNavController().navigate(Uri.parse(deepLink))

View file

@ -115,6 +115,11 @@ class DetailContactFragment : Fragment() {
}
}
binding.setDeleteClickListener {
viewModel.deleteContact()
findNavController().navigateUp()
}
viewModel.onErrorEvent.observe(viewLifecycleOwner, Observer {
it.consume { messageResourceId ->
(activity as MainActivity).showSnackBar(messageResourceId)

View file

@ -169,15 +169,17 @@ class MasterContactsFragment : MasterFragment() {
} else {
// When trying to display a non-native contact from history
val addressString = arguments?.getString("address")
val address = Factory.instance().createAddress(addressString)
if (address != null) {
Log.i("[Contacts] Found friend native pointer parameter in arguments: ${address.asStringUriOnly()}")
arguments?.clear()
if (addressString != null) {
val address = Factory.instance().createAddress(addressString)
if (address != null) {
Log.i("[Contacts] Found friend native pointer parameter in arguments: ${address.asStringUriOnly()}")
arguments?.clear()
val contact = coreContext.contactsManager.findContactByAddress(address)
if (contact != null) {
Log.i("[Contacts] Found matching contact $contact")
adapter.selectedContactEvent.value = Event(contact)
val contact = coreContext.contactsManager.findContactByAddress(address)
if (contact != null) {
Log.i("[Contacts] Found matching contact $contact")
adapter.selectedContactEvent.value = Event(contact)
}
}
}
}

View file

@ -78,7 +78,9 @@ class ContactEditorViewModel(val c: Contact?) : ViewModel(), ContactViewModelInt
fun save(): Contact {
var contact = c
var created = false
if (contact == null) {
created = true
contact = if (PermissionHelper.get().hasWriteContactsPermission()) {
NativeContact(AppUtils.createAndroidContact().toString())
} else {
@ -107,6 +109,10 @@ class ContactEditorViewModel(val c: Contact?) : ViewModel(), ContactViewModelInt
}
}
}
if (created) {
coreContext.contactsManager.addContact(contact)
}
return contact
}

View file

@ -19,6 +19,8 @@
*/
package org.linphone.activities.main.contact.viewmodels
import android.content.ContentProviderOperation
import android.provider.ContactsContract
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
@ -129,6 +131,36 @@ class ContactViewModel(private val c: Contact) : ErrorReportingViewModel(), Cont
super.onCleared()
}
fun deleteContact() {
val select = ContactsContract.Data.CONTACT_ID + " = ?"
val ops = java.util.ArrayList<ContentProviderOperation>()
if (c is NativeContact) {
val nativeContact: NativeContact = c
Log.i("[Contact] Setting Android contact id ${nativeContact.nativeId} to batch removal")
val args = arrayOf(nativeContact.nativeId)
ops.add(
ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
.withSelection(select, args)
.build()
)
}
if (c.friend != null) {
Log.i("[Contact] Removing friend")
c.friend?.remove()
}
if (ops.isNotEmpty()) {
try {
Log.i("[Contact] Removing ${ops.size} contacts")
coreContext.context.contentResolver.applyBatch(ContactsContract.AUTHORITY, ops)
} catch (e: Exception) {
Log.e("[Contact] $e")
}
}
}
private fun updateNumbersAndAddresses() {
val list = arrayListOf<ContactNumberOrAddressViewModel>()
for (address in c.sipAddresses) {

View file

@ -117,6 +117,11 @@ class ContactsManager(private val context: Context) {
loadContactsTask?.executeOnExecutor(THREAD_POOL_EXECUTOR)
}
@Synchronized
fun addContact(contact: Contact) {
contacts.add(contact)
}
@Synchronized
fun updateContacts(all: ArrayList<Contact>, sip: ArrayList<Contact>) {
contacts.clear()