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.Menu;
9import android.view.MenuItem;
10import android.view.View;
11import android.view.View.OnClickListener;
12import android.widget.AutoCompleteTextView;
13import android.widget.Button;
14import android.widget.CheckBox;
15import android.widget.CompoundButton;
16import android.widget.CompoundButton.OnCheckedChangeListener;
17import android.widget.EditText;
18import android.widget.ImageButton;
19import android.widget.ImageView;
20import android.widget.LinearLayout;
21import android.widget.RelativeLayout;
22import android.widget.TableLayout;
23import android.widget.TextView;
24import android.widget.Toast;
25
26import eu.siacs.conversations.R;
27import eu.siacs.conversations.entities.Account;
28import eu.siacs.conversations.services.XmppConnectionService;
29import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
30import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
31import eu.siacs.conversations.utils.CryptoHelper;
32import eu.siacs.conversations.utils.UIHelper;
33import eu.siacs.conversations.xmpp.XmppConnection.Features;
34import eu.siacs.conversations.xmpp.jid.InvalidJidException;
35import eu.siacs.conversations.xmpp.jid.Jid;
36import eu.siacs.conversations.xmpp.pep.Avatar;
37
38public class EditAccountActivity extends XmppActivity implements OnAccountUpdate, XmppConnectionService.OnAccountPasswordChanged {
39
40 private AutoCompleteTextView mAccountJid;
41 private EditText mPassword;
42 private EditText mPasswordConfirm;
43 private CheckBox mRegisterNew;
44 private CheckBox mChangePassword;
45 private Button mCancelButton;
46 private Button mSaveButton;
47 private TableLayout mMoreTable;
48
49 private LinearLayout mStats;
50 private TextView mServerInfoSm;
51 private TextView mServerInfoRosterVersion;
52 private TextView mServerInfoCarbons;
53 private TextView mServerInfoMam;
54 private TextView mServerInfoCSI;
55 private TextView mServerInfoBlocking;
56 private TextView mServerInfoPep;
57 private TextView mSessionEst;
58 private TextView mOtrFingerprint;
59 private ImageView mAvatar;
60 private RelativeLayout mOtrFingerprintBox;
61 private ImageButton mOtrFingerprintToClipboardButton;
62
63 private Jid jidToEdit;
64 private Account mAccount;
65
66 private boolean mFetchingAvatar = false;
67 private boolean mChangingPassword = false;
68
69 private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
70
71 @Override
72 public void onClick(final View v) {
73 if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
74 mAccount.setOption(Account.OPTION_DISABLED, false);
75 xmppConnectionService.updateAccount(mAccount);
76 return;
77 }
78 final boolean registerNewAccount = mRegisterNew.isChecked();
79 final boolean changePassword = mChangePassword.isChecked();
80 final Jid jid;
81 try {
82 jid = Jid.fromString(mAccountJid.getText().toString());
83 } catch (final InvalidJidException e) {
84 mAccountJid.setError(getString(R.string.invalid_jid));
85 mAccountJid.requestFocus();
86 return;
87 }
88 if (jid.isDomainJid()) {
89 mAccountJid.setError(getString(R.string.invalid_jid));
90 mAccountJid.requestFocus();
91 return;
92 }
93 final String password = mPassword.getText().toString();
94 final String passwordConfirm = mPasswordConfirm.getText().toString();
95 if (registerNewAccount || changePassword) {
96 if (!password.equals(passwordConfirm)) {
97 mPasswordConfirm.setError(getString(R.string.passwords_do_not_match));
98 mPasswordConfirm.requestFocus();
99 return;
100 }
101 }
102 if (mAccount != null) {
103 try {
104 mAccount.setUsername(jid.hasLocalpart() ? jid.getLocalpart() : "");
105 mAccount.setServer(jid.getDomainpart());
106 } catch (final InvalidJidException ignored) {
107 }
108 if (changePassword) {
109 if (mAccount.isOnlineAndConnected()) {
110 xmppConnectionService.updateAccountPasswordOnServer(mAccount, mPassword.getText().toString(),EditAccountActivity.this);
111 mChangingPassword = true;
112 updateSaveButton();
113 } else {
114 Toast.makeText(EditAccountActivity.this,R.string.not_connected_try_again,Toast.LENGTH_SHORT).show();
115 }
116 return;
117 } else {
118 mAccount.setPassword(password);
119 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
120 xmppConnectionService.updateAccount(mAccount);
121 }
122 } else {
123 try {
124 if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
125 mAccountJid.setError(getString(R.string.account_already_exists));
126 mAccountJid.requestFocus();
127 return;
128 }
129 } catch (final InvalidJidException e) {
130 return;
131 }
132 mAccount = new Account(jid.toBareJid(), password);
133 mAccount.setOption(Account.OPTION_USETLS, true);
134 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
135 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
136 xmppConnectionService.createAccount(mAccount);
137 }
138 if (jidToEdit != null) {
139 finish();
140 } else {
141 updateSaveButton();
142 updateAccountInformation();
143 }
144
145 }
146 };
147 private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
148
149 @Override
150 public void onClick(final View v) {
151 finish();
152 }
153 };
154 @Override
155 public void onAccountUpdate() {
156 runOnUiThread(new Runnable() {
157
158 @Override
159 public void run() {
160 if (mAccount != null
161 && mAccount.getStatus() != Account.State.ONLINE
162 && mFetchingAvatar) {
163 startActivity(new Intent(getApplicationContext(),
164 ManageAccountActivity.class));
165 finish();
166 } else if (jidToEdit == null && mAccount != null
167 && mAccount.getStatus() == Account.State.ONLINE) {
168 if (!mFetchingAvatar) {
169 mFetchingAvatar = true;
170 xmppConnectionService.checkForAvatar(mAccount,
171 mAvatarFetchCallback);
172 }
173 } else {
174 updateSaveButton();
175 }
176 if (mAccount != null) {
177 updateAccountInformation();
178 }
179 }
180 });
181 }
182 private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
183
184 @Override
185 public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
186 finishInitialSetup(avatar);
187 }
188
189 @Override
190 public void success(final Avatar avatar) {
191 finishInitialSetup(avatar);
192 }
193
194 @Override
195 public void error(final int errorCode, final Avatar avatar) {
196 finishInitialSetup(avatar);
197 }
198 };
199 private final TextWatcher mTextWatcher = new TextWatcher() {
200
201 @Override
202 public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
203 updateSaveButton();
204 }
205
206 @Override
207 public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
208 }
209
210 @Override
211 public void afterTextChanged(final Editable s) {
212 toggleChangePasswordCheckbox();
213 }
214 };
215
216 private void toggleChangePasswordCheckbox() {
217 final boolean registrationReady = mAccount != null &&
218 mAccount.isOnlineAndConnected() &&
219 mAccount.getXmppConnection().getFeatures().register();
220 if (passwordFieldEdited() && registrationReady) {
221 mChangePassword.setVisibility(View.VISIBLE);
222 } else {
223 mChangePassword.setVisibility(View.INVISIBLE);
224 mChangePassword.setChecked(false);
225 }
226 }
227
228 private boolean passwordFieldEdited() {
229 final String password = this.mPassword.getText().toString();
230 return jidToEdit != null && mAccount != null && !password.isEmpty() && !mAccount.getPassword().equals(password);
231 }
232 private final OnClickListener mAvatarClickListener = new OnClickListener() {
233 @Override
234 public void onClick(final View view) {
235 if (mAccount != null) {
236 final Intent intent = new Intent(getApplicationContext(),
237 PublishProfilePictureActivity.class);
238 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
239 startActivity(intent);
240 }
241 }
242 };
243
244 protected void finishInitialSetup(final Avatar avatar) {
245 runOnUiThread(new Runnable() {
246
247 @Override
248 public void run() {
249 final Intent intent;
250 if (avatar != null) {
251 intent = new Intent(getApplicationContext(),
252 StartConversationActivity.class);
253 } else {
254 intent = new Intent(getApplicationContext(),
255 PublishProfilePictureActivity.class);
256 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
257 intent.putExtra("setup", true);
258 }
259 startActivity(intent);
260 finish();
261 }
262 });
263 }
264
265 protected void updateSaveButton() {
266 if (mChangingPassword) {
267 this.mSaveButton.setEnabled(false);
268 this.mSaveButton.setTextColor(getSecondaryTextColor());
269 this.mSaveButton.setText(R.string.updating);
270 } else if (mAccount != null && mAccount.getStatus() == Account.State.CONNECTING) {
271 this.mSaveButton.setEnabled(false);
272 this.mSaveButton.setTextColor(getSecondaryTextColor());
273 this.mSaveButton.setText(R.string.account_status_connecting);
274 } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
275 this.mSaveButton.setEnabled(true);
276 this.mSaveButton.setTextColor(getPrimaryTextColor());
277 this.mSaveButton.setText(R.string.enable);
278 } else {
279 this.mSaveButton.setEnabled(true);
280 this.mSaveButton.setTextColor(getPrimaryTextColor());
281 if (jidToEdit != null) {
282 if (mAccount != null && mAccount.isOnlineAndConnected()) {
283 this.mSaveButton.setText(R.string.save);
284 if (!accountInfoEdited()) {
285 this.mSaveButton.setEnabled(false);
286 this.mSaveButton.setTextColor(getSecondaryTextColor());
287 }
288 } else {
289 this.mSaveButton.setText(R.string.connect);
290 }
291 } else {
292 this.mSaveButton.setText(R.string.next);
293 }
294 }
295 }
296
297 protected boolean accountInfoEdited() {
298 return (!this.mAccount.getJid().toBareJid().toString().equals(
299 this.mAccountJid.getText().toString()))
300 || (!this.mAccount.getPassword().equals(
301 this.mPassword.getText().toString()));
302 }
303
304 @Override
305 protected String getShareableUri() {
306 if (mAccount!=null) {
307 return mAccount.getShareableUri();
308 } else {
309 return "";
310 }
311 }
312
313 @Override
314 protected void onCreate(final Bundle savedInstanceState) {
315 super.onCreate(savedInstanceState);
316 setContentView(R.layout.activity_edit_account);
317 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
318 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
319 this.mPassword = (EditText) findViewById(R.id.account_password);
320 this.mPassword.addTextChangedListener(this.mTextWatcher);
321 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
322 this.mAvatar = (ImageView) findViewById(R.id.avater);
323 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
324 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
325 this.mChangePassword = (CheckBox) findViewById(R.id.account_change_password);
326 this.mStats = (LinearLayout) findViewById(R.id.stats);
327 this.mSessionEst = (TextView) findViewById(R.id.session_est);
328 this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
329 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
330 this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
331 this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
332 this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
333 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
334 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
335 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
336 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
337 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
338 this.mSaveButton = (Button) findViewById(R.id.save_button);
339 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
340 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
341 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
342 this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
343 final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
344 @Override
345 public void onCheckedChanged(final CompoundButton buttonView,
346 final boolean isChecked) {
347 if (isChecked) {
348 mPasswordConfirm.setVisibility(View.VISIBLE);
349 } else {
350 mPasswordConfirm.setVisibility(View.GONE);
351 }
352 updateSaveButton();
353 }
354 };
355 this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
356 this.mChangePassword.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
357 }
358
359 @Override
360 public boolean onCreateOptionsMenu(final Menu menu) {
361 super.onCreateOptionsMenu(menu);
362 getMenuInflater().inflate(R.menu.editaccount, menu);
363 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
364 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
365 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
366 if (mAccount == null) {
367 showQrCode.setVisible(false);
368 showBlocklist.setVisible(false);
369 showMoreInfo.setVisible(false);
370 } else if (mAccount.getStatus() != Account.State.ONLINE) {
371 showBlocklist.setVisible(false);
372 showMoreInfo.setVisible(false);
373 } else if (!mAccount.getXmppConnection().getFeatures().blocking()) {
374 showBlocklist.setVisible(false);
375 }
376 return true;
377 }
378
379 @Override
380 protected void onStart() {
381 super.onStart();
382 if (getIntent() != null) {
383 try {
384 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
385 } catch (final InvalidJidException | NullPointerException ignored) {
386 this.jidToEdit = null;
387 }
388 if (this.jidToEdit != null) {
389 this.mRegisterNew.setVisibility(View.GONE);
390 if (getActionBar() != null) {
391 getActionBar().setTitle(getString(R.string.account_details));
392 }
393 } else {
394 this.mAvatar.setVisibility(View.GONE);
395 if (getActionBar() != null) {
396 getActionBar().setTitle(R.string.action_add_account);
397 }
398 }
399 this.mChangePassword.setVisibility(View.GONE);
400 this.mChangePassword.setChecked(false);
401 }
402 }
403
404 @Override
405 protected void onBackendConnected() {
406 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
407 android.R.layout.simple_list_item_1,
408 xmppConnectionService.getKnownHosts());
409 if (this.jidToEdit != null) {
410 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
411 updateAccountInformation();
412 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
413 if (getActionBar() != null) {
414 getActionBar().setDisplayHomeAsUpEnabled(false);
415 getActionBar().setDisplayShowHomeEnabled(false);
416 }
417 this.mCancelButton.setEnabled(false);
418 this.mCancelButton.setTextColor(getSecondaryTextColor());
419 }
420 this.mAccountJid.setAdapter(mKnownHostsAdapter);
421 updateSaveButton();
422 }
423
424 @Override
425 public boolean onOptionsItemSelected(final MenuItem item) {
426 switch (item.getItemId()) {
427 case R.id.action_show_block_list:
428 final Intent intent = new Intent(this, BlocklistActivity.class);
429 intent.putExtra("account", mAccount.getJid().toString());
430 startActivity(intent);
431 break;
432 case R.id.action_server_info_show_more:
433 mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
434 item.setChecked(!item.isChecked());
435 }
436 return super.onOptionsItemSelected(item);
437 }
438
439 private void updateAccountInformation() {
440 this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
441 this.mPassword.setText(this.mAccount.getPassword());
442 if (this.jidToEdit != null) {
443 this.mAvatar.setVisibility(View.VISIBLE);
444 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
445 }
446 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
447 this.mRegisterNew.setVisibility(View.VISIBLE);
448 this.mChangePassword.setVisibility(View.GONE);
449 this.mChangePassword.setChecked(false);
450 this.mRegisterNew.setChecked(true);
451 this.mPasswordConfirm.setText(this.mAccount.getPassword());
452 } else {
453 this.mRegisterNew.setVisibility(View.GONE);
454 this.mRegisterNew.setChecked(false);
455 this.mChangePassword.setVisibility(View.GONE);
456 this.mChangePassword.setChecked(false);
457 }
458 if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
459 toggleChangePasswordCheckbox();
460 this.mStats.setVisibility(View.VISIBLE);
461 this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
462 .getLastSessionEstablished()));
463 Features features = this.mAccount.getXmppConnection().getFeatures();
464 if (features.rosterVersioning()) {
465 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
466 } else {
467 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
468 }
469 if (features.carbons()) {
470 this.mServerInfoCarbons.setText(R.string.server_info_available);
471 } else {
472 this.mServerInfoCarbons
473 .setText(R.string.server_info_unavailable);
474 }
475 if (features.mam()) {
476 this.mServerInfoMam.setText(R.string.server_info_available);
477 } else {
478 this.mServerInfoMam.setText(R.string.server_info_unavailable);
479 }
480 if (features.csi()) {
481 this.mServerInfoCSI.setText(R.string.server_info_available);
482 } else {
483 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
484 }
485 if (features.blocking()) {
486 this.mServerInfoBlocking.setText(R.string.server_info_available);
487 } else {
488 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
489 }
490 if (features.sm()) {
491 this.mServerInfoSm.setText(R.string.server_info_available);
492 } else {
493 this.mServerInfoSm.setText(R.string.server_info_unavailable);
494 }
495 if (features.pubsub()) {
496 this.mServerInfoPep.setText(R.string.server_info_available);
497 } else {
498 this.mServerInfoPep.setText(R.string.server_info_unavailable);
499 }
500 final String fingerprint = this.mAccount.getOtrFingerprint();
501 if (fingerprint != null) {
502 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
503 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
504 this.mOtrFingerprintToClipboardButton
505 .setVisibility(View.VISIBLE);
506 this.mOtrFingerprintToClipboardButton
507 .setOnClickListener(new View.OnClickListener() {
508
509 @Override
510 public void onClick(final View v) {
511
512 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
513 Toast.makeText(
514 EditAccountActivity.this,
515 R.string.toast_message_otr_fingerprint,
516 Toast.LENGTH_SHORT).show();
517 }
518 }
519 });
520 } else {
521 this.mOtrFingerprintBox.setVisibility(View.GONE);
522 }
523 } else {
524 if (this.mAccount.errorStatus()) {
525 this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
526 this.mAccountJid.requestFocus();
527 }
528 this.mStats.setVisibility(View.GONE);
529 }
530 }
531
532 @Override
533 public void onPasswordChangeSucceeded() {
534 this.mChangingPassword = false;
535 runOnUiThread(new Runnable() {
536 @Override
537 public void run() {
538 Toast.makeText(EditAccountActivity.this,R.string.password_changed,Toast.LENGTH_SHORT).show();
539 updateSaveButton();
540 updateAccountInformation();
541 }
542 });
543 }
544
545 @Override
546 public void onPasswordChangeFailed() {
547 this.mChangingPassword = false;
548 runOnUiThread(new Runnable() {
549 @Override
550 public void run() {
551 mPassword.requestFocus();
552 mPassword.setError(getString(R.string.could_not_change_password));
553 updateSaveButton();
554 }
555 });
556 }
557}