/** * Connect to Clementine * * @return true if the connection was established, false if not */ private boolean connect() { String ip = mSharedPref.getString(App.SP_KEY_IP, ""); int port; try { port = Integer.valueOf( mSharedPref.getString(App.SP_KEY_PORT, String.valueOf(Clementine.DefaultPort))); } catch (NumberFormatException e) { port = Clementine.DefaultPort; } int authCode = mSharedPref.getInt(App.SP_LAST_AUTH_CODE, 0); return mClient.createConnection( ClementineMessageFactory.buildConnectMessage(ip, port, authCode, false, true)); }
/** * 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; }