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.TextView;
 23import android.widget.Toast;
 24
 25import eu.siacs.conversations.R;
 26import eu.siacs.conversations.entities.Account;
 27import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 28import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
 29import eu.siacs.conversations.utils.CryptoHelper;
 30import eu.siacs.conversations.utils.UIHelper;
 31import eu.siacs.conversations.utils.Validator;
 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
 46	private LinearLayout mStats;
 47	private TextView mServerInfoSm;
 48	private TextView mServerInfoCarbons;
 49	private TextView mServerInfoPep;
 50	private TextView mSessionEst;
 51	private TextView mOtrFingerprint;
 52	private ImageView mAvatar;
 53	private RelativeLayout mOtrFingerprintBox;
 54	private ImageButton mOtrFingerprintToClipboardButton;
 55
 56	private Jid jidToEdit;
 57	private Account mAccount;
 58
 59	private boolean mFetchingAvatar = false;
 60
 61	private OnClickListener mSaveButtonClickListener = new OnClickListener() {
 62
 63		@Override
 64		public void onClick(View v) {
 65			if (mAccount != null
 66					&& mAccount.getStatus() == Account.State.DISABLED) {
 67				mAccount.setOption(Account.OPTION_DISABLED, false);
 68				xmppConnectionService.updateAccount(mAccount);
 69				return;
 70					}
 71			if (!Validator.isValidJid(mAccountJid.getText().toString())) {
 72				mAccountJid.setError(getString(R.string.invalid_jid));
 73				mAccountJid.requestFocus();
 74				return;
 75			}
 76			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			String password = mPassword.getText().toString();
 86			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.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
296		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
297		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
298		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
299		this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
300		this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
301		this.mSaveButton = (Button) findViewById(R.id.save_button);
302		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
303		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
304		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
305		this.mRegisterNew
306			.setOnCheckedChangeListener(new OnCheckedChangeListener() {
307
308				@Override
309				public void onCheckedChanged(CompoundButton buttonView,
310						boolean isChecked) {
311					if (isChecked) {
312						mPasswordConfirm.setVisibility(View.VISIBLE);
313					} else {
314						mPasswordConfirm.setVisibility(View.GONE);
315					}
316					updateSaveButton();
317				}
318			});
319	}
320
321	@Override
322	public boolean onCreateOptionsMenu(final Menu menu) {
323		super.onCreateOptionsMenu(menu);
324		getMenuInflater().inflate(R.menu.editaccount, menu);
325		final MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
326		final MenuItem showBlocklist = menu.findItem(R.id.action_show_block_list);
327		if (mAccount == null) {
328			showQrCode.setVisible(false);
329			showBlocklist.setVisible(false);
330		} else if (mAccount.getStatus() != Account.State.ONLINE || !mAccount.getXmppConnection().getFeatures().blocking()) {
331			showBlocklist.setVisible(false);
332		}
333		return true;
334	}
335
336	@Override
337	protected void onStart() {
338		super.onStart();
339		if (getIntent() != null) {
340			try {
341				this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
342			} catch (final InvalidJidException | NullPointerException ignored) {
343				this.jidToEdit = null;
344			}
345			if (this.jidToEdit != null) {
346				this.mRegisterNew.setVisibility(View.GONE);
347				if (getActionBar() != null) {
348					getActionBar().setTitle(getString(R.string.account_details));
349				}
350			} else {
351				this.mAvatar.setVisibility(View.GONE);
352				if (getActionBar() != null) {
353					getActionBar().setTitle(R.string.action_add_account);
354				}
355			}
356		}
357	}
358
359	@Override
360	protected void onBackendConnected() {
361		final KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
362				android.R.layout.simple_list_item_1,
363				xmppConnectionService.getKnownHosts());
364		if (this.jidToEdit != null) {
365			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
366			updateAccountInformation();
367		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
368			if (getActionBar() != null) {
369				getActionBar().setDisplayHomeAsUpEnabled(false);
370				getActionBar().setDisplayShowHomeEnabled(false);
371			}
372			this.mCancelButton.setEnabled(false);
373			this.mCancelButton.setTextColor(getSecondaryTextColor());
374		}
375		this.mAccountJid.setAdapter(mKnownHostsAdapter);
376		updateSaveButton();
377	}
378
379	@Override
380	public boolean onOptionsItemSelected(final MenuItem item) {
381		switch (item.getItemId()) {
382			case R.id.action_show_block_list:
383				final Intent intent = new Intent(this, BlocklistActivity.class);
384				intent.putExtra("account", mAccount.getJid().toString());
385				startActivity(intent);
386				break;
387		}
388		return super.onOptionsItemSelected(item);
389	}
390
391	private void updateAccountInformation() {
392		this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
393		this.mPassword.setText(this.mAccount.getPassword());
394		if (this.jidToEdit != null) {
395			this.mAvatar.setVisibility(View.VISIBLE);
396			this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
397		}
398		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
399			this.mRegisterNew.setVisibility(View.VISIBLE);
400			this.mRegisterNew.setChecked(true);
401			this.mPasswordConfirm.setText(this.mAccount.getPassword());
402		} else {
403			this.mRegisterNew.setVisibility(View.GONE);
404			this.mRegisterNew.setChecked(false);
405		}
406		if (this.mAccount.getStatus() == Account.State.ONLINE
407				&& !this.mFetchingAvatar) {
408			this.mStats.setVisibility(View.VISIBLE);
409			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(
410						getApplicationContext(), this.mAccount.getXmppConnection()
411						.getLastSessionEstablished()));
412			final Features features = this.mAccount.getXmppConnection().getFeatures();
413			if (features.carbons()) {
414				this.mServerInfoCarbons.setText(R.string.server_info_available);
415			} else {
416				this.mServerInfoCarbons
417					.setText(R.string.server_info_unavailable);
418			}
419			if (features.sm()) {
420				this.mServerInfoSm.setText(R.string.server_info_available);
421			} else {
422				this.mServerInfoSm.setText(R.string.server_info_unavailable);
423			}
424			if (features.pubsub()) {
425				this.mServerInfoPep.setText(R.string.server_info_available);
426			} else {
427				this.mServerInfoPep.setText(R.string.server_info_unavailable);
428			}
429			final String fingerprint = this.mAccount.getOtrFingerprint();
430			if (fingerprint != null) {
431				this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
432				this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
433				this.mOtrFingerprintToClipboardButton
434					.setVisibility(View.VISIBLE);
435				this.mOtrFingerprintToClipboardButton
436					.setOnClickListener(new View.OnClickListener() {
437
438						@Override
439						public void onClick(View v) {
440
441							if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
442								Toast.makeText(
443										EditAccountActivity.this,
444										R.string.toast_message_otr_fingerprint,
445										Toast.LENGTH_SHORT).show();
446							}
447						}
448					});
449			} else {
450				this.mOtrFingerprintBox.setVisibility(View.GONE);
451			}
452		} else {
453			if (this.mAccount.errorStatus()) {
454				this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
455				this.mAccountJid.requestFocus();
456			}
457			this.mStats.setVisibility(View.GONE);
458		}
459	}
460}