EditAccountActivity.java

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