Skip to content

Commit

Permalink
show call time
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Hibbe <[email protected]>
  • Loading branch information
mahibi committed Jul 27, 2023
1 parent 8513a32 commit 1d5c929
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 60 deletions.
159 changes: 100 additions & 59 deletions app/src/main/java/com/nextcloud/talk/activities/CallActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import android.os.Handler
import android.os.Looper
import android.provider.Settings
import android.text.TextUtils
import android.text.format.DateUtils
import android.util.Log
import android.view.MotionEvent
import android.view.View
Expand Down Expand Up @@ -84,8 +85,8 @@ import com.nextcloud.talk.events.ProximitySensorEvent
import com.nextcloud.talk.events.WebSocketCommunicationEvent
import com.nextcloud.talk.models.ExternalSignalingServer
import com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall
import com.nextcloud.talk.models.json.conversations.Conversation
import com.nextcloud.talk.models.json.conversations.RoomOverall
import com.nextcloud.talk.models.json.conversations.RoomsOverall
import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.models.json.participants.Participant
import com.nextcloud.talk.models.json.signaling.DataChannelMessage
Expand Down Expand Up @@ -240,6 +241,8 @@ class CallActivity : CallBaseActivity() {
private val callInfosHandler = Handler()
private val cameraSwitchHandler = Handler()

private val callTimeHandler = Handler(Looper.getMainLooper())

// push to talk
private var isPushToTalkActive = false
private var pulseAnimation: PulseAnimation? = null
Expand Down Expand Up @@ -714,37 +717,6 @@ class CallActivity : CallBaseActivity() {
DrawableCompat.setTint(binding!!.audioOutputButton.drawable, Color.WHITE)
}

private fun handleFromNotification() {
val apiVersion = ApiUtils.getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.APIv4, 1))
ncApi!!.getRooms(credentials, ApiUtils.getUrlForRooms(apiVersion, baseUrl), java.lang.Boolean.FALSE)
.retry(3)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Observer<RoomsOverall> {
override fun onSubscribe(d: Disposable) {
// unused atm
}

override fun onNext(roomsOverall: RoomsOverall) {
for ((roomId1, token) in roomsOverall.ocs!!.data!!) {
if (roomId == roomId1) {
roomToken = token
break
}
}
checkDevicePermissions()
}

override fun onError(e: Throwable) {
// unused atm
}

override fun onComplete() {
// unused atm
}
})
}

