3

I am working on an android that will allow the user to add numbers to a blacklist. When an incoming number matches a number in the blacklist then the call should be rejected, even if the phone does ring briefly and then disconnects the call.

Everything I've found including on SO, says it can't be done without creating AIDL in com.android.internal.telephony which I've created but I can't add the modify phone state permission as it says it needs to be a system app.

I am targetting ICS upwards and I have seen other apps block calls in ICS and up so how is this done. I've also tried adding the modify phone state permission to the manifest file and it displays an error saying that it is only available for system apps so how do I get around this issue. I don't want the app to have to be rooted.

Thanks for any help you can provide

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • Maybe this thread can help you http://stackoverflow.com/questions/7121508/android-taking-complete-control-of-phone-is-it-possible-how/7121586#7121586 – Matthew Fisher Feb 06 '13 at 22:32
  • @MatthewFisher Unfortunately not, done everything in that question but I can't add the permission as it errors saying that it needs to be a system app – Boardy Feb 06 '13 at 22:45
  • Your current approach is using reflection to grab an interface. while it does work.. I think this is a little too special to expose to apps. There is probably a reason why its not in the SDK and why you have to use reflection to grab it. But we strive to find solutions to our problems and requirements :)! – JoxTraex Feb 06 '13 at 23:02
  • I know this is not the best solution and personally I feel it is a bit 'hacky' of a technique to make it work but unfortunately seems to be the only way. Don't entirely understand why google would have done this. Based on all the blogs and posts it seems to be quite popular functionality. Not entirely happy with this solution to be honest, worried it may eventually break if google decide to change/block this method – Boardy Feb 06 '13 at 23:08
  • There is no other solution. Simply put, it's kind of a dangerous functionality to have freely available to app to have the ability to cancel incoming calls and answer calls without it being mediated by the SDK does seem a little wierd, also a little dangerous. – JoxTraex Feb 06 '13 at 23:11
  • Wouldn't have thought it to be that dangerous though, as long as it requires the permission to do the job, even if Google create a separate permission for this functionality, the user can decide whether they want to trust the app or not – Boardy Feb 06 '13 at 23:18

1 Answers1

4

I've found the answer by a bit of luck.

Instead of adding permission MODIFY_PHONE_STATE add permission CALL_PHONE

Create a new package called com.android.internal.telephony

Inside this package create a file called ITelephony.aidl and add the following content

package com.android.internal.telephony;

interface ITelephony {      

    boolean endCall();     

    void answerRingingCall();      

    void silenceRinger(); 

}

Use the below code in order to block the call

try
        {
            TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            Class c = Class.forName(tm.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);

            com.android.internal.telephony.ITelephony  telephonyService = (ITelephony)m.invoke(tm);
            //telephonyService.silenceRinger();
            telephonyService.endCall();
        }
        catch (Exception e)
        {
            Log.d("BLOCK CALL", e.toString());
            Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
        }

Hope this helps others, its not too easy to find. I don't understand why Google removed the ability to do this without mucking about like this though.

Boardy
  • 35,417
  • 104
  • 256
  • 447