Android Open Source - PhoneProfiles_Eclipse Phone Call Receiver






From Project

Back to project page PhoneProfiles_Eclipse.

License

The source code is released under:

Apache License

If you think the Android project PhoneProfiles_Eclipse 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 sk.henrichg.phoneprofiles;
//from  w  w w  .j  a v a  2 s .c  o  m
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.util.Date;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public abstract class PhoneCallReceiver extends BroadcastReceiver {

  static TelephonyManager telephony;
    //The receiver will be recreated whenever android feels like it.  We need a static variable to remember data between instantiations
    static PhonecallStartEndDetector listener;
    //String outgoingSavedNumber;
    protected Context savedContext;


    @Override
    public void onReceive(Context context, Intent intent) {
        savedContext = context;
        
        if (onStartReceive())
        {
          if (telephony == null)
            telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
          
          if(listener == null){
              listener = new PhonecallStartEndDetector();
          }

          //Log.e("PhoneCallReceiver.onReceive","action="+intent.getAction());
          
          //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
          if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
              //Log.e("PhoneCallReceiver.onReceive","outgoing call");
              listener.setOutgoingNumber(intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"));
              return;
          }
  
          //The other intent tells us the phone state changed.  Here we set a listener to deal with it
          telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        }
        onEndReceive();
    }
        

    //Derived classes should override these to respond to specific events of interest
    protected abstract boolean onStartReceive();
    protected abstract void onIncomingCallStarted(String number, Date start);
    protected abstract void onOutgoingCallStarted(String number, Date start);
    protected abstract void onOutgoingCallAnswered(String number, Date start);
    protected abstract void onIncomingCallAnswered(String number, Date start);
    protected abstract void onIncomingCallEnded(String number, Date start, Date end); 
    protected abstract void onOutgoingCallEnded(String number, Date start, Date end);
    protected abstract void onMissedCall(String number, Date start);
    protected abstract void onEndReceive();

    //Deals with actual events
    public class PhonecallStartEndDetector extends PhoneStateListener {
        int lastState = TelephonyManager.CALL_STATE_IDLE;
        Date callStartTime;
        boolean inCall;
        boolean isIncoming;
        String savedNumber;  //because the passed incoming is only valid in ringing

        public PhonecallStartEndDetector() {}

        //The outgoing number is only sent via a separate intent, so we need to store it out of band
        public void setOutgoingNumber(String number){
          inCall = false;
            isIncoming = false;
            savedNumber = number;
            callStartTime = new Date();
            onOutgoingCallStarted(savedNumber, callStartTime);
        }

        //Incoming call-  goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
        //Outgoing call-  goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            if(lastState == state){
                //No change, debounce extras
                return;
            }
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                  //Log.e("PhonecallStartEndDetector.onCallStateChanged","ringing");
                  inCall = false;
                    isIncoming = true;
                    callStartTime = new Date();
                    savedNumber = incomingNumber;
                    onIncomingCallStarted(incomingNumber, callStartTime);
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                  //Log.e("PhonecallStartEndDetector.onCallStateChanged","offhook");
                    //Transition of ringing->offhook are pickups of incoming calls.  Nothing donw on them
                    if(lastState != TelephonyManager.CALL_STATE_RINGING){
                      //Log.e("PhonecallStartEndDetector.onCallStateChanged","isincoming=false");
                      inCall = true;
                        isIncoming = false;
                        callStartTime = new Date();
                        onOutgoingCallAnswered(savedNumber, callStartTime);                      
                    }
                    else
                    {
                      //Log.e("PhonecallStartEndDetector.onCallStateChanged","isincoming=true");
                      inCall = true;
                        isIncoming = true;
                        callStartTime = new Date();
                      onIncomingCallAnswered(savedNumber, callStartTime);
                    }
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                  //Log.e("PhonecallStartEndDetector.onCallStateChanged","idle");
                    //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                    if(!inCall){
                        //Ring but no pickup-  a miss
                      //Log.e("PhonecallStartEndDetector.onCallStateChanged","missed call");
                        onMissedCall(savedNumber, callStartTime);
                    }
                    else 
                    {
                      if(isIncoming){
                        //Log.e("PhonecallStartEndDetector.onCallStateChanged","isincoming=true");
                          onIncomingCallEnded(savedNumber, callStartTime, new Date());                        
                      }
                      else{
                        //Log.e("PhonecallStartEndDetector.onCallStateChanged","isincoming=false");
                          onOutgoingCallEnded(savedNumber, callStartTime, new Date());                                                
                      }
                      inCall = false;
                    }
                    break;
            }
            lastState = state;

          telephony.listen(listener, PhoneStateListener.LISTEN_NONE);
            
        }

    }

}




