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