示例#1
0
  /**
   * Set the current state of the chat connection
   *
   * @param state An integer defining the current connection state
   */
  private synchronized void setState(int state, boolean postOnCommunicatorQueue) {
    Log.d(TAG, "setState() " + mState + " -> " + state);
    mState = state;

    // Give the new state to the Handler so the UI Activity can update
    switch (state) {
      case STATE_NONE:
        mLoggerListener.log("BLUETOOTH: disconnected");
        mConnectionStateListener.onCalculatorDisconnected();
        mContext.getServiceInstance().displayNotification(false);
        if (postOnCommunicatorQueue) {
          mContext.post(
              new Runnable() {
                @Override
                public void run() {
                  mContext.getCommunicatorInstance().setConnected(false);
                }
              });
        } else {
          mContext.getCommunicatorInstance().setConnected(false);
        }
        break;
      case STATE_CONNECTING:
        mLoggerListener.log("BLUETOOTH: connecting to " + mDeviceAddress);
        mConnectionStateListener.onCalculatorConnecting();
        break;
      case STATE_CONNECTED:
        mLoggerListener.log("BLUETOOTH: connected");
        mConnectionStateListener.onCalculatorConnected();
        mContext.getServiceInstance().displayNotification(true);
        if (postOnCommunicatorQueue) {
          mContext.post(
              new Runnable() {
                @Override
                public void run() {
                  mContext.getCommunicatorInstance().setConnected(true);
                }
              });
        } else {
          mContext.getCommunicatorInstance().setConnected(true);
        }
        break;
    }
  }