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 final String password = mPassword.getText().toString();
86 final String passwordConfirm = mPasswordConfirm.getText().toString();
87 if (registerNewAccount) {
88 if (!password.equals(passwordConfirm)) {
89 mPasswordConfirm
90 .setError(getString(R.string.passwords_do_not_match));
91 mPasswordConfirm.requestFocus();
92 return;
93 }
94 }
95 if (mAccount != null) {
96 mAccount.setPassword(password);
97 try {
98 mAccount.setUsername(jid.hasLocalpart() ? jid.getLocalpart() : "");
99 mAccount.setServer(jid.getDomainpart());
100 } catch (final InvalidJidException ignored) {
101 }
102 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
103 xmppConnectionService.updateAccount(mAccount);
104 } else {
105 try {
106 if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
107 mAccountJid
108 .setError(getString(R.string.account_already_exists));
109 mAccountJid.requestFocus();
110 return;
111 }
112 } catch (InvalidJidException e) {
113 return;
114 }
115 mAccount = new Account(jid.toBareJid(), password);
116 mAccount.setOption(Account.OPTION_USETLS, true);
117 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
118 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
119 xmppConnectionService.createAccount(mAccount);
120 }
121 if (jidToEdit != null) {
122 finish();
123 } else {
124 updateSaveButton();
125 updateAccountInformation();
126 }
127
128 }
129 };
130 private OnClickListener mCancelButtonClickListener = new OnClickListener() {
131
132 @Override
133 public void onClick(View v) {
134 finish();
135 }
136 };
137 @Override
138 public void onAccountUpdate() {
139 runOnUiThread(new Runnable() {
140
141 @Override
142 public void run() {
143 if (mAccount != null
144 && mAccount.getStatus() != Account.State.ONLINE
145 && mFetchingAvatar) {
146 startActivity(new Intent(getApplicationContext(),
147 ManageAccountActivity.class));
148 finish();
149 } else if (jidToEdit == null && mAccount != null
150 && mAccount.getStatus() == Account.State.ONLINE) {
151 if (!mFetchingAvatar) {
152 mFetchingAvatar = true;
153 xmppConnectionService.checkForAvatar(mAccount,
154 mAvatarFetchCallback);
155 }
156 } else {
157 updateSaveButton();
158 }
159 if (mAccount != null) {
160 updateAccountInformation();
161 }
162 }
163 });
164 }
165 private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
166
167 @Override
168 public void userInputRequried(PendingIntent pi, Avatar avatar) {
169 finishInitialSetup(avatar);
170 }
171
172 @Override
173 public void success(Avatar avatar) {
174 finishInitialSetup(avatar);
175 }
176
177 @Override
178 public void error(int errorCode, Avatar avatar) {
179 finishInitialSetup(avatar);
180 }
181 };
182 private TextWatcher mTextWatcher = new TextWatcher() {
183
184 @Override
185 public void onTextChanged(CharSequence s, int start, int before,
186 int count) {
187 updateSaveButton();
188 }
189
190 @Override
191 public void beforeTextChanged(CharSequence s, int start, int count,
192 int after) {
193
194 }
195
196 @Override
197 public void afterTextChanged(Editable s) {
198
199 }
200 };
201 private OnClickListener mAvatarClickListener = new OnClickListener() {
202 @Override
203 public void onClick(View view) {
204 if (mAccount!=null) {
205 Intent intent = new Intent(getApplicationContext(),
206 PublishProfilePictureActivity.class);
207 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
208 startActivity(intent);
209 }
210 }
211 };
212
213 protected void finishInitialSetup(final Avatar avatar) {
214 runOnUiThread(new Runnable() {
215
216 @Override
217 public void run() {
218 Intent intent;
219 if (avatar != null) {
220 intent = new Intent(getApplicationContext(),
221 StartConversationActivity.class);
222 } else {
223 intent = new Intent(getApplicationContext(),
224 PublishProfilePictureActivity.class);
225 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
226 intent.putExtra("setup", true);
227 }
228 startActivity(intent);
229 finish();
230 }
231 });
232 }
233
234 protected void updateSaveButton() {
235 if (mAccount != null
236 && mAccount.getStatus() == Account.State.CONNECTING) {
237 this.mSaveButton.setEnabled(false);
238 this.mSaveButton.setTextColor(getSecondaryTextColor());
239 this.mSaveButton.setText(R.string.account_status_connecting);
240 } else if (mAccount != null
241 && mAccount.getStatus() == Account.State.DISABLED) {
242 this.mSaveButton.setEnabled(true);
243 this.mSaveButton.setTextColor(getPrimaryTextColor());
244 this.mSaveButton.setText(R.string.enable);
245 } else {
246 this.mSaveButton.setEnabled(true);
247 this.mSaveButton.setTextColor(getPrimaryTextColor());
248 if (jidToEdit != null) {
249 if (mAccount != null
250 && mAccount.getStatus() == Account.State.ONLINE) {
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().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(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 this.mRegisterNew
311 .setOnCheckedChangeListener(new OnCheckedChangeListener() {
312
313 @Override
314 public void onCheckedChanged(CompoundButton buttonView,
315 boolean isChecked) {
316 if (isChecked) {
317 mPasswordConfirm.setVisibility(View.VISIBLE);
318 } else {
319 mPasswordConfirm.setVisibility(View.GONE);
320 }
321 updateSaveButton();
322 }
323 });
324 }
325
326 @Override
327 public boolean onCreateOptionsMenu(final Menu menu) {
328 super.onCreateOptionsMenu(menu);
329 getMenuInflater().inflate(R.menu.editaccount, menu);
330 final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
331 final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
332 final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
333 if (mAccount == null) {
334 showQrCode.setVisible(false);
335 showBlocklist.setVisible(false);
336 showMoreInfo.setVisible(false);
337 } else if (mAccount.getStatus() != Account.State.ONLINE || !mAccount.getXmppConnection().getFeatures().blocking()) {
338 showBlocklist.setVisible(false);
339 showMoreInfo.setVisible(false);
340 }
341 return true;
342 }
343
344 @Override
345 protected void onStart() {
346 super.onStart();
347 if (getIntent() != null) {
348 try {
349 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
350 } catch (final InvalidJidException | NullPointerException ignored) {
351 this.jidToEdit = null;
352 }
353 if (this.jidToEdit != null) {
354 this.mRegisterNew.setVisibility(View.GONE);
355 if (getActionBar() != null) {
356 getActionBar().setTitle(getString(R.string.account_details));
357 }
358 } else {
359 this.mAvatar.setVisibility(View.GONE);
360 if (getActionBar() != null) {
361 getActionBar().setTitle(R.string.action_add_account);
362 }
363 }
364 }
365 }
366
367 @Override
368 protected void onBackendConnected() {
369 final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
370 android.R.layout.simple_list_item_1,
371 xmppConnectionService.getKnownHosts());
372 if (this.jidToEdit != null) {
373 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
374 updateAccountInformation();
375 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
376 if (getActionBar() != null) {
377 getActionBar().setDisplayHomeAsUpEnabled(false);
378 getActionBar().setDisplayShowHomeEnabled(false);
379 }
380 this.mCancelButton.setEnabled(false);
381 this.mCancelButton.setTextColor(getSecondaryTextColor());
382 }
383 this.mAccountJid.setAdapter(mKnownHostsAdapter);
384 updateSaveButton();
385 }
386
387 @Override
388 public boolean onOptionsItemSelected(final MenuItem item) {
389 switch (item.getItemId()) {
390 case R.id.action_show_block_list:
391 final Intent intent = new Intent(this, BlocklistActivity.class);
392 intent.putExtra("account", mAccount.getJid().toString());
393 startActivity(intent);
394 break;
395 case R.id.action_server_info_show_more:
396 mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
397 item.setChecked(!item.isChecked());
398 }
399 return super.onOptionsItemSelected(item);
400 }
401
402 private void updateAccountInformation() {
403 this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
404 this.mPassword.setText(this.mAccount.getPassword());
405 if (this.jidToEdit != null) {
406 this.mAvatar.setVisibility(View.VISIBLE);
407 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
408 }
409 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
410 this.mRegisterNew.setVisibility(View.VISIBLE);
411 this.mRegisterNew.setChecked(true);
412 this.mPasswordConfirm.setText(this.mAccount.getPassword());
413 } else {
414 this.mRegisterNew.setVisibility(View.GONE);
415 this.mRegisterNew.setChecked(false);
416 }
417 if (this.mAccount.getStatus() == Account.State.ONLINE
418 && !this.mFetchingAvatar) {
419 this.mStats.setVisibility(View.VISIBLE);
420 this.mSessionEst.setText(UIHelper.readableTimeDifference(
421 getApplicationContext(), this.mAccount.getXmppConnection()
422 .getLastSessionEstablished()));
423 Features features = this.mAccount.getXmppConnection().getFeatures();
424 if (features.rosterVersioning()) {
425 this.mServerInfoRosterVersion.setText(R.string.server_info_available);
426 } else {
427 this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
428 }
429 if (features.carbons()) {
430 this.mServerInfoCarbons.setText(R.string.server_info_available);
431 } else {
432 this.mServerInfoCarbons
433 .setText(R.string.server_info_unavailable);
434 }
435 if (features.mam()) {
436 this.mServerInfoMam.setText(R.string.server_info_available);
437 } else {
438 this.mServerInfoMam.setText(R.string.server_info_unavailable);
439 }
440 if (features.csi()) {
441 this.mServerInfoCSI.setText(R.string.server_info_available);
442 } else {
443 this.mServerInfoCSI.setText(R.string.server_info_unavailable);
444 }
445 if (features.blocking()) {
446 this.mServerInfoBlocking.setText(R.string.server_info_available);
447 } else {
448 this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
449 }
450 if (features.sm()) {
451 this.mServerInfoSm.setText(R.string.server_info_available);
452 } else {
453 this.mServerInfoSm.setText(R.string.server_info_unavailable);
454 }
455 if (features.pubsub()) {
456 this.mServerInfoPep.setText(R.string.server_info_available);
457 } else {
458 this.mServerInfoPep.setText(R.string.server_info_unavailable);
459 }
460 final String fingerprint = this.mAccount.getOtrFingerprint();
461 if (fingerprint != null) {
462 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
463 this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
464 this.mOtrFingerprintToClipboardButton
465 .setVisibility(View.VISIBLE);
466 this.mOtrFingerprintToClipboardButton
467 .setOnClickListener(new View.OnClickListener() {
468
469 @Override
470 public void onClick(View v) {
471
472 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
473 Toast.makeText(
474 EditAccountActivity.this,
475 R.string.toast_message_otr_fingerprint,
476 Toast.LENGTH_SHORT).show();
477 }
478 }
479 });
480 } else {
481 this.mOtrFingerprintBox.setVisibility(View.GONE);
482 }
483 } else {
484 if (this.mAccount.errorStatus()) {
485 this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
486 this.mAccountJid.requestFocus();
487 }
488 this.mStats.setVisibility(View.GONE);
489 }
490 }
491}