2014-02-28 17:46:01 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
2014-01-28 18:21:54 +00:00
|
|
|
|
2014-02-28 17:46:01 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.utils.Validator;
|
2014-01-28 18:21:54 +00:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.DialogFragment;
|
|
|
|
import android.os.Bundle;
|
2014-03-13 03:21:59 +00:00
|
|
|
import android.util.Log;
|
2014-01-28 18:21:54 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
public class EditAccount extends DialogFragment {
|
|
|
|
|
|
|
|
protected Account account;
|
|
|
|
|
|
|
|
public void setAccount(Account account) {
|
|
|
|
this.account = account;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface EditAccountListener {
|
|
|
|
public void onAccountEdited(Account account);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected EditAccountListener listener = null;
|
|
|
|
|
|
|
|
public void setEditAccountListener(EditAccountListener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
|
|
LayoutInflater inflater = getActivity().getLayoutInflater();
|
|
|
|
View view = inflater.inflate(R.layout.edit_account_dialog, null);
|
|
|
|
final EditText jidText = (EditText) view.findViewById(R.id.account_jid);
|
2014-02-04 21:58:29 +00:00
|
|
|
final TextView confirmPwDesc = (TextView) view
|
|
|
|
.findViewById(R.id.account_confirm_password_desc);
|
2014-04-03 16:16:14 +00:00
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
final EditText password = (EditText) view
|
|
|
|
.findViewById(R.id.account_password);
|
|
|
|
final EditText passwordConfirm = (EditText) view
|
|
|
|
.findViewById(R.id.account_password_confirm2);
|
|
|
|
final CheckBox registerAccount = (CheckBox) view
|
|
|
|
.findViewById(R.id.edit_account_register_new);
|
|
|
|
|
|
|
|
if (account != null) {
|
|
|
|
jidText.setText(account.getJid());
|
|
|
|
password.setText(account.getPassword());
|
2014-03-13 02:52:41 +00:00
|
|
|
if (account.isOptionSet(Account.OPTION_REGISTER)) {
|
|
|
|
registerAccount.setChecked(true);
|
2014-03-13 03:21:59 +00:00
|
|
|
passwordConfirm.setVisibility(View.VISIBLE);
|
2014-03-13 03:32:19 +00:00
|
|
|
passwordConfirm.setText(account.getPassword());
|
2014-03-13 02:52:41 +00:00
|
|
|
} else {
|
|
|
|
registerAccount.setVisibility(View.GONE);
|
|
|
|
}
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-05-18 09:49:44 +00:00
|
|
|
builder.setTitle(R.string.account_settings);
|
|
|
|
|
2014-01-28 18:21:54 +00:00
|
|
|
|
|
|
|
registerAccount
|
|
|
|
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView,
|
|
|
|
boolean isChecked) {
|
|
|
|
if (isChecked) {
|
|
|
|
passwordConfirm.setVisibility(View.VISIBLE);
|
|
|
|
confirmPwDesc.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
passwordConfirm.setVisibility(View.GONE);
|
|
|
|
confirmPwDesc.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.setView(view);
|
2014-05-18 09:49:44 +00:00
|
|
|
builder.setNeutralButton(R.string.cancel, null);
|
|
|
|
builder.setPositiveButton(R.string.save, null);
|
2014-01-28 18:21:54 +00:00
|
|
|
return builder.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
final AlertDialog d = (AlertDialog) getDialog();
|
|
|
|
Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE);
|
|
|
|
positiveButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
EditText jidEdit = (EditText) d.findViewById(R.id.account_jid);
|
|
|
|
String jid = jidEdit.getText().toString();
|
2014-02-04 21:58:29 +00:00
|
|
|
EditText passwordEdit = (EditText) d
|
|
|
|
.findViewById(R.id.account_password);
|
2014-05-18 09:49:44 +00:00
|
|
|
EditText passwordConfirmEdit = (EditText) d.findViewById(R.id.account_password_confirm2);
|
2014-01-28 18:21:54 +00:00
|
|
|
String password = passwordEdit.getText().toString();
|
2014-05-18 09:49:44 +00:00
|
|
|
String passwordConfirm = passwordConfirmEdit.getText().toString();
|
2014-03-13 02:52:41 +00:00
|
|
|
CheckBox register = (CheckBox) d.findViewById(R.id.edit_account_register_new);
|
2014-02-04 21:58:29 +00:00
|
|
|
String username;
|
|
|
|
String server;
|
|
|
|
if (Validator.isValidJid(jid)) {
|
|
|
|
String[] parts = jid.split("@");
|
|
|
|
username = parts[0];
|
|
|
|
server = parts[1];
|
2014-01-28 18:21:54 +00:00
|
|
|
} else {
|
2014-05-18 09:49:44 +00:00
|
|
|
jidEdit.setError(getString(R.string.invalid_jid));
|
2014-02-04 21:58:29 +00:00
|
|
|
return;
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-05-18 09:49:44 +00:00
|
|
|
if (register.isChecked()) {
|
|
|
|
if (!passwordConfirm.equals(password)) {
|
|
|
|
passwordConfirmEdit.setError(getString(R.string.passwords_do_not_match));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 21:58:29 +00:00
|
|
|
if (account != null) {
|
2014-01-28 18:21:54 +00:00
|
|
|
account.setPassword(password);
|
|
|
|
account.setUsername(username);
|
|
|
|
account.setServer(server);
|
|
|
|
} else {
|
|
|
|
account = new Account(username, server, password);
|
2014-04-03 19:25:03 +00:00
|
|
|
account.setOption(Account.OPTION_USETLS, true);
|
|
|
|
account.setOption(Account.OPTION_USECOMPRESSION, true);
|
2014-01-28 18:21:54 +00:00
|
|
|
}
|
2014-03-13 02:52:41 +00:00
|
|
|
account.setOption(Account.OPTION_REGISTER, register.isChecked());
|
2014-02-04 21:58:29 +00:00
|
|
|
if (listener != null) {
|
2014-01-28 18:21:54 +00:00
|
|
|
listener.onAccountEdited(account);
|
|
|
|
d.dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|