示例#1
0
 /** Sends new presence information for all accounts. */
 public void resendPresence() {
   for (AccountItem accountItem : accountItems.values())
     try {
       PresenceManager.getInstance().resendPresence(accountItem.getAccount());
     } catch (NetworkException e) {
     }
 }
示例#2
0
 private boolean hasSameProtocol(String account) {
   AccountProtocol protocol = getAccount(account).getConnectionSettings().getProtocol();
   for (AccountItem check : accountItems.values())
     if (!check.getAccount().equals(account)
         && check.getConnectionSettings().getProtocol() == protocol) return true;
   return false;
 }
示例#3
0
  public CommonState getCommonState() {
    boolean disabled = false;
    boolean offline = false;
    boolean waiting = false;
    boolean connecting = false;
    boolean roster = false;
    boolean online = false;

    for (AccountItem accountItem : accountItems.values()) {
      ConnectionState state = accountItem.getState();
      if (state == ConnectionState.connected) online = true;
      if (RosterManager.getInstance().isRosterReceived(accountItem.getAccount())) roster = true;
      if (state == ConnectionState.connecting || state == ConnectionState.authentication)
        connecting = true;
      if (state == ConnectionState.waiting) waiting = true;
      if (accountItem.isEnabled()) offline = true;
      disabled = true;
    }
    if (online) return CommonState.online;
    else if (roster) return CommonState.roster;
    else if (connecting) return CommonState.connecting;
    if (waiting) return CommonState.waiting;
    else if (offline) return CommonState.offline;
    else if (disabled) return CommonState.disabled;
    else return CommonState.empty;
  }
示例#4
0
 private boolean hasSameBareAddress(String account) {
   String bareAddress = Jid.getBareAddress(account);
   for (AccountItem check : accountItems.values())
     if (!check.getAccount().equals(account)
         && Jid.getBareAddress(check.getAccount()).equals(bareAddress)) return true;
   return false;
 }
示例#5
0
 /** @return Next color index for the next account. */
 int getNextColorIndex() {
   int[] count = new int[colors];
   for (AccountItem accountItem : accountItems.values())
     count[accountItem.getColorIndex() % colors] += 1;
   int result = 0;
   int value = count[0];
   for (int index = 0; index < count.length; index++) if (count[index] < value) result = index;
   return result;
 }
示例#6
0
 private void addAccount(AccountItem accountItem) {
   accountItems.put(accountItem.getAccount(), accountItem);
   if (accountItem.isEnabled()) enabledAccounts.add(accountItem.getAccount());
   for (OnAccountAddedListener listener : application.getManagers(OnAccountAddedListener.class))
     listener.onAccountAdded(accountItem);
   if (accountItem.isEnabled()) {
     onAccountEnabled(accountItem);
     if (accountItem.getRawStatusMode().isOnline()) onAccountOnline(accountItem);
   }
   onAccountChanged(accountItem.getAccount());
 }
示例#7
0
 /**
  * Sets status for all accounts.
  *
  * @param statusMode
  * @param statusText can be <code>null</code> if value was not changed.
  */
 public void setStatus(StatusMode statusMode, String statusText) {
   SettingsManager.setStatusMode(statusMode);
   if (statusText != null) {
     addSavedStatus(statusMode, statusText);
     SettingsManager.setStatusText(statusText);
   }
   for (AccountItem accountItem : accountItems.values()) {
     setStatus(
         accountItem, statusMode, statusText == null ? accountItem.getStatusText() : statusText);
   }
   resendPresence();
   onAccountsChanged(new ArrayList<String>(AccountManager.getInstance().getAllAccounts()));
 }
示例#8
0
 /**
  * Sets status for account.
  *
  * @param account
  * @param statusMode
  * @param statusText
  */
 public void setStatus(String account, StatusMode statusMode, String statusText) {
   addSavedStatus(statusMode, statusText);
   AccountItem accountItem = getAccount(account);
   setStatus(accountItem, statusMode, statusText);
   try {
     PresenceManager.getInstance().resendPresence(account);
   } catch (NetworkException e) {
   }
   boolean found = false;
   for (AccountItem check : accountItems.values())
     if (check.isEnabled() && SettingsManager.statusMode() == check.getRawStatusMode()) {
       found = true;
       break;
     }
   if (!found) SettingsManager.setStatusMode(statusMode);
   found = false;
   for (AccountItem check : accountItems.values())
     if (check.isEnabled() && SettingsManager.statusText().equals(check.getStatusText())) {
       found = true;
       break;
     }
   if (!found) SettingsManager.setStatusText(statusText);
   onAccountChanged(account);
 }
