EditAccountActivity.java

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