Java Source Code List

sk.henrichg.phoneprofiles.ActivateProfileActivity.java
sk.henrichg.phoneprofiles.ActivateProfileHelper.java
sk.henrichg.phoneprofiles.ActivateProfileListAdapter.java
sk.henrichg.phoneprofiles.ActivateProfileListFragment.java
sk.henrichg.phoneprofiles.ApplicationsCache.java
sk.henrichg.phoneprofiles.ApplicationsPreferenceAdapter.java
sk.henrichg.phoneprofiles.ApplicationsPreferenceDialog.java
sk.henrichg.phoneprofiles.ApplicationsPreference.java
sk.henrichg.phoneprofiles.BackgroundActivateProfileActivity.java
sk.henrichg.phoneprofiles.BitmapManipulator.java
sk.henrichg.phoneprofiles.BootUpReceiver.java
sk.henrichg.phoneprofiles.BrightnessDialogPreference.java
sk.henrichg.phoneprofiles.BrightnessView.java
sk.henrichg.phoneprofiles.DashClockBroadcastReceiver.java
sk.henrichg.phoneprofiles.DataWrapper.java
sk.henrichg.phoneprofiles.DatabaseHandler.java
sk.henrichg.phoneprofiles.EditorProfileListAdapter.java
sk.henrichg.phoneprofiles.EditorProfileListFragment.java
sk.henrichg.phoneprofiles.EditorProfilesActivity.java
sk.henrichg.phoneprofiles.ExecuteRadioProfilePrefsService.java
sk.henrichg.phoneprofiles.ExecuteVolumeProfilePrefsService.java
sk.henrichg.phoneprofiles.FirstStartService.java
sk.henrichg.phoneprofiles.GUIData.java
sk.henrichg.phoneprofiles.GlobalData.java
sk.henrichg.phoneprofiles.IconWidgetProvider.java
sk.henrichg.phoneprofiles.ImageViewPreferenceAdapter.java
sk.henrichg.phoneprofiles.ImageViewPreferenceDialog.java
sk.henrichg.phoneprofiles.ImageViewPreference.java
sk.henrichg.phoneprofiles.KeyguardService.java
sk.henrichg.phoneprofiles.Keyguard.java
sk.henrichg.phoneprofiles.LocaleChangedReceiver.java
sk.henrichg.phoneprofiles.NumberPickerPreference.java
sk.henrichg.phoneprofiles.OneRowWidgetProvider.java
sk.henrichg.phoneprofiles.PackageReplacedReceiver.java
sk.henrichg.phoneprofiles.PhoneCallBroadcastReceiver.java
sk.henrichg.phoneprofiles.PhoneCallReceiver.java
sk.henrichg.phoneprofiles.PhoneProfilesDashClockExtension.java
sk.henrichg.phoneprofiles.PhoneProfilesHelper.java
sk.henrichg.phoneprofiles.PhoneProfilesPreferencesActivity.java
sk.henrichg.phoneprofiles.PhoneProfilesPreferencesFragment.java
sk.henrichg.phoneprofiles.ProfileDurationAlarmBroadcastReceiver.java
sk.henrichg.phoneprofiles.ProfileListWidgetFactory.java
sk.henrichg.phoneprofiles.ProfileListWidgetProvider.java
sk.henrichg.phoneprofiles.ProfileListWidgetService.java
sk.henrichg.phoneprofiles.ProfilePreferenceAdapter.java
sk.henrichg.phoneprofiles.ProfilePreferenceDialog.java
sk.henrichg.phoneprofiles.ProfilePreference.java
sk.henrichg.phoneprofiles.ProfilePreferencesFragmentActivity.java
sk.henrichg.phoneprofiles.ProfilePreferencesFragment.java
sk.henrichg.phoneprofiles.ProfilePreferencesIndicator.java
sk.henrichg.phoneprofiles.Profile.java
sk.henrichg.phoneprofiles.ReceiversService.java
sk.henrichg.phoneprofiles.RefreshGUIBroadcastReceiver.java
sk.henrichg.phoneprofiles.RemoteExportDataActivity.java
sk.henrichg.phoneprofiles.RemoveBrightnessViewBroadcastReceiver.java
sk.henrichg.phoneprofiles.ScreenOnOffBroadcastReceiver.java
sk.henrichg.phoneprofiles.ShortcutCreatorActivity.java
sk.henrichg.phoneprofiles.ShortcutCreatorListFragment.java
sk.henrichg.phoneprofiles.ShortcutProfileListAdapter.java
sk.henrichg.phoneprofiles.UpgradePPHelperActivity.java
sk.henrichg.phoneprofiles.VolumeDialogPreference.java