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