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