diff --git a/app/src/main/java/org/linphone/activities/main/history/adapters/CallLogsListAdapter.kt b/app/src/main/java/org/linphone/activities/main/history/adapters/CallLogsListAdapter.kt index ae1e80203..a2506d516 100644 --- a/app/src/main/java/org/linphone/activities/main/history/adapters/CallLogsListAdapter.kt +++ b/app/src/main/java/org/linphone/activities/main/history/adapters/CallLogsListAdapter.kt @@ -65,7 +65,7 @@ class CallLogsListAdapter( ) : RecyclerView.ViewHolder(binding.root) { fun bind(callLogGroup: GroupedCallLogData) { with(binding) { - val callLogViewModel = callLogGroup.lastCallLogViewModel + val callLogViewModel = callLogGroup.lastCallLogData viewModel = callLogViewModel lifecycleOwner = viewLifecycleOwner diff --git a/app/src/main/java/org/linphone/activities/main/history/data/CallLogData.kt b/app/src/main/java/org/linphone/activities/main/history/data/CallLogData.kt new file mode 100644 index 000000000..72483c694 --- /dev/null +++ b/app/src/main/java/org/linphone/activities/main/history/data/CallLogData.kt @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2010-2021 Belledonne Communications SARL. + * + * This file is part of linphone-android + * (see https://www.linphone.org). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.linphone.activities.main.history.data + +import java.text.SimpleDateFormat +import java.util.* +import org.linphone.R +import org.linphone.contact.GenericContactData +import org.linphone.core.Call +import org.linphone.core.CallLog +import org.linphone.utils.TimestampUtils + +class CallLogData(callLog: CallLog) : GenericContactData(callLog.remoteAddress) { + val statusIconResource: Int by lazy { + if (callLog.dir == Call.Dir.Incoming) { + if (callLog.status == Call.Status.Missed) { + R.drawable.call_status_missed + } else { + R.drawable.call_status_incoming + } + } else { + R.drawable.call_status_outgoing + } + } + + val iconContentDescription: Int by lazy { + if (callLog.dir == Call.Dir.Incoming) { + if (callLog.status == Call.Status.Missed) { + R.string.content_description_missed_call + } else { + R.string.content_description_incoming_call + } + } else { + R.string.content_description_outgoing_call + } + } + + val directionIconResource: Int by lazy { + if (callLog.dir == Call.Dir.Incoming) { + if (callLog.status == Call.Status.Missed) { + R.drawable.call_missed + } else { + R.drawable.call_incoming + } + } else { + R.drawable.call_outgoing + } + } + + val duration: String by lazy { + val dateFormat = SimpleDateFormat(if (callLog.duration >= 3600) "HH:mm:ss" else "mm:ss", Locale.getDefault()) + val cal = Calendar.getInstance() + cal[0, 0, 0, 0, 0] = callLog.duration + dateFormat.format(cal.time) + } + + val date: String by lazy { + TimestampUtils.toString(callLog.startDate, shortDate = false, hideYear = false) + } +} diff --git a/app/src/main/java/org/linphone/activities/main/history/data/GroupedCallLogData.kt b/app/src/main/java/org/linphone/activities/main/history/data/GroupedCallLogData.kt index 89b0d1716..ea76e8fe8 100644 --- a/app/src/main/java/org/linphone/activities/main/history/data/GroupedCallLogData.kt +++ b/app/src/main/java/org/linphone/activities/main/history/data/GroupedCallLogData.kt @@ -19,15 +19,14 @@ */ package org.linphone.activities.main.history.data -import org.linphone.activities.main.history.viewmodels.CallLogViewModel import org.linphone.core.CallLog class GroupedCallLogData(callLog: CallLog) { var lastCallLog: CallLog = callLog val callLogs = arrayListOf(callLog) - val lastCallLogViewModel = CallLogViewModel(lastCallLog) + val lastCallLogData = CallLogData(lastCallLog) fun destroy() { - lastCallLogViewModel.destroy() + lastCallLogData.destroy() } } diff --git a/app/src/main/java/org/linphone/activities/main/history/fragments/DetailCallLogFragment.kt b/app/src/main/java/org/linphone/activities/main/history/fragments/DetailCallLogFragment.kt index dc2599280..f4254f96b 100644 --- a/app/src/main/java/org/linphone/activities/main/history/fragments/DetailCallLogFragment.kt +++ b/app/src/main/java/org/linphone/activities/main/history/fragments/DetailCallLogFragment.kt @@ -70,7 +70,7 @@ class DetailCallLogFragment : GenericFragment() { useMaterialSharedAxisXForwardAnimation = sharedViewModel.isSlidingPaneSlideable.value == false - viewModel.relatedCallLogs.value = callLogGroup.callLogs + viewModel.addRelatedCallLogs(callLogGroup.callLogs) binding.setBackClickListener { goBack() diff --git a/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogViewModel.kt b/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogViewModel.kt index f5529312a..22b734f7a 100644 --- a/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogViewModel.kt +++ b/app/src/main/java/org/linphone/activities/main/history/viewmodels/CallLogViewModel.kt @@ -22,17 +22,17 @@ package org.linphone.activities.main.history.viewmodels import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider -import java.text.SimpleDateFormat import java.util.* import kotlin.collections.ArrayList +import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.R +import org.linphone.activities.main.history.data.CallLogData import org.linphone.contact.GenericContactViewModel import org.linphone.core.* import org.linphone.core.tools.Log import org.linphone.utils.Event import org.linphone.utils.LinphoneUtils -import org.linphone.utils.TimestampUtils class CallLogViewModelFactory(private val callLog: CallLog) : ViewModelProvider.NewInstanceFactory() { @@ -48,53 +48,6 @@ class CallLogViewModel(val callLog: CallLog) : GenericContactViewModel(callLog.r LinphoneUtils.getDisplayableAddress(callLog.remoteAddress) } - val statusIconResource: Int by lazy { - if (callLog.dir == Call.Dir.Incoming) { - if (callLog.status == Call.Status.Missed) { - R.drawable.call_status_missed - } else { - R.drawable.call_status_incoming - } - } else { - R.drawable.call_status_outgoing - } - } - - val iconContentDescription: Int by lazy { - if (callLog.dir == Call.Dir.Incoming) { - if (callLog.status == Call.Status.Missed) { - R.string.content_description_missed_call - } else { - R.string.content_description_incoming_call - } - } else { - R.string.content_description_outgoing_call - } - } - - val directionIconResource: Int by lazy { - if (callLog.dir == Call.Dir.Incoming) { - if (callLog.status == Call.Status.Missed) { - R.drawable.call_missed - } else { - R.drawable.call_incoming - } - } else { - R.drawable.call_outgoing - } - } - - val duration: String by lazy { - val dateFormat = SimpleDateFormat(if (callLog.duration >= 3600) "HH:mm:ss" else "mm:ss", Locale.getDefault()) - val cal = Calendar.getInstance() - cal[0, 0, 0, 0, 0] = callLog.duration - dateFormat.format(cal.time) - } - - val date: String by lazy { - TimestampUtils.toString(callLog.startDate, shortDate = false, hideYear = false) - } - val startCallEvent: MutableLiveData> by lazy { MutableLiveData>() } @@ -109,7 +62,16 @@ class CallLogViewModel(val callLog: CallLog) : GenericContactViewModel(callLog.r val secureChatAllowed = contact.value?.friend?.getPresenceModelForUriOrTel(peerSipUri)?.hasCapability(FriendCapability.LimeX3Dh) ?: false - val relatedCallLogs = MutableLiveData>() + val relatedCallLogs = MutableLiveData>() + + private val listener = object : CoreListenerStub() { + override fun onCallLogUpdated(core: Core, log: CallLog) { + if (callLog.remoteAddress.weakEqual(log.remoteAddress) && callLog.localAddress.weakEqual(log.localAddress)) { + Log.i("[History Detail] New call log for ${callLog.remoteAddress.asStringUriOnly()} with local address ${callLog.localAddress.asStringUriOnly()}") + addRelatedCallLogs(arrayListOf(log)) + } + } + } private val chatRoomListener = object : ChatRoomListenerStub() { override fun onStateChanged(chatRoom: ChatRoom, state: ChatRoom.State) { @@ -126,14 +88,19 @@ class CallLogViewModel(val callLog: CallLog) : GenericContactViewModel(callLog.r init { waitForChatRoomCreation.value = false + + coreContext.core.addListener(listener) } override fun onCleared() { + coreContext.core.removeListener(listener) destroy() + super.onCleared() } fun destroy() { + relatedCallLogs.value.orEmpty().forEach(CallLogData::destroy) } fun startCall() { @@ -157,11 +124,15 @@ class CallLogViewModel(val callLog: CallLog) : GenericContactViewModel(callLog.r } } - fun getCallsHistory(): ArrayList { - val callsHistory = ArrayList() - for (callLog in relatedCallLogs.value.orEmpty()) { - callsHistory.add(CallLogViewModel(callLog)) + fun addRelatedCallLogs(logs: ArrayList) { + val callsHistory = ArrayList() + + // We assume new logs are more recent than the ones we already have, so we add them first + for (log in logs) { + callsHistory.add(CallLogData(log)) } - return callsHistory + callsHistory.addAll(relatedCallLogs.value.orEmpty()) + + relatedCallLogs.value = callsHistory } } diff --git a/app/src/main/res/layout/history_detail_cell.xml b/app/src/main/res/layout/history_detail_cell.xml index f682ba7a3..b2aadd39a 100644 --- a/app/src/main/res/layout/history_detail_cell.xml +++ b/app/src/main/res/layout/history_detail_cell.xml @@ -5,7 +5,7 @@ + type="org.linphone.activities.main.history.data.CallLogData" /> - - - - - - - + android:layout_height="match_parent" + android:layout_below="@id/top_bar"> + android:orientation="vertical" + android:paddingTop="10dp" + android:paddingBottom="5dp"> - + - + - + + + + + - + - + - + - + - + - + + + - + - + + type="org.linphone.activities.main.history.data.CallLogData" />