Added a way to choose which friend list to use to store newly created contacts into (for CardDAV friend list for example)

This commit is contained in:
Sylvain Berfini 2024-08-19 11:59:33 +02:00
parent bf4ab1b412
commit 2c09158977
6 changed files with 112 additions and 10 deletions

View file

@ -26,6 +26,7 @@ import androidx.annotation.WorkerThread
import java.io.File
import java.io.FileOutputStream
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.contacts.ContactLoader.Companion.LINPHONE_ADDRESS_BOOK_FRIEND_LIST
class CorePreferences @UiThread constructor(private val context: Context) {
companion object {
@ -129,6 +130,16 @@ class CorePreferences @UiThread constructor(private val context: Context) {
config.setBool("ui", "show_favorites_contacts", value)
}
var friendListInWhichStoreNewlyCreatedFriends: String
get() = config.getString(
"app",
"friend_list_to_store_newly_created_contacts",
LINPHONE_ADDRESS_BOOK_FRIEND_LIST
)!!
set(value) {
config.setString("app", "friend_list_to_store_newly_created_contacts", value)
}
/* Voice recordings related */
@get:WorkerThread @set:WorkerThread

View file

@ -27,11 +27,13 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
import org.linphone.contacts.ContactLoader.Companion.LINPHONE_ADDRESS_BOOK_FRIEND_LIST
import org.linphone.core.Address
import org.linphone.core.Factory
import org.linphone.core.Friend
import org.linphone.core.FriendList
import org.linphone.core.FriendList.Status
import org.linphone.core.FriendPhoneNumber
import org.linphone.core.SubscribePolicy
@ -224,18 +226,44 @@ class ContactNewOrEditViewModel @UiThread constructor() : GenericViewModel() {
friend.done()
val fl = core.getFriendListByName(LINPHONE_ADDRESS_BOOK_FRIEND_LIST) ?: core.createFriendList()
if (fl.displayName.isNullOrEmpty()) {
Log.i(
"$TAG Locally saved friend list [$LINPHONE_ADDRESS_BOOK_FRIEND_LIST] didn't exist yet, let's create it"
)
val friendListNameToStoreFriends = corePreferences.friendListInWhichStoreNewlyCreatedFriends
Log.i(
"$TAG Looking for friend list with name [$friendListNameToStoreFriends] to use to store newly created contact"
)
val friendList = core.getFriendListByName(friendListNameToStoreFriends) ?: core.getFriendListByName(
LINPHONE_ADDRESS_BOOK_FRIEND_LIST
)
val fl = friendList ?: core.createFriendList()
if (friendList == null) {
if (friendListNameToStoreFriends != LINPHONE_ADDRESS_BOOK_FRIEND_LIST) {
Log.w(
"$TAG Locally saved friend list [$friendListNameToStoreFriends] didn't exist yet (nor [$LINPHONE_ADDRESS_BOOK_FRIEND_LIST]), let's create it"
)
} else {
Log.w(
"$TAG Locally saved friend list [$friendListNameToStoreFriends] didn't exist yet, let's create it"
)
}
fl.isDatabaseStorageEnabled = true // We do want to store friends created in app in DB
fl.displayName = LINPHONE_ADDRESS_BOOK_FRIEND_LIST
core.addFriendList(fl)
}
status = fl.addFriend(friend)
fl.updateSubscriptions()
if (status == Status.OK) {
Log.i("$TAG Contact successfully created, updating subscriptions")
fl.updateSubscriptions()
if (fl.type == FriendList.Type.CardDAV) {
Log.i(
"$TAG Contact successfully created into CardDAV friend list, synchronizing it"
)
fl.synchronizeFriendsFromServer()
}
} else {
Log.e("$TAG Failed to add contact to friend list [${fl.displayName}]!")
}
} else {
Log.i("$TAG Finished applying changes to existing friend")
friend.done()
}

View file

@ -23,7 +23,9 @@ import androidx.annotation.UiThread
import androidx.annotation.WorkerThread
import androidx.lifecycle.MutableLiveData
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
import org.linphone.contacts.ContactLoader.Companion.LINPHONE_ADDRESS_BOOK_FRIEND_LIST
import org.linphone.core.Factory
import org.linphone.core.FriendList
import org.linphone.core.FriendListListenerStub
@ -31,7 +33,7 @@ import org.linphone.core.tools.Log
import org.linphone.ui.GenericViewModel
import org.linphone.utils.Event
class CardDavViewModel : GenericViewModel() {
class CardDavViewModel @UiThread constructor() : GenericViewModel() {
companion object {
private const val TAG = "[CardDAV ViewModel]"
}
@ -52,9 +54,15 @@ class CardDavViewModel : GenericViewModel() {
val syncInProgress = MutableLiveData<Boolean>()
val syncSuccessfulEvent = MutableLiveData<Event<Boolean>>()
val storeNewContactsInIt = MutableLiveData<Boolean>()
val friendListRemovedEvent = MutableLiveData<Event<Boolean>>()
val syncSuccessfulEvent: MutableLiveData<Event<Boolean>> by lazy {
MutableLiveData<Event<Boolean>>()
}
val friendListRemovedEvent: MutableLiveData<Event<Boolean>> by lazy {
MutableLiveData<Event<Boolean>>()
}
private lateinit var friendList: FriendList
@ -110,6 +118,7 @@ class CardDavViewModel : GenericViewModel() {
isEdit.value = false
showPassword.value = false
syncInProgress.value = false
storeNewContactsInIt.value = false
}
override fun onCleared() {
@ -134,6 +143,9 @@ class CardDavViewModel : GenericViewModel() {
friendList.addListener(friendListListener)
displayName.postValue(name)
storeNewContactsInIt.postValue(
name == corePreferences.friendListInWhichStoreNewlyCreatedFriends
)
serverUrl.postValue(friendList.uri)
Log.i("$TAG Existing friend list CardDAV values loaded")
}
@ -144,6 +156,12 @@ class CardDavViewModel : GenericViewModel() {
coreContext.postOnCoreThread { core ->
if (isEdit.value == true && ::friendList.isInitialized) {
val name = friendList.displayName
if (name == corePreferences.friendListInWhichStoreNewlyCreatedFriends) {
Log.i(
"$TAG Deleting friend list configured to be used to store newly created friends, updating default friend list back to [$LINPHONE_ADDRESS_BOOK_FRIEND_LIST]"
)
corePreferences.friendListInWhichStoreNewlyCreatedFriends = LINPHONE_ADDRESS_BOOK_FRIEND_LIST
}
core.removeFriendList(friendList)
Log.i("$TAG Removed friends list with display name [$name]")
showGreenToastEvent.postValue(
@ -225,7 +243,11 @@ class CardDavViewModel : GenericViewModel() {
friendList = core.createFriendList()
friendList.displayName = name
friendList.type = FriendList.Type.CardDAV
friendList.uri = server
friendList.uri = if (server.startsWith("http://") || server.startsWith("https://")) {
server
} else {
"https://$server"
}
friendList.isDatabaseStorageEnabled = true
friendList.addListener(friendListListener)
core.addFriendList(friendList)
@ -235,6 +257,19 @@ class CardDavViewModel : GenericViewModel() {
)
}
if (storeNewContactsInIt.value == true) {
val previous = corePreferences.friendListInWhichStoreNewlyCreatedFriends
Log.i(
"$TAG Updating default friend list to store newly created contacts from [$previous] to [$name]"
)
corePreferences.friendListInWhichStoreNewlyCreatedFriends = name
} else if (storeNewContactsInIt.value == false) {
Log.i(
"$TAG No longer using friend list [$name] as default friend list, switching back to [$LINPHONE_ADDRESS_BOOK_FRIEND_LIST]"
)
corePreferences.friendListInWhichStoreNewlyCreatedFriends = LINPHONE_ADDRESS_BOOK_FRIEND_LIST
}
syncInProgress.postValue(true)
friendList.synchronizeFriendsFromServer()
}

View file

@ -251,6 +251,32 @@
app:layout_constraintTop_toBottomOf="@id/realm_title"
app:layout_constraintBottom_toBottomOf="parent"/>
<androidx.appcompat.widget.AppCompatTextView
style="@style/settings_title_style"
android:id="@+id/save_new_contacts_in_this_list_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="10dp"
android:text="@string/settings_contacts_carddav_use_as_default_title"
android:maxLines="2"
android:ellipsize="end"
app:layout_constraintTop_toTopOf="@id/save_new_contacts_in_this_list_switch"
app:layout_constraintBottom_toBottomOf="@id/save_new_contacts_in_this_list_switch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/save_new_contacts_in_this_list_switch"/>
<com.google.android.material.materialswitch.MaterialSwitch
style="@style/material_switch_style"
android:id="@+id/save_new_contacts_in_this_list_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="16dp"
android:checked="@={viewModel.storeNewContactsInIt}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/realm" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

View file

@ -195,6 +195,7 @@
<string name="settings_contacts_carddav_username_title">Nom d\'utilisateur</string>
<string name="settings_contacts_carddav_password_title">Mot de passe</string>
<string name="settings_contacts_carddav_realm_title">Domaine d\'authentification</string>
<string name="settings_contacts_carddav_use_as_default_title">Stocker ici les contacts nouvellement crées</string>
<string name="settings_contacts_carddav_sync_successful_toast">Synchronization réussie</string>
<string name="settings_contacts_carddav_sync_error_toast">Erreur de synchronization !</string>
<string name="settings_contacts_carddav_deleted_toast">Compte CardDAV supprimé</string>

View file

@ -232,6 +232,7 @@
<string name="settings_contacts_carddav_username_title">Username</string>
<string name="settings_contacts_carddav_password_title">Password</string>
<string name="settings_contacts_carddav_realm_title">Auth realm</string>
<string name="settings_contacts_carddav_use_as_default_title">Store newly created contacts in it</string>
<string name="settings_contacts_carddav_sync_successful_toast">Synchronization was successful</string>
<string name="settings_contacts_carddav_sync_error_toast">Synchronization error!</string>
<string name="settings_contacts_carddav_deleted_toast">CardDAV account removed</string>