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