@SuppressLint("ClickableViewAccessibility")
private fun initViews() {
Log.d(TAG, "initViews")
Expand Down Expand Up @@ -878,6 +850,7 @@ class CallActivity : CallBaseActivity() {
} else {
permissionsToRequest.add(Manifest.permission.RECORD_AUDIO)
}

if (!isVoiceOnlyCall) {
if (permissionUtil!!.isCameraPermissionGranted()) {
if (!videoOn) {
Expand Down Expand Up @@ -913,6 +886,10 @@ class CallActivity : CallBaseActivity() {
requestPermissionLauncher.launch(permissionsToRequest.toTypedArray())
}
}

// TODO: check if this is necessary. fetchSignalingSettings is called twice when the OnClick methods are also
// called

if (!isConnectionEstablished) {
fetchSignalingSettings()
}
Expand Down Expand Up @@ -1473,6 +1450,8 @@ class CallActivity : CallBaseActivity() {
Log.d(TAG, " callSession= $callSession")
val url = ApiUtils.getUrlForParticipantsActive(apiVersion, baseUrl, roomToken)
Log.d(TAG, " url= $url")

// if session is empty, e.g. we when we got here by notification, we need to join the room to get a session
if (TextUtils.isEmpty(callSession)) {
ncApi!!.joinRoom(credentials, url, conversationPassword)
.subscribeOn(Schedulers.io())
Expand All @@ -1488,10 +1467,9 @@ class CallActivity : CallBaseActivity() {
callRecordingViewModel!!.setRecordingState(conversation!!.callRecording)
callSession = conversation.sessionId
Log.d(TAG, " new callSession by joinRoom= $callSession")
ApplicationWideCurrentRoomHolder.getInstance().session = callSession
ApplicationWideCurrentRoomHolder.getInstance().currentRoomId = conversation.roomId
ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken = roomToken
ApplicationWideCurrentRoomHolder.getInstance().userInRoom = conversationUser

setInitialApplicationWideCurrentRoomHolderValues(conversation)

callOrJoinRoomViaWebSocket()
}

Expand All @@ -1518,6 +1496,59 @@ class CallActivity : CallBaseActivity() {
}

private fun performCall() {

fun getRoomAndContinue() {
val getRoomApiVersion = ApiUtils.getConversationApiVersion(
conversationUser,
intArrayOf(ApiUtils.APIv4, 1)
)
ncApi!!.getRoom(credentials, ApiUtils.getUrlForRoom(getRoomApiVersion, baseUrl, roomToken))
.retry(3)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Observer<RoomOverall> {
override fun onSubscribe(d: Disposable) {
// unused atm
}

override fun onNext(roomOverall: RoomOverall) {
val conversation = roomOverall.ocs!!.data
callRecordingViewModel!!.setRecordingState(conversation!!.callRecording)
callSession = conversation.sessionId

setInitialApplicationWideCurrentRoomHolderValues(conversation)

startCallTimeCounter(conversation.callStartTime!!)

if (currentCallStatus !== CallStatus.LEAVING) {
if (currentCallStatus !== CallStatus.IN_CONVERSATION) {
setCallState(CallStatus.JOINED)
}
ApplicationWideCurrentRoomHolder.getInstance().isInCall = true
ApplicationWideCurrentRoomHolder.getInstance().isDialing = false
if (!TextUtils.isEmpty(roomToken)) {
cancelExistingNotificationsForRoom(
applicationContext,
conversationUser!!,
roomToken!!
)
}
if (!hasExternalSignalingServer) {
pullSignalingMessages()
}
}
}

override fun onError(e: Throwable) {
Log.e(TAG, "Failed to get room", e)
}

override fun onComplete() {
// unused atm
}
})
}

var inCallFlag = Participant.InCallFlags.IN_CALL
if (canPublishAudioStream) {
inCallFlag += Participant.InCallFlags.WITH_AUDIO
Expand All @@ -1544,27 +1575,11 @@ class CallActivity : CallBaseActivity() {
}

override fun onNext(genericOverall: GenericOverall) {
if (currentCallStatus !== CallStatus.LEAVING) {
if (currentCallStatus !== CallStatus.IN_CONVERSATION) {
setCallState(CallStatus.JOINED)
}
ApplicationWideCurrentRoomHolder.getInstance().isInCall = true
ApplicationWideCurrentRoomHolder.getInstance().isDialing = false
if (!TextUtils.isEmpty(roomToken)) {
cancelExistingNotificationsForRoom(
applicationContext,
conversationUser!!,
roomToken!!
)
}
if (!hasExternalSignalingServer) {
pullSignalingMessages()
}
}
getRoomAndContinue()
}

override fun onError(e: Throwable) {
// unused atm
Log.e(TAG, "Failed to join call", e)
}

override fun onComplete() {
Expand All @@ -1573,6 +1588,32 @@ class CallActivity : CallBaseActivity() {
})
}

private fun setInitialApplicationWideCurrentRoomHolderValues(conversation: Conversation) {
ApplicationWideCurrentRoomHolder.getInstance().userInRoom = conversationUser
ApplicationWideCurrentRoomHolder.getInstance().session = conversation.sessionId
ApplicationWideCurrentRoomHolder.getInstance().currentRoomId = conversation.roomId
ApplicationWideCurrentRoomHolder.getInstance().currentRoomToken = conversation.token
ApplicationWideCurrentRoomHolder.getInstance().callStartTime = conversation.callStartTime
}

private fun startCallTimeCounter(callStartTime: Long) {
val currentTimeInSec = System.currentTimeMillis() / 1000
val initialSecondsElapsedByStart = currentTimeInSec - callStartTime
var elapsedSeconds: Long = initialSecondsElapsedByStart

val callTimeTask: Runnable = object : Runnable {
override fun run() {
binding!!.callStartTime.text = DateUtils.formatElapsedTime(elapsedSeconds)
if (elapsedSeconds.toInt() == CALL_TIME_ONE_HOUR) {
Toast.makeText(context, "The call is running for 10s", Toast.LENGTH_LONG).show()
}
elapsedSeconds += 1
callTimeHandler.postDelayed(this, CALL_TIME_COUNTER_DELAY)
}
}
callTimeHandler.post(callTimeTask)
}

private fun pullSignalingMessages() {
val signalingApiVersion = ApiUtils.getSignalingApiVersion(conversationUser, intArrayOf(ApiUtils.APIv3, 2, 1))
val delayOnError = AtomicInteger(0)
Expand Down Expand Up @@ -1648,11 +1689,7 @@ class CallActivity : CallBaseActivity() {
}

private fun initiateCall() {
if (!TextUtils.isEmpty(roomToken)) {
checkDevicePermissions()
} else {
handleFromNotification()
}
checkDevicePermissions()
}

@Subscribe(threadMode = ThreadMode.BACKGROUND)
Expand Down Expand Up @@ -1748,6 +1785,7 @@ class CallActivity : CallBaseActivity() {
setCallState(CallStatus.LEAVING)
}
stopCallingSound()
callTimeHandler.removeCallbacksAndMessages(null)
dispose(null)

if (shutDownView) {
Expand Down Expand Up @@ -2705,6 +2743,7 @@ class CallActivity : CallBaseActivity() {
override fun onSubscribe(d: Disposable) {
// unused atm
}

override fun onNext(signalingOverall: SignalingOverall) {
// When sending messages to the internal signaling server the response has been empty since
// Talk v2.9.0, so it is not really needed to process it, but there is no harm either in
Expand Down Expand Up @@ -2920,5 +2959,7 @@ class CallActivity : CallBaseActivity() {
const val OPACITY_INVISIBLE = 0.0f

const val SECOND_IN_MILLIES: Long = 1000
const val CALL_TIME_COUNTER_DELAY: Long = 1000
const val CALL_TIME_ONE_HOUR = 10
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ data class Conversation(
// "@JsonField annotation can only be used on private fields if both getter and setter are present."
// Instead, name it with "has" at the beginning: isCustomAvatar -> hasCustomAvatar
@JsonField(name = ["isCustomAvatar"])
var hasCustomAvatar: Boolean? = null
var hasCustomAvatar: Boolean? = null,

@JsonField(name = ["callStartTime"])
var callStartTime: Long? = null

) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class ApplicationWideCurrentRoomHolder {
private boolean isDialing = false;
private String session = "";

private Long callStartTime = null;

public static ApplicationWideCurrentRoomHolder getInstance() {
return holder;
}
Expand Down Expand Up @@ -96,4 +98,12 @@ public String getSession() {
public void setSession(String session) {
this.session = session;
}

public Long getCallStartTime() {
return callStartTime;
}

public void setCallStartTime(Long callStartTime) {
this.callStartTime = callStartTime;
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/layout/call_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@
app:layout_constraintTop_toTopOf="parent"
tools:text="Voice Call" />

<TextView
android:id="@+id/call_start_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="00:22" />

<TextView
android:id="@+id/callConversationNameTextView"
android:layout_width="match_parent"
Expand Down

0 comments on commit 1d5c929

Please sign in to comment.