EditAccountActivity.java

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