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 || !mAccount.getXmppConnection().getFeatures().blocking()) {
343 showBlocklist.setVisible(false);
344 showMoreInfo.setVisible(false);
345 }
346 return true;
347 }
348
349 @Override
350 protected void onStart() {
351 super.onStart();
352 if (getIntent() != null) {
353 try {
354 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
355 } catch (final InvalidJidException | NullPointerException ignored) {
356 this.jidToEdit = null;
357 }
358 if (this.jidToEdit != null) {
359 this.mRegisterNew.setVisibility(View.GONE);
360 if (getActionBar() != null) {
361 getActionBar().setTitle(getString(R.string.account_details));
362 }
363 } else {
364 this.mAvatar.setVisibility(View.GONE);
365 if (getActionBar() != null) {
366 getActionBar().setTitle(R.string.action_add_account);
367 }
368 }
369 }
370 }
371
372 @Override
373 protected void onBackendConnected() {
374 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
375 android.R.layout.simple_list_item_1,
376 xmppConnectionService.getKnownHosts());
377 if (this.jidToEdit != null) {
378 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
379 updateAccountInformation();
380 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
381 if (getActionBar() != null) {
382 getActionBar().setDisplayHomeAsUpEnabled(false);
383 getActionBar().setDisplayShowHomeEnabled(false);
384 }
385 this.mCancelButton.setEnabled(false);
386 this.mCancelButton.setTextColor(getSecondaryTextColor());
387 }
388 this.mAccountJid.setAdapter(mKnownHostsAdapter);
389 updateSaveButton();
390 }
391
392 @Override
393 public boolean onOptionsItemSelected(final MenuItem item) {
394 switch (item.getItemId()) {
395 case R.id.action_show_block_list:
396 final Intent intent = new Intent(this, BlocklistActivity.class);
397 intent.putExtra("account", mAccount.getJid().toString());
398 startActivity(intent);
399 break;
400 case R.id.action_server_info_show_more:
401 mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
402 item.setChecked(!item.isChecked());
403 }
404 return super.onOptionsItemSelected(item);
405 }
406
407 private void updateAccountInformation() {
408 this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
409 this.mPassword.setText(this.mAccount.getPassword());
410 if (this.jidToEdit != null) {
411 this.mAvatar.setVisibility(View.VISIBLE);
412 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
413 }
414 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
415 this.mRegisterNew.setVisibility(View.VISIBLE);
416 this.mRegisterNew.setChecked(true);
417 this.mPasswordConfirm.setText(this.mAccount.getPassword());
418 } else {
419 this.mRegisterNew.setVisibility(View.GONE);
420 this.mRegisterNew.setChecked(false);
421 }
422 if (this.mAccount.getStatus() == Account.State.ONLINE
423 && !this.mFetchingAvatar) {
424 this.mStats.setVisibility(View.VISIBLE);
425 this.mSessionEst.setText(UIHelper.readableTimeDifference(
426 getApplicationContext(), this.mAccount.getXmppConnection()
427 .getLastSessionEstablished()));
428 Features features = this.mAccount.getXmppConnection().getFeatures();
429 if (features.rosterVersioning()) {
430 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
431 } else {
432 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
433 }
434 if (features.carbons()) {
435 this.mServerInfoCarbons.setText(R.string.server_info_available);
436 } else {
437 this.mServerInfoCarbons
438 .setText(R.string.server_info_unavailable);
439 }
440 if (features.mam()) {
441 this.mServerInfoMam.setText(R.string.server_info_available);
442 } else {
443 this.mServerInfoMam.setText(R.string.server_info_unavailable);
444 }
445 if (features.csi()) {
446 this.mServerInfoCSI.setText(R.string.server_info_available);
447 } else {
448 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
449 }
450 if (features.blocking()) {
451 this.mServerInfoBlocking.setText(R.string.server_info_available);
452 } else {
453 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
454 }
455 if (features.sm()) {
456 this.mServerInfoSm.setText(R.string.server_info_available);
457 } else {
458 this.mServerInfoSm.setText(R.string.server_info_unavailable);
459 }
460 if (features.pubsub()) {
461 this.mServerInfoPep.setText(R.string.server_info_available);
462 } else {
463 this.mServerInfoPep.setText(R.string.server_info_unavailable);
464 }
465 final String fingerprint = this.mAccount.getOtrFingerprint();
466 if (fingerprint != null) {
467 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
468 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
469 this.mOtrFingerprintToClipboardButton
470 .setVisibility(View.VISIBLE);
471 this.mOtrFingerprintToClipboardButton
472 .setOnClickListener(new View.OnClickListener() {
473
474 @Override
475 public void onClick(View v) {
476
477 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
478 Toast.makeText(
479 EditAccountActivity.this,
480 R.string.toast_message_otr_fingerprint,
481 Toast.LENGTH_SHORT).show();
482 }
483 }
484 });
485 } else {
486 this.mOtrFingerprintBox.setVisibility(View.GONE);
487 }
488 } else {
489 if (this.mAccount.errorStatus()) {
490 this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
491 this.mAccountJid.requestFocus();
492 }
493 this.mStats.setVisibility(View.GONE);
494 }
495 }
496}