1package eu.siacs.conversations.ui;
2
3import android.app.AlertDialog;
4import android.app.PendingIntent;
5import android.content.Intent;
6import android.graphics.Bitmap;
7import android.graphics.Color;
8import android.graphics.Point;
9import android.os.Bundle;
10import android.text.Editable;
11import android.text.TextWatcher;
12import android.util.Log;
13import android.view.Menu;
14import android.view.MenuItem;
15import android.view.View;
16import android.view.View.OnClickListener;
17import android.widget.AutoCompleteTextView;
18import android.widget.Button;
19import android.widget.CheckBox;
20import android.widget.CompoundButton;
21import android.widget.CompoundButton.OnCheckedChangeListener;
22import android.widget.EditText;
23import android.widget.ImageButton;
24import android.widget.ImageView;
25import android.widget.LinearLayout;
26import android.widget.RelativeLayout;
27import android.widget.TextView;
28import android.widget.Toast;
29
30import com.google.zxing.BarcodeFormat;
31import com.google.zxing.EncodeHintType;
32import com.google.zxing.WriterException;
33import com.google.zxing.common.BitMatrix;
34import com.google.zxing.integration.android.IntentIntegrator;
35import com.google.zxing.integration.android.IntentResult;
36import com.google.zxing.qrcode.QRCodeWriter;
37import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
38
39import java.util.Hashtable;
40
41import eu.siacs.conversations.Config;
42import eu.siacs.conversations.R;
43import eu.siacs.conversations.entities.Account;
44import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
45import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
46import eu.siacs.conversations.utils.UIHelper;
47import eu.siacs.conversations.utils.Validator;
48import eu.siacs.conversations.xmpp.XmppConnection.Features;
49import eu.siacs.conversations.xmpp.pep.Avatar;
50
51public class EditAccountActivity extends XmppActivity {
52
53 private AutoCompleteTextView mAccountJid;
54 private EditText mPassword;
55 private EditText mPasswordConfirm;
56 private CheckBox mRegisterNew;
57 private Button mCancelButton;
58 private Button mSaveButton;
59
60 private LinearLayout mStats;
61 private TextView mServerInfoSm;
62 private TextView mServerInfoCarbons;
63 private TextView mServerInfoPep;
64 private TextView mSessionEst;
65 private TextView mOtrFingerprint;
66 private RelativeLayout mOtrFingerprintBox;
67 private ImageButton mOtrFingerprintToClipboardButton;
68
69 private String jidToEdit;
70 private Account mAccount;
71
72 private boolean mFetchingAvatar = false;
73
74 private OnClickListener mSaveButtonClickListener = new OnClickListener() {
75
76 @Override
77 public void onClick(View v) {
78 if (mAccount != null
79 && mAccount.getStatus() == Account.STATUS_DISABLED) {
80 mAccount.setOption(Account.OPTION_DISABLED, false);
81 xmppConnectionService.updateAccount(mAccount);
82 return;
83 }
84 if (!Validator.isValidJid(mAccountJid.getText().toString())) {
85 mAccountJid.setError(getString(R.string.invalid_jid));
86 mAccountJid.requestFocus();
87 return;
88 }
89 boolean registerNewAccount = mRegisterNew.isChecked();
90 String[] jidParts = mAccountJid.getText().toString().split("@");
91 String username = jidParts[0];
92 String server;
93 if (jidParts.length >= 2) {
94 server = jidParts[1];
95 } else {
96 server = "";
97 }
98 String password = mPassword.getText().toString();
99 String passwordConfirm = mPasswordConfirm.getText().toString();
100 if (registerNewAccount) {
101 if (!password.equals(passwordConfirm)) {
102 mPasswordConfirm
103 .setError(getString(R.string.passwords_do_not_match));
104 mPasswordConfirm.requestFocus();
105 return;
106 }
107 }
108 if (mAccount != null) {
109 mAccount.setPassword(password);
110 mAccount.setUsername(username);
111 mAccount.setServer(server);
112 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
113 xmppConnectionService.updateAccount(mAccount);
114 } else {
115 if (xmppConnectionService.findAccountByJid(mAccountJid
116 .getText().toString()) != null) {
117 mAccountJid
118 .setError(getString(R.string.account_already_exists));
119 mAccountJid.requestFocus();
120 return;
121 }
122 mAccount = new Account(username, server, password);
123 mAccount.setOption(Account.OPTION_USETLS, true);
124 mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
125 mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
126 xmppConnectionService.createAccount(mAccount);
127 }
128 if (jidToEdit != null) {
129 finish();
130 } else {
131 updateSaveButton();
132 updateAccountInformation();
133 }
134
135 }
136 };
137 private OnClickListener mCancelButtonClickListener = new OnClickListener() {
138
139 @Override
140 public void onClick(View v) {
141 finish();
142 }
143 };
144 private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
145
146 @Override
147 public void onAccountUpdate() {
148 runOnUiThread(new Runnable() {
149
150 @Override
151 public void run() {
152 if (mAccount != null
153 && mAccount.getStatus() != Account.STATUS_ONLINE
154 && mFetchingAvatar) {
155 startActivity(new Intent(getApplicationContext(),
156 ManageAccountActivity.class));
157 finish();
158 } else if (jidToEdit == null && mAccount != null
159 && mAccount.getStatus() == Account.STATUS_ONLINE) {
160 if (!mFetchingAvatar) {
161 mFetchingAvatar = true;
162 xmppConnectionService.checkForAvatar(mAccount,
163 mAvatarFetchCallback);
164 }
165 } else {
166 updateSaveButton();
167 }
168 if (mAccount != null) {
169 updateAccountInformation();
170 }
171 }
172 });
173 }
174 };
175 private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
176
177 @Override
178 public void userInputRequried(PendingIntent pi, Avatar avatar) {
179 finishInitialSetup(avatar);
180 }
181
182 @Override
183 public void success(Avatar avatar) {
184 finishInitialSetup(avatar);
185 }
186
187 @Override
188 public void error(int errorCode, Avatar avatar) {
189 finishInitialSetup(avatar);
190 }
191 };
192 private KnownHostsAdapter mKnownHostsAdapter;
193 private TextWatcher mTextWatcher = new TextWatcher() {
194
195 @Override
196 public void onTextChanged(CharSequence s, int start, int before,
197 int count) {
198 updateSaveButton();
199 }
200
201 @Override
202 public void beforeTextChanged(CharSequence s, int start, int count,
203 int after) {
204
205 }
206
207 @Override
208 public void afterTextChanged(Editable s) {
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());
226 intent.putExtra("setup", true);
227 }
228 startActivity(intent);
229 finish();
230 }
231 });
232 }
233
234 protected boolean inputDataDiffersFromAccount() {
235 if (mAccount == null) {
236 return true;
237 } else {
238 return (!mAccount.getJid().equals(mAccountJid.getText().toString()))
239 || (!mAccount.getPassword().equals(
240 mPassword.getText().toString()) || mAccount
241 .isOptionSet(Account.OPTION_REGISTER) != mRegisterNew
242 .isChecked());
243 }
244 }
245
246 protected void showQrCode() {
247 Point size = new Point();
248 getWindowManager().getDefaultDisplay().getSize(size);
249 final int width = (size.x < size.y ? size.x : size.y);
250 String jid = mAccount.getJid();
251 Bitmap bitmap = createQrCodeBitmap("xmpp:" + jid, width);
252 ImageView view = new ImageView(this);
253 view.setImageBitmap(bitmap);
254 AlertDialog.Builder builder = new AlertDialog.Builder(this);
255 builder.setView(view);
256 builder.create().show();
257 }
258
259 protected Bitmap createQrCodeBitmap(String input, int size) {
260 try {
261 final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
262 final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
263 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
264 final BitMatrix result = QR_CODE_WRITER.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
265 final int width = result.getWidth();
266 final int height = result.getHeight();
267 final int[] pixels = new int[width * height];
268 for (int y = 0; y < height; y++) {
269 final int offset = y * width;
270 for (int x = 0; x < width; x++) {
271 pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
272 }
273 }
274 final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
275 bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
276 return bitmap;
277 } catch (final WriterException e) {
278 return null;
279 }
280 }
281
282 protected void updateSaveButton() {
283 if (mAccount != null
284 && mAccount.getStatus() == Account.STATUS_CONNECTING) {
285 this.mSaveButton.setEnabled(false);
286 this.mSaveButton.setTextColor(getSecondaryTextColor());
287 this.mSaveButton.setText(R.string.account_status_connecting);
288 } else if (mAccount != null
289 && mAccount.getStatus() == Account.STATUS_DISABLED) {
290 this.mSaveButton.setEnabled(true);
291 this.mSaveButton.setTextColor(getPrimaryTextColor());
292 this.mSaveButton.setText(R.string.enable);
293 } else {
294 this.mSaveButton.setEnabled(true);
295 this.mSaveButton.setTextColor(getPrimaryTextColor());
296 if (jidToEdit != null) {
297 if (mAccount != null
298 && mAccount.getStatus() == Account.STATUS_ONLINE) {
299 this.mSaveButton.setText(R.string.save);
300 if (!accountInfoEdited()) {
301 this.mSaveButton.setEnabled(false);
302 this.mSaveButton.setTextColor(getSecondaryTextColor());
303 }
304 } else {
305 this.mSaveButton.setText(R.string.connect);
306 }
307 } else {
308 this.mSaveButton.setText(R.string.next);
309 }
310 }
311 }
312
313 protected boolean accountInfoEdited() {
314 return (!this.mAccount.getJid().equals(
315 this.mAccountJid.getText().toString()))
316 || (!this.mAccount.getPassword().equals(
317 this.mPassword.getText().toString()));
318 }
319
320 @Override
321 protected void onCreate(Bundle savedInstanceState) {
322 super.onCreate(savedInstanceState);
323 setContentView(R.layout.activity_edit_account);
324 this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
325 this.mAccountJid.addTextChangedListener(this.mTextWatcher);
326 this.mPassword = (EditText) findViewById(R.id.account_password);
327 this.mPassword.addTextChangedListener(this.mTextWatcher);
328 this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
329 this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
330 this.mStats = (LinearLayout) findViewById(R.id.stats);
331 this.mSessionEst = (TextView) findViewById(R.id.session_est);
332 this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
333 this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
334 this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
335 this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
336 this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
337 this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
338 this.mSaveButton = (Button) findViewById(R.id.save_button);
339 this.mCancelButton = (Button) findViewById(R.id.cancel_button);
340 this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
341 this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
342 this.mRegisterNew
343 .setOnCheckedChangeListener(new OnCheckedChangeListener() {
344
345 @Override
346 public void onCheckedChanged(CompoundButton buttonView,
347 boolean isChecked) {
348 if (isChecked) {
349 mPasswordConfirm.setVisibility(View.VISIBLE);
350 } else {
351 mPasswordConfirm.setVisibility(View.GONE);
352 }
353 updateSaveButton();
354 }
355 });
356 }
357
358 @Override
359 public boolean onCreateOptionsMenu(Menu menu) {
360 super.onCreateOptionsMenu(menu);
361 getMenuInflater().inflate(R.menu.editaccount, menu);
362 MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
363 if (mAccount == null) {
364 showQrCode.setVisible(false);
365 }
366 return true;
367 }
368
369 @Override
370 public boolean onOptionsItemSelected(MenuItem item) {
371 switch (item.getItemId()) {
372 case R.id.action_show_qr_code:
373 showQrCode();
374 return true;
375 }
376 return super.onOptionsItemSelected(item);
377 }
378
379 @Override
380 protected void onStart() {
381 super.onStart();
382 if (getIntent() != null) {
383 this.jidToEdit = getIntent().getStringExtra("jid");
384 if (this.jidToEdit != null) {
385 this.mRegisterNew.setVisibility(View.GONE);
386 getActionBar().setTitle(jidToEdit);
387 } else {
388 getActionBar().setTitle(R.string.action_add_account);
389 }
390 }
391 }
392
393 @Override
394 protected void onStop() {
395 if (xmppConnectionServiceBound) {
396 xmppConnectionService.removeOnAccountListChangedListener();
397 }
398 super.onStop();
399 }
400
401 @Override
402 protected void onBackendConnected() {
403 this.mKnownHostsAdapter = new KnownHostsAdapter(this,
404 android.R.layout.simple_list_item_1,
405 xmppConnectionService.getKnownHosts());
406 this.xmppConnectionService
407 .setOnAccountListChangedListener(this.mOnAccountUpdateListener);
408 if (this.jidToEdit != null) {
409 this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
410 updateAccountInformation();
411 } else if (this.xmppConnectionService.getAccounts().size() == 0) {
412 getActionBar().setDisplayHomeAsUpEnabled(false);
413 getActionBar().setDisplayShowHomeEnabled(false);
414 this.mCancelButton.setEnabled(false);
415 this.mCancelButton.setTextColor(getSecondaryTextColor());
416 }
417 this.mAccountJid.setAdapter(this.mKnownHostsAdapter);
418 updateSaveButton();
419 }
420
421 private void updateAccountInformation() {
422 this.mAccountJid.setText(this.mAccount.getJid());
423 this.mPassword.setText(this.mAccount.getPassword());
424 if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
425 this.mRegisterNew.setVisibility(View.VISIBLE);
426 this.mRegisterNew.setChecked(true);
427 this.mPasswordConfirm.setText(this.mAccount.getPassword());
428 } else {
429 this.mRegisterNew.setVisibility(View.GONE);
430 this.mRegisterNew.setChecked(false);
431 }
432 if (this.mAccount.getStatus() == Account.STATUS_ONLINE
433 && !this.mFetchingAvatar) {
434 this.mStats.setVisibility(View.VISIBLE);
435 this.mSessionEst.setText(UIHelper.readableTimeDifference(
436 getApplicationContext(), this.mAccount.getXmppConnection()
437 .getLastSessionEstablished()));
438 Features features = this.mAccount.getXmppConnection().getFeatures();
439 if (features.carbons()) {
440 this.mServerInfoCarbons.setText(R.string.server_info_available);
441 } else {
442 this.mServerInfoCarbons
443 .setText(R.string.server_info_unavailable);
444 }
445 if (features.sm()) {
446 this.mServerInfoSm.setText(R.string.server_info_available);
447 } else {
448 this.mServerInfoSm.setText(R.string.server_info_unavailable);
449 }
450 if (features.pubsub()) {
451 this.mServerInfoPep.setText(R.string.server_info_available);
452 } else {
453 this.mServerInfoPep.setText(R.string.server_info_unavailable);
454 }
455 final String fingerprint = this.mAccount
456 .getOtrFingerprint(xmppConnectionService);
457 if (fingerprint != null) {
458 this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
459 this.mOtrFingerprint.setText(fingerprint);
460 this.mOtrFingerprintToClipboardButton
461 .setVisibility(View.VISIBLE);
462 this.mOtrFingerprintToClipboardButton
463 .setOnClickListener(new View.OnClickListener() {
464
465 @Override
466 public void onClick(View v) {
467
468 if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
469 Toast.makeText(
470 EditAccountActivity.this,
471 R.string.toast_message_otr_fingerprint,
472 Toast.LENGTH_SHORT).show();
473 }
474 }
475 });
476 } else {
477 this.mOtrFingerprintBox.setVisibility(View.GONE);
478 }
479 } else {
480 if (this.mAccount.errorStatus()) {
481 this.mAccountJid.setError(getString(this.mAccount
482 .getReadableStatusId()));
483 this.mAccountJid.requestFocus();
484 }
485 this.mStats.setVisibility(View.GONE);
486 }
487 }
488}