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