Skip to content

Commit

Permalink
feat: open app to show number of the incoming call
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmadhusudhan committed Jan 11, 2024
1 parent 7c4a43b commit 2d69e86
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId = "com.shreekaram.calllogger"
minSdk = 28
targetSdk = 34
versionCode = 3
versionName = "1.2"
versionCode = 5
versionName = "1.4"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<uses-permission
android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
android:allowBackup="true"
Expand All @@ -43,6 +44,8 @@
</intent-filter>
</activity>

<activity android:name=".IncomingCallActivity" />

<service
android:name=".AppNotificationService"
android:exported="true"
Expand Down
24 changes: 21 additions & 3 deletions app/src/main/java/com/shreekaram/calllogger/CallStateReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,39 @@ class CallStateReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == TelephonyManager.ACTION_PHONE_STATE_CHANGED) {
val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
val incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER)
val incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)

Log.d("CallStateReceiver", state.toString())
Log.d("CallStateReceiver", incomingNumber.toString())
if (incomingNumber != null) {
Log.d("CallStateReceiver", incomingNumber)
}
when (state) {
TelephonyManager.EXTRA_STATE_RINGING -> {
print("This is extra state ringing")
print(intent.extras)

// The phone is ringing
Log.d("CallStateReceiver", "Incoming call from $incomingNumber")

if(incomingNumber != null) {
val i = Intent(
context,
IncomingCallActivity::class.java
)
i.putExtra("incomingNumber",incomingNumber);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
context.startActivity(i)
}
}
TelephonyManager.EXTRA_STATE_OFFHOOK -> {
Log.d("CallStateReceiver", "outgoing call from $incomingNumber")
// A call is dialing, active or on hold
Log.d("CallStateReceiver", "Call answered")
}
TelephonyManager.EXTRA_STATE_IDLE -> {
// The phone is idle
Log.d("CallStateReceiver", "Call ended")
Log.d("CallStateReceiver", "Call ended: "+intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER))

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.shreekaram.calllogger;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

//https://www.quora.com/How-do-I-add-a-popup-window-over-an-Android-native-incoming-call-screen-like-the-Truecaller-Android-app

public class IncomingCallActivity extends Activity {
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
Log.d("IncomingCallActivity: onCreate: ", "flag2");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
Log.d("IncomingCallActivity: onCreate: ", "flagy");
setContentView(R.layout.activity_incoming_call);
Log.d("IncomingCallActivity: onCreate: ", "flagz");
String number = getIntent().getStringExtra("incomingNumber");
@SuppressLint({"MissingInflatedId", "LocalSuppress"})
TextView text = findViewById(R.id.callText);
text.setText("call with " + number);
} catch (Exception e) {
Log.d("Exception", e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
15 changes: 14 additions & 1 deletion app/src/main/java/com/shreekaram/calllogger/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.telephony.PhoneStateListener
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
Expand Down Expand Up @@ -37,6 +40,9 @@ data class CallRecord(
val duration: String
)

val OVERLAY_PERMISSION_REQ_CODE = 1245


class MainActivity : ComponentActivity() {
private val permissions = arrayOf(
Manifest.permission.READ_CALL_LOG,
Expand All @@ -48,6 +54,7 @@ class MainActivity : ComponentActivity() {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE,
Manifest.permission.SYSTEM_ALERT_WINDOW,
)

private fun requestPermissions() {
Expand Down Expand Up @@ -140,7 +147,7 @@ class MainActivity : ComponentActivity() {
val constraints = Constraints.Builder()
.build()

val workRequest = PeriodicWorkRequestBuilder<CallLogWorker>(15, TimeUnit.MINUTES)
val workRequest = PeriodicWorkRequestBuilder<CallLogWorker>(1, TimeUnit.DAYS)
.setConstraints(constraints)
.build()
WorkManager.getInstance(applicationContext).enqueueUniquePeriodicWork(
Expand All @@ -161,6 +168,12 @@ class MainActivity : ComponentActivity() {
scheduleCallLogWorker()
listenToPhoneStateChanges()

if (!Settings.canDrawOverlays(this)) {
val intent =
Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
startActivity(intent)
}

setContent {
CallLoggerTheme {
val navHostController = rememberNavController()
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_incoming_call.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/callText"
android:textSize="30sp"
/>

</LinearLayout>

0 comments on commit 2d69e86

Please sign in to comment.