EditAccountActivity.java

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