示例#9
0
 /**
  * Remove user`s account. Don't call any callbacks.
  *
  * @param account
  */
 private void removeAccountWithoutCallback(final String account) {
   final AccountItem accountItem = getAccount(account);
   boolean wasEnabled = accountItem.isEnabled();
   accountItem.setEnabled(false);
   accountItem.updateConnection(true);
   if (wasEnabled) {
     if (accountItem.getRawStatusMode().isOnline()) onAccountOffline(accountItem);
     onAccountDisabled(accountItem);
   }
   Application.getInstance()
       .runInBackground(
           new Runnable() {
             @Override
             public void run() {
               AccountTable.getInstance().remove(account, accountItem.getId());
             }
           });
   accountItems.remove(account);
   enabledAccounts.remove(account);
   for (OnAccountRemovedListener listener :
       application.getManagers(OnAccountRemovedListener.class))
     listener.onAccountRemoved(accountItem);
   removeAuthorizationError(account);
 }
示例#10
0
 /** @return List of all accounts including disabled. */
 public Collection<String> getAllAccounts() {
   return Collections.unmodifiableCollection(accountItems.keySet());
 }
示例#11
0
  /**
   * Creates new account.
   *
   * @param user full or bare jid.
   * @param password
   * @param accountType xmpp account type can be replaced depend on server part.
   * @param syncable
   * @param storePassword
   * @param useOrbot
   * @return assigned account name.
   * @throws NetworkException if user or server part are invalid.
   */
  public String addAccount(
      String user,
      String password,
      AccountType accountType,
      boolean syncable,
      boolean storePassword,
      boolean useOrbot)
      throws NetworkException {
    if (accountType.getProtocol().isOAuth()) {
      int index = 1;
      while (true) {
        user = String.valueOf(index);
        boolean found = false;
        for (AccountItem accountItem : accountItems.values())
          if (accountItem
                  .getConnectionSettings()
                  .getServerName()
                  .equals(accountType.getFirstServer())
              && accountItem.getConnectionSettings().getUserName().equals(user)) {
            found = true;
            break;
          }
        if (!found) break;
        index++;
      }
    }

    if (user == null) throw new NetworkException(R.string.EMPTY_USER_NAME);

    if (user.indexOf("@") == -1) {
      if ("".equals(accountType.getFirstServer()))
        throw new NetworkException(R.string.EMPTY_SERVER_NAME);
      else user += "@" + accountType.getFirstServer();
    }

    String serverName = StringUtils.parseServer(user);
    String userName = StringUtils.parseName(user);
    String resource = StringUtils.parseResource(user);
    String host = accountType.getHost();
    int port = accountType.getPort();
    boolean tlsRequired = accountType.isTLSRequired();
    if (useOrbot) tlsRequired = true;

    if ("".equals(serverName)) {
      throw new NetworkException(R.string.EMPTY_SERVER_NAME);
    } else if (!accountType.isAllowServer() && !serverName.equals(accountType.getFirstServer()))
      throw new NetworkException(R.string.INCORRECT_USER_NAME);

    if ("".equals(userName)) throw new NetworkException(R.string.EMPTY_USER_NAME);
    if ("".equals(resource)) resource = "android" + StringUtils.randomString(8);

    if (accountType.getId() == R.array.account_type_xmpp) {
      host = serverName;
      for (AccountType check : accountTypes)
        if (check.getServers().contains(serverName)) {
          accountType = check;
          host = check.getHost();
          port = check.getPort();
          tlsRequired = check.isTLSRequired();
          break;
        }
    }

    AccountItem accountItem;
    for (; ; ) {
      if (getAccount(userName + '@' + serverName + '/' + resource) == null) break;
      resource = "android" + StringUtils.randomString(8);
    }

    accountItem =
        addAccount(
            accountType.getProtocol(),
            true,
            host,
            port,
            serverName,
            userName,
            storePassword,
            password,
            resource,
            getNextColorIndex(),
            0,
            StatusMode.available,
            SettingsManager.statusText(),
            true,
            true,
            tlsRequired ? TLSMode.required : TLSMode.enabled,
            false,
            useOrbot ? ProxyType.orbot : ProxyType.none,
            "localhost",
            8080,
            "",
            "",
            syncable,
            null,
            null,
            ArchiveMode.available);
    onAccountChanged(accountItem.getAccount());
    if (accountItems.size() > 1 && SettingsManager.contactsEnableShowAccounts())
      SettingsManager.enableContactsShowAccount();
    return accountItem.getAccount();
  }
示例#12
0
 /**
  * @param account full jid.
  * @return Specified account or <code>null</code> if account doesn't exists.
  */
 public AccountItem getAccount(String account) {
   return accountItems.get(account);
 }