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.OmemoActivity;
45import eu.siacs.conversations.R;
46import eu.siacs.conversations.crypto.axolotl.AxolotlService;
47import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
48import eu.siacs.conversations.entities.Account;
49import eu.siacs.conversations.services.BarcodeProvider;
50import eu.siacs.conversations.services.XmppConnectionService.OnCaptchaRequested;
51import eu.siacs.conversations.services.XmppConnectionService;
52import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
53import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
54import eu.siacs.conversations.utils.CryptoHelper;
55import eu.siacs.conversations.utils.UIHelper;
56import eu.siacs.conversations.utils.XmppUri;
57import eu.siacs.conversations.xml.Element;
58import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
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,
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 }
577 if (!mAccount.getXmppConnection().getFeatures().register()) {
578 changePassword.setVisible(false);
579 }
580 mamPrefs.setVisible(mAccount.getXmppConnection().getFeatures().mam());
581 changePresence.setVisible(manuallyChangePresence());
582 } else {
583 showQrCode.setVisible(false);
584 showBlocklist.setVisible(false);
585 showMoreInfo.setVisible(false);
586 changePassword.setVisible(false);
587 mamPrefs.setVisible(false);
588 changePresence.setVisible(false);
589 }
590
591 if (mAccount != null) {
592 showPassword.setVisible(mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE)
593 && !mAccount.isOptionSet(Account.OPTION_REGISTER));
594 } else {
595 showPassword.setVisible(false);
596 }
597 return super.onCreateOptionsMenu(menu);
598 }
599
600 @Override
601 public boolean onPrepareOptionsMenu(Menu menu) {
602 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
603 if (showMoreInfo.isVisible()) {
604 showMoreInfo.setChecked(mMoreTable.getVisibility() == View.VISIBLE);
605 }
606 return super.onPrepareOptionsMenu(menu);
607 }
608
609 @Override
610 protected void onStart() {
611 super.onStart();
612 final int theme = findTheme();
613 if (this.mTheme != theme) {
614 recreate();
615 } else if (getIntent() != null) {
616 try {
617 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
618 } catch (final InvalidJidException | NullPointerException ignored) {
619 this.jidToEdit = null;
620 }
621 boolean init = getIntent().getBooleanExtra("init", false);
622 this.mInitMode = init || this.jidToEdit == null;
623 this.messageFingerprint = getIntent().getStringExtra("fingerprint");
624 if (!mInitMode) {
625 this.mRegisterNew.setVisibility(View.GONE);
626 if (getActionBar() != null) {
627 getActionBar().setTitle(getString(R.string.account_details));
628 }
629 } else {
630 this.mAvatar.setVisibility(View.GONE);
631 ActionBar ab = getActionBar();
632 if (ab != null) {
633 if (init && Config.MAGIC_CREATE_DOMAIN == null) {
634 ab.setDisplayShowHomeEnabled(false);
635 ab.setDisplayHomeAsUpEnabled(false);
636 }
637 ab.setTitle(R.string.action_add_account);
638 }
639 }
640 }
641 SharedPreferences preferences = getPreferences();
642 boolean useTor = Config.FORCE_ORBOT || preferences.getBoolean("use_tor", false);
643 this.mShowOptions = useTor || preferences.getBoolean("show_connection_options", false);
644 mHostname.setHint(useTor ? R.string.hostname_or_onion : R.string.hostname_example);
645 this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
646 }
647
648 @Override
649 public void onSaveInstanceState(final Bundle savedInstanceState) {
650 if (mAccount != null) {
651 savedInstanceState.putString("account", mAccount.getJid().toBareJid().toString());
652 savedInstanceState.putBoolean("initMode", mInitMode);
653 savedInstanceState.putBoolean("showMoreTable", mMoreTable.getVisibility() == View.VISIBLE);
654 }
655 super.onSaveInstanceState(savedInstanceState);
656 }
657
658 protected void onBackendConnected() {
659 boolean init = true;
660 if (mSavedInstanceAccount != null) {
661 try {
662 this.mAccount = xmppConnectionService.findAccountByJid(Jid.fromString(mSavedInstanceAccount));
663 this.mInitMode = mSavedInstanceInit;
664 init = false;
665 } catch (InvalidJidException e) {
666 this.mAccount = null;
667 }
668
669 } else if (this.jidToEdit != null) {
670 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
671 }
672
673 if (mAccount != null) {
674 this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER);
675 this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER);
676 if (this.mAccount.getPrivateKeyAlias() != null) {
677 this.mPassword.setHint(R.string.authenticate_with_certificate);
678 if (this.mInitMode) {
679 this.mPassword.requestFocus();
680 }
681 }
682 if (mPendingFingerprintVerificationUri != null) {
683 processFingerprintVerification(mPendingFingerprintVerificationUri);
684 mPendingFingerprintVerificationUri = null;
685 }
686 updateAccountInformation(init);
687 }
688
689
690 if (Config.MAGIC_CREATE_DOMAIN == null && this.xmppConnectionService.getAccounts().size() == 0) {
691 this.mCancelButton.setEnabled(false);
692 this.mCancelButton.setTextColor(getSecondaryTextColor());
693 }
694 if (mUsernameMode) {
695 this.mAccountJidLabel.setText(R.string.username);
696 this.mAccountJid.setHint(R.string.username_hint);
697 } else {
698 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
699 R.layout.simple_list_item,
700 xmppConnectionService.getKnownHosts());
701 this.mAccountJid.setAdapter(mKnownHostsAdapter);
702 }
703 updateSaveButton();
704 invalidateOptionsMenu();
705 }
706
707 private String getUserModeDomain() {
708 if (mAccount != null) {
709 return mAccount.getJid().getDomainpart();
710 } else {
711 return Config.DOMAIN_LOCK;
712 }
713 }
714
715 @Override
716 public boolean onOptionsItemSelected(final MenuItem item) {
717 switch (item.getItemId()) {
718 case R.id.action_show_block_list:
719 final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
720 showBlocklistIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
721 startActivity(showBlocklistIntent);
722 break;
723 case R.id.action_server_info_show_more:
724 changeMoreTableVisibility(!item.isChecked());
725 break;
726 case R.id.action_share_barcode:
727 shareBarcode();
728 break;
729 case R.id.action_share_http:
730 shareLink(true);
731 break;
732 case R.id.action_share_uri:
733 shareLink(false);
734 break;
735 case R.id.action_change_password_on_server:
736 gotoChangePassword(null);
737 break;
738 case R.id.action_mam_prefs:
739 editMamPrefs();
740 break;
741 case R.id.action_renew_certificate:
742 renewCertificate();
743 break;
744 case R.id.action_change_presence:
745 changePresence();
746 break;
747 case R.id.action_show_password:
748 showPassword();
749 break;
750 }
751 return super.onOptionsItemSelected(item);
752 }
753
754 private void shareLink(boolean http) {
755 Intent intent = new Intent(Intent.ACTION_SEND);
756 intent.setType("text/plain");
757 String text;
758 if (http) {
759 text = mAccount.getShareableLink();
760 } else {
761 text = mAccount.getShareableUri();
762 }
763 intent.putExtra(Intent.EXTRA_TEXT,text);
764 startActivity(Intent.createChooser(intent, getText(R.string.share_with)));
765 }
766
767 private void shareBarcode() {
768 Intent intent = new Intent(Intent.ACTION_SEND);
769 intent.putExtra(Intent.EXTRA_STREAM,BarcodeProvider.getUriForAccount(this,mAccount));
770 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
771 intent.setType("image/png");
772 startActivity(Intent.createChooser(intent, getText(R.string.share_with)));
773 }
774
775 private void changeMoreTableVisibility(boolean visible) {
776 mMoreTable.setVisibility(visible ? View.VISIBLE : View.GONE);
777 }
778
779 private void gotoChangePassword(String newPassword) {
780 final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
781 changePasswordIntent.putExtra(EXTRA_ACCOUNT, mAccount.getJid().toString());
782 if (newPassword != null) {
783 changePasswordIntent.putExtra("password", newPassword);
784 }
785 startActivity(changePasswordIntent);
786 }
787
788 private void renewCertificate() {
789 KeyChain.choosePrivateKeyAlias(this, this, null, null, null, -1, null);
790 }
791
792 private void changePresence() {
793 Intent intent = new Intent(this, SetPresenceActivity.class);
794 intent.putExtra(SetPresenceActivity.EXTRA_ACCOUNT,mAccount.getJid().toBareJid().toString());
795 startActivity(intent);
796 }
797
798 @Override
799 public void alias(String alias) {
800 if (alias != null) {
801 xmppConnectionService.updateKeyInAccount(mAccount, alias);
802 }
803 }
804
805 private void updateAccountInformation(boolean init) {
806 if (init) {
807 this.mAccountJid.getEditableText().clear();
808 if (mUsernameMode) {
809 this.mAccountJid.getEditableText().append(this.mAccount.getJid().getLocalpart());
810 } else {
811 this.mAccountJid.getEditableText().append(this.mAccount.getJid().toBareJid().toString());
812 }
813 this.mPassword.setText(this.mAccount.getPassword());
814 this.mHostname.setText("");
815 this.mHostname.getEditableText().append(this.mAccount.getHostname());
816 this.mPort.setText("");
817 this.mPort.getEditableText().append(String.valueOf(this.mAccount.getPort()));
818 this.mNamePort.setVisibility(mShowOptions ? View.VISIBLE : View.GONE);
819
820 }
821
822 if (!mInitMode) {
823 this.mAvatar.setVisibility(View.VISIBLE);
824 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
825 } else {
826 this.mAvatar.setVisibility(View.GONE);
827 }
828 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
829 this.mRegisterNew.setVisibility(View.VISIBLE);
830 this.mRegisterNew.setChecked(true);
831 this.mPasswordConfirm.setText(this.mAccount.getPassword());
832 } else {
833 this.mRegisterNew.setVisibility(View.GONE);
834 this.mRegisterNew.setChecked(false);
835 }
836 if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
837 Features features = this.mAccount.getXmppConnection().getFeatures();
838 this.mStats.setVisibility(View.VISIBLE);
839 boolean showBatteryWarning = !xmppConnectionService.getPushManagementService().available(mAccount) && isOptimizingBattery();
840 boolean showDataSaverWarning = isAffectedByDataSaver();
841 showOsOptimizationWarning(showBatteryWarning,showDataSaverWarning);
842 this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
843 .getLastSessionEstablished()));
844 if (features.rosterVersioning()) {
845 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
846 } else {
847 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
848 }
849 if (features.carbons()) {
850 this.mServerInfoCarbons.setText(R.string.server_info_available);
851 } else {
852 this.mServerInfoCarbons
853 .setText(R.string.server_info_unavailable);
854 }
855 if (features.mam()) {
856 this.mServerInfoMam.setText(R.string.server_info_available);
857 } else {
858 this.mServerInfoMam.setText(R.string.server_info_unavailable);
859 }
860 if (features.csi()) {
861 this.mServerInfoCSI.setText(R.string.server_info_available);
862 } else {
863 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
864 }
865 if (features.blocking()) {
866 this.mServerInfoBlocking.setText(R.string.server_info_available);
867 } else {
868 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
869 }
870 if (features.sm()) {
871 this.mServerInfoSm.setText(R.string.server_info_available);
872 } else {
873 this.mServerInfoSm.setText(R.string.server_info_unavailable);
874 }
875 if (features.pep()) {
876 AxolotlService axolotlService = this.mAccount.getAxolotlService();
877 if (axolotlService != null && axolotlService.isPepBroken()) {
878 this.mServerInfoPep.setText(R.string.server_info_broken);
879 } else {
880 this.mServerInfoPep.setText(R.string.server_info_available);
881 }
882 } else {
883 this.mServerInfoPep.setText(R.string.server_info_unavailable);
884 }
885 if (features.httpUpload(0)) {
886 this.mServerInfoHttpUpload.setText(R.string.server_info_available);
887 } else {
888 this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable);
889 }
890
891 this.mPushRow.setVisibility(xmppConnectionService.getPushManagementService().isStub() ? View.GONE : View.VISIBLE);
892
893 if (xmppConnectionService.getPushManagementService().available(mAccount)) {
894 this.mServerInfoPush.setText(R.string.server_info_available);
895 } else {
896 this.mServerInfoPush.setText(R.string.server_info_unavailable);
897 }
898 final String otrFingerprint = this.mAccount.getOtrFingerprint();
899 if (otrFingerprint != null && Config.supportOtr()) {
900 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
901 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(otrFingerprint));
902 this.mOtrFingerprintToClipboardButton
903 .setVisibility(View.VISIBLE);
904 this.mOtrFingerprintToClipboardButton
905 .setOnClickListener(new View.OnClickListener() {
906
907 @Override
908 public void onClick(final View v) {
909
910 if (copyTextToClipboard(otrFingerprint, R.string.otr_fingerprint)) {
911 Toast.makeText(
912 EditAccountActivity.this,
913 R.string.toast_message_otr_fingerprint,
914 Toast.LENGTH_SHORT).show();
915 }
916 }
917 });
918 } else {
919 this.mOtrFingerprintBox.setVisibility(View.GONE);
920 }
921 final String ownAxolotlFingerprint = this.mAccount.getAxolotlService().getOwnFingerprint();
922 if (ownAxolotlFingerprint != null && Config.supportOmemo()) {
923 this.mAxolotlFingerprintBox.setVisibility(View.VISIBLE);
924 if (ownAxolotlFingerprint.equals(messageFingerprint)) {
925 this.mOwnFingerprintDesc.setTextColor(getResources().getColor(R.color.accent));
926 } else {
927 this.mOwnFingerprintDesc.setTextColor(getSecondaryTextColor());
928 }
929 this.mAxolotlFingerprint.setText(CryptoHelper.prettifyFingerprint(ownAxolotlFingerprint.substring(2)));
930 this.mAxolotlFingerprintToClipboardButton
931 .setVisibility(View.VISIBLE);
932 this.mAxolotlFingerprintToClipboardButton
933 .setOnClickListener(new View.OnClickListener() {
934
935 @Override
936 public void onClick(final View v) {
937 copyOmemoFingerprint(ownAxolotlFingerprint);
938 }
939 });
940 if (Config.SHOW_REGENERATE_AXOLOTL_KEYS_BUTTON) {
941 this.mRegenerateAxolotlKeyButton
942 .setVisibility(View.VISIBLE);
943 this.mRegenerateAxolotlKeyButton
944 .setOnClickListener(new View.OnClickListener() {
945
946 @Override
947 public void onClick(final View v) {
948 showRegenerateAxolotlKeyDialog();
949 }
950 });
951 }
952 } else {
953 this.mAxolotlFingerprintBox.setVisibility(View.GONE);
954 }
955 boolean hasKeys = false;
956 keys.removeAllViews();
957 for(XmppAxolotlSession session : mAccount.getAxolotlService().findOwnSessions()) {
958 if (!session.getTrust().isCompromised()) {
959 boolean highlight = session.getFingerprint().equals(messageFingerprint);
960 addFingerprintRow(keys,session,highlight);
961 hasKeys = true;
962 }
963 }
964 if (hasKeys && Config.supportOmemo()) {
965 keysCard.setVisibility(View.VISIBLE);
966 Set<Integer> otherDevices = mAccount.getAxolotlService().getOwnDeviceIds();
967 if (otherDevices == null || otherDevices.isEmpty()) {
968 mClearDevicesButton.setVisibility(View.GONE);
969 } else {
970 mClearDevicesButton.setVisibility(View.VISIBLE);
971 }
972 } else {
973 keysCard.setVisibility(View.GONE);
974 }
975 } else {
976 if (this.mAccount.errorStatus()) {
977 final EditText errorTextField;
978 if (this.mAccount.getStatus() == Account.State.UNAUTHORIZED) {
979 errorTextField = this.mPassword;
980 } else if (mShowOptions
981 && this.mAccount.getStatus() == Account.State.SERVER_NOT_FOUND
982 && this.mHostname.getText().length() > 0) {
983 errorTextField = this.mHostname;
984 } else {
985 errorTextField = this.mAccountJid;
986 }
987 errorTextField.setError(getString(this.mAccount.getStatus().getReadableId()));
988 if (init || !accountInfoEdited()) {
989 errorTextField.requestFocus();
990 }
991 } else {
992 this.mAccountJid.setError(null);
993 this.mPassword.setError(null);
994 this.mHostname.setError(null);
995 }
996 this.mStats.setVisibility(View.GONE);
997 }
998 }
999
1000 private void showOsOptimizationWarning(boolean showBatteryWarning, boolean showDataSaverWarning) {
1001 this.mOsOptimizations.setVisibility(showBatteryWarning || showDataSaverWarning ? View.VISIBLE : View.GONE);
1002 if (showDataSaverWarning) {
1003 this.mDisableOsOptimizationsHeadline.setText(R.string.data_saver_enabled);
1004 this.getmDisableOsOptimizationsBody.setText(R.string.data_saver_enabled_explained);
1005 this.mDisableOsOptimizationsButton.setText(R.string.allow);
1006 this.mDisableOsOptimizationsButton.setOnClickListener(new OnClickListener() {
1007 @Override
1008 public void onClick(View v) {
1009 Intent intent = new Intent(Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS);
1010 Uri uri = Uri.parse("package:"+getPackageName());
1011 intent.setData(uri);
1012 try {
1013 startActivityForResult(intent, REQUEST_DATA_SAVER);
1014 } catch (ActivityNotFoundException e) {
1015 Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_data_saver, Toast.LENGTH_SHORT).show();
1016 }
1017 }
1018 });
1019 } else if (showBatteryWarning) {
1020 this.mDisableOsOptimizationsButton.setText(R.string.disable);
1021 this.mDisableOsOptimizationsHeadline.setText(R.string.battery_optimizations_enabled);
1022 this.getmDisableOsOptimizationsBody.setText(R.string.battery_optimizations_enabled_explained);
1023 this.mDisableOsOptimizationsButton.setOnClickListener(new OnClickListener() {
1024 @Override
1025 public void onClick(View v) {
1026 Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
1027 Uri uri = Uri.parse("package:"+getPackageName());
1028 intent.setData(uri);
1029 try {
1030 startActivityForResult(intent, REQUEST_BATTERY_OP);
1031 } catch (ActivityNotFoundException e) {
1032 Toast.makeText(EditAccountActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
1033 }
1034 }
1035 });
1036 }
1037 }
1038
1039 public void showRegenerateAxolotlKeyDialog() {
1040 Builder builder = new Builder(this);
1041 builder.setTitle("Regenerate Key");
1042 builder.setIconAttribute(android.R.attr.alertDialogIcon);
1043 builder.setMessage("Are you sure you want to regenerate your Identity Key? (This will also wipe all established sessions and contact Identity Keys)");
1044 builder.setNegativeButton(getString(R.string.cancel), null);
1045 builder.setPositiveButton("Yes",
1046 new DialogInterface.OnClickListener() {
1047 @Override
1048 public void onClick(DialogInterface dialog, int which) {
1049 mAccount.getAxolotlService().regenerateKeys(false);
1050 }
1051 });
1052 builder.create().show();
1053 }
1054
1055 public void showWipePepDialog() {
1056 Builder builder = new Builder(this);
1057 builder.setTitle(getString(R.string.clear_other_devices));
1058 builder.setIconAttribute(android.R.attr.alertDialogIcon);
1059 builder.setMessage(getString(R.string.clear_other_devices_desc));
1060 builder.setNegativeButton(getString(R.string.cancel), null);
1061 builder.setPositiveButton(getString(R.string.accept),
1062 new DialogInterface.OnClickListener() {
1063 @Override
1064 public void onClick(DialogInterface dialog, int which) {
1065 mAccount.getAxolotlService().wipeOtherPepDevices();
1066 }
1067 });
1068 builder.create().show();
1069 }
1070
1071 private void editMamPrefs() {
1072 this.mFetchingMamPrefsToast = Toast.makeText(this, R.string.fetching_mam_prefs, Toast.LENGTH_LONG);
1073 this.mFetchingMamPrefsToast.show();
1074 xmppConnectionService.fetchMamPreferences(mAccount, this);
1075 }
1076
1077 private void showPassword() {
1078 AlertDialog.Builder builder = new AlertDialog.Builder(this);
1079 View view = getLayoutInflater().inflate(R.layout.dialog_show_password, null);
1080 TextView password = (TextView) view.findViewById(R.id.password);
1081 password.setText(mAccount.getPassword());
1082 builder.setTitle(R.string.password);
1083 builder.setView(view);
1084 builder.setPositiveButton(R.string.cancel, null);
1085 builder.create().show();
1086 }
1087
1088 @Override
1089 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
1090 refreshUi();
1091 }
1092
1093 @Override
1094 public void onCaptchaRequested(final Account account, final String id, final Data data, final Bitmap captcha) {
1095 runOnUiThread(new Runnable() {
1096 @Override
1097 public void run() {
1098 if ((mCaptchaDialog != null) && mCaptchaDialog.isShowing()) {
1099 mCaptchaDialog.dismiss();
1100 }
1101 final AlertDialog.Builder builder = new AlertDialog.Builder(EditAccountActivity.this);
1102 final View view = getLayoutInflater().inflate(R.layout.captcha, null);
1103 final ImageView imageView = (ImageView) view.findViewById(R.id.captcha);
1104 final EditText input = (EditText) view.findViewById(R.id.input);
1105 imageView.setImageBitmap(captcha);
1106
1107 builder.setTitle(getString(R.string.captcha_required));
1108 builder.setView(view);
1109
1110 builder.setPositiveButton(getString(R.string.ok),
1111 new DialogInterface.OnClickListener() {
1112 @Override
1113 public void onClick(DialogInterface dialog, int which) {
1114 String rc = input.getText().toString();
1115 data.put("username", account.getUsername());
1116 data.put("password", account.getPassword());
1117 data.put("ocr", rc);
1118 data.submit();
1119
1120 if (xmppConnectionServiceBound) {
1121 xmppConnectionService.sendCreateAccountWithCaptchaPacket(
1122 account, id, data);
1123 }
1124 }
1125 });
1126 builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
1127 @Override
1128 public void onClick(DialogInterface dialog, int which) {
1129 if (xmppConnectionService != null) {
1130 xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
1131 }
1132 }
1133 });
1134
1135 builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
1136 @Override
1137 public void onCancel(DialogInterface dialog) {
1138 if (xmppConnectionService != null) {
1139 xmppConnectionService.sendCreateAccountWithCaptchaPacket(account, null, null);
1140 }
1141 }
1142 });
1143 mCaptchaDialog = builder.create();
1144 mCaptchaDialog.show();
1145 }
1146 });
1147 }
1148
1149 public void onShowErrorToast(final int resId) {
1150 runOnUiThread(new Runnable() {
1151 @Override
1152 public void run() {
1153 Toast.makeText(EditAccountActivity.this, resId, Toast.LENGTH_SHORT).show();
1154 }
1155 });
1156 }
1157
1158 @Override
1159 public void onPreferencesFetched(final Element prefs) {
1160 runOnUiThread(new Runnable() {
1161 @Override
1162 public void run() {
1163 if (mFetchingMamPrefsToast != null) {
1164 mFetchingMamPrefsToast.cancel();
1165 }
1166 AlertDialog.Builder builder = new Builder(EditAccountActivity.this);
1167 builder.setTitle(R.string.server_side_mam_prefs);
1168 String defaultAttr = prefs.getAttribute("default");
1169 final List<String> defaults = Arrays.asList("never", "roster", "always");
1170 final AtomicInteger choice = new AtomicInteger(Math.max(0,defaults.indexOf(defaultAttr)));
1171 builder.setSingleChoiceItems(R.array.mam_prefs, choice.get(), new DialogInterface.OnClickListener() {
1172 @Override
1173 public void onClick(DialogInterface dialog, int which) {
1174 choice.set(which);
1175 }
1176 });
1177 builder.setNegativeButton(R.string.cancel, null);
1178 builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1179 @Override
1180 public void onClick(DialogInterface dialog, int which) {
1181 prefs.setAttribute("default",defaults.get(choice.get()));
1182 xmppConnectionService.pushMamPreferences(mAccount, prefs);
1183 }
1184 });
1185 builder.create().show();
1186 }
1187 });
1188 }
1189
1190 @Override
1191 public void onPreferencesFetchFailed() {
1192 runOnUiThread(new Runnable() {
1193 @Override
1194 public void run() {
1195 if (mFetchingMamPrefsToast != null) {
1196 mFetchingMamPrefsToast.cancel();
1197 }
1198 Toast.makeText(EditAccountActivity.this,R.string.unable_to_fetch_mam_prefs,Toast.LENGTH_LONG).show();
1199 }
1200 });
1201 }
1202}