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