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