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