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.pep.Avatar;
 23
 24public class EditAccountActivity extends XmppActivity {
 25
 26	private AutoCompleteTextView mAccountJid;
 27	private EditText mPassword;
 28	private EditText mPasswordConfirm;
 29	private CheckBox mRegisterNew;
 30	private Button mCancelButton;
 31	private Button mSaveButton;
 32
 33	private LinearLayout mStats;
 34	private TextView mServerCompat;
 35	private TextView mSessionEst;
 36	private TextView mOtrFingerprint;
 37	private TextView mOtrFingerprintHeadline;
 38
 39	private String jidToEdit;
 40	private Account mAccount;
 41
 42	private boolean mFetchingAvatar = false;
 43
 44	private OnClickListener mSaveButtonClickListener = new OnClickListener() {
 45
 46		@Override
 47		public void onClick(View v) {
 48			if (!Validator.isValidJid(mAccountJid.getText().toString())) {
 49				mAccountJid.setError(getString(R.string.invalid_jid));
 50				mAccountJid.requestFocus();
 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					mPasswordConfirm.requestFocus();
 69					return;
 70				}
 71			}
 72			if (mAccount != null) {
 73				mAccount.setPassword(password);
 74				mAccount.setUsername(username);
 75				mAccount.setServer(server);
 76				mAccount.setOption(Account.OPTION_REGISTER,registerNewAccount);
 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					mAccountJid.requestFocus();
 84					return;
 85				}
 86				mAccount = new Account(username, server, password);
 87				mAccount.setOption(Account.OPTION_USETLS, true);
 88				mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
 89				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
 90				xmppConnectionService.createAccount(mAccount);
 91			}
 92			if (jidToEdit != null) {
 93				finish();
 94			} else {
 95				updateSaveButton();
 96				updateAccountInformation();
 97			}
 98
 99		}
100	};
101	private OnClickListener mCancelButtonClickListener = new OnClickListener() {
102
103		@Override
104		public void onClick(View v) {
105			finish();
106		}
107	};
108	private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
109
110		@Override
111		public void onAccountUpdate() {
112			runOnUiThread(new Runnable() {
113
114				@Override
115				public void run() {
116					if (jidToEdit == null && mAccount != null
117							&& mAccount.getStatus() == Account.STATUS_ONLINE) {
118						if (!mFetchingAvatar) {
119							mFetchingAvatar = true;
120							xmppConnectionService.checkForAvatar(mAccount,
121									mAvatarFetchCallback);
122						}
123					} else {
124						updateSaveButton();
125					}
126					if (mAccount != null) {
127						updateAccountInformation();
128					}
129				}
130			});
131		}
132	};
133	private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
134
135		@Override
136		public void userInputRequried(PendingIntent pi, Avatar avatar) {
137			finishInitialSetup(avatar);
138		}
139
140		@Override
141		public void success(Avatar avatar) {
142			finishInitialSetup(avatar);
143		}
144
145		@Override
146		public void error(int errorCode, Avatar avatar) {
147			finishInitialSetup(avatar);
148		}
149	};
150	private KnownHostsAdapter mKnownHostsAdapter;
151
152	protected void finishInitialSetup(final Avatar avatar) {
153		runOnUiThread(new Runnable() {
154
155			@Override
156			public void run() {
157				Intent intent;
158				if (avatar != null) {
159					intent = new Intent(getApplicationContext(),
160							StartConversationActivity.class);
161				} else {
162					intent = new Intent(getApplicationContext(),
163							PublishProfilePictureActivity.class);
164					intent.putExtra("account", mAccount.getJid());
165				}
166				startActivity(intent);
167				finish();
168			}
169		});
170	}
171
172	protected boolean inputDataDiffersFromAccount() {
173		if (mAccount == null) {
174			return true;
175		} else {
176			return (!mAccount.getJid().equals(mAccountJid.getText().toString()))
177					|| (!mAccount.getPassword().equals(
178							mPassword.getText().toString()) || mAccount
179							.isOptionSet(Account.OPTION_REGISTER) != mRegisterNew
180							.isChecked());
181		}
182	}
183
184	protected void updateSaveButton() {
185		if (mAccount != null
186				&& mAccount.getStatus() == Account.STATUS_CONNECTING) {
187			this.mSaveButton.setEnabled(false);
188			this.mSaveButton.setTextColor(getSecondaryTextColor());
189			this.mSaveButton.setText(R.string.account_status_connecting);
190		} else {
191			this.mSaveButton.setEnabled(true);
192			this.mSaveButton.setTextColor(getPrimaryTextColor());
193			if (jidToEdit != null) {
194				this.mSaveButton.setText(R.string.connect);
195			} else {
196				this.mSaveButton.setText(R.string.next);
197			}
198		}
199	}
200
201	@Override
202	protected void onCreate(Bundle savedInstanceState) {
203		super.onCreate(savedInstanceState);
204		setContentView(R.layout.activity_edit_account);
205		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
206		this.mPassword = (EditText) findViewById(R.id.account_password);
207		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
208		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
209		this.mStats = (LinearLayout) findViewById(R.id.stats);
210		this.mSessionEst = (TextView) findViewById(R.id.session_est);
211		this.mServerCompat = (TextView) findViewById(R.id.server_compat);
212		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
213		this.mOtrFingerprintHeadline = (TextView) findViewById(R.id.otr_fingerprint_headline);
214		this.mSaveButton = (Button) findViewById(R.id.save_button);
215		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
216		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
217		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
218		this.mRegisterNew
219				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
220
221					@Override
222					public void onCheckedChanged(CompoundButton buttonView,
223							boolean isChecked) {
224						if (isChecked) {
225							mPasswordConfirm.setVisibility(View.VISIBLE);
226						} else {
227							mPasswordConfirm.setVisibility(View.GONE);
228						}
229						updateSaveButton();
230					}
231				});
232	}
233
234	@Override
235	protected void onStart() {
236		super.onStart();
237		if (getIntent() != null) {
238			this.jidToEdit = getIntent().getStringExtra("jid");
239			if (this.jidToEdit != null) {
240				this.mRegisterNew.setVisibility(View.GONE);
241				getActionBar().setTitle(R.string.mgmt_account_edit);
242			} else {
243				getActionBar().setTitle(R.string.action_add_account);
244			}
245		}
246	}
247
248	@Override
249	protected void onBackendConnected() {
250		this.mKnownHostsAdapter = new KnownHostsAdapter(this,
251				android.R.layout.simple_list_item_1,
252				xmppConnectionService.getKnownHosts());
253		this.xmppConnectionService
254				.setOnAccountListChangedListener(this.mOnAccountUpdateListener);
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 && !this.mFetchingAvatar) {
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			String fingerprint = this.mAccount.getOtrFingerprint(getApplicationContext());
287			if (fingerprint!=null) {
288				this.mOtrFingerprintHeadline.setVisibility(View.VISIBLE);
289				this.mOtrFingerprint.setVisibility(View.VISIBLE);
290				this.mOtrFingerprint.setText(fingerprint);
291			} else {
292				this.mOtrFingerprint.setVisibility(View.GONE);
293				this.mOtrFingerprintHeadline.setVisibility(View.GONE);
294			}
295		} else {
296			if (this.mAccount.errorStatus()) {
297				this.mAccountJid.setError(getString(this.mAccount
298						.getReadableStatusId()));
299				this.mAccountJid.requestFocus();
300			}
301			this.mStats.setVisibility(View.GONE);
302		}
303	}
304}