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