EditAccountActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.app.PendingIntent;
  4import android.content.Intent;
  5import android.os.Bundle;
  6import android.view.View;
  7import android.view.View.OnClickListener;
  8import android.widget.AutoCompleteTextView;
  9import android.widget.Button;
 10import android.widget.CheckBox;
 11import android.widget.CompoundButton;
 12import android.widget.EditText;
 13import android.widget.LinearLayout;
 14import android.widget.CompoundButton.OnCheckedChangeListener;
 15import android.widget.TextView;
 16import eu.siacs.conversations.R;
 17import eu.siacs.conversations.entities.Account;
 18import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 19import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
 20import eu.siacs.conversations.utils.UIHelper;
 21import eu.siacs.conversations.utils.Validator;
 22import eu.siacs.conversations.xmpp.XmppConnection.Features;
 23import eu.siacs.conversations.xmpp.pep.Avatar;
 24
 25public class EditAccountActivity extends XmppActivity {
 26
 27	private AutoCompleteTextView mAccountJid;
 28	private EditText mPassword;
 29	private EditText mPasswordConfirm;
 30	private CheckBox mRegisterNew;
 31	private Button mCancelButton;
 32	private Button mSaveButton;
 33
 34	private LinearLayout mStats;
 35	private TextView mServerInfoSm;
 36	private TextView mServerInfoCarbons;
 37	private TextView mServerInfoPep;
 38	private TextView mSessionEst;
 39	private TextView mOtrFingerprint;
 40	private TextView mOtrFingerprintHeadline;
 41
 42	private String jidToEdit;
 43	private Account mAccount;
 44
 45	private boolean mFetchingAvatar = false;
 46
 47	private OnClickListener mSaveButtonClickListener = new OnClickListener() {
 48
 49		@Override
 50		public void onClick(View v) {
 51			if (!Validator.isValidJid(mAccountJid.getText().toString())) {
 52				mAccountJid.setError(getString(R.string.invalid_jid));
 53				mAccountJid.requestFocus();
 54				return;
 55			}
 56			boolean registerNewAccount = mRegisterNew.isChecked();
 57			String[] jidParts = mAccountJid.getText().toString().split("@");
 58			String username = jidParts[0];
 59			String server;
 60			if (jidParts.length >= 2) {
 61				server = jidParts[1];
 62			} else {
 63				server = "";
 64			}
 65			String password = mPassword.getText().toString();
 66			String passwordConfirm = mPasswordConfirm.getText().toString();
 67			if (registerNewAccount) {
 68				if (!password.equals(passwordConfirm)) {
 69					mPasswordConfirm
 70							.setError(getString(R.string.passwords_do_not_match));
 71					mPasswordConfirm.requestFocus();
 72					return;
 73				}
 74			}
 75			if (mAccount != null) {
 76				mAccount.setPassword(password);
 77				mAccount.setUsername(username);
 78				mAccount.setServer(server);
 79				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
 80				xmppConnectionService.updateAccount(mAccount);
 81			} else {
 82				if (xmppConnectionService.findAccountByJid(mAccountJid
 83						.getText().toString()) != null) {
 84					mAccountJid
 85							.setError(getString(R.string.account_already_exists));
 86					mAccountJid.requestFocus();
 87					return;
 88				}
 89				mAccount = new Account(username, server, password);
 90				mAccount.setOption(Account.OPTION_USETLS, true);
 91				mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
 92				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
 93				xmppConnectionService.createAccount(mAccount);
 94			}
 95			if (jidToEdit != null) {
 96				finish();
 97			} else {
 98				updateSaveButton();
 99				updateAccountInformation();
100			}
101
102		}
103	};
104	private OnClickListener mCancelButtonClickListener = new OnClickListener() {
105
106		@Override
107		public void onClick(View v) {
108			finish();
109		}
110	};
111	private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
112
113		@Override
114		public void onAccountUpdate() {
115			runOnUiThread(new Runnable() {
116
117				@Override
118				public void run() {
119					if (mAccount != null
120							&& mAccount.getStatus() != Account.STATUS_ONLINE
121							&& mFetchingAvatar) {
122						startActivity(new Intent(getApplicationContext(),
123								ManageAccountActivity.class));
124						finish();
125					} else if (jidToEdit == null && mAccount != null
126							&& mAccount.getStatus() == Account.STATUS_ONLINE) {
127						if (!mFetchingAvatar) {
128							mFetchingAvatar = true;
129							xmppConnectionService.checkForAvatar(mAccount,
130									mAvatarFetchCallback);
131						}
132					} else {
133						updateSaveButton();
134					}
135					if (mAccount != null) {
136						updateAccountInformation();
137					}
138				}
139			});
140		}
141	};
142	private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
143
144		@Override
145		public void userInputRequried(PendingIntent pi, Avatar avatar) {
146			finishInitialSetup(avatar);
147		}
148
149		@Override
150		public void success(Avatar avatar) {
151			finishInitialSetup(avatar);
152		}
153
154		@Override
155		public void error(int errorCode, Avatar avatar) {
156			finishInitialSetup(avatar);
157		}
158	};
159	private KnownHostsAdapter mKnownHostsAdapter;
160
161	protected void finishInitialSetup(final Avatar avatar) {
162		runOnUiThread(new Runnable() {
163
164			@Override
165			public void run() {
166				Intent intent;
167				if (avatar != null) {
168					intent = new Intent(getApplicationContext(),
169							StartConversationActivity.class);
170				} else {
171					intent = new Intent(getApplicationContext(),
172							PublishProfilePictureActivity.class);
173					intent.putExtra("account", mAccount.getJid());
174					intent.putExtra("setup", true);
175				}
176				startActivity(intent);
177				finish();
178			}
179		});
180	}
181
182	protected boolean inputDataDiffersFromAccount() {
183		if (mAccount == null) {
184			return true;
185		} else {
186			return (!mAccount.getJid().equals(mAccountJid.getText().toString()))
187					|| (!mAccount.getPassword().equals(
188							mPassword.getText().toString()) || mAccount
189							.isOptionSet(Account.OPTION_REGISTER) != mRegisterNew
190							.isChecked());
191		}
192	}
193
194	protected void updateSaveButton() {
195		if (mAccount != null
196				&& mAccount.getStatus() == Account.STATUS_CONNECTING) {
197			this.mSaveButton.setEnabled(false);
198			this.mSaveButton.setTextColor(getSecondaryTextColor());
199			this.mSaveButton.setText(R.string.account_status_connecting);
200		} else {
201			this.mSaveButton.setEnabled(true);
202			this.mSaveButton.setTextColor(getPrimaryTextColor());
203			if (jidToEdit != null) {
204				if (mAccount != null
205						&& mAccount.getStatus() == Account.STATUS_ONLINE) {
206					this.mSaveButton.setText(R.string.save);
207				} else {
208					this.mSaveButton.setText(R.string.connect);
209				}
210			} else {
211				this.mSaveButton.setText(R.string.next);
212			}
213		}
214	}
215
216	@Override
217	protected void onCreate(Bundle savedInstanceState) {
218		super.onCreate(savedInstanceState);
219		setContentView(R.layout.activity_edit_account);
220		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
221		this.mPassword = (EditText) findViewById(R.id.account_password);
222		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
223		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
224		this.mStats = (LinearLayout) findViewById(R.id.stats);
225		this.mSessionEst = (TextView) findViewById(R.id.session_est);
226		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
227		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
228		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
229		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
230		this.mOtrFingerprintHeadline = (TextView) findViewById(R.id.otr_fingerprint_headline);
231		this.mSaveButton = (Button) findViewById(R.id.save_button);
232		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
233		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
234		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
235		this.mRegisterNew
236				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
237
238					@Override
239					public void onCheckedChanged(CompoundButton buttonView,
240							boolean isChecked) {
241						if (isChecked) {
242							mPasswordConfirm.setVisibility(View.VISIBLE);
243						} else {
244							mPasswordConfirm.setVisibility(View.GONE);
245						}
246						updateSaveButton();
247					}
248				});
249	}
250
251	@Override
252	protected void onStart() {
253		super.onStart();
254		if (getIntent() != null) {
255			this.jidToEdit = getIntent().getStringExtra("jid");
256			if (this.jidToEdit != null) {
257				this.mRegisterNew.setVisibility(View.GONE);
258				getActionBar().setTitle(R.string.mgmt_account_edit);
259			} else {
260				getActionBar().setTitle(R.string.action_add_account);
261			}
262		}
263	}
264
265	@Override
266	protected void onStop() {
267		if (xmppConnectionServiceBound) {
268			xmppConnectionService.removeOnAccountListChangedListener();
269		}
270		super.onStop();
271	}
272
273	@Override
274	protected void onBackendConnected() {
275		this.mKnownHostsAdapter = new KnownHostsAdapter(this,
276				android.R.layout.simple_list_item_1,
277				xmppConnectionService.getKnownHosts());
278		this.xmppConnectionService
279				.setOnAccountListChangedListener(this.mOnAccountUpdateListener);
280		if (this.jidToEdit != null) {
281			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
282			updateAccountInformation();
283		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
284			getActionBar().setDisplayHomeAsUpEnabled(false);
285			getActionBar().setDisplayShowHomeEnabled(false);
286			this.mCancelButton.setEnabled(false);
287			this.mCancelButton.setTextColor(getSecondaryTextColor());
288		}
289		this.mAccountJid.setAdapter(this.mKnownHostsAdapter);
290		updateSaveButton();
291	}
292
293	private void updateAccountInformation() {
294		this.mAccountJid.setText(this.mAccount.getJid());
295		this.mPassword.setText(this.mAccount.getPassword());
296		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
297			this.mRegisterNew.setVisibility(View.VISIBLE);
298			this.mRegisterNew.setChecked(true);
299			this.mPasswordConfirm.setText(this.mAccount.getPassword());
300		} else {
301			this.mRegisterNew.setVisibility(View.GONE);
302			this.mRegisterNew.setChecked(false);
303		}
304		if (this.mAccount.getStatus() == Account.STATUS_ONLINE
305				&& !this.mFetchingAvatar) {
306			this.mStats.setVisibility(View.VISIBLE);
307			this.mSessionEst.setText(UIHelper.readableTimeDifference(
308					getApplicationContext(), this.mAccount.getXmppConnection()
309							.getLastSessionEstablished()));
310			Features features = this.mAccount.getXmppConnection().getFeatures();
311			if (features.carbons()) {
312				this.mServerInfoCarbons.setText(R.string.server_info_available);
313			} else {
314				this.mServerInfoCarbons
315						.setText(R.string.server_info_unavailable);
316			}
317			if (features.sm()) {
318				this.mServerInfoSm.setText(R.string.server_info_available);
319			} else {
320				this.mServerInfoSm.setText(R.string.server_info_unavailable);
321			}
322			if (features.pubsub()) {
323				this.mServerInfoPep.setText(R.string.server_info_available);
324			} else {
325				this.mServerInfoPep.setText(R.string.server_info_unavailable);
326			}
327			String fingerprint = this.mAccount
328					.getOtrFingerprint(xmppConnectionService);
329			if (fingerprint != null) {
330				this.mOtrFingerprintHeadline.setVisibility(View.VISIBLE);
331				this.mOtrFingerprint.setVisibility(View.VISIBLE);
332				this.mOtrFingerprint.setText(fingerprint);
333			} else {
334				this.mOtrFingerprint.setVisibility(View.GONE);
335				this.mOtrFingerprintHeadline.setVisibility(View.GONE);
336			}
337		} else {
338			if (this.mAccount.errorStatus()) {
339				this.mAccountJid.setError(getString(this.mAccount
340						.getReadableStatusId()));
341				this.mAccountJid.requestFocus();
342			}
343			this.mStats.setVisibility(View.GONE);
344		}
345	}
346}