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