1package eu.siacs.conversations.ui;
2
3import android.app.PendingIntent;
4import android.content.Intent;
5import android.os.Bundle;
6import android.text.Editable;
7import android.text.TextWatcher;
8import android.view.Menu;
9import android.view.MenuItem;
10import android.view.View;
11import android.view.View.OnClickListener;
12import android.widget.AutoCompleteTextView;
13import android.widget.Button;
14import android.widget.CheckBox;
15import android.widget.CompoundButton;
16import android.widget.CompoundButton.OnCheckedChangeListener;
17import android.widget.EditText;
18import android.widget.ImageButton;
19import android.widget.ImageView;
20import android.widget.LinearLayout;
21import android.widget.RelativeLayout;
22import android.widget.TableLayout;
23import android.widget.TextView;
24import android.widget.Toast;
25
26import eu.siacs.conversations.R;
27import eu.siacs.conversations.entities.Account;
28import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
29import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
30import eu.siacs.conversations.utils.CryptoHelper;
31import eu.siacs.conversations.utils.UIHelper;
32import eu.siacs.conversations.xmpp.XmppConnection.Features;
33import eu.siacs.conversations.xmpp.jid.InvalidJidException;
34import eu.siacs.conversations.xmpp.jid.Jid;
35import eu.siacs.conversations.xmpp.pep.Avatar;
36
37public class EditAccountActivity extends XmppActivity implements OnAccountUpdate{
38
39 private AutoCompleteTextView mAccountJid;
40 private EditText mPassword;
41 private EditText mPasswordConfirm;
42 private CheckBox mRegisterNew;
43 private Button mCancelButton;
44 private Button mSaveButton;
45 private TableLayout mMoreTable;
46
47 private LinearLayout mStats;
48 private TextView mServerInfoSm;
49 private TextView mServerInfoRosterVersion;
50 private TextView mServerInfoCarbons;
51 private TextView mServerInfoMam;
52 private TextView mServerInfoCSI;
53 private TextView mServerInfoBlocking;
54 private TextView mServerInfoPep;
55 private TextView mSessionEst;
56 private TextView mOtrFingerprint;
57 private ImageView mAvatar;
58 private RelativeLayout mOtrFingerprintBox;
59 private ImageButton mOtrFingerprintToClipboardButton;
60
61 private Jid jidToEdit;
62 private Account mAccount;
63
64 private boolean mFetchingAvatar = false;
65
66 private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
67
68 @Override
69 public void onClick(final View v) {
70 if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED && !accountInfoEdited()) {
71 mAccount.setOption(Account.OPTION_DISABLED, false);
72 xmppConnectionService.updateAccount(mAccount);
73 return;
74 }
75 final boolean registerNewAccount = mRegisterNew.isChecked();
76 final Jid jid;
77 try {
78 jid = Jid.fromString(mAccountJid.getText().toString());
79 } catch (final InvalidJidException e) {
80 mAccountJid.setError(getString(R.string.invalid_jid));
81 mAccountJid.requestFocus();
82 return;
83 }
84 if (jid.isDomainJid()) {
85 mAccountJid.setError(getString(R.string.invalid_jid));
86 mAccountJid.requestFocus();
87 return;
88 }
89 final String password = mPassword.getText().toString();
90 final String passwordConfirm = mPasswordConfirm.getText().toString();
91 if (registerNewAccount) {
92 if (!password.equals(passwordConfirm)) {
93 mPasswordConfirm.setError(getString(R.string.passwords_do_not_match));
94 mPasswordConfirm.requestFocus();
95 return;
96 }
97 }
98 if (mAccount != null) {
99 try {
100 mAccount.setUsername(jid.hasLocalpart() ? jid.getLocalpart() : "");
101 mAccount.setServer(jid.getDomainpart());
102 } catch (final InvalidJidException ignored) {
103 return;
104 }
105 mAccountJid.setError(null);
106 mPasswordConfirm.setError(null);
107 mAccount.setPassword(password);
108 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
109 xmppConnectionService.updateAccount(mAccount);
110 } else {
111 try {
112 if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
113 mAccountJid.setError(getString(R.string.account_already_exists));
114 mAccountJid.requestFocus();
115 return;
116 }
117 } catch (final InvalidJidException e) {
118 return;
119 }
120 mAccount = new Account(jid.toBareJid(), password);
121 mAccount.setOption(Account.OPTION_USETLS, true);
122 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
123 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
124 xmppConnectionService.createAccount(mAccount);
125 }
126 if (jidToEdit != null && !mAccount.isOptionSet(Account.OPTION_DISABLED)) {
127 finish();
128 } else {
129 updateSaveButton();
130 updateAccountInformation(true);
131 }
132
133 }
134 };
135 private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
136
137 @Override
138 public void onClick(final View v) {
139 finish();
140 }
141 };
142 @Override
143 public void onAccountUpdate() {
144 runOnUiThread(new Runnable() {
145
146 @Override
147 public void run() {
148 invalidateOptionsMenu();
149 if (mAccount != null
150 && mAccount.getStatus() != Account.State.ONLINE
151 && mFetchingAvatar) {
152 startActivity(new Intent(getApplicationContext(),
153 ManageAccountActivity.class));
154 finish();
155 } else if (jidToEdit == null && mAccount != null
156 && mAccount.getStatus() == Account.State.ONLINE) {
157 if (!mFetchingAvatar) {
158 mFetchingAvatar = true;
159 xmppConnectionService.checkForAvatar(mAccount,
160 mAvatarFetchCallback);
161 }
162 } else {
163 updateSaveButton();
164 }
165 if (mAccount != null) {
166 updateAccountInformation(false);
167 }
168 }
169 });
170 }
171 private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
172
173 @Override
174 public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
175 finishInitialSetup(avatar);
176 }
177
178 @Override
179 public void success(final Avatar avatar) {
180 finishInitialSetup(avatar);
181 }
182
183 @Override
184 public void error(final int errorCode, final Avatar avatar) {
185 finishInitialSetup(avatar);
186 }
187 };
188 private final TextWatcher mTextWatcher = new TextWatcher() {
189
190 @Override
191 public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
192 updateSaveButton();
193 }
194
195 @Override
196 public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
197 }
198
199 @Override
200 public void afterTextChanged(final Editable s) {
201
202 }
203 };
204
205 private final OnClickListener mAvatarClickListener = new OnClickListener() {
206 @Override
207 public void onClick(final View view) {
208 if (mAccount != null) {
209 final Intent intent = new Intent(getApplicationContext(),
210 PublishProfilePictureActivity.class);
211 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
212 startActivity(intent);
213 }
214 }
215 };
216
217 protected void finishInitialSetup(final Avatar avatar) {
218 runOnUiThread(new Runnable() {
219
220 @Override
221 public void run() {
222 final Intent intent;
223 if (avatar != null) {
224 intent = new Intent(getApplicationContext(),
225 StartConversationActivity.class);
226 if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 1) {
227 intent.putExtra("init", true);
228 }
229 } else {
230 intent = new Intent(getApplicationContext(),
231 PublishProfilePictureActivity.class);
232 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
233 intent.putExtra("setup", true);
234 }
235 startActivity(intent);
236 finish();
237 }
238 });
239 }
240
241 protected void updateSaveButton() {
242 if (accountInfoEdited() && jidToEdit != null) {
243 this.mSaveButton.setText(R.string.save);
244 this.mSaveButton.setEnabled(true);
245 this.mSaveButton.setTextColor(getPrimaryTextColor());
246 } else if (mAccount != null && (mAccount.getStatus() == Account.State.CONNECTING || mFetchingAvatar)) {
247 this.mSaveButton.setEnabled(false);
248 this.mSaveButton.setTextColor(getSecondaryTextColor());
249 this.mSaveButton.setText(R.string.account_status_connecting);
250 } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
251 this.mSaveButton.setEnabled(true);
252 this.mSaveButton.setTextColor(getPrimaryTextColor());
253 this.mSaveButton.setText(R.string.enable);
254 } else {
255 this.mSaveButton.setEnabled(true);
256 this.mSaveButton.setTextColor(getPrimaryTextColor());
257 if (jidToEdit != null) {
258 if (mAccount != null && mAccount.isOnlineAndConnected()) {
259 this.mSaveButton.setText(R.string.save);
260 if (!accountInfoEdited()) {
261 this.mSaveButton.setEnabled(false);
262 this.mSaveButton.setTextColor(getSecondaryTextColor());
263 }
264 } else {
265 this.mSaveButton.setText(R.string.connect);
266 }
267 } else {
268 this.mSaveButton.setText(R.string.next);
269 }
270 }
271 }
272
273 protected boolean accountInfoEdited() {
274 return this.mAccount != null && (!this.mAccount.getJid().toBareJid().toString().equals(
275 this.mAccountJid.getText().toString())
276 || !this.mAccount.getPassword().equals(
277 this.mPassword.getText().toString()));
278 }
279
280 @Override
281 protected String getShareableUri() {
282 if (mAccount!=null) {
283 return mAccount.getShareableUri();
284 } else {
285 return "";
286 }
287 }
288
289 @Override
290 protected void onCreate(final Bundle savedInstanceState) {
291 super.onCreate(savedInstanceState);
292 setContentView(R.layout.activity_edit_account);
293 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
294 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
295 this.mPassword = (EditText) findViewById(R.id.account_password);
296 this.mPassword.addTextChangedListener(this.mTextWatcher);
297 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
298 this.mAvatar = (ImageView) findViewById(R.id.avater);
299 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
300 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
301 this.mStats = (LinearLayout) findViewById(R.id.stats);
302 this.mSessionEst = (TextView) findViewById(R.id.session_est);
303 this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
304 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
305 this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
306 this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
307 this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
308 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
309 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
310 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
311 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
312 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
313 this.mSaveButton = (Button) findViewById(R.id.save_button);
314 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
315 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
316 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
317 this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
318 final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
319 @Override
320 public void onCheckedChanged(final CompoundButton buttonView,
321 final boolean isChecked) {
322 if (isChecked) {
323 mPasswordConfirm.setVisibility(View.VISIBLE);
324 } else {
325 mPasswordConfirm.setVisibility(View.GONE);
326 }
327 updateSaveButton();
328 }
329 };
330 this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
331 }
332
333 @Override
334 public boolean onCreateOptionsMenu(final Menu menu) {
335 super.onCreateOptionsMenu(menu);
336 getMenuInflater().inflate(R.menu.editaccount, menu);
337 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
338 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
339 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
340 final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
341 if (mAccount != null && mAccount.isOnlineAndConnected()) {
342 if (!mAccount.getXmppConnection().getFeatures().blocking()) {
343 showBlocklist.setVisible(false);
344 }
345 if (!mAccount.getXmppConnection().getFeatures().register()) {
346 changePassword.setVisible(false);
347 }
348 } else {
349 showQrCode.setVisible(false);
350 showBlocklist.setVisible(false);
351 showMoreInfo.setVisible(false);
352 changePassword.setVisible(false);
353 }
354 return true;
355 }
356
357 @Override
358 protected void onStart() {
359 super.onStart();
360 if (getIntent() != null) {
361 try {
362 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
363 } catch (final InvalidJidException | NullPointerException ignored) {
364 this.jidToEdit = null;
365 }
366 if (this.jidToEdit != null) {
367 this.mRegisterNew.setVisibility(View.GONE);
368 if (getActionBar() != null) {
369 getActionBar().setTitle(getString(R.string.account_details));
370 }
371 } else {
372 this.mAvatar.setVisibility(View.GONE);
373 if (getActionBar() != null) {
374 getActionBar().setTitle(R.string.action_add_account);
375 }
376 }
377 }
378 }
379
380 @Override
381 protected void onBackendConnected() {
382 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
383 android.R.layout.simple_list_item_1,
384 xmppConnectionService.getKnownHosts());
385 if (this.jidToEdit != null) {
386 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
387 updateAccountInformation(true);
388 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
389 if (getActionBar() != null) {
390 getActionBar().setDisplayHomeAsUpEnabled(false);
391 getActionBar().setDisplayShowHomeEnabled(false);
392 getActionBar().setHomeButtonEnabled(false);
393 }
394 this.mCancelButton.setEnabled(false);
395 this.mCancelButton.setTextColor(getSecondaryTextColor());
396 }
397 this.mAccountJid.setAdapter(mKnownHostsAdapter);
398 updateSaveButton();
399 }
400
401 @Override
402 public boolean onOptionsItemSelected(final MenuItem item) {
403 switch (item.getItemId()) {
404 case R.id.action_show_block_list:
405 final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
406 showBlocklistIntent.putExtra("account", mAccount.getJid().toString());
407 startActivity(showBlocklistIntent);
408 break;
409 case R.id.action_server_info_show_more:
410 mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
411 item.setChecked(!item.isChecked());
412 break;
413 case R.id.action_change_password_on_server:
414 final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
415 changePasswordIntent.putExtra("account", mAccount.getJid().toString());
416 startActivity(changePasswordIntent);
417 break;
418 }
419 return super.onOptionsItemSelected(item);
420 }
421
422 private void updateAccountInformation(boolean init) {
423 if (init) {
424 this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
425 this.mPassword.setText(this.mAccount.getPassword());
426 }
427 if (this.jidToEdit != null) {
428 this.mAvatar.setVisibility(View.VISIBLE);
429 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
430 }
431 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
432 this.mRegisterNew.setVisibility(View.VISIBLE);
433 this.mRegisterNew.setChecked(true);
434 this.mPasswordConfirm.setText(this.mAccount.getPassword());
435 } else {
436 this.mRegisterNew.setVisibility(View.GONE);
437 this.mRegisterNew.setChecked(false);
438 }
439 if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
440 this.mStats.setVisibility(View.VISIBLE);
441 this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
442 .getLastSessionEstablished()));
443 Features features = this.mAccount.getXmppConnection().getFeatures();
444 if (features.rosterVersioning()) {
445 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
446 } else {
447 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
448 }
449 if (features.carbons()) {
450 this.mServerInfoCarbons.setText(R.string.server_info_available);
451 } else {
452 this.mServerInfoCarbons
453 .setText(R.string.server_info_unavailable);
454 }
455 if (features.mam()) {
456 this.mServerInfoMam.setText(R.string.server_info_available);
457 } else {
458 this.mServerInfoMam.setText(R.string.server_info_unavailable);
459 }
460 if (features.csi()) {
461 this.mServerInfoCSI.setText(R.string.server_info_available);
462 } else {
463 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
464 }
465 if (features.blocking()) {
466 this.mServerInfoBlocking.setText(R.string.server_info_available);
467 } else {
468 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
469 }
470 if (features.sm()) {
471 this.mServerInfoSm.setText(R.string.server_info_available);
472 } else {
473 this.mServerInfoSm.setText(R.string.server_info_unavailable);
474 }
475 if (features.pep()) {
476 this.mServerInfoPep.setText(R.string.server_info_available);
477 } else {
478 this.mServerInfoPep.setText(R.string.server_info_unavailable);
479 }
480 final String fingerprint = this.mAccount.getOtrFingerprint();
481 if (fingerprint != null) {
482 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
483 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
484 this.mOtrFingerprintToClipboardButton
485 .setVisibility(View.VISIBLE);
486 this.mOtrFingerprintToClipboardButton
487 .setOnClickListener(new View.OnClickListener() {
488
489 @Override
490 public void onClick(final View v) {
491
492 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
493 Toast.makeText(
494 EditAccountActivity.this,
495 R.string.toast_message_otr_fingerprint,
496 Toast.LENGTH_SHORT).show();
497 }
498 }
499 });
500 } else {
501 this.mOtrFingerprintBox.setVisibility(View.GONE);
502 }
503 } else {
504 if (this.mAccount.errorStatus()) {
505 this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
506 if (init || !accountInfoEdited()) {
507 this.mAccountJid.requestFocus();
508 }
509 } else {
510 this.mAccountJid.setError(null);
511 }
512 this.mStats.setVisibility(View.GONE);
513 }
514 }
515}