コード例 #1
0
  /**
   * Try to connect to Clementine
   *
   * @param message The Request Object. Stores the ip to connect to.
   */
  public boolean createConnection(ClementineMessage message) {
    SocketAddress socketAddress = new InetSocketAddress(message.getIp(), message.getPort());
    mSocket = new Socket();
    try {
      mSocket.connect(socketAddress, 3000);
      mIn = new DataInputStream(mSocket.getInputStream());
      mOut = new DataOutputStream(mSocket.getOutputStream());

      // Send the connect request to clementine
      sendRequest(message);
    } catch (IOException e) {
      return false;
    }

    return true;
  }
コード例 #2
0
  /**
   * Try to connect to Clementine
   *
   * @param message The Request Object. Stores the ip to connect to.
   */
  @Override
  public boolean createConnection(ClementineMessage message) {
    boolean connected = false;
    // Reset the connected flag
    mLastKeepAlive = 0;

    // Now try to connect and set the input and output streams
    connected = super.createConnection(message);

    // Check if Clementine dropped the connection.
    // Is possible when we connect from a public ip and clementine rejects it
    if (connected && !mSocket.isClosed()) {
      // Enter the main loop in the thread
      Message msg = Message.obtain();
      msg.arg1 = CHECK_FOR_DATA_ARG;
      mHandler.sendMessage(msg);

      // Now we are connected
      mLastSong = null;
      mLastState = App.mClementine.getState();

      // Setup the MediaButtonReceiver and the RemoteControlClient
      registerRemoteControlClient();

      // Register MediaButtonReceiver
      IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
      App.mApp.registerReceiver(mMediaButtonBroadcastReceiver, filter);

      updateNotification();

      // The device shall be awake
      mWakeLock.acquire();

      // We can now reconnect MAX_RECONNECTS times when
      // we get a keep alive timeout
      mLeftReconnects = MAX_RECONNECTS;

      // Set the current time to last keep alive
      setLastKeepAlive(System.currentTimeMillis());

      // Until we get a new connection request from ui,
      // don't request the first data a second time
      mRequestConnect =
          ClementineMessageFactory.buildConnectMessage(
              message.getIp(),
              message.getPort(),
              message.getMessage().getRequestConnect().getAuthCode(),
              false,
              message.getMessage().getRequestConnect().getDownloader());

      // Save started transmitted bytes
      int uid = App.mApp.getApplicationInfo().uid;
      mStartTx = TrafficStats.getUidTxBytes(uid);
      mStartRx = TrafficStats.getUidRxBytes(uid);

      mStartTime = new Date().getTime();
    } else {
      sendUiMessage(new ClementineMessage(ErrorMessage.NO_CONNECTION));
    }

    return connected;
  }