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