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