diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 2cd38ffb..294b0a55 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -29,6 +29,7 @@ + @@ -40,7 +41,10 @@ + + @@ -135,11 +139,14 @@ - - + + + + --> @@ -156,11 +163,19 @@ + + + + + + + + + - { + for (state in channel) { + Timber.d("Phone state changed: $state") + when (state) { + is PhoneState.IncomingCall -> { + // Incoming call + val cookie = state.cookie + val incomingNumber = state.number + val contactName = state.contactName + lastCookie = cookie + phoneControlService.send( + PhoneControl.IncomingCall( + cookie, + incomingNumber ?: "Unknown", + contactName ?: "", + ) + ) + } + is PhoneState.OutgoingCall -> { + // Outgoing call + // Needs implementing when firmware supports it + } + is PhoneState.CallReceived -> { + // Call received + lastCookie?.let { + phoneControlService.send(PhoneControl.Start(it)) + } + } + is PhoneState.CallEnded -> { + // Call ended + lastCookie?.let { + phoneControlService.send(PhoneControl.End(it)) + lastCookie = null + } + } + } + } + } + override fun onReceive(context: Context?, intent: Intent?) { + val injectionComponent = (context!!.applicationContext as CobbleApplication).component + phoneControlService = injectionComponent.createPhoneControlService() + + + when (intent?.action) { + TelephonyManager.ACTION_PHONE_STATE_CHANGED -> { + val state = intent?.getStringExtra(TelephonyManager.EXTRA_STATE) + val number = intent?.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) + val contactName = number?.let { getContactName(context, it) } + + when (state) { + TelephonyManager.EXTRA_STATE_RINGING -> { + phoneStateChangeActor.trySend(PhoneState.IncomingCall(Random.nextUInt(), number, contactName)) + } + TelephonyManager.EXTRA_STATE_OFFHOOK -> { + phoneStateChangeActor.trySend(PhoneState.CallReceived) + } + TelephonyManager.EXTRA_STATE_IDLE -> { + phoneStateChangeActor.trySend(PhoneState.CallEnded) + } + } + } + Intent.ACTION_NEW_OUTGOING_CALL -> { + val number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER) + val contactName = number?.let { getContactName(context, it) } + phoneStateChangeActor.trySend(PhoneState.OutgoingCall(Random.nextUInt(), number, contactName)) + } + } + + } + + private fun getContactName(context: Context, number: String): String? { + val cursor = context.contentResolver.query( + ContactsContract.CommonDataKinds.Phone.CONTENT_URI, + arrayOf( + ContactsContract.Contacts.DISPLAY_NAME, + ContactsContract.CommonDataKinds.Phone.NUMBER + ), + ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", + arrayOf(number), + null + ) + val name = cursor?.use { + if (it.moveToFirst()) { + it.getString(it.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)) + } else { + null + } + } + return name + } +} \ No newline at end of file