1package eu.siacs.conversations.ui;
2
3import android.app.PendingIntent;
4import android.content.Intent;
5import android.os.Bundle;
6import android.text.Editable;
7import android.text.TextWatcher;
8import android.view.View;
9import android.view.View.OnClickListener;
10import android.widget.AutoCompleteTextView;
11import android.widget.Button;
12import android.widget.CheckBox;
13import android.widget.CompoundButton;
14import android.widget.EditText;
15import android.widget.CompoundButton.OnCheckedChangeListener;
16import eu.siacs.conversations.R;
17import eu.siacs.conversations.entities.Account;
18import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
19import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
20import eu.siacs.conversations.utils.Validator;
21import eu.siacs.conversations.xmpp.pep.Avatar;
22
23public class EditAccountActivity extends XmppActivity {
24
25 private AutoCompleteTextView mAccountJid;
26 private EditText mPassword;
27 private EditText mPasswordConfirm;
28 private CheckBox mRegisterNew;
29 private Button mCancelButton;
30 private Button mSaveButton;
31
32 private String jidToEdit;
33 private Account mAccount;
34 private Avatar mAvatar = null;
35
36 private boolean mUserInputIsValid = false;
37 private boolean mFetchingAvatar = false;
38 private boolean mFinishedInitialSetup = false;
39
40 private OnClickListener mSaveButtonClickListener = new OnClickListener() {
41
42 @Override
43 public void onClick(View v) {
44 if (mAccount != null && mFinishedInitialSetup) {
45 Intent intent;
46 if (mAvatar!=null) {
47 intent = new Intent(getApplicationContext(), StartConversationActivity.class);
48 } else {
49 intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
50 intent.putExtra("account", mAccount.getJid());
51 }
52 startActivity(intent);
53 finish();
54 return;
55 } else if (mAccount != null && mAccount.errorStatus()
56 && !mUserInputIsValid) {
57 xmppConnectionService.reconnectAccount(mAccount, true);
58 return;
59 }
60 boolean registerNewAccount = mRegisterNew.isChecked();
61 String[] jidParts = mAccountJid.getText().toString().split("@");
62 String username = jidParts[0];
63 String server;
64 if (jidParts.length >= 2) {
65 server = jidParts[1];
66 } else {
67 server = "";
68 }
69 String password = mPassword.getText().toString();
70 String passwordConfirm = mPasswordConfirm.getText().toString();
71 if (registerNewAccount) {
72 if (!password.equals(passwordConfirm)) {
73 mPasswordConfirm
74 .setError(getString(R.string.passwords_do_not_match));
75 return;
76 }
77 }
78 if (mAccount != null) {
79 mAccount.setPassword(password);
80 mAccount.setUsername(username);
81 mAccount.setServer(server);
82 mAccount.setOption(Account.OPTION_REGISTER, mRegisterNew.isChecked());
83 xmppConnectionService.updateAccount(mAccount);
84 } else {
85 if (xmppConnectionService.findAccountByJid(mAccountJid.getText().toString())!=null) {
86 mAccountJid.setError(getString(R.string.account_already_exists));
87 return;
88 }
89 mAccount = new Account(username, server, password);
90 mAccount.setOption(Account.OPTION_USETLS, true);
91 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
92 if (registerNewAccount) {
93 mAccount.setOption(Account.OPTION_REGISTER, true);
94 }
95 xmppConnectionService.createAccount(mAccount);
96 }
97 if (jidToEdit != null) {
98 finish();
99 } else {
100 mUserInputIsValid = false;
101 updateSaveButton();
102 updateAccountInformation();
103 }
104
105 }
106 };
107 private OnClickListener mCancelButtonClickListener = new OnClickListener() {
108
109 @Override
110 public void onClick(View v) {
111 finish();
112 }
113 };
114 private TextWatcher mTextWatcher = new TextWatcher() {
115
116 @Override
117 public void onTextChanged(CharSequence s, int start, int before,
118 int count) {
119 if (Validator.isValidJid(mAccountJid.getText().toString())) {
120 mUserInputIsValid = inputDataDiffersFromAccount();
121 } else {
122 mUserInputIsValid = false;
123 }
124 updateSaveButton();
125 }
126
127 @Override
128 public void beforeTextChanged(CharSequence s, int start, int count,
129 int after) {
130 }
131
132 @Override
133 public void afterTextChanged(Editable s) {
134 }
135 };
136 private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
137
138 @Override
139 public void onAccountUpdate() {
140 runOnUiThread(new Runnable() {
141
142 @Override
143 public void run() {
144 if (jidToEdit==null && mAccount!=null && mAccount.getStatus() == Account.STATUS_ONLINE) {
145 if (!mFetchingAvatar) {
146 mFetchingAvatar = true;
147 xmppConnectionService.checkForAvatar(mAccount, mAvatarFetchCallback);
148 }
149 } else {
150 updateSaveButton();
151 }
152 }
153 });
154 }
155 };
156 private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
157
158 @Override
159 public void userInputRequried(PendingIntent pi, Avatar avatar) {
160 finishInitialSetup(avatar);
161 }
162
163 @Override
164 public void success(Avatar avatar) {
165 finishInitialSetup(avatar);
166 }
167
168 @Override
169 public void error(int errorCode, Avatar avatar) {
170 finishInitialSetup(avatar);
171 }
172 };
173
174 protected void finishInitialSetup(Avatar avatar) {
175 this.mFinishedInitialSetup = true;
176 this.mAvatar = avatar;
177 runOnUiThread(new Runnable() {
178
179 @Override
180 public void run() {
181 updateSaveButton();
182 }
183 });
184 }
185
186 protected boolean inputDataDiffersFromAccount() {
187 if (mAccount == null) {
188 return true;
189 } else {
190 return (!mAccount.getJid().equals(mAccountJid.getText().toString()))
191 || (!mAccount.getPassword().equals(
192 mPassword.getText().toString()) || mAccount
193 .isOptionSet(Account.OPTION_REGISTER) != mRegisterNew
194 .isChecked());
195 }
196 }
197
198 protected void updateSaveButton() {
199 if (mAccount != null && mFinishedInitialSetup) {
200 this.mSaveButton.setEnabled(true);
201 this.mSaveButton.setTextColor(getPrimaryTextColor());
202 this.mSaveButton.setText(R.string.next);
203 } else if (mAccount != null
204 && mAccount.getStatus() == Account.STATUS_CONNECTING
205 && !mUserInputIsValid) {
206 this.mSaveButton.setEnabled(false);
207 this.mSaveButton.setTextColor(getSecondaryTextColor());
208 this.mSaveButton.setText(R.string.account_status_connecting);
209 } else if (mAccount != null && mAccount.errorStatus()
210 && !mUserInputIsValid) {
211 this.mSaveButton.setEnabled(true);
212 this.mSaveButton.setTextColor(getPrimaryTextColor());
213 this.mSaveButton.setText(R.string.connect);
214 } else if (mUserInputIsValid) {
215 this.mSaveButton.setEnabled(true);
216 this.mSaveButton.setTextColor(getPrimaryTextColor());
217 } else {
218 this.mSaveButton.setEnabled(false);
219 this.mSaveButton.setTextColor(getSecondaryTextColor());
220 this.mSaveButton.setText(R.string.save);
221 }
222 }
223
224 @Override
225 protected void onCreate(Bundle savedInstanceState) {
226 super.onCreate(savedInstanceState);
227 setContentView(R.layout.activity_edit_account);
228 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
229 this.mPassword = (EditText) findViewById(R.id.account_password);
230 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
231 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
232 this.mSaveButton = (Button) findViewById(R.id.save_button);
233 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
234 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
235 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
236 this.mRegisterNew
237 .setOnCheckedChangeListener(new OnCheckedChangeListener() {
238
239 @Override
240 public void onCheckedChanged(CompoundButton buttonView,
241 boolean isChecked) {
242 if (isChecked) {
243 mPasswordConfirm.setVisibility(View.VISIBLE);
244 } else {
245 mPasswordConfirm.setVisibility(View.GONE);
246 }
247 mUserInputIsValid = inputDataDiffersFromAccount();
248 updateSaveButton();
249 }
250 });
251 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
252 this.mPassword.addTextChangedListener(this.mTextWatcher);
253 }
254
255 @Override
256 protected void onStart() {
257 super.onStart();
258 if (getIntent() != null) {
259 this.jidToEdit = getIntent().getStringExtra("jid");
260 if (this.jidToEdit != null) {
261 this.mRegisterNew.setVisibility(View.GONE);
262 getActionBar().setTitle(R.string.mgmt_account_edit);
263 } else {
264 getActionBar().setTitle(R.string.action_add_account);
265 }
266 }
267 }
268
269 @Override
270 protected void onBackendConnected() {
271 this.xmppConnectionService
272 .setOnAccountListChangedListener(this.mOnAccountUpdateListener);
273 this.mAccountJid.setAdapter(null);
274 if (this.jidToEdit != null) {
275 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
276 updateAccountInformation();
277 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
278 getActionBar().setDisplayHomeAsUpEnabled(false);
279 getActionBar().setDisplayShowHomeEnabled(false);
280 this.mCancelButton.setEnabled(false);
281 }
282 this.mAccountJid.setAdapter(new KnownHostsAdapter(this,
283 android.R.layout.simple_list_item_1, xmppConnectionService
284 .getKnownHosts()));
285 updateSaveButton();
286 }
287
288 private void updateAccountInformation() {
289 this.mAccountJid.setText(this.mAccount.getJid());
290 this.mPassword.setText(this.mAccount.getPassword());
291 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
292 this.mRegisterNew.setVisibility(View.VISIBLE);
293 this.mRegisterNew.setChecked(true);
294 this.mPasswordConfirm.setText(this.mAccount.getPassword());
295 } else {
296 this.mRegisterNew.setVisibility(View.GONE);
297 this.mRegisterNew.setChecked(false);
298 }
299 }
300}