Friday, October 5, 2012

Android Call Blocking

Android Manifest
---------------------------------
<receiver android:name="com.kites.profile.util.PhoneCallReceiver">
            <intent-filter android:priority="100">
                <action android:name="android.intent.action.PHONE_STATE"/>
             </intent-filter>
        </receiver>
 <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

------------------------------------------------------------------------------------------
PhoneCallReceiver.java
--------------------------------
package com.kites.profile.util;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;

import com.android.internal.telephony.ITelephony;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsManager;
import android.util.Log;

public class PhoneCallReceiver extends BroadcastReceiver {
    boolean active = true;
    String replymsg = "";


    Context context = null;
    private static final String TAG = "Phone call";
    private ITelephony telephonyService;
    TelephonyManager telephony = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "Receving....");
        telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
        telephony.listen(customPhoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);

    }

    public void endCall() {
        try {

            Class c = Class.forName(telephony.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            telephonyService = (ITelephony) m.invoke(telephony);
           
           telephonyService.silenceRinger(); //comment this for android 2.3 and above

            telephonyService.endCall();
            // s
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class CustomPhoneStateListener extends PhoneStateListener {

        private static final String TAG = "CustomPhoneStateListener";

        public void onCallStateChanged(int state, String incomingNumber) {

            Log.v(TAG, "WE ARE Receiving!");
            Log.v(TAG, incomingNumber);
            Log.v(TAG, incomingNumber);            if (active) {
                endCall();
                sendSMS(incomingNumber, replymsg);
            }

            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d(TAG, "RINGING");
                break;
            }
        }

        public void sendSMS(String no, String msg) {
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(no, null, msg, null, null);
        }
    }
}
--------------------------------------------------------------------------------------------
ITelephony.aidl
---------------------------
package com.android.internal.telephony;

  interface ITelephony {

  
    boolean endCall();

 
    void answerRingingCall();

  
    void silenceRinger();

  }