Last active
May 1, 2020 17:28
-
-
Save MotasemF/8d8a5ebc89bd1462d8adaab339282bb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CallReceiver : PhonecallReceiver() { | |
override fun onIncomingCallStarted(ctx: Context, number: String?, start: Date) { | |
} | |
override fun onOutgoingCallStarted(ctx: Context, number: String?, start: Date) { | |
} | |
override fun onIncomingCallEnded(ctx: Context, number: String?, start: Date?, end: Date) { | |
} | |
override fun onOutgoingCallEnded(ctx: Context, number: String?, start: Date?, end: Date) { | |
} | |
override fun onMissedCall(ctx: Context, number: String?, start: Date?) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class PhonecallReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
if (intent.action == "android.intent.action.NEW_OUTGOING_CALL") { | |
savedNumber = intent.extras!!.getString("android.intent.extra.PHONE_NUMBER") | |
} else { | |
val stateStr = try { | |
intent.extras!!.getString(TelephonyManager.EXTRA_STATE) | |
} catch (e: Exception) { | |
TelephonyManager.CALL_STATE_IDLE | |
} | |
val number = try { | |
intent.extras!!.getString(TelephonyManager.EXTRA_INCOMING_NUMBER) | |
} catch (e: Exception) { | |
"" | |
} | |
var state = 0 | |
if (stateStr == TelephonyManager.EXTRA_STATE_IDLE) { | |
state = TelephonyManager.CALL_STATE_IDLE | |
} else if (stateStr == TelephonyManager.EXTRA_STATE_OFFHOOK) { | |
state = TelephonyManager.CALL_STATE_OFFHOOK | |
} else if (stateStr == TelephonyManager.EXTRA_STATE_RINGING) { | |
state = TelephonyManager.CALL_STATE_RINGING | |
} | |
if (intent.hasExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) && | |
intent.extras!!.getString(TelephonyManager.EXTRA_INCOMING_NUMBER) != null | |
) | |
onCallStateChanged(context, state, number) | |
} | |
} | |
abstract fun onIncomingCallStarted(ctx: Context, number: String?, start: Date) | |
abstract fun onOutgoingCallStarted(ctx: Context, number: String?, start: Date) | |
abstract fun onIncomingCallEnded(ctx: Context, number: String?, start: Date?, end: Date) | |
abstract fun onOutgoingCallEnded(ctx: Context, number: String?, start: Date?, end: Date) | |
abstract fun onMissedCall(ctx: Context, number: String?, start: Date?) | |
open fun onCallStateChanged(context: Context, state: Int, number: String?) { | |
if (lastState == state) { | |
return | |
} | |
when (state) { | |
TelephonyManager.CALL_STATE_RINGING -> { | |
isIncoming = true | |
callStartTime = Date() | |
savedNumber = number | |
onIncomingCallStarted(context, number, callStartTime!!) | |
} | |
TelephonyManager.CALL_STATE_OFFHOOK -> | |
if (lastState != TelephonyManager.CALL_STATE_RINGING) { | |
isIncoming = false | |
callStartTime = Date() | |
onOutgoingCallStarted(context, savedNumber, callStartTime!!) | |
} | |
TelephonyManager.CALL_STATE_IDLE -> | |
if (lastState == TelephonyManager.CALL_STATE_RINGING) { | |
onMissedCall(context, savedNumber, callStartTime) | |
} else if (isIncoming) { | |
onIncomingCallEnded(context, savedNumber, callStartTime, Date()) | |
} else { | |
onOutgoingCallEnded(context, savedNumber, callStartTime, Date()) | |
} | |
} | |
lastState = state | |
} | |
companion object { | |
private var lastState = TelephonyManager.CALL_STATE_IDLE | |
private var callStartTime: Date? = null | |
private var isIncoming: Boolean = false | |
private var savedNumber: String? = | |
null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment