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