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