Commit e602df51 authored by Grigory Fedorov's avatar Grigory Fedorov

Merge branch 'feature/account_color' into develop

parents cfacbc7e f510b01c
......@@ -14,12 +14,6 @@
*/
package com.xabber.android.data.account;
import java.security.KeyPair;
import java.util.Date;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import com.xabber.android.data.NetworkException;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.connection.ConnectionItem;
......@@ -29,6 +23,12 @@ import com.xabber.android.data.connection.ProxyType;
import com.xabber.android.data.connection.TLSMode;
import com.xabber.androiddev.R;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import java.security.KeyPair;
import java.util.Date;
/**
* Represent account settings and status.
*
......@@ -51,7 +51,7 @@ public class AccountItem extends ConnectionItem {
*/
private final String account;
private final int colorIndex;
private int colorIndex;
/**
* Whether account is enabled.
......@@ -436,4 +436,7 @@ public class AccountItem extends ConnectionItem {
return Math.min(128, Math.max(-128, priority));
}
public void setColorIndex(int colorIndex) {
this.colorIndex = colorIndex;
}
}
......@@ -23,6 +23,7 @@ import com.xabber.android.data.NetworkException;
import com.xabber.android.data.OnLoadListener;
import com.xabber.android.data.OnWipeListener;
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.ProxyType;
import com.xabber.android.data.connection.TLSMode;
......@@ -113,40 +114,35 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
private AccountManager() {
this.application = Application.getInstance();
accountItems = new HashMap<String, AccountItem>();
enabledAccounts = new HashSet<String>();
savedStatuses = new ArrayList<SavedStatus>();
authorizationErrorProvider = new BaseAccountNotificationProvider<AccountAuthorizationError>(
R.drawable.ic_stat_error);
passwordRequestProvider = new BaseAccountNotificationProvider<PasswordRequest>(
R.drawable.ic_stat_ic_add_circle);
TypedArray accountAvatars = application.getResources()
.obtainTypedArray(R.array.account_avatars);
accountItems = new HashMap<>();
enabledAccounts = new HashSet<>();
savedStatuses = new ArrayList<>();
authorizationErrorProvider = new BaseAccountNotificationProvider<>(R.drawable.ic_stat_error);
passwordRequestProvider = new BaseAccountNotificationProvider<>(R.drawable.ic_stat_ic_add_circle);
TypedArray accountAvatars = application.getResources().obtainTypedArray(R.array.account_avatars);
colors = accountAvatars.length();
accountAvatars.recycle();
TypedArray types = application.getResources().obtainTypedArray(
R.array.account_types);
accountTypes = new ArrayList<AccountType>();
TypedArray types = application.getResources().obtainTypedArray(R.array.account_types);
accountTypes = new ArrayList<>();
for (int index = 0; index < types.length(); index++) {
int id = types.getResourceId(index, 0);
TypedArray values = application.getResources().obtainTypedArray(id);
AccountProtocol protocol = AccountProtocol.valueOf(values
.getString(0));
AccountProtocol protocol = AccountProtocol.valueOf(values.getString(0));
if (Build.VERSION.SDK_INT < 8 && protocol == AccountProtocol.wlm) {
values.recycle();
continue;
}
ArrayList<String> servers = new ArrayList<String>();
ArrayList<String> servers = new ArrayList<>();
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));
}
accountTypes.add(new AccountType(id, protocol, values.getString(1),
values.getString(2), values.getString(3), values
.getDrawable(4), values.getBoolean(5, false),
values.getString(6), values.getInt(7, 5222), values
.getBoolean(8, false), servers));
values.getString(2), values.getString(3), values.getDrawable(4),
values.getBoolean(5, false), values.getString(6), values.getInt(7, 5222),
values.getBoolean(8, false), servers));
values.recycle();
}
types.recycle();
......@@ -156,15 +152,14 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
@Override
public void onLoad() {
final Collection<SavedStatus> savedStatuses = new ArrayList<SavedStatus>();
final Collection<AccountItem> accountItems = new ArrayList<AccountItem>();
final Collection<SavedStatus> savedStatuses = new ArrayList<>();
final Collection<AccountItem> accountItems = new ArrayList<>();
Cursor cursor = StatusTable.getInstance().list();
try {
if (cursor.moveToFirst()) {
do {
savedStatuses.add(new SavedStatus(StatusTable
.getStatusMode(cursor), StatusTable
.getStatusText(cursor)));
savedStatuses.add(new SavedStatus(StatusTable.getStatusMode(cursor),
StatusTable.getStatusText(cursor)));
} while (cursor.moveToNext());
}
} finally {
......@@ -218,28 +213,28 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
});
}
private void onLoaded(Collection<SavedStatus> savedStatuses,
Collection<AccountItem> accountItems) {
private void onLoaded(Collection<SavedStatus> savedStatuses, Collection<AccountItem> accountItems) {
this.savedStatuses.addAll(savedStatuses);
for (AccountItem accountItem : accountItems)
for (AccountItem accountItem : accountItems) {
addAccount(accountItem);
NotificationManager.getInstance().registerNotificationProvider(
authorizationErrorProvider);
NotificationManager.getInstance().registerNotificationProvider(
passwordRequestProvider);
}
NotificationManager.getInstance().registerNotificationProvider(authorizationErrorProvider);
NotificationManager.getInstance().registerNotificationProvider(passwordRequestProvider);
}
private void addAccount(AccountItem accountItem) {
accountItems.put(accountItem.getAccount(), accountItem);
if (accountItem.isEnabled())
if (accountItem.isEnabled()) {
enabledAccounts.add(accountItem.getAccount());
for (OnAccountAddedListener listener : application
.getManagers(OnAccountAddedListener.class))
}
for (OnAccountAddedListener listener : application.getManagers(OnAccountAddedListener.class)) {
listener.onAccountAdded(accountItem);
}
if (accountItem.isEnabled()) {
onAccountEnabled(accountItem);
if (accountItem.getRawStatusMode().isOnline())
if (accountItem.getRawStatusMode().isOnline()) {
onAccountOnline(accountItem);
}
}
onAccountChanged(accountItem.getAccount());
}
......@@ -256,13 +251,16 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/
int getNextColorIndex() {
int[] count = new int[colors];
for (AccountItem accountItem : accountItems.values())
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)
for (int index = 0; index < count.length; index++) {
if (count[index] < value) {
result = index;
}
}
return result;
}
......@@ -280,55 +278,10 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param accountItem
*/
void requestToWriteAccount(final AccountItem accountItem) {
final AccountProtocol protocol = accountItem.getConnectionSettings()
.getProtocol();
final boolean custom = accountItem.getConnectionSettings().isCustom();
final String host = accountItem.getConnectionSettings().getHost();
final int port = accountItem.getConnectionSettings().getPort();
final String serverName = accountItem.getConnectionSettings()
.getServerName();
final String userName = accountItem.getConnectionSettings()
.getUserName();
final String resource = accountItem.getConnectionSettings()
.getResource();
final boolean storePassword = accountItem.isStorePassword();
final String password = accountItem.getConnectionSettings()
.getPassword();
final int colorIndex = accountItem.getColorIndex();
final int priority = accountItem.getPriority();
final StatusMode statusMode = accountItem.getRawStatusMode();
final String statusText = accountItem.getStatusText();
final boolean enabled = accountItem.isEnabled();
final boolean saslEnabled = accountItem.getConnectionSettings()
.isSaslEnabled();
final TLSMode tlsMode = accountItem.getConnectionSettings()
.getTlsMode();
final boolean compression = accountItem.getConnectionSettings()
.useCompression();
final ProxyType proxyType = accountItem.getConnectionSettings()
.getProxyType();
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 KeyPair keyPair = accountItem.getKeyPair();
final Date lastSync = accountItem.getLastSync();
final ArchiveMode archiveMode = accountItem.getArchiveMode();
Application.getInstance().runInBackground(new Runnable() {
@Override
public void run() {
accountItem.setId(AccountTable.getInstance().write(
accountItem.getId(), protocol, custom, host, port,
serverName, userName, resource, storePassword,
password, colorIndex, priority, statusMode, statusText,
enabled, saslEnabled, tlsMode, compression, proxyType,
proxyHost, proxyPort, proxyUser, proxyPassword,
syncable, keyPair, lastSync, archiveMode));
accountItem.setId(AccountTable.getInstance().write(accountItem.getId(), accountItem));
}
});
}
......@@ -336,26 +289,25 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
/**
* Creates new account and starts connection.
*/
private AccountItem addAccount(AccountProtocol protocol, boolean custom,
String host, int port, String serverName, String userName,
boolean storePassword, String password, String resource, int color,
int priority, StatusMode statusMode, String statusText,
boolean enabled, boolean saslEnabled, TLSMode tlsMode,
boolean compression, ProxyType proxyType, String proxyHost,
int proxyPort, String proxyUser, String proxyPassword,
boolean syncable, KeyPair keyPair, Date lastSync,
ArchiveMode archiveMode,
private AccountItem addAccount(AccountProtocol protocol, boolean custom, String host, int port,
String serverName, String userName, boolean storePassword,
String password, String resource, int color, int priority,
StatusMode statusMode, String statusText, boolean enabled,
boolean saslEnabled, TLSMode tlsMode, boolean compression,
ProxyType proxyType, String proxyHost, int proxyPort,
String proxyUser, String proxyPassword, boolean syncable,
KeyPair keyPair, Date lastSync, ArchiveMode archiveMode,
boolean registerNewAccount) {
AccountItem accountItem = new AccountItem(protocol, custom, host, port,
serverName, userName, resource, storePassword, password, color,
priority, statusMode, statusText, enabled, saslEnabled,
tlsMode, compression, proxyType, proxyHost, proxyPort,
proxyUser, proxyPassword, syncable, keyPair, lastSync,
archiveMode);
if(registerNewAccount) {
// TODO: attempt to register account, if that fails return null;
accountItem.registerAccount();
//return(null);
AccountItem accountItem = new AccountItem(protocol, custom, host, port, serverName, userName,
resource, storePassword, password, color, priority, statusMode, statusText, enabled,
saslEnabled, tlsMode, compression, proxyType, proxyHost, proxyPort, proxyUser,
proxyPassword, syncable, keyPair, lastSync, archiveMode);
if (registerNewAccount) {
// TODO: attempt to register account, if that fails return null;
// accountItem.registerAccount();
// return(null);
}
requestToWriteAccount(accountItem);
addAccount(accountItem);
......@@ -375,37 +327,39 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @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,
boolean registerNewAccount) throws NetworkException {
public String addAccount(String user, String password, AccountType accountType, boolean syncable,
boolean storePassword, boolean useOrbot, boolean registerNewAccount)
throws NetworkException {
if (accountType.getProtocol().isOAuth()) {
int index = 1;
while (true) {
user = String.valueOf(index);
boolean found = false;
for (AccountItem accountItem : accountItems.values())
for (AccountItem accountItem : accountItems.values()) {
if (accountItem.getConnectionSettings().getServerName()
.equals(accountType.getFirstServer())
&& accountItem.getConnectionSettings()
.getUserName().equals(user)) {
&& accountItem.getConnectionSettings().getUserName().equals(user)) {
found = true;
break;
}
if (!found)
}
if (!found) {
break;
}
index++;
}
}
if (user == null)
if (user == null) {
throw new NetworkException(R.string.EMPTY_USER_NAME);
}
if (user.indexOf("@") == -1) {
if ("".equals(accountType.getFirstServer()))
if (!user.contains("@")) {
if ("".equals(accountType.getFirstServer())) {
throw new NetworkException(R.string.EMPTY_SERVER_NAME);
else
} else {
user += "@" + accountType.getFirstServer();
}
}
String serverName = StringUtils.parseServer(user);
......@@ -414,23 +368,26 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
String host = accountType.getHost();
int port = accountType.getPort();
boolean tlsRequired = accountType.isTLSRequired();
if (useOrbot)
if (useOrbot) {
tlsRequired = true;
}
if ("".equals(serverName)) {
throw new NetworkException(R.string.EMPTY_SERVER_NAME);
} else if (!accountType.isAllowServer()
&& !serverName.equals(accountType.getFirstServer()))
} else if (!accountType.isAllowServer() && !serverName.equals(accountType.getFirstServer())) {
throw new NetworkException(R.string.INCORRECT_USER_NAME);
}
if ("".equals(userName))
if ("".equals(userName)) {
throw new NetworkException(R.string.EMPTY_USER_NAME);
if ("".equals(resource))
}
if ("".equals(resource)) {
resource = "android" + StringUtils.randomString(8);
}
if (accountType.getId() == R.array.account_type_xmpp) {
host = serverName;
for (AccountType check : accountTypes)
for (AccountType check : accountTypes) {
if (check.getServers().contains(serverName)) {
accountType = check;
host = check.getHost();
......@@ -438,30 +395,30 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
tlsRequired = check.isTLSRequired();
break;
}
}
}
AccountItem accountItem;
for (; ; ) {
if (getAccount(userName + '@' + serverName + '/' + resource) == null)
while(true) {
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,
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, registerNewAccount);
if(accountItem == null) {
if (accountItem == null) {
throw new NetworkException(R.string.ACCOUNT_REGISTER_FAILED);
}
onAccountChanged(accountItem.getAccount());
if (accountItems.size() > 1
&& SettingsManager.contactsEnableShowAccounts())
if (accountItems.size() > 1 && SettingsManager.contactsEnableShowAccounts()) {
SettingsManager.enableContactsShowAccount();
}
return accountItem.getAccount();
}
......@@ -476,8 +433,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
accountItem.setEnabled(false);
accountItem.updateConnection(true);
if (wasEnabled) {
if (accountItem.getRawStatusMode().isOnline())
if (accountItem.getRawStatusMode().isOnline()) {
onAccountOffline(accountItem);
}
onAccountDisabled(accountItem);
}
Application.getInstance().runInBackground(new Runnable() {
......@@ -488,9 +446,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
});
accountItems.remove(account);
enabledAccounts.remove(account);
for (OnAccountRemovedListener listener : application
.getManagers(OnAccountRemovedListener.class))
for (OnAccountRemovedListener listener : application.getManagers(OnAccountRemovedListener.class)) {
listener.onAccountRemoved(accountItem);
}
removeAuthorizationError(account);
}
......@@ -527,50 +485,45 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param syncable
* @param archiveMode
*/
public void updateAccount(String account, boolean custom, String host,
int port, String serverName, String userName,
boolean storePassword, String password, String resource,
int priority, boolean enabled, boolean saslEnabled,
TLSMode tlsMode, boolean compression, ProxyType proxyType,
String proxyHost, int proxyPort, String proxyUser,
String proxyPassword, boolean syncable, ArchiveMode archiveMode) {
public void updateAccount(String account, boolean custom, String host, int port, String serverName,
String userName, boolean storePassword, String password, String resource,
int priority, boolean enabled, boolean saslEnabled, TLSMode tlsMode,
boolean compression, ProxyType proxyType, String proxyHost, int proxyPort,
String proxyUser, String proxyPassword, boolean syncable,
ArchiveMode archiveMode, int colorIndex) {
AccountItem result;
AccountItem accountItem = getAccount(account);
if (accountItem.getConnectionSettings().getServerName()
.equals(serverName)
&& accountItem.getConnectionSettings().getUserName()
.equals(userName)
&& accountItem.getConnectionSettings().getResource()
.equals(resource)) {
if (accountItem.getConnectionSettings().getServerName().equals(serverName)
&& accountItem.getConnectionSettings().getUserName().equals(userName)
&& accountItem.getConnectionSettings().getResource().equals(resource)) {
result = accountItem;
result.setColorIndex(colorIndex);
boolean reconnect = false;
if (accountItem.getConnectionSettings().isCustom() != custom
|| !accountItem.getConnectionSettings().getHost()
.equals(host)
|| !accountItem.getConnectionSettings().getHost().equals(host)
|| accountItem.getConnectionSettings().getPort() != port
|| !accountItem.getConnectionSettings().getPassword()
.equals(password)
|| !accountItem.getConnectionSettings().getPassword().equals(password)
|| accountItem.getConnectionSettings().getTlsMode() != tlsMode
|| accountItem.getConnectionSettings().isSaslEnabled() != saslEnabled
|| accountItem.getConnectionSettings().useCompression() != compression
|| accountItem.getConnectionSettings().getProxyType() != proxyType
|| !accountItem.getConnectionSettings().getProxyHost()
.equals(proxyHost)
|| !accountItem.getConnectionSettings().getProxyHost().equals(proxyHost)
|| accountItem.getConnectionSettings().getProxyPort() != proxyPort
|| !accountItem.getConnectionSettings().getProxyUser()
.equals(proxyUser)
|| !accountItem.getConnectionSettings().getProxyPassword()
.equals(proxyPassword)) {
result.updateConnectionSettings(custom, host, port, password,
saslEnabled, tlsMode, compression, proxyType,
proxyHost, proxyPort, proxyUser, proxyPassword);
|| !accountItem.getConnectionSettings().getProxyUser().equals(proxyUser)
|| !accountItem.getConnectionSettings().getProxyPassword().equals(proxyPassword)) {
result.updateConnectionSettings(custom, host, port, password, saslEnabled, tlsMode,
compression, proxyType, proxyHost, proxyPort, proxyUser, proxyPassword);
reconnect = true;
}
if (result.isSyncable() != syncable) {
result.setSyncable(syncable);
for (OnAccountSyncableChangedListener listener : application
.getManagers(OnAccountSyncableChangedListener.class))
for (OnAccountSyncableChangedListener listener :
application.getManagers(OnAccountSyncableChangedListener.class)) {
listener.onAccountSyncableChanged(result);
}
}
result.setStorePassword(storePassword);
boolean changed = result.isEnabled() != enabled;
......@@ -585,15 +538,17 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
if (result.getArchiveMode() != archiveMode) {
reconnect = (result.getArchiveMode() == ArchiveMode.server) != (archiveMode == ArchiveMode.server);
result.setArchiveMode(archiveMode);
for (OnAccountArchiveModeChangedListener listener : application
.getManagers(OnAccountArchiveModeChangedListener.class))
for (OnAccountArchiveModeChangedListener listener :
application.getManagers(OnAccountArchiveModeChangedListener.class)) {
listener.onAccountArchiveModeChanged(result);
}
}
if (changed && enabled) {
enabledAccounts.add(account);
onAccountEnabled(result);
if (result.getRawStatusMode().isOnline())
if (result.getRawStatusMode().isOnline()) {
onAccountOnline(result);
}
}
if (changed || reconnect) {
result.updateConnection(true);
......@@ -601,26 +556,23 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
}
if (changed && !enabled) {
enabledAccounts.remove(account);
if (result.getRawStatusMode().isOnline())
if (result.getRawStatusMode().isOnline()) {
onAccountOffline(result);
}
onAccountDisabled(result);
}
requestToWriteAccount(result);
} else {
int colorIndex = accountItem.getColorIndex();
StatusMode statusMode = accountItem.getRawStatusMode();
String statusText = accountItem.getStatusText();
AccountProtocol protocol = accountItem.getConnectionSettings()
.getProtocol();
AccountProtocol protocol = accountItem.getConnectionSettings().getProtocol();
KeyPair keyPair = accountItem.getKeyPair();
Date lastSync = accountItem.getLastSync();
removeAccountWithoutCallback(account);
result = addAccount(protocol, custom, host, port, serverName,
userName, storePassword, password, resource, colorIndex,
priority, statusMode, statusText, enabled, saslEnabled,
tlsMode, compression, proxyType, proxyHost, proxyPort,
proxyUser, proxyPassword, syncable, keyPair, lastSync,
archiveMode, false);
result = addAccount(protocol, custom, host, port, serverName, userName, storePassword,
password, resource, colorIndex, priority, statusMode, statusText, enabled,
saslEnabled, tlsMode, compression, proxyType, proxyHost, proxyPort, proxyUser,
proxyPassword, syncable, keyPair, lastSync, archiveMode, false);
}
onAccountChanged(result.getAccount());
}
......@@ -639,75 +591,96 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
public void setSyncable(String account, boolean syncable) {
AccountItem accountItem = getAccount(account);
updateAccount(account, accountItem.getConnectionSettings().isCustom(),
accountItem.getConnectionSettings().getHost(), accountItem
.getConnectionSettings().getPort(), accountItem
.getConnectionSettings().getServerName(), accountItem
.getConnectionSettings().getUserName(),
accountItem.isStorePassword(), accountItem
.getConnectionSettings().getPassword(), accountItem
.getConnectionSettings().getResource(),
accountItem.getPriority(), accountItem.isEnabled(), accountItem
.getConnectionSettings().isSaslEnabled(), accountItem
.getConnectionSettings().getTlsMode(), accountItem
.getConnectionSettings().useCompression(), accountItem
.getConnectionSettings().getProxyType(), accountItem
.getConnectionSettings().getProxyHost(), accountItem
.getConnectionSettings().getProxyPort(), accountItem
.getConnectionSettings().getProxyUser(), accountItem
.getConnectionSettings().getProxyPassword(), syncable,
accountItem.getArchiveMode());
ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
updateAccount(
account,
connectionSettings.isCustom(),
connectionSettings.getHost(),
connectionSettings.getPort(),
connectionSettings.getServerName(),
connectionSettings.getUserName(),
accountItem.isStorePassword(),
connectionSettings.getPassword(),
connectionSettings.getResource(),
accountItem.getPriority(),
accountItem.isEnabled(),
connectionSettings.isSaslEnabled(),
connectionSettings.getTlsMode(),
connectionSettings.useCompression(),
connectionSettings.getProxyType(),
connectionSettings.getProxyHost(),
connectionSettings.getProxyPort(),
connectionSettings.getProxyUser(),
connectionSettings.getProxyPassword(),
syncable,
accountItem.getArchiveMode(),
accountItem.getColorIndex()
);
}
public void setPassword(String account, boolean storePassword,
String password) {
public void setPassword(String account, boolean storePassword, String password) {
AccountItem accountItem = getAccount(account);
updateAccount(account, accountItem.getConnectionSettings().isCustom(),
accountItem.getConnectionSettings().getHost(), accountItem
.getConnectionSettings().getPort(), accountItem
.getConnectionSettings().getServerName(), accountItem
.getConnectionSettings().getUserName(), storePassword,
password, accountItem.getConnectionSettings().getResource(),
accountItem.getPriority(), accountItem.isEnabled(), accountItem
.getConnectionSettings().isSaslEnabled(), accountItem
.getConnectionSettings().getTlsMode(), accountItem
.getConnectionSettings().useCompression(), accountItem
.getConnectionSettings().getProxyType(), accountItem
.getConnectionSettings().getProxyHost(), accountItem
.getConnectionSettings().getProxyPort(), accountItem
.getConnectionSettings().getProxyUser(), accountItem
.getConnectionSettings().getProxyPassword(),
accountItem.isSyncable(), accountItem.getArchiveMode());
ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
updateAccount(
account,
connectionSettings.isCustom(),
connectionSettings.getHost(),
connectionSettings.getPort(),
connectionSettings.getServerName(),
connectionSettings.getUserName(),
storePassword,
password,
connectionSettings.getResource(),
accountItem.getPriority(),
accountItem.isEnabled(),
connectionSettings.isSaslEnabled(),
connectionSettings.getTlsMode(),
connectionSettings.useCompression(),
connectionSettings.getProxyType(),
connectionSettings.getProxyHost(),
connectionSettings.getProxyPort(),
connectionSettings.getProxyUser(),
connectionSettings.getProxyPassword(),
accountItem.isSyncable(),
accountItem.getArchiveMode(),
accountItem.getColorIndex()
);
}
public void setArchiveMode(String account, ArchiveMode archiveMode) {
AccountItem accountItem = AccountManager.getInstance().getAccount(
account);
AccountManager.getInstance().updateAccount(account,
accountItem.getConnectionSettings().isCustom(),
accountItem.getConnectionSettings().getHost(),
accountItem.getConnectionSettings().getPort(),
accountItem.getConnectionSettings().getServerName(),
accountItem.getConnectionSettings().getUserName(),
AccountItem accountItem = AccountManager.getInstance().getAccount(account);
ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
AccountManager.getInstance().updateAccount(
account,
connectionSettings.isCustom(),
connectionSettings.getHost(),
connectionSettings.getPort(),
connectionSettings.getServerName(),
connectionSettings.getUserName(),
accountItem.isStorePassword(),
accountItem.getConnectionSettings().getPassword(),
accountItem.getConnectionSettings().getResource(),
accountItem.getPriority(), accountItem.isEnabled(),
accountItem.getConnectionSettings().isSaslEnabled(),
accountItem.getConnectionSettings().getTlsMode(),
accountItem.getConnectionSettings().useCompression(),
accountItem.getConnectionSettings().getProxyType(),
accountItem.getConnectionSettings().getProxyHost(),
accountItem.getConnectionSettings().getProxyPort(),
accountItem.getConnectionSettings().getProxyUser(),
accountItem.getConnectionSettings().getProxyPassword(),
accountItem.isSyncable(), archiveMode);
connectionSettings.getPassword(),
connectionSettings.getResource(),
accountItem.getPriority(),
accountItem.isEnabled(),
connectionSettings.isSaslEnabled(),
connectionSettings.getTlsMode(),
connectionSettings.useCompression(),
connectionSettings.getProxyType(),
connectionSettings.getProxyHost(),
connectionSettings.getProxyPort(),
connectionSettings.getProxyUser(),
connectionSettings.getProxyPassword(),
accountItem.isSyncable(),
archiveMode,
accountItem.getColorIndex()
);
}
public ArchiveMode getArchiveMode(String account) {
AccountItem accountItem = getAccount(account);
if (accountItem == null)
if (accountItem == null) {
return ArchiveMode.available;
}
return accountItem.getArchiveMode();
}
......@@ -735,34 +708,41 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
for (AccountItem accountItem : accountItems.values()) {
ConnectionState state = accountItem.getState();
if (state == ConnectionState.connected)
if (state == ConnectionState.connected) {
online = true;
if (RosterManager.getInstance().isRosterReceived(
accountItem.getAccount()))
}
if (RosterManager.getInstance().isRosterReceived(accountItem.getAccount())) {
roster = true;
if (state == ConnectionState.connecting
|| state == ConnectionState.authentication)
}
if (state == ConnectionState.connecting || state == ConnectionState.authentication) {
connecting = true;
if (state == ConnectionState.waiting)
}
if (state == ConnectionState.waiting) {
waiting = true;
if (accountItem.isEnabled())
}
if (accountItem.isEnabled()) {
offline = true;
}
disabled = true;
}
if (online)
if (online) {
return CommonState.online;
else if (roster)
} else if (roster) {
return CommonState.roster;
else if (connecting)
} else if (connecting) {
return CommonState.connecting;
if (waiting)
}
if (waiting) {
return CommonState.waiting;
else if (offline)
} else if (offline) {
return CommonState.offline;
else if (disabled)
} else if (disabled) {
return CommonState.disabled;
else
} else {
return CommonState.empty;
}
}
/**
......@@ -772,12 +752,16 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
public int getColorLevel(String account) {
AccountItem accountItem = getAccount(account);
int colorIndex;
if (accountItem == null)
if (accountItem == null) {
return 0;
else
} else {
colorIndex = accountItem.getColorIndex() % colors;
if (colorIndex < 0)
}
if (colorIndex < 0) {
colorIndex += colors;
}
return colorIndex;
}
......@@ -790,21 +774,23 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
private boolean hasSameBareAddress(String account) {
String bareAddress = Jid.getBareAddress(account);
for (AccountItem check : accountItems.values())
for (AccountItem check : accountItems.values()) {
if (!check.getAccount().equals(account)
&& Jid.getBareAddress(check.getAccount()).equals(
bareAddress))
&& Jid.getBareAddress(check.getAccount()).equals(bareAddress)) {
return true;
}
}
return false;
}
private boolean hasSameProtocol(String account) {
AccountProtocol protocol = getAccount(account).getConnectionSettings()
.getProtocol();
for (AccountItem check : accountItems.values())
AccountProtocol protocol = getAccount(account).getConnectionSettings().getProtocol();
for (AccountItem check : accountItems.values()) {
if (!check.getAccount().equals(account)
&& check.getConnectionSettings().getProtocol() == protocol)
&& check.getConnectionSettings().getProtocol() == protocol) {
return true;
}
}
return false;
}
......@@ -814,32 +800,32 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/
public String getVerboseName(String account) {
AccountItem accountItem = getAccount(account);
if (accountItem == null)
if (accountItem == null) {
return account;
}
if (accountItem.getConnectionSettings().getProtocol().isOAuth()) {
String jid = OAuthManager.getInstance().getAssignedJid(account);
AccountProtocol accountProtocol = accountItem
.getConnectionSettings().getProtocol();
AccountProtocol accountProtocol = accountItem.getConnectionSettings().getProtocol();
String name;
if (jid == null) {
if (hasSameProtocol(account))
if (hasSameProtocol(account)) {
name = accountItem.getConnectionSettings().getUserName();
else
return application.getString(accountProtocol
.getNameResource());
} else {
return application.getString(accountProtocol.getNameResource());
}
} else {
name = Jid.getBareAddress(jid);
if (!hasSameBareAddress(jid))
if (!hasSameBareAddress(jid)) {
return name;
}
}
return application.getString(accountProtocol.getShortResource())
+ " - " + name;
return application.getString(accountProtocol.getShortResource()) + " - " + name;
} else {
if (hasSameBareAddress(account))
if (hasSameBareAddress(account)) {
return account;
else
} else {
return Jid.getBareAddress(account);
}
}
}
......@@ -850,12 +836,12 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/
public String getNickName(String account) {
String jid = OAuthManager.getInstance().getAssignedJid(account);
String result = VCardManager.getInstance().getName(
Jid.getBareAddress(jid));
if ("".equals(result))
String result = VCardManager.getInstance().getName(Jid.getBareAddress(jid));
if ("".equals(result)) {
return getVerboseName(account);
else
} else {
return result;
}
}
/**
......@@ -865,8 +851,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param statusMode
* @param statusText
*/
public void setStatus(String account, StatusMode statusMode,
String statusText) {
public void setStatus(String account, StatusMode statusMode, String statusText) {
addSavedStatus(statusMode, statusText);
AccountItem accountItem = getAccount(account);
setStatus(accountItem, statusMode, statusText);
......@@ -875,24 +860,25 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
} catch (NetworkException e) {
}
boolean found = false;
for (AccountItem check : accountItems.values())
if (check.isEnabled()
&& SettingsManager.statusMode() == check.getRawStatusMode()) {
for (AccountItem check : accountItems.values()) {
if (check.isEnabled() && SettingsManager.statusMode() == check.getRawStatusMode()) {
found = true;
break;
}
if (!found)
}
if (!found) {
SettingsManager.setStatusMode(statusMode);
}
found = false;
for (AccountItem check : accountItems.values())
if (check.isEnabled()
&& SettingsManager.statusText().equals(
check.getStatusText())) {
for (AccountItem check : accountItems.values()) {
if (check.isEnabled() && SettingsManager.statusText().equals(check.getStatusText())) {
found = true;
break;
}
if (!found)
}
if (!found) {
SettingsManager.setStatusText(statusText);
}
onAccountChanged(account);
}
......@@ -910,8 +896,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* If we are already away or xa, do nothing.
*/
public void goAway() {
if (away || xa)
if (away || xa) {
return;
}
away = true;
resendPresence();
}
......@@ -922,8 +909,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* If we are already xa, do nothing.
*/
public void goXa() {
if (xa)
if (xa) {
return;
}
xa = true;
resendPresence();
}
......@@ -934,8 +922,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* If we are already waked up, do nothing.
*/
public void wakeUp() {
if (!away && !xa)
if (!away && !xa) {
return;
}
away = false;
xa = false;
resendPresence();
......@@ -945,12 +934,12 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* Sends new presence information for all accounts.
*/
public void resendPresence() {
for (AccountItem accountItem : accountItems.values())
for (AccountItem accountItem : accountItems.values()) {
try {
PresenceManager.getInstance().resendPresence(
accountItem.getAccount());
PresenceManager.getInstance().resendPresence(accountItem.getAccount());
} catch (NetworkException e) {
}
}
}
/**
......@@ -960,17 +949,17 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param statusMode
* @param statusText
*/
private void setStatus(AccountItem accountItem, StatusMode statusMode,
String statusText) {
private void setStatus(AccountItem accountItem, StatusMode statusMode, String statusText) {
boolean changed = accountItem.isEnabled()
&& accountItem.getRawStatusMode().isOnline() != statusMode
.isOnline();
&& accountItem.getRawStatusMode().isOnline() != statusMode.isOnline();
accountItem.setStatus(statusMode, statusText);
if (changed && statusMode.isOnline())
if (changed && statusMode.isOnline()) {
onAccountOnline(accountItem);
}
accountItem.updateConnection(true);
if (changed && !statusMode.isOnline())
if (changed && !statusMode.isOnline()) {
onAccountOffline(accountItem);
}
requestToWriteAccount(accountItem);
}
......@@ -982,18 +971,19 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/
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);
statusText == null ? accountItem.getStatusText() : statusText);
}
resendPresence();
onAccountsChanged(new ArrayList<String>(AccountManager.getInstance()
.getAllAccounts()));
onAccountsChanged(new ArrayList<>(AccountManager.getInstance().getAllAccounts()));
}
/**
......@@ -1002,11 +992,11 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param statusMode
* @param statusText
*/
private void addSavedStatus(final StatusMode statusMode,
final String statusText) {
private void addSavedStatus(final StatusMode statusMode, final String statusText) {
SavedStatus savedStatus = new SavedStatus(statusMode, statusText);
if (savedStatuses.contains(savedStatus))
if (savedStatuses.contains(savedStatus)) {
return;
}
savedStatuses.add(savedStatus);
Application.getInstance().runInBackground(new Runnable() {
@Override
......@@ -1023,8 +1013,9 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* @param statusText
*/
public void removeSavedStatus(final SavedStatus savedStatus) {
if (!savedStatuses.remove(savedStatus))
if (!savedStatuses.remove(savedStatus)) {
return;
}
Application.getInstance().runInBackground(new Runnable() {
@Override
public void run() {
......@@ -1063,11 +1054,13 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
* </ul>
*/
public String getSelectedAccount() {
if (SettingsManager.contactsShowAccounts())
if (SettingsManager.contactsShowAccounts()) {
return null;
}
String selected = SettingsManager.contactsSelectedAccount();
if (enabledAccounts.contains(selected))
if (enabledAccounts.contains(selected)) {
return selected;
}
return null;
}
......@@ -1076,8 +1069,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
}
public void addAuthenticationError(String account) {
authorizationErrorProvider.add(new AccountAuthorizationError(account),
true);
authorizationErrorProvider.add(new AccountAuthorizationError(account), true);
}
public void removePasswordRequest(String account) {
......@@ -1089,7 +1081,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
}
public void onAccountChanged(String account) {
Collection<String> accounts = new ArrayList<String>(1);
Collection<String> accounts = new ArrayList<>(1);
accounts.add(account);
onAccountsChanged(accounts);
}
......@@ -1098,37 +1090,37 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
Application.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
for (OnAccountChangedListener accountListener : Application
.getInstance().getUIListeners(
OnAccountChangedListener.class))
for (OnAccountChangedListener accountListener
: Application.getInstance().getUIListeners(OnAccountChangedListener.class)) {
accountListener.onAccountsChanged(accounts);
}
}
});
}
public void onAccountEnabled(AccountItem accountItem) {
for (OnAccountEnabledListener listener : application
.getManagers(OnAccountEnabledListener.class))
for (OnAccountEnabledListener listener : application.getManagers(OnAccountEnabledListener.class)) {
listener.onAccountEnabled(accountItem);
}
}
public void onAccountOnline(AccountItem accountItem) {
for (OnAccountOnlineListener listener : application
.getManagers(OnAccountOnlineListener.class))
for (OnAccountOnlineListener listener : application.getManagers(OnAccountOnlineListener.class)) {
listener.onAccountOnline(accountItem);
}
}
public void onAccountOffline(AccountItem accountItem) {
accountItem.clearPassword();
for (OnAccountOfflineListener listener : application
.getManagers(OnAccountOfflineListener.class))
for (OnAccountOfflineListener listener : application.getManagers(OnAccountOfflineListener.class)) {
listener.onAccountOffline(accountItem);
}
}
public void onAccountDisabled(AccountItem accountItem) {
for (OnAccountDisabledListener listener : application
.getManagers(OnAccountDisabledListener.class))
for (OnAccountDisabledListener listener : application.getManagers(OnAccountDisabledListener.class)) {
listener.onAccountDisabled(accountItem);
}
}
@Override
......@@ -1136,4 +1128,4 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
AccountTable.getInstance().wipe();
}
}
}
\ No newline at end of file
......@@ -14,16 +14,6 @@
*/
package com.xabber.android.data.account;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Date;
import android.content.ContentValues;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
......@@ -34,10 +24,21 @@ import android.provider.BaseColumns;
import com.xabber.android.data.AbstractTable;
import com.xabber.android.data.Application;
import com.xabber.android.data.DatabaseManager;
import com.xabber.android.data.connection.ConnectionSettings;
import com.xabber.android.data.connection.ProxyType;
import com.xabber.android.data.connection.TLSMode;
import com.xabber.androiddev.R;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Date;
/**
* Storage with account settings.
*
......@@ -181,19 +182,16 @@ class AccountTable extends AbstractTable {
DatabaseManager.execSQL(db, sql);
break;
case 34:
long count = db.compileStatement("SELECT COUNT(*) FROM accounts;")
.simpleQueryForLong();
long count = db.compileStatement("SELECT COUNT(*) FROM accounts;").simpleQueryForLong();
Editor editor = PreferenceManager.getDefaultSharedPreferences(
Application.getInstance().getBaseContext()).edit();
if (count < 2)
editor.putBoolean(
Application.getInstance().getString(
R.string.contacts_show_accounts_key), false);
else
editor.putBoolean(
Application.getInstance().getString(
R.string.contacts_enable_show_accounts_key),
false);
if (count < 2) {
editor.putBoolean(Application.getInstance().getString(
R.string.contacts_show_accounts_key), false);
} else {
editor.putBoolean(Application.getInstance().getString(
R.string.contacts_enable_show_accounts_key), false);
}
editor.commit();
break;
case 36:
......@@ -266,12 +264,17 @@ class AccountTable extends AbstractTable {
String value = PreferenceManager.getDefaultSharedPreferences(
Application.getInstance().getBaseContext()).getString(
"chats_history", "all");
if ("all".equals(value))
archiveMode = ArchiveMode.available;
else if ("unread".equals(value))
archiveMode = ArchiveMode.unreadOnly;
else
archiveMode = ArchiveMode.dontStore;
switch (value) {
case "all":
archiveMode = ArchiveMode.available;
break;
case "unread":
archiveMode = ArchiveMode.unreadOnly;
break;
default:
archiveMode = ArchiveMode.dontStore;
break;
}
sql = "UPDATE accounts SET archive_mode = " + archiveMode.ordinal()
+ ";";
DatabaseManager.execSQL(db, sql);
......@@ -299,71 +302,65 @@ class AccountTable extends AbstractTable {
}
}
/**
* Adds or updates account.
*
* @return Assigned id.
*/
long write(Long id, AccountProtocol protocol, boolean custom, String host,
int port, String serverName, String userName, String resource,
boolean storePassword, String password, int colorIndex,
int priority, StatusMode statusMode, String statusText,
boolean enabled, boolean saslEnabled, TLSMode tlsMode,
boolean compression, ProxyType proxyType, String proxyHost,
int proxyPort, String proxyUser, String proxyPassword,
boolean syncable, KeyPair keyPair, Date lastSync,
ArchiveMode archiveMode) {
public long write(Long id, AccountItem accountItem) {
ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
ContentValues values = new ContentValues();
values.put(Fields.PROTOCOL, protocol.name());
values.put(Fields.CUSTOM, custom ? 1 : 0);
values.put(Fields.HOST, host);
values.put(Fields.PORT, port);
values.put(Fields.SERVER_NAME, serverName);
values.put(Fields.USER_NAME, userName);
if (!storePassword)
values.put(Fields.PROTOCOL, connectionSettings.getProtocol().name());
values.put(Fields.CUSTOM, connectionSettings.isCustom() ? 1 : 0);
values.put(Fields.HOST, connectionSettings.getHost());
values.put(Fields.PORT, connectionSettings.getPort());
values.put(Fields.SERVER_NAME, connectionSettings.getServerName());
values.put(Fields.USER_NAME, connectionSettings.getUserName());
String password = connectionSettings.getPassword();
if (!accountItem.isStorePassword()) {
password = AccountItem.UNDEFINED_PASSWORD;
}
values.put(Fields.PASSWORD, password);
values.put(Fields.RESOURCE, resource);
values.put(Fields.COLOR_INDEX, colorIndex);
values.put(Fields.PRIORITY, priority);
values.put(Fields.STATUS_MODE, statusMode.ordinal());
values.put(Fields.STATUS_TEXT, statusText);
values.put(Fields.ENABLED, enabled ? 1 : 0);
values.put(Fields.SASL_ENABLED, saslEnabled ? 1 : 0);
values.put(Fields.TLS_MODE, tlsMode.ordinal());
values.put(Fields.COMPRESSION, compression ? 1 : 0);
values.put(Fields.PROXY_TYPE, proxyType.ordinal());
values.put(Fields.PROXY_HOST, proxyHost);
values.put(Fields.PROXY_PORT, proxyPort);
values.put(Fields.PROXY_USER, proxyUser);
values.put(Fields.PROXY_PASSWORD, proxyPassword);
values.put(Fields.SYNCABLE, syncable ? 1 : 0);
values.put(Fields.STORE_PASSWORD, storePassword ? 1 : 0);
values.put(Fields.RESOURCE, connectionSettings.getResource());
values.put(Fields.COLOR_INDEX, accountItem.getColorIndex());
values.put(Fields.PRIORITY, accountItem.getPriority());
values.put(Fields.STATUS_MODE, accountItem.getRawStatusMode().ordinal());
values.put(Fields.STATUS_TEXT, accountItem.getStatusText());
values.put(Fields.ENABLED, accountItem.isEnabled() ? 1 : 0);
values.put(Fields.SASL_ENABLED, connectionSettings.isSaslEnabled() ? 1 : 0);
values.put(Fields.TLS_MODE, connectionSettings.getTlsMode().ordinal());
values.put(Fields.COMPRESSION, connectionSettings.useCompression() ? 1 : 0);
values.put(Fields.PROXY_TYPE, connectionSettings.getProxyType().ordinal());
values.put(Fields.PROXY_HOST, connectionSettings.getProxyHost());
values.put(Fields.PROXY_PORT, connectionSettings.getProxyPort());
values.put(Fields.PROXY_USER, connectionSettings.getProxyUser());
values.put(Fields.PROXY_PASSWORD, connectionSettings.getProxyPassword());
values.put(Fields.SYNCABLE, accountItem.isSyncable() ? 1 : 0);
values.put(Fields.STORE_PASSWORD, accountItem.isStorePassword() ? 1 : 0);
final KeyPair keyPair = accountItem.getKeyPair();
if (keyPair == null) {
values.putNull(Fields.PUBLIC_KEY);
values.putNull(Fields.PRIVATE_KEY);
} else {
PublicKey publicKey = keyPair.getPublic();
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
publicKey.getEncoded());
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
PrivateKey privateKey = keyPair.getPrivate();
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
privateKey.getEncoded());
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
byte[] publicKeyBytes = x509EncodedKeySpec.getEncoded();
byte[] privateKeyBytes = pkcs8EncodedKeySpec.getEncoded();
values.put(Fields.PUBLIC_KEY, publicKeyBytes);
values.put(Fields.PRIVATE_KEY, privateKeyBytes);
}
if (lastSync == null)
if (accountItem.getLastSync() == null) {
values.putNull(Fields.LAST_SYNC);
else
values.put(Fields.LAST_SYNC, lastSync.getTime());
values.put(Fields.ARCHIVE_MODE, archiveMode.ordinal());
} else {
values.put(Fields.LAST_SYNC, accountItem.getLastSync().getTime());
}
values.put(Fields.ARCHIVE_MODE, accountItem.getArchiveMode().ordinal());
SQLiteDatabase db = databaseManager.getWritableDatabase();
if (id == null)
if (id == null) {
return db.insert(NAME, Fields.USER_NAME, values);
db.update(NAME, values, Fields._ID + " = ?",
new String[]{String.valueOf(id)});
}
db.update(NAME, values, Fields._ID + " = ?", new String[]{String.valueOf(id)});
return id;
}
......@@ -375,8 +372,7 @@ class AccountTable extends AbstractTable {
*/
void remove(String account, long id) {
SQLiteDatabase db = databaseManager.getWritableDatabase();
db.delete(NAME, Fields._ID + " = ?",
new String[]{String.valueOf(id)});
db.delete(NAME, Fields._ID + " = ?", new String[]{ String.valueOf(id) });
databaseManager.removeAccount(account);
}
......@@ -404,8 +400,7 @@ class AccountTable extends AbstractTable {
}
static AccountProtocol getProtocol(Cursor cursor) {
return AccountProtocol.valueOf(cursor.getString(cursor
.getColumnIndex(Fields.PROTOCOL)));
return AccountProtocol.valueOf(cursor.getString(cursor.getColumnIndex(Fields.PROTOCOL)));
}
static String getHost(Cursor cursor) {
......@@ -429,8 +424,9 @@ class AccountTable extends AbstractTable {
}
static String getPassword(Cursor cursor) {
if (!isStorePassword(cursor))
if (!isStorePassword(cursor)) {
return AccountItem.UNDEFINED_PASSWORD;
}
return cursor.getString(cursor.getColumnIndex(Fields.PASSWORD));
}
......@@ -447,8 +443,7 @@ class AccountTable extends AbstractTable {
}
static StatusMode getStatusMode(Cursor cursor) {
int statusModeIndex = cursor.getInt(cursor
.getColumnIndex(Fields.STATUS_MODE));
int statusModeIndex = cursor.getInt(cursor.getColumnIndex(Fields.STATUS_MODE));
return StatusMode.values()[statusModeIndex];
}
......@@ -465,8 +460,7 @@ class AccountTable extends AbstractTable {
}
public static TLSMode getTLSMode(Cursor cursor) {
int tlsModeIndex = cursor
.getInt(cursor.getColumnIndex(Fields.TLS_MODE));
int tlsModeIndex = cursor.getInt(cursor.getColumnIndex(Fields.TLS_MODE));
return TLSMode.values()[tlsModeIndex];
}
......@@ -483,11 +477,11 @@ class AccountTable extends AbstractTable {
}
static Date getLastSync(Cursor cursor) {
if (cursor.isNull(cursor.getColumnIndex(Fields.LAST_SYNC)))
if (cursor.isNull(cursor.getColumnIndex(Fields.LAST_SYNC))) {
return null;
else
return new Date(cursor.getLong(cursor
.getColumnIndex(Fields.LAST_SYNC)));
} else {
return new Date(cursor.getLong(cursor.getColumnIndex(Fields.LAST_SYNC)));
}
}
static ArchiveMode getArchiveMode(Cursor cursor) {
......@@ -517,16 +511,13 @@ class AccountTable extends AbstractTable {
}
static KeyPair getKeyPair(Cursor cursor) {
byte[] publicKeyBytes = cursor.getBlob(cursor
.getColumnIndex(Fields.PUBLIC_KEY));
byte[] privateKeyBytes = cursor.getBlob(cursor
.getColumnIndex(Fields.PRIVATE_KEY));
if (privateKeyBytes == null || publicKeyBytes == null)
byte[] publicKeyBytes = cursor.getBlob(cursor.getColumnIndex(Fields.PUBLIC_KEY));
byte[] privateKeyBytes = cursor.getBlob(cursor.getColumnIndex(Fields.PRIVATE_KEY));
if (privateKeyBytes == null || publicKeyBytes == null) {
return null;
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
publicKeyBytes);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
privateKeyBytes);
}
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PublicKey publicKey;
PrivateKey privateKey;
KeyFactory keyFactory;
......@@ -534,9 +525,7 @@ class AccountTable extends AbstractTable {
keyFactory = KeyFactory.getInstance("DSA");
publicKey = keyFactory.generatePublic(publicKeySpec);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e);
}
return new KeyPair(publicKey, privateKey);
......
......@@ -67,7 +67,6 @@ public class AccountEditor extends ManagedActivity implements
getFragmentManager().beginTransaction()
.add(R.id.preferences_activity_container, new AccountEditorFragment()).commit();
} else {
token = savedInstanceState.getString(SAVED_TOKEN);
}
......@@ -92,11 +91,11 @@ public class AccountEditor extends ManagedActivity implements
token = INVALIDATED_TOKEN;
} else {
String value = OAuthActivity.getToken(data);
if (value == null)
Application.getInstance().onError(
R.string.AUTHENTICATION_FAILED);
else
if (value == null) {
Application.getInstance().onError(R.string.AUTHENTICATION_FAILED);
} else {
token = value;
}
}
((AccountEditorFragment) getFragmentManager().findFragmentById(
......@@ -109,9 +108,8 @@ public class AccountEditor extends ManagedActivity implements
@Override
public boolean onPreferenceClick(Preference preference) {
if (getString(R.string.account_oauth_key).equals(preference.getKey())) {
startActivityForResult(OAuthActivity.createIntent(this, accountItem
.getConnectionSettings().getProtocol()),
OAUTH_WML_REQUEST_CODE);
startActivityForResult(OAuthActivity.createIntent(this,
accountItem.getConnectionSettings().getProtocol()), OAUTH_WML_REQUEST_CODE);
return true;
}
return false;
......@@ -120,8 +118,7 @@ public class AccountEditor extends ManagedActivity implements
@Override
protected Dialog onCreateDialog(int id) {
if (id == ORBOT_DIALOG_ID) {
return new OrbotInstallerDialogBuilder(this, ORBOT_DIALOG_ID)
.create();
return new OrbotInstallerDialogBuilder(this, ORBOT_DIALOG_ID).create();
}
return super.onCreateDialog(id);
}
......@@ -131,8 +128,7 @@ public class AccountEditor extends ManagedActivity implements
}
public static Intent createIntent(Context context, String account) {
return new AccountIntentBuilder(context, AccountEditor.class)
.setAccount(account).build();
return new AccountIntentBuilder(context, AccountEditor.class).setAccount(account).build();
}
@Override
......@@ -152,9 +148,8 @@ public class AccountEditor extends ManagedActivity implements
@Override
public void onOAuthClick() {
startActivityForResult(OAuthActivity.createIntent(this, accountItem
.getConnectionSettings().getProtocol()),
OAUTH_WML_REQUEST_CODE);
startActivityForResult(OAuthActivity.createIntent(this,
accountItem.getConnectionSettings().getProtocol()), OAUTH_WML_REQUEST_CODE);
}
@Override
......
......@@ -27,19 +27,19 @@ public class AccountEditorFragment extends BaseSettingsFragment
@Override
protected void onInflate(Bundle savedInstanceState) {
AccountProtocol protocol = mListener.getAccountItem().getConnectionSettings()
.getProtocol();
if (protocol == AccountProtocol.xmpp)
AccountProtocol protocol = mListener.getAccountItem().getConnectionSettings().getProtocol();
if (protocol == AccountProtocol.xmpp) {
addPreferencesFromResource(R.xml.account_editor_xmpp);
else if (protocol == AccountProtocol.gtalk)
} else if (protocol == AccountProtocol.gtalk) {
addPreferencesFromResource(R.xml.account_editor_xmpp);
else if (protocol == AccountProtocol.wlm)
} else if (protocol == AccountProtocol.wlm) {
addPreferencesFromResource(R.xml.account_editor_oauth);
else
} else {
throw new IllegalStateException();
if (!Application.getInstance().isContactsSupported())
getPreferenceScreen().removePreference(
findPreference(getString(R.string.account_syncable_key)));
}
if (!Application.getInstance().isContactsSupported()) {
getPreferenceScreen().removePreference(findPreference(getString(R.string.account_syncable_key)));
}
oauthPreference = findPreference(getString(R.string.account_oauth_key));
if (oauthPreference != null) {
......@@ -57,7 +57,9 @@ public class AccountEditorFragment extends BaseSettingsFragment
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (getString(R.string.account_port_key).equals(preference.getKey()))
String key = preference.getKey();
if (getString(R.string.account_port_key).equals(key)) {
try {
Integer.parseInt((String) newValue);
} catch (NumberFormatException e) {
......@@ -65,45 +67,43 @@ public class AccountEditorFragment extends BaseSettingsFragment
Toast.LENGTH_LONG).show();
return false;
}
if (getString(R.string.account_tls_mode_key)
.equals(preference.getKey())
|| getString(R.string.account_archive_mode_key).equals(
preference.getKey())
|| getString(R.string.account_proxy_type_key).equals(
preference.getKey()))
}
if (getString(R.string.account_tls_mode_key).equals(key)
|| getString(R.string.account_archive_mode_key).equals(key)
|| getString(R.string.account_proxy_type_key).equals(key)
|| getString(R.string.account_color_key).equals(key)) {
preference.setSummary((String) newValue);
else if (!getString(R.string.account_password_key).equals(
preference.getKey())
&& !getString(R.string.account_proxy_password_key).equals(
preference.getKey())
&& !getString(R.string.account_priority_key).equals(
preference.getKey()))
} else if (!getString(R.string.account_password_key).equals(key)
&& !getString(R.string.account_proxy_password_key).equals(key)
&& !getString(R.string.account_priority_key).equals(key)) {
super.onPreferenceChange(preference, newValue);
if (getString(R.string.account_proxy_type_key).equals(
preference.getKey())) {
boolean enabled = !getString(R.string.account_proxy_type_none)
.equals(newValue)
&& !getString(R.string.account_proxy_type_orbot).equals(
newValue);
}
if (getString(R.string.account_proxy_type_key).equals(key)) {
boolean enabled = !getString(R.string.account_proxy_type_none).equals(newValue)
&& !getString(R.string.account_proxy_type_orbot).equals(newValue);
for (int id : new Integer[]{R.string.account_proxy_host_key,
R.string.account_proxy_port_key,
R.string.account_proxy_user_key,
R.string.account_proxy_port_key, R.string.account_proxy_user_key,
R.string.account_proxy_password_key,}) {
Preference proxyPreference = findPreference(getString(id));
if (proxyPreference != null)
if (proxyPreference != null) {
proxyPreference.setEnabled(enabled);
}
}
}
return true;
}
public void onOAuthChange() {
if (oauthPreference == null)
if (oauthPreference == null) {
return;
if (AccountEditor.INVALIDATED_TOKEN.equals(mListener.getToken()))
}
if (AccountEditor.INVALIDATED_TOKEN.equals(mListener.getToken())) {
oauthPreference.setSummary(R.string.account_oauth_invalidated);
else
} else {
oauthPreference.setSummary(R.string.account_oauth_summary);
}
}
@Override
......@@ -120,59 +120,45 @@ public class AccountEditorFragment extends BaseSettingsFragment
Map<String, Object> source = new HashMap<>();
AccountItem accountItem = mListener.getAccountItem();
putValue(source, R.string.account_custom_key, accountItem
.getConnectionSettings().isCustom());
putValue(source, R.string.account_host_key, accountItem
.getConnectionSettings().getHost());
putValue(source, R.string.account_port_key, accountItem
.getConnectionSettings().getPort());
putValue(source, R.string.account_server_key, accountItem
.getConnectionSettings().getServerName());
putValue(source, R.string.account_username_key, accountItem
.getConnectionSettings().getUserName());
putValue(source, R.string.account_store_password_key, accountItem.isStorePassword());
putValue(source, R.string.account_password_key, accountItem
.getConnectionSettings().getPassword());
putValue(source, R.string.account_resource_key, accountItem
.getConnectionSettings().getResource());
putValue(source, R.string.account_priority_key, accountItem.getPriority());
putValue(source, R.string.account_enabled_key, accountItem.isEnabled());
putValue(source, R.string.account_sasl_key, accountItem
.getConnectionSettings().isSaslEnabled());
putValue(source, R.string.account_tls_mode_key,
accountItem.getConnectionSettings().getTlsMode().ordinal());
putValue(source, R.string.account_compression_key, accountItem
.getConnectionSettings().useCompression());
putValue(source, R.string.account_proxy_type_key,
accountItem.getConnectionSettings().getProxyType().ordinal());
putValue(source, R.string.account_proxy_host_key, accountItem
.getConnectionSettings().getProxyHost());
putValue(source, R.string.account_proxy_port_key, accountItem
.getConnectionSettings().getProxyPort());
putValue(source, R.string.account_proxy_user_key, accountItem
.getConnectionSettings().getProxyUser());
putValue(source, R.string.account_proxy_password_key, accountItem
.getConnectionSettings().getProxyPassword());
putValue(source, R.string.account_syncable_key,
accountItem.isSyncable());
putValue(source, R.string.account_archive_mode_key,
accountItem.getArchiveMode().ordinal());
putValue(source, R.string.account_store_password_key, accountItem.isStorePassword());
putValue(source, R.string.account_syncable_key, accountItem.isSyncable());
putValue(source, R.string.account_archive_mode_key, accountItem.getArchiveMode().ordinal());
putValue(source, R.string.account_color_key, accountItem.getColorIndex());
com.xabber.android.data.connection.ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
putValue(source, R.string.account_custom_key, connectionSettings.isCustom());
putValue(source, R.string.account_host_key, connectionSettings.getHost());
putValue(source, R.string.account_port_key, connectionSettings.getPort());
putValue(source, R.string.account_server_key, connectionSettings.getServerName());
putValue(source, R.string.account_username_key, connectionSettings.getUserName());
putValue(source, R.string.account_password_key, connectionSettings.getPassword());
putValue(source, R.string.account_resource_key, connectionSettings.getResource());
putValue(source, R.string.account_sasl_key, connectionSettings.isSaslEnabled());
putValue(source, R.string.account_tls_mode_key, connectionSettings.getTlsMode().ordinal());
putValue(source, R.string.account_compression_key, connectionSettings.useCompression());
putValue(source, R.string.account_proxy_type_key, connectionSettings.getProxyType().ordinal());
putValue(source, R.string.account_proxy_host_key, connectionSettings.getProxyHost());
putValue(source, R.string.account_proxy_port_key, connectionSettings.getProxyPort());
putValue(source, R.string.account_proxy_user_key, connectionSettings.getProxyUser());
putValue(source, R.string.account_proxy_password_key, connectionSettings.getProxyPassword());
return source;
}
@Override
protected Map<String, Object> getPreferences(Map<String, Object> source) {
Map<String, Object> result = super.getPreferences(source);
if (oauthPreference != null)
if (oauthPreference != null) {
putValue(result, R.string.account_password_key, mListener.getAccount());
}
return result;
}
@Override
protected boolean setValues(Map<String, Object> source,
Map<String, Object> result) {
ProxyType proxyType = ProxyType.values()[getInt(result,
R.string.account_proxy_type_key)];
protected boolean setValues(Map<String, Object> source, Map<String, Object> result) {
ProxyType proxyType = ProxyType.values()[getInt(result, R.string.account_proxy_type_key)];
if (proxyType == ProxyType.orbot && !OrbotHelper.isOrbotInstalled()) {
mListener.showOrbotDialog();
return false;
......@@ -198,13 +184,17 @@ public class AccountEditorFragment extends BaseSettingsFragment
getString(result, R.string.account_proxy_user_key),
getString(result, R.string.account_proxy_password_key),
getBoolean(result, R.string.account_syncable_key),
ArchiveMode.values()[getInt(result, R.string.account_archive_mode_key)]);
ArchiveMode.values()[getInt(result, R.string.account_archive_mode_key)],
getInt(result, R.string.account_color_key)
);
return true;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (AccountEditorFragmentInteractionListener) activity;
} catch (ClassCastException e) {
......@@ -221,13 +211,9 @@ public class AccountEditorFragment extends BaseSettingsFragment
public interface AccountEditorFragmentInteractionListener {
public String getAccount();
public AccountItem getAccountItem();
public String getToken();
public void onOAuthClick();
public void showOrbotDialog();
}
}
......@@ -162,4 +162,10 @@
<string name="orbot_required_message">Для использования TOR необходимо установить приложение Orbot и активировать в нём передачу данных. Вы хотите установить его из Google Play?</string>
<string name="orbot_required_title">Установить Orbot?</string>
<string name="ACCOUNT_REGISTER_FAILED">Не удалось зарегистрировать аккаунт на сервере.</string>
<string name="account_color_name_green">Зеленый</string>
<string name="account_color_name_orange">Оранжевый</string>
<string name="account_color_name_red">Красный</string>
<string name="account_color_name_blue">Синий</string>
<string name="account_color">Цвет учётной записи</string>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<resources>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_editor_02.png -->
<string name="account_compression">Use compression</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_editor_01.png -->
......@@ -162,4 +162,10 @@
<string name="orbot_required_message">In order to process using TOR you must have Orbot installed and activated to proxy traffic through it. Would you like to install it from Google Play?</string>
<string name="orbot_required_title">Install Orbot?</string>
<string name="ACCOUNT_REGISTER_FAILED">Failed to register account on the server.</string>
<string name="account_color_name_green">Green</string>
<string name="account_color_name_orange">Orange</string>
<string name="account_color_name_red">Red</string>
<string name="account_color_name_blue">Blue</string>
<string name="account_color">Account color</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="account_color_names">
<item>@string/account_color_name_green</item>
<item>@string/account_color_name_orange</item>
<item>@string/account_color_name_red</item>
<item>@string/account_color_name_blue</item>
</array>
<array name="account_action_bar">
<item>@color/teal_500</item>
<item>@color/deep_orange_500</item>
......
......@@ -397,6 +397,7 @@
<string name="account_proxy_password_key">account_proxy_password</string>
<string name="account_custom_key">account_custom</string>
<string name="account_archive_mode_key">account_archive_mode</string>
<string name="account_color_key">account_color_key</string>
<string-array name="account_tls_entries">
<item>@string/account_tls_enable</item>
......
......@@ -19,6 +19,12 @@
android:summary="@string/account_enabled_summary"
android:key="@string/account_enabled_key"
/>
<ListPreference
android:title="@string/account_color"
android:key="@string/account_color_key"
android:entries="@array/account_color_names"
android:entryValues="@array/account_color_names"
/>
<CheckBoxPreference
android:title="@string/account_syncable"
android:summary="@string/account_syncable_summary"
......
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