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.OmemoActivity;
  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.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,
  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 ImageButton mRegenerateAxolotlKeyButton;
 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				final Intent intent;
 358				final XmppConnection connection = mAccount.getXmppConnection();
 359				final boolean wasFirstAccount = xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1;
 360				if (avatar != null || (connection != null && !connection.getFeatures().pep())) {
 361					intent = new Intent(getApplicationContext(), StartConversationActivity.class);
 362					if (wasFirstAccount) {
 363						intent.putExtra("init", true);
 364					}
 365				} else {
 366					intent = new Intent(getApplicationContext(), PublishProfilePictureActivity.class);
 367					intent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toBareJid().toString());
 368					intent.putExtra("setup", true);
 369				}
 370				if (wasFirstAccount) {
 371					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
 372				}
 373				startActivity(intent);
 374				finish();
 375			}
 376		});
 377	}
 378
 379	@Override
 380	public void onActivityResult(int requestCode, int resultCode, Intent data) {
 381		super.onActivityResult(requestCode, resultCode, data);
 382		if (requestCode == REQUEST_BATTERY_OP || requestCode == REQUEST_DATA_SAVER) {
 383			updateAccountInformation(mAccount == null);
 384		}
 385	}
 386
 387	@Override
 388	protected void processFingerprintVerification(XmppUri uri) {
 389		if (mAccount != null && mAccount.getJid().toBareJid().equals(uri.getJid()) && uri.hasFingerprints()) {
 390			if (xmppConnectionService.verifyFingerprints(mAccount,uri.getFingerprints())) {
 391				Toast.makeText(this,R.string.verified_fingerprints,Toast.LENGTH_SHORT).show();
 392			}
 393		} else {
 394			Toast.makeText(this,R.string.invalid_barcode,Toast.LENGTH_SHORT).show();
 395		}
 396	}
 397
 398	protected void updateSaveButton() {
 399		boolean accountInfoEdited = accountInfoEdited();
 400
 401		if (!mInitMode && passwordChangedInMagicCreateMode()) {
 402			this.mSaveButton.setText(R.string.change_password);
 403			this.mSaveButton.setEnabled(true);
 404			this.mSaveButton.setTextColor(getPrimaryTextColor());
 405		} else if (accountInfoEdited && !mInitMode) {
 406			this.mSaveButton.setText(R.string.save);
 407			this.mSaveButton.setEnabled(true);
 408			this.mSaveButton.setTextColor(getPrimaryTextColor());
 409		} else if (mAccount != null
 410				&& (mAccount.getStatus() == Account.State.CONNECTING || mAccount.getStatus() == Account.State.REGISTRATION_SUCCESSFUL|| mFetchingAvatar)) {
 411			this.mSaveButton.setEnabled(false);
 412			this.mSaveButton.setTextColor(getSecondaryTextColor());
 413			this.mSaveButton.setText(R.string.account_status_connecting);
 414		} else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !mInitMode) {
 415			this.mSaveButton.setEnabled(true);
 416			this.mSaveButton.setTextColor(getPrimaryTextColor());
 417			this.mSaveButton.setText(R.string.enable);
 418		} else {
 419			this.mSaveButton.setEnabled(true);
 420			this.mSaveButton.setTextColor(getPrimaryTextColor());
 421			if (!mInitMode) {
 422				if (mAccount != null && mAccount.isOnlineAndConnected()) {
 423					this.mSaveButton.setText(R.string.save);
 424					if (!accountInfoEdited) {
 425						this.mSaveButton.setEnabled(false);
 426						this.mSaveButton.setTextColor(getSecondaryTextColor());
 427					}
 428				} else {
 429					this.mSaveButton.setText(R.string.connect);
 430				}
 431			} else {
 432				this.mSaveButton.setText(R.string.next);
 433			}
 434		}
 435	}
 436
 437	protected boolean accountInfoEdited() {
 438		if (this.mAccount == null) {
 439			return false;
 440		}
 441		return jidEdited() ||
 442				!this.mAccount.getPassword().equals(this.mPassword.getText().toString()) ||
 443				!this.mAccount.getHostname().equals(this.mHostname.getText().toString()) ||
 444				!String.valueOf(this.mAccount.getPort()).equals(this.mPort.getText().toString());
 445	}
 446
 447	protected boolean jidEdited() {
 448		final String unmodified;
 449		if (mUsernameMode) {
 450			unmodified = this.mAccount.getJid().getLocalpart();
 451		} else {
 452			unmodified = this.mAccount.getJid().toBareJid().toString();
 453		}
 454		return !unmodified.equals(this.mAccountJid.getText().toString());
 455	}
 456
 457	protected boolean passwordChangedInMagicCreateMode() {
 458		return mAccount != null
 459				&& mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
 460				&& !this.mAccount.getPassword().equals(this.mPassword.getText().toString())
 461				&& !this.jidEdited()
 462				&& mAccount.isOnlineAndConnected();
 463	}
 464
 465	@Override
 466	protected String getShareableUri() {
 467		if (mAccount != null) {
 468			return mAccount.getShareableUri();
 469		} else {
 470			return "";
 471		}
 472	}
 473
 474	@Override
 475	protected void onCreate(final Bundle savedInstanceState) {
 476		super.onCreate(savedInstanceState);
 477		if (savedInstanceState != null) {
 478			this.mSavedInstanceAccount = savedInstanceState.getString("account");
 479			this.mSavedInstanceInit = savedInstanceState.getBoolean("initMode", false);
 480		}
 481		setContentView(R.layout.activity_edit_account);
 482		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
 483		this.mAccountJid.addTextChangedListener(this.mTextWatcher);
 484		this.mAccountJidLabel = (TextView) findViewById(R.id.account_jid_label);
 485		this.mPassword = (EditText) findViewById(R.id.account_password);
 486		this.mPassword.addTextChangedListener(this.mTextWatcher);
 487		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
 488		this.mAvatar = (ImageView) findViewById(R.id.avater);
 489		this.mAvatar.setOnClickListener(this.mAvatarClickListener);
 490		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
 491		this.mStats = (LinearLayout) findViewById(R.id.stats);
 492		this.mOsOptimizations = (RelativeLayout) findViewById(R.id.os_optimization);
 493		this.mDisableOsOptimizationsButton = (Button) findViewById(R.id.os_optimization_disable);
 494		this.mDisableOsOptimizationsHeadline = (TextView) findViewById(R.id.os_optimization_headline);
 495		this.getmDisableOsOptimizationsBody = (TextView) findViewById(R.id.os_optimization_body);
 496		this.mSessionEst = (TextView) findViewById(R.id.session_est);
 497		this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
 498		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
 499		this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
 500		this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
 501		this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
 502		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
 503		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
 504		this.mServerInfoHttpUpload = (TextView) findViewById(R.id.server_info_http_upload);
 505		this.mPushRow = (TableRow) findViewById(R.id.push_row);
 506		this.mServerInfoPush = (TextView) findViewById(R.id.server_info_push);
 507		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
 508		this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
 509		this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
 510		this.mAxolotlFingerprint = (TextView) findViewById(R.id.axolotl_fingerprint);
 511		this.mAxolotlFingerprintBox = (RelativeLayout) findViewById(R.id.axolotl_fingerprint_box);
 512		this.mAxolotlFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_axolotl_to_clipboard);
 513		this.mRegenerateAxolotlKeyButton = (ImageButton) findViewById(R.id.action_regenerate_axolotl_key);
 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		renewCertificate.setVisible(mAccount != null && mAccount.getPrivateKeyAlias() != null);
 569
 570		if (mAccount != null && mAccount.isOnlineAndConnected()) {
 571			if (!mAccount.getXmppConnection().getFeatures().blocking()) {
 572				showBlocklist.setVisible(false);
 573			}
 574			if (!mAccount.getXmppConnection().getFeatures().register()) {
 575				changePassword.setVisible(false);
 576			}
 577			mamPrefs.setVisible(mAccount.getXmppConnection().getFeatures().mam());
 578			changePresence.setVisible(manuallyChangePresence());
 579		} else {
 580			showQrCode.setVisible(false);
 581			showBlocklist.setVisible(false);
 582			showMoreInfo.setVisible(false);
 583			changePassword.setVisible(false);
 584			mamPrefs.setVisible(false);
 585			changePresence.setVisible(false);
 586		}
 587
 588		if (mAccount != null) {
 589			showPassword.setVisible(mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
 590					&& !mAccount.isOptionSet(Account.OPTION_REGISTER));
 591		} else {
 592			showPassword.setVisible(false);
 593		}
 594		return super.onCreateOptionsMenu(menu);
 595	}
 596
 597	@Override
 598	public boolean onPrepareOptionsMenu(Menu menu) {
 599		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
 600		if (showMoreInfo.isVisible()) {
 601			showMoreInfo.setChecked(mMoreTable.getVisibility() == View.VISIBLE);
 602		}
 603		return super.onPrepareOptionsMenu(menu);
 604	}
 605
 606	@Override
 607	protected void onStart() {
 608		super.onStart();
 609		final int theme = findTheme();
 610		if (this.mTheme != theme) {
 611			recreate();
 612		} else if (getIntent() != null) {
 613			try {
 614				this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
 615			} catch (final InvalidJidException | NullPointerException ignored) {
 616				this.jidToEdit = null;
 617			}
 618			boolean init = getIntent().getBooleanExtra("init", false);
 619			this.mInitMode = init || this.jidToEdit == null;
 620			this.messageFingerprint = getIntent().getStringExtra("fingerprint");
 621			if (!mInitMode) {
 622				this.mRegisterNew.setVisibility(View.GONE);
 623				if (getActionBar() != null) {
 624					getActionBar().setTitle(getString(R.string.account_details));
 625				}
 626			} else {
 627				this.mAvatar.setVisibility(View.GONE);
 628				ActionBar ab = getActionBar();
 629				if (ab != null) {
 630					if (init && Config.MAGIC_CREATE_DOMAIN == null) {
 631						ab.setDisplayShowHomeEnabled(false);
 632						ab.setDisplayHomeAsUpEnabled(false);
 633					}
 634					ab.setTitle(R.string.action_add_account);
 635				}
 636			}
 637		}
 638		SharedPreferences preferences = getPreferences();
 639		boolean useTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
 640		this.mShowOptions = useTor || preferences.getBoolean("show_connection_options", false);
 641		mHostname.setHint(useTor ? R.string.hostname_or_onion : R.string.hostname_example);
 642		this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
 643	}
 644
 645	@Override
 646	public void onSaveInstanceState(final Bundle savedInstanceState) {
 647		if (mAccount != null) {
 648			savedInstanceState.putString("account", mAccount.getJid().toBareJid().toString());
 649			savedInstanceState.putBoolean("initMode", mInitMode);
 650			savedInstanceState.putBoolean("showMoreTable", mMoreTable.getVisibility() == View.VISIBLE);
 651		}
 652		super.onSaveInstanceState(savedInstanceState);
 653	}
 654
 655	protected void onBackendConnected() {
 656		boolean init = true;
 657		if (mSavedInstanceAccount != null) {
 658			try {
 659				this.mAccount = xmppConnectionService.findAccountByJid(Jid.fromString(mSavedInstanceAccount));
 660				this.mInitMode = mSavedInstanceInit;
 661				init = false;
 662			} catch (InvalidJidException e) {
 663				this.mAccount = null;
 664			}
 665
 666		} else if (this.jidToEdit != null) {
 667			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
 668		}
 669
 670		if (mAccount != null) {
 671			this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER);
 672			this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER);
 673			if (this.mAccount.getPrivateKeyAlias() != null) {
 674				this.mPassword.setHint(R.string.authenticate_with_certificate);
 675				if (this.mInitMode) {
 676					this.mPassword.requestFocus();
 677				}
 678			}
 679			if (mPendingFingerprintVerificationUri != null) {
 680				processFingerprintVerification(mPendingFingerprintVerificationUri);
 681				mPendingFingerprintVerificationUri = null;
 682			}
 683			updateAccountInformation(init);
 684		}
 685
 686
 687		if (Config.MAGIC_CREATE_DOMAIN == null && this.xmppConnectionService.getAccounts().size() == 0) {
 688			this.mCancelButton.setEnabled(false);
 689			this.mCancelButton.setTextColor(getSecondaryTextColor());
 690		}
 691		if (mUsernameMode) {
 692			this.mAccountJidLabel.setText(R.string.username);
 693			this.mAccountJid.setHint(R.string.username_hint);
 694		} else {
 695			final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
 696					R.layout.simple_list_item,
 697					xmppConnectionService.getKnownHosts());
 698			this.mAccountJid.setAdapter(mKnownHostsAdapter);
 699		}
 700		updateSaveButton();
 701		invalidateOptionsMenu();
 702	}
 703
 704	private String getUserModeDomain() {
 705		if (mAccount != null) {
 706			return mAccount.getJid().getDomainpart();
 707		} else {
 708			return Config.DOMAIN_LOCK;
 709		}
 710	}
 711
 712	@Override
 713	public boolean onOptionsItemSelected(final MenuItem item) {
 714		switch (item.getItemId()) {
 715			case R.id.action_show_block_list:
 716				final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
 717				showBlocklistIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
 718				startActivity(showBlocklistIntent);
 719				break;
 720			case R.id.action_server_info_show_more:
 721				changeMoreTableVisibility(!item.isChecked());
 722				break;
 723			case R.id.action_share_barcode:
 724				shareBarcode();
 725				break;
 726			case R.id.action_share_http:
 727				shareLink(true);
 728				break;
 729			case R.id.action_share_uri:
 730				shareLink(false);
 731				break;
 732			case R.id.action_change_password_on_server:
 733				gotoChangePassword(null);
 734				break;
 735			case R.id.action_mam_prefs:
 736				editMamPrefs();
 737				break;
 738			case R.id.action_renew_certificate:
 739				renewCertificate();
 740				break;
 741			case R.id.action_change_presence:
 742				changePresence();
 743				break;
 744			case R.id.action_show_password:
 745				showPassword();
 746				break;
 747		}
 748		return super.onOptionsItemSelected(item);
 749	}
 750
 751	private void shareLink(boolean http) {
 752		Intent intent = new Intent(Intent.ACTION_SEND);
 753		intent.setType("text/plain");
 754		String text;
 755		if (http) {
 756			text = "https://conversations.im/i/"+mAccount.getJid().toBareJid().toString();
 757		} else {
 758			text = mAccount.getShareableUri();
 759		}
 760		intent.putExtra(Intent.EXTRA_TEXT,text);
 761		startActivity(Intent.createChooser(intent, getText(R.string.share_with)));
 762	}
 763
 764	private void shareBarcode() {
 765		Intent intent = new Intent(Intent.ACTION_SEND);
 766		intent.putExtra(Intent.EXTRA_STREAM,BarcodeProvider.getUriForAccount(mAccount));
 767		intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 768		intent.setType("image/png");
 769		startActivity(Intent.createChooser(intent, getText(R.string.share_with)));
 770	}
 771
 772	private void changeMoreTableVisibility(boolean visible) {
 773		mMoreTable.setVisibility(visible ? View.VISIBLE : View.GONE);
 774	}
 775
 776	private void gotoChangePassword(String newPassword) {
 777		final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
 778		changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
 779		if (newPassword != null) {
 780			changePasswordIntent.putExtra("password", newPassword);
 781		}
 782		startActivity(changePasswordIntent);
 783	}
 784
 785	private void renewCertificate() {
 786		KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null);
 787	}
 788
 789	private void changePresence() {
 790		Intent intent = new Intent(this, SetPresenceActivity.class);
 791		intent.putExtra(SetPresenceActivity.EXTRA_ACCOUNT,mAccount.getJid().toBareJid().toString());
 792		startActivity(intent);
 793	}
 794
 795	@Override
 796	public void alias(String alias) {
 797		if (alias != null) {
 798			xmppConnectionService.updateKeyInAccount(mAccount, alias);
 799		}
 800	}
 801
 802	private void updateAccountInformation(boolean init) {
 803		if (init) {
 804			this.mAccountJid.getEditableText().clear();
 805			if (mUsernameMode) {
 806				this.mAccountJid.getEditableText().append(this.mAccount.getJid().getLocalpart());
 807			} else {
 808				this.mAccountJid.getEditableText().append(this.mAccount.getJid().toBareJid().toString());
 809			}
 810			this.mPassword.setText(this.mAccount.getPassword());
 811			this.mHostname.setText("");
 812			this.mHostname.getEditableText().append(this.mAccount.getHostname());
 813			this.mPort.setText("");
 814			this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort()));
 815			this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
 816
 817		}
 818
 819		if (!mInitMode) {
 820			this.mAvatar.setVisibility(View.VISIBLE);
 821			this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
 822		} else {
 823			this.mAvatar.setVisibility(View.GONE);
 824		}
 825		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
 826			this.mRegisterNew.setVisibility(View.VISIBLE);
 827			this.mRegisterNew.setChecked(true);
 828			this.mPasswordConfirm.setText(this.mAccount.getPassword());
 829		} else {
 830			this.mRegisterNew.setVisibility(View.GONE);
 831			this.mRegisterNew.setChecked(false);
 832		}
 833		if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
 834			Features features = this.mAccount.getXmppConnection().getFeatures();
 835			this.mStats.setVisibility(View.VISIBLE);
 836			boolean showBatteryWarning = !xmppConnectionService.getPushManagementService().available(mAccount) && isOptimizingBattery();
 837			boolean showDataSaverWarning = isAffectedByDataSaver();
 838			showOsOptimizationWarning(showBatteryWarning,showDataSaverWarning);
 839			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
 840					.getLastSessionEstablished()));
 841			if (features.rosterVersioning()) {
 842				this.mServerInfoRosterVersion.setText(R.string.server_info_available);
 843			} else {
 844				this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
 845			}
 846			if (features.carbons()) {
 847				this.mServerInfoCarbons.setText(R.string.server_info_available);
 848			} else {
 849				this.mServerInfoCarbons
 850						.setText(R.string.server_info_unavailable);
 851			}
 852			if (features.mam()) {
 853				this.mServerInfoMam.setText(R.string.server_info_available);
 854			} else {
 855				this.mServerInfoMam.setText(R.string.server_info_unavailable);
 856			}
 857			if (features.csi()) {
 858				this.mServerInfoCSI.setText(R.string.server_info_available);
 859			} else {
 860				this.mServerInfoCSI.setText(R.string.server_info_unavailable);
 861			}
 862			if (features.blocking()) {
 863				this.mServerInfoBlocking.setText(R.string.server_info_available);
 864			} else {
 865				this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
 866			}
 867			if (features.sm()) {
 868				this.mServerInfoSm.setText(R.string.server_info_available);
 869			} else {
 870				this.mServerInfoSm.setText(R.string.server_info_unavailable);
 871			}
 872			if (features.pep()) {
 873				AxolotlService axolotlService = this.mAccount.getAxolotlService();
 874				if (axolotlService != null && axolotlService.isPepBroken()) {
 875					this.mServerInfoPep.setText(R.string.server_info_broken);
 876				} else {
 877					this.mServerInfoPep.setText(R.string.server_info_available);
 878				}
 879			} else {
 880				this.mServerInfoPep.setText(R.string.server_info_unavailable);
 881			}
 882			if (features.httpUpload(0)) {
 883				this.mServerInfoHttpUpload.setText(R.string.server_info_available);
 884			} else {
 885				this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable);
 886			}
 887
 888			this.mPushRow.setVisibility(xmppConnectionService.getPushManagementService().isStub() ? View.GONE : View.VISIBLE);
 889
 890			if (xmppConnectionService.getPushManagementService().available(mAccount)) {
 891				this.mServerInfoPush.setText(R.string.server_info_available);
 892			} else {
 893				this.mServerInfoPush.setText(R.string.server_info_unavailable);
 894			}
 895			final String otrFingerprint = this.mAccount.getOtrFingerprint();
 896			if (otrFingerprint != null && Config.supportOtr()) {
 897				this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
 898				this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
 899				this.mOtrFingerprintToClipboardButton
 900						.setVisibility(View.VISIBLE);
 901				this.mOtrFingerprintToClipboardButton
 902						.setOnClickListener(new View.OnClickListener() {
 903
 904							@Override
 905							public void onClick(final View v) {
 906
 907								if (copyTextToClipboard(otrFingerprint, R.string.otr_fingerprint)) {
 908									Toast.makeText(
 909											EditAccountActivity.this,
 910											R.string.toast_message_otr_fingerprint,
 911											Toast.LENGTH_SHORT).show();
 912								}
 913							}
 914						});
 915			} else {
 916				this.mOtrFingerprintBox.setVisibility(View.GONE);
 917			}
 918			final String ownAxolotlFingerprint = this.mAccount.getAxolotlService().getOwnFingerprint();
 919			if (ownAxolotlFingerprint != null && Config.supportOmemo()) {
 920				this.mAxolotlFingerprintBox.setVisibility(View.VISIBLE);
 921				if (ownAxolotlFingerprint.equals(messageFingerprint)) {
 922					this.mOwnFingerprintDesc.setTextColor(getResources().getColor(R.color.accent));
 923				} else {
 924					this.mOwnFingerprintDesc.setTextColor(getSecondaryTextColor());
 925				}
 926				this.mAxolotlFingerprint.setText(CryptoHelper.prettifyFingerprint(ownAxolotlFingerprint.substring(2)));
 927				this.mAxolotlFingerprintToClipboardButton
 928						.setVisibility(View.VISIBLE);
 929				this.mAxolotlFingerprintToClipboardButton
 930						.setOnClickListener(new View.OnClickListener() {
 931
 932							@Override
 933							public void onClick(final View v) {
 934								copyOmemoFingerprint(ownAxolotlFingerprint);
 935							}
 936						});
 937				if (Config.SHOW_REGENERATE_AXOLOTL_KEYS_BUTTON) {
 938					this.mRegenerateAxolotlKeyButton
 939							.setVisibility(View.VISIBLE);
 940					this.mRegenerateAxolotlKeyButton
 941							.setOnClickListener(new View.OnClickListener() {
 942
 943								@Override
 944								public void onClick(final View v) {
 945									showRegenerateAxolotlKeyDialog();
 946								}
 947							});
 948				}
 949			} else {
 950				this.mAxolotlFingerprintBox.setVisibility(View.GONE);
 951			}
 952			boolean hasKeys = false;
 953			keys.removeAllViews();
 954			for(XmppAxolotlSession session : mAccount.getAxolotlService().findOwnSessions()) {
 955				if (!session.getTrust().isCompromised()) {
 956					boolean highlight = session.getFingerprint().equals(messageFingerprint);
 957					addFingerprintRow(keys,session,highlight);
 958					hasKeys = true;
 959				}
 960			}
 961			if (hasKeys && Config.supportOmemo()) {
 962				keysCard.setVisibility(View.VISIBLE);
 963				Set<Integer> otherDevices = mAccount.getAxolotlService().getOwnDeviceIds();
 964				if (otherDevices == null || otherDevices.isEmpty()) {
 965					mClearDevicesButton.setVisibility(View.GONE);
 966				} else {
 967					mClearDevicesButton.setVisibility(View.VISIBLE);
 968				}
 969			} else {
 970				keysCard.setVisibility(View.GONE);
 971			}
 972		} else {
 973			if (this.mAccount.errorStatus()) {
 974				final EditText errorTextField;
 975				if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
 976					errorTextField = this.mPassword;
 977				} else if (mShowOptions
 978						&& this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
 979						&& this.mHostname.getText().length() > 0) {
 980					errorTextField = this.mHostname;
 981				} else {
 982					errorTextField = this.mAccountJid;
 983				}
 984				errorTextField.setError(getString(this.mAccount.getStatus().getReadableId()));
 985				if (init || !accountInfoEdited()) {
 986					errorTextField.requestFocus();
 987				}
 988			} else {
 989				this.mAccountJid.setError(null);
 990				this.mPassword.setError(null);
 991				this.mHostname.setError(null);
 992			}
 993			this.mStats.setVisibility(View.GONE);
 994		}
 995	}
 996
 997	private void showOsOptimizationWarning(boolean showBatteryWarning, boolean showDataSaverWarning) {
 998		this.mOsOptimizations.setVisibility(showBatteryWarning || showDataSaverWarning ? View.VISIBLE : View.GONE);
 999		if (showDataSaverWarning) {
1000			this.mDisableOsOptimizationsHeadline.setText(R.string.data_saver_enabled);
1001			this.getmDisableOsOptimizationsBody.setText(R.string.data_saver_enabled_explained);
1002			this.mDisableOsOptimizationsButton.setText(R.string.allow);
1003			this.mDisableOsOptimizationsButton.setOnClickListener(new OnClickListener() {
1004				@Override
1005				public void onClick(View v) {
1006					Intent intent = new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS);
1007					Uri uri = Uri.parse("package:"+getPackageName());
1008					intent.setData(uri);
1009					try {
1010						startActivityForResult(intent, REQUEST_DATA_SAVER);
1011					} catch (ActivityNotFoundException e) {
1012						Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_data_saver, Toast.LENGTH_SHORT).show();
1013					}
1014				}
1015			});
1016		} else if (showBatteryWarning) {
1017			this.mDisableOsOptimizationsButton.setText(R.string.disable);
1018			this.mDisableOsOptimizationsHeadline.setText(R.string.battery_optimizations_enabled);
1019			this.getmDisableOsOptimizationsBody.setText(R.string.battery_optimizations_enabled_explained);
1020			this.mDisableOsOptimizationsButton.setOnClickListener(new OnClickListener() {
1021				@Override
1022				public void onClick(View v) {
1023					Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
1024					Uri uri = Uri.parse("package:"+getPackageName());
1025					intent.setData(uri);
1026					try {
1027						startActivityForResult(intent, REQUEST_BATTERY_OP);
1028					} catch (ActivityNotFoundException e) {
1029						Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
1030					}
1031				}
1032			});
1033		}
1034	}
1035
1036	public void showRegenerateAxolotlKeyDialog() {
1037		Builder builder = new Builder(this);
1038		builder.setTitle("Regenerate Key");
1039		builder.setIconAttribute(android.R.attr.alertDialogIcon);
1040		builder.setMessage("Are you sure you want to regenerate your Identity Key? (This will also wipe all established sessions and contact Identity Keys)");
1041		builder.setNegativeButton(getString(R.string.cancel), null);
1042		builder.setPositiveButton("Yes",
1043				new DialogInterface.OnClickListener() {
1044					@Override
1045					public void onClick(DialogInterface dialog, int which) {
1046						mAccount.getAxolotlService().regenerateKeys(false);
1047					}
1048				});
1049		builder.create().show();
1050	}
1051
1052	public void showWipePepDialog() {
1053		Builder builder = new Builder(this);
1054		builder.setTitle(getString(R.string.clear_other_devices));
1055		builder.setIconAttribute(android.R.attr.alertDialogIcon);
1056		builder.setMessage(getString(R.string.clear_other_devices_desc));
1057		builder.setNegativeButton(getString(R.string.cancel), null);
1058		builder.setPositiveButton(getString(R.string.accept),
1059				new DialogInterface.OnClickListener() {
1060					@Override
1061					public void onClick(DialogInterface dialog, int which) {
1062						mAccount.getAxolotlService().wipeOtherPepDevices();
1063					}
1064				});
1065		builder.create().show();
1066	}
1067
1068	private void editMamPrefs() {
1069		this.mFetchingMamPrefsToast = Toast.makeText(this, R.string.fetching_mam_prefs, Toast.LENGTH_LONG);
1070		this.mFetchingMamPrefsToast.show();
1071		xmppConnectionService.fetchMamPreferences(mAccount, this);
1072	}
1073
1074	private void showPassword() {
1075		AlertDialog.Builder builder = new AlertDialog.Builder(this);
1076		View view = getLayoutInflater().inflate(R.layout.dialog_show_password, null);
1077		TextView password = (TextView) view.findViewById(R.id.password);
1078		password.setText(mAccount.getPassword());
1079		builder.setTitle(R.string.password);
1080		builder.setView(view);
1081		builder.setPositiveButton(R.string.cancel, null);
1082		builder.create().show();
1083	}
1084
1085	@Override
1086	public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
1087		refreshUi();
1088	}
1089
1090	@Override
1091	public void onCaptchaRequested(final Account account, final String id, final Data data, final Bitmap captcha) {
1092		runOnUiThread(new Runnable() {
1093			@Override
1094			public void run() {
1095				if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) {
1096					mCaptchaDialog.dismiss();
1097				}
1098				final AlertDialog.Builder builder = new AlertDialog.Builder(EditAccountActivity.this);
1099				final View view = getLayoutInflater().inflate(R.layout.captcha, null);
1100				final ImageView imageView = (ImageView) view.findViewById(R.id.captcha);
1101				final EditText input = (EditText) view.findViewById(R.id.input);
1102				imageView.setImageBitmap(captcha);
1103
1104				builder.setTitle(getString(R.string.captcha_required));
1105				builder.setView(view);
1106
1107				builder.setPositiveButton(getString(R.string.ok),
1108						new DialogInterface.OnClickListener() {
1109							@Override
1110							public void onClick(DialogInterface dialog, int which) {
1111								String rc = input.getText().toString();
1112								data.put("username", account.getUsername());
1113								data.put("password", account.getPassword());
1114								data.put("ocr", rc);
1115								data.submit();
1116
1117								if (xmppConnectionServiceBound) {
1118									xmppConnectionService.sendCreateAccountWithCaptchaPacket(
1119											account, id, data);
1120								}
1121							}
1122						});
1123				builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
1124					@Override
1125					public void onClick(DialogInterface dialog, int which) {
1126						if (xmppConnectionService != null) {
1127							xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
1128						}
1129					}
1130				});
1131
1132				builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
1133					@Override
1134					public void onCancel(DialogInterface dialog) {
1135						if (xmppConnectionService != null) {
1136							xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
1137						}
1138					}
1139				});
1140				mCaptchaDialog = builder.create();
1141				mCaptchaDialog.show();
1142			}
1143		});
1144	}
1145
1146	public void onShowErrorToast(final int resId) {
1147		runOnUiThread(new Runnable() {
1148			@Override
1149			public void run() {
1150				Toast.makeText(EditAccountActivity.this, resId, Toast.LENGTH_SHORT).show();
1151			}
1152		});
1153	}
1154
1155	@Override
1156	public void onPreferencesFetched(final Element prefs) {
1157		runOnUiThread(new Runnable() {
1158			@Override
1159			public void run() {
1160				if (mFetchingMamPrefsToast != null) {
1161					mFetchingMamPrefsToast.cancel();
1162				}
1163				AlertDialog.Builder builder = new Builder(EditAccountActivity.this);
1164				builder.setTitle(R.string.server_side_mam_prefs);
1165				String defaultAttr = prefs.getAttribute("default");
1166				final List<String> defaults = Arrays.asList("never", "roster", "always");
1167				final AtomicInteger choice = new AtomicInteger(Math.max(0,defaults.indexOf(defaultAttr)));
1168				builder.setSingleChoiceItems(R.array.mam_prefs, choice.get(), new DialogInterface.OnClickListener() {
1169					@Override
1170					public void onClick(DialogInterface dialog, int which) {
1171						choice.set(which);
1172					}
1173				});
1174				builder.setNegativeButton(R.string.cancel, null);
1175				builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1176					@Override
1177					public void onClick(DialogInterface dialog, int which) {
1178						prefs.setAttribute("default",defaults.get(choice.get()));
1179						xmppConnectionService.pushMamPreferences(mAccount, prefs);
1180					}
1181				});
1182				builder.create().show();
1183			}
1184		});
1185	}
1186
1187	@Override
1188	public void onPreferencesFetchFailed() {
1189		runOnUiThread(new Runnable() {
1190			@Override
1191			public void run() {
1192				if (mFetchingMamPrefsToast != null) {
1193					mFetchingMamPrefsToast.cancel();
1194				}
1195				Toast.makeText(EditAccountActivity.this,R.string.unable_to_fetch_mam_prefs,Toast.LENGTH_LONG).show();
1196			}
1197		});
1198	}
1199}