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) {
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) {
127 finish();
128 } else {
129 updateSaveButton();
130 updateAccountInformation();
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();
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 intent.putExtra("init",true);
227 } else {
228 intent = new Intent(getApplicationContext(),
229 PublishProfilePictureActivity.class);
230 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
231 intent.putExtra("setup", true);
232 }
233 startActivity(intent);
234 finish();
235 }
236 });
237 }
238
239 protected void updateSaveButton() {
240 if (mAccount != null && (mAccount.getStatus() == Account.State.CONNECTING || mFetchingAvatar)) {
241 this.mSaveButton.setEnabled(false);
242 this.mSaveButton.setTextColor(getSecondaryTextColor());
243 this.mSaveButton.setText(R.string.account_status_connecting);
244 } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
245 this.mSaveButton.setEnabled(true);
246 this.mSaveButton.setTextColor(getPrimaryTextColor());
247 this.mSaveButton.setText(R.string.enable);
248 } else {
249 this.mSaveButton.setEnabled(true);
250 this.mSaveButton.setTextColor(getPrimaryTextColor());
251 if (jidToEdit != null) {
252 if (mAccount != null && mAccount.isOnlineAndConnected()) {
253 this.mSaveButton.setText(R.string.save);
254 if (!accountInfoEdited()) {
255 this.mSaveButton.setEnabled(false);
256 this.mSaveButton.setTextColor(getSecondaryTextColor());
257 }
258 } else {
259 this.mSaveButton.setText(R.string.connect);
260 }
261 } else {
262 this.mSaveButton.setText(R.string.next);
263 }
264 }
265 }
266
267 protected boolean accountInfoEdited() {
268 return (!this.mAccount.getJid().toBareJid().toString().equals(
269 this.mAccountJid.getText().toString()))
270 || (!this.mAccount.getPassword().equals(
271 this.mPassword.getText().toString()));
272 }
273
274 @Override
275 protected String getShareableUri() {
276 if (mAccount!=null) {
277 return mAccount.getShareableUri();
278 } else {
279 return "";
280 }
281 }
282
283 @Override
284 protected void onCreate(final Bundle savedInstanceState) {
285 super.onCreate(savedInstanceState);
286 setContentView(R.layout.activity_edit_account);
287 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
288 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
289 this.mPassword = (EditText) findViewById(R.id.account_password);
290 this.mPassword.addTextChangedListener(this.mTextWatcher);
291 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
292 this.mAvatar = (ImageView) findViewById(R.id.avater);
293 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
294 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
295 this.mStats = (LinearLayout) findViewById(R.id.stats);
296 this.mSessionEst = (TextView) findViewById(R.id.session_est);
297 this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
298 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
299 this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
300 this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
301 this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
302 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
303 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
304 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
305 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
306 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
307 this.mSaveButton = (Button) findViewById(R.id.save_button);
308 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
309 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
310 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
311 this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
312 final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
313 @Override
314 public void onCheckedChanged(final CompoundButton buttonView,
315 final boolean isChecked) {
316 if (isChecked) {
317 mPasswordConfirm.setVisibility(View.VISIBLE);
318 } else {
319 mPasswordConfirm.setVisibility(View.GONE);
320 }
321 updateSaveButton();
322 }
323 };
324 this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
325 }
326
327 @Override
328 public boolean onCreateOptionsMenu(final Menu menu) {
329 super.onCreateOptionsMenu(menu);
330 getMenuInflater().inflate(R.menu.editaccount, menu);
331 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
332 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
333 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
334 final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
335 if (mAccount != null && mAccount.isOnlineAndConnected()) {
336 if (!mAccount.getXmppConnection().getFeatures().blocking()) {
337 showBlocklist.setVisible(false);
338 }
339 if (!mAccount.getXmppConnection().getFeatures().register()) {
340 changePassword.setVisible(false);
341 }
342 } else {
343 showQrCode.setVisible(false);
344 showBlocklist.setVisible(false);
345 showMoreInfo.setVisible(false);
346 changePassword.setVisible(false);
347 }
348 return true;
349 }
350
351 @Override
352 protected void onStart() {
353 super.onStart();
354 if (getIntent() != null) {
355 try {
356 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
357 } catch (final InvalidJidException | NullPointerException ignored) {
358 this.jidToEdit = null;
359 }
360 if (this.jidToEdit != null) {
361 this.mRegisterNew.setVisibility(View.GONE);
362 if (getActionBar() != null) {
363 getActionBar().setTitle(getString(R.string.account_details));
364 }
365 } else {
366 this.mAvatar.setVisibility(View.GONE);
367 if (getActionBar() != null) {
368 getActionBar().setTitle(R.string.action_add_account);
369 }
370 }
371 }
372 }
373
374 @Override
375 protected void onBackendConnected() {
376 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
377 android.R.layout.simple_list_item_1,
378 xmppConnectionService.getKnownHosts());
379 if (this.jidToEdit != null) {
380 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
381 updateAccountInformation();
382 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
383 if (getActionBar() != null) {
384 getActionBar().setDisplayHomeAsUpEnabled(false);
385 getActionBar().setDisplayShowHomeEnabled(false);
386 getActionBar().setHomeButtonEnabled(false);
387 }
388 this.mCancelButton.setEnabled(false);
389 this.mCancelButton.setTextColor(getSecondaryTextColor());
390 }
391 this.mAccountJid.setAdapter(mKnownHostsAdapter);
392 updateSaveButton();
393 }
394
395 @Override
396 public boolean onOptionsItemSelected(final MenuItem item) {
397 switch (item.getItemId()) {
398 case R.id.action_show_block_list:
399 final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
400 showBlocklistIntent.putExtra("account", mAccount.getJid().toString());
401 startActivity(showBlocklistIntent);
402 break;
403 case R.id.action_server_info_show_more:
404 mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
405 item.setChecked(!item.isChecked());
406 break;
407 case R.id.action_change_password_on_server:
408 final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
409 changePasswordIntent.putExtra("account", mAccount.getJid().toString());
410 startActivity(changePasswordIntent);
411 break;
412 }
413 return super.onOptionsItemSelected(item);
414 }
415
416 private void updateAccountInformation() {
417 this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
418 this.mPassword.setText(this.mAccount.getPassword());
419 if (this.jidToEdit != null) {
420 this.mAvatar.setVisibility(View.VISIBLE);
421 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
422 }
423 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
424 this.mRegisterNew.setVisibility(View.VISIBLE);
425 this.mRegisterNew.setChecked(true);
426 this.mPasswordConfirm.setText(this.mAccount.getPassword());
427 } else {
428 this.mRegisterNew.setVisibility(View.GONE);
429 this.mRegisterNew.setChecked(false);
430 }
431 if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
432 this.mStats.setVisibility(View.VISIBLE);
433 this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
434 .getLastSessionEstablished()));
435 Features features = this.mAccount.getXmppConnection().getFeatures();
436 if (features.rosterVersioning()) {
437 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
438 } else {
439 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
440 }
441 if (features.carbons()) {
442 this.mServerInfoCarbons.setText(R.string.server_info_available);
443 } else {
444 this.mServerInfoCarbons
445 .setText(R.string.server_info_unavailable);
446 }
447 if (features.mam()) {
448 this.mServerInfoMam.setText(R.string.server_info_available);
449 } else {
450 this.mServerInfoMam.setText(R.string.server_info_unavailable);
451 }
452 if (features.csi()) {
453 this.mServerInfoCSI.setText(R.string.server_info_available);
454 } else {
455 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
456 }
457 if (features.blocking()) {
458 this.mServerInfoBlocking.setText(R.string.server_info_available);
459 } else {
460 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
461 }
462 if (features.sm()) {
463 this.mServerInfoSm.setText(R.string.server_info_available);
464 } else {
465 this.mServerInfoSm.setText(R.string.server_info_unavailable);
466 }
467 if (features.pubsub()) {
468 this.mServerInfoPep.setText(R.string.server_info_available);
469 } else {
470 this.mServerInfoPep.setText(R.string.server_info_unavailable);
471 }
472 final String fingerprint = this.mAccount.getOtrFingerprint();
473 if (fingerprint != null) {
474 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
475 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
476 this.mOtrFingerprintToClipboardButton
477 .setVisibility(View.VISIBLE);
478 this.mOtrFingerprintToClipboardButton
479 .setOnClickListener(new View.OnClickListener() {
480
481 @Override
482 public void onClick(final View v) {
483
484 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
485 Toast.makeText(
486 EditAccountActivity.this,
487 R.string.toast_message_otr_fingerprint,
488 Toast.LENGTH_SHORT).show();
489 }
490 }
491 });
492 } else {
493 this.mOtrFingerprintBox.setVisibility(View.GONE);
494 }
495 } else {
496 if (this.mAccount.errorStatus()) {
497 this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
498 this.mAccountJid.requestFocus();
499 } else {
500 this.mAccountJid.setError(null);
501 }
502 this.mStats.setVisibility(View.GONE);
503 }
504 }
505}