Android Open Source - HeadphoneController Media Button Receiver






From Project

Back to project page HeadphoneController.

License

The source code is released under:

GNU General Public License

If you think the Android project HeadphoneController 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 ca.mbabic.headphonecontroller.services;
//from  w  w w . j  av a2  s . c  om
import ca.mbabic.headphonecontroller.statemachine.HCStateMachine;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

/**
 * Receives presses of the media button and invokes appropriate HCStateMachine
 * methods.
 * @author Marko Babic
 */
public class MediaButtonReceiver extends BroadcastReceiver {

  /**
   * Logging tag.
   */
  private final String TAG = ".services.MediaButtonReceiver";

  /**
   * Reference to state machine keeping track of time elapsed between key
   * presses.
   */
  private static HCStateMachine stateMachine = HCStateMachine.getInstance();

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

    KeyEvent event;
    int action;
    
    // Ensure that we have received the intent action of the type
    // we expected.
    if (!Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
      Log.e(TAG,
          "Received intent action type = " + intent.getAction());
      return;
    }

    event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

    if (event == null) {
      Log.e(TAG, "Received intent with null key event!");
      return;
    }

    action = event.getAction();
    
    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_HEADSETHOOK:
      
      if (action == KeyEvent.ACTION_UP) {
        
        stateMachine.keyUp();
        
      } else if (action == KeyEvent.ACTION_DOWN) {
        
        Log.d(TAG, "KEYDOWN ACTION RECEIVED.");
        
      }

    }

    // Stop other apps from seeing the event and interfering with our
    // actions.
    setResultData(null);
    abortBroadcast();
  }

}




Java Source Code List

ca.mbabic.headphonecontroller.ConfigurationFragment.java
ca.mbabic.headphonecontroller.HCApplication.java
ca.mbabic.headphonecontroller.HomeActivity.java
ca.mbabic.headphonecontroller.SelectCommandActivity.java
ca.mbabic.headphonecontroller.commands.CommandExecutor.java
ca.mbabic.headphonecontroller.commands.HCCommandContext.java
ca.mbabic.headphonecontroller.commands.HCCommandFactory.java
ca.mbabic.headphonecontroller.commands.HCCommand.java
ca.mbabic.headphonecontroller.commands.MuteMusicCommand.java
ca.mbabic.headphonecontroller.commands.NoOpCommand.java
ca.mbabic.headphonecontroller.commands.PlayPauseCommand.java
ca.mbabic.headphonecontroller.commands.PreviousCommand.java
ca.mbabic.headphonecontroller.commands.SkipCommand.java
ca.mbabic.headphonecontroller.configuration.HCConfigAdapter.java
ca.mbabic.headphonecontroller.configuration.HCConfigConstants.java
ca.mbabic.headphonecontroller.db.DbHelper.java
ca.mbabic.headphonecontroller.db.HCCallStateTable.java
ca.mbabic.headphonecontroller.db.HCCommandCallStateTable.java
ca.mbabic.headphonecontroller.db.HCCommandTable.java
ca.mbabic.headphonecontroller.db.HCDbAdapter.java
ca.mbabic.headphonecontroller.db.HCDbHelper.java
ca.mbabic.headphonecontroller.db.HCDbTable.java
ca.mbabic.headphonecontroller.db.HCInputSequenceCommandsTable.java
ca.mbabic.headphonecontroller.db.HCInputSequenceTable.java
ca.mbabic.headphonecontroller.models.HCCmd.java
ca.mbabic.headphonecontroller.models.HCInputSequence.java
ca.mbabic.headphonecontroller.services.MediaButtonListenerService.java
ca.mbabic.headphonecontroller.services.MediaButtonReceiver.java
ca.mbabic.headphonecontroller.services.MediaStateChangeReceiver.java
ca.mbabic.headphonecontroller.statemachine.FourPressState.java
ca.mbabic.headphonecontroller.statemachine.HCStateMachine.java
ca.mbabic.headphonecontroller.statemachine.HCState.java
ca.mbabic.headphonecontroller.statemachine.InactiveState.java
ca.mbabic.headphonecontroller.statemachine.OnePressState.java
ca.mbabic.headphonecontroller.statemachine.ThreePressState.java
ca.mbabic.headphonecontroller.statemachine.TwoPressState.java
ca.mbabic.headphonecontroller.views.CommandAdapter.java