Esempio n. 1
0
  private void tryToConnect(boolean create_account) throws YaximXMPPException {
    try {
      if (mXMPPConnection.isConnected()) {
        try {
          mStreamHandler.quickShutdown(); // blocking shutdown prior to re-connection
        } catch (Exception e) {
          debugLog("conn.shutdown() failed: " + e);
        }
      }
      registerRosterListener();
      boolean need_bind = !mStreamHandler.isResumePossible();

      mXMPPConnection.connect(need_bind);
      // the following should not happen as of smack 3.3.1
      if (!mXMPPConnection.isConnected()) {
        throw new YaximXMPPException("SMACK connect failed without exception!");
      }
      if (mConnectionListener != null)
        mXMPPConnection.removeConnectionListener(mConnectionListener);
      mConnectionListener =
          new ConnectionListener() {
            public void connectionClosedOnError(Exception e) {
              onDisconnected(e);
            }

            public void connectionClosed() {
              // TODO: fix reconnect when we got kicked by the server or SM failed!
              // onDisconnected(null);
              updateConnectionState(ConnectionState.OFFLINE);
            }

            public void reconnectingIn(int seconds) {}

            public void reconnectionFailed(Exception e) {}

            public void reconnectionSuccessful() {}
          };
      mXMPPConnection.addConnectionListener(mConnectionListener);

      // SMACK auto-logins if we were authenticated before
      if (!mXMPPConnection.isAuthenticated()) {
        if (create_account) {
          Log.d(TAG, "creating new server account...");
          AccountManager am = new AccountManager(mXMPPConnection);
          am.createAccount(mConfig.userName, mConfig.password);
        }
        mXMPPConnection.login(mConfig.userName, mConfig.password, mConfig.ressource);
      }
      Log.d(
          TAG, "SM: can resume = " + mStreamHandler.isResumePossible() + " needbind=" + need_bind);
      if (need_bind) {
        mStreamHandler.notifyInitialLogin();
        setStatusFromConfig();
      }

    } catch (YaximXMPPException e) {
      throw e;
    } catch (Exception e) {
      // actually we just care for IllegalState or NullPointer or XMPPEx.
      throw new YaximXMPPException("tryToConnect failed", e);
    }
  }