Android Open Source - info-mailer Incoming Phone State Listener






From Project

Back to project page info-mailer.

License

The source code is released under:

MIT License

If you think the Android project info-mailer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package hu.thewolf.infomailer;
//from   www .j  a va  2 s  .c o  m
import static hu.thewolf.infomailer.CONSTANTS.SEP;
import static hu.thewolf.infomailer.CONSTANTS.DATEFORMAT;

import java.lang.reflect.Method;
import java.util.Date;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.telephony.ITelephony;

public class IncomingPhoneStateListener extends PhoneStateListener {
  
  private Context context;
  
  public IncomingPhoneStateListener(Context context) {
    super();
    this.context = context;    
  }
  
  public void onCallStateChanged(int state, String incomingNumber) {
    if (!StringUtils.isBlank(incomingNumber)) {
      incomingNumber = StringEscapeUtils.escapeHtml4(incomingNumber);
    } else {
      incomingNumber = "Hidden number";
    }
    boolean number = StringUtils.isNumeric(incomingNumber) || (incomingNumber.startsWith("+") && StringUtils.isNumeric(incomingNumber.substring(1)));
    switch (state) {
//    case TelephonyManager.CALL_STATE_IDLE:
//      Log.d("DEBUG", "IDLE");
//      break;
//    case TelephonyManager.CALL_STATE_OFFHOOK:
//      Log.d("DEBUG", "OFFHOOK");
//      break;    
    case TelephonyManager.CALL_STATE_RINGING:
      Log.i("INCOMING CALL", "RINGING");
      Intent eMailIntent = new Intent(context, NotifierService.class);
      eMailIntent.putExtra(NotifierService.SUBJECT, "Incoming call from: " + incomingNumber);
      String now = android.text.format.DateFormat.format(DATEFORMAT, new Date()).toString();
      StringBuilder sb = new StringBuilder();
      sb.append("Incoming call from: ");
      if (number) {
        sb.append("<a href=\"tel:").append(incomingNumber).append("\">")
        .append(incomingNumber).append("</a>");
      } else {
        sb.append(incomingNumber);
      }
      sb.append(SEP).append("at ").append(now);
      eMailIntent.putExtra(NotifierService.BODY, sb.toString());
      context.startService(eMailIntent);
      disconnectPhoneItelephony(context);
      break;
    }
  }
  
  public static void disconnectPhoneItelephony(Context context) {
       ITelephony telephonyService;
        TelephonyManager telephony = (TelephonyManager) 
        context.getSystemService(Context.TELEPHONY_SERVICE);  
        try {
            Class<?> c = Class.forName(telephony.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            telephonyService = (ITelephony) m.invoke(telephony);
            //telephonyService.silenceRinger();
            //telephonyService.answerRingingCall();
            //telephonyService.endcall();
            telephonyService.endCall();
            Log.i("INCOMING CALL", "ENDED");
        } catch (Exception e) {
          Log.e("INCOMING CALL", "FATAL ERROR: could not connect to telephony subsystem", e);
          e.printStackTrace();
        }
   }
}




Java Source Code List

com.android.internal.telephony.ITelephony.java
hu.thewolf.infomailer.CONSTANTS.java
hu.thewolf.infomailer.IncomingPhoneStateListener.java
hu.thewolf.infomailer.Mail.java
hu.thewolf.infomailer.MainActivity.java
hu.thewolf.infomailer.NotifierService.java
hu.thewolf.infomailer.SMSandPhoneReceiver.java