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