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