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                // TODO: Handle this error?
 82                return;
 83            }
 84            String password = mPassword.getText().toString();
 85			String passwordConfirm = mPasswordConfirm.getText().toString();
 86			if (registerNewAccount) {
 87				if (!password.equals(passwordConfirm)) {
 88					mPasswordConfirm
 89							.setError(getString(R.string.passwords_do_not_match));
 90					mPasswordConfirm.requestFocus();
 91					return;
 92				}
 93			}
 94			if (mAccount != null) {
 95				mAccount.setPassword(password);
 96                try {
 97                    mAccount.setUsername(jid.hasLocalpart() ? jid.getLocalpart() : "");
 98                    mAccount.setServer(jid.getDomainpart());
 99                } catch (final InvalidJidException ignored) {
100                }
101				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
102				xmppConnectionService.updateAccount(mAccount);
103			} else {
104                try {
105                    if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
106                        mAccountJid
107                                .setError(getString(R.string.account_already_exists));
108                        mAccountJid.requestFocus();
109                        return;
110                    }
111                } catch (InvalidJidException e) {
112                    return;
113                }
114                mAccount = new Account(jid.toBareJid(), password);
115				mAccount.setOption(Account.OPTION_USETLS, true);
116				mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
117				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
118				xmppConnectionService.createAccount(mAccount);
119			}
120			if (jidToEdit != null) {
121				finish();
122			} else {
123				updateSaveButton();
124				updateAccountInformation();
125			}
126
127		}
128	};
129	private OnClickListener mCancelButtonClickListener = new OnClickListener() {
130
131		@Override
132		public void onClick(View v) {
133			finish();
134		}
135	};
136		@Override
137		public void onAccountUpdate() {
138			runOnUiThread(new Runnable() {
139
140				@Override
141				public void run() {
142					if (mAccount != null
143							&& mAccount.getStatus() != Account.State.ONLINE
144							&& mFetchingAvatar) {
145						startActivity(new Intent(getApplicationContext(),
146								ManageAccountActivity.class));
147						finish();
148					} else if (jidToEdit == null && mAccount != null
149							&& mAccount.getStatus() == Account.State.ONLINE) {
150						if (!mFetchingAvatar) {
151							mFetchingAvatar = true;
152							xmppConnectionService.checkForAvatar(mAccount,
153									mAvatarFetchCallback);
154						}
155					} else {
156						updateSaveButton();
157					}
158					if (mAccount != null) {
159						updateAccountInformation();
160					}
161				}
162			});
163		}
164	private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
165
166		@Override
167		public void userInputRequried(PendingIntent pi, Avatar avatar) {
168			finishInitialSetup(avatar);
169		}
170
171		@Override
172		public void success(Avatar avatar) {
173			finishInitialSetup(avatar);
174		}
175
176		@Override
177		public void error(int errorCode, Avatar avatar) {
178			finishInitialSetup(avatar);
179		}
180	};
181    private TextWatcher mTextWatcher = new TextWatcher() {
182
183		@Override
184		public void onTextChanged(CharSequence s, int start, int before,
185								  int count) {
186			updateSaveButton();
187		}
188
189		@Override
190		public void beforeTextChanged(CharSequence s, int start, int count,
191									  int after) {
192
193		}
194
195		@Override
196		public void afterTextChanged(Editable s) {
197
198		}
199	};
200	private OnClickListener mAvatarClickListener = new OnClickListener() {
201		@Override
202		public void onClick(View view) {
203			if (mAccount!=null) {
204				Intent intent = new Intent(getApplicationContext(),
205						PublishProfilePictureActivity.class);
206				intent.putExtra("account", mAccount.getJid().toBareJid().toString());
207				startActivity(intent);
208			}
209		}
210	};
211
212	protected void finishInitialSetup(final Avatar avatar) {
213		runOnUiThread(new Runnable() {
214
215			@Override
216			public void run() {
217				Intent intent;
218				if (avatar != null) {
219					intent = new Intent(getApplicationContext(),
220							StartConversationActivity.class);
221				} else {
222					intent = new Intent(getApplicationContext(),
223							PublishProfilePictureActivity.class);
224					intent.putExtra("account", mAccount.getJid().toBareJid().toString());
225					intent.putExtra("setup", true);
226				}
227				startActivity(intent);
228				finish();
229			}
230		});
231	}
232
233	protected void updateSaveButton() {
234		if (mAccount != null
235				&& mAccount.getStatus() == Account.State.CONNECTING) {
236			this.mSaveButton.setEnabled(false);
237			this.mSaveButton.setTextColor(getSecondaryTextColor());
238			this.mSaveButton.setText(R.string.account_status_connecting);
239		} else if (mAccount != null
240				&& mAccount.getStatus() == Account.State.DISABLED) {
241			this.mSaveButton.setEnabled(true);
242			this.mSaveButton.setTextColor(getPrimaryTextColor());
243			this.mSaveButton.setText(R.string.enable);
244		} else {
245			this.mSaveButton.setEnabled(true);
246			this.mSaveButton.setTextColor(getPrimaryTextColor());
247			if (jidToEdit != null) {
248				if (mAccount != null
249						&& mAccount.getStatus() == Account.State.ONLINE) {
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().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(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.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
295		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
296		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
297		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
298		this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
299		this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
300		this.mSaveButton = (Button) findViewById(R.id.save_button);
301		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
302		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
303		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
304		this.mRegisterNew
305				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
306
307					@Override
308					public void onCheckedChanged(CompoundButton buttonView,
309												 boolean isChecked) {
310						if (isChecked) {
311							mPasswordConfirm.setVisibility(View.VISIBLE);
312						} else {
313							mPasswordConfirm.setVisibility(View.GONE);
314						}
315						updateSaveButton();
316					}
317				});
318	}
319
320	@Override
321	public boolean onCreateOptionsMenu(Menu menu) {
322		super.onCreateOptionsMenu(menu);
323		getMenuInflater().inflate(R.menu.editaccount, menu);
324		MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
325		if (mAccount == null) {
326			showQrCode.setVisible(false);
327		}
328		return true;
329	}
330
331	@Override
332	protected void onStart() {
333		super.onStart();
334		if (getIntent() != null) {
335            try {
336                this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
337            } catch (final InvalidJidException | NullPointerException ignored) {
338                this.jidToEdit = null;
339            }
340            if (this.jidToEdit != null) {
341				this.mRegisterNew.setVisibility(View.GONE);
342				getActionBar().setTitle(getString(R.string.account_details));
343			} else {
344				this.mAvatar.setVisibility(View.GONE);
345				getActionBar().setTitle(R.string.action_add_account);
346			}
347		}
348	}
349
350	@Override
351	protected void onBackendConnected() {
352        KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
353                android.R.layout.simple_list_item_1,
354                xmppConnectionService.getKnownHosts());
355		if (this.jidToEdit != null) {
356			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
357			updateAccountInformation();
358		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
359			getActionBar().setDisplayHomeAsUpEnabled(false);
360			getActionBar().setDisplayShowHomeEnabled(false);
361			this.mCancelButton.setEnabled(false);
362			this.mCancelButton.setTextColor(getSecondaryTextColor());
363		}
364		this.mAccountJid.setAdapter(mKnownHostsAdapter);
365		updateSaveButton();
366	}
367
368	private void updateAccountInformation() {
369		this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
370		this.mPassword.setText(this.mAccount.getPassword());
371		if (this.jidToEdit != null) {
372			this.mAvatar.setVisibility(View.VISIBLE);
373			this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
374		}
375		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
376			this.mRegisterNew.setVisibility(View.VISIBLE);
377			this.mRegisterNew.setChecked(true);
378			this.mPasswordConfirm.setText(this.mAccount.getPassword());
379		} else {
380			this.mRegisterNew.setVisibility(View.GONE);
381			this.mRegisterNew.setChecked(false);
382		}
383		if (this.mAccount.getStatus() == Account.State.ONLINE
384				&& !this.mFetchingAvatar) {
385			this.mStats.setVisibility(View.VISIBLE);
386			this.mSessionEst.setText(UIHelper.readableTimeDifferenceFull(
387					getApplicationContext(), this.mAccount.getXmppConnection()
388							.getLastSessionEstablished()));
389			Features features = this.mAccount.getXmppConnection().getFeatures();
390			if (features.carbons()) {
391				this.mServerInfoCarbons.setText(R.string.server_info_available);
392			} else {
393				this.mServerInfoCarbons
394						.setText(R.string.server_info_unavailable);
395			}
396			if (features.sm()) {
397				this.mServerInfoSm.setText(R.string.server_info_available);
398			} else {
399				this.mServerInfoSm.setText(R.string.server_info_unavailable);
400			}
401			if (features.pubsub()) {
402				this.mServerInfoPep.setText(R.string.server_info_available);
403			} else {
404				this.mServerInfoPep.setText(R.string.server_info_unavailable);
405			}
406			final String fingerprint = this.mAccount.getOtrFingerprint();
407			if (fingerprint != null) {
408				this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
409				this.mOtrFingerprint.setText(CryptoHelper.prettifyFingerprint(fingerprint));
410				this.mOtrFingerprintToClipboardButton
411						.setVisibility(View.VISIBLE);
412				this.mOtrFingerprintToClipboardButton
413						.setOnClickListener(new View.OnClickListener() {
414
415							@Override
416							public void onClick(View v) {
417
418								if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
419									Toast.makeText(
420											EditAccountActivity.this,
421											R.string.toast_message_otr_fingerprint,
422											Toast.LENGTH_SHORT).show();
423								}
424							}
425						});
426			} else {
427				this.mOtrFingerprintBox.setVisibility(View.GONE);
428			}
429		} else {
430			if (this.mAccount.errorStatus()) {
431				this.mAccountJid.setError(getString(this.mAccount.getStatus().getReadableId()));
432				this.mAccountJid.requestFocus();
433			}
434			this.mStats.setVisibility(View.GONE);
435		}
436	}
437}