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