2

I have a broadcast receiver, add before blocking the call matching the incoming number with the local database also i have added below permissions in my manifest and also register this broadcast receiver:

@Override
public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();
  if(bundle != null){
      try {
String state = bundle.getString(TelephonyManager.EXTRA_STATE);           
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){      
               if(!isChecking){
                  isChecking = true;
                  String incomingnumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
HashMap<String, String> minelist =  getAllBlockedContacts(context);
boolean isBlockedNumber = false;
                 if(minelist != null){
if(minelist.containsKey(incomingnumber))
                        isBlockedNumber = true;
                    else {
Iterator<String> myVeryOwnIterator = minelist.keySet().iterator();
                          while(myVeryOwnIterator.hasNext()) {
                              String number=(String)myVeryOwnIterator.next();
                              String countrycode=(String)minelist.get(number);
                              String finalNumber = countrycode + number;
if(finalNumber.equals(incomingnumber)){
                                    isBlockedNumber = true;
                                    break;
}
                              else if (("+"+finalNumber).equals("+"+incomingnumber)){
                                    isBlockedNumber = true;
                                    break;
                              }else if(("0"+number).equals(incomingnumber)){
                                    isBlockedNumber = true;
                                    break;                                    
                              }
}
                    }
                 } 
if(isBlockedNumber){                                                
                    endCall(context);

                }
                else
                  isChecking = false;
              }
          }
else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
              isChecking = false;
              Log.v(TAG, "Call State OffHook");
          }
          else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
              isChecking = false;
              Log.v(TAG, "Call State Idle");
                }                
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

private void endCall(Context context){

    try {
        // Get the boring old TelephonyManager.
        TelephonyManager telephonyManager =(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        //get the class telephone
        Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());

        // Get the getITelephony() method     
        Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

        // Ignore that the method is supposed to be private                      
        methodGetITelephony.setAccessible(true);

        // Invoke getITelephony() to get the ITelephony interface
        Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

        // Get the endCall method from ITelephony
        Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());

        // Get the method end call
        Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");

        // Invoke endCall()
        methodEndCall.invoke(telephonyInterface);

    } catch (Exception e) {
        e.printStackTrace();

} 
}
/**
 * Method used to get the list of all blocked list numbers
 * @return
 */
public HashMap<String, String> getAllBlockedContacts(Context context) {
    //ba.textfile("In getAllBlockedContacts");
    ba.saveMessage(context,"In getAllBlockedContacts" );

    HashMap<String, String> contactlist = null;
    DBHelper dbhelper = null;
    SQLiteDatabase db = null;
    Cursor cursor = null;
    try {
        String selectQuery = "SELECT * FROM blocktable";
        dbhelper = new DBHelper(context);
        db = dbhelper.getWritableDatabase();
        cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            contactlist = new HashMap<String, String>();
            //ba.textfile("Cursor Key Length" +cursor.getCount() );
            ba.saveMessage(context,"Cursor Key Length" +cursor.getCount()  );

            while(!cursor.isAfterLast()){
                String countryCode = "+"+cursor.getString(cursor.getColumnIndex("countrycode"));
                String blockedNo =   cursor.getString(cursor.getColumnIndex("blockedno"));
                //Log.e(TAG, "Blocked Numbers "+countryCode+blockedNo);
                contactlist.put(blockedNo, countryCode);
                cursor.moveToNext();


            }
            db.close();
        }
        //db.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally{
        if(cursor != null){
            cursor.close();
            cursor = null;
        }
        if(db != null){
            db.close();
            db = null;
        }
        if(dbhelper != null){
            dbhelper.close();
            dbhelper = null;
        }
    }
    return contactlist;
}

}

Rohit
  • 2,646
  • 6
  • 27
  • 52
User413
  • 43
  • 1
  • 12
  • Your algorithm seems slow as it fetches data upon receiving a request to block a call. Try to cache the blocked numbers instead – Machinarius Sep 02 '14 at 10:45
  • Thankyou, @Machinarius for your speedy response, could you please let me know, some more about caching the sqlite values. – User413 Sep 02 '14 at 10:54
  • I would fire a service upon device startup that reads the blacklist and holds it as variables in memory at all times, setting up the broadcast receiver from there as well. Try to load as little data as possible into memory though, you don't want to hog up the device's RAM – Machinarius Sep 02 '14 at 12:45
  • No problem. Tell me if you have issues with this approach – Machinarius Sep 04 '14 at 13:03

0 Answers0