EditAccountActivity.java

   1package eu.siacs.conversations.ui;
   2
   3import android.app.Activity;
   4import android.app.PendingIntent;
   5import android.content.ActivityNotFoundException;
   6import android.content.Intent;
   7import android.content.IntentSender;
   8import android.content.SharedPreferences;
   9import android.databinding.DataBindingUtil;
  10import android.graphics.Bitmap;
  11import android.net.Uri;
  12import android.os.Bundle;
  13import android.os.Handler;
  14import android.preference.PreferenceManager;
  15import android.provider.Settings;
  16import android.security.KeyChain;
  17import android.security.KeyChainAliasCallback;
  18import android.support.design.widget.TextInputLayout;
  19import android.support.v7.app.ActionBar;
  20import android.support.v7.app.AlertDialog;
  21import android.support.v7.app.AlertDialog.Builder;
  22import android.support.v7.widget.Toolbar;
  23import android.text.Editable;
  24import android.text.TextWatcher;
  25import android.util.Log;
  26import android.view.Menu;
  27import android.view.MenuItem;
  28import android.view.View;
  29import android.view.View.OnClickListener;
  30import android.widget.Button;
  31import android.widget.CompoundButton;
  32import android.widget.CompoundButton.OnCheckedChangeListener;
  33import android.widget.EditText;
  34import android.widget.ImageButton;
  35import android.widget.ImageView;
  36import android.widget.LinearLayout;
  37import android.widget.RelativeLayout;
  38import android.widget.TableLayout;
  39import android.widget.TextView;
  40import android.widget.Toast;
  41
  42import org.openintents.openpgp.util.OpenPgpUtils;
  43
  44import java.net.URL;
  45import java.util.Arrays;
  46import java.util.List;
  47import java.util.Set;
  48import java.util.concurrent.atomic.AtomicInteger;
  49
  50import eu.siacs.conversations.Config;
  51import eu.siacs.conversations.R;
  52import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  53import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
  54import eu.siacs.conversations.databinding.ActivityEditAccountBinding;
  55import eu.siacs.conversations.databinding.DialogPresenceBinding;
  56import eu.siacs.conversations.entities.Account;
  57import eu.siacs.conversations.entities.Presence;
  58import eu.siacs.conversations.entities.PresenceTemplate;
  59import eu.siacs.conversations.services.BarcodeProvider;
  60import eu.siacs.conversations.services.XmppConnectionService;
  61import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
  62import eu.siacs.conversations.services.XmppConnectionService.OnCaptchaRequested;
  63import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
  64import eu.siacs.conversations.ui.adapter.PresenceTemplateAdapter;
  65import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
  66import eu.siacs.conversations.ui.util.PendingItem;
  67import eu.siacs.conversations.utils.CryptoHelper;
  68import eu.siacs.conversations.utils.UIHelper;
  69import eu.siacs.conversations.utils.XmppUri;
  70import eu.siacs.conversations.xml.Element;
  71import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
  72import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
  73import eu.siacs.conversations.xmpp.XmppConnection;
  74import eu.siacs.conversations.xmpp.XmppConnection.Features;
  75import eu.siacs.conversations.xmpp.forms.Data;
  76import eu.siacs.conversations.xmpp.pep.Avatar;
  77import rocks.xmpp.addr.Jid;
  78
  79public class EditAccountActivity extends OmemoActivity implements OnAccountUpdate, OnUpdateBlocklist,
  80		OnKeyStatusUpdated, OnCaptchaRequested, KeyChainAliasCallback, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnMamPreferencesFetched {
  81
  82	private static final int REQUEST_DATA_SAVER = 0xf244;
  83	private static final int REQUEST_CHANGE_STATUS = 0xee11;
  84	private TextInputLayout mAccountJidLayout;
  85	private EditText mPassword;
  86	private TextInputLayout mPasswordLayout;
  87	private Button mCancelButton;
  88	private Button mSaveButton;
  89	private Button mDisableOsOptimizationsButton;
  90	private TextView getmDisableOsOptimizationsBody;
  91	private TableLayout mMoreTable;
  92
  93	private TextView mAxolotlFingerprint;
  94	private TextView mPgpFingerprint;
  95	private TextView mOwnFingerprintDesc;
  96	private TextView getmPgpFingerprintDesc;
  97	private ImageView mAvatar;
  98	private RelativeLayout mAxolotlFingerprintBox;
  99	private RelativeLayout mPgpFingerprintBox;
 100	private ImageButton mAxolotlFingerprintToClipboardButton;
 101	private ImageButton mPgpDeleteFingerprintButton;
 102	private LinearLayout keys;
 103	private LinearLayout mNamePort;
 104	private EditText mHostname;
 105	private TextInputLayout mHostnameLayout;
 106	private EditText mPort;
 107	private TextInputLayout mPortLayout;
 108	private AlertDialog mCaptchaDialog = null;
 109
 110	private Jid jidToEdit;
 111	private boolean mInitMode = false;
 112	private boolean mUsernameMode = Config.DOMAIN_LOCK != null;
 113	private boolean mShowOptions = false;
 114	private Account mAccount;
 115	private String messageFingerprint;
 116
 117	private final PendingItem<PresenceTemplate> mPendingPresenceTemplate = new PendingItem<>();
 118
 119	private boolean mFetchingAvatar = false;
 120
 121	private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
 122
 123		@Override
 124		public void onClick(final View v) {
 125			final String password = mPassword.getText().toString();
 126			final boolean wasDisabled = mAccount != null && mAccount.getStatus() == Account.State.DISABLED;
 127
 128			if (!mInitMode && passwordChangedInMagicCreateMode()) {
 129				gotoChangePassword(password);
 130				return;
 131			}
 132			if (mInitMode && mAccount != null) {
 133				mAccount.setOption(Account.OPTION_DISABLED, false);
 134			}
 135			if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !accountInfoEdited()) {
 136				mAccount.setOption(Account.OPTION_DISABLED, false);
 137				if (!xmppConnectionService.updateAccount(mAccount)) {
 138					Toast.makeText(EditAccountActivity.this, R.string.unable_to_update_account, Toast.LENGTH_SHORT).show();
 139				}
 140				return;
 141			}
 142			final boolean registerNewAccount = binding.accountRegisterNew.isChecked() && !Config.DISALLOW_REGISTRATION_IN_UI;
 143			if (mUsernameMode && binding.accountJid.getText().toString().contains("@")) {
 144				mAccountJidLayout.setError(getString(R.string.invalid_username));
 145				removeErrorsOnAllBut(mAccountJidLayout);
 146				binding.accountJid.requestFocus();
 147				return;
 148			}
 149
 150			XmppConnection connection = mAccount == null ? null : mAccount.getXmppConnection();
 151			boolean openRegistrationUrl = registerNewAccount && mAccount != null && mAccount.getStatus() == Account.State.REGISTRATION_WEB;
 152			boolean openPaymentUrl = mAccount != null && mAccount.getStatus() == Account.State.PAYMENT_REQUIRED;
 153			final boolean redirectionWorthyStatus = openPaymentUrl || openRegistrationUrl;
 154			URL url = connection != null && redirectionWorthyStatus ? connection.getRedirectionUrl() : null;
 155			if (url != null && redirectionWorthyStatus && !wasDisabled) {
 156				try {
 157					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url.toString())));
 158					return;
 159				} catch (ActivityNotFoundException e) {
 160					Toast.makeText(EditAccountActivity.this, R.string.application_found_to_open_website, Toast.LENGTH_SHORT);
 161					return;
 162				}
 163			}
 164
 165			final Jid jid;
 166			try {
 167				if (mUsernameMode) {
 168					jid = Jid.of(binding.accountJid.getText().toString(), getUserModeDomain(), null);
 169				} else {
 170					jid = Jid.of(binding.accountJid.getText().toString());
 171				}
 172			} catch (final IllegalArgumentException e) {
 173				if (mUsernameMode) {
 174					mAccountJidLayout.setError(getString(R.string.invalid_username));
 175				} else {
 176					mAccountJidLayout.setError(getString(R.string.invalid_jid));
 177				}
 178				binding.accountJid.requestFocus();
 179				removeErrorsOnAllBut(mAccountJidLayout);
 180				return;
 181			}
 182			String hostname = null;
 183			int numericPort = 5222;
 184			if (mShowOptions) {
 185				hostname = mHostname.getText().toString().replaceAll("\\s", "");
 186				final String port = mPort.getText().toString().replaceAll("\\s", "");
 187				if (hostname.contains(" ")) {
 188					mHostnameLayout.setError(getString(R.string.not_valid_hostname));
 189					mHostname.requestFocus();
 190					removeErrorsOnAllBut(mHostnameLayout);
 191					return;
 192				}
 193				try {
 194					numericPort = Integer.parseInt(port);
 195					if (numericPort < 0 || numericPort > 65535) {
 196						mPortLayout.setError(getString(R.string.not_a_valid_port));
 197						removeErrorsOnAllBut(mPortLayout);
 198						mPort.requestFocus();
 199						return;
 200					}
 201
 202				} catch (NumberFormatException e) {
 203					mPortLayout.setError(getString(R.string.not_a_valid_port));
 204					removeErrorsOnAllBut(mPortLayout);
 205					mPort.requestFocus();
 206					return;
 207				}
 208			}
 209
 210			if (jid.getLocal() == null) {
 211				if (mUsernameMode) {
 212					mAccountJidLayout.setError(getString(R.string.invalid_username));
 213				} else {
 214					mAccountJidLayout.setError(getString(R.string.invalid_jid));
 215				}
 216				removeErrorsOnAllBut(mAccountJidLayout);
 217				binding.accountJid.requestFocus();
 218				return;
 219			}
 220			if (mAccount != null) {
 221				if (mInitMode && mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) {
 222					mAccount.setOption(Account.OPTION_MAGIC_CREATE, mAccount.getPassword().contains(password));
 223				}
 224				mAccount.setJid(jid);
 225				mAccount.setPort(numericPort);
 226				mAccount.setHostname(hostname);
 227				mAccountJidLayout.setError(null);
 228				mAccount.setPassword(password);
 229				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
 230				if (!xmppConnectionService.updateAccount(mAccount)) {
 231					Toast.makeText(EditAccountActivity.this, R.string.unable_to_update_account, Toast.LENGTH_SHORT).show();
 232					return;
 233				}
 234			} else {
 235				if (xmppConnectionService.findAccountByJid(jid) != null) {
 236					mAccountJidLayout.setError(getString(R.string.account_already_exists));
 237					removeErrorsOnAllBut(mAccountJidLayout);
 238					binding.accountJid.requestFocus();
 239					return;
 240				}
 241				mAccount = new Account(jid.asBareJid(), password);
 242				mAccount.setPort(numericPort);
 243				mAccount.setHostname(hostname);
 244				mAccount.setOption(Account.OPTION_USETLS, true);
 245				mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
 246				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
 247				xmppConnectionService.createAccount(mAccount);
 248			}
 249			mHostnameLayout.setError(null);
 250			mPortLayout.setError(null);
 251			if (mAccount.isEnabled()
 252					&& !registerNewAccount
 253					&& !mInitMode) {
 254				finish();
 255			} else {
 256				updateSaveButton();
 257				updateAccountInformation(true);
 258			}
 259
 260		}
 261	};
 262	private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
 263
 264		@Override
 265		public void onClick(final View v) {
 266			deleteAccountAndReturnIfNecessary();
 267			finish();
 268		}
 269	};
 270	private Toast mFetchingMamPrefsToast;
 271	private String mSavedInstanceAccount;
 272	private boolean mSavedInstanceInit = false;
 273	private Button mClearDevicesButton;
 274	private XmppUri pendingUri = null;
 275	private boolean mUseTor;
 276	private ActivityEditAccountBinding binding;
 277
 278	public void refreshUiReal() {
 279		invalidateOptionsMenu();
 280		if (mAccount != null
 281				&& mAccount.getStatus() != Account.State.ONLINE
 282				&& mFetchingAvatar) {
 283			startActivity(new Intent(getApplicationContext(),
 284					ManageAccountActivity.class));
 285			finish();
 286		} else if (mInitMode && mAccount != null && mAccount.getStatus() == Account.State.ONLINE) {
 287			if (!mFetchingAvatar) {
 288				mFetchingAvatar = true;
 289				xmppConnectionService.checkForAvatar(mAccount, mAvatarFetchCallback);
 290			}
 291		}
 292		if (mAccount != null) {
 293			updateAccountInformation(false);
 294		}
 295		updateSaveButton();
 296	}
 297
 298	@Override
 299	public boolean onNavigateUp() {
 300		deleteAccountAndReturnIfNecessary();
 301		return super.onNavigateUp();
 302	}
 303
 304	@Override
 305	public void onBackPressed() {
 306		deleteAccountAndReturnIfNecessary();
 307		super.onBackPressed();
 308	}
 309
 310	private void deleteAccountAndReturnIfNecessary() {
 311		if (mInitMode && mAccount != null && !mAccount.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY)) {
 312			xmppConnectionService.deleteAccount(mAccount);
 313		}
 314
 315		if (xmppConnectionService.getAccounts().size() == 0) {
 316			Intent intent = new Intent(EditAccountActivity.this, WelcomeActivity.class);
 317			WelcomeActivity.addInviteUri(intent, getIntent());
 318			startActivity(intent);
 319		}
 320	}
 321
 322	@Override
 323	public void onAccountUpdate() {
 324		refreshUi();
 325	}
 326
 327	private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
 328
 329		@Override
 330		public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
 331			finishInitialSetup(avatar);
 332		}
 333
 334		@Override
 335		public void success(final Avatar avatar) {
 336			finishInitialSetup(avatar);
 337		}
 338
 339		@Override
 340		public void error(final int errorCode, final Avatar avatar) {
 341			finishInitialSetup(avatar);
 342		}
 343	};
 344	private final TextWatcher mTextWatcher = new TextWatcher() {
 345
 346		@Override
 347		public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
 348			updateSaveButton();
 349		}
 350
 351		@Override
 352		public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
 353		}
 354
 355		@Override
 356		public void afterTextChanged(final Editable s) {
 357
 358		}
 359	};
 360
 361	private View.OnFocusChangeListener mEditTextFocusListener = new View.OnFocusChangeListener() {
 362		@Override
 363		public void onFocusChange(View view, boolean b) {
 364			EditText et = (EditText) view;
 365			if (b) {
 366				int resId = mUsernameMode ? R.string.username : R.string.account_settings_example_jabber_id;
 367				if (view.getId() == R.id.hostname) {
 368					resId = mUseTor ? R.string.hostname_or_onion : R.string.hostname_example;
 369				}
 370				final int res = resId;
 371				new Handler().postDelayed(() -> et.setHint(res), 200);
 372			} else {
 373				et.setHint(null);
 374			}
 375		}
 376	};
 377
 378
 379	private final OnClickListener mAvatarClickListener = new OnClickListener() {
 380		@Override
 381		public void onClick(final View view) {
 382			if (mAccount != null) {
 383				final Intent intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
 384				intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().asBareJid().toString());
 385				startActivity(intent);
 386			}
 387		}
 388	};
 389
 390	protected void finishInitialSetup(final Avatar avatar) {
 391		runOnUiThread(() -> {
 392			hideKeyboard();
 393			final Intent intent;
 394			final XmppConnection connection = mAccount.getXmppConnection();
 395			final boolean wasFirstAccount = xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1;
 396			if (avatar != null || (connection != null && !connection.getFeatures().pep())) {
 397				intent = new Intent(getApplicationContext(), StartConversationActivity.class);
 398				if (wasFirstAccount) {
 399					intent.putExtra("init", true);
 400				}
 401			} else {
 402				intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
 403				intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().asBareJid().toString());
 404				intent.putExtra("setup", true);
 405			}
 406			if (wasFirstAccount) {
 407				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
 408			}
 409			WelcomeActivity.addInviteUri(intent, getIntent());
 410			startActivity(intent);
 411			finish();
 412		});
 413	}
 414
 415	@Override
 416	public void onActivityResult(int requestCode, int resultCode, Intent data) {
 417		super.onActivityResult(requestCode, resultCode, data);
 418		if (requestCode == REQUEST_BATTERY_OP || requestCode == REQUEST_DATA_SAVER) {
 419			updateAccountInformation(mAccount == null);
 420		}
 421		if (requestCode == REQUEST_CHANGE_STATUS) {
 422			PresenceTemplate template = mPendingPresenceTemplate.pop();
 423			if (template != null && resultCode == Activity.RESULT_OK) {
 424				generateSignature(data, template);
 425			} else {
 426				Log.d(Config.LOGTAG, "pgp result not ok");
 427			}
 428		}
 429	}
 430
 431	@Override
 432	protected void processFingerprintVerification(XmppUri uri) {
 433		processFingerprintVerification(uri, true);
 434	}
 435
 436
 437	protected void processFingerprintVerification(XmppUri uri, boolean showWarningToast) {
 438		if (mAccount != null && mAccount.getJid().asBareJid().equals(uri.getJid()) && uri.hasFingerprints()) {
 439			if (xmppConnectionService.verifyFingerprints(mAccount, uri.getFingerprints())) {
 440				Toast.makeText(this, R.string.verified_fingerprints, Toast.LENGTH_SHORT).show();
 441				updateAccountInformation(false);
 442			}
 443		} else if (showWarningToast) {
 444			Toast.makeText(this, R.string.invalid_barcode, Toast.LENGTH_SHORT).show();
 445		}
 446	}
 447
 448	protected void updateSaveButton() {
 449		boolean accountInfoEdited = accountInfoEdited();
 450
 451		if (!mInitMode && passwordChangedInMagicCreateMode()) {
 452			this.mSaveButton.setText(R.string.change_password);
 453			this.mSaveButton.setEnabled(true);
 454		} else if (accountInfoEdited && !mInitMode) {
 455			this.mSaveButton.setText(R.string.save);
 456			this.mSaveButton.setEnabled(true);
 457		} else if (mAccount != null
 458				&& (mAccount.getStatus() == Account.State.CONNECTING || mAccount.getStatus() == Account.State.REGISTRATION_SUCCESSFUL || mFetchingAvatar)) {
 459			this.mSaveButton.setEnabled(false);
 460			this.mSaveButton.setText(R.string.account_status_connecting);
 461		} else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) {
 462			this.mSaveButton.setEnabled(true);
 463			this.mSaveButton.setText(R.string.enable);
 464		} else {
 465			this.mSaveButton.setEnabled(true);
 466			if (!mInitMode) {
 467				if (mAccount != null && mAccount.isOnlineAndConnected()) {
 468					this.mSaveButton.setText(R.string.save);
 469					if (!accountInfoEdited) {
 470						this.mSaveButton.setEnabled(false);
 471					}
 472				} else {
 473					XmppConnection connection = mAccount == null ? null : mAccount.getXmppConnection();
 474					URL url = connection != null && mAccount.getStatus() == Account.State.PAYMENT_REQUIRED ? connection.getRedirectionUrl() : null;
 475					if (url != null) {
 476						this.mSaveButton.setText(R.string.open_website);
 477					} else {
 478						this.mSaveButton.setText(R.string.connect);
 479					}
 480				}
 481			} else {
 482				XmppConnection connection = mAccount == null ? null : mAccount.getXmppConnection();
 483				URL url = connection != null && mAccount.getStatus() == Account.State.REGISTRATION_WEB ? connection.getRedirectionUrl() : null;
 484				if (url != null && this.binding.accountRegisterNew.isChecked()) {
 485					this.mSaveButton.setText(R.string.open_website);
 486				} else {
 487					this.mSaveButton.setText(R.string.next);
 488				}
 489			}
 490		}
 491	}
 492
 493	protected boolean accountInfoEdited() {
 494		if (this.mAccount == null) {
 495			return false;
 496		}
 497		return jidEdited() ||
 498				!this.mAccount.getPassword().equals(this.mPassword.getText().toString()) ||
 499				!this.mAccount.getHostname().equals(this.mHostname.getText().toString()) ||
 500				!String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString());
 501	}
 502
 503	protected boolean jidEdited() {
 504		final String unmodified;
 505		if (mUsernameMode) {
 506			unmodified = this.mAccount.getJid().getLocal();
 507		} else {
 508			unmodified = this.mAccount.getJid().asBareJid().toString();
 509		}
 510		return !unmodified.equals(this.binding.accountJid.getText().toString());
 511	}
 512
 513	protected boolean passwordChangedInMagicCreateMode() {
 514		return mAccount != null
 515				&& mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
 516				&& !this.mAccount.getPassword().equals(this.mPassword.getText().toString())
 517				&& !this.jidEdited()
 518				&& mAccount.isOnlineAndConnected();
 519	}
 520
 521	@Override
 522	protected String getShareableUri(boolean http) {
 523		if (mAccount != null) {
 524			return http ? mAccount.getShareableLink() : mAccount.getShareableUri();
 525		} else {
 526			return null;
 527		}
 528	}
 529
 530	@Override
 531	protected void onCreate(final Bundle savedInstanceState) {
 532		super.onCreate(savedInstanceState);
 533		if (savedInstanceState != null) {
 534			this.mSavedInstanceAccount = savedInstanceState.getString("account");
 535			this.mSavedInstanceInit = savedInstanceState.getBoolean("initMode", false);
 536		}
 537		this.binding = DataBindingUtil.setContentView(this, R.layout.activity_edit_account);
 538		setSupportActionBar((Toolbar) binding.toolbar);
 539		configureActionBar(getSupportActionBar());
 540		binding.accountJid.addTextChangedListener(this.mTextWatcher);
 541		binding.accountJid.setOnFocusChangeListener(this.mEditTextFocusListener);
 542		this.mAccountJidLayout = (TextInputLayout) findViewById(R.id.account_jid_layout);
 543		this.mPassword = (EditText) findViewById(R.id.account_password);
 544		this.mPassword.addTextChangedListener(this.mTextWatcher);
 545		this.mPasswordLayout = (TextInputLayout) findViewById(R.id.account_password_layout);
 546		this.mAvatar = (ImageView) findViewById(R.id.avater);
 547		this.mAvatar.setOnClickListener(this.mAvatarClickListener);
 548		this.mDisableOsOptimizationsButton = (Button) findViewById(R.id.os_optimization_disable);
 549		this.getmDisableOsOptimizationsBody = (TextView) findViewById(R.id.os_optimization_body);
 550		this.mPgpFingerprintBox = (RelativeLayout) findViewById(R.id.pgp_fingerprint_box);
 551		this.mPgpFingerprint = (TextView) findViewById(R.id.pgp_fingerprint);
 552		this.getmPgpFingerprintDesc = (TextView) findViewById(R.id.pgp_fingerprint_desc);
 553		this.mPgpDeleteFingerprintButton = (ImageButton) findViewById(R.id.action_delete_pgp);
 554		this.mAxolotlFingerprint = (TextView) findViewById(R.id.axolotl_fingerprint);
 555		this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box);
 556		this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard);
 557		this.mOwnFingerprintDesc = (TextView) findViewById(R.id.own_fingerprint_desc);
 558		this.keys = (LinearLayout) findViewById(R.id.other_device_keys);
 559		this.mNamePort = (LinearLayout) findViewById(R.id.name_port);
 560		this.mHostname = (EditText) findViewById(R.id.hostname);
 561		this.mHostname.addTextChangedListener(mTextWatcher);
 562		this.mHostname.setOnFocusChangeListener(mEditTextFocusListener);
 563		this.mHostnameLayout = (TextInputLayout) findViewById(R.id.hostname_layout);
 564		this.mClearDevicesButton = (Button) findViewById(R.id.clear_devices);
 565		this.mClearDevicesButton.setOnClickListener(v -> showWipePepDialog());
 566		this.mPort = (EditText) findViewById(R.id.port);
 567		this.mPort.setText("5222");
 568		this.mPort.addTextChangedListener(mTextWatcher);
 569		this.mPortLayout = (TextInputLayout) findViewById(R.id.port_layout);
 570		this.mSaveButton = (Button) findViewById(R.id.save_button);
 571		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
 572		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
 573		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
 574		this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
 575		if (savedInstanceState != null && savedInstanceState.getBoolean("showMoreTable")) {
 576			changeMoreTableVisibility(true);
 577		}
 578		final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
 579			@Override
 580			public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
 581				updateSaveButton();
 582			}
 583		};
 584		this.binding.accountRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
 585		if (Config.DISALLOW_REGISTRATION_IN_UI) {
 586			this.binding.accountRegisterNew.setVisibility(View.GONE);
 587		}
 588	}
 589
 590	@Override
 591	public boolean onCreateOptionsMenu(final Menu menu) {
 592		super.onCreateOptionsMenu(menu);
 593		getMenuInflater().inflate(R.menu.editaccount, menu);
 594		final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
 595		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
 596		final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
 597		final MenuItem renewCertificate = menu.findItem(R.id.action_renew_certificate);
 598		final MenuItem mamPrefs = menu.findItem(R.id.action_mam_prefs);
 599		final MenuItem changePresence = menu.findItem(R.id.action_change_presence);
 600		final MenuItem share = menu.findItem(R.id.action_share);
 601		renewCertificate.setVisible(mAccount != null && mAccount.getPrivateKeyAlias() != null);
 602
 603		share.setVisible(mAccount != null && !mInitMode);
 604
 605		if (mAccount != null && mAccount.isOnlineAndConnected()) {
 606			if (!mAccount.getXmppConnection().getFeatures().blocking()) {
 607				showBlocklist.setVisible(false);
 608			}
 609
 610			if (!mAccount.getXmppConnection().getFeatures().register()) {
 611				changePassword.setVisible(false);
 612			}
 613			mamPrefs.setVisible(mAccount.getXmppConnection().getFeatures().mam());
 614			changePresence.setVisible(!mInitMode);
 615		} else {
 616			showBlocklist.setVisible(false);
 617			showMoreInfo.setVisible(false);
 618			changePassword.setVisible(false);
 619			mamPrefs.setVisible(false);
 620			changePresence.setVisible(false);
 621		}
 622		return super.onCreateOptionsMenu(menu);
 623	}
 624
 625	@Override
 626	public boolean onPrepareOptionsMenu(Menu menu) {
 627		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
 628		if (showMoreInfo.isVisible()) {
 629			showMoreInfo.setChecked(mMoreTable.getVisibility() == View.VISIBLE);
 630		}
 631		return super.onPrepareOptionsMenu(menu);
 632	}
 633
 634	@Override
 635	protected void onStart() {
 636		super.onStart();
 637		final Intent intent = getIntent();
 638		final int theme = findTheme();
 639		if (this.mTheme != theme) {
 640			recreate();
 641		} else if (intent != null) {
 642			try {
 643				this.jidToEdit = Jid.of(intent.getStringExtra("jid"));
 644			} catch (final IllegalArgumentException | NullPointerException ignored) {
 645				this.jidToEdit = null;
 646			}
 647			if (jidToEdit != null && intent.getData() != null && intent.getBooleanExtra("scanned", false)) {
 648				final XmppUri uri = new XmppUri(intent.getData());
 649				if (xmppConnectionServiceBound) {
 650					processFingerprintVerification(uri, false);
 651				} else {
 652					this.pendingUri = uri;
 653				}
 654			}
 655			boolean init = intent.getBooleanExtra("init", false);
 656			this.mInitMode = init || this.jidToEdit == null;
 657			this.messageFingerprint = intent.getStringExtra("fingerprint");
 658			if (!mInitMode) {
 659				this.binding.accountRegisterNew.setVisibility(View.GONE);
 660				if (getSupportActionBar() != null) {
 661					getSupportActionBar().setTitle(getString(R.string.account_details));
 662				}
 663			} else {
 664				this.mAvatar.setVisibility(View.GONE);
 665				ActionBar ab = getSupportActionBar();
 666				if (ab != null) {
 667					if (init && Config.MAGIC_CREATE_DOMAIN == null) {
 668						ab.setDisplayShowHomeEnabled(false);
 669						ab.setDisplayHomeAsUpEnabled(false);
 670					}
 671					ab.setTitle(R.string.action_add_account);
 672				}
 673			}
 674		}
 675		SharedPreferences preferences = getPreferences();
 676		mUseTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
 677		this.mShowOptions = mUseTor || preferences.getBoolean("show_connection_options", false);
 678		this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
 679	}
 680
 681	@Override
 682	public void onNewIntent(Intent intent) {
 683		if (intent != null && intent.getData() != null) {
 684			final XmppUri uri = new XmppUri(intent.getData());
 685			if (xmppConnectionServiceBound) {
 686				processFingerprintVerification(uri, false);
 687			} else {
 688				this.pendingUri = uri;
 689			}
 690		}
 691	}
 692
 693	@Override
 694	public void onSaveInstanceState(final Bundle savedInstanceState) {
 695		if (mAccount != null) {
 696			savedInstanceState.putString("account", mAccount.getJid().asBareJid().toString());
 697			savedInstanceState.putBoolean("initMode", mInitMode);
 698			savedInstanceState.putBoolean("showMoreTable", mMoreTable.getVisibility() == View.VISIBLE);
 699		}
 700		super.onSaveInstanceState(savedInstanceState);
 701	}
 702
 703	protected void onBackendConnected() {
 704		boolean init = true;
 705		if (mSavedInstanceAccount != null) {
 706			try {
 707				this.mAccount = xmppConnectionService.findAccountByJid(Jid.of(mSavedInstanceAccount));
 708				this.mInitMode = mSavedInstanceInit;
 709				init = false;
 710			} catch (IllegalArgumentException e) {
 711				this.mAccount = null;
 712			}
 713
 714		} else if (this.jidToEdit != null) {
 715			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
 716		}
 717
 718		if (mAccount != null) {
 719			this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER);
 720			this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER);
 721			if (this.mAccount.getPrivateKeyAlias() != null) {
 722				this.mPassword.setHint(R.string.authenticate_with_certificate);
 723				if (this.mInitMode) {
 724					this.mPassword.requestFocus();
 725				}
 726			}
 727			if (mPendingFingerprintVerificationUri != null) {
 728				processFingerprintVerification(mPendingFingerprintVerificationUri, false);
 729				mPendingFingerprintVerificationUri = null;
 730			}
 731			updateAccountInformation(init);
 732		}
 733
 734
 735		if (Config.MAGIC_CREATE_DOMAIN == null && this.xmppConnectionService.getAccounts().size() == 0) {
 736			this.mCancelButton.setEnabled(false);
 737		}
 738		if (mUsernameMode) {
 739			this.binding.accountJid.setHint(R.string.username_hint);
 740		} else {
 741			final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
 742					R.layout.simple_list_item,
 743					xmppConnectionService.getKnownHosts());
 744			this.binding.accountJid.setAdapter(mKnownHostsAdapter);
 745		}
 746
 747		if (pendingUri != null) {
 748			processFingerprintVerification(pendingUri, false);
 749			pendingUri = null;
 750		}
 751
 752		updateSaveButton();
 753		invalidateOptionsMenu();
 754	}
 755
 756	private String getUserModeDomain() {
 757		if (mAccount != null) {
 758			return mAccount.getJid().getDomain();
 759		} else {
 760			return Config.DOMAIN_LOCK;
 761		}
 762	}
 763
 764	@Override
 765	public boolean onOptionsItemSelected(final MenuItem item) {
 766		if (MenuDoubleTabUtil.shouldIgnoreTap()) {
 767			return false;
 768		}
 769		switch (item.getItemId()) {
 770			case R.id.action_show_block_list:
 771				final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
 772				showBlocklistIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
 773				startActivity(showBlocklistIntent);
 774				break;
 775			case R.id.action_server_info_show_more:
 776				changeMoreTableVisibility(!item.isChecked());
 777				break;
 778			case R.id.action_share_barcode:
 779				shareBarcode();
 780				break;
 781			case R.id.action_share_http:
 782				shareLink(true);
 783				break;
 784			case R.id.action_share_uri:
 785				shareLink(false);
 786				break;
 787			case R.id.action_change_password_on_server:
 788				gotoChangePassword(null);
 789				break;
 790			case R.id.action_mam_prefs:
 791				editMamPrefs();
 792				break;
 793			case R.id.action_renew_certificate:
 794				renewCertificate();
 795				break;
 796			case R.id.action_change_presence:
 797				changePresence();
 798				break;
 799		}
 800		return super.onOptionsItemSelected(item);
 801	}
 802
 803	private void shareBarcode() {
 804		Intent intent = new Intent(Intent.ACTION_SEND);
 805		intent.putExtra(Intent.EXTRA_STREAM, BarcodeProvider.getUriForAccount(this, mAccount));
 806		intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 807		intent.setType("image/png");
 808		startActivity(Intent.createChooser(intent, getText(R.string.share_with)));
 809	}
 810
 811	private void changeMoreTableVisibility(boolean visible) {
 812		mMoreTable.setVisibility(visible ? View.VISIBLE : View.GONE);
 813	}
 814
 815	private void gotoChangePassword(String newPassword) {
 816		final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
 817		changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
 818		if (newPassword != null) {
 819			changePasswordIntent.putExtra("password", newPassword);
 820		}
 821		startActivity(changePasswordIntent);
 822	}
 823
 824	private void renewCertificate() {
 825		KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null);
 826	}
 827
 828	private void changePresence() {
 829		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
 830		boolean manualStatus = sharedPreferences.getBoolean(SettingsActivity.MANUALLY_CHANGE_PRESENCE, getResources().getBoolean(R.bool.manually_change_presence));
 831		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 832		final DialogPresenceBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.dialog_presence, null, false);
 833		String current = mAccount.getPresenceStatusMessage();
 834		if (current != null && !current.trim().isEmpty()) {
 835			binding.statusMessage.append(current);
 836		}
 837		setAvailabilityRadioButton(mAccount.getPresenceStatus(), binding);
 838		binding.show.setVisibility(manualStatus ? View.VISIBLE : View.GONE);
 839		List<PresenceTemplate> templates = xmppConnectionService.getPresenceTemplates(mAccount);
 840		PresenceTemplateAdapter presenceTemplateAdapter = new PresenceTemplateAdapter(this, R.layout.simple_list_item, templates);
 841		binding.statusMessage.setAdapter(presenceTemplateAdapter);
 842		binding.statusMessage.setOnItemClickListener((parent, view, position, id) -> {
 843			PresenceTemplate template = (PresenceTemplate) parent.getItemAtPosition(position);
 844			setAvailabilityRadioButton(template.getStatus(), binding);
 845		});
 846		builder.setTitle(R.string.edit_status_message_title);
 847		builder.setView(binding.getRoot());
 848		builder.setNegativeButton(R.string.cancel, null);
 849		builder.setPositiveButton(R.string.confirm, (dialog, which) -> {
 850			PresenceTemplate template = new PresenceTemplate(getAvailabilityRadioButton(binding), binding.statusMessage.getText().toString().trim());
 851			if (mAccount.getPgpId() != 0 && hasPgp()) {
 852				generateSignature(null, template);
 853			} else {
 854				xmppConnectionService.changeStatus(mAccount, template, null);
 855			}
 856		});
 857		builder.create().show();
 858	}
 859
 860	private void generateSignature(Intent intent, PresenceTemplate template) {
 861		xmppConnectionService.getPgpEngine().generateSignature(intent, mAccount, template.getStatusMessage(), new UiCallback<String>() {
 862			@Override
 863			public void success(String signature) {
 864				xmppConnectionService.changeStatus(mAccount, template, signature);
 865			}
 866
 867			@Override
 868			public void error(int errorCode, String object) {
 869
 870			}
 871
 872			@Override
 873			public void userInputRequried(PendingIntent pi, String object) {
 874				mPendingPresenceTemplate.push(template);
 875				try {
 876					startIntentSenderForResult(pi.getIntentSender(), REQUEST_CHANGE_STATUS, null, 0, 0, 0);
 877				} catch (final IntentSender.SendIntentException ignored) {
 878				}
 879			}
 880		});
 881	}
 882
 883	private static void setAvailabilityRadioButton(Presence.Status status, DialogPresenceBinding binding) {
 884		if (status == null) {
 885			binding.online.setChecked(true);
 886			return;
 887		}
 888		switch (status) {
 889			case DND:
 890				binding.dnd.setChecked(true);
 891				break;
 892			case XA:
 893				binding.xa.setChecked(true);
 894				break;
 895			case AWAY:
 896				binding.xa.setChecked(true);
 897				break;
 898			default:
 899				binding.online.setChecked(true);
 900		}
 901	}
 902
 903	private static Presence.Status getAvailabilityRadioButton(DialogPresenceBinding binding) {
 904		if (binding.dnd.isChecked()) {
 905			return Presence.Status.DND;
 906		} else if (binding.xa.isChecked()) {
 907			return Presence.Status.XA;
 908		} else if (binding.away.isChecked()) {
 909			return Presence.Status.AWAY;
 910		} else {
 911			return Presence.Status.ONLINE;
 912		}
 913	}
 914
 915	@Override
 916	public void alias(String alias) {
 917		if (alias != null) {
 918			xmppConnectionService.updateKeyInAccount(mAccount, alias);
 919		}
 920	}
 921
 922	private void updateAccountInformation(boolean init) {
 923		if (init) {
 924			this.binding.accountJid.getEditableText().clear();
 925			if (mUsernameMode) {
 926				this.binding.accountJid.getEditableText().append(this.mAccount.getJid().getLocal());
 927			} else {
 928				this.binding.accountJid.getEditableText().append(this.mAccount.getJid().asBareJid().toString());
 929			}
 930			this.mPassword.getEditableText().clear();
 931			this.mPassword.getEditableText().append(this.mAccount.getPassword());
 932			this.mHostname.setText("");
 933			this.mHostname.getEditableText().append(this.mAccount.getHostname());
 934			this.mPort.setText("");
 935			this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort()));
 936			this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
 937
 938		}
 939
 940		final boolean editable = !mAccount.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY);
 941		this.binding.accountJid.setEnabled(editable);
 942		this.binding.accountJid.setFocusable(editable);
 943		this.binding.accountJid.setFocusableInTouchMode(editable);
 944
 945
 946		if (mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) || !mAccount.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY)) {
 947			this.binding.accountPasswordLayout.setPasswordVisibilityToggleEnabled(true);
 948		} else {
 949			this.binding.accountPasswordLayout.setPasswordVisibilityToggleEnabled(false);
 950		}
 951
 952		if (!mInitMode) {
 953			this.mAvatar.setVisibility(View.VISIBLE);
 954			this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
 955		} else {
 956			this.mAvatar.setVisibility(View.GONE);
 957		}
 958		this.binding.accountRegisterNew.setChecked(this.mAccount.isOptionSet(Account.OPTION_REGISTER));
 959		if (this.mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)) {
 960			if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
 961				ActionBar actionBar = getSupportActionBar();
 962				if (actionBar != null) {
 963					actionBar.setTitle(R.string.create_account);
 964				}
 965			}
 966			this.binding.accountRegisterNew.setVisibility(View.GONE);
 967		} else if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
 968			this.binding.accountRegisterNew.setVisibility(View.VISIBLE);
 969		} else {
 970			this.binding.accountRegisterNew.setVisibility(View.GONE);
 971		}
 972		if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
 973			Features features = this.mAccount.getXmppConnection().getFeatures();
 974			this.binding.stats.setVisibility(View.VISIBLE);
 975			boolean showBatteryWarning = !xmppConnectionService.getPushManagementService().available(mAccount) && isOptimizingBattery();
 976			boolean showDataSaverWarning = isAffectedByDataSaver();
 977			showOsOptimizationWarning(showBatteryWarning, showDataSaverWarning);
 978			this.binding.sessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
 979					.getLastSessionEstablished()));
 980			if (features.rosterVersioning()) {
 981				this.binding.serverInfoRosterVersion.setText(R.string.server_info_available);
 982			} else {
 983				this.binding.serverInfoRosterVersion.setText(R.string.server_info_unavailable);
 984			}
 985			if (features.carbons()) {
 986				this.binding.serverInfoCarbons.setText(R.string.server_info_available);
 987			} else {
 988				this.binding.serverInfoCarbons.setText(R.string.server_info_unavailable);
 989			}
 990			if (features.mam()) {
 991				this.binding.serverInfoMam.setText(R.string.server_info_available);
 992			} else {
 993				this.binding.serverInfoMam.setText(R.string.server_info_unavailable);
 994			}
 995			if (features.csi()) {
 996				this.binding.serverInfoCsi.setText(R.string.server_info_available);
 997			} else {
 998				this.binding.serverInfoCsi.setText(R.string.server_info_unavailable);
 999			}
