Remove old JID validator

This commit is contained in:
Sam Whited 2014-12-23 13:23:13 -05:00
parent 45ef112b0c
commit d454788011
3 changed files with 77 additions and 105 deletions

View File

@ -29,7 +29,6 @@ import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter; import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.utils.Validator;
import eu.siacs.conversations.xmpp.XmppConnection.Features; import eu.siacs.conversations.xmpp.XmppConnection.Features;
import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
@ -67,19 +66,14 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
private OnClickListener mSaveButtonClickListener = new OnClickListener() { private OnClickListener mSaveButtonClickListener = new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(final View v) {
if (mAccount != null if (mAccount != null
&& mAccount.getStatus() == Account.State.DISABLED) { && mAccount.getStatus() == Account.State.DISABLED) {
mAccount.setOption(Account.OPTION_DISABLED, false); mAccount.setOption(Account.OPTION_DISABLED, false);
xmppConnectionService.updateAccount(mAccount); xmppConnectionService.updateAccount(mAccount);
return; return;
} }
if (!Validator.isValidJid(mAccountJid.getText().toString())) { final boolean registerNewAccount = mRegisterNew.isChecked();
mAccountJid.setError(getString(R.string.invalid_jid));
mAccountJid.requestFocus();
return;
}
boolean registerNewAccount = mRegisterNew.isChecked();
final Jid jid; final Jid jid;
try { try {
jid = Jid.fromString(mAccountJid.getText().toString()); jid = Jid.fromString(mAccountJid.getText().toString());
@ -88,8 +82,8 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate
mAccountJid.requestFocus(); mAccountJid.requestFocus();
return; return;
} }
String password = mPassword.getText().toString(); final String password = mPassword.getText().toString();
String passwordConfirm = mPasswordConfirm.getText().toString(); final String passwordConfirm = mPasswordConfirm.getText().toString();
if (registerNewAccount) { if (registerNewAccount) {
if (!password.equals(passwordConfirm)) { if (!password.equals(passwordConfirm)) {
mPasswordConfirm mPasswordConfirm

View File

@ -38,6 +38,7 @@ import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Spinner; import android.widget.Spinner;
@ -61,7 +62,6 @@ import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter; import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.ui.adapter.ListItemAdapter; import eu.siacs.conversations.ui.adapter.ListItemAdapter;
import eu.siacs.conversations.utils.Validator;
import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.utils.XmppUri;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.InvalidJidException;
@ -349,41 +349,37 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
new View.OnClickListener() { new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(final View v) {
if (!xmppConnectionServiceBound) { if (!xmppConnectionServiceBound) {
return; return;
} }
if (Validator.isValidJid(jid.getText().toString())) { final Jid accountJid;
final Jid accountJid; try {
try { accountJid = Jid.fromString((String) spinner.getSelectedItem());
accountJid = Jid.fromString((String) spinner.getSelectedItem()); } catch (final InvalidJidException e) {
} catch (final InvalidJidException e) { return;
return; }
} final Jid contactJid;
final Jid contactJid; try {
try { contactJid = Jid.fromString(jid.getText().toString());
contactJid = Jid.fromString(jid.getText().toString()); } catch (final InvalidJidException e) {
} catch (final InvalidJidException e) {
jid.setError(getString(R.string.invalid_jid));
return;
}
Account account = xmppConnectionService
.findAccountByJid(accountJid);
if (account == null) {
dialog.dismiss();
return;
}
Contact contact = account.getRoster().getContact(contactJid);
if (contact.showInRoster()) {
jid.setError(getString(R.string.contact_already_exists));
} else {
contact.addOtrFingerprint(fingerprint);
xmppConnectionService.createContact(contact);
dialog.dismiss();
switchToConversation(contact);
}
} else {
jid.setError(getString(R.string.invalid_jid)); jid.setError(getString(R.string.invalid_jid));
return;
}
final Account account = xmppConnectionService
.findAccountByJid(accountJid);
if (account == null) {
dialog.dismiss();
return;
}
final Contact contact = account.getRoster().getContact(contactJid);
if (contact.showInRoster()) {
jid.setError(getString(R.string.contact_already_exists));
} else {
contact.addOtrFingerprint(fingerprint);
xmppConnectionService.createContact(contact);
dialog.dismiss();
switchToConversation(contact);
} }
} }
}); });
@ -391,10 +387,10 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
protected void showJoinConferenceDialog(String prefilledJid) { protected void showJoinConferenceDialog(final String prefilledJid) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.join_conference); builder.setTitle(R.string.join_conference);
View dialogView = getLayoutInflater().inflate(R.layout.join_conference_dialog, null); final View dialogView = getLayoutInflater().inflate(R.layout.join_conference_dialog, null);
final Spinner spinner = (Spinner) dialogView.findViewById(R.id.account); final Spinner spinner = (Spinner) dialogView.findViewById(R.id.account);
final AutoCompleteTextView jid = (AutoCompleteTextView) dialogView.findViewById(R.id.jid); final AutoCompleteTextView jid = (AutoCompleteTextView) dialogView.findViewById(R.id.jid);
jid.setAdapter(new KnownHostsAdapter(this,android.R.layout.simple_list_item_1, mKnownConferenceHosts)); jid.setAdapter(new KnownHostsAdapter(this,android.R.layout.simple_list_item_1, mKnownConferenceHosts));
@ -402,7 +398,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
jid.append(prefilledJid); jid.append(prefilledJid);
} }
populateAccountSpinner(spinner); populateAccountSpinner(spinner);
final CheckBox bookmarkCheckBox = (CheckBox) dialogView final Checkable bookmarkCheckBox = (CheckBox) dialogView
.findViewById(R.id.bookmark); .findViewById(R.id.bookmark);
builder.setView(dialogView); builder.setView(dialogView);
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
@ -413,63 +409,59 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
new View.OnClickListener() { new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(final View v) {
if (!xmppConnectionServiceBound) { if (!xmppConnectionServiceBound) {
return; return;
} }
if (Validator.isValidJid(jid.getText().toString())) { final Jid accountJid;
final Jid accountJid; try {
try { accountJid = Jid.fromString((String) spinner.getSelectedItem());
accountJid = Jid.fromString((String) spinner.getSelectedItem()); } catch (final InvalidJidException e) {
} catch (final InvalidJidException e) { return;
return; }
} final Jid conferenceJid;
final Jid conferenceJid; try {
try { conferenceJid = Jid.fromString(jid.getText().toString());
conferenceJid = Jid.fromString(jid.getText().toString()); } catch (final InvalidJidException e) {
} catch (final InvalidJidException e) { jid.setError(getString(R.string.invalid_jid));
jid.setError(getString(R.string.invalid_jid)); return;
return; }
} final Account account = xmppConnectionService
Account account = xmppConnectionService .findAccountByJid(accountJid);
.findAccountByJid(accountJid); if (account == null) {
if (account == null) { dialog.dismiss();
dialog.dismiss(); return;
return; }
} if (bookmarkCheckBox.isChecked()) {
if (bookmarkCheckBox.isChecked()) { if (account.hasBookmarkFor(conferenceJid)) {
if (account.hasBookmarkFor(conferenceJid)) { jid.setError(getString(R.string.bookmark_already_exists));
jid.setError(getString(R.string.bookmark_already_exists));
} else {
Bookmark bookmark = new Bookmark(account,
conferenceJid);
bookmark.setAutojoin(true);
account.getBookmarks().add(bookmark);
xmppConnectionService
.pushBookmarks(account);
Conversation conversation = xmppConnectionService
.findOrCreateConversation(account,
conferenceJid, true);
conversation.setBookmark(bookmark);
if (!conversation.getMucOptions().online()) {
xmppConnectionService
.joinMuc(conversation);
}
dialog.dismiss();
switchToConversation(conversation);
}
} else { } else {
Conversation conversation = xmppConnectionService final Bookmark bookmark = new Bookmark(account,
conferenceJid);
bookmark.setAutojoin(true);
account.getBookmarks().add(bookmark);
xmppConnectionService
.pushBookmarks(account);
final Conversation conversation = xmppConnectionService
.findOrCreateConversation(account, .findOrCreateConversation(account,
conferenceJid, true); conferenceJid, true);
conversation.setBookmark(bookmark);
if (!conversation.getMucOptions().online()) { if (!conversation.getMucOptions().online()) {
xmppConnectionService.joinMuc(conversation); xmppConnectionService
.joinMuc(conversation);
} }
dialog.dismiss(); dialog.dismiss();
switchToConversation(conversation); switchToConversation(conversation);
} }
} else { } else {
jid.setError(getString(R.string.invalid_jid)); final Conversation conversation = xmppConnectionService
.findOrCreateConversation(account,
conferenceJid, true);
if (!conversation.getMucOptions().online()) {
xmppConnectionService.joinMuc(conversation);
}
dialog.dismiss();
switchToConversation(conversation);
} }
} }
}); });

View File

@ -1,14 +0,0 @@
package eu.siacs.conversations.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Validator {
public static final Pattern VALID_JID = Pattern.compile(
"^[^@/<>'\"\\s]+@[^@/<>'\"\\s]+$", Pattern.CASE_INSENSITIVE);
public static boolean isValidJid(String jid) {
Matcher matcher = VALID_JID.matcher(jid);
return matcher.find();
}
}