Esempio n. 1
0
 @Override
 public void onReceive(Context context, Intent intent) {
   if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
     LogHelper.d(TAG, "Headphones disconnected.");
     if (isPlaying()) {
       Intent i = new Intent(context, MusicService.class);
       i.setAction(MusicService.ACTION_CMD);
       i.putExtra(MusicService.CMD_NAME, MusicService.CMD_PAUSE);
       mService.startService(i);
     }
   }
 }
 @Override
 public void onReceive(Context context, Intent intent) {
   /* If the device is unplugged, this will immediately detect that action,
    * and close the device.
    */
   if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
     mbAudioConnected = false;
     if (mMTSCRA.getDeviceType() == MagTekSCRA.DEVICE_TYPE_AUDIO) {
       if (mMTSCRA.isDeviceConnected()) {
         mMTSCRA.closeDevice();
       }
     }
   }
 }
        @Override
        public void onReceive(Context context, Intent intent) {
          if (intent == null) return;

          String action = intent.getAction();
          if (TextUtils.isEmpty(action)) return;

          if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(action)) {
            onReceiveActionAudioBecomingNoisy(intent);
          } else if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
            onReceiveActionHeadsetPlug(intent);
          } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)
              || BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            onReceiveActionAclConnection(intent);
          }
        }
Esempio n. 4
0
  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent == null) {
      return;
    }

    String action = intent.getAction();

    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(action)) {
      handleAudioChangeEvent();
    }

    if (Intent.ACTION_MEDIA_BUTTON.equals(action)) {
      handleAudioControlEvent(intent);
    }
  }
  @Override
  public void onReceive(Context context, Intent intent) {

    String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
      Intent i = new Intent(context, MusicPlaybackService.class);
      i.setAction(SERVICECMD);
      i.putExtra(CMDNAME, CMDPAUSE);
      context.startService(i);
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
      KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

      if (event == null) return;

      int keycode = event.getKeyCode();
      int action = event.getAction();
      long eventtime = event.getEventTime();

      switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
          sendMediaCommand(context, CMDSTOP);
          break;
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
          sendMediaCommand(context, CMDTOGGLEPAUSE);
          break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
          sendMediaCommand(context, CMDNEXT);
          break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
          sendMediaCommand(context, CMDPREVIOUS);
          break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
          processHeadsetHookEvent(context, action, eventtime);
          break;
      }
    }
  }
package com.irof.util;
  @Override
  public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
      Intent i = new Intent(context, MediaPlaybackService.class);
      i.setAction(MediaPlaybackService.SERVICECMD);
      i.putExtra(MediaPlaybackService.CMDNAME, MediaPlaybackService.CMDPAUSE);
      context.startService(i);
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
      KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

      if (event == null) {
        return;
      }

      int keycode = event.getKeyCode();
      int action = event.getAction();
      long eventtime = event.getEventTime();

      // single quick press: pause/resume.
      // double press: next track
      // long press: start auto-shuffle mode.

      String command = null;
      switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
          command = MediaPlaybackService.CMDSTOP;
          break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
          command = MediaPlaybackService.CMDTOGGLEPAUSE;
          break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
          command = MediaPlaybackService.CMDNEXT;
          break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
          command = MediaPlaybackService.CMDPREVIOUS;
          break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
          command = MediaPlaybackService.CMDPAUSE;
          break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
          command = MediaPlaybackService.CMDPLAY;
          break;
      }

      if (command != null) {
        if (action == KeyEvent.ACTION_DOWN) {
          if (mDown) {
            if ((MediaPlaybackService.CMDTOGGLEPAUSE.equals(command)
                    || MediaPlaybackService.CMDPLAY.equals(command))
                && mLastClickTime != 0
                && eventtime - mLastClickTime > LONG_PRESS_DELAY) {
              mHandler.sendMessage(mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context));
            }
          } else if (event.getRepeatCount() == 0) {
            // only consider the first event in a sequence, not the repeat events,
            // so that we don't trigger in cases where the first event went to
            // a different app (e.g. when the user ends a phone call by
            // long pressing the headset button)

            // The service may or may not be running, but we need to send it
            // a command.
            Intent i = new Intent(context, MediaPlaybackService.class);
            i.setAction(MediaPlaybackService.SERVICECMD);
            if (keycode == KeyEvent.KEYCODE_HEADSETHOOK && eventtime - mLastClickTime < 300) {
              i.putExtra(MediaPlaybackService.CMDNAME, MediaPlaybackService.CMDNEXT);
              context.startService(i);
              mLastClickTime = 0;
            } else {
              i.putExtra(MediaPlaybackService.CMDNAME, command);
              context.startService(i);
              mLastClickTime = eventtime;
            }

            mLaunched = false;
            mDown = true;
          }
        } else {
          mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
          mDown = false;
        }
        if (isOrderedBroadcast()) {
          abortBroadcast();
        }
      }
    }
  }
  @Override
  public void onReceive(final Context context, final Intent intent) {
    if (DEBUG) Log.v(TAG, "Received intent: " + intent);
    final String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
      startService(context, MusicService.CMDPAUSE);
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
      final KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
      if (event == null) {
        return;
      }

      final int keycode = event.getKeyCode();
      final int action = event.getAction();
      final long eventtime = event.getEventTime();

      String command = null;
      switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
          command = MusicService.CMDSTOP;
          break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
          command = MusicService.CMDTOGGLEPAUSE;
          break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
          command = MusicService.CMDNEXT;
          break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
          command = MusicService.CMDPREVIOUS;
          break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
          command = MusicService.CMDPAUSE;
          break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
          command = MusicService.CMDPLAY;
          break;
      }
      if (command != null) {
        if (action == KeyEvent.ACTION_DOWN) {
          if (mDown) {
            if (MusicService.CMDTOGGLEPAUSE.equals(command)
                || MusicService.CMDPLAY.equals(command)) {
              if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) {
                acquireWakeLockAndSendMessage(
                    context, mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0);
              }
            }
          } else if (event.getRepeatCount() == 0) {

            if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
              if (eventtime - mLastClickTime >= DOUBLE_CLICK) {
                mClickCounter = 0;
              }

              mClickCounter++;
              if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter);
              mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

              Message msg =
                  mHandler.obtainMessage(
                      MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context);

              long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
              if (mClickCounter >= 3) {
                mClickCounter = 0;
              }
              mLastClickTime = eventtime;
              acquireWakeLockAndSendMessage(context, msg, delay);
            } else {
              startService(context, command);
            }
            mLaunched = false;
            mDown = true;
          }
        } else {
          mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
          mDown = false;
        }
        if (isOrderedBroadcast()) {
          abortBroadcast();
        }
        releaseWakeLockIfHandlerIdle();
      }
    }
  }