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 OnClickListener mSaveButtonClickListener = new OnClickListener() {
67
68 @Override
69 public void onClick(final View v) {
70 if (mAccount != null
71 && mAccount.getStatus() == Account.State.DISABLED) {
72 mAccount.setOption(Account.OPTION_DISABLED, false);
73 xmppConnectionService.updateAccount(mAccount);
74 return;
75 }
76 final boolean registerNewAccount = mRegisterNew.isChecked();
77 final Jid jid;
78 try {
79 jid = Jid.fromString(mAccountJid.getText().toString());
80 } catch (final InvalidJidException e) {
81 mAccountJid.setError(getString(R.string.invalid_jid));
82 mAccountJid.requestFocus();
83 return;
84 }
85 if (jid.isDomainJid()) {
86 mAccountJid.setError(getString(R.string.invalid_jid));
87 mAccountJid.requestFocus();
88 return;
89 }
90 final String password = mPassword.getText().toString();
91 final String passwordConfirm = mPasswordConfirm.getText().toString();
92 if (registerNewAccount) {
93 if (!password.equals(passwordConfirm)) {
94 mPasswordConfirm
95 .setError(getString(R.string.passwords_do_not_match));
96 mPasswordConfirm.requestFocus();
97 return;
98 }
99 }
100 if (mAccount != null) {
101 mAccount.setPassword(password);
102 try {
103 mAccount.setUsername(jid.hasLocalpart() ? jid.getLocalpart() : "");
104 mAccount.setServer(jid.getDomainpart());
105 } catch (final InvalidJidException ignored) {
106 }
107 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
108 xmppConnectionService.updateAccount(mAccount);
109 } else {
110 try {
111 if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
112 mAccountJid
113 .setError(getString(R.string.account_already_exists));
114 mAccountJid.requestFocus();
115 return;
116 }
117 } catch (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 OnClickListener mCancelButtonClickListener = new OnClickListener() {
136
137 @Override
138 public void onClick(View v) {
139 finish();
140 }
141 };
142 @Override
143 public void onAccountUpdate() {
144 runOnUiThread(new Runnable() {
145
146 @Override
147 public void run() {
148 if (mAccount != null
149 && mAccount.getStatus() != Account.State.ONLINE
150 && mFetchingAvatar) {
151 startActivity(new Intent(getApplicationContext(),
152 ManageAccountActivity.class));
153 finish();
154 } else if (jidToEdit == null && mAccount != null
155 && mAccount.getStatus() == Account.State.ONLINE) {
156 if (!mFetchingAvatar) {
157 mFetchingAvatar = true;
158 xmppConnectionService.checkForAvatar(mAccount,
159 mAvatarFetchCallback);
160 }
161 } else {
162 updateSaveButton();
163 }
164 if (mAccount != null) {
165 updateAccountInformation();
166 }
167 }
168 });
169 }
170 private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
171
172 @Override
173 public void userInputRequried(PendingIntent pi, Avatar avatar) {
174 finishInitialSetup(avatar);
175 }
176
177 @Override
178 public void success(Avatar avatar) {
179 finishInitialSetup(avatar);
180 }
181
182 @Override
183 public void error(int errorCode, Avatar avatar) {
184 finishInitialSetup(avatar);
185 }
186 };
187 private TextWatcher mTextWatcher = new TextWatcher() {
188
189 @Override
190 public void onTextChanged(CharSequence s, int start, int before,
191 int count) {
192 updateSaveButton();
193 }
194
195 @Override
196 public void beforeTextChanged(CharSequence s, int start, int count,
197 int after) {
198
199 }
200
201 @Override
202 public void afterTextChanged(Editable s) {
203
204 }
205 };
206 private OnClickListener mAvatarClickListener = new OnClickListener() {
207 @Override
208 public void onClick(View view) {
209 if (mAccount!=null) {
210 Intent intent = new Intent(getApplicationContext(),
211 PublishProfilePictureActivity.class);
212 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
213 startActivity(intent);
214 }
215 }
216 };
217
218 protected void finishInitialSetup(final Avatar avatar) {
219 runOnUiThread(new Runnable() {
220
221 @Override
222 public void run() {
223 Intent intent;
224 if (avatar != null) {
225 intent = new Intent(getApplicationContext(),
226 StartConversationActivity.class);
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
241 && mAccount.getStatus() == Account.State.CONNECTING) {
242 this.mSaveButton.setEnabled(false);
243 this.mSaveButton.setTextColor(getSecondaryTextColor());
244 this.mSaveButton.setText(R.string.account_status_connecting);
245 } else if (mAccount != null
246 && mAccount.getStatus() == Account.State.DISABLED) {
247 this.mSaveButton.setEnabled(true);
248 this.mSaveButton.setTextColor(getPrimaryTextColor());
249 this.mSaveButton.setText(R.string.enable);
250 } else {
251 this.mSaveButton.setEnabled(true);
252 this.mSaveButton.setTextColor(getPrimaryTextColor());
253 if (jidToEdit != null) {
254 if (mAccount != null
255 && mAccount.getStatus() == Account.State.ONLINE) {
256 this.mSaveButton.setText(R.string.save);
257 if (!accountInfoEdited()) {
258 this.mSaveButton.setEnabled(false);
259 this.mSaveButton.setTextColor(getSecondaryTextColor());
260 }
261 } else {
262 this.mSaveButton.setText(R.string.connect);
263 }
264 } else {
265 this.mSaveButton.setText(R.string.next);
266 }
267 }
268 }
269
270 protected boolean accountInfoEdited() {
271 return (!this.mAccount.getJid().toBareJid().equals(
272 this.mAccountJid.getText().toString()))
273 || (!this.mAccount.getPassword().equals(
274 this.mPassword.getText().toString()));
275 }
276
277 @Override
278 protected String getShareableUri() {
279 if (mAccount!=null) {
280 return mAccount.getShareableUri();
281 } else {
282 return "";
283 }
284 }
285
286 @Override
287 protected void onCreate(Bundle savedInstanceState) {
288 super.onCreate(savedInstanceState);
289 setContentView(R.layout.activity_edit_account);
290 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
291 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
292 this.mPassword = (EditText) findViewById(R.id.account_password);
293 this.mPassword.addTextChangedListener(this.mTextWatcher);
294 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
295 this.mAvatar = (ImageView) findViewById(R.id.avater);
296 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
297 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
298 this.mStats = (LinearLayout) findViewById(R.id.stats);
299 this.mSessionEst = (TextView) findViewById(R.id.session_est);
300 this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
301 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
302 this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
303 this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
304 this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
305 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
306 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
307 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
308 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
309 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
310 this.mSaveButton = (Button) findViewById(R.id.save_button);
311 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
312 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
313 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
314 this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
315 this.mRegisterNew
316 .setOnCheckedChangeListener(new OnCheckedChangeListener() {
317
318 @Override
319 public void onCheckedChanged(CompoundButton buttonView,
320 boolean isChecked) {
321 if (isChecked) {
322 mPasswordConfirm.setVisibility(View.VISIBLE);
323 } else {
324 mPasswordConfirm.setVisibility(View.GONE);
325 }
326 updateSaveButton();
327 }
328 });
329 }
330
331 @Override
332 public boolean onCreateOptionsMenu(final Menu menu) {
333 super.onCreateOptionsMenu(menu);
334 getMenuInflater().inflate(R.menu.editaccount, menu);
335 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
336 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
337 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
338 if (mAccount == null) {
339 showQrCode.setVisible(false);
340 showBlocklist.setVisible(false);
341 showMoreInfo.setVisible(false);
342 } else if (mAccount.getStatus() != Account.State.ONLINE) {
343 showBlocklist.setVisible(false);
344 showMoreInfo.setVisible(false);
345 } else if (!mAccount.getXmppConnection().getFeatures().blocking()) {
346 showBlocklist.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 }
387 this.mCancelButton.setEnabled(false);
388 this.mCancelButton.setTextColor(getSecondaryTextColor());
389 }
390 this.mAccountJid.setAdapter(mKnownHostsAdapter);
391 updateSaveButton();
392 }
393
394 @Override
395 public boolean onOptionsItemSelected(final MenuItem item) {
396 switch (item.getItemId()) {
397 case R.id.action_show_block_list:
398 final Intent intent = new Intent(this, BlocklistActivity.class);
399 intent.putExtra("account", mAccount.getJid().toString());
400 startActivity(intent);
401 break;
402 case R.id.action_server_info_show_more:
403 mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
404 item.setChecked(!item.isChecked());
405 }
406 return super.onOptionsItemSelected(item);
407 }
408
409 private void updateAccountInformation() {
410 this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
411 this.mPassword.setText(this.mAccount.getPassword());
412 if (this.jidToEdit != null) {
413 this.mAvatar.setVisibility(View.VISIBLE);
414 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
415 }
416 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
417 this.mRegisterNew.setVisibility(View.VISIBLE);
418 this.mRegisterNew.setChecked(true);
419 this.mPasswordConfirm.setText(this.mAccount.getPassword());
420 } else {
421 this.mRegisterNew.setVisibility(View.GONE);
422 this.mRegisterNew.setChecked(false);
423 }
424 if (this.mAccount.getStatus() == Account.State.ONLINE
425 && !this.mFetchingAvatar) {
426 this.mStats.setVisibility(View.VISIBLE);
427 this.mSessionEst.setText(UIHelper.readableTimeDifference(
428 getApplicationContext(), this.mAccount.getXmppConnection()
429 .getLastSessionEstablished()));
430 Features features = this.mAccount.getXmppConnection().getFeatures();
431 if (features.rosterVersioning()) {
432 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
433 } else {
434 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
435 }
436 if (features.carbons()) {
437 this.mServerInfoCarbons.setText(R.string.server_info_available);
438 } else {
439 this.mServerInfoCarbons
440 .setText(R.string.server_info_unavailable);
441 }
442 if (features.mam()) {
443 this.mServerInfoMam.setText(R.string.server_info_available);
444 } else {
445 this.mServerInfoMam.setText(R.string.server_info_unavailable);
446 }
447 if (features.csi()) {
448 this.mServerInfoCSI.setText(R.string.server_info_available);
449 } else {
450 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
451 }
452 if (features.blocking()) {
453 this.mServerInfoBlocking.setText(R.string.server_info_available);
454 } else {
455 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
456 }
457 if (features.sm()) {
458 this.mServerInfoSm.setText(R.string.server_info_available);
459 } else {
460 this.mServerInfoSm.setText(R.string.server_info_unavailable);
461 }
462 if (features.pubsub()) {
463 this.mServerInfoPep.setText(R.string.server_info_available);
464 } else {
465 this.mServerInfoPep.setText(R.string.server_info_unavailable);
466 }
467 final String fingerprint = this.mAccount.getOtrFingerprint();
468 if (fingerprint != null) {
469 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
470 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
471 this.mOtrFingerprintToClipboardButton
472 .setVisibility(View.VISIBLE);
473 this.mOtrFingerprintToClipboardButton
474 .setOnClickListener(new View.OnClickListener() {
475
476 @Override
477 public void onClick(View v) {
478
479 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
480 Toast.makeText(
481 EditAccountActivity.this,
482 R.string.toast_message_otr_fingerprint,
483 Toast.LENGTH_SHORT).show();
484 }
485 }
486 });
487 } else {
488 this.mOtrFingerprintBox.setVisibility(View.GONE);
489 }
490 } else {
491 if (this.mAccount.errorStatus()) {
492 this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
493 this.mAccountJid.requestFocus();
494 }
495 this.mStats.setVisibility(View.GONE);
496 }
497 }
498}