1package eu.siacs.conversations.ui;
2
3import android.app.AlertDialog;
4import android.app.AlertDialog.Builder;
5import android.app.PendingIntent;
6import android.content.DialogInterface;
7import android.content.Intent;
8import android.content.SharedPreferences;
9import android.graphics.Bitmap;
10import android.net.Uri;
11import android.os.Bundle;
12import android.provider.Settings;
13import android.security.KeyChain;
14import android.security.KeyChainAliasCallback;
15import android.text.Editable;
16import android.text.TextWatcher;
17import android.view.Menu;
18import android.view.MenuItem;
19import android.view.View;
20import android.view.View.OnClickListener;
21import android.widget.AutoCompleteTextView;
22import android.widget.Button;
23import android.widget.CheckBox;
24import android.widget.CompoundButton;
25import android.widget.CompoundButton.OnCheckedChangeListener;
26import android.widget.EditText;
27import android.widget.ImageButton;
28import android.widget.ImageView;
29import android.widget.LinearLayout;
30import android.widget.RelativeLayout;
31import android.widget.TableLayout;
32import android.widget.TextView;
33import android.widget.Toast;
34
35import java.util.Set;
36
37import eu.siacs.conversations.Config;
38import eu.siacs.conversations.R;
39import eu.siacs.conversations.crypto.axolotl.AxolotlService;
40import eu.siacs.conversations.entities.Account;
41import eu.siacs.conversations.services.XmppConnectionService.OnCaptchaRequested;
42import eu.siacs.conversations.services.XmppConnectionService;
43import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
44import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
45import eu.siacs.conversations.utils.CryptoHelper;
46import eu.siacs.conversations.utils.UIHelper;
47import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
48import eu.siacs.conversations.xmpp.XmppConnection.Features;
49import eu.siacs.conversations.xmpp.forms.Data;
50import eu.siacs.conversations.xmpp.jid.InvalidJidException;
51import eu.siacs.conversations.xmpp.jid.Jid;
52import eu.siacs.conversations.xmpp.pep.Avatar;
53
54public class EditAccountActivity extends XmppActivity implements OnAccountUpdate,
55 OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast {
56
57 private AutoCompleteTextView mAccountJid;
58 private EditText mPassword;
59 private EditText mPasswordConfirm;
60 private CheckBox mRegisterNew;
61 private Button mCancelButton;
62 private Button mSaveButton;
63 private Button mDisableBatterOptimizations;
64 private TableLayout mMoreTable;
65
66 private LinearLayout mStats;
67 private RelativeLayout mBatteryOptimizations;
68 private TextView mServerInfoSm;
69 private TextView mServerInfoRosterVersion;
70 private TextView mServerInfoCarbons;
71 private TextView mServerInfoMam;
72 private TextView mServerInfoCSI;
73 private TextView mServerInfoBlocking;
74 private TextView mServerInfoPep;
75 private TextView mServerInfoHttpUpload;
76 private TextView mSessionEst;
77 private TextView mOtrFingerprint;
78 private TextView mAxolotlFingerprint;
79 private TextView mAccountJidLabel;
80 private ImageView mAvatar;
81 private RelativeLayout mOtrFingerprintBox;
82 private RelativeLayout mAxolotlFingerprintBox;
83 private ImageButton mOtrFingerprintToClipboardButton;
84 private ImageButton mAxolotlFingerprintToClipboardButton;
85 private ImageButton mRegenerateAxolotlKeyButton;
86 private LinearLayout keys;
87 private LinearLayout keysCard;
88 private LinearLayout mNamePort;
89 private EditText mHostname;
90 private EditText mPort;
91 private AlertDialog mCaptchaDialog = null;
92
93 private Jid jidToEdit;
94 private boolean mInitMode = false;
95 private boolean mShowOptions = false;
96 private Account mAccount;
97 private String messageFingerprint;
98
99 private boolean mFetchingAvatar = false;
100
101 private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
102
103 @Override
104 public void onClick(final View v) {
105 if (mInitMode && mAccount != null) {
106 mAccount.setOption(Account.OPTION_DISABLED, false);
107 }
108 if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !accountInfoEdited()) {
109 mAccount.setOption(Account.OPTION_DISABLED, false);
110 xmppConnectionService.updateAccount(mAccount);
111 return;
112 }
113 final boolean registerNewAccount = mRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI;
114 if (Config.DOMAIN_LOCK != null && mAccountJid.getText().toString().contains("@")) {
115 mAccountJid.setError(getString(R.string.invalid_username));
116 mAccountJid.requestFocus();
117 return;
118 }
119 final Jid jid;
120 try {
121 if (Config.DOMAIN_LOCK != null) {
122 jid = Jid.fromParts(mAccountJid.getText().toString(), Config.DOMAIN_LOCK, null);
123 } else {
124 jid = Jid.fromString(mAccountJid.getText().toString());
125 }
126 } catch (final InvalidJidException e) {
127 if (Config.DOMAIN_LOCK != null) {
128 mAccountJid.setError(getString(R.string.invalid_username));
129 } else {
130 mAccountJid.setError(getString(R.string.invalid_jid));
131 }
132 mAccountJid.requestFocus();
133 return;
134 }
135 String hostname = null;
136 int numericPort = 5222;
137 if (mShowOptions) {
138 hostname = mHostname.getText().toString();
139 final String port = mPort.getText().toString();
140 if (hostname.contains(" ")) {
141 mHostname.setError(getString(R.string.not_valid_hostname));
142 mHostname.requestFocus();
143 return;
144 }
145 try {
146 numericPort = Integer.parseInt(port);
147 if (numericPort < 0 || numericPort > 65535) {
148 mPort.setError(getString(R.string.not_a_valid_port));
149 mPort.requestFocus();
150 return;
151 }
152
153 } catch (NumberFormatException e) {
154 mPort.setError(getString(R.string.not_a_valid_port));
155 mPort.requestFocus();
156 return;
157 }
158 }
159
160 if (jid.isDomainJid()) {
161 if (Config.DOMAIN_LOCK != null) {
162 mAccountJid.setError(getString(R.string.invalid_username));
163 } else {
164 mAccountJid.setError(getString(R.string.invalid_jid));
165 }
166 mAccountJid.requestFocus();
167 return;
168 }
169 final String password = mPassword.getText().toString();
170 final String passwordConfirm = mPasswordConfirm.getText().toString();
171 if (registerNewAccount) {
172 if (!password.equals(passwordConfirm)) {
173 mPasswordConfirm.setError(getString(R.string.passwords_do_not_match));
174 mPasswordConfirm.requestFocus();
175 return;
176 }
177 }
178 if (mAccount != null) {
179 mAccount.setJid(jid);
180 mAccount.setPort(numericPort);
181 mAccount.setHostname(hostname);
182 mAccountJid.setError(null);
183 mPasswordConfirm.setError(null);
184 mAccount.setPassword(password);
185 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
186 xmppConnectionService.updateAccount(mAccount);
187 } else {
188 if (xmppConnectionService.findAccountByJid(jid) != null) {
189 mAccountJid.setError(getString(R.string.account_already_exists));
190 mAccountJid.requestFocus();
191 return;
192 }
193 mAccount = new Account(jid.toBareJid(), password);
194 mAccount.setPort(numericPort);
195 mAccount.setHostname(hostname);
196 mAccount.setOption(Account.OPTION_USETLS, true);
197 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
198 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
199 xmppConnectionService.createAccount(mAccount);
200 }
201 mHostname.setError(null);
202 mPort.setError(null);
203 if (!mAccount.isOptionSet(Account.OPTION_DISABLED)
204 && !registerNewAccount
205 && !mInitMode) {
206 finish();
207 } else {
208 updateSaveButton();
209 updateAccountInformation(true);
210 }
211
212 }
213 };
214 private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
215
216 @Override
217 public void onClick(final View v) {
218 finish();
219 }
220 };
221
222 public void refreshUiReal() {
223 invalidateOptionsMenu();
224 if (mAccount != null
225 && mAccount.getStatus() != Account.State.ONLINE
226 && mFetchingAvatar) {
227 startActivity(new Intent(getApplicationContext(),
228 ManageAccountActivity.class));
229 finish();
230 } else if (mInitMode && mAccount != null && mAccount.getStatus() == Account.State.ONLINE) {
231 if (!mFetchingAvatar) {
232 mFetchingAvatar = true;
233 xmppConnectionService.checkForAvatar(mAccount, mAvatarFetchCallback);
234 }
235 } else {
236 updateSaveButton();
237 }
238 if (mAccount != null) {
239 updateAccountInformation(false);
240 }
241 }
242
243 @Override
244 public void onAccountUpdate() {
245 refreshUi();
246 }
247
248 private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
249
250 @Override
251 public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
252 finishInitialSetup(avatar);
253 }
254
255 @Override
256 public void success(final Avatar avatar) {
257 finishInitialSetup(avatar);
258 }
259
260 @Override
261 public void error(final int errorCode, final Avatar avatar) {
262 finishInitialSetup(avatar);
263 }
264 };
265 private final TextWatcher mTextWatcher = new TextWatcher() {
266
267 @Override
268 public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
269 updateSaveButton();
270 }
271
272 @Override
273 public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
274 }
275
276 @Override
277 public void afterTextChanged(final Editable s) {
278
279 }
280 };
281
282 private final OnClickListener mAvatarClickListener = new OnClickListener() {
283 @Override
284 public void onClick(final View view) {
285 if (mAccount != null) {
286 final Intent intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
287 intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
288 startActivity(intent);
289 }
290 }
291 };
292
293 protected void finishInitialSetup(final Avatar avatar) {
294 runOnUiThread(new Runnable() {
295
296 @Override
297 public void run() {
298 final Intent intent;
299 if (avatar != null) {
300 intent = new Intent(getApplicationContext(),
301 StartConversationActivity.class);
302 if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) {
303 intent.putExtra("init", true);
304 }
305 } else {
306 intent = new Intent(getApplicationContext(),
307 PublishProfilePictureActivity.class);
308 intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
309 intent.putExtra("setup", true);
310 }
311 startActivity(intent);
312 finish();
313 }
314 });
315 }
316
317 @Override
318 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
319 super.onActivityResult(requestCode, resultCode, data);
320 if (requestCode == REQUEST_BATTERY_OP) {
321 updateAccountInformation(mAccount == null);
322 }
323 }
324
325 protected void updateSaveButton() {
326 if (accountInfoEdited() && !mInitMode) {
327 this.mSaveButton.setText(R.string.save);
328 this.mSaveButton.setEnabled(true);
329 this.mSaveButton.setTextColor(getPrimaryTextColor());
330 } else if (mAccount != null && (mAccount.getStatus() == Account.State.CONNECTING || mFetchingAvatar)) {
331 this.mSaveButton.setEnabled(false);
332 this.mSaveButton.setTextColor(getSecondaryTextColor());
333 this.mSaveButton.setText(R.string.account_status_connecting);
334 } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) {
335 this.mSaveButton.setEnabled(true);
336 this.mSaveButton.setTextColor(getPrimaryTextColor());
337 this.mSaveButton.setText(R.string.enable);
338 } else {
339 this.mSaveButton.setEnabled(true);
340 this.mSaveButton.setTextColor(getPrimaryTextColor());
341 if (!mInitMode) {
342 if (mAccount != null && mAccount.isOnlineAndConnected()) {
343 this.mSaveButton.setText(R.string.save);
344 if (!accountInfoEdited()) {
345 this.mSaveButton.setEnabled(false);
346 this.mSaveButton.setTextColor(getSecondaryTextColor());
347 }
348 } else {
349 this.mSaveButton.setText(R.string.connect);
350 }
351 } else {
352 this.mSaveButton.setText(R.string.next);
353 }
354 }
355 }
356
357 protected boolean accountInfoEdited() {
358 if (this.mAccount == null) {
359 return false;
360 }
361 final String unmodified;
362 if (Config.DOMAIN_LOCK != null) {
363 unmodified = this.mAccount.getJid().getLocalpart();
364 } else {
365 unmodified = this.mAccount.getJid().toBareJid().toString();
366 }
367 return !unmodified.equals(this.mAccountJid.getText().toString()) ||
368 !this.mAccount.getPassword().equals(this.mPassword.getText().toString()) ||
369 !this.mAccount.getHostname().equals(this.mHostname.getText().toString()) ||
370 !String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString());
371 }
372
373 @Override
374 protected String getShareableUri() {
375 if (mAccount != null) {
376 return mAccount.getShareableUri();
377 } else {
378 return "";
379 }
380 }
381
382 @Override
383 protected void onCreate(final Bundle savedInstanceState) {
384 super.onCreate(savedInstanceState);
385 setContentView(R.layout.activity_edit_account);
386 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
387 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
388 this.mAccountJidLabel = (TextView) findViewById(R.id.account_jid_label);
389 if (Config.DOMAIN_LOCK != null) {
390 this.mAccountJidLabel.setText(R.string.username);
391 this.mAccountJid.setHint(R.string.username_hint);
392 }
393 this.mPassword = (EditText) findViewById(R.id.account_password);
394 this.mPassword.addTextChangedListener(this.mTextWatcher);
395 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
396 this.mAvatar = (ImageView) findViewById(R.id.avater);
397 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
398 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
399 this.mStats = (LinearLayout) findViewById(R.id.stats);
400 this.mBatteryOptimizations = (RelativeLayout) findViewById(R.id.battery_optimization);
401 this.mDisableBatterOptimizations = (Button) findViewById(R.id.batt_op_disable);
402 this.mDisableBatterOptimizations.setOnClickListener(new OnClickListener() {
403 @Override
404 public void onClick(View v) {
405 Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
406 Uri uri = Uri.parse("package:"+getPackageName());
407 intent.setData(uri);
408 startActivityForResult(intent,REQUEST_BATTERY_OP);
409 }
410 });
411 this.mSessionEst = (TextView) findViewById(R.id.session_est);
412 this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
413 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
414 this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
415 this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
416 this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
417 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
418 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
419 this.mServerInfoHttpUpload = (TextView) findViewById(R.id.server_info_http_upload);
420 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
421 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
422 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
423 this.mAxolotlFingerprint = (TextView) findViewById(R.id.axolotl_fingerprint);
424 this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box);
425 this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard);
426 this.mRegenerateAxolotlKeyButton = (ImageButton) findViewById(R.id.action_regenerate_axolotl_key);
427 this.keysCard = (LinearLayout) findViewById(R.id.other_device_keys_card);
428 this.keys = (LinearLayout) findViewById(R.id.other_device_keys);
429 this.mNamePort = (LinearLayout) findViewById(R.id.name_port);
430 this.mHostname = (EditText) findViewById(R.id.hostname);
431 this.mHostname.addTextChangedListener(mTextWatcher);
432 this.mPort = (EditText) findViewById(R.id.port);
433 this.mPort.setText("5222");
434 this.mPort.addTextChangedListener(mTextWatcher);
435 this.mSaveButton = (Button) findViewById(R.id.save_button);
436 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
437 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
438 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
439 this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
440 final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
441 @Override
442 public void onCheckedChanged(final CompoundButton buttonView,
443 final boolean isChecked) {
444 if (isChecked) {
445 mPasswordConfirm.setVisibility(View.VISIBLE);
446 } else {
447 mPasswordConfirm.setVisibility(View.GONE);
448 }
449 updateSaveButton();
450 }
451 };
452 this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
453 if (Config.DISALLOW_REGISTRATION_IN_UI) {
454 this.mRegisterNew.setVisibility(View.GONE);
455 }
456 }
457
458 @Override
459 public boolean onCreateOptionsMenu(final Menu menu) {
460 super.onCreateOptionsMenu(menu);
461 getMenuInflater().inflate(R.menu.editaccount, menu);
462 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
463 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
464 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
465 final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
466 final MenuItem clearDevices = menu.findItem(R.id.action_clear_devices);
467 final MenuItem renewCertificate = menu.findItem(R.id.action_renew_certificate);
468
469 renewCertificate.setVisible(mAccount != null && mAccount.getPrivateKeyAlias() != null);
470
471 if (mAccount != null && mAccount.isOnlineAndConnected()) {
472 if (!mAccount.getXmppConnection().getFeatures().blocking()) {
473 showBlocklist.setVisible(false);
474 }
475 if (!mAccount.getXmppConnection().getFeatures().register()) {
476 changePassword.setVisible(false);
477 }
478 Set<Integer> otherDevices = mAccount.getAxolotlService().getOwnDeviceIds();
479 if (otherDevices == null || otherDevices.isEmpty()) {
480 clearDevices.setVisible(false);
481 }
482 } else {
483 showQrCode.setVisible(false);
484 showBlocklist.setVisible(false);
485 showMoreInfo.setVisible(false);
486 changePassword.setVisible(false);
487 clearDevices.setVisible(false);
488 }
489 return true;
490 }
491
492 @Override
493 protected void onStart() {
494 super.onStart();
495 if (getIntent() != null) {
496 try {
497 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
498 } catch (final InvalidJidException | NullPointerException ignored) {
499 this.jidToEdit = null;
500 }
501 this.mInitMode = getIntent().getBooleanExtra("init", false) || this.jidToEdit == null;
502 this.messageFingerprint = getIntent().getStringExtra("fingerprint");
503 if (!mInitMode) {
504 this.mRegisterNew.setVisibility(View.GONE);
505 if (getActionBar() != null) {
506 getActionBar().setTitle(getString(R.string.account_details));
507 }
508 } else {
509 this.mAvatar.setVisibility(View.GONE);
510 if (getActionBar() != null) {
511 getActionBar().setTitle(R.string.action_add_account);
512 }
513 }
514 }
515 SharedPreferences preferences = getPreferences();
516 boolean useTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
517 this.mShowOptions = useTor || preferences.getBoolean("show_connection_options", false);
518 mHostname.setHint(useTor ? R.string.hostname_or_onion : R.string.hostname_example);
519 this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
520 }
521
522 @Override
523 protected void onBackendConnected() {
524 if (this.jidToEdit != null) {
525 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
526 if (this.mAccount != null) {
527 if (this.mAccount.getPrivateKeyAlias() != null) {
528 this.mPassword.setHint(R.string.authenticate_with_certificate);
529 if (this.mInitMode) {
530 this.mPassword.requestFocus();
531 }
532 }
533 updateAccountInformation(true);
534 }
535 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
536 if (getActionBar() != null) {
537 getActionBar().setDisplayHomeAsUpEnabled(false);
538 getActionBar().setDisplayShowHomeEnabled(false);
539 getActionBar().setHomeButtonEnabled(false);
540 }
541 this.mCancelButton.setEnabled(false);
542 this.mCancelButton.setTextColor(getSecondaryTextColor());
543 }
544 if (Config.DOMAIN_LOCK == null) {
545 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
546 android.R.layout.simple_list_item_1,
547 xmppConnectionService.getKnownHosts());
548 this.mAccountJid.setAdapter(mKnownHostsAdapter);
549 }
550 updateSaveButton();
551 invalidateOptionsMenu();
552 }
553
554 @Override
555 public boolean onOptionsItemSelected(final MenuItem item) {
556 switch (item.getItemId()) {
557 case R.id.action_show_block_list:
558 final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
559 showBlocklistIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
560 startActivity(showBlocklistIntent);
561 break;
562 case R.id.action_server_info_show_more:
563 mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
564 item.setChecked(!item.isChecked());
565 break;
566 case R.id.action_change_password_on_server:
567 final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
568 changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
569 startActivity(changePasswordIntent);
570 break;
571 case R.id.action_clear_devices:
572 showWipePepDialog();
573 break;
574 case R.id.action_renew_certificate:
575 renewCertificate();
576 break;
577 }
578 return super.onOptionsItemSelected(item);
579 }
580
581 private void renewCertificate() {
582 KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null);
583 }
584
585 @Override
586 public void alias(String alias) {
587 if (alias != null) {
588 xmppConnectionService.updateKeyInAccount(mAccount, alias);
589 }
590 }
591
592 private void updateAccountInformation(boolean init) {
593 if (init) {
594 this.mAccountJid.getEditableText().clear();
595 if (Config.DOMAIN_LOCK != null) {
596 this.mAccountJid.getEditableText().append(this.mAccount.getJid().getLocalpart());
597 } else {
598 this.mAccountJid.getEditableText().append(this.mAccount.getJid().toBareJid().toString());
599 }
600 this.mPassword.setText(this.mAccount.getPassword());
601 this.mHostname.setText("");
602 this.mHostname.getEditableText().append(this.mAccount.getHostname());
603 this.mPort.setText("");
604 this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort()));
605 this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
606
607 }
608 if (!mInitMode) {
609 this.mAvatar.setVisibility(View.VISIBLE);
610 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
611 }
612 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
613 this.mRegisterNew.setVisibility(View.VISIBLE);
614 this.mRegisterNew.setChecked(true);
615 this.mPasswordConfirm.setText(this.mAccount.getPassword());
616 } else {
617 this.mRegisterNew.setVisibility(View.GONE);
618 this.mRegisterNew.setChecked(false);
619 }
620 if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
621 this.mStats.setVisibility(View.VISIBLE);
622 this.mBatteryOptimizations.setVisibility(showBatteryOptimizationWarning() ? View.VISIBLE : View.GONE);
623 this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
624 .getLastSessionEstablished()));
625 Features features = this.mAccount.getXmppConnection().getFeatures();
626 if (features.rosterVersioning()) {
627 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
628 } else {
629 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
630 }
631 if (features.carbons()) {
632 this.mServerInfoCarbons.setText(R.string.server_info_available);
633 } else {
634 this.mServerInfoCarbons
635 .setText(R.string.server_info_unavailable);
636 }
637 if (features.mam()) {
638 this.mServerInfoMam.setText(R.string.server_info_available);
639 } else {
640 this.mServerInfoMam.setText(R.string.server_info_unavailable);
641 }
642 if (features.csi()) {
643 this.mServerInfoCSI.setText(R.string.server_info_available);
644 } else {
645 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
646 }
647 if (features.blocking()) {
648 this.mServerInfoBlocking.setText(R.string.server_info_available);
649 } else {
650 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
651 }
652 if (features.sm()) {
653 this.mServerInfoSm.setText(R.string.server_info_available);
654 } else {
655 this.mServerInfoSm.setText(R.string.server_info_unavailable);
656 }
657 if (features.pep()) {
658 AxolotlService axolotlService = this.mAccount.getAxolotlService();
659 if (axolotlService != null && axolotlService.isPepBroken()) {
660 this.mServerInfoPep.setText(R.string.server_info_broken);
661 } else {
662 this.mServerInfoPep.setText(R.string.server_info_available);
663 }
664 } else {
665 this.mServerInfoPep.setText(R.string.server_info_unavailable);
666 }
667 if (features.httpUpload()) {
668 this.mServerInfoHttpUpload.setText(R.string.server_info_available);
669 } else {
670 this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable);
671 }
672 final String otrFingerprint = this.mAccount.getOtrFingerprint();
673 if (otrFingerprint != null) {
674 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
675 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
676 this.mOtrFingerprintToClipboardButton
677 .setVisibility(View.VISIBLE);
678 this.mOtrFingerprintToClipboardButton
679 .setOnClickListener(new View.OnClickListener() {
680
681 @Override
682 public void onClick(final View v) {
683
684 if (copyTextToClipboard(otrFingerprint, R.string.otr_fingerprint)) {
685 Toast.makeText(
686 EditAccountActivity.this,
687 R.string.toast_message_otr_fingerprint,
688 Toast.LENGTH_SHORT).show();
689 }
690 }
691 });
692 } else {
693 this.mOtrFingerprintBox.setVisibility(View.GONE);
694 }
695 final String axolotlFingerprint = this.mAccount.getAxolotlService().getOwnFingerprint();
696 if (axolotlFingerprint != null) {
697 this.mAxolotlFingerprintBox.setVisibility(View.VISIBLE);
698 this.mAxolotlFingerprint.setText(CryptoHelper.prettifyFingerprint(axolotlFingerprint.substring(2)));
699 this.mAxolotlFingerprintToClipboardButton
700 .setVisibility(View.VISIBLE);
701 this.mAxolotlFingerprintToClipboardButton
702 .setOnClickListener(new View.OnClickListener() {
703
704 @Override
705 public void onClick(final View v) {
706
707 if (copyTextToClipboard(axolotlFingerprint.substring(2), R.string.omemo_fingerprint)) {
708 Toast.makeText(
709 EditAccountActivity.this,
710 R.string.toast_message_omemo_fingerprint,
711 Toast.LENGTH_SHORT).show();
712 }
713 }
714 });
715 if (Config.SHOW_REGENERATE_AXOLOTL_KEYS_BUTTON) {
716 this.mRegenerateAxolotlKeyButton
717 .setVisibility(View.VISIBLE);
718 this.mRegenerateAxolotlKeyButton
719 .setOnClickListener(new View.OnClickListener() {
720
721 @Override
722 public void onClick(final View v) {
723 showRegenerateAxolotlKeyDialog();
724 }
725 });
726 }
727 } else {
728 this.mAxolotlFingerprintBox.setVisibility(View.GONE);
729 }
730 final String ownFingerprint = mAccount.getAxolotlService().getOwnFingerprint();
731 boolean hasKeys = false;
732 keys.removeAllViews();
733 for (final String fingerprint : mAccount.getAxolotlService().getFingerprintsForOwnSessions()) {
734 if (ownFingerprint.equals(fingerprint)) {
735 continue;
736 }
737 boolean highlight = fingerprint.equals(messageFingerprint);
738 hasKeys |= addFingerprintRow(keys, mAccount, fingerprint, highlight, null);
739 }
740 if (hasKeys) {
741 keysCard.setVisibility(View.VISIBLE);
742 } else {
743 keysCard.setVisibility(View.GONE);
744 }
745 } else {
746 if (this.mAccount.errorStatus()) {
747 final EditText errorTextField;
748 if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
749 errorTextField = this.mPassword;
750 } else if (mShowOptions
751 && this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
752 && this.mHostname.getText().length() > 0) {
753 errorTextField = this.mHostname;
754 } else {
755 errorTextField = this.mAccountJid;
756 }
757 errorTextField.setError(getString(this.mAccount.getStatus().getReadableId()));
758 if (init || !accountInfoEdited()) {
759 errorTextField.requestFocus();
760 }
761 } else {
762 this.mAccountJid.setError(null);
763 this.mPassword.setError(null);
764 this.mHostname.setError(null);
765 }
766 this.mStats.setVisibility(View.GONE);
767 }
768 }
769
770 public void showRegenerateAxolotlKeyDialog() {
771 Builder builder = new Builder(this);
772 builder.setTitle("Regenerate Key");
773 builder.setIconAttribute(android.R.attr.alertDialogIcon);
774 builder.setMessage("Are you sure you want to regenerate your Identity Key? (This will also wipe all established sessions and contact Identity Keys)");
775 builder.setNegativeButton(getString(R.string.cancel), null);
776 builder.setPositiveButton("Yes",
777 new DialogInterface.OnClickListener() {
778 @Override
779 public void onClick(DialogInterface dialog, int which) {
780 mAccount.getAxolotlService().regenerateKeys(false);
781 }
782 });
783 builder.create().show();
784 }
785
786 public void showWipePepDialog() {
787 Builder builder = new Builder(this);
788 builder.setTitle(getString(R.string.clear_other_devices));
789 builder.setIconAttribute(android.R.attr.alertDialogIcon);
790 builder.setMessage(getString(R.string.clear_other_devices_desc));
791 builder.setNegativeButton(getString(R.string.cancel), null);
792 builder.setPositiveButton(getString(R.string.accept),
793 new DialogInterface.OnClickListener() {
794 @Override
795 public void onClick(DialogInterface dialog, int which) {
796 mAccount.getAxolotlService().wipeOtherPepDevices();
797 }
798 });
799 builder.create().show();
800 }
801
802 @Override
803 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
804 refreshUi();
805 }
806
807 @Override
808 public void onCaptchaRequested(final Account account, final String id, final Data data,
809 final Bitmap captcha) {
810 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
811 final ImageView view = new ImageView(this);
812 final LinearLayout layout = new LinearLayout(this);
813 final EditText input = new EditText(this);
814
815 view.setImageBitmap(captcha);
816 view.setScaleType(ImageView.ScaleType.FIT_CENTER);
817
818 input.setHint(getString(R.string.captcha_hint));
819
820 layout.setOrientation(LinearLayout.VERTICAL);
821 layout.addView(view);
822 layout.addView(input);
823
824 builder.setTitle(getString(R.string.captcha_required));
825 builder.setView(layout);
826
827 builder.setPositiveButton(getString(R.string.ok),
828 new DialogInterface.OnClickListener() {
829 @Override
830 public void onClick(DialogInterface dialog, int which) {
831 String rc = input.getText().toString();
832 data.put("username", account.getUsername());
833 data.put("password", account.getPassword());
834 data.put("ocr", rc);
835 data.submit();
836
837 if (xmppConnectionServiceBound) {
838 xmppConnectionService.sendCreateAccountWithCaptchaPacket(
839 account, id, data);
840 }
841 }
842 });
843 builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
844 @Override
845 public void onClick(DialogInterface dialog, int which) {
846 if (xmppConnectionService != null) {
847 xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
848 }
849 }
850 });
851
852 builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
853 @Override
854 public void onCancel(DialogInterface dialog) {
855 if (xmppConnectionService != null) {
856 xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
857 }
858 }
859 });
860
861 runOnUiThread(new Runnable() {
862 @Override
863 public void run() {
864 if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) {
865 mCaptchaDialog.dismiss();
866 }
867 mCaptchaDialog = builder.create();
868 mCaptchaDialog.show();
869 }
870 });
871 }
872
873 public void onShowErrorToast(final int resId) {
874 runOnUiThread(new Runnable() {
875 @Override
876 public void run() {
877 Toast.makeText(EditAccountActivity.this, resId, Toast.LENGTH_SHORT).show();
878 }
879 });
880 }
881}