I need help to get CNAP(CallerDisplayName) in Android

I using android.telecom.ConnectionService, because i want get in connection class who contains void getCallerDisplayName(), but i create my service extends ConnectionService. And register my service in TelecomManager

TelecomManager telecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);

    ComponentName componentName = new ComponentName("com.ukrainelike.rate", "com.ukrainelike.rate.myservice");
    PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(componentName, "Admin");
    PhoneAccount.Builder builder = new PhoneAccount.Builder(phoneAccountHandle, "Admin");
    builder.setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER);
    PhoneAccount phoneAccount = builder.build();
    telecomManager.registerPhoneAccount(phoneAccount);

And get exception:java.lang.SecurityException: PhoneAccount connection service requires BIND_TELECOM_CONNECTION_SERVICE permission., but i write this permission in manifest file

   `<service
        android:name=".MyService"
      android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
        <intent-filter>
            <action android:name="android.telecom.ConnectionService" />
        </intent-filter>
    </service>`

and <uses-permission android:name="android.permission.BIND_TELECOM_CONNECTION_SERVICE"/> Please help me.

Upvotes: 2

Views: 1311

Answers (2)

Jose Riballo
Jose Riballo

Reputation: 318

I think your ComponentName is failing. It should be something like this:

ComponentName cName = new ComponentName(mContext.getPackageName(), MyConnectionService.class.getName());
mPhoneAccountHandle = new PhoneAccountHandle(cName, "whatever you want");

Furthermore,

<uses-permission android:name="android.permission.BIND_TELECOM_CONNECTION_SERVICE"/> is not necessary.

if you are trying to get caller name of connections not created by your ConnectionService you will get the SecurityException

Upvotes: 5

You Kim
You Kim

Reputation: 2805

Remove permission from your service.

<service
    android:name=".MyService">
    <intent-filter>
        <action android:name="android.telecom.ConnectionService" />
    </intent-filter>
</service>

Platform code

/**
 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
 *
 * @param phoneAccountHandle The phone account to check.
 * @return {@code True} if the phone account has permission.
 */
public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
    List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
    if (resolveInfos.isEmpty()) {
        Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
        return false;
    }
    for (ResolveInfo resolveInfo : resolveInfos) {
        ServiceInfo serviceInfo = resolveInfo.serviceInfo;
        if (serviceInfo == null) {
            return false;
        }

        if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
                !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
                        serviceInfo.permission)) {
            // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
            // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
            // system/signature only.
            return false;
        }
    }
    return true;
}

However, if you want to get CallerDisplayName from TelephonyConnection or SipConnection, you should be a platform developer, 3rd party application can not access those caller information.

Upvotes: 0

Related Questions