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