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 && !accountInfoEdited()) {
 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 (accountInfoEdited() && jidToEdit != null) {
241			this.mSaveButton.setText(R.string.save);
242			this.mSaveButton.setEnabled(true);
243			this.mSaveButton.setTextColor(getPrimaryTextColor());
244		} else if (mAccount != null && (mAccount.getStatus() == Account.State.CONNECTING || mFetchingAvatar)) {
245			this.mSaveButton.setEnabled(false);
246			this.mSaveButton.setTextColor(getSecondaryTextColor());
247			this.mSaveButton.setText(R.string.account_status_connecting);
248		} else if (mAccount != null && mAccount.getStatus() == Account.State.DISABLED) {
249			this.mSaveButton.setEnabled(true);
250			this.mSaveButton.setTextColor(getPrimaryTextColor());
251			this.mSaveButton.setText(R.string.enable);
252		} else {
253			this.mSaveButton.setEnabled(true);
254			this.mSaveButton.setTextColor(getPrimaryTextColor());
255			if (jidToEdit != null) {
256				if (mAccount != null && mAccount.isOnlineAndConnected()) {
257					this.mSaveButton.setText(R.string.save);
258					if (!accountInfoEdited()) {
259						this.mSaveButton.setEnabled(false);
260						this.mSaveButton.setTextColor(getSecondaryTextColor());
261					}
262				} else {
263					this.mSaveButton.setText(R.string.connect);
264				}
265			} else {
266				this.mSaveButton.setText(R.string.next);
267			}
268		}
269	}
270
271	protected boolean accountInfoEdited() {
272		return this.mAccount != null && (!this.mAccount.getJid().toBareJid().toString().equals(
273					this.mAccountJid.getText().toString())
274			|| !this.mAccount.getPassword().equals(
275						this.mPassword.getText().toString()));
276	}
277
278	@Override
279	protected String getShareableUri() {
280		if (mAccount!=null) {
281			return mAccount.getShareableUri();
282		} else {
283			return "";
284		}
285	}
286
287	@Override
288	protected void onCreate(final Bundle savedInstanceState) {
289		super.onCreate(savedInstanceState);
290		setContentView(R.layout.activity_edit_account);
291		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
292		this.mAccountJid.addTextChangedListener(this.mTextWatcher);
293		this.mPassword = (EditText) findViewById(R.id.account_password);
294		this.mPassword.addTextChangedListener(this.mTextWatcher);
295		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
296		this.mAvatar = (ImageView) findViewById(R.id.avater);
297		this.mAvatar.setOnClickListener(this.mAvatarClickListener);
298		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
299		this.mStats = (LinearLayout) findViewById(R.id.stats);
300		this.mSessionEst = (TextView) findViewById(R.id.session_est);
301		this.mServerInfoRosterVersion = (TextView) findViewById(R.id.server_info_roster_version);
302		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
303		this.mServerInfoMam = (TextView) findViewById(R.id.server_info_mam);
304		this.mServerInfoCSI = (TextView) findViewById(R.id.server_info_csi);
305		this.mServerInfoBlocking = (TextView) findViewById(R.id.server_info_blocking);
306		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
307		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
308		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
309		this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
310		this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
311		this.mSaveButton = (Button) findViewById(R.id.save_button);
312		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
313		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
314		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
315		this.mMoreTable = (TableLayout) findViewById(R.id.server_info_more);
316		final OnCheckedChangeListener OnCheckedShowConfirmPassword = new OnCheckedChangeListener() {
317			@Override
318			public void onCheckedChanged(final CompoundButton buttonView,
319					final boolean isChecked) {
320				if (isChecked) {
321					mPasswordConfirm.setVisibility(View.VISIBLE);
322				} else {
323					mPasswordConfirm.setVisibility(View.GONE);
324				}
325				updateSaveButton();
326			}
327		};
328		this.mRegisterNew.setOnCheckedChangeListener(OnCheckedShowConfirmPassword);
329	}
330
331	@Override
332	public boolean onCreateOptionsMenu(final Menu menu) {
333		super.onCreateOptionsMenu(menu);
334		getMenuInflater().inflate(R.menu.editaccount, menu);
335		final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
336		final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
337		final MenuItem showMoreInfo = menu.findItem(R.id.action_server_info_show_more);
338		final MenuItem changePassword = menu.findItem(R.id.action_change_password_on_server);
339		if (mAccount != null && mAccount.isOnlineAndConnected()) {
340			if (!mAccount.getXmppConnection().getFeatures().blocking()) {
341				showBlocklist.setVisible(false);
342			}
343			if (!mAccount.getXmppConnection().getFeatures().register()) {
344				changePassword.setVisible(false);
345			}
346		} else {
347			showQrCode.setVisible(false);
348			showBlocklist.setVisible(false);
349			showMoreInfo.setVisible(false);
350			changePassword.setVisible(false);
351		}
352		return true;
353	}
354
355	@Override
356	protected void onStart() {
357		super.onStart();
358		if (getIntent() != null) {
359			try {
360				this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
361			} catch (final InvalidJidException | NullPointerException ignored) {
362				this.jidToEdit = null;
363			}
364			if (this.jidToEdit != null) {
365				this.mRegisterNew.setVisibility(View.GONE);
366				if (getActionBar() != null) {
367					getActionBar().setTitle(getString(R.string.account_details));
368				}
369			} else {
370				this.mAvatar.setVisibility(View.GONE);
371				if (getActionBar() != null) {
372					getActionBar().setTitle(R.string.action_add_account);
373				}
374			}
375		}
376	}
377
378	@Override
379	protected void onBackendConnected() {
380		final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
381				android.R.layout.simple_list_item_1,
382				xmppConnectionService.getKnownHosts());
383		if (this.jidToEdit != null) {
384			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
385			updateAccountInformation();
386		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
387			if (getActionBar() != null) {
388				getActionBar().setDisplayHomeAsUpEnabled(false);
389				getActionBar().setDisplayShowHomeEnabled(false);
390				getActionBar().setHomeButtonEnabled(false);
391			}
392			this.mCancelButton.setEnabled(false);
393			this.mCancelButton.setTextColor(getSecondaryTextColor());
394		}
395		this.mAccountJid.setAdapter(mKnownHostsAdapter);
396		updateSaveButton();
397	}
398
399	@Override
400	public boolean onOptionsItemSelected(final MenuItem item) {
401		switch (item.getItemId()) {
402			case R.id.action_show_block_list:
403				final Intent showBlocklistIntent = new Intent(this, BlocklistActivity.class);
404				showBlocklistIntent.putExtra("account", mAccount.getJid().toString());
405				startActivity(showBlocklistIntent);
406				break;
407			case R.id.action_server_info_show_more:
408				mMoreTable.setVisibility(item.isChecked() ? View.GONE : View.VISIBLE);
409				item.setChecked(!item.isChecked());
410				break;
411			case R.id.action_change_password_on_server:
412				final Intent changePasswordIntent = new Intent(this, ChangePasswordActivity.class);
413				changePasswordIntent.putExtra("account", mAccount.getJid().toString());
414				startActivity(changePasswordIntent);
415				break;
416		}
417		return super.onOptionsItemSelected(item);
418	}
419
420	private void updateAccountInformation() {
421		this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
422		this.mPassword.setText(this.mAccount.getPassword());
423		if (this.jidToEdit != null) {
424			this.mAvatar.setVisibility(View.VISIBLE);
425			this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
426		}
427		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
428			this.mRegisterNew.setVisibility(View.VISIBLE);
429			this.mRegisterNew.setChecked(true);
430			this.mPasswordConfirm.setText(this.mAccount.getPassword());
431		} else {
432			this.mRegisterNew.setVisibility(View.GONE);
433			this.mRegisterNew.setChecked(false);
434		}
435		if (this.mAccount.isOnlineAndConnected() && !this.mFetchingAvatar) {
436			this.mStats.setVisibility(View.VISIBLE);
437			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(this, this.mAccount.getXmppConnection()
438						.getLastSessionEstablished()));
439			Features features = this.mAccount.getXmppConnection().getFeatures();
440			if (features.rosterVersioning()) {
441				this.mServerInfoRosterVersion.setText(R.string.server_info_available);
442			} else {
443				this.mServerInfoRosterVersion.setText(R.string.server_info_unavailable);
444			}
445			if (features.carbons()) {
446				this.mServerInfoCarbons.setText(R.string.server_info_available);
447			} else {
448				this.mServerInfoCarbons
449					.setText(R.string.server_info_unavailable);
450			}
451			if (features.mam()) {
452				this.mServerInfoMam.setText(R.string.server_info_available);
453			} else {
454				this.mServerInfoMam.setText(R.string.server_info_unavailable);
455			}
456			if (features.csi()) {
457				this.mServerInfoCSI.setText(R.string.server_info_available);
458			} else {
459				this.mServerInfoCSI.setText(R.string.server_info_unavailable);
460			}
461			if (features.blocking()) {
462				this.mServerInfoBlocking.setText(R.string.server_info_available);
463			} else {
464				this.mServerInfoBlocking.setText(R.string.server_info_unavailable);
465			}
466			if (features.sm()) {
467				this.mServerInfoSm.setText(R.string.server_info_available);
468			} else {
469				this.mServerInfoSm.setText(R.string.server_info_unavailable);
470			}
471			if (features.pep()) {
472				this.mServerInfoPep.setText(R.string.server_info_available);
473			} else {
474				this.mServerInfoPep.setText(R.string.server_info_unavailable);
475			}
476			final String fingerprint = this.mAccount.getOtrFingerprint();
477			if (fingerprint != null) {
478				this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
479				this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
480				this.mOtrFingerprintToClipboardButton
481					.setVisibility(View.VISIBLE);
482				this.mOtrFingerprintToClipboardButton
483					.setOnClickListener(new View.OnClickListener() {
484
485						@Override
486						public void onClick(final View v) {
487
488							if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
489								Toast.makeText(
490										EditAccountActivity.this,
491										R.string.toast_message_otr_fingerprint,
492										Toast.LENGTH_SHORT).show();
493							}
494						}
495					});
496			} else {
497				this.mOtrFingerprintBox.setVisibility(View.GONE);
498			}
499		} else {
500			if (this.mAccount.errorStatus()) {
501				this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
502				this.mAccountJid.requestFocus();
503			} else {
504				this.mAccountJid.setError(null);
505			}
506			this.mStats.setVisibility(View.GONE);
507		}
508	}
509}