Simplified code
This commit is contained in:
parent
8d0b571f38
commit
4a05302722
3 changed files with 22 additions and 68 deletions
|
@ -79,9 +79,6 @@ class AsyncContactsLoader(private val context: Context) :
|
||||||
nativeIds.add(contact.nativeId)
|
nativeIds.add(contact.nativeId)
|
||||||
} else {
|
} else {
|
||||||
data.contacts.add(contact)
|
data.contacts.add(contact)
|
||||||
if (contact.sipAddresses.isNotEmpty()) {
|
|
||||||
data.sipContacts.add(contact)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (friend.refKey != null) {
|
if (friend.refKey != null) {
|
||||||
|
@ -95,9 +92,6 @@ class AsyncContactsLoader(private val context: Context) :
|
||||||
contact.syncValuesFromFriend()
|
contact.syncValuesFromFriend()
|
||||||
friend.userData = contact
|
friend.userData = contact
|
||||||
data.contacts.add(contact)
|
data.contacts.add(contact)
|
||||||
if (contact.sipAddresses.isNotEmpty()) {
|
|
||||||
data.sipContacts.add(contact)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -209,11 +203,6 @@ class AsyncContactsLoader(private val context: Context) :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!corePreferences.hideContactsWithoutPresence) {
|
|
||||||
if (contact.sipAddresses.isNotEmpty() && !data.sipContacts.contains(contact)) {
|
|
||||||
data.sipContacts.add(contact)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
data.contacts.add(contact)
|
data.contacts.add(contact)
|
||||||
}
|
}
|
||||||
androidContactsCache.clear()
|
androidContactsCache.clear()
|
||||||
|
@ -226,19 +215,13 @@ class AsyncContactsLoader(private val context: Context) :
|
||||||
|
|
||||||
override fun onPostExecute(data: AsyncContactsData) {
|
override fun onPostExecute(data: AsyncContactsData) {
|
||||||
if (isCancelled) return
|
if (isCancelled) return
|
||||||
Log.i("[Contacts Loader] ${data.contacts.size} contacts found in which ${data.sipContacts.size} are SIP")
|
Log.i("[Contacts Loader] ${data.contacts.size} contacts")
|
||||||
|
|
||||||
for (contact in data.contacts) {
|
for (contact in data.contacts) {
|
||||||
if (contact is NativeContact) {
|
if (contact is NativeContact) {
|
||||||
contact.createOrUpdateFriendFromNativeContact()
|
contact.createOrUpdateFriendFromNativeContact()
|
||||||
|
|
||||||
if (contact.friend?.presenceModel?.basicStatus == PresenceBasicStatus.Open && !data.sipContacts.contains(contact)) {
|
|
||||||
Log.i("[Contacts Loader] Friend $contact has presence information, adding it to SIP list")
|
|
||||||
data.sipContacts.add(contact)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
data.sipContacts.sort()
|
|
||||||
|
|
||||||
// Now that contact fetching is asynchronous, this is required to ensure
|
// Now that contact fetching is asynchronous, this is required to ensure
|
||||||
// presence subscription event will be sent with all friends
|
// presence subscription event will be sent with all friends
|
||||||
|
@ -266,13 +249,12 @@ class AsyncContactsLoader(private val context: Context) :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
coreContext.contactsManager.updateContacts(data.contacts, data.sipContacts)
|
coreContext.contactsManager.updateContacts(data.contacts)
|
||||||
|
|
||||||
Log.i("[Contacts Loader] Synchronization finished")
|
Log.i("[Contacts Loader] Synchronization finished")
|
||||||
}
|
}
|
||||||
|
|
||||||
class AsyncContactsData {
|
class AsyncContactsData {
|
||||||
val contacts = arrayListOf<Contact>()
|
val contacts = arrayListOf<Contact>()
|
||||||
val sipContacts = arrayListOf<Contact>()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,11 +73,6 @@ class ContactsManager(private val context: Context) {
|
||||||
get
|
get
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private set
|
private set
|
||||||
var sipContacts = ArrayList<Contact>()
|
|
||||||
@Synchronized
|
|
||||||
get
|
|
||||||
@Synchronized
|
|
||||||
private set
|
|
||||||
|
|
||||||
val magicSearch: MagicSearch by lazy {
|
val magicSearch: MagicSearch by lazy {
|
||||||
val magicSearch = coreContext.core.createMagicSearch()
|
val magicSearch = coreContext.core.createMagicSearch()
|
||||||
|
@ -93,7 +88,7 @@ class ContactsManager(private val context: Context) {
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private set
|
private set
|
||||||
|
|
||||||
private val friendsMap: HashMap<String, Friend> = HashMap()
|
private val friendsMap: HashMap<String, Contact> = HashMap()
|
||||||
|
|
||||||
private val contactsUpdatedListeners = ArrayList<ContactsUpdatedListener>()
|
private val contactsUpdatedListeners = ArrayList<ContactsUpdatedListener>()
|
||||||
|
|
||||||
|
@ -103,20 +98,12 @@ class ContactsManager(private val context: Context) {
|
||||||
@Synchronized
|
@Synchronized
|
||||||
override fun onPresenceReceived(list: FriendList, friends: Array<Friend>) {
|
override fun onPresenceReceived(list: FriendList, friends: Array<Friend>) {
|
||||||
Log.i("[Contacts Manager] Presence received")
|
Log.i("[Contacts Manager] Presence received")
|
||||||
var sipContactsListUpdated = false
|
|
||||||
for (friend in friends) {
|
for (friend in friends) {
|
||||||
if (refreshContactOnPresenceReceived(friend)) {
|
refreshContactOnPresenceReceived(friend)
|
||||||
sipContactsListUpdated = true
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (sipContactsListUpdated) {
|
|
||||||
sipContacts.sort()
|
|
||||||
Log.i("[Contacts Manager] Notifying observers that list has changed")
|
|
||||||
notifyListeners()
|
notifyListeners()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
if (PermissionHelper.required(context).hasReadContactsPermission()) {
|
if (PermissionHelper.required(context).hasReadContactsPermission()) {
|
||||||
|
@ -185,12 +172,9 @@ class ContactsManager(private val context: Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun updateContacts(all: ArrayList<Contact>, sip: ArrayList<Contact>) {
|
fun updateContacts(all: ArrayList<Contact>) {
|
||||||
contacts.clear()
|
contacts.clear()
|
||||||
sipContacts.clear()
|
|
||||||
|
|
||||||
contacts.addAll(all)
|
contacts.addAll(all)
|
||||||
sipContacts.addAll(sip)
|
|
||||||
|
|
||||||
updateLocalContacts()
|
updateLocalContacts()
|
||||||
|
|
||||||
|
@ -231,10 +215,13 @@ class ContactsManager(private val context: Context) {
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun findContactByPhoneNumber(number: String): Contact? {
|
fun findContactByPhoneNumber(number: String): Contact? {
|
||||||
val cacheFriend = friendsMap[number]
|
val contact = friendsMap[number]
|
||||||
val friend: Friend? = cacheFriend ?: coreContext.core.findFriendByPhoneNumber(number)
|
if (contact != null) return contact
|
||||||
if (cacheFriend == null && friend != null) friendsMap[number] = friend
|
|
||||||
return friend?.userData as? Contact
|
val friend = coreContext.core.findFriendByPhoneNumber(number)
|
||||||
|
val udContact = friend?.userData as? Contact
|
||||||
|
if (udContact != null) friendsMap[number] = udContact
|
||||||
|
return udContact
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
|
@ -277,21 +264,16 @@ class ContactsManager(private val context: Context) {
|
||||||
cleanAddress.clean() // To remove gruu if any
|
cleanAddress.clean() // To remove gruu if any
|
||||||
val cleanStringAddress = cleanAddress.asStringUriOnly()
|
val cleanStringAddress = cleanAddress.asStringUriOnly()
|
||||||
|
|
||||||
val cacheFriend = friendsMap[cleanStringAddress]
|
val cacheContact = friendsMap[cleanStringAddress]
|
||||||
if (cacheFriend != null) {
|
if (cacheContact != null) {
|
||||||
val contact: Contact? = cacheFriend.userData as? Contact
|
return cacheContact
|
||||||
if (contact != null) {
|
|
||||||
Log.i("[Contacts Manager] Found contact $contact from friend in cache: $cacheFriend")
|
|
||||||
return contact
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val friends = coreContext.core.findFriends(address)
|
val friends = coreContext.core.findFriends(address)
|
||||||
for (friend in friends) {
|
for (friend in friends) {
|
||||||
val contact: Contact? = friend?.userData as? Contact
|
val contact: Contact? = friend?.userData as? Contact
|
||||||
if (contact != null) {
|
if (contact != null) {
|
||||||
Log.i("[Contacts Manager] Found contact $contact from friend in Core: $friend")
|
friendsMap[cleanStringAddress] = contact
|
||||||
friendsMap[cleanStringAddress] = friend
|
|
||||||
return contact
|
return contact
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -343,11 +325,6 @@ class ContactsManager(private val context: Context) {
|
||||||
}
|
}
|
||||||
contacts.clear()
|
contacts.clear()
|
||||||
|
|
||||||
for (contact in sipContacts) {
|
|
||||||
contact.friend = null
|
|
||||||
}
|
|
||||||
sipContacts.clear()
|
|
||||||
|
|
||||||
val core = coreContext.core
|
val core = coreContext.core
|
||||||
for (list in core.friendsLists) list.removeListener(friendListListener)
|
for (list in core.friendsLists) list.removeListener(friendListListener)
|
||||||
}
|
}
|
||||||
|
@ -405,8 +382,8 @@ class ContactsManager(private val context: Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
private fun refreshContactOnPresenceReceived(friend: Friend): Boolean {
|
private fun refreshContactOnPresenceReceived(friend: Friend) {
|
||||||
if (friend.userData == null) return false
|
if (friend.userData == null) return
|
||||||
|
|
||||||
val contact: Contact = friend.userData as Contact
|
val contact: Contact = friend.userData as Contact
|
||||||
Log.d("[Contacts Manager] Received presence information for contact $contact")
|
Log.d("[Contacts Manager] Received presence information for contact $contact")
|
||||||
|
@ -420,13 +397,6 @@ class ContactsManager(private val context: Context) {
|
||||||
} else {
|
} else {
|
||||||
notifyListeners(contact)
|
notifyListeners(contact)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sipContacts.contains(contact)) {
|
|
||||||
sipContacts.add(contact)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
|
|
|
@ -223,7 +223,9 @@ class NativeContact(val nativeId: String, private val lookupKey: String? = null)
|
||||||
for (number in rawPhoneNumbers) friend.addPhoneNumber(number)
|
for (number in rawPhoneNumbers) friend.addPhoneNumber(number)
|
||||||
|
|
||||||
friend.done()
|
friend.done()
|
||||||
if (created) coreContext.core.defaultFriendList?.addFriend(friend)
|
if (created) {
|
||||||
|
coreContext.core.defaultFriendList?.addFriend(friend)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue