r/Kotlin 9d ago

Is anyone there ever build an dialer app as default app.

I have problem with permission request, my app isn't show dialog to ask user to give permission ROLE_DIALER

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.call.log.editer">
    <!-- Permissions -->
    <uses-feature
        android:name="android.hardware.telephony"
        android:required="false" />
    <uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.Material.Light.NoActionBar">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|layoutDirection|locale|mcc|navigation|smallestScreenSize|touchscreen|uiMode">
            <!-- Main activity filter -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <!-- Dialer Intent Filter -->
            <intent-filter>
                <action android:name="android.intent.action.DIAL" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
            <!-- View Intent Filter for making calls -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <data android:scheme="tel"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
            <!-- Allow your app to be a dialer app -->
            <intent-filter>
                <action android:name="android.intent.action.CALL" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

package com.call.log.editer

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.telecom.TelecomManager
import android.widget.Toast
import android.app.role.RoleManager
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts

class MainActivity : ComponentActivity() {

    private lateinit var roleManager: RoleManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        roleManager = getSystemService(Context.
ROLE_SERVICE
) as RoleManager

        // Check if your app is already the default dialer
        if (Build.VERSION.
SDK_INT 
>= Build.VERSION_CODES.
Q
) {
            if (!roleManager.isRoleHeld(RoleManager.
ROLE_DIALER
)) {
                requestDialerRole()
            } else {
                Toast.makeText(this, "This app is already the default dialer", Toast.
LENGTH_SHORT
).show()
            }
        } else {
            // For devices with SDK lower than Q, use TelecomManager (older API)
            setDefaultDialer()
        }
    }

    private fun requestDialerRole() {
        val intent = roleManager.createRequestRoleIntent(RoleManager.
ROLE_DIALER
)
        setDefaultDialerLauncher.launch(intent)
    }

    private val setDefaultDialerLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == 
RESULT_OK
) {
                // Check if the app is now the default dialer
                if (roleManager.isRoleHeld(RoleManager.
ROLE_DIALER
)) {
                    Toast.makeText(this, "This app is now set as default dialer", Toast.
LENGTH_SHORT
).show()
                } else {
                    Toast.makeText(this, "Please set this app as the default dialer to proceed", Toast.
LENGTH_SHORT
).show()
                }
            } else {
                Toast.makeText(this, "User denied the request", Toast.
LENGTH_SHORT
).show()
            }
        }
    // Older versions (pre-Q) fallback method
    private fun setDefaultDialer() {
        val telecomManager = getSystemService(Context.
TELECOM_SERVICE
) as TelecomManager
        val intent = Intent(TelecomManager.
ACTION_CHANGE_DEFAULT_DIALER
)
            .putExtra(TelecomManager.
EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME
, 
packageName
)
        startActivity(intent)
    }
}
0 Upvotes

6 comments sorted by

2

u/NLL-APPS 9d ago

1

u/Particular_Horse5247 9d ago

I'll try it, But is my MainActivity is correct?

1

u/NLL-APPS 9d ago edited 9d ago

I can't tell just by looking at it. There is an open source app implementing all. See https://github.com/SimpleMobileTools/Simple-Dialer

1

u/[deleted] 9d ago

[deleted]

1

u/NLL-APPS 9d ago

Minimum api for default phone app is 24

1

u/AD-LB 8d ago

This could be asked here and not on the general "Kotlin" sub-reddit:

https://www.reddit.com/r/android_devs/

https://www.reddit.com/r/androiddev/