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