@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (DEBUG) Log.d(TAG, "onStartCommand startId: " + startId + " flags: " + flags);

    if (intent == null) {
      // Nothing to process, stop.
      if (DEBUG) Log.d(TAG, "START_NOT_STICKY - intent is null.");

      // NOTE: We MUST not call stopSelf() directly, since we need to
      // make sure the wake lock acquired by the Receiver is released.
      DockEventReceiver.finishStartingService(this, startId);
      return START_NOT_STICKY;
    }

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
      handleBtStateChange(intent, startId);
      return START_NOT_STICKY;
    }

    /*
     * This assumes that the intent sender has checked that this is a dock
     * and that the intent is for a disconnect
     */
    final SharedPreferences prefs = getPrefs();
    if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
      BluetoothDevice disconnectedDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
      int retryCount = prefs.getInt(KEY_CONNECT_RETRY_COUNT, 0);
      if (retryCount < MAX_CONNECT_RETRY) {
        prefs.edit().putInt(KEY_CONNECT_RETRY_COUNT, retryCount + 1).apply();
        handleUnexpectedDisconnect(
            disconnectedDevice, mProfileManager.getHeadsetProfile(), startId);
      }
      return START_NOT_STICKY;
    } else if (BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
      BluetoothDevice disconnectedDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

      int retryCount = prefs.getInt(KEY_CONNECT_RETRY_COUNT, 0);
      if (retryCount < MAX_CONNECT_RETRY) {
        prefs.edit().putInt(KEY_CONNECT_RETRY_COUNT, retryCount + 1).apply();
        handleUnexpectedDisconnect(disconnectedDevice, mProfileManager.getA2dpProfile(), startId);
      }
      return START_NOT_STICKY;
    }

    Message msg = parseIntent(intent);
    if (msg == null) {
      // Bad intent
      if (DEBUG) Log.d(TAG, "START_NOT_STICKY - Bad intent.");
      DockEventReceiver.finishStartingService(this, startId);
      return START_NOT_STICKY;
    }

    if (msg.what == MSG_TYPE_DOCKED) {
      prefs.edit().remove(KEY_CONNECT_RETRY_COUNT).apply();
    }

    msg.arg2 = startId;
    processMessage(msg);

    return START_NOT_STICKY;
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent == null) return;

    int state =
        intent.getIntExtra(
            Intent.EXTRA_DOCK_STATE,
            intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, EXTRA_INVALID));
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

    if (DEBUG) {
      Log.d(
          TAG,
          "Action: "
              + intent.getAction()
              + " State:"
              + state
              + " Device: "
              + (device == null ? "null" : device.getAliasName()));
    }

    if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())
        || ACTION_DOCK_SHOW_UI.endsWith(intent.getAction())) {
      if (device == null) {
        if (DEBUG) Log.d(TAG, "Device is missing");
        return;
      }

      switch (state) {
        case Intent.EXTRA_DOCK_STATE_UNDOCKED:
        case Intent.EXTRA_DOCK_STATE_CAR:
        case Intent.EXTRA_DOCK_STATE_DESK:
        case Intent.EXTRA_DOCK_STATE_LE_DESK:
        case Intent.EXTRA_DOCK_STATE_HE_DESK:
          Intent i = new Intent(intent);
          i.setClass(context, DockService.class);
          beginStartingService(context, i);
          break;
        default:
          Log.e(TAG, "Unknown state: " + state);
          break;
      }
    } else if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())
        || BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
      int newState =
          intent.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_CONNECTED);
      int oldState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0);

      /*
       *  Reconnect to the dock if:
       *  1) it is a dock
       *  2) it is disconnected
       *  3) the disconnect is initiated remotely
       *  4) the dock is still docked (check can only be done in the Service)
       */
      if (device == null) {
        if (DEBUG) Log.d(TAG, "Device is missing");
        return;
      }

      if (newState == BluetoothProfile.STATE_DISCONNECTED
          && oldState != BluetoothProfile.STATE_DISCONNECTING) {
        // Too bad, the dock state can't be checked from a BroadcastReceiver.
        Intent i = new Intent(intent);
        i.setClass(context, DockService.class);
        beginStartingService(context, i);
      }

    } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
      int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
      if (btState != BluetoothAdapter.STATE_TURNING_ON) {
        Intent i = new Intent(intent);
        i.setClass(context, DockService.class);
        beginStartingService(context, i);
      }
    }
  }