EditAccountActivity.java

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