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				this.mSaveButton.setText(R.string.connect);
205			} else {
206				this.mSaveButton.setText(R.string.next);
207			}
208		}
209	}
210
211	@Override
212	protected void onCreate(Bundle savedInstanceState) {
213		super.onCreate(savedInstanceState);
214		setContentView(R.layout.activity_edit_account);
215		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
216		this.mPassword = (EditText) findViewById(R.id.account_password);
217		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
218		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
219		this.mStats = (LinearLayout) findViewById(R.id.stats);
220		this.mSessionEst = (TextView) findViewById(R.id.session_est);
221		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
222		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
223		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
224		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
225		this.mOtrFingerprintHeadline = (TextView) findViewById(R.id.otr_fingerprint_headline);
226		this.mSaveButton = (Button) findViewById(R.id.save_button);
227		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
228		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
229		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
230		this.mRegisterNew
231				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
232
233					@Override
234					public void onCheckedChanged(CompoundButton buttonView,
235							boolean isChecked) {
236						if (isChecked) {
237							mPasswordConfirm.setVisibility(View.VISIBLE);
238						} else {
239							mPasswordConfirm.setVisibility(View.GONE);
240						}
241						updateSaveButton();
242					}
243				});
244	}
245
246	@Override
247	protected void onStart() {
248		super.onStart();
249		if (getIntent() != null) {
250			this.jidToEdit = getIntent().getStringExtra("jid");
251			if (this.jidToEdit != null) {
252				this.mRegisterNew.setVisibility(View.GONE);
253				getActionBar().setTitle(R.string.mgmt_account_edit);
254			} else {
255				getActionBar().setTitle(R.string.action_add_account);
256			}
257		}
258	}
259
260	@Override
261	protected void onStop() {
262		if (xmppConnectionServiceBound) {
263			xmppConnectionService.removeOnAccountListChangedListener();
264		}
265		super.onStop();
266	}
267
268	@Override
269	protected void onBackendConnected() {
270		this.mKnownHostsAdapter = new KnownHostsAdapter(this,
271				android.R.layout.simple_list_item_1,
272				xmppConnectionService.getKnownHosts());
273		this.xmppConnectionService
274				.setOnAccountListChangedListener(this.mOnAccountUpdateListener);
275		if (this.jidToEdit != null) {
276			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
277			updateAccountInformation();
278		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
279			getActionBar().setDisplayHomeAsUpEnabled(false);
280			getActionBar().setDisplayShowHomeEnabled(false);
281			this.mCancelButton.setEnabled(false);
282			this.mCancelButton.setTextColor(getSecondaryTextColor());
283		}
284		this.mAccountJid.setAdapter(this.mKnownHostsAdapter);
285		updateSaveButton();
286	}
287
288	private void updateAccountInformation() {
289		this.mAccountJid.setText(this.mAccount.getJid());
290		this.mPassword.setText(this.mAccount.getPassword());
291		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
292			this.mRegisterNew.setVisibility(View.VISIBLE);
293			this.mRegisterNew.setChecked(true);
294			this.mPasswordConfirm.setText(this.mAccount.getPassword());
295		} else {
296			this.mRegisterNew.setVisibility(View.GONE);
297			this.mRegisterNew.setChecked(false);
298		}
299		if (this.mAccount.getStatus() == Account.STATUS_ONLINE
300				&& !this.mFetchingAvatar) {
301			this.mStats.setVisibility(View.VISIBLE);
302			this.mSessionEst.setText(UIHelper.readableTimeDifference(
303					getApplicationContext(), this.mAccount.getXmppConnection()
304							.getLastSessionEstablished()));
305			Features features = this.mAccount.getXmppConnection().getFeatures();
306			if (features.carbons()) {
307				this.mServerInfoCarbons.setText(R.string.server_info_available);
308			} else {
309				this.mServerInfoCarbons
310						.setText(R.string.server_info_unavailable);
311			}
312			if (features.sm()) {
313				this.mServerInfoSm.setText(R.string.server_info_available);
314			} else {
315				this.mServerInfoSm.setText(R.string.server_info_unavailable);
316			}
317			if (features.pubsub()) {
318				this.mServerInfoPep.setText(R.string.server_info_available);
319			} else {
320				this.mServerInfoPep.setText(R.string.server_info_unavailable);
321			}
322			String fingerprint = this.mAccount
323					.getOtrFingerprint(getApplicationContext());
324			if (fingerprint != null) {
325				this.mOtrFingerprintHeadline.setVisibility(View.VISIBLE);
326				this.mOtrFingerprint.setVisibility(View.VISIBLE);
327				this.mOtrFingerprint.setText(fingerprint);
328			} else {
329				this.mOtrFingerprint.setVisibility(View.GONE);
330				this.mOtrFingerprintHeadline.setVisibility(View.GONE);
331			}
332		} else {
333			if (this.mAccount.errorStatus()) {
334				this.mAccountJid.setError(getString(this.mAccount
335						.getReadableStatusId()));
336				this.mAccountJid.requestFocus();
337			}
338			this.mStats.setVisibility(View.GONE);
339		}
340	}
341}