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