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 mAccount.setPassword(password);
106 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
107 xmppConnectionService.updateAccount(mAccount);
108 } else {
109 try {
110 if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
111 mAccountJid.setError(getString(R.string.account_already_exists));
112 mAccountJid.requestFocus();
113 return;
114 }
115 } catch (final InvalidJidException e) {
116 return;
117 }
118 mAccount = new Account(jid.toBareJid(), password);
119 mAccount.setOption(Account.OPTION_USETLS, true);
120 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
121 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
122 xmppConnectionService.createAccount(mAccount);
123 }
124 if (jidToEdit != null) {
125 finish();
126 } else {
127 updateSaveButton();
128 updateAccountInformation();
129 }
130
131 }
132 };
133 private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
134
135 @Override
136 public void onClick(final View v) {
137 finish();
138 }
139 };
140 @Override
141 public void onAccountUpdate() {
142 runOnUiThread(new Runnable() {
143
144 @Override
145 public void run() {
146 invalidateOptionsMenu();
147 if (mAccount != null
148 && mAccount.getStatus() != Account.State.ONLINE
149 && mFetchingAvatar) {
150 startActivity(new Intent(getApplicationContext(),
151 ManageAccountActivity.class));
152 finish();
153 } else if (jidToEdit == null && mAccount != null
154 && mAccount.getStatus() == Account.State.ONLINE) {
155 if (!mFetchingAvatar) {
156 mFetchingAvatar = true;
157 xmppConnectionService.checkForAvatar(mAccount,
158 mAvatarFetchCallback);
159 }
160 } else {
161 updateSaveButton();
162 }
163 if (mAccount != null) {
164 updateAccountInformation();
165 }
166 }
167 });
168 }
169 private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
170
171 @Override
172 public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
173 finishInitialSetup(avatar);
174 }
175
176 @Override
177 public void success(final Avatar avatar) {
178 finishInitialSetup(avatar);
179 }
180
181 @Override
182 public void error(final int errorCode, final Avatar avatar) {
183 finishInitialSetup(avatar);
184 }
185 };
186 private final TextWatcher mTextWatcher = new TextWatcher() {
187
188 @Override
189 public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
190 updateSaveButton();
191 }
192
193 @Override
194 public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
195 }
196
197 @Override
198 public void afterTextChanged(final Editable s) {
199
200 }
201 };
202
203 private final OnClickListener mAvatarClickListener = new OnClickListener() {
204 @Override
205 public void onClick(final View view) {
206 if (mAccount != null) {
207 final Intent intent = new Intent(getApplicationContext(),
208 PublishProfilePictureActivity.class);
209 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
210 startActivity(intent);
211 }
212 }
213 };
214
215 protected void finishInitialSetup(final Avatar avatar) {
216 runOnUiThread(new Runnable() {
217
218 @Override
219 public void run() {
220 final Intent intent;
221 if (avatar != null) {
222 intent = new Intent(getApplicationContext(),
223 StartConversationActivity.class);
224 intent.putExtra("init",true);
225 } else {
226 intent = new Intent(getApplicationContext(),
227 PublishProfilePictureActivity.class);
228 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
229 intent.putExtra("setup", true);
230 }
231 startActivity(intent);
232 finish();
233 }
234 });
235 }
236
237 protected void updateSaveButton() {
238 if (mAccount != null && (mAccount.getStatus() == Account.State.CONNECTING || mFetchingAvatar)) {
239 this.mSaveButton.setEnabled(false);
240 this.mSaveButton.setTextColor(getSecondaryTextColor());
241 this.mSaveButton.setText(R.string.account_status_connecting);
242 } else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
243 this.mSaveButton.setEnabled(true);
244 this.mSaveButton.setTextColor(getPrimaryTextColor());
245 this.mSaveButton.setText(R.string.enable);
246 } else {
247 this.mSaveButton.setEnabled(true);
248 this.mSaveButton.setTextColor(getPrimaryTextColor());
249 if (jidToEdit != null) {
250 if (mAccount != null && mAccount.isOnlineAndConnected()) {
251 this.mSaveButton.setText(R.string.save);
252 if (!accountInfoEdited()) {
253 this.mSaveButton.setEnabled(false);
254 this.mSaveButton.setTextColor(getSecondaryTextColor());
255 }
256 } else {
257 this.mSaveButton.setText(R.string.connect);
258 }
259 } else {
260 this.mSaveButton.setText(R.string.next);
261 }
262 }
263 }
264
265 protected boolean accountInfoEdited() {
266 return (!this.mAccount.getJid().toBareJid().toString().equals(
267 this.mAccountJid.getText().toString()))
268 || (!this.mAccount.getPassword().equals(
269 this.mPassword.getText().toString()));
270 }
271
272 @Override
273 protected String getShareableUri() {
274 if (mAccount!=null) {
275 return mAccount.getShareableUri();
276 } else {
277 return "";
278 }
279 }
280
281 @Override
282 protected void onCreate(final Bundle savedInstanceState) {
283 super.onCreate(savedInstanceState);
284 setContentView(R.layout.activity_edit_account);
285 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
286 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
287 this.mPassword = (EditText) findViewById(R.id.account_password);
288 this.mPassword.addTextChangedListener(this.mTextWatcher);
289 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
290 this.mAvatar = (ImageView) findViewById(R.id.avater);
291 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
292 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
293 this.mStats = (LinearLayout) findViewById(R.id.stats);
294 this.mSessionEst = (TextView) findViewById(R.id.session_est);
295 this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
296 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
297 this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
298 this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
299 this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
300 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
301 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
302 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
303 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
304 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
305 this.mSaveButton = (Button) findViewById(R.id.save_button);
306 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
307 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
308 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
309 this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
310 final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
311 @Override
312 public void onCheckedChanged(final CompoundButton buttonView,
313 final boolean isChecked) {
314 if (isChecked) {
315 mPasswordConfirm.setVisibility(View.VISIBLE);
316 } else {
317 mPasswordConfirm.setVisibility(View.GONE);
318 }
319 updateSaveButton();
320 }
321 };
322 this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
323 }
324
325 @Override
326 public boolean onCreateOptionsMenu(final Menu menu) {
327 super.onCreateOptionsMenu(menu);
328 getMenuInflater().inflate(R.menu.editaccount, menu);
329 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
330 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
331 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
332 final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
333 if (mAccount == null) {
334 showQrCode.setVisible(false);
335 showBlocklist.setVisible(false);
336 showMoreInfo.setVisible(false);
337 changePassword.setVisible(false);
338 } else if (mAccount.getStatus() != Account.State.ONLINE) {
339 showBlocklist.setVisible(false);
340 showMoreInfo.setVisible(false);
341 changePassword.setVisible(false);
342 } else if (!mAccount.getXmppConnection().getFeatures().blocking()) {
343 showBlocklist.setVisible(false);
344 }
345 return true;
346 }
347
348 @Override
349 protected void onStart() {
350 super.onStart();
351 if (getIntent() != null) {
352 try {
353 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
354 } catch (final InvalidJidException | NullPointerException ignored) {
355 this.jidToEdit = null;
356 }
357 if (this.jidToEdit != null) {
358 this.mRegisterNew.setVisibility(View.GONE);
359 if (getActionBar() != null) {
360 getActionBar().setTitle(getString(R.string.account_details));
361 }
362 } else {
363 this.mAvatar.setVisibility(View.GONE);
364 if (getActionBar() != null) {
365 getActionBar().setTitle(R.string.action_add_account);
366 }
367 }
368 }
369 }
370
371 @Override
372 protected void onBackendConnected() {
373 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
374 android.R.layout.simple_list_item_1,
375 xmppConnectionService.getKnownHosts());
376 if (this.jidToEdit != null) {
377 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
378 updateAccountInformation();
379 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
380 if (getActionBar() != null) {
381 getActionBar().setDisplayHomeAsUpEnabled(false);
382 getActionBar().setDisplayShowHomeEnabled(false);
383 getActionBar().setHomeButtonEnabled(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 showBlocklistIntent = new Intent(this, BlocklistActivity.class);
397 showBlocklistIntent.putExtra("account", mAccount.getJid().toString());
398 startActivity(showBlocklistIntent);
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 break;
404 case R.id.action_change_password_on_server:
405 final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
406 changePasswordIntent.putExtra("account", mAccount.getJid().toString());
407 startActivity(changePasswordIntent);
408 break;
409 }
410 return super.onOptionsItemSelected(item);
411 }
412
413 private void updateAccountInformation() {
414 this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
415 this.mPassword.setText(this.mAccount.getPassword());
416 if (this.jidToEdit != null) {
417 this.mAvatar.setVisibility(View.VISIBLE);
418 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
419 }
420 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
421 this.mRegisterNew.setVisibility(View.VISIBLE);
422 this.mRegisterNew.setChecked(true);
423 this.mPasswordConfirm.setText(this.mAccount.getPassword());
424 } else {
425 this.mRegisterNew.setVisibility(View.GONE);
426 this.mRegisterNew.setChecked(false);
427 }
428 if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
429 this.mStats.setVisibility(View.VISIBLE);
430 this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
431 .getLastSessionEstablished()));
432 Features features = this.mAccount.getXmppConnection().getFeatures();
433 if (features.rosterVersioning()) {
434 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
435 } else {
436 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
437 }
438 if (features.carbons()) {
439 this.mServerInfoCarbons.setText(R.string.server_info_available);
440 } else {
441 this.mServerInfoCarbons
442 .setText(R.string.server_info_unavailable);
443 }
444 if (features.mam()) {
445 this.mServerInfoMam.setText(R.string.server_info_available);
446 } else {
447 this.mServerInfoMam.setText(R.string.server_info_unavailable);
448 }
449 if (features.csi()) {
450 this.mServerInfoCSI.setText(R.string.server_info_available);
451 } else {
452 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
453 }
454 if (features.blocking()) {
455 this.mServerInfoBlocking.setText(R.string.server_info_available);
456 } else {
457 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
458 }
459 if (features.sm()) {
460 this.mServerInfoSm.setText(R.string.server_info_available);
461 } else {
462 this.mServerInfoSm.setText(R.string.server_info_unavailable);
463 }
464 if (features.pubsub()) {
465 this.mServerInfoPep.setText(R.string.server_info_available);
466 } else {
467 this.mServerInfoPep.setText(R.string.server_info_unavailable);
468 }
469 final String fingerprint = this.mAccount.getOtrFingerprint();
470 if (fingerprint != null) {
471 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
472 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
473 this.mOtrFingerprintToClipboardButton
474 .setVisibility(View.VISIBLE);
475 this.mOtrFingerprintToClipboardButton
476 .setOnClickListener(new View.OnClickListener() {
477
478 @Override
479 public void onClick(final View v) {
480
481 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
482 Toast.makeText(
483 EditAccountActivity.this,
484 R.string.toast_message_otr_fingerprint,
485 Toast.LENGTH_SHORT).show();
486 }
487 }
488 });
489 } else {
490 this.mOtrFingerprintBox.setVisibility(View.GONE);
491 }
492 } else {
493 if (this.mAccount.errorStatus()) {
494 this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
495 this.mAccountJid.requestFocus();
496 }
497 this.mStats.setVisibility(View.GONE);
498 }
499 }
500}