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