Store native contacts copy inside app

This commit is contained in:
Sylvain Berfini 2024-02-28 16:33:01 +01:00
parent 3a2d85265d
commit 9a1ca386ca
5 changed files with 33 additions and 19 deletions

View file

@ -50,10 +50,10 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
private const val TAG = "[Contacts Loader]"
private const val NATIVE_ADDRESS_BOOK_FRIEND_LIST = "Native address-book"
const val NATIVE_ADDRESS_BOOK_FRIEND_LIST = "Native address-book"
const val LINPHONE_ADDRESS_BOOK_FRIEND_LIST = "Linphone address-book"
private const val MAX_INTERVAL_TO_REFRESH = 60000L // 1 minute
private const val MIN_INTERVAL_TO_WAIT_BEFORE_REFRESH = 300000L // 5 minutes
}
@MainThread
@ -78,7 +78,7 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
ContactsContract.Data.CONTACT_ID + " ASC"
)
loader.setUpdateThrottle(MAX_INTERVAL_TO_REFRESH) // Update at most once per minute
loader.setUpdateThrottle(MIN_INTERVAL_TO_WAIT_BEFORE_REFRESH) // Update at most once per minute
return loader
}
@ -301,12 +301,13 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
Log.i(
"$TAG Friend list [$NATIVE_ADDRESS_BOOK_FRIEND_LIST] didn't exist yet, let's create it"
)
fl.isDatabaseStorageEnabled = false // We don't want to store local address-book in DB
fl.isDatabaseStorageEnabled = true // Store them to allow user
fl.type = FriendList.Type.Default
fl.displayName = NATIVE_ADDRESS_BOOK_FRIEND_LIST
core.addFriendList(fl)
} else {
Log.i(
"$TAG Friend list [$LINPHONE_ADDRESS_BOOK_FRIEND_LIST] found, removing existing friends if any"
"$TAG Friend list [$NATIVE_ADDRESS_BOOK_FRIEND_LIST] found, removing existing friends if any"
)
for (friend in fl.friends) {
fl.removeFriend(friend)

View file

@ -46,6 +46,7 @@ import org.linphone.core.Address
import org.linphone.core.ConferenceInfo
import org.linphone.core.Core
import org.linphone.core.CoreListenerStub
import org.linphone.core.Factory
import org.linphone.core.Friend
import org.linphone.core.FriendList
import org.linphone.core.FriendListListenerStub
@ -93,7 +94,17 @@ class ContactsManager @UiThread constructor() {
Log.d(
"$TAG Newly discovered SIP Address [$sipUri] for friend [${friend.name}] in list [${friendList.displayName}]"
)
newContactAddedWithSipUri(sipUri)
val address = Factory.instance().createAddress(sipUri)
if (address != null) {
Log.i("$TAG Storing discovered SIP URI inside Friend")
friend.edit()
friend.addAddress(address)
friend.done()
newContactAddedWithSipUri(sipUri)
} else {
Log.e("$TAG Failed to parse SIP URI [$sipUri] as Address!")
}
reloadContactsJob = coroutineScope.launch {
delay(DELAY_BEFORE_RELOADING_CONTACTS_AFTER_PRESENCE_RECEIVED)

View file

@ -19,7 +19,6 @@
*/
package org.linphone.ui.call.model
import android.annotation.SuppressLint
import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.lifecycle.MutableLiveData

View file

@ -32,6 +32,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.contacts.ContactLoader
import org.linphone.core.Account
import org.linphone.core.AuthInfo
import org.linphone.core.AuthMethod
@ -180,11 +181,7 @@ class MainViewModel @UiThread constructor() : ViewModel() {
}
RegistrationState.Ok -> {
if (!firstAccountRegistered) {
Log.i(
"$TAG First account registered, start loading contacts if permission has been granted"
)
firstAccountRegistered = true
startLoadingContactsEvent.postValue(Event(true))
triggerNativeAddressBookImport()
}
if (account == core.defaultAccount) {
@ -289,11 +286,7 @@ class MainViewModel @UiThread constructor() : ViewModel() {
atLeastOneCall.postValue(core.callsNb > 0)
if (core.defaultAccount?.state == RegistrationState.Ok && !firstAccountRegistered) {
Log.i(
"$TAG First account registered, start loading contacts if permission has been granted"
)
firstAccountRegistered = true
startLoadingContactsEvent.postValue(Event(true))
triggerNativeAddressBookImport()
}
}
}
@ -472,4 +465,16 @@ class MainViewModel @UiThread constructor() : ViewModel() {
}
}
}
@WorkerThread
private fun triggerNativeAddressBookImport() {
firstAccountRegistered = true
if (coreContext.core.getFriendListByName(ContactLoader.NATIVE_ADDRESS_BOOK_FRIEND_LIST) == null) {
Log.i("$TAG Native friend list not found, trying to fetch native contacts")
startLoadingContactsEvent.postValue(Event(true))
} else {
Log.i("$TAG Native contacts were already imported once, do not do it again")
}
}
}

View file

@ -21,13 +21,11 @@ package org.linphone.utils
import androidx.annotation.AnyThread
import java.text.DateFormat
import java.text.Format
import java.text.SimpleDateFormat
import java.time.format.TextStyle
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.TimeZone
import org.linphone.LinphoneApplication.Companion.coreContext
class TimestampUtils {