I have implemented a CallService which I have in the manifest like this:
<service
android:name=".dialer.CallService"
android:permission="android.permission.BIND_INCALL_SERVICE">
<meta-data
android:name="android.telecom.IN_CALL_SERVICE_UI"
android:value="true"/>
<intent-filter>
<action android:name="android.telecom.InCallService"/>
</intent-filter>
</service>
And this is it:
@TargetApi(Build.VERSION_CODES.M)
class CallService : InCallService() {
companion object {
private const val LOG_TAG = "CallService"
}
override fun onCallAdded(call: Call) {
super.onCallAdded(call)
App.GSMCALL = true;
Log.i(LOG_TAG, "onCallAdded: $call")
Log.i(LOG_TAG, "onCallAdded: ${call.details.handle.schemeSpecificPart}")
call.registerCallback(callCallback)
val intent = Intent(this, IncomingCallActivity::class.java)
if (call.details.handle.schemeSpecificPart.contains("<INSERT PHONE NR HERE>")) { // something similar will be done, if I don't pass new_task it will go through default caller
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
startActivity(intent)
CallManager.updateCall(call)
}
override fun onCallRemoved(call: Call) {
super.onCallRemoved(call)
App.GSMCALL = false;
Log.i(LOG_TAG, "onCallRemoved: $call")
call.unregisterCallback(callCallback)
CallManager.updateCall(null)
}
private val callCallback = object : Call.Callback() {
override fun onStateChanged(call: Call, state: Int) {
Log.i(LOG_TAG, "Call.Callback onStateChanged: $call, state: $state")
CallManager.updateCall(call)
}
}
}
Now this is what I want. If my phone call is then I want it to go through my Call Activity. But if it's a different phone number, I want it to fall back to the phones dialler. Is this possible? I've seen that If I do not add the FLAG_ACTIVITY_NEW_TASK it will crash which makes the phones main Dialler to appear. But this just works once, And I wouldn't use such a hack to make it work. So any other ideas?
EDIT:
I am using a Google Pixel with Android Pie
I tried to implement the logic that @marmor suggested but still doesn't work.
This is my activity on create:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
wakeUpDeviceWhenWindowShown()
setTheme(R.style.AppTheme)
var params = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
if (VERSION.SDK_INT >= VERSION_CODES.O) WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY else WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or FLAG_SHOW_WHEN_LOCKED or FLAG_DISMISS_KEYGUARD, ActivityInfo.COLOR_MODE_DEFAULT)
setContentView(R.layout.activity_call)
val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
wm.addView(window.decorView, params)
}
I added in AndroidManifest: <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
And also have the code for:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (!Settings.canDrawOverlays(this))
{
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0);
}
}
This is the error I get:
2019-07-23 13:13:06.600 18418-18418/com.xelion.android.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xelion.android.debug, PID: 18418
java.lang.IllegalStateException: View DecorView@53d8f92[IncomingCallActivity] has already been added to the window manager.
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:328)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3906)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I also tried just doing:
window.setType(if (VERSION.SDK_INT >= VERSION_CODES.O) WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY else WindowManager.LayoutParams.TYPE_SYSTEM_ERROR)
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or FLAG_SHOW_WHEN_LOCKED or FLAG_DISMISS_KEYGUARD)
Without doing wm.addView(window.decorView, params)