2016-01-04 11:27:51 -05:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.AutoCompleteTextView;
|
|
|
|
import android.widget.Spinner;
|
2016-02-24 10:53:19 -05:00
|
|
|
import android.widget.TextView;
|
2016-01-04 11:27:51 -05:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
|
|
|
|
public class EnterJidDialog {
|
2016-01-22 05:14:56 -05:00
|
|
|
public interface OnEnterJidDialogPositiveListener {
|
|
|
|
boolean onEnterJidDialogPositive(Jid account, Jid contact) throws EnterJidDialog.JidError;
|
2016-01-04 11:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class JidError extends Exception {
|
|
|
|
final String msg;
|
|
|
|
|
|
|
|
public JidError(final String msg) {
|
|
|
|
this.msg = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final AlertDialog dialog;
|
|
|
|
protected View.OnClickListener dialogOnClick;
|
|
|
|
protected OnEnterJidDialogPositiveListener listener = null;
|
|
|
|
|
|
|
|
public EnterJidDialog(
|
2016-01-22 05:14:56 -05:00
|
|
|
final Context context, List<String> knownHosts, final List<String> activatedAccounts,
|
2016-01-04 11:27:51 -05:00
|
|
|
final String title, final String positiveButton,
|
|
|
|
final String prefilledJid, final String account, boolean allowEditJid
|
|
|
|
) {
|
2016-02-24 10:53:19 -05:00
|
|
|
final boolean lock = Config.LOCK_DOMAINS_IN_CONVERSATIONS && Config.DOMAIN_LOCK != null;
|
2016-01-04 11:27:51 -05:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
|
|
|
builder.setTitle(title);
|
|
|
|
View dialogView = LayoutInflater.from(context).inflate(R.layout.enter_jid_dialog, null);
|
2016-02-24 10:53:19 -05:00
|
|
|
final TextView jabberIdDesc = (TextView) dialogView.findViewById(R.id.jabber_id);
|
|
|
|
jabberIdDesc.setText(lock ? R.string.username : R.string.account_settings_jabber_id);
|
2016-01-04 11:27:51 -05:00
|
|
|
final Spinner spinner = (Spinner) dialogView.findViewById(R.id.account);
|
|
|
|
final AutoCompleteTextView jid = (AutoCompleteTextView) dialogView.findViewById(R.id.jid);
|
2016-02-24 10:53:19 -05:00
|
|
|
if (!lock) {
|
|
|
|
jid.setAdapter(new KnownHostsAdapter(context, android.R.layout.simple_list_item_1, knownHosts));
|
|
|
|
}
|
2016-01-04 11:27:51 -05:00
|
|
|
if (prefilledJid != null) {
|
|
|
|
jid.append(prefilledJid);
|
|
|
|
if (!allowEditJid) {
|
|
|
|
jid.setFocusable(false);
|
|
|
|
jid.setFocusableInTouchMode(false);
|
|
|
|
jid.setClickable(false);
|
|
|
|
jid.setCursorVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 10:53:19 -05:00
|
|
|
jid.setHint(Config.LOCK_DOMAINS_IN_CONVERSATIONS && Config.DOMAIN_LOCK != null ? R.string.username_hint : R.string.account_settings_example_jabber_id);
|
2016-01-22 05:14:56 -05:00
|
|
|
|
2016-01-04 11:27:51 -05:00
|
|
|
if (account == null) {
|
2016-01-22 05:14:56 -05:00
|
|
|
StartConversationActivity.populateAccountSpinner(context, activatedAccounts, spinner);
|
2016-01-04 11:27:51 -05:00
|
|
|
} else {
|
2016-01-22 05:14:56 -05:00
|
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(context,
|
|
|
|
android.R.layout.simple_spinner_item,
|
|
|
|
new String[] { account });
|
2016-01-04 11:27:51 -05:00
|
|
|
spinner.setEnabled(false);
|
2016-01-22 05:14:56 -05:00
|
|
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
|
spinner.setAdapter(adapter);
|
2016-01-04 11:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.setView(dialogView);
|
|
|
|
builder.setNegativeButton(R.string.cancel, null);
|
|
|
|
builder.setPositiveButton(positiveButton, null);
|
|
|
|
this.dialog = builder.create();
|
|
|
|
|
|
|
|
this.dialogOnClick = new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(final View v) {
|
|
|
|
final Jid accountJid;
|
2016-01-22 05:14:56 -05:00
|
|
|
if (!spinner.isEnabled() && account == null) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-04 11:27:51 -05:00
|
|
|
try {
|
|
|
|
if (Config.DOMAIN_LOCK != null) {
|
|
|
|
accountJid = Jid.fromParts((String) spinner.getSelectedItem(), Config.DOMAIN_LOCK, null);
|
|
|
|
} else {
|
|
|
|
accountJid = Jid.fromString((String) spinner.getSelectedItem());
|
|
|
|
}
|
|
|
|
} catch (final InvalidJidException e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Jid contactJid;
|
|
|
|
try {
|
2016-02-24 10:53:19 -05:00
|
|
|
if (lock) {
|
|
|
|
contactJid = Jid.fromParts(jid.getText().toString(), Config.DOMAIN_LOCK, null);
|
|
|
|
} else {
|
|
|
|
contactJid = Jid.fromString(jid.getText().toString());
|
|
|
|
}
|
2016-01-04 11:27:51 -05:00
|
|
|
} catch (final InvalidJidException e) {
|
2016-02-24 10:53:19 -05:00
|
|
|
jid.setError(context.getString(lock ? R.string.invalid_username : R.string.invalid_jid));
|
2016-01-04 11:27:51 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(listener != null) {
|
|
|
|
try {
|
|
|
|
if(listener.onEnterJidDialogPositive(accountJid, contactJid)) {
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
} catch(JidError error) {
|
|
|
|
jid.setError(error.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOnEnterJidDialogPositiveListener(OnEnterJidDialogPositiveListener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void show() {
|
|
|
|
this.dialog.show();
|
|
|
|
this.dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(this.dialogOnClick);
|
|
|
|
}
|
|
|
|
}
|