aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/ui/EditAccount.java
blob: 9a0b8d84281de0de5989454ef771db5c4a1478e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package eu.siacs.conversations.ui;

import java.util.List;

import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.KnownHostsAdapter;
import eu.siacs.conversations.utils.Validator;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AutoCompleteTextView;
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;
	
	protected AutoCompleteTextView mAccountJid;

	public void setAccount(Account account) {
		this.account = account;
	}

	public interface EditAccountListener {
		public void onAccountEdited(Account account);
	}

	protected EditAccountListener listener = null;

	private KnownHostsAdapter mKnownHostsAdapter;

	public void setEditAccountListener(EditAccountListener listener) {
		this.listener = listener;
	}
	
	public void setKnownHosts(List<String> hosts, Context context) {
		this.mKnownHostsAdapter = new KnownHostsAdapter(context, android.R.layout.simple_list_item_1, hosts);
		if (this.mAccountJid != null) {
			this.mAccountJid.setAdapter(this.mKnownHostsAdapter);
		}
	}

	@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);
		mAccountJid = (AutoCompleteTextView) view.findViewById(R.id.account_jid);
		if (this.mKnownHostsAdapter!=null) {
			mAccountJid.setAdapter(this.mKnownHostsAdapter);
		}
		final TextView confirmPwDesc = (TextView) view
				.findViewById(R.id.account_confirm_password_desc);

		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) {
			mAccountJid.setText(account.getJid());
			password.setText(account.getPassword());
			if (account.isOptionSet(Account.OPTION_REGISTER)) {
				registerAccount.setChecked(true);
				passwordConfirm.setVisibility(View.VISIBLE);
				passwordConfirm.setText(account.getPassword());
			} else {
				registerAccount.setVisibility(View.GONE);
			}
		}
		builder.setTitle(R.string.account_settings);
		

		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);
		builder.setNeutralButton(getString(R.string.cancel), null);
		builder.setPositiveButton(getString(R.string.save), null);
		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();
				EditText passwordEdit = (EditText) d
						.findViewById(R.id.account_password);
				EditText passwordConfirmEdit = (EditText) d.findViewById(R.id.account_password_confirm2);
				String password = passwordEdit.getText().toString();
				String passwordConfirm = passwordConfirmEdit.getText().toString();
				CheckBox register = (CheckBox) d.findViewById(R.id.edit_account_register_new);
				String username;
				String server;
				if (Validator.isValidJid(jid)) {
					String[] parts = jid.split("@");
					username = parts[0];
					server = parts[1];
				} else {
					jidEdit.setError(getString(R.string.invalid_jid));
					return;
				}
				if (register.isChecked()) {
					if (!passwordConfirm.equals(password)) {
						passwordConfirmEdit.setError(getString(R.string.passwords_do_not_match));
						return;
					}
				}
				if (account != null) {
					account.setPassword(password);
					account.setUsername(username);
					account.setServer(server);
				} else {
					account = new Account(username, server, password);
					account.setOption(Account.OPTION_USETLS, true);
					account.setOption(Account.OPTION_USECOMPRESSION, true);
				}
				account.setOption(Account.OPTION_REGISTER, register.isChecked());
				if (listener != null) {
					listener.onAccountEdited(account);
					d.dismiss();
				}
			}
		});
	}
}