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 implements OnAccountUpdate {
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 @Override
136 public void onAccountUpdate() {
137 runOnUiThread(new Runnable() {
138
139 @Override
140 public void run() {
141 if (mAccount != null
142 && mAccount.getStatus() != Account.STATUS_ONLINE
143 && mFetchingAvatar) {
144 startActivity(new Intent(getApplicationContext(),
145 ManageAccountActivity.class));
146 finish();
147 } else if (jidToEdit == null && mAccount != null
148 && mAccount.getStatus() == Account.STATUS_ONLINE) {
149 if (!mFetchingAvatar) {
150 mFetchingAvatar = true;
151 xmppConnectionService.checkForAvatar(mAccount,
152 mAvatarFetchCallback);
153 }
154 } else {
155 updateSaveButton();
156 }
157 if (mAccount != null) {
158 updateAccountInformation();
159 }
160 }
161 });
162 }
163 private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
164
165 @Override
166 public void userInputRequried(PendingIntent pi, Avatar avatar) {
167 finishInitialSetup(avatar);
168 }
169
170 @Override
171 public void success(Avatar avatar) {
172 finishInitialSetup(avatar);
173 }
174
175 @Override
176 public void error(int errorCode, Avatar avatar) {
177 finishInitialSetup(avatar);
178 }
179 };
180 private TextWatcher mTextWatcher = new TextWatcher() {
181
182 @Override
183 public void onTextChanged(CharSequence s, int start, int before,
184 int count) {
185 updateSaveButton();
186 }
187
188 @Override
189 public void beforeTextChanged(CharSequence s, int start, int count,
190 int after) {
191
192 }
193
194 @Override
195 public void afterTextChanged(Editable s) {
196
197 }
198 };
199 private OnClickListener mAvatarClickListener = new OnClickListener() {
200 @Override
201 public void onClick(View view) {
202 if (mAccount!=null) {
203 Intent intent = new Intent(getApplicationContext(),
204 PublishProfilePictureActivity.class);
205 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
206 startActivity(intent);
207 }
208 }
209 };
210
211 protected void finishInitialSetup(final Avatar avatar) {
212 runOnUiThread(new Runnable() {
213
214 @Override
215 public void run() {
216 Intent intent;
217 if (avatar != null) {
218 intent = new Intent(getApplicationContext(),
219 StartConversationActivity.class);
220 } else {
221 intent = new Intent(getApplicationContext(),
222 PublishProfilePictureActivity.class);
223 intent.putExtra("account", mAccount.getJid().toBareJid().toString());
224 intent.putExtra("setup", true);
225 }
226 startActivity(intent);
227 finish();
228 }
229 });
230 }
231
232 protected void updateSaveButton() {
233 if (mAccount != null
234 && mAccount.getStatus() == Account.STATUS_CONNECTING) {
235 this.mSaveButton.setEnabled(false);
236 this.mSaveButton.setTextColor(getSecondaryTextColor());
237 this.mSaveButton.setText(R.string.account_status_connecting);
238 } else if (mAccount != null
239 && mAccount.getStatus() == Account.STATUS_DISABLED) {
240 this.mSaveButton.setEnabled(true);
241 this.mSaveButton.setTextColor(getPrimaryTextColor());
242 this.mSaveButton.setText(R.string.enable);
243 } else {
244 this.mSaveButton.setEnabled(true);
245 this.mSaveButton.setTextColor(getPrimaryTextColor());
246 if (jidToEdit != null) {
247 if (mAccount != null
248 && mAccount.getStatus() == Account.STATUS_ONLINE) {
249 this.mSaveButton.setText(R.string.save);
250 if (!accountInfoEdited()) {
251 this.mSaveButton.setEnabled(false);
252 this.mSaveButton.setTextColor(getSecondaryTextColor());
253 }
254 } else {
255 this.mSaveButton.setText(R.string.connect);
256 }
257 } else {
258 this.mSaveButton.setText(R.string.next);
259 }
260 }
261 }
262
263 protected boolean accountInfoEdited() {
264 return (!this.mAccount.getJid().toBareJid().equals(
265 this.mAccountJid.getText().toString()))
266 || (!this.mAccount.getPassword().equals(
267 this.mPassword.getText().toString()));
268 }
269
270 @Override
271 protected String getShareableUri() {
272 if (mAccount!=null) {
273 return "xmpp:"+mAccount.getJid().toBareJid();
274 } else {
275 return "";
276 }
277 }
278
279 @Override
280 protected void onCreate(Bundle savedInstanceState) {
281 super.onCreate(savedInstanceState);
282 setContentView(R.layout.activity_edit_account);
283 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
284 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
285 this.mPassword = (EditText) findViewById(R.id.account_password);
286 this.mPassword.addTextChangedListener(this.mTextWatcher);
287 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
288 this.mAvatar = (ImageView) findViewById(R.id.avater);
289 this.mAvatar.setOnClickListener(this.mAvatarClickListener);
290 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
291 this.mStats = (LinearLayout) findViewById(R.id.stats);
292 this.mSessionEst = (TextView) findViewById(R.id.session_est);
293 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
294 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
295 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
296 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
297 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
298 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
299 this.mSaveButton = (Button) findViewById(R.id.save_button);
300 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
301 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
302 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
303 this.mRegisterNew
304 .setOnCheckedChangeListener(new OnCheckedChangeListener() {
305
306 @Override
307 public void onCheckedChanged(CompoundButton buttonView,
308 boolean isChecked) {
309 if (isChecked) {
310 mPasswordConfirm.setVisibility(View.VISIBLE);
311 } else {
312 mPasswordConfirm.setVisibility(View.GONE);
313 }
314 updateSaveButton();
315 }
316 });
317 }
318
319 @Override
320 public boolean onCreateOptionsMenu(Menu menu) {
321 super.onCreateOptionsMenu(menu);
322 getMenuInflater().inflate(R.menu.editaccount, menu);
323 MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
324 if (mAccount == null) {
325 showQrCode.setVisible(false);
326 }
327 return true;
328 }
329
330 @Override
331 protected void onStart() {
332 super.onStart();
333 if (getIntent() != null) {
334 try {
335 this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
336 } catch (final InvalidJidException | NullPointerException ignored) {
337 this.jidToEdit = null;
338 }
339 if (this.jidToEdit != null) {
340 this.mRegisterNew.setVisibility(View.GONE);
341 getActionBar().setTitle(getString(R.string.account_details));
342 } else {
343 this.mAvatar.setVisibility(View.GONE);
344 getActionBar().setTitle(R.string.action_add_account);
345 }
346 }
347 }
348
349 @Override
350 protected void onBackendConnected() {
351 KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
352 android.R.layout.simple_list_item_1,
353 xmppConnectionService.getKnownHosts());
354 if (this.jidToEdit != null) {
355 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
356 updateAccountInformation();
357 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
358 getActionBar().setDisplayHomeAsUpEnabled(false);
359 getActionBar().setDisplayShowHomeEnabled(false);
360 this.mCancelButton.setEnabled(false);
361 this.mCancelButton.setTextColor(getSecondaryTextColor());
362 }
363 this.mAccountJid.setAdapter(mKnownHostsAdapter);
364 updateSaveButton();
365 }
366
367 private void updateAccountInformation() {
368 this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
369 this.mPassword.setText(this.mAccount.getPassword());
370 if (this.jidToEdit != null) {
371 this.mAvatar.setVisibility(View.VISIBLE);
372 this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
373 }
374 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
375 this.mRegisterNew.setVisibility(View.VISIBLE);
376 this.mRegisterNew.setChecked(true);
377 this.mPasswordConfirm.setText(this.mAccount.getPassword());
378 } else {
379 this.mRegisterNew.setVisibility(View.GONE);
380 this.mRegisterNew.setChecked(false);
381 }
382 if (this.mAccount.getStatus() == Account.STATUS_ONLINE
383 && !this.mFetchingAvatar) {
384 this.mStats.setVisibility(View.VISIBLE);
385 this.mSessionEst.setText(UIHelper.readableTimeDifference(
386 getApplicationContext(), this.mAccount.getXmppConnection()
387 .getLastSessionEstablished()));
388 Features features = this.mAccount.getXmppConnection().getFeatures();
389 if (features.carbons()) {
390 this.mServerInfoCarbons.setText(R.string.server_info_available);
391 } else {
392 this.mServerInfoCarbons
393 .setText(R.string.server_info_unavailable);
394 }
395 if (features.sm()) {
396 this.mServerInfoSm.setText(R.string.server_info_available);
397 } else {
398 this.mServerInfoSm.setText(R.string.server_info_unavailable);
399 }
400 if (features.pubsub()) {
401 this.mServerInfoPep.setText(R.string.server_info_available);
402 } else {
403 this.mServerInfoPep.setText(R.string.server_info_unavailable);
404 }
405 final String fingerprint = this.mAccount
406 .getOtrFingerprint(xmppConnectionService);
407 if (fingerprint != null) {
408 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
409 this.mOtrFingerprint.setText(fingerprint);
410 this.mOtrFingerprintToClipboardButton
411 .setVisibility(View.VISIBLE);
412 this.mOtrFingerprintToClipboardButton
413 .setOnClickListener(new View.OnClickListener() {
414
415 @Override
416 public void onClick(View v) {
417
418 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
419 Toast.makeText(
420 EditAccountActivity.this,
421 R.string.toast_message_otr_fingerprint,
422 Toast.LENGTH_SHORT).show();
423 }
424 }
425 });
426 } else {
427 this.mOtrFingerprintBox.setVisibility(View.GONE);
428 }
429 } else {
430 if (this.mAccount.errorStatus()) {
431 this.mAccountJid.setError(getString(this.mAccount
432 .getReadableStatusId()));
433 this.mAccountJid.requestFocus();
434 }
435 this.mStats.setVisibility(View.GONE);
436 }
437 }
438}