EditAccountActivity.java

  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;
 29import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 30import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
 31import eu.siacs.conversations.utils.CryptoHelper;
 32import eu.siacs.conversations.utils.UIHelper;
 33import eu.siacs.conversations.xmpp.XmppConnection.Features;
 34import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 35import eu.siacs.conversations.xmpp.jid.Jid;
 36import eu.siacs.conversations.xmpp.pep.Avatar;
 37
 38public class EditAccountActivity extends XmppActivity implements OnAccountUpdate, XmppConnectionService.OnAccountPasswordChanged {
 39
 40	private AutoCompleteTextView mAccountJid;
 41	private EditText mPassword;
 42	private EditText mPasswordConfirm;
 43	private CheckBox mRegisterNew;
 44	private CheckBox mChangePassword;
 45	private Button mCancelButton;
 46	private Button mSaveButton;
 47	private TableLayout mMoreTable;
 48
 49	private LinearLayout mStats;
 50	private TextView mServerInfoSm;
 51	private TextView mServerInfoRosterVersion;
 52	private TextView mServerInfoCarbons;
 53	private TextView mServerInfoMam;
 54	private TextView mServerInfoCSI;
 55	private TextView mServerInfoBlocking;
 56	private TextView mServerInfoPep;
 57	private TextView mSessionEst;
 58	private TextView mOtrFingerprint;
 59	private ImageView mAvatar;
 60	private RelativeLayout mOtrFingerprintBox;
 61	private ImageButton mOtrFingerprintToClipboardButton;
 62
 63	private Jid jidToEdit;
 64	private Account mAccount;
 65
 66	private boolean mFetchingAvatar = false;
 67	private boolean mChangingPassword = false;
 68
 69	private final OnClickListener mSaveButtonClickListener = new OnClickListener() {
 70
 71		@Override
 72		public void onClick(final View v) {
 73			if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
 74				mAccount.setOption(Account.OPTION_DISABLED, false);
 75				xmppConnectionService.updateAccount(mAccount);
 76				return;
 77			}
 78			final boolean registerNewAccount = mRegisterNew.isChecked();
 79			final boolean changePassword = mChangePassword.isChecked();
 80			final Jid jid;
 81			try {
 82				jid = Jid.fromString(mAccountJid.getText().toString());
 83			} catch (final InvalidJidException e) {
 84				mAccountJid.setError(getString(R.string.invalid_jid));
 85				mAccountJid.requestFocus();
 86				return;
 87			}
 88			if (jid.isDomainJid()) {
 89				mAccountJid.setError(getString(R.string.invalid_jid));
 90				mAccountJid.requestFocus();
 91				return;
 92			}
 93			final String password = mPassword.getText().toString();
 94			final String passwordConfirm = mPasswordConfirm.getText().toString();
 95			if (registerNewAccount || changePassword) {
 96				if (!password.equals(passwordConfirm)) {
 97					mPasswordConfirm
 98						.setError(getString(R.string.passwords_do_not_match));
 99					mPasswordConfirm.requestFocus();
100					return;
101				}
102			}
103			if (mAccount != null) {
104				try {
105					mAccount.setUsername(jid.hasLocalpart() ? jid.getLocalpart() : "");
106					mAccount.setServer(jid.getDomainpart());
107				} catch (final InvalidJidException ignored) {
108				}
109				if (changePassword) {
110					if (mAccount.isOnlineAndConnected()) {
111						xmppConnectionService.updateAccountPasswordOnServer(mAccount, mPassword.getText().toString(),EditAccountActivity.this);
112						mChangingPassword = true;
113						updateSaveButton();
114					} else {
115						Toast.makeText(EditAccountActivity.this,R.string.not_connected_try_again,Toast.LENGTH_SHORT).show();
116					}
117					return;
118				} else {
119					mAccount.setPassword(password);
120					mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
121					xmppConnectionService.updateAccount(mAccount);
122				}
123			} else {
124				try {
125					if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
126						mAccountJid.setError(getString(R.string.account_already_exists));
127						mAccountJid.requestFocus();
128						return;
129					}
130				} catch (final InvalidJidException e) {
131					return;
132				}
133				mAccount = new Account(jid.toBareJid(), password);
134				mAccount.setOption(Account.OPTION_USETLS, true);
135				mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
136				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
137				xmppConnectionService.createAccount(mAccount);
138			}
139			if (jidToEdit != null) {
140				finish();
141			} else {
142				updateSaveButton();
143				updateAccountInformation();
144			}
145
146		}
147	};
148	private final OnClickListener mCancelButtonClickListener = new OnClickListener() {
149
150		@Override
151		public void onClick(final View v) {
152			finish();
153		}
154	};
155	@Override
156	public void onAccountUpdate() {
157		runOnUiThread(new Runnable() {
158
159			@Override
160			public void run() {
161				if (mAccount != null
162						&& mAccount.getStatus() != Account.State.ONLINE
163						&& mFetchingAvatar) {
164					startActivity(new Intent(getApplicationContext(),
165								ManageAccountActivity.class));
166					finish();
167				} else if (jidToEdit == null && mAccount != null
168						&& mAccount.getStatus() == Account.State.ONLINE) {
169					if (!mFetchingAvatar) {
170						mFetchingAvatar = true;
171						xmppConnectionService.checkForAvatar(mAccount,
172								mAvatarFetchCallback);
173					}
174				} else {
175					updateSaveButton();
176				}
177				if (mAccount != null) {
178					updateAccountInformation();
179				}
180			}
181		});
182	}
183	private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
184
185		@Override
186		public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
187			finishInitialSetup(avatar);
188		}
189
190		@Override
191		public void success(final Avatar avatar) {
192			finishInitialSetup(avatar);
193		}
194
195		@Override
196		public void error(final int errorCode, final Avatar avatar) {
197			finishInitialSetup(avatar);
198		}
199	};
200	private final TextWatcher mTextWatcher = new TextWatcher() {
201
202		@Override
203		public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
204			updateSaveButton();
205		}
206
207		@Override
208		public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
209		}
210
211		@Override
212		public void afterTextChanged(final Editable s) {
213			final boolean registrationReady = mAccount != null &&
214				mAccount.isOnlineAndConnected() &&
215				mAccount.getXmppConnection().getFeatures().register();
216			if (jidToEdit != null && mAccount != null && registrationReady &&
217					!mAccount.getPassword().equals(s.toString()) && !"".equals(s.toString())) {
218				mChangePassword.setVisibility(View.VISIBLE);
219			} else {
220				mChangePassword.setVisibility(View.INVISIBLE);
221				mChangePassword.setChecked(false);
222			}
223		}
224	};
225	private final OnClickListener mAvatarClickListener = new OnClickListener() {
226		@Override
227		public void onClick(final View view) {
228			if (mAccount != null) {
229				final Intent intent = new Intent(getApplicationContext(),
230						PublishProfilePictureActivity.class);
231				intent.putExtra("account", mAccount.getJid().toBareJid().toString());
232				startActivity(intent);
233			}
234		}
235	};
236
237	protected void finishInitialSetup(final Avatar avatar) {
238		runOnUiThread(new Runnable() {
239
240			@Override
241			public void run() {
242				final Intent intent;
243				if (avatar != null) {
244					intent = new Intent(getApplicationContext(),
245							StartConversationActivity.class);
246				} else {
247					intent = new Intent(getApplicationContext(),
248							PublishProfilePictureActivity.class);
249					intent.putExtra("account", mAccount.getJid().toBareJid().toString());
250					intent.putExtra("setup", true);
251				}
252				startActivity(intent);
253				finish();
254			}
255		});
256	}
257
258	protected void updateSaveButton() {
259		if (mChangingPassword) {
260			this.mSaveButton.setEnabled(false);
261			this.mSaveButton.setTextColor(getSecondaryTextColor());
262			this.mSaveButton.setText(R.string.updating);
263		} else if (mAccount != null && mAccount.getStatus() == Account.State.CONNECTING) {
264			this.mSaveButton.setEnabled(false);
265			this.mSaveButton.setTextColor(getSecondaryTextColor());
266			this.mSaveButton.setText(R.string.account_status_connecting);
267		} else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
268			this.mSaveButton.setEnabled(true);
269			this.mSaveButton.setTextColor(getPrimaryTextColor());
270			this.mSaveButton.setText(R.string.enable);
271		} else {
272			this.mSaveButton.setEnabled(true);
273			this.mSaveButton.setTextColor(getPrimaryTextColor());
274			if (jidToEdit != null) {
275				if (mAccount != null && mAccount.isOnlineAndConnected()) {
276					this.mSaveButton.setText(R.string.save);
277					if (!accountInfoEdited()) {
278						this.mSaveButton.setEnabled(false);
279						this.mSaveButton.setTextColor(getSecondaryTextColor());
280					}
281				} else {
282					this.mSaveButton.setText(R.string.connect);
283				}
284			} else {
285				this.mSaveButton.setText(R.string.next);
286			}
287		}
288	}
289
290	protected boolean accountInfoEdited() {
291		return (!this.mAccount.getJid().toBareJid().toString().equals(
292					this.mAccountJid.getText().toString()))
293			|| (!this.mAccount.getPassword().equals(
294						this.mPassword.getText().toString()));
295	}
296
297	@Override
298	protected String getShareableUri() {
299		if (mAccount!=null) {
300			return mAccount.getShareableUri();
301		} else {
302			return "";
303		}
304	}
305
306	@Override
307	protected void onCreate(final Bundle savedInstanceState) {
308		super.onCreate(savedInstanceState);
309		setContentView(R.layout.activity_edit_account);
310		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
311		this.mAccountJid.addTextChangedListener(this.mTextWatcher);
312		this.mPassword = (EditText) findViewById(R.id.account_password);
313		this.mPassword.addTextChangedListener(this.mTextWatcher);
314		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
315		this.mAvatar = (ImageView) findViewById(R.id.avater);
316		this.mAvatar.setOnClickListener(this.mAvatarClickListener);
317		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
318		this.mChangePassword = (CheckBox) findViewById(R.id.account_change_password);
319		this.mStats = (LinearLayout) findViewById(R.id.stats);
320		this.mSessionEst = (TextView) findViewById(R.id.session_est);
321		this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
322		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
323		this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
324		this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
325		this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
326		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
327		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
328		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
329		this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
330		this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
331		this.mSaveButton = (Button) findViewById(R.id.save_button);
332		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
333		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
334		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
335		this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
336		final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
337			@Override
338			public void onCheckedChanged(final CompoundButton buttonView,
339					final boolean isChecked) {
340				if (isChecked) {
341					mPasswordConfirm.setVisibility(View.VISIBLE);
342				} else {
343					mPasswordConfirm.setVisibility(View.GONE);
344				}
345				updateSaveButton();
346			}
347		};
348		this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
349		this.mChangePassword.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
350	}
351
352	@Override
353	public boolean onCreateOptionsMenu(final Menu menu) {
354		super.onCreateOptionsMenu(menu);
355		getMenuInflater().inflate(R.menu.editaccount, menu);
356		final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
357		final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
358		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
359		if (mAccount == null) {
360			showQrCode.setVisible(false);
361			showBlocklist.setVisible(false);
362			showMoreInfo.setVisible(false);
363		} else if (mAccount.getStatus() != Account.State.ONLINE) {
364			showBlocklist.setVisible(false);
365			showMoreInfo.setVisible(false);
366		} else if (!mAccount.getXmppConnection().getFeatures().blocking()) {
367			showBlocklist.setVisible(false);
368		}
369		return true;
370	}
371
372	@Override
373	protected void onStart() {
374		super.onStart();
375		if (getIntent() != null) {
376			try {
377				this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
378			} catch (final InvalidJidException | NullPointerException ignored) {
379				this.jidToEdit = null;
380			}
381			if (this.jidToEdit != null) {
382				this.mRegisterNew.setVisibility(View.GONE);
383				if (getActionBar() != null) {
384					getActionBar().setTitle(getString(R.string.account_details));
385				}
386			} else {
387				this.mAvatar.setVisibility(View.GONE);
388				if (getActionBar() != null) {
389					getActionBar().setTitle(R.string.action_add_account);
390				}
391			}
392			this.mChangePassword.setVisibility(View.GONE);
393			this.mChangePassword.setChecked(false);
394		}
395	}
396
397	@Override
398	protected void onBackendConnected() {
399		final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
400				android.R.layout.simple_list_item_1,
401				xmppConnectionService.getKnownHosts());
402		if (this.jidToEdit != null) {
403			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
404			updateAccountInformation();
405		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
406			if (getActionBar() != null) {
407				getActionBar().setDisplayHomeAsUpEnabled(false);
408				getActionBar().setDisplayShowHomeEnabled(false);
409			}
410			this.mCancelButton.setEnabled(false);
411			this.mCancelButton.setTextColor(getSecondaryTextColor());
412		}
413		this.mAccountJid.setAdapter(mKnownHostsAdapter);
414		updateSaveButton();
415	}
416
417	@Override
418	public boolean onOptionsItemSelected(final MenuItem item) {
419		switch (item.getItemId()) {
420			case R.id.action_show_block_list:
421				final Intent intent = new Intent(this, BlocklistActivity.class);
422				intent.putExtra("account", mAccount.getJid().toString());
423				startActivity(intent);
424				break;
425			case R.id.action_server_info_show_more:
426				mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
427				item.setChecked(!item.isChecked());
428		}
429		return super.onOptionsItemSelected(item);
430	}
431
432	private void updateAccountInformation() {
433		this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
434		this.mPassword.setText(this.mAccount.getPassword());
435		if (this.jidToEdit != null) {
436			this.mAvatar.setVisibility(View.VISIBLE);
437			this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
438		}
439		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
440			this.mRegisterNew.setVisibility(View.VISIBLE);
441			this.mChangePassword.setVisibility(View.GONE);
442			this.mChangePassword.setChecked(false);
443			this.mRegisterNew.setChecked(true);
444			this.mPasswordConfirm.setText(this.mAccount.getPassword());
445		} else {
446			this.mRegisterNew.setVisibility(View.GONE);
447			this.mRegisterNew.setChecked(false);
448			this.mChangePassword.setVisibility(View.GONE);
449			this.mChangePassword.setChecked(false);
450		}
451		if (this.mAccount.getStatus() == Account.State.ONLINE
452				&& !this.mFetchingAvatar) {
453			this.mStats.setVisibility(View.VISIBLE);
454			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
455						.getLastSessionEstablished()));
456			Features features = this.mAccount.getXmppConnection().getFeatures();
457			if (features.rosterVersioning()) {
458				this.mServerInfoRosterVersion.setText(R.string.server_info_available);
459			} else {
460				this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
461			}
462			if (features.carbons()) {
463				this.mServerInfoCarbons.setText(R.string.server_info_available);
464			} else {
465				this.mServerInfoCarbons
466					.setText(R.string.server_info_unavailable);
467			}
468			if (features.mam()) {
469				this.mServerInfoMam.setText(R.string.server_info_available);
470			} else {
471				this.mServerInfoMam.setText(R.string.server_info_unavailable);
472			}
473			if (features.csi()) {
474				this.mServerInfoCSI.setText(R.string.server_info_available);
475			} else {
476				this.mServerInfoCSI.setText(R.string.server_info_unavailable);
477			}
478			if (features.blocking()) {
479				this.mServerInfoBlocking.setText(R.string.server_info_available);
480			} else {
481				this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
482			}
483			if (features.sm()) {
484				this.mServerInfoSm.setText(R.string.server_info_available);
485			} else {
486				this.mServerInfoSm.setText(R.string.server_info_unavailable);
487			}
488			if (features.pubsub()) {
489				this.mServerInfoPep.setText(R.string.server_info_available);
490			} else {
491				this.mServerInfoPep.setText(R.string.server_info_unavailable);
492			}
493			final String fingerprint = this.mAccount.getOtrFingerprint();
494			if (fingerprint != null) {
495				this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
496				this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
497				this.mOtrFingerprintToClipboardButton
498					.setVisibility(View.VISIBLE);
499				this.mOtrFingerprintToClipboardButton
500					.setOnClickListener(new View.OnClickListener() {
501
502						@Override
503						public void onClick(final View v) {
504
505							if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
506								Toast.makeText(
507										EditAccountActivity.this,
508										R.string.toast_message_otr_fingerprint,
509										Toast.LENGTH_SHORT).show();
510							}
511						}
512					});
513			} else {
514				this.mOtrFingerprintBox.setVisibility(View.GONE);
515			}
516		} else {
517			if (this.mAccount.errorStatus()) {
518				this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
519				this.mAccountJid.requestFocus();
520			}
521			this.mStats.setVisibility(View.GONE);
522		}
523	}
524
525	@Override
526	public void onPasswordChangeSucceeded() {
527		this.mChangingPassword = false;
528		runOnUiThread(new Runnable() {
529			@Override
530			public void run() {
531				Toast.makeText(EditAccountActivity.this,R.string.password_changed,Toast.LENGTH_SHORT).show();
532				updateSaveButton();
533				updateAccountInformation();
534			}
535		});
536	}
537
538	@Override
539	public void onPasswordChangeFailed() {
540		this.mChangingPassword = false;
541		runOnUiThread(new Runnable() {
542			@Override
543			public void run() {
544				mPassword.requestFocus();
545				mPassword.setError(getString(R.string.could_not_change_password));
546				updateSaveButton();
547			}
548		});
549	}
550}