diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index b01d45b3..540588e8 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -27,8 +27,6 @@ class BaresipService: Service() { internal lateinit var intent: Intent internal lateinit var nm: NotificationManager internal lateinit var snb: NotificationCompat.Builder - internal lateinit var cnb: NotificationCompat.Builder - internal lateinit var ni: Intent internal lateinit var npi: PendingIntent internal lateinit var nr: BroadcastReceiver internal lateinit var wl: PowerManager.WakeLock @@ -44,9 +42,8 @@ class BaresipService: Service() { nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager createNotificationChannels() snb = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID) - cnb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) - ni = Intent(this, MainActivity::class.java) + val ni = Intent(this, MainActivity::class.java) .setAction(Intent.ACTION_MAIN) .addCategory(Intent.CATEGORY_LAUNCHER) npi = PendingIntent.getActivity(this, 0, ni, 0) @@ -252,51 +249,40 @@ class BaresipService: Service() { } "call incoming" -> { if (!Utils.isVisible()) { + val cnb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) cnb.setVisibility(VISIBILITY_PUBLIC) .setSmallIcon(R.drawable.ic_stat) .setContentIntent(npi) .setAutoCancel(true) + .setContentText("Call from ${Api.call_peeruri(callp)}") if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { cnb.setVibrate(LongArray(0)) .setPriority(Notification.PRIORITY_HIGH) } - val view = RemoteViews(getPackageName(), R.layout.call_notification) + /* val view = RemoteViews(getPackageName(), R.layout.call_notification) view.setTextViewText(R.id.callFrom, "Call from ${Api.call_peeruri(callp)}") - cnb.setContent(view) - nm.notify(CALL_NOTIFICATION_ID, cnb.build()) - } - /* if (!Utils.isVisible()) { - val cnb = NotificationCompat.Builder(this) - .setSmallIcon(R.drawable.ic_stat) - .setContentIntent(npi) - .setVisibility(VISIBILITY_PUBLIC) - .setPriority(Notification.PRIORITY_HIGH) - .setVibrate(LongArray(0)) + cnb.setContent(view) */ val answerIntent = Intent(this, MainActivity::class.java) answerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP) answerIntent.putExtra("action", "answer") - answerIntent.putExtra("uap", uap) answerIntent.putExtra("callp", callp) val answerPendingIntent = PendingIntent.getActivity(this, 0, answerIntent, PendingIntent.FLAG_UPDATE_CURRENT) val rejectIntent = Intent(this, MainActivity::class.java) rejectIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or - Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP) + Intent.FLAG_ACTIVITY_SINGLE_TOP) rejectIntent.putExtra("action", "reject") - rejectIntent.putExtra("uap", uap) rejectIntent.putExtra("callp", callp) val rejectPendingIntent = PendingIntent.getActivity(this, - 0, rejectIntent, PendingIntent.FLAG_UPDATE_CURRENT) - val view = RemoteViews(getPackageName(), R.layout.call_notification) - view.setTextViewText(R.id.caller, "${Api.call_peeruri(callp)}") - view.setOnClickPendingIntent(R.id.answerButton, answerPendingIntent) + 1, rejectIntent, PendingIntent.FLAG_UPDATE_CURRENT) + /* view.setOnClickPendingIntent(R.id.answerButton, answerPendingIntent) view.setOnClickPendingIntent(R.id.rejectButton, rejectPendingIntent) - cnb.setCustomBigContentView(view) - cnb.setVibrate(LongArray(0)) + cnb.setCustomBigContentView(view) */ + cnb.addAction(R.drawable.ic_stat, "Answer", answerPendingIntent) + cnb.addAction(R.drawable.ic_stat, "Reject", rejectPendingIntent) nm.notify(CALL_NOTIFICATION_ID, cnb.build()) - return - } */ + } } "call established", "call closed" -> { nm.cancel(CALL_NOTIFICATION_ID) diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 97dc05e9..e26fed07 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -83,7 +83,7 @@ class MainActivity : AppCompatActivity() { serviceEventReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { handleServiceEvent(intent.getStringExtra("event"), - intent.getStringExtra("uap" ), + intent.getStringExtra("uap"), intent.getStringExtra("callp")) } } @@ -97,11 +97,12 @@ class MainActivity : AppCompatActivity() { arrayOf(Manifest.permission.RECORD_AUDIO), RECORD_AUDIO_PERMISSION) } - aorSpinner = findViewById(R.id.AoRList) as Spinner + aorSpinner = findViewById(R.id.AoRList) uaAdapter = UaSpinnerAdapter(applicationContext, uas, images) aorSpinner.adapter = uaAdapter aorSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { + Log.d("Baresip", "aorSpinner selecting $position") val acc = uas[position].account val aor = acc.aor val ua = uas[position] @@ -144,14 +145,26 @@ class MainActivity : AppCompatActivity() { dtmf.visibility = View.INVISIBLE } } - val `in` = Call.uaCalls(calls, ua, "in") val view_count = layout.childCount // Log.d("Baresip", "View count is $view_count") if (view_count > 7) layout.removeViews(7, view_count - 7) - for (c in `in`) - for (call_index in Call.uaCalls(calls, ua,"in").indices) - addCallViews(ua, c, (call_index + 1) * 10) + for (c in Call.uaCalls(calls, ua, "in")) + for (call_index in Call.uaCalls(calls, ua, "in").indices) { + val startIndex = (call_index + 1) * 10 + addCallViews(ua, c, startIndex) + if (answerCall == c.callp) { + answerCall = "" + val answerButton = layout.findViewById(startIndex + 5) as ImageButton + answerButton.performClick() + } + if (rejectCall == c.callp) { + rejectCall = "" + val rejectButton = layout.findViewById(startIndex + 6) as ImageButton + rejectButton.performClick() + moveTaskToBack(true) + } + } } override fun onNothingSelected(parent: AdapterView<*>) { @@ -184,7 +197,7 @@ class MainActivity : AppCompatActivity() { callee.threshold = 2 callee.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item, - ContactsActivity.contacts.map{Contact -> Contact.name})) + ContactsActivity.contacts.map { Contact -> Contact.name })) securityButton.setOnClickListener { when (securityButton.tag) { @@ -258,7 +271,7 @@ class MainActivity : AppCompatActivity() { Log.d("Baresip", "Hanging up AoR $aor call $callp to " + (findViewById(R.id.callee) as EditText).text) callButton.isEnabled = false - ua_hangup(ua.uap, callp,0, "") + ua_hangup(ua.uap, callp, 0, "") } } } @@ -275,7 +288,7 @@ class MainActivity : AppCompatActivity() { val call = Call.calls(calls, "out")[0] call_unhold(call.callp) call.hold = false - holdButton.tag= "Hold" + holdButton.tag = "Hold" holdButton.setImageResource(R.drawable.hold) } } @@ -332,16 +345,7 @@ class MainActivity : AppCompatActivity() { aorSpinner.setSelection(account_index) else addCallViews(ua, new_call, Call.uaCalls(calls, ua, "in").size * 10) - am.mode = AudioManager.MODE_RINGTONE - rt.play() - rtTimer = Timer() - rtTimer!!.scheduleAtFixedRate(object : TimerTask() { - override fun run() { - if (!rt.isPlaying()) { - rt.play() - } - } - }, 1000 * 1, 1000 * 1) + startRinging() if (Utils.isVisible()) { Log.d("Baresip", "Baresip is visible") val i = Intent(applicationContext, MainActivity::class.java) @@ -531,11 +535,7 @@ class MainActivity : AppCompatActivity() { History.aorRemoveHistory(history, aor) history.add(History(aor, call.peerURI, "in", false)) } - if (rtTimer != null) { - rtTimer!!.cancel() - rtTimer = null - if (rt.isPlaying) rt.stop() - } + stopRinging() } else { Log.d("Baresip", "Removing outgoing call $uap/$callp/" + call.peerURI) @@ -576,21 +576,13 @@ class MainActivity : AppCompatActivity() { val action = intent.getStringExtra("action") Log.d("Baresip", "Got onNewIntent action $action") if (action == null) return - val uap = intent.getStringExtra("uap") val callp = intent.getStringExtra("callp") - val ua = UserAgent.find(uas, intent.getStringExtra("uap"))!! - val aor = ua.account.aor - val peer_uri = Api.call_peeruri(callp) when (action) { "answer" -> { - /* TODO */ + answerCall = callp } "reject" -> { - Log.d("Baresip", "Rejecting incoming call from $peer_uri") - ua_hangup(uap, callp, 486, "Busy Here") - if (History.aorHistory(history, aor) > HISTORY_SIZE) - History.aorRemoveHistory(history, aor) - history.add(History(aor, peer_uri, "in", false)) + rejectCall = callp moveTaskToBack(true) } } @@ -607,6 +599,24 @@ class MainActivity : AppCompatActivity() { Log.d("Baresip", "Resumed") imm.hideSoftInputFromWindow(callee.windowToken, 0) visible = true + if ((answerCall != "") && (layout.childCount > 7)) { + answerCall = "" + /* if multiple calls, right answer button needs to be searched */ + val answerButton = layout.findViewById(10 + 5) as ImageButton + answerButton.performClick() + } + if ((rejectCall != "") && (layout.childCount > 7)) { + rejectCall = "" + /* if multiple calls, right reject button needs to be searched */ + val rejectButton = layout.findViewById(10 + 6) as ImageButton + rejectButton.performClick() + } + } + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + Log.d("Baresip", "View count at onAttachedToWindow is ${layout.childCount}") + Toast.makeText(this, "Hi UI is fully loaded", Toast.LENGTH_SHORT).show(); } override fun onBackPressed() { @@ -727,10 +737,10 @@ class MainActivity : AppCompatActivity() { val call = ua_connect(ua.uap, uri) if (call != "") { Log.d("Baresip", "Adding outgoing call ${ua.uap}/$call/$uri") - val dtmfWatcher = object: TextWatcher { + val dtmfWatcher = object : TextWatcher { override fun beforeTextChanged(sequence: CharSequence, start: Int, count: Int, after: Int) {} override fun onTextChanged(sequence: CharSequence, start: Int, before: Int, count: Int) { - val digit = sequence.subSequence(start, start+count).toString() + val digit = sequence.subSequence(start, start + count).toString() Log.d("Baresip", "Got DTMF digit '" + digit + "'") if (digit.isNotEmpty()) call_send_digit(call, digit[0]) } @@ -738,7 +748,7 @@ class MainActivity : AppCompatActivity() { call_send_digit(call, 4.toChar()) } } - calls.add(Call(call, ua, uri, "out","Cancel", dtmfWatcher)) + calls.add(Call(call, ua, uri, "out", "Cancel", dtmfWatcher)) callButton.tag = "Cancel" callButton.setImageResource(R.drawable.hangup) holdButton.visibility = View.INVISIBLE @@ -928,9 +938,15 @@ class MainActivity : AppCompatActivity() { private fun setSecurityButtonTag(button: ImageButton, security: Int) { when (security) { - R.drawable.box_red -> { button.tag = "red" } - R.drawable.box_yellow -> { button.tag = "yellow" } - R.drawable.box_green -> { button.tag = "green" } + R.drawable.box_red -> { + button.tag = "red" + } + R.drawable.box_yellow -> { + button.tag = "yellow" + } + R.drawable.box_green -> { + button.tag = "green" + } } } @@ -938,10 +954,31 @@ class MainActivity : AppCompatActivity() { Log.d("Baresip", "Holding call with ${call.peerURI} at ${button.id}") call_hold(call.callp) call.hold = true - button.tag= "Resume" + button.tag = "Resume" button.setImageResource(R.drawable.resume) } + private fun startRinging() { + am.mode = AudioManager.MODE_RINGTONE + rt.play() + rtTimer = Timer() + rtTimer!!.scheduleAtFixedRate(object : TimerTask() { + override fun run() { + if (!rt.isPlaying()) { + rt.play() + } + } + }, 1000 * 1, 1000 * 1) + } + + private fun stopRinging() { + if (rtTimer != null) { + rtTimer!!.cancel() + rtTimer = null + if (rt.isPlaying) rt.stop() + } + } + external fun uag_current(): String external fun uag_current_set(uap: String) external fun ua_connect(uap: String, peer_uri: String): String @@ -961,6 +998,8 @@ class MainActivity : AppCompatActivity() { internal var calls = ArrayList() var filesPath = "" var visible = true + var answerCall = "" + var rejectCall = "" const val ACCOUNTS_CODE = 1 const val CONTACTS_CODE = 2 diff --git a/app/src/main/res/layout/call_notification.xml b/app/src/main/res/layout/call_notification.xml index c0f9f184..513ae8aa 100644 --- a/app/src/main/res/layout/call_notification.xml +++ b/app/src/main/res/layout/call_notification.xml @@ -41,7 +41,6 @@ android:textSize="15dp" style = "@android:style/TextAppearance.Medium" /> -