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