Skip to content

Commit

Permalink
Merge pull request #232 from KryptKode/feat/back-press
Browse files Browse the repository at this point in the history
Feat/ handle back press for calls
  • Loading branch information
tibbi authored Sep 25, 2021
2 parents 7a28c71 + 233fbca commit 4757723
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 155 deletions.
2 changes: 2 additions & 0 deletions app/src/main/kotlin/com/simplemobiletools/dialer/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.simplemobiletools.dialer

import android.app.Application
import com.simplemobiletools.commons.extensions.checkUseEnglish
import com.simplemobiletools.dialer.helpers.CallDurationHelper

class App : Application() {
val callDurationHelper by lazy { CallDurationHelper() }
override fun onCreate() {
super.onCreate()
checkUseEnglish()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
package com.simplemobiletools.dialer.activities

import android.annotation.SuppressLint
import android.app.*
import android.app.KeyguardManager
import android.content.Context
import android.content.Intent
import android.graphics.*
import android.graphics.Bitmap
import android.media.AudioManager
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.provider.MediaStore
import android.telecom.Call
import android.telecom.CallAudioState
import android.util.Size
import android.view.WindowManager
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.helpers.MINUTE_SECONDS
import com.simplemobiletools.commons.helpers.isOreoMr1Plus
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.dialer.App
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.extensions.addCharacter
import com.simplemobiletools.dialer.extensions.audioManager
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.extensions.getHandleToUse
import com.simplemobiletools.dialer.helpers.ACCEPT_CALL
import com.simplemobiletools.dialer.helpers.CallContactAvatarHelper
import com.simplemobiletools.dialer.helpers.CallManager
import com.simplemobiletools.dialer.helpers.DECLINE_CALL
import com.simplemobiletools.dialer.models.CallContact
import com.simplemobiletools.dialer.receivers.CallActionReceiver
import kotlinx.android.synthetic.main.activity_call.*
import kotlinx.android.synthetic.main.dialpad.*
import java.util.*

class CallActivity : SimpleActivity() {
private val CALL_NOTIFICATION_ID = 1
companion object {
fun getStartIntent(context: Context): Intent {
val openAppIntent = Intent(context, CallActivity::class.java)
openAppIntent.flags = Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT or Intent.FLAG_ACTIVITY_NEW_TASK
return openAppIntent
}
}

private var isSpeakerOn = false
private var isMicrophoneOn = true
private var isCallEnded = false
private var callDuration = 0
private var callContact: CallContact? = null
private var callContactAvatar: Bitmap? = null
private var proximityWakeLock: PowerManager.WakeLock? = null
private var callTimer = Timer()
private var callDuration = 0
private val callContactAvatarHelper by lazy { CallContactAvatarHelper(this) }
private val callDurationHelper by lazy { (application as App).callDurationHelper }

override fun onCreate(savedInstanceState: Bundle?) {
supportActionBar?.hide()
Expand All @@ -58,10 +58,9 @@ class CallActivity : SimpleActivity() {

CallManager.getCallContact(applicationContext) { contact ->
callContact = contact
callContactAvatar = getCallContactAvatar()
val avatar = callContactAvatarHelper.getCallContactAvatar(contact)
runOnUiThread {
setupNotification()
updateOtherPersonsInfo()
updateOtherPersonsInfo(avatar)
checkCalledSIMCard()
}
}
Expand All @@ -74,14 +73,10 @@ class CallActivity : SimpleActivity() {

override fun onDestroy() {
super.onDestroy()
notificationManager.cancel(CALL_NOTIFICATION_ID)
CallManager.unregisterCallback(callCallback)
callTimer.cancel()
if (proximityWakeLock?.isHeld == true) {
proximityWakeLock!!.release()
}

endCall()
}

override fun onBackPressed() {
Expand All @@ -92,7 +87,8 @@ class CallActivity : SimpleActivity() {
super.onBackPressed()
}

if (CallManager.getState() == Call.STATE_DIALING) {
val callState = CallManager.getState()
if (callState == Call.STATE_CONNECTING || callState == Call.STATE_DIALING) {
endCall()
}
}
Expand Down Expand Up @@ -180,7 +176,7 @@ class CallActivity : SimpleActivity() {
}
}

private fun updateOtherPersonsInfo() {
private fun updateOtherPersonsInfo(avatar: Bitmap?) {
if (callContact == null) {
return
}
Expand All @@ -192,8 +188,8 @@ class CallActivity : SimpleActivity() {
caller_number_label.beGone()
}

if (callContactAvatar != null) {
caller_avatar.setImageBitmap(callContactAvatar)
if (avatar != null) {
caller_avatar.setImageBitmap(avatar)
}
}

Expand Down Expand Up @@ -223,10 +219,6 @@ class CallActivity : SimpleActivity() {
Call.STATE_SELECT_PHONE_ACCOUNT -> showPhoneAccountPicker()
}

if (state == Call.STATE_DISCONNECTED || state == Call.STATE_DISCONNECTING) {
callTimer.cancel()
}

val statusTextId = when (state) {
Call.STATE_RINGING -> R.string.is_calling
Call.STATE_DIALING -> R.string.dialing
Expand All @@ -236,8 +228,6 @@ class CallActivity : SimpleActivity() {
if (statusTextId != 0) {
call_status_label.text = getString(statusTextId)
}

setupNotification()
}

private fun acceptCall() {
Expand All @@ -258,9 +248,13 @@ class CallActivity : SimpleActivity() {
initProximitySensor()
incoming_call_holder.beGone()
ongoing_call_holder.beVisible()
try {
callTimer.scheduleAtFixedRate(getCallTimerUpdateTask(), 1000, 1000)
} catch (ignored: Exception) {
callDurationHelper.onDurationChange {
callDuration = it
runOnUiThread {
if (!isCallEnded) {
call_status_label.text = callDuration.getFormattedDuration()
}
}
}
}

Expand Down Expand Up @@ -302,17 +296,6 @@ class CallActivity : SimpleActivity() {
}
}

private fun getCallTimerUpdateTask() = object : TimerTask() {
override fun run() {
callDuration++
runOnUiThread {
if (!isCallEnded) {
call_status_label.text = callDuration.getFormattedDuration()
}
}
}
}

private val callCallback = object : Call.Callback() {
override fun onStateChanged(call: Call, state: Int) {
super.onStateChanged(call, state)
Expand Down Expand Up @@ -348,106 +331,4 @@ class CallActivity : SimpleActivity() {
proximityWakeLock!!.acquire(10 * MINUTE_SECONDS * 1000L)
}
}

@SuppressLint("NewApi")
private fun setupNotification() {
val callState = CallManager.getState()
val channelId = "simple_dialer_call"
if (isOreoPlus()) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
val name = "call_notification_channel"

NotificationChannel(channelId, name, importance).apply {
setSound(null, null)
notificationManager.createNotificationChannel(this)
}
}

val openAppIntent = Intent(this, CallActivity::class.java)
openAppIntent.flags = Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
val openAppPendingIntent = PendingIntent.getActivity(this, 0, openAppIntent, 0)

val acceptCallIntent = Intent(this, CallActionReceiver::class.java)
acceptCallIntent.action = ACCEPT_CALL
val acceptPendingIntent = PendingIntent.getBroadcast(this, 0, acceptCallIntent, PendingIntent.FLAG_CANCEL_CURRENT)

val declineCallIntent = Intent(this, CallActionReceiver::class.java)
declineCallIntent.action = DECLINE_CALL
val declinePendingIntent = PendingIntent.getBroadcast(this, 1, declineCallIntent, PendingIntent.FLAG_CANCEL_CURRENT)

val callerName = if (callContact != null && callContact!!.name.isNotEmpty()) callContact!!.name else getString(R.string.unknown_caller)
val contentTextId = when (callState) {
Call.STATE_RINGING -> R.string.is_calling
Call.STATE_DIALING -> R.string.dialing
Call.STATE_DISCONNECTED -> R.string.call_ended
Call.STATE_DISCONNECTING -> R.string.call_ending
else -> R.string.ongoing_call
}

val collapsedView = RemoteViews(packageName, R.layout.call_notification).apply {
setText(R.id.notification_caller_name, callerName)
setText(R.id.notification_call_status, getString(contentTextId))
setVisibleIf(R.id.notification_accept_call, callState == Call.STATE_RINGING)

setOnClickPendingIntent(R.id.notification_decline_call, declinePendingIntent)
setOnClickPendingIntent(R.id.notification_accept_call, acceptPendingIntent)

if (callContactAvatar != null) {
setImageViewBitmap(R.id.notification_thumbnail, getCircularBitmap(callContactAvatar!!))
}
}

val builder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_phone_vector)
.setContentIntent(openAppPendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_CALL)
.setCustomContentView(collapsedView)
.setOngoing(true)
.setSound(null)
.setUsesChronometer(callState == Call.STATE_ACTIVE)
.setChannelId(channelId)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())

val notification = builder.build()
notificationManager.notify(CALL_NOTIFICATION_ID, notification)
}

@SuppressLint("NewApi")
private fun getCallContactAvatar(): Bitmap? {
var bitmap: Bitmap? = null
if (callContact?.photoUri?.isNotEmpty() == true) {
val photoUri = Uri.parse(callContact!!.photoUri)
try {
bitmap = if (isQPlus()) {
val tmbSize = resources.getDimension(R.dimen.list_avatar_size).toInt()
contentResolver.loadThumbnail(photoUri, Size(tmbSize, tmbSize), null)
} else {
MediaStore.Images.Media.getBitmap(contentResolver, photoUri)

}

bitmap = getCircularBitmap(bitmap!!)
} catch (ignored: Exception) {
return null
}
}

return bitmap
}

private fun getCircularBitmap(bitmap: Bitmap): Bitmap {
val output = Bitmap.createBitmap(bitmap.width, bitmap.width, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)
val paint = Paint()
val rect = Rect(0, 0, bitmap.width, bitmap.height)
val radius = bitmap.width / 2.toFloat()

paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
canvas.drawCircle(radius, radius, radius, paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
return output
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.simplemobiletools.dialer.helpers

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.net.Uri
import android.provider.MediaStore
import android.util.Size
import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.models.CallContact

class CallContactAvatarHelper(private val context: Context) {
@SuppressLint("NewApi")
fun getCallContactAvatar(callContact: CallContact?): Bitmap? {
var bitmap: Bitmap? = null
if (callContact?.photoUri?.isNotEmpty() == true) {
val photoUri = Uri.parse(callContact.photoUri)
try {
val contentResolver = context.contentResolver
bitmap = if (isQPlus()) {
val tmbSize = context.resources.getDimension(R.dimen.list_avatar_size).toInt()
contentResolver.loadThumbnail(photoUri, Size(tmbSize, tmbSize), null)
} else {
MediaStore.Images.Media.getBitmap(contentResolver, photoUri)
}
bitmap = getCircularBitmap(bitmap!!)
} catch (ignored: Exception) {
return null
}
}
return bitmap
}

fun getCircularBitmap(bitmap: Bitmap): Bitmap {
val output = Bitmap.createBitmap(bitmap.width, bitmap.width, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)
val paint = Paint()
val rect = Rect(0, 0, bitmap.width, bitmap.height)
val radius = bitmap.width / 2.toFloat()

paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
canvas.drawCircle(radius, radius, radius, paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
return output
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.simplemobiletools.dialer.helpers

import java.util.Timer
import java.util.TimerTask

class CallDurationHelper {
private var callTimer: Timer? = null
private var callDuration = 0
private var callback: ((durationSecs: Int) -> Unit)? = null

fun onDurationChange(callback: (durationSecs: Int) -> Unit) {
this.callback = callback
}

fun start() {
try {
callDuration = 0
callTimer = Timer()
callTimer?.scheduleAtFixedRate(getTimerUpdateTask(), 1000, 1000)
} catch (ignored: Exception) {
}
}

fun cancel() {
callTimer?.cancel()
}

private fun getTimerUpdateTask() = object : TimerTask() {
override fun run() {
callDuration++
callback?.invoke(callDuration)
}
}
}
Loading

0 comments on commit 4757723

Please sign in to comment.