1000			if (features.blocking()) {
1001				this.binding.serverInfoBlocking.setText(R.string.server_info_available);
1002			} else {
1003				this.binding.serverInfoBlocking.setText(R.string.server_info_unavailable);
1004			}
1005			if (features.sm()) {
1006				this.binding.serverInfoSm.setText(R.string.server_info_available);
1007			} else {
1008				this.binding.serverInfoSm.setText(R.string.server_info_unavailable);
1009			}
1010			if (features.pep()) {
1011				AxolotlService axolotlService = this.mAccount.getAxolotlService();
1012				if (axolotlService != null && axolotlService.isPepBroken()) {
1013					this.binding.serverInfoPep.setText(R.string.server_info_broken);
1014				} else if (features.pepPublishOptions() || features.pepOmemoWhitelisted()) {
1015					this.binding.serverInfoPep.setText(R.string.server_info_available);
1016				} else {
1017					this.binding.serverInfoPep.setText(R.string.server_info_partial);
1018				}
1019			} else {
1020				this.binding.serverInfoPep.setText(R.string.server_info_unavailable);
1021			}
1022			if (features.httpUpload(0)) {
1023				this.binding.serverInfoHttpUpload.setText(R.string.server_info_available);
1024			} else {
1025				this.binding.serverInfoHttpUpload.setText(R.string.server_info_unavailable);
1026			}
1027
1028			this.binding.pushRow.setVisibility(xmppConnectionService.getPushManagementService().isStub() ? View.GONE : View.VISIBLE);
1029
1030			if (xmppConnectionService.getPushManagementService().available(mAccount)) {
1031				this.binding.serverInfoPush.setText(R.string.server_info_available);
1032			} else {
1033				this.binding.serverInfoPush.setText(R.string.server_info_unavailable);
1034			}
1035			final long pgpKeyId = this.mAccount.getPgpId();
1036			if (pgpKeyId != 0 && Config.supportOpenPgp()) {
1037				OnClickListener openPgp = view -> launchOpenKeyChain(pgpKeyId);
1038				OnClickListener delete = view -> showDeletePgpDialog();
1039				this.mPgpFingerprintBox.setVisibility(View.VISIBLE);
1040				this.mPgpFingerprint.setText(OpenPgpUtils.convertKeyIdToHex(pgpKeyId));
1041				this.mPgpFingerprint.setOnClickListener(openPgp);
1042				if ("pgp".equals(messageFingerprint)) {
1043					this.getmPgpFingerprintDesc.setTextAppearance(this, R.style.TextAppearance_Conversations_Caption_Highlight);
1044				}
1045				this.getmPgpFingerprintDesc.setOnClickListener(openPgp);
1046				this.mPgpDeleteFingerprintButton.setOnClickListener(delete);
1047			} else {
1048				this.mPgpFingerprintBox.setVisibility(View.GONE);
1049			}
1050			final String ownAxolotlFingerprint = this.mAccount.getAxolotlService().getOwnFingerprint();
1051			if (ownAxolotlFingerprint != null && Config.supportOmemo()) {
1052				this.mAxolotlFingerprintBox.setVisibility(View.VISIBLE);
1053				if (ownAxolotlFingerprint.equals(messageFingerprint)) {
1054					this.mOwnFingerprintDesc.setTextAppearance(this, R.style.TextAppearance_Conversations_Caption_Highlight);
1055					this.mOwnFingerprintDesc.setText(R.string.omemo_fingerprint_selected_message);
1056				} else {
1057					this.mOwnFingerprintDesc.setTextAppearance(this, R.style.TextAppearance_Conversations_Caption);
1058					this.mOwnFingerprintDesc.setText(R.string.omemo_fingerprint);
1059				}
1060				this.mAxolotlFingerprint.setText(CryptoHelper.prettifyFingerprint(ownAxolotlFingerprint.substring(2)));
1061				this.mAxolotlFingerprintToClipboardButton.setVisibility(View.VISIBLE);
1062				this.mAxolotlFingerprintToClipboardButton.setOnClickListener(v -> copyOmemoFingerprint(ownAxolotlFingerprint));
1063			} else {
1064				this.mAxolotlFingerprintBox.setVisibility(View.GONE);
1065			}
1066			boolean hasKeys = false;
1067			keys.removeAllViews();
1068			for (XmppAxolotlSession session : mAccount.getAxolotlService().findOwnSessions()) {
1069				if (!session.getTrust().isCompromised()) {
1070					boolean highlight = session.getFingerprint().equals(messageFingerprint);
1071					addFingerprintRow(keys, session, highlight);
1072					hasKeys = true;
1073				}
1074			}
1075			if (hasKeys && Config.supportOmemo()) {
1076				this.binding.otherDeviceKeysCard.setVisibility(View.VISIBLE);
1077				Set<Integer> otherDevices = mAccount.getAxolotlService().getOwnDeviceIds();
1078				if (otherDevices == null || otherDevices.isEmpty()) {
1079					mClearDevicesButton.setVisibility(View.GONE);
1080				} else {
1081					mClearDevicesButton.setVisibility(View.VISIBLE);
1082				}
1083			} else {
1084				this.binding.otherDeviceKeysCard.setVisibility(View.GONE);
1085			}
1086		} else {
1087			final TextInputLayout errorLayout;
1088			if (this.mAccount.errorStatus()) {
1089				if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
1090					errorLayout = this.mPasswordLayout;
1091				} else if (mShowOptions
1092						&& this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
1093						&& this.mHostname.getText().length() > 0) {
1094					errorLayout = this.mHostnameLayout;
1095				} else {
1096					errorLayout = this.mAccountJidLayout;
1097				}
1098				errorLayout.setError(getString(this.mAccount.getStatus().getReadableId()));
1099				if (init || !accountInfoEdited()) {
1100					errorLayout.requestFocus();
1101				}
1102			} else {
1103				errorLayout = null;
1104			}
1105			removeErrorsOnAllBut(errorLayout);
1106			this.binding.stats.setVisibility(View.GONE);
1107			this.binding.otherDeviceKeysCard.setVisibility(View.GONE);
1108		}
1109	}
1110
1111	private void removeErrorsOnAllBut(TextInputLayout exception) {
1112		if (this.mAccountJidLayout != exception) {
1113			this.mAccountJidLayout.setErrorEnabled(false);
1114			this.mAccountJidLayout.setError(null);
1115		}
1116		if (this.mPasswordLayout != exception) {
1117			this.mPasswordLayout.setErrorEnabled(false);
1118			this.mPasswordLayout.setError(null);
1119		}
1120		if (this.mHostnameLayout != exception) {
1121			this.mHostnameLayout.setErrorEnabled(false);
1122			this.mHostnameLayout.setError(null);
1123		}
1124		if (this.mPortLayout != exception) {
1125			this.mPortLayout.setErrorEnabled(false);
1126			this.mPortLayout.setError(null);
1127		}
1128	}
1129
1130	private void showDeletePgpDialog() {
1131		AlertDialog.Builder builder = new AlertDialog.Builder(this);
1132		builder.setTitle(R.string.unpublish_pgp);
1133		builder.setMessage(R.string.unpublish_pgp_message);
1134		builder.setNegativeButton(R.string.cancel, null);
1135		builder.setPositiveButton(R.string.confirm, (dialogInterface, i) -> {
1136			mAccount.setPgpSignId(0);
1137			mAccount.unsetPgpSignature();
1138			xmppConnectionService.databaseBackend.updateAccount(mAccount);
1139			xmppConnectionService.sendPresence(mAccount);
1140			refreshUiReal();
1141		});
1142		builder.create().show();
1143	}
1144
1145	private void showOsOptimizationWarning(boolean showBatteryWarning, boolean showDataSaverWarning) {
1146		this.binding.osOptimization.setVisibility(showBatteryWarning || showDataSaverWarning ? View.VISIBLE : View.GONE);
1147		if (showDataSaverWarning) {
1148			this.binding.osOptimizationHeadline.setText(R.string.data_saver_enabled);
1149			this.getmDisableOsOptimizationsBody.setText(R.string.data_saver_enabled_explained);
1150			this.mDisableOsOptimizationsButton.setText(R.string.allow);
1151			this.mDisableOsOptimizationsButton.setOnClickListener(v -> {
1152				Intent intent = new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS);
1153				Uri uri = Uri.parse("package:" + getPackageName());
1154				intent.setData(uri);
1155				try {
1156					startActivityForResult(intent, REQUEST_DATA_SAVER);
1157				} catch (ActivityNotFoundException e) {
1158					Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_data_saver, Toast.LENGTH_SHORT).show();
1159				}
1160			});
1161		} else if (showBatteryWarning) {
1162			this.mDisableOsOptimizationsButton.setText(R.string.disable);
1163			this.binding.osOptimizationHeadline.setText(R.string.battery_optimizations_enabled);
1164			this.getmDisableOsOptimizationsBody.setText(R.string.battery_optimizations_enabled_explained);
1165			this.mDisableOsOptimizationsButton.setOnClickListener(v -> {
1166				Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
1167				Uri uri = Uri.parse("package:" + getPackageName());
1168				intent.setData(uri);
1169				try {
1170					startActivityForResult(intent, REQUEST_BATTERY_OP);
1171				} catch (ActivityNotFoundException e) {
1172					Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
1173				}
1174			});
1175		}
1176	}
1177
1178	public void showWipePepDialog() {
1179		Builder builder = new Builder(this);
1180		builder.setTitle(getString(R.string.clear_other_devices));
1181		builder.setIconAttribute(android.R.attr.alertDialogIcon);
1182		builder.setMessage(getString(R.string.clear_other_devices_desc));
1183		builder.setNegativeButton(getString(R.string.cancel), null);
1184		builder.setPositiveButton(getString(R.string.accept),
1185				(dialog, which) -> mAccount.getAxolotlService().wipeOtherPepDevices());
1186		builder.create().show();
1187	}
1188
1189	private void editMamPrefs() {
1190		this.mFetchingMamPrefsToast = Toast.makeText(this, R.string.fetching_mam_prefs, Toast.LENGTH_LONG);
1191		this.mFetchingMamPrefsToast.show();
1192		xmppConnectionService.fetchMamPreferences(mAccount, this);
1193	}
1194
1195	@Override
1196	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
1197		refreshUi();
1198	}
1199
1200	@Override
1201	public void onCaptchaRequested(final Account account, final String id, final Data data, final Bitmap captcha) {
1202		runOnUiThread(() -> {
1203			if (mCaptchaDialog != null && mCaptchaDialog.isShowing()) {
1204				mCaptchaDialog.dismiss();
1205			}
1206			final Builder builder = new Builder(EditAccountActivity.this);
1207			final View view = getLayoutInflater().inflate(R.layout.captcha, null);
1208			final ImageView imageView = view.findViewById(R.id.captcha);
1209			final EditText input = view.findViewById(R.id.input);
1210			imageView.setImageBitmap(captcha);
1211
1212			builder.setTitle(getString(R.string.captcha_required));
1213			builder.setView(view);
1214
1215			builder.setPositiveButton(getString(R.string.ok),
1216					(dialog, which) -> {
1217						String rc = input.getText().toString();
1218						data.put("username", account.getUsername());
1219						data.put("password", account.getPassword());
1220						data.put("ocr", rc);
1221						data.submit();
1222
1223						if (xmppConnectionServiceBound) {
1224							xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, id, data);
1225						}
1226					});
1227			builder.setNegativeButton(getString(R.string.cancel), (dialog, which) -> {
1228				if (xmppConnectionService != null) {
1229					xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
1230				}
1231			});
1232
1233			builder.setOnCancelListener(dialog -> {
1234				if (xmppConnectionService != null) {
1235					xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
1236				}
1237			});
1238			mCaptchaDialog = builder.create();
1239			mCaptchaDialog.show();
1240			input.requestFocus();
1241		});
1242	}
1243
1244	public void onShowErrorToast(final int resId) {
1245		runOnUiThread(() -> Toast.makeText(EditAccountActivity.this, resId, Toast.LENGTH_SHORT).show());
1246	}
1247
1248	@Override
1249	public void onPreferencesFetched(final Element prefs) {
1250		runOnUiThread(() -> {
1251			if (mFetchingMamPrefsToast != null) {
1252				mFetchingMamPrefsToast.cancel();
1253			}
1254			Builder builder = new Builder(EditAccountActivity.this);
1255			builder.setTitle(R.string.server_side_mam_prefs);
1256			String defaultAttr = prefs.getAttribute("default");
1257			final List<String> defaults = Arrays.asList("never", "roster", "always");
1258			final AtomicInteger choice = new AtomicInteger(Math.max(0, defaults.indexOf(defaultAttr)));
1259			builder.setSingleChoiceItems(R.array.mam_prefs, choice.get(), (dialog, which) -> choice.set(which));
1260			builder.setNegativeButton(R.string.cancel, null);
1261			builder.setPositiveButton(R.string.ok, (dialog, which) -> {
1262				prefs.setAttribute("default", defaults.get(choice.get()));
1263				xmppConnectionService.pushMamPreferences(mAccount, prefs);
1264			});
1265			builder.create().show();
1266		});
1267	}
1268
1269	@Override
1270	public void onPreferencesFetchFailed() {
1271		runOnUiThread(() -> {
1272			if (mFetchingMamPrefsToast != null) {
1273				mFetchingMamPrefsToast.cancel();
1274			}
1275			Toast.makeText(EditAccountActivity.this, R.string.unable_to_fetch_mam_prefs, Toast.LENGTH_LONG).show();
1276		});
1277	}
1278
1279	@Override
1280	public void OnUpdateBlocklist(Status status) {
1281		refreshUi();
1282	}
1283}