Commit b6cb58ba authored by Grigory Fedorov's avatar Grigory Fedorov

AccountManager: light refactoring.

parent 97e326bb
...@@ -23,6 +23,7 @@ import com.xabber.android.data.NetworkException; ...@@ -23,6 +23,7 @@ import com.xabber.android.data.NetworkException;
import com.xabber.android.data.OnLoadListener; import com.xabber.android.data.OnLoadListener;
import com.xabber.android.data.OnWipeListener; import com.xabber.android.data.OnWipeListener;
import com.xabber.android.data.SettingsManager; import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.connection.ConnectionSettings;
import com.xabber.android.data.connection.ConnectionState; import com.xabber.android.data.connection.ConnectionState;
import com.xabber.android.data.connection.ProxyType; import com.xabber.android.data.connection.ProxyType;
import com.xabber.android.data.connection.TLSMode; import com.xabber.android.data.connection.TLSMode;
...@@ -113,40 +114,35 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -113,40 +114,35 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
private AccountManager() { private AccountManager() {
this.application = Application.getInstance(); this.application = Application.getInstance();
accountItems = new HashMap<String, AccountItem>(); accountItems = new HashMap<>();
enabledAccounts = new HashSet<String>(); enabledAccounts = new HashSet<>();
savedStatuses = new ArrayList<SavedStatus>(); savedStatuses = new ArrayList<>();
authorizationErrorProvider = new BaseAccountNotificationProvider<AccountAuthorizationError>( authorizationErrorProvider = new BaseAccountNotificationProvider<>(R.drawable.ic_stat_error);
R.drawable.ic_stat_error); passwordRequestProvider = new BaseAccountNotificationProvider<>(R.drawable.ic_stat_ic_add_circle);
passwordRequestProvider = new BaseAccountNotificationProvider<PasswordRequest>(
R.drawable.ic_stat_ic_add_circle); TypedArray accountAvatars = application.getResources().obtainTypedArray(R.array.account_avatars);
TypedArray accountAvatars = application.getResources()
.obtainTypedArray(R.array.account_avatars);
colors = accountAvatars.length(); colors = accountAvatars.length();
accountAvatars.recycle(); accountAvatars.recycle();
TypedArray types = application.getResources().obtainTypedArray( TypedArray types = application.getResources().obtainTypedArray(R.array.account_types);
R.array.account_types); accountTypes = new ArrayList<>();
accountTypes = new ArrayList<AccountType>();
for (int index = 0; index < types.length(); index++) { for (int index = 0; index < types.length(); index++) {
int id = types.getResourceId(index, 0); int id = types.getResourceId(index, 0);
TypedArray values = application.getResources().obtainTypedArray(id); TypedArray values = application.getResources().obtainTypedArray(id);
AccountProtocol protocol = AccountProtocol.valueOf(values AccountProtocol protocol = AccountProtocol.valueOf(values.getString(0));
.getString(0));
if (Build.VERSION.SDK_INT < 8 && protocol == AccountProtocol.wlm) { if (Build.VERSION.SDK_INT < 8 && protocol == AccountProtocol.wlm) {
values.recycle(); values.recycle();
continue; continue;
} }
ArrayList<String> servers = new ArrayList<String>(); ArrayList<String> servers = new ArrayList<>();
servers.add(values.getString(9)); servers.add(values.getString(9));
for (int i = 10; i < values.length(); i++) for (int i = 10; i < values.length(); i++) {
servers.add(values.getString(i)); servers.add(values.getString(i));
}
accountTypes.add(new AccountType(id, protocol, values.getString(1), accountTypes.add(new AccountType(id, protocol, values.getString(1),
values.getString(2), values.getString(3), values values.getString(2), values.getString(3), values.getDrawable(4),
.getDrawable(4), values.getBoolean(5, false), values.getBoolean(5, false), values.getString(6), values.getInt(7, 5222),
values.getString(6), values.getInt(7, 5222), values values.getBoolean(8, false), servers));
.getBoolean(8, false), servers));
values.recycle(); values.recycle();
} }
types.recycle(); types.recycle();
...@@ -156,15 +152,14 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -156,15 +152,14 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
@Override @Override
public void onLoad() { public void onLoad() {
final Collection<SavedStatus> savedStatuses = new ArrayList<SavedStatus>(); final Collection<SavedStatus> savedStatuses = new ArrayList<>();
final Collection<AccountItem> accountItems = new ArrayList<AccountItem>(); final Collection<AccountItem> accountItems = new ArrayList<>();
Cursor cursor = StatusTable.getInstance().list(); Cursor cursor = StatusTable.getInstance().list();
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
savedStatuses.add(new SavedStatus(StatusTable savedStatuses.add(new SavedStatus(StatusTable.getStatusMode(cursor),
.getStatusMode(cursor), StatusTable StatusTable.getStatusText(cursor)));
.getStatusText(cursor)));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
} finally { } finally {
...@@ -218,28 +213,28 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -218,28 +213,28 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
}); });
} }
private void onLoaded(Collection<SavedStatus> savedStatuses, private void onLoaded(Collection<SavedStatus> savedStatuses, Collection<AccountItem> accountItems) {
Collection<AccountItem> accountItems) {
this.savedStatuses.addAll(savedStatuses); this.savedStatuses.addAll(savedStatuses);
for (AccountItem accountItem : accountItems) for (AccountItem accountItem : accountItems) {
addAccount(accountItem); addAccount(accountItem);
NotificationManager.getInstance().registerNotificationProvider( }
authorizationErrorProvider); NotificationManager.getInstance().registerNotificationProvider(authorizationErrorProvider);
NotificationManager.getInstance().registerNotificationProvider( NotificationManager.getInstance().registerNotificationProvider(passwordRequestProvider);
passwordRequestProvider);
} }
private void addAccount(AccountItem accountItem) { private void addAccount(AccountItem accountItem) {
accountItems.put(accountItem.getAccount(), accountItem); accountItems.put(accountItem.getAccount(), accountItem);
if (accountItem.isEnabled()) if (accountItem.isEnabled()) {
enabledAccounts.add(accountItem.getAccount()); enabledAccounts.add(accountItem.getAccount());
for (OnAccountAddedListener listener : application }
.getManagers(OnAccountAddedListener.class)) for (OnAccountAddedListener listener : application.getManagers(OnAccountAddedListener.class)) {
listener.onAccountAdded(accountItem); listener.onAccountAdded(accountItem);
}
if (accountItem.isEnabled()) { if (accountItem.isEnabled()) {
onAccountEnabled(accountItem); onAccountEnabled(accountItem);
if (accountItem.getRawStatusMode().isOnline()) if (accountItem.getRawStatusMode().isOnline()) {
onAccountOnline(accountItem); onAccountOnline(accountItem);
}
} }
onAccountChanged(accountItem.getAccount()); onAccountChanged(accountItem.getAccount());
} }
...@@ -256,13 +251,16 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -256,13 +251,16 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/ */
int getNextColorIndex() { int getNextColorIndex() {
int[] count = new int[colors]; int[] count = new int[colors];
for (AccountItem accountItem : accountItems.values()) for (AccountItem accountItem : accountItems.values()) {
count[accountItem.getColorIndex() % colors] += 1; count[accountItem.getColorIndex() % colors] += 1;
}
int result = 0; int result = 0;
int value = count[0]; int value = count[0];
for (int index = 0; index < count.length; index++) for (int index = 0; index < count.length; index++) {
if (count[index] < value) if (count[index] < value) {
result = index; result = index;
}
}
return result; return result;
} }
...@@ -280,54 +278,42 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -280,54 +278,42 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param accountItem * @param accountItem
*/ */
void requestToWriteAccount(final AccountItem accountItem) { void requestToWriteAccount(final AccountItem accountItem) {
final AccountProtocol protocol = accountItem.getConnectionSettings() ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
.getProtocol();
final boolean custom = accountItem.getConnectionSettings().isCustom(); final AccountProtocol protocol = connectionSettings.getProtocol();
final String host = accountItem.getConnectionSettings().getHost(); final boolean custom = connectionSettings.isCustom();
final int port = accountItem.getConnectionSettings().getPort(); final String host = connectionSettings.getHost();
final String serverName = accountItem.getConnectionSettings() final int port = connectionSettings.getPort();
.getServerName(); final String serverName = connectionSettings.getServerName();
final String userName = accountItem.getConnectionSettings() final String userName = connectionSettings.getUserName();
.getUserName(); final String resource = connectionSettings.getResource();
final String resource = accountItem.getConnectionSettings()
.getResource();
final boolean storePassword = accountItem.isStorePassword(); final boolean storePassword = accountItem.isStorePassword();
final String password = accountItem.getConnectionSettings() final String password = connectionSettings.getPassword();
.getPassword();
final int colorIndex = accountItem.getColorIndex(); final int colorIndex = accountItem.getColorIndex();
final int priority = accountItem.getPriority(); final int priority = accountItem.getPriority();
final StatusMode statusMode = accountItem.getRawStatusMode(); final StatusMode statusMode = accountItem.getRawStatusMode();
final String statusText = accountItem.getStatusText(); final String statusText = accountItem.getStatusText();
final boolean enabled = accountItem.isEnabled(); final boolean enabled = accountItem.isEnabled();
final boolean saslEnabled = accountItem.getConnectionSettings() final boolean saslEnabled = connectionSettings.isSaslEnabled();
.isSaslEnabled(); final TLSMode tlsMode = connectionSettings.getTlsMode();
final TLSMode tlsMode = accountItem.getConnectionSettings() final boolean compression = connectionSettings.useCompression();
.getTlsMode(); final ProxyType proxyType = connectionSettings.getProxyType();
final boolean compression = accountItem.getConnectionSettings() final String proxyHost = connectionSettings.getProxyHost();
.useCompression(); final int proxyPort = connectionSettings.getProxyPort();
final ProxyType proxyType = accountItem.getConnectionSettings() final String proxyUser = connectionSettings.getProxyUser();
.getProxyType(); final String proxyPassword = connectionSettings.getProxyPassword();
final String proxyHost = accountItem.getConnectionSettings()
.getProxyHost();
final int proxyPort = accountItem.getConnectionSettings()
.getProxyPort();
final String proxyUser = accountItem.getConnectionSettings()
.getProxyUser();
final String proxyPassword = accountItem.getConnectionSettings()
.getProxyPassword();
final boolean syncable = accountItem.isSyncable(); final boolean syncable = accountItem.isSyncable();
final KeyPair keyPair = accountItem.getKeyPair(); final KeyPair keyPair = accountItem.getKeyPair();
final Date lastSync = accountItem.getLastSync(); final Date lastSync = accountItem.getLastSync();
final ArchiveMode archiveMode = accountItem.getArchiveMode(); final ArchiveMode archiveMode = accountItem.getArchiveMode();
Application.getInstance().runInBackground(new Runnable() { Application.getInstance().runInBackground(new Runnable() {
@Override @Override
public void run() { public void run() {
accountItem.setId(AccountTable.getInstance().write( accountItem.setId(AccountTable.getInstance().write(accountItem.getId(), protocol,
accountItem.getId(), protocol, custom, host, port, custom, host, port, serverName, userName, resource, storePassword, password,
serverName, userName, resource, storePassword, colorIndex, priority, statusMode, statusText, enabled, saslEnabled, tlsMode,
password, colorIndex, priority, statusMode, statusText, compression, proxyType, proxyHost, proxyPort, proxyUser, proxyPassword,
enabled, saslEnabled, tlsMode, compression, proxyType,
proxyHost, proxyPort, proxyUser, proxyPassword,
syncable, keyPair, lastSync, archiveMode)); syncable, keyPair, lastSync, archiveMode));
} }
}); });
...@@ -336,26 +322,25 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -336,26 +322,25 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
/** /**
* Creates new account and starts connection. * Creates new account and starts connection.
*/ */
private AccountItem addAccount(AccountProtocol protocol, boolean custom, private AccountItem addAccount(AccountProtocol protocol, boolean custom, String host, int port,
String host, int port, String serverName, String userName, String serverName, String userName, boolean storePassword,
boolean storePassword, String password, String resource, int color, String password, String resource, int color, int priority,
int priority, StatusMode statusMode, String statusText, StatusMode statusMode, String statusText, boolean enabled,
boolean enabled, boolean saslEnabled, TLSMode tlsMode, boolean saslEnabled, TLSMode tlsMode, boolean compression,
boolean compression, ProxyType proxyType, String proxyHost, ProxyType proxyType, String proxyHost, int proxyPort,
int proxyPort, String proxyUser, String proxyPassword, String proxyUser, String proxyPassword, boolean syncable,
boolean syncable, KeyPair keyPair, Date lastSync, KeyPair keyPair, Date lastSync, ArchiveMode archiveMode,
ArchiveMode archiveMode,
boolean registerNewAccount) { boolean registerNewAccount) {
AccountItem accountItem = new AccountItem(protocol, custom, host, port,
serverName, userName, resource, storePassword, password, color, AccountItem accountItem = new AccountItem(protocol, custom, host, port, serverName, userName,
priority, statusMode, statusText, enabled, saslEnabled, resource, storePassword, password, color, priority, statusMode, statusText, enabled,
tlsMode, compression, proxyType, proxyHost, proxyPort, saslEnabled, tlsMode, compression, proxyType, proxyHost, proxyPort, proxyUser,
proxyUser, proxyPassword, syncable, keyPair, lastSync, proxyPassword, syncable, keyPair, lastSync, archiveMode);
archiveMode);
if(registerNewAccount) { if (registerNewAccount) {
// TODO: attempt to register account, if that fails return null; // TODO: attempt to register account, if that fails return null;
accountItem.registerAccount(); // accountItem.registerAccount();
//return(null); // return(null);
} }
requestToWriteAccount(accountItem); requestToWriteAccount(accountItem);
addAccount(accountItem); addAccount(accountItem);
...@@ -375,37 +360,39 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -375,37 +360,39 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @return assigned account name. * @return assigned account name.
* @throws NetworkException if user or server part are invalid. * @throws NetworkException if user or server part are invalid.
*/ */
public String addAccount(String user, String password, public String addAccount(String user, String password, AccountType accountType, boolean syncable,
AccountType accountType, boolean syncable, boolean storePassword, boolean storePassword, boolean useOrbot, boolean registerNewAccount)
boolean useOrbot, throws NetworkException {
boolean registerNewAccount) throws NetworkException {
if (accountType.getProtocol().isOAuth()) { if (accountType.getProtocol().isOAuth()) {
int index = 1; int index = 1;
while (true) { while (true) {
user = String.valueOf(index); user = String.valueOf(index);
boolean found = false; boolean found = false;
for (AccountItem accountItem : accountItems.values()) for (AccountItem accountItem : accountItems.values()) {
if (accountItem.getConnectionSettings().getServerName() if (accountItem.getConnectionSettings().getServerName()
.equals(accountType.getFirstServer()) .equals(accountType.getFirstServer())
&& accountItem.getConnectionSettings() && accountItem.getConnectionSettings().getUserName().equals(user)) {
.getUserName().equals(user)) {
found = true; found = true;
break; break;
} }
if (!found) }
if (!found) {
break; break;
}
index++; index++;
} }
} }
if (user == null) if (user == null) {
throw new NetworkException(R.string.EMPTY_USER_NAME); throw new NetworkException(R.string.EMPTY_USER_NAME);
}
if (user.indexOf("@") == -1) { if (!user.contains("@")) {
if ("".equals(accountType.getFirstServer())) if ("".equals(accountType.getFirstServer())) {
throw new NetworkException(R.string.EMPTY_SERVER_NAME); throw new NetworkException(R.string.EMPTY_SERVER_NAME);
else } else {
user += "@" + accountType.getFirstServer(); user += "@" + accountType.getFirstServer();
}
} }
String serverName = StringUtils.parseServer(user); String serverName = StringUtils.parseServer(user);
...@@ -414,23 +401,26 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -414,23 +401,26 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
String host = accountType.getHost(); String host = accountType.getHost();
int port = accountType.getPort(); int port = accountType.getPort();
boolean tlsRequired = accountType.isTLSRequired(); boolean tlsRequired = accountType.isTLSRequired();
if (useOrbot) if (useOrbot) {
tlsRequired = true; tlsRequired = true;
}
if ("".equals(serverName)) { if ("".equals(serverName)) {
throw new NetworkException(R.string.EMPTY_SERVER_NAME); throw new NetworkException(R.string.EMPTY_SERVER_NAME);
} else if (!accountType.isAllowServer() } else if (!accountType.isAllowServer() && !serverName.equals(accountType.getFirstServer())) {
&& !serverName.equals(accountType.getFirstServer()))
throw new NetworkException(R.string.INCORRECT_USER_NAME); throw new NetworkException(R.string.INCORRECT_USER_NAME);
}
if ("".equals(userName)) if ("".equals(userName)) {
throw new NetworkException(R.string.EMPTY_USER_NAME); throw new NetworkException(R.string.EMPTY_USER_NAME);
if ("".equals(resource)) }
if ("".equals(resource)) {
resource = "android" + StringUtils.randomString(8); resource = "android" + StringUtils.randomString(8);
}
if (accountType.getId() == R.array.account_type_xmpp) { if (accountType.getId() == R.array.account_type_xmpp) {
host = serverName; host = serverName;
for (AccountType check : accountTypes) for (AccountType check : accountTypes) {
if (check.getServers().contains(serverName)) { if (check.getServers().contains(serverName)) {
accountType = check; accountType = check;
host = check.getHost(); host = check.getHost();
...@@ -438,30 +428,30 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -438,30 +428,30 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
tlsRequired = check.isTLSRequired(); tlsRequired = check.isTLSRequired();
break; break;
} }
}
} }
AccountItem accountItem; AccountItem accountItem;
for (; ; ) { while(true) {
if (getAccount(userName + '@' + serverName + '/' + resource) == null) if (getAccount(userName + '@' + serverName + '/' + resource) == null) {
break; break;
}
resource = "android" + StringUtils.randomString(8); resource = "android" + StringUtils.randomString(8);
} }
accountItem = addAccount(accountType.getProtocol(), true, host, port, accountItem = addAccount(accountType.getProtocol(), true, host, port, serverName, userName,
serverName, userName, storePassword, password, resource, storePassword, password, resource, getNextColorIndex(), 0, StatusMode.available,
getNextColorIndex(), 0, StatusMode.available, SettingsManager.statusText(), true, true, tlsRequired ? TLSMode.required : TLSMode.enabled,
SettingsManager.statusText(), true, true, false, useOrbot ? ProxyType.orbot : ProxyType.none, "localhost", 8080,
tlsRequired ? TLSMode.required : TLSMode.enabled, false,
useOrbot ? ProxyType.orbot : ProxyType.none, "localhost", 8080,
"", "", syncable, null, null, ArchiveMode.available, registerNewAccount); "", "", syncable, null, null, ArchiveMode.available, registerNewAccount);
if(accountItem == null) { if (accountItem == null) {
throw new NetworkException(R.string.ACCOUNT_REGISTER_FAILED); throw new NetworkException(R.string.ACCOUNT_REGISTER_FAILED);
} }
onAccountChanged(accountItem.getAccount()); onAccountChanged(accountItem.getAccount());
if (accountItems.size() > 1 if (accountItems.size() > 1 && SettingsManager.contactsEnableShowAccounts()) {
&& SettingsManager.contactsEnableShowAccounts())
SettingsManager.enableContactsShowAccount(); SettingsManager.enableContactsShowAccount();
}
return accountItem.getAccount(); return accountItem.getAccount();
} }
...@@ -476,8 +466,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -476,8 +466,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
accountItem.setEnabled(false); accountItem.setEnabled(false);
accountItem.updateConnection(true); accountItem.updateConnection(true);
if (wasEnabled) { if (wasEnabled) {
if (accountItem.getRawStatusMode().isOnline()) if (accountItem.getRawStatusMode().isOnline()) {
onAccountOffline(accountItem); onAccountOffline(accountItem);
}
onAccountDisabled(accountItem); onAccountDisabled(accountItem);
} }
Application.getInstance().runInBackground(new Runnable() { Application.getInstance().runInBackground(new Runnable() {
...@@ -488,9 +479,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -488,9 +479,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
}); });
accountItems.remove(account); accountItems.remove(account);
enabledAccounts.remove(account); enabledAccounts.remove(account);
for (OnAccountRemovedListener listener : application for (OnAccountRemovedListener listener : application.getManagers(OnAccountRemovedListener.class)) {
.getManagers(OnAccountRemovedListener.class))
listener.onAccountRemoved(accountItem); listener.onAccountRemoved(accountItem);
}
removeAuthorizationError(account); removeAuthorizationError(account);
} }
...@@ -527,50 +518,40 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -527,50 +518,40 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param syncable * @param syncable
* @param archiveMode * @param archiveMode
*/ */
public void updateAccount(String account, boolean custom, String host, public void updateAccount(String account, boolean custom, String host, int port, String serverName,
int port, String serverName, String userName, String userName, boolean storePassword, String password, String resource,
boolean storePassword, String password, String resource, int priority, boolean enabled, boolean saslEnabled, TLSMode tlsMode,
int priority, boolean enabled, boolean saslEnabled, boolean compression, ProxyType proxyType, String proxyHost, int proxyPort,
TLSMode tlsMode, boolean compression, ProxyType proxyType, String proxyUser, String proxyPassword, boolean syncable, ArchiveMode archiveMode) {
String proxyHost, int proxyPort, String proxyUser,
String proxyPassword, boolean syncable, ArchiveMode archiveMode) {
AccountItem result; AccountItem result;
AccountItem accountItem = getAccount(account); AccountItem accountItem = getAccount(account);
if (accountItem.getConnectionSettings().getServerName() if (accountItem.getConnectionSettings().getServerName().equals(serverName)
.equals(serverName) && accountItem.getConnectionSettings().getUserName().equals(userName)
&& accountItem.getConnectionSettings().getUserName() && accountItem.getConnectionSettings().getResource().equals(resource)) {
.equals(userName)
&& accountItem.getConnectionSettings().getResource()
.equals(resource)) {
result = accountItem; result = accountItem;
boolean reconnect = false; boolean reconnect = false;
if (accountItem.getConnectionSettings().isCustom() != custom if (accountItem.getConnectionSettings().isCustom() != custom
|| !accountItem.getConnectionSettings().getHost() || !accountItem.getConnectionSettings().getHost().equals(host)
.equals(host)
|| accountItem.getConnectionSettings().getPort() != port || accountItem.getConnectionSettings().getPort() != port
|| !accountItem.getConnectionSettings().getPassword() || !accountItem.getConnectionSettings().getPassword().equals(password)
.equals(password)
|| accountItem.getConnectionSettings().getTlsMode() != tlsMode || accountItem.getConnectionSettings().getTlsMode() != tlsMode
|| accountItem.getConnectionSettings().isSaslEnabled() != saslEnabled || accountItem.getConnectionSettings().isSaslEnabled() != saslEnabled
|| accountItem.getConnectionSettings().useCompression() != compression || accountItem.getConnectionSettings().useCompression() != compression
|| accountItem.getConnectionSettings().getProxyType() != proxyType || accountItem.getConnectionSettings().getProxyType() != proxyType
|| !accountItem.getConnectionSettings().getProxyHost() || !accountItem.getConnectionSettings().getProxyHost().equals(proxyHost)
.equals(proxyHost)
|| accountItem.getConnectionSettings().getProxyPort() != proxyPort || accountItem.getConnectionSettings().getProxyPort() != proxyPort
|| !accountItem.getConnectionSettings().getProxyUser() || !accountItem.getConnectionSettings().getProxyUser().equals(proxyUser)
.equals(proxyUser) || !accountItem.getConnectionSettings().getProxyPassword().equals(proxyPassword)) {
|| !accountItem.getConnectionSettings().getProxyPassword() result.updateConnectionSettings(custom, host, port, password, saslEnabled, tlsMode,
.equals(proxyPassword)) { compression, proxyType, proxyHost, proxyPort, proxyUser, proxyPassword);
result.updateConnectionSettings(custom, host, port, password,
saslEnabled, tlsMode, compression, proxyType,
proxyHost, proxyPort, proxyUser, proxyPassword);
reconnect = true; reconnect = true;
} }
if (result.isSyncable() != syncable) { if (result.isSyncable() != syncable) {
result.setSyncable(syncable); result.setSyncable(syncable);
for (OnAccountSyncableChangedListener listener : application for (OnAccountSyncableChangedListener listener :
.getManagers(OnAccountSyncableChangedListener.class)) application.getManagers(OnAccountSyncableChangedListener.class)) {
listener.onAccountSyncableChanged(result); listener.onAccountSyncableChanged(result);
}
} }
result.setStorePassword(storePassword); result.setStorePassword(storePassword);
boolean changed = result.isEnabled() != enabled; boolean changed = result.isEnabled() != enabled;
...@@ -585,15 +566,17 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -585,15 +566,17 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
if (result.getArchiveMode() != archiveMode) { if (result.getArchiveMode() != archiveMode) {
reconnect = (result.getArchiveMode() == ArchiveMode.server) != (archiveMode == ArchiveMode.server); reconnect = (result.getArchiveMode() == ArchiveMode.server) != (archiveMode == ArchiveMode.server);
result.setArchiveMode(archiveMode); result.setArchiveMode(archiveMode);
for (OnAccountArchiveModeChangedListener listener : application for (OnAccountArchiveModeChangedListener listener :
.getManagers(OnAccountArchiveModeChangedListener.class)) application.getManagers(OnAccountArchiveModeChangedListener.class)) {
listener.onAccountArchiveModeChanged(result); listener.onAccountArchiveModeChanged(result);
}
} }
if (changed && enabled) { if (changed && enabled) {
enabledAccounts.add(account); enabledAccounts.add(account);
onAccountEnabled(result); onAccountEnabled(result);
if (result.getRawStatusMode().isOnline()) if (result.getRawStatusMode().isOnline()) {
onAccountOnline(result); onAccountOnline(result);
}
} }
if (changed || reconnect) { if (changed || reconnect) {
result.updateConnection(true); result.updateConnection(true);
...@@ -601,8 +584,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -601,8 +584,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
} }
if (changed && !enabled) { if (changed && !enabled) {
enabledAccounts.remove(account); enabledAccounts.remove(account);
if (result.getRawStatusMode().isOnline()) if (result.getRawStatusMode().isOnline()) {
onAccountOffline(result); onAccountOffline(result);
}
onAccountDisabled(result); onAccountDisabled(result);
} }
requestToWriteAccount(result); requestToWriteAccount(result);
...@@ -610,17 +594,14 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -610,17 +594,14 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
int colorIndex = accountItem.getColorIndex(); int colorIndex = accountItem.getColorIndex();
StatusMode statusMode = accountItem.getRawStatusMode(); StatusMode statusMode = accountItem.getRawStatusMode();
String statusText = accountItem.getStatusText(); String statusText = accountItem.getStatusText();
AccountProtocol protocol = accountItem.getConnectionSettings() AccountProtocol protocol = accountItem.getConnectionSettings().getProtocol();
.getProtocol();
KeyPair keyPair = accountItem.getKeyPair(); KeyPair keyPair = accountItem.getKeyPair();
Date lastSync = accountItem.getLastSync(); Date lastSync = accountItem.getLastSync();
removeAccountWithoutCallback(account); removeAccountWithoutCallback(account);
result = addAccount(protocol, custom, host, port, serverName, result = addAccount(protocol, custom, host, port, serverName, userName, storePassword,
userName, storePassword, password, resource, colorIndex, password, resource, colorIndex, priority, statusMode, statusText, enabled,
priority, statusMode, statusText, enabled, saslEnabled, saslEnabled, tlsMode, compression, proxyType, proxyHost, proxyPort, proxyUser,
tlsMode, compression, proxyType, proxyHost, proxyPort, proxyPassword, syncable, keyPair, lastSync, archiveMode, false);
proxyUser, proxyPassword, syncable, keyPair, lastSync,
archiveMode, false);
} }
onAccountChanged(result.getAccount()); onAccountChanged(result.getAccount());
} }
...@@ -639,75 +620,92 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -639,75 +620,92 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
public void setSyncable(String account, boolean syncable) { public void setSyncable(String account, boolean syncable) {
AccountItem accountItem = getAccount(account); AccountItem accountItem = getAccount(account);
updateAccount(account, accountItem.getConnectionSettings().isCustom(), ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
accountItem.getConnectionSettings().getHost(), accountItem updateAccount(
.getConnectionSettings().getPort(), accountItem account,
.getConnectionSettings().getServerName(), accountItem connectionSettings.isCustom(),
.getConnectionSettings().getUserName(), connectionSettings.getHost(),
accountItem.isStorePassword(), accountItem connectionSettings.getPort(),
.getConnectionSettings().getPassword(), accountItem connectionSettings.getServerName(),
.getConnectionSettings().getResource(), connectionSettings.getUserName(),
accountItem.getPriority(), accountItem.isEnabled(), accountItem accountItem.isStorePassword(),
.getConnectionSettings().isSaslEnabled(), accountItem connectionSettings.getPassword(),
.getConnectionSettings().getTlsMode(), accountItem connectionSettings.getResource(),
.getConnectionSettings().useCompression(), accountItem accountItem.getPriority(),
.getConnectionSettings().getProxyType(), accountItem accountItem.isEnabled(),
.getConnectionSettings().getProxyHost(), accountItem connectionSettings.isSaslEnabled(),
.getConnectionSettings().getProxyPort(), accountItem connectionSettings.getTlsMode(),
.getConnectionSettings().getProxyUser(), accountItem connectionSettings.useCompression(),
.getConnectionSettings().getProxyPassword(), syncable, connectionSettings.getProxyType(),
connectionSettings.getProxyHost(),
connectionSettings.getProxyPort(),
connectionSettings.getProxyUser(),
connectionSettings.getProxyPassword(),
syncable,
accountItem.getArchiveMode()); accountItem.getArchiveMode());
} }
public void setPassword(String account, boolean storePassword, public void setPassword(String account, boolean storePassword, String password) {
String password) {
AccountItem accountItem = getAccount(account); AccountItem accountItem = getAccount(account);
updateAccount(account, accountItem.getConnectionSettings().isCustom(), ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
accountItem.getConnectionSettings().getHost(), accountItem updateAccount(
.getConnectionSettings().getPort(), accountItem account,
.getConnectionSettings().getServerName(), accountItem connectionSettings.isCustom(),
.getConnectionSettings().getUserName(), storePassword, connectionSettings.getHost(),
password, accountItem.getConnectionSettings().getResource(), connectionSettings.getPort(),
accountItem.getPriority(), accountItem.isEnabled(), accountItem connectionSettings.getServerName(),
.getConnectionSettings().isSaslEnabled(), accountItem connectionSettings.getUserName(),
.getConnectionSettings().getTlsMode(), accountItem storePassword,
.getConnectionSettings().useCompression(), accountItem password,
.getConnectionSettings().getProxyType(), accountItem connectionSettings.getResource(),
.getConnectionSettings().getProxyHost(), accountItem accountItem.getPriority(),
.getConnectionSettings().getProxyPort(), accountItem accountItem.isEnabled(),
.getConnectionSettings().getProxyUser(), accountItem connectionSettings.isSaslEnabled(),
.getConnectionSettings().getProxyPassword(), connectionSettings.getTlsMode(),
accountItem.isSyncable(), accountItem.getArchiveMode()); connectionSettings.useCompression(),
connectionSettings.getProxyType(),
connectionSettings.getProxyHost(),
connectionSettings.getProxyPort(),
connectionSettings.getProxyUser(),
connectionSettings.getProxyPassword(),
accountItem.isSyncable(),
accountItem.getArchiveMode()
);
} }
public void setArchiveMode(String account, ArchiveMode archiveMode) { public void setArchiveMode(String account, ArchiveMode archiveMode) {
AccountItem accountItem = AccountManager.getInstance().getAccount( AccountItem accountItem = AccountManager.getInstance().getAccount(account);
account); ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
AccountManager.getInstance().updateAccount(account, AccountManager.getInstance().updateAccount(
accountItem.getConnectionSettings().isCustom(), account,
accountItem.getConnectionSettings().getHost(), connectionSettings.isCustom(),
accountItem.getConnectionSettings().getPort(), connectionSettings.getHost(),
accountItem.getConnectionSettings().getServerName(), connectionSettings.getPort(),
accountItem.getConnectionSettings().getUserName(), connectionSettings.getServerName(),
connectionSettings.getUserName(),
accountItem.isStorePassword(), accountItem.isStorePassword(),
accountItem.getConnectionSettings().getPassword(), connectionSettings.getPassword(),
accountItem.getConnectionSettings().getResource(), connectionSettings.getResource(),
accountItem.getPriority(), accountItem.isEnabled(), accountItem.getPriority(),
accountItem.getConnectionSettings().isSaslEnabled(), accountItem.isEnabled(),
accountItem.getConnectionSettings().getTlsMode(), connectionSettings.isSaslEnabled(),
accountItem.getConnectionSettings().useCompression(), connectionSettings.getTlsMode(),
accountItem.getConnectionSettings().getProxyType(), connectionSettings.useCompression(),
accountItem.getConnectionSettings().getProxyHost(), connectionSettings.getProxyType(),
accountItem.getConnectionSettings().getProxyPort(), connectionSettings.getProxyHost(),
accountItem.getConnectionSettings().getProxyUser(), connectionSettings.getProxyPort(),
accountItem.getConnectionSettings().getProxyPassword(), connectionSettings.getProxyUser(),
accountItem.isSyncable(), archiveMode); connectionSettings.getProxyPassword(),
accountItem.isSyncable(),
archiveMode
);
} }
public ArchiveMode getArchiveMode(String account) { public ArchiveMode getArchiveMode(String account) {
AccountItem accountItem = getAccount(account); AccountItem accountItem = getAccount(account);
if (accountItem == null) if (accountItem == null) {
return ArchiveMode.available; return ArchiveMode.available;
}
return accountItem.getArchiveMode(); return accountItem.getArchiveMode();
} }
...@@ -735,34 +733,41 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -735,34 +733,41 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
for (AccountItem accountItem : accountItems.values()) { for (AccountItem accountItem : accountItems.values()) {
ConnectionState state = accountItem.getState(); ConnectionState state = accountItem.getState();
if (state == ConnectionState.connected) if (state == ConnectionState.connected) {
online = true; online = true;
if (RosterManager.getInstance().isRosterReceived( }
accountItem.getAccount())) if (RosterManager.getInstance().isRosterReceived(accountItem.getAccount())) {
roster = true; roster = true;
if (state == ConnectionState.connecting }
|| state == ConnectionState.authentication) if (state == ConnectionState.connecting || state == ConnectionState.authentication) {
connecting = true; connecting = true;
if (state == ConnectionState.waiting) }
if (state == ConnectionState.waiting) {
waiting = true; waiting = true;
if (accountItem.isEnabled()) }
if (accountItem.isEnabled()) {
offline = true; offline = true;
}
disabled = true; disabled = true;
} }
if (online)
if (online) {
return CommonState.online; return CommonState.online;
else if (roster) } else if (roster) {
return CommonState.roster; return CommonState.roster;
else if (connecting) } else if (connecting) {
return CommonState.connecting; return CommonState.connecting;
if (waiting) }
if (waiting) {
return CommonState.waiting; return CommonState.waiting;
else if (offline) } else if (offline) {
return CommonState.offline; return CommonState.offline;
else if (disabled) } else if (disabled) {
return CommonState.disabled; return CommonState.disabled;
else } else {
return CommonState.empty; return CommonState.empty;
}
} }
/** /**
...@@ -772,12 +777,16 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -772,12 +777,16 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
public int getColorLevel(String account) { public int getColorLevel(String account) {
AccountItem accountItem = getAccount(account); AccountItem accountItem = getAccount(account);
int colorIndex; int colorIndex;
if (accountItem == null)
if (accountItem == null) {
return 0; return 0;
else } else {
colorIndex = accountItem.getColorIndex() % colors; colorIndex = accountItem.getColorIndex() % colors;
if (colorIndex < 0) }
if (colorIndex < 0) {
colorIndex += colors; colorIndex += colors;
}
return colorIndex; return colorIndex;
} }
...@@ -790,21 +799,23 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -790,21 +799,23 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
private boolean hasSameBareAddress(String account) { private boolean hasSameBareAddress(String account) {
String bareAddress = Jid.getBareAddress(account); String bareAddress = Jid.getBareAddress(account);
for (AccountItem check : accountItems.values()) for (AccountItem check : accountItems.values()) {
if (!check.getAccount().equals(account) if (!check.getAccount().equals(account)
&& Jid.getBareAddress(check.getAccount()).equals( && Jid.getBareAddress(check.getAccount()).equals(bareAddress)) {
bareAddress))
return true; return true;
}
}
return false; return false;
} }
private boolean hasSameProtocol(String account) { private boolean hasSameProtocol(String account) {
AccountProtocol protocol = getAccount(account).getConnectionSettings() AccountProtocol protocol = getAccount(account).getConnectionSettings().getProtocol();
.getProtocol(); for (AccountItem check : accountItems.values()) {
for (AccountItem check : accountItems.values())
if (!check.getAccount().equals(account) if (!check.getAccount().equals(account)
&& check.getConnectionSettings().getProtocol() == protocol) && check.getConnectionSettings().getProtocol() == protocol) {
return true; return true;
}
}
return false; return false;
} }
...@@ -814,32 +825,32 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -814,32 +825,32 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/ */
public String getVerboseName(String account) { public String getVerboseName(String account) {
AccountItem accountItem = getAccount(account); AccountItem accountItem = getAccount(account);
if (accountItem == null) if (accountItem == null) {
return account; return account;
}
if (accountItem.getConnectionSettings().getProtocol().isOAuth()) { if (accountItem.getConnectionSettings().getProtocol().isOAuth()) {
String jid = OAuthManager.getInstance().getAssignedJid(account); String jid = OAuthManager.getInstance().getAssignedJid(account);
AccountProtocol accountProtocol = accountItem AccountProtocol accountProtocol = accountItem.getConnectionSettings().getProtocol();
.getConnectionSettings().getProtocol();
String name; String name;
if (jid == null) { if (jid == null) {
if (hasSameProtocol(account)) if (hasSameProtocol(account)) {
name = accountItem.getConnectionSettings().getUserName(); name = accountItem.getConnectionSettings().getUserName();
else } else {
return application.getString(accountProtocol return application.getString(accountProtocol.getNameResource());
.getNameResource()); }
} else { } else {
name = Jid.getBareAddress(jid); name = Jid.getBareAddress(jid);
if (!hasSameBareAddress(jid)) if (!hasSameBareAddress(jid)) {
return name; return name;
}
} }
return application.getString(accountProtocol.getShortResource()) return application.getString(accountProtocol.getShortResource()) + " - " + name;
+ " - " + name;
} else { } else {
if (hasSameBareAddress(account)) if (hasSameBareAddress(account)) {
return account; return account;
else } else {
return Jid.getBareAddress(account); return Jid.getBareAddress(account);
}
} }
} }
...@@ -850,12 +861,12 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -850,12 +861,12 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/ */
public String getNickName(String account) { public String getNickName(String account) {
String jid = OAuthManager.getInstance().getAssignedJid(account); String jid = OAuthManager.getInstance().getAssignedJid(account);
String result = VCardManager.getInstance().getName( String result = VCardManager.getInstance().getName(Jid.getBareAddress(jid));
Jid.getBareAddress(jid)); if ("".equals(result)) {
if ("".equals(result))
return getVerboseName(account); return getVerboseName(account);
else } else {
return result; return result;
}
} }
/** /**
...@@ -865,8 +876,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -865,8 +876,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param statusMode * @param statusMode
* @param statusText * @param statusText
*/ */
public void setStatus(String account, StatusMode statusMode, public void setStatus(String account, StatusMode statusMode, String statusText) {
String statusText) {
addSavedStatus(statusMode, statusText); addSavedStatus(statusMode, statusText);
AccountItem accountItem = getAccount(account); AccountItem accountItem = getAccount(account);
setStatus(accountItem, statusMode, statusText); setStatus(accountItem, statusMode, statusText);
...@@ -875,24 +885,25 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -875,24 +885,25 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
} catch (NetworkException e) { } catch (NetworkException e) {
} }
boolean found = false; boolean found = false;
for (AccountItem check : accountItems.values()) for (AccountItem check : accountItems.values()) {
if (check.isEnabled() if (check.isEnabled() && SettingsManager.statusMode() == check.getRawStatusMode()) {
&& SettingsManager.statusMode() == check.getRawStatusMode()) {
found = true; found = true;
break; break;
} }
if (!found) }
if (!found) {
SettingsManager.setStatusMode(statusMode); SettingsManager.setStatusMode(statusMode);
}
found = false; found = false;
for (AccountItem check : accountItems.values()) for (AccountItem check : accountItems.values()) {
if (check.isEnabled() if (check.isEnabled() && SettingsManager.statusText().equals(check.getStatusText())) {
&& SettingsManager.statusText().equals(
check.getStatusText())) {
found = true; found = true;
break; break;
} }
if (!found) }
if (!found) {
SettingsManager.setStatusText(statusText); SettingsManager.setStatusText(statusText);
}
onAccountChanged(account); onAccountChanged(account);
} }
...@@ -910,8 +921,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -910,8 +921,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* If we are already away or xa, do nothing. * If we are already away or xa, do nothing.
*/ */
public void goAway() { public void goAway() {
if (away || xa) if (away || xa) {
return; return;
}
away = true; away = true;
resendPresence(); resendPresence();
} }
...@@ -922,8 +934,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -922,8 +934,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* If we are already xa, do nothing. * If we are already xa, do nothing.
*/ */
public void goXa() { public void goXa() {
if (xa) if (xa) {
return; return;
}
xa = true; xa = true;
resendPresence(); resendPresence();
} }
...@@ -934,8 +947,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -934,8 +947,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* If we are already waked up, do nothing. * If we are already waked up, do nothing.
*/ */
public void wakeUp() { public void wakeUp() {
if (!away && !xa) if (!away && !xa) {
return; return;
}
away = false; away = false;
xa = false; xa = false;
resendPresence(); resendPresence();
...@@ -945,12 +959,12 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -945,12 +959,12 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* Sends new presence information for all accounts. * Sends new presence information for all accounts.
*/ */
public void resendPresence() { public void resendPresence() {
for (AccountItem accountItem : accountItems.values()) for (AccountItem accountItem : accountItems.values()) {
try { try {
PresenceManager.getInstance().resendPresence( PresenceManager.getInstance().resendPresence(accountItem.getAccount());
accountItem.getAccount());
} catch (NetworkException e) { } catch (NetworkException e) {
} }
}
} }
/** /**
...@@ -960,17 +974,17 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -960,17 +974,17 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param statusMode * @param statusMode
* @param statusText * @param statusText
*/ */
private void setStatus(AccountItem accountItem, StatusMode statusMode, private void setStatus(AccountItem accountItem, StatusMode statusMode, String statusText) {
String statusText) {
boolean changed = accountItem.isEnabled() boolean changed = accountItem.isEnabled()
&& accountItem.getRawStatusMode().isOnline() != statusMode && accountItem.getRawStatusMode().isOnline() != statusMode.isOnline();
.isOnline();
accountItem.setStatus(statusMode, statusText); accountItem.setStatus(statusMode, statusText);
if (changed && statusMode.isOnline()) if (changed && statusMode.isOnline()) {
onAccountOnline(accountItem); onAccountOnline(accountItem);
}
accountItem.updateConnection(true); accountItem.updateConnection(true);
if (changed && !statusMode.isOnline()) if (changed && !statusMode.isOnline()) {
onAccountOffline(accountItem); onAccountOffline(accountItem);
}
requestToWriteAccount(accountItem); requestToWriteAccount(accountItem);
} }
...@@ -982,18 +996,19 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -982,18 +996,19 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/ */
public void setStatus(StatusMode statusMode, String statusText) { public void setStatus(StatusMode statusMode, String statusText) {
SettingsManager.setStatusMode(statusMode); SettingsManager.setStatusMode(statusMode);
if (statusText != null) { if (statusText != null) {
addSavedStatus(statusMode, statusText); addSavedStatus(statusMode, statusText);
SettingsManager.setStatusText(statusText); SettingsManager.setStatusText(statusText);
} }
for (AccountItem accountItem : accountItems.values()) { for (AccountItem accountItem : accountItems.values()) {
setStatus(accountItem, statusMode, setStatus(accountItem, statusMode,
statusText == null ? accountItem.getStatusText() statusText == null ? accountItem.getStatusText() : statusText);
: statusText);
} }
resendPresence(); resendPresence();
onAccountsChanged(new ArrayList<String>(AccountManager.getInstance() onAccountsChanged(new ArrayList<>(AccountManager.getInstance().getAllAccounts()));
.getAllAccounts()));
} }
/** /**
...@@ -1002,11 +1017,11 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -1002,11 +1017,11 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param statusMode * @param statusMode
* @param statusText * @param statusText
*/ */
private void addSavedStatus(final StatusMode statusMode, private void addSavedStatus(final StatusMode statusMode, final String statusText) {
final String statusText) {
SavedStatus savedStatus = new SavedStatus(statusMode, statusText); SavedStatus savedStatus = new SavedStatus(statusMode, statusText);
if (savedStatuses.contains(savedStatus)) if (savedStatuses.contains(savedStatus)) {
return; return;
}
savedStatuses.add(savedStatus); savedStatuses.add(savedStatus);
Application.getInstance().runInBackground(new Runnable() { Application.getInstance().runInBackground(new Runnable() {
@Override @Override
...@@ -1023,8 +1038,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -1023,8 +1038,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param statusText * @param statusText
*/ */
public void removeSavedStatus(final SavedStatus savedStatus) { public void removeSavedStatus(final SavedStatus savedStatus) {
if (!savedStatuses.remove(savedStatus)) if (!savedStatuses.remove(savedStatus)) {
return; return;
}
Application.getInstance().runInBackground(new Runnable() { Application.getInstance().runInBackground(new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -1063,11 +1079,13 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -1063,11 +1079,13 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* </ul> * </ul>
*/ */
public String getSelectedAccount() { public String getSelectedAccount() {
if (SettingsManager.contactsShowAccounts()) if (SettingsManager.contactsShowAccounts()) {
return null; return null;
}
String selected = SettingsManager.contactsSelectedAccount(); String selected = SettingsManager.contactsSelectedAccount();
if (enabledAccounts.contains(selected)) if (enabledAccounts.contains(selected)) {
return selected; return selected;
}
return null; return null;
} }
...@@ -1076,8 +1094,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -1076,8 +1094,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
} }
public void addAuthenticationError(String account) { public void addAuthenticationError(String account) {
authorizationErrorProvider.add(new AccountAuthorizationError(account), authorizationErrorProvider.add(new AccountAuthorizationError(account), true);
true);
} }
public void removePasswordRequest(String account) { public void removePasswordRequest(String account) {
...@@ -1089,7 +1106,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -1089,7 +1106,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
} }
public void onAccountChanged(String account) { public void onAccountChanged(String account) {
Collection<String> accounts = new ArrayList<String>(1); Collection<String> accounts = new ArrayList<>(1);
accounts.add(account); accounts.add(account);
onAccountsChanged(accounts); onAccountsChanged(accounts);
} }
...@@ -1098,37 +1115,37 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -1098,37 +1115,37 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
Application.getInstance().runOnUiThread(new Runnable() { Application.getInstance().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
for (OnAccountChangedListener accountListener : Application for (OnAccountChangedListener accountListener
.getInstance().getUIListeners( : Application.getInstance().getUIListeners(OnAccountChangedListener.class)) {
OnAccountChangedListener.class))
accountListener.onAccountsChanged(accounts); accountListener.onAccountsChanged(accounts);
}
} }
}); });
} }
public void onAccountEnabled(AccountItem accountItem) { public void onAccountEnabled(AccountItem accountItem) {
for (OnAccountEnabledListener listener : application for (OnAccountEnabledListener listener : application.getManagers(OnAccountEnabledListener.class)) {
.getManagers(OnAccountEnabledListener.class))
listener.onAccountEnabled(accountItem); listener.onAccountEnabled(accountItem);
}
} }
public void onAccountOnline(AccountItem accountItem) { public void onAccountOnline(AccountItem accountItem) {
for (OnAccountOnlineListener listener : application for (OnAccountOnlineListener listener : application.getManagers(OnAccountOnlineListener.class)) {
.getManagers(OnAccountOnlineListener.class))
listener.onAccountOnline(accountItem); listener.onAccountOnline(accountItem);
}
} }
public void onAccountOffline(AccountItem accountItem) { public void onAccountOffline(AccountItem accountItem) {
accountItem.clearPassword(); accountItem.clearPassword();
for (OnAccountOfflineListener listener : application for (OnAccountOfflineListener listener : application.getManagers(OnAccountOfflineListener.class)) {
.getManagers(OnAccountOfflineListener.class))
listener.onAccountOffline(accountItem); listener.onAccountOffline(accountItem);
}
} }
public void onAccountDisabled(AccountItem accountItem) { public void onAccountDisabled(AccountItem accountItem) {
for (OnAccountDisabledListener listener : application for (OnAccountDisabledListener listener : application.getManagers(OnAccountDisabledListener.class)) {
.getManagers(OnAccountDisabledListener.class))
listener.onAccountDisabled(accountItem); listener.onAccountDisabled(accountItem);
}
} }
@Override @Override
...@@ -1136,4 +1153,4 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -1136,4 +1153,4 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
AccountTable.getInstance().wipe(); AccountTable.getInstance().wipe();
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment