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 (jidToEdit == null && mAccount != null
120							&& mAccount.getStatus() == Account.STATUS_ONLINE) {
121						if (!mFetchingAvatar) {
122							mFetchingAvatar = true;
123							xmppConnectionService.checkForAvatar(mAccount,
124									mAvatarFetchCallback);
125						}
126					} else {
127						updateSaveButton();
128					}
129					if (mAccount != null) {
130						updateAccountInformation();
131					}
132				}
133			});
134		}
135	};
136	private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
137
138		@Override
139		public void userInputRequried(PendingIntent pi, Avatar avatar) {
140			finishInitialSetup(avatar);
141		}
142
143		@Override
144		public void success(Avatar avatar) {
145			finishInitialSetup(avatar);
146		}
147
148		@Override
149		public void error(int errorCode, Avatar avatar) {
150			finishInitialSetup(avatar);
151		}
152	};
153	private KnownHostsAdapter mKnownHostsAdapter;
154
155	protected void finishInitialSetup(final Avatar avatar) {
156		runOnUiThread(new Runnable() {
157
158			@Override
159			public void run() {
160				Intent intent;
161				if (avatar != null) {
162					intent = new Intent(getApplicationContext(),
163							StartConversationActivity.class);
164				} else {
165					intent = new Intent(getApplicationContext(),
166							PublishProfilePictureActivity.class);
167					intent.putExtra("account", mAccount.getJid());
168					intent.putExtra("setup", true);
169				}
170				startActivity(intent);
171				finish();
172			}
173		});
174	}
175
176	protected boolean inputDataDiffersFromAccount() {
177		if (mAccount == null) {
178			return true;
179		} else {
180			return (!mAccount.getJid().equals(mAccountJid.getText().toString()))
181					|| (!mAccount.getPassword().equals(
182							mPassword.getText().toString()) || mAccount
183							.isOptionSet(Account.OPTION_REGISTER) != mRegisterNew
184							.isChecked());
185		}
186	}
187
188	protected void updateSaveButton() {
189		if (mAccount != null
190				&& mAccount.getStatus() == Account.STATUS_CONNECTING) {
191			this.mSaveButton.setEnabled(false);
192			this.mSaveButton.setTextColor(getSecondaryTextColor());
193			this.mSaveButton.setText(R.string.account_status_connecting);
194		} else {
195			this.mSaveButton.setEnabled(true);
196			this.mSaveButton.setTextColor(getPrimaryTextColor());
197			if (jidToEdit != null) {
198				this.mSaveButton.setText(R.string.connect);
199			} else {
200				this.mSaveButton.setText(R.string.next);
201			}
202		}
203	}
204
205	@Override
206	protected void onCreate(Bundle savedInstanceState) {
207		super.onCreate(savedInstanceState);
208		setContentView(R.layout.activity_edit_account);
209		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
210		this.mPassword = (EditText) findViewById(R.id.account_password);
211		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
212		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
213		this.mStats = (LinearLayout) findViewById(R.id.stats);
214		this.mSessionEst = (TextView) findViewById(R.id.session_est);
215		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
216		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
217		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
218		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
219		this.mOtrFingerprintHeadline = (TextView) findViewById(R.id.otr_fingerprint_headline);
220		this.mSaveButton = (Button) findViewById(R.id.save_button);
221		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
222		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
223		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
224		this.mRegisterNew
225				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
226
227					@Override
228					public void onCheckedChanged(CompoundButton buttonView,
229							boolean isChecked) {
230						if (isChecked) {
231							mPasswordConfirm.setVisibility(View.VISIBLE);
232						} else {
233							mPasswordConfirm.setVisibility(View.GONE);
234						}
235						updateSaveButton();
236					}
237				});
238	}
239
240	@Override
241	protected void onStart() {
242		super.onStart();
243		if (getIntent() != null) {
244			this.jidToEdit = getIntent().getStringExtra("jid");
245			if (this.jidToEdit != null) {
246				this.mRegisterNew.setVisibility(View.GONE);
247				getActionBar().setTitle(R.string.mgmt_account_edit);
248			} else {
249				getActionBar().setTitle(R.string.action_add_account);
250			}
251		}
252	}
253	
254	@Override
255	protected void onStop() {
256		if (xmppConnectionServiceBound) {
257			xmppConnectionService.removeOnAccountListChangedListener();
258		}
259		super.onStop();
260	}
261
262	@Override
263	protected void onBackendConnected() {
264		this.mKnownHostsAdapter = new KnownHostsAdapter(this,
265				android.R.layout.simple_list_item_1,
266				xmppConnectionService.getKnownHosts());
267		this.xmppConnectionService
268				.setOnAccountListChangedListener(this.mOnAccountUpdateListener);
269		if (this.jidToEdit != null) {
270			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
271			updateAccountInformation();
272		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
273			getActionBar().setDisplayHomeAsUpEnabled(false);
274			getActionBar().setDisplayShowHomeEnabled(false);
275			this.mCancelButton.setEnabled(false);
276		}
277		this.mAccountJid.setAdapter(this.mKnownHostsAdapter);
278		updateSaveButton();
279	}
280
281	private void updateAccountInformation() {
282		this.mAccountJid.setText(this.mAccount.getJid());
283		this.mPassword.setText(this.mAccount.getPassword());
284		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
285			this.mRegisterNew.setVisibility(View.VISIBLE);
286			this.mRegisterNew.setChecked(true);
287			this.mPasswordConfirm.setText(this.mAccount.getPassword());
288		} else {
289			this.mRegisterNew.setVisibility(View.GONE);
290			this.mRegisterNew.setChecked(false);
291		}
292		if (this.mAccount.getStatus() == Account.STATUS_ONLINE && !this.mFetchingAvatar) {
293			this.mStats.setVisibility(View.VISIBLE);
294			this.mSessionEst.setText(UIHelper.readableTimeDifference(
295					getApplicationContext(), this.mAccount.getXmppConnection()
296							.getLastSessionEstablished()));
297			Features features = this.mAccount.getXmppConnection().getFeatures();
298			if (features.carbons()) {
299				this.mServerInfoCarbons.setText(R.string.server_info_available);
300			} else {
301				this.mServerInfoCarbons.setText(R.string.server_info_unavailable);
302			}
303			if (features.sm()) {
304				this.mServerInfoSm.setText(R.string.server_info_available);
305			} else {
306				this.mServerInfoSm.setText(R.string.server_info_unavailable);
307			}
308			if (features.pubsub()) {
309				this.mServerInfoPep.setText(R.string.server_info_available);
310			} else {
311				this.mServerInfoPep.setText(R.string.server_info_unavailable);
312			}
313			String fingerprint = this.mAccount.getOtrFingerprint(getApplicationContext());
314			if (fingerprint!=null) {
315				this.mOtrFingerprintHeadline.setVisibility(View.VISIBLE);
316				this.mOtrFingerprint.setVisibility(View.VISIBLE);
317				this.mOtrFingerprint.setText(fingerprint);
318			} else {
319				this.mOtrFingerprint.setVisibility(View.GONE);
320				this.mOtrFingerprintHeadline.setVisibility(View.GONE);
321			}
322		} else {
323			if (this.mAccount.errorStatus()) {
324				this.mAccountJid.setError(getString(this.mAccount
325						.getReadableStatusId()));
326				this.mAccountJid.requestFocus();
327			}
328			this.mStats.setVisibility(View.GONE);
329		}
330	}
331}