Commit 97e326bb authored by Grigory Fedorov's avatar Grigory Fedorov

AccountEditorFragment: light refactoring.

parent cfacbc7e
...@@ -27,19 +27,19 @@ public class AccountEditorFragment extends BaseSettingsFragment ...@@ -27,19 +27,19 @@ public class AccountEditorFragment extends BaseSettingsFragment
@Override @Override
protected void onInflate(Bundle savedInstanceState) { protected void onInflate(Bundle savedInstanceState) {
AccountProtocol protocol = mListener.getAccountItem().getConnectionSettings() AccountProtocol protocol = mListener.getAccountItem().getConnectionSettings().getProtocol();
.getProtocol(); if (protocol == AccountProtocol.xmpp) {
if (protocol == AccountProtocol.xmpp)
addPreferencesFromResource(R.xml.account_editor_xmpp); addPreferencesFromResource(R.xml.account_editor_xmpp);
else if (protocol == AccountProtocol.gtalk) } else if (protocol == AccountProtocol.gtalk) {
addPreferencesFromResource(R.xml.account_editor_xmpp); addPreferencesFromResource(R.xml.account_editor_xmpp);
else if (protocol == AccountProtocol.wlm) } else if (protocol == AccountProtocol.wlm) {
addPreferencesFromResource(R.xml.account_editor_oauth); addPreferencesFromResource(R.xml.account_editor_oauth);
else } else {
throw new IllegalStateException(); throw new IllegalStateException();
if (!Application.getInstance().isContactsSupported()) }
getPreferenceScreen().removePreference( if (!Application.getInstance().isContactsSupported()) {
findPreference(getString(R.string.account_syncable_key))); getPreferenceScreen().removePreference(findPreference(getString(R.string.account_syncable_key)));
}
oauthPreference = findPreference(getString(R.string.account_oauth_key)); oauthPreference = findPreference(getString(R.string.account_oauth_key));
if (oauthPreference != null) { if (oauthPreference != null) {
...@@ -57,7 +57,9 @@ public class AccountEditorFragment extends BaseSettingsFragment ...@@ -57,7 +57,9 @@ public class AccountEditorFragment extends BaseSettingsFragment
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { 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 { try {
Integer.parseInt((String) newValue); Integer.parseInt((String) newValue);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
...@@ -65,45 +67,42 @@ public class AccountEditorFragment extends BaseSettingsFragment ...@@ -65,45 +67,42 @@ public class AccountEditorFragment extends BaseSettingsFragment
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
return false; return false;
} }
if (getString(R.string.account_tls_mode_key) }
.equals(preference.getKey())
|| getString(R.string.account_archive_mode_key).equals( if (getString(R.string.account_tls_mode_key).equals(key)
preference.getKey()) || getString(R.string.account_archive_mode_key).equals(key)
|| getString(R.string.account_proxy_type_key).equals( || getString(R.string.account_proxy_type_key).equals(key)) {
preference.getKey()))
preference.setSummary((String) newValue); preference.setSummary((String) newValue);
else if (!getString(R.string.account_password_key).equals( } else if (!getString(R.string.account_password_key).equals(key)
preference.getKey()) && !getString(R.string.account_proxy_password_key).equals(key)
&& !getString(R.string.account_proxy_password_key).equals( && !getString(R.string.account_priority_key).equals(key)) {
preference.getKey())
&& !getString(R.string.account_priority_key).equals(
preference.getKey()))
super.onPreferenceChange(preference, newValue); super.onPreferenceChange(preference, newValue);
if (getString(R.string.account_proxy_type_key).equals( }
preference.getKey())) {
boolean enabled = !getString(R.string.account_proxy_type_none) if (getString(R.string.account_proxy_type_key).equals(key)) {
.equals(newValue) boolean enabled = !getString(R.string.account_proxy_type_none).equals(newValue)
&& !getString(R.string.account_proxy_type_orbot).equals( && !getString(R.string.account_proxy_type_orbot).equals(newValue);
newValue);
for (int id : new Integer[]{R.string.account_proxy_host_key, for (int id : new Integer[]{R.string.account_proxy_host_key,
R.string.account_proxy_port_key, R.string.account_proxy_port_key, R.string.account_proxy_user_key,
R.string.account_proxy_user_key,
R.string.account_proxy_password_key,}) { R.string.account_proxy_password_key,}) {
Preference proxyPreference = findPreference(getString(id)); Preference proxyPreference = findPreference(getString(id));
if (proxyPreference != null) if (proxyPreference != null) {
proxyPreference.setEnabled(enabled); proxyPreference.setEnabled(enabled);
}
} }
} }
return true; return true;
} }
public void onOAuthChange() { public void onOAuthChange() {
if (oauthPreference == null) if (oauthPreference == null) {
return; return;
if (AccountEditor.INVALIDATED_TOKEN.equals(mListener.getToken())) }
if (AccountEditor.INVALIDATED_TOKEN.equals(mListener.getToken())) {
oauthPreference.setSummary(R.string.account_oauth_invalidated); oauthPreference.setSummary(R.string.account_oauth_invalidated);
else } else {
oauthPreference.setSummary(R.string.account_oauth_summary); oauthPreference.setSummary(R.string.account_oauth_summary);
}
} }
@Override @Override
...@@ -120,59 +119,44 @@ public class AccountEditorFragment extends BaseSettingsFragment ...@@ -120,59 +119,44 @@ public class AccountEditorFragment extends BaseSettingsFragment
Map<String, Object> source = new HashMap<>(); Map<String, Object> source = new HashMap<>();
AccountItem accountItem = mListener.getAccountItem(); 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_priority_key, accountItem.getPriority());
putValue(source, R.string.account_enabled_key, accountItem.isEnabled()); putValue(source, R.string.account_enabled_key, accountItem.isEnabled());
putValue(source, R.string.account_sasl_key, accountItem putValue(source, R.string.account_store_password_key, accountItem.isStorePassword());
.getConnectionSettings().isSaslEnabled()); putValue(source, R.string.account_syncable_key, accountItem.isSyncable());
putValue(source, R.string.account_tls_mode_key, putValue(source, R.string.account_archive_mode_key, accountItem.getArchiveMode().ordinal());
accountItem.getConnectionSettings().getTlsMode().ordinal());
putValue(source, R.string.account_compression_key, accountItem com.xabber.android.data.connection.ConnectionSettings connectionSettings = accountItem.getConnectionSettings();
.getConnectionSettings().useCompression()); putValue(source, R.string.account_custom_key, connectionSettings.isCustom());
putValue(source, R.string.account_proxy_type_key, putValue(source, R.string.account_host_key, connectionSettings.getHost());
accountItem.getConnectionSettings().getProxyType().ordinal()); putValue(source, R.string.account_port_key, connectionSettings.getPort());
putValue(source, R.string.account_proxy_host_key, accountItem putValue(source, R.string.account_server_key, connectionSettings.getServerName());
.getConnectionSettings().getProxyHost()); putValue(source, R.string.account_username_key, connectionSettings.getUserName());
putValue(source, R.string.account_proxy_port_key, accountItem putValue(source, R.string.account_password_key, connectionSettings.getPassword());
.getConnectionSettings().getProxyPort()); putValue(source, R.string.account_resource_key, connectionSettings.getResource());
putValue(source, R.string.account_proxy_user_key, accountItem putValue(source, R.string.account_sasl_key, connectionSettings.isSaslEnabled());
.getConnectionSettings().getProxyUser()); putValue(source, R.string.account_tls_mode_key, connectionSettings.getTlsMode().ordinal());
putValue(source, R.string.account_proxy_password_key, accountItem putValue(source, R.string.account_compression_key, connectionSettings.useCompression());
.getConnectionSettings().getProxyPassword()); putValue(source, R.string.account_proxy_type_key, connectionSettings.getProxyType().ordinal());
putValue(source, R.string.account_syncable_key, putValue(source, R.string.account_proxy_host_key, connectionSettings.getProxyHost());
accountItem.isSyncable()); putValue(source, R.string.account_proxy_port_key, connectionSettings.getProxyPort());
putValue(source, R.string.account_archive_mode_key, putValue(source, R.string.account_proxy_user_key, connectionSettings.getProxyUser());
accountItem.getArchiveMode().ordinal()); putValue(source, R.string.account_proxy_password_key, connectionSettings.getProxyPassword());
return source; return source;
} }
@Override @Override
protected Map<String, Object> getPreferences(Map<String, Object> source) { protected Map<String, Object> getPreferences(Map<String, Object> source) {
Map<String, Object> result = super.getPreferences(source); Map<String, Object> result = super.getPreferences(source);
if (oauthPreference != null) if (oauthPreference != null) {
putValue(result, R.string.account_password_key, mListener.getAccount()); putValue(result, R.string.account_password_key, mListener.getAccount());
}
return result; return result;
} }
@Override @Override
protected boolean setValues(Map<String, Object> source, protected boolean setValues(Map<String, Object> source, Map<String, Object> result) {
Map<String, Object> result) { ProxyType proxyType = ProxyType.values()[getInt(result, R.string.account_proxy_type_key)];
ProxyType proxyType = ProxyType.values()[getInt(result,
R.string.account_proxy_type_key)];
if (proxyType == ProxyType.orbot && !OrbotHelper.isOrbotInstalled()) { if (proxyType == ProxyType.orbot && !OrbotHelper.isOrbotInstalled()) {
mListener.showOrbotDialog(); mListener.showOrbotDialog();
return false; return false;
...@@ -199,12 +183,14 @@ public class AccountEditorFragment extends BaseSettingsFragment ...@@ -199,12 +183,14 @@ public class AccountEditorFragment extends BaseSettingsFragment
getString(result, R.string.account_proxy_password_key), getString(result, R.string.account_proxy_password_key),
getBoolean(result, R.string.account_syncable_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)]);
return true; return true;
} }
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
try { try {
mListener = (AccountEditorFragmentInteractionListener) activity; mListener = (AccountEditorFragmentInteractionListener) activity;
} catch (ClassCastException e) { } catch (ClassCastException e) {
...@@ -221,13 +207,9 @@ public class AccountEditorFragment extends BaseSettingsFragment ...@@ -221,13 +207,9 @@ public class AccountEditorFragment extends BaseSettingsFragment
public interface AccountEditorFragmentInteractionListener { public interface AccountEditorFragmentInteractionListener {
public String getAccount(); public String getAccount();
public AccountItem getAccountItem(); public AccountItem getAccountItem();
public String getToken(); public String getToken();
public void onOAuthClick(); public void onOAuthClick();
public void showOrbotDialog(); public void showOrbotDialog();
} }
} }
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