EditAccountActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.app.PendingIntent;
  4import android.content.ClipData;
  5import android.content.ClipboardManager;
  6import android.content.Intent;
  7import android.os.Bundle;
  8import android.text.Editable;
  9import android.text.TextWatcher;
 10import android.view.View;
 11import android.view.View.OnClickListener;
 12import android.widget.AutoCompleteTextView;
 13import android.widget.Button;
 14import android.widget.CheckBox;
 15import android.widget.CompoundButton;
 16import android.widget.EditText;
 17import android.widget.ImageButton;
 18import android.widget.LinearLayout;
 19import android.widget.CompoundButton.OnCheckedChangeListener;
 20import android.widget.RelativeLayout;
 21import android.widget.TextView;
 22import android.widget.Toast;
 23import eu.siacs.conversations.R;
 24import eu.siacs.conversations.entities.Account;
 25import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 26import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
 27import eu.siacs.conversations.utils.UIHelper;
 28import eu.siacs.conversations.utils.Validator;
 29import eu.siacs.conversations.xmpp.XmppConnection.Features;
 30import eu.siacs.conversations.xmpp.pep.Avatar;
 31
 32public class EditAccountActivity extends XmppActivity {
 33
 34	private AutoCompleteTextView mAccountJid;
 35	private EditText mPassword;
 36	private EditText mPasswordConfirm;
 37	private CheckBox mRegisterNew;
 38	private Button mCancelButton;
 39	private Button mSaveButton;
 40
 41	private LinearLayout mStats;
 42	private TextView mServerInfoSm;
 43	private TextView mServerInfoCarbons;
 44	private TextView mServerInfoPep;
 45	private TextView mSessionEst;
 46	private TextView mOtrFingerprint;
 47	private RelativeLayout mOtrFingerprintBox;
 48	private ImageButton mOtrFingerprintToClipboardButton;
 49
 50	private String jidToEdit;
 51	private Account mAccount;
 52
 53	private boolean mFetchingAvatar = false;
 54
 55	private OnClickListener mSaveButtonClickListener = new OnClickListener() {
 56
 57		@Override
 58		public void onClick(View v) {
 59			if (mAccount != null
 60					&& mAccount.getStatus() == Account.STATUS_DISABLED) {
 61				mAccount.setOption(Account.OPTION_DISABLED, false);
 62				xmppConnectionService.updateAccount(mAccount);
 63				return;
 64			}
 65			if (!Validator.isValidJid(mAccountJid.getText().toString())) {
 66				mAccountJid.setError(getString(R.string.invalid_jid));
 67				mAccountJid.requestFocus();
 68				return;
 69			}
 70			boolean registerNewAccount = mRegisterNew.isChecked();
 71			String[] jidParts = mAccountJid.getText().toString().split("@");
 72			String username = jidParts[0];
 73			String server;
 74			if (jidParts.length >= 2) {
 75				server = jidParts[1];
 76			} else {
 77				server = "";
 78			}
 79			String password = mPassword.getText().toString();
 80			String passwordConfirm = mPasswordConfirm.getText().toString();
 81			if (registerNewAccount) {
 82				if (!password.equals(passwordConfirm)) {
 83					mPasswordConfirm
 84							.setError(getString(R.string.passwords_do_not_match));
 85					mPasswordConfirm.requestFocus();
 86					return;
 87				}
 88			}
 89			if (mAccount != null) {
 90				mAccount.setPassword(password);
 91				mAccount.setUsername(username);
 92				mAccount.setServer(server);
 93				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
 94				xmppConnectionService.updateAccount(mAccount);
 95			} else {
 96				if (xmppConnectionService.findAccountByJid(mAccountJid
 97						.getText().toString()) != null) {
 98					mAccountJid
 99							.setError(getString(R.string.account_already_exists));
100					mAccountJid.requestFocus();
101					return;
102				}
103				mAccount = new Account(username, server, password);
104				mAccount.setOption(Account.OPTION_USETLS, true);
105				mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
106				mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
107				xmppConnectionService.createAccount(mAccount);
108			}
109			if (jidToEdit != null) {
110				finish();
111			} else {
112				updateSaveButton();
113				updateAccountInformation();
114			}
115
116		}
117	};
118	private OnClickListener mCancelButtonClickListener = new OnClickListener() {
119
120		@Override
121		public void onClick(View v) {
122			finish();
123		}
124	};
125	private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
126
127		@Override
128		public void onAccountUpdate() {
129			runOnUiThread(new Runnable() {
130
131				@Override
132				public void run() {
133					if (mAccount != null
134							&& mAccount.getStatus() != Account.STATUS_ONLINE
135							&& mFetchingAvatar) {
136						startActivity(new Intent(getApplicationContext(),
137								ManageAccountActivity.class));
138						finish();
139					} else if (jidToEdit == null && mAccount != null
140							&& mAccount.getStatus() == Account.STATUS_ONLINE) {
141						if (!mFetchingAvatar) {
142							mFetchingAvatar = true;
143							xmppConnectionService.checkForAvatar(mAccount,
144									mAvatarFetchCallback);
145						}
146					} else {
147						updateSaveButton();
148					}
149					if (mAccount != null) {
150						updateAccountInformation();
151					}
152				}
153			});
154		}
155	};
156	private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
157
158		@Override
159		public void userInputRequried(PendingIntent pi, Avatar avatar) {
160			finishInitialSetup(avatar);
161		}
162
163		@Override
164		public void success(Avatar avatar) {
165			finishInitialSetup(avatar);
166		}
167
168		@Override
169		public void error(int errorCode, Avatar avatar) {
170			finishInitialSetup(avatar);
171		}
172	};
173	private KnownHostsAdapter mKnownHostsAdapter;
174	private TextWatcher mTextWatcher = new TextWatcher() {
175
176		@Override
177		public void onTextChanged(CharSequence s, int start, int before,
178				int count) {
179			updateSaveButton();
180		}
181
182		@Override
183		public void beforeTextChanged(CharSequence s, int start, int count,
184				int after) {
185
186		}
187
188		@Override
189		public void afterTextChanged(Editable s) {
190
191		}
192	};
193
194	protected void finishInitialSetup(final Avatar avatar) {
195		runOnUiThread(new Runnable() {
196
197			@Override
198			public void run() {
199				Intent intent;
200				if (avatar != null) {
201					intent = new Intent(getApplicationContext(),
202							StartConversationActivity.class);
203				} else {
204					intent = new Intent(getApplicationContext(),
205							PublishProfilePictureActivity.class);
206					intent.putExtra("account", mAccount.getJid());
207					intent.putExtra("setup", true);
208				}
209				startActivity(intent);
210				finish();
211			}
212		});
213	}
214
215	protected boolean inputDataDiffersFromAccount() {
216		if (mAccount == null) {
217			return true;
218		} else {
219			return (!mAccount.getJid().equals(mAccountJid.getText().toString()))
220					|| (!mAccount.getPassword().equals(
221							mPassword.getText().toString()) || mAccount
222							.isOptionSet(Account.OPTION_REGISTER) != mRegisterNew
223							.isChecked());
224		}
225	}
226
227	protected void updateSaveButton() {
228		if (mAccount != null
229				&& mAccount.getStatus() == Account.STATUS_CONNECTING) {
230			this.mSaveButton.setEnabled(false);
231			this.mSaveButton.setTextColor(getSecondaryTextColor());
232			this.mSaveButton.setText(R.string.account_status_connecting);
233		} else if (mAccount != null
234				&& mAccount.getStatus() == Account.STATUS_DISABLED) {
235			this.mSaveButton.setEnabled(true);
236			this.mSaveButton.setTextColor(getPrimaryTextColor());
237			this.mSaveButton.setText(R.string.enable);
238		} else {
239			this.mSaveButton.setEnabled(true);
240			this.mSaveButton.setTextColor(getPrimaryTextColor());
241			if (jidToEdit != null) {
242				if (mAccount != null
243						&& mAccount.getStatus() == Account.STATUS_ONLINE) {
244					this.mSaveButton.setText(R.string.save);
245					if (!accountInfoEdited()) {
246						this.mSaveButton.setEnabled(false);
247						this.mSaveButton.setTextColor(getSecondaryTextColor());
248					}
249				} else {
250					this.mSaveButton.setText(R.string.connect);
251				}
252			} else {
253				this.mSaveButton.setText(R.string.next);
254			}
255		}
256	}
257
258	protected boolean accountInfoEdited() {
259		return (!this.mAccount.getJid().equals(
260				this.mAccountJid.getText().toString()))
261				|| (!this.mAccount.getPassword().equals(
262						this.mPassword.getText().toString()));
263	}
264
265	@Override
266	protected void onCreate(Bundle savedInstanceState) {
267		super.onCreate(savedInstanceState);
268		setContentView(R.layout.activity_edit_account);
269		this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
270		this.mAccountJid.addTextChangedListener(this.mTextWatcher);
271		this.mPassword = (EditText) findViewById(R.id.account_password);
272		this.mPassword.addTextChangedListener(this.mTextWatcher);
273		this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
274		this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
275		this.mStats = (LinearLayout) findViewById(R.id.stats);
276		this.mSessionEst = (TextView) findViewById(R.id.session_est);
277		this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
278		this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
279		this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
280		this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
281		this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
282		this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
283		this.mSaveButton = (Button) findViewById(R.id.save_button);
284		this.mCancelButton = (Button) findViewById(R.id.cancel_button);
285		this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
286		this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
287		this.mRegisterNew
288				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
289
290					@Override
291					public void onCheckedChanged(CompoundButton buttonView,
292							boolean isChecked) {
293						if (isChecked) {
294							mPasswordConfirm.setVisibility(View.VISIBLE);
295						} else {
296							mPasswordConfirm.setVisibility(View.GONE);
297						}
298						updateSaveButton();
299					}
300				});
301	}
302
303	@Override
304	protected void onStart() {
305		super.onStart();
306		if (getIntent() != null) {
307			this.jidToEdit = getIntent().getStringExtra("jid");
308			if (this.jidToEdit != null) {
309				this.mRegisterNew.setVisibility(View.GONE);
310				getActionBar().setTitle(jidToEdit);
311			} else {
312				getActionBar().setTitle(R.string.action_add_account);
313			}
314		}
315	}
316
317	@Override
318	protected void onStop() {
319		if (xmppConnectionServiceBound) {
320			xmppConnectionService.removeOnAccountListChangedListener();
321		}
322		super.onStop();
323	}
324
325	@Override
326	protected void onBackendConnected() {
327		this.mKnownHostsAdapter = new KnownHostsAdapter(this,
328				android.R.layout.simple_list_item_1,
329				xmppConnectionService.getKnownHosts());
330		this.xmppConnectionService
331				.setOnAccountListChangedListener(this.mOnAccountUpdateListener);
332		if (this.jidToEdit != null) {
333			this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
334			updateAccountInformation();
335		} else if (this.xmppConnectionService.getAccounts().size() == 0) {
336			getActionBar().setDisplayHomeAsUpEnabled(false);
337			getActionBar().setDisplayShowHomeEnabled(false);
338			this.mCancelButton.setEnabled(false);
339			this.mCancelButton.setTextColor(getSecondaryTextColor());
340		}
341		this.mAccountJid.setAdapter(this.mKnownHostsAdapter);
342		updateSaveButton();
343	}
344
345	private void updateAccountInformation() {
346		this.mAccountJid.setText(this.mAccount.getJid());
347		this.mPassword.setText(this.mAccount.getPassword());
348		if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
349			this.mRegisterNew.setVisibility(View.VISIBLE);
350			this.mRegisterNew.setChecked(true);
351			this.mPasswordConfirm.setText(this.mAccount.getPassword());
352		} else {
353			this.mRegisterNew.setVisibility(View.GONE);
354			this.mRegisterNew.setChecked(false);
355		}
356		if (this.mAccount.getStatus() == Account.STATUS_ONLINE
357				&& !this.mFetchingAvatar) {
358			this.mStats.setVisibility(View.VISIBLE);
359			this.mSessionEst.setText(UIHelper.readableTimeDifference(
360					getApplicationContext(), this.mAccount.getXmppConnection()
361							.getLastSessionEstablished()));
362			Features features = this.mAccount.getXmppConnection().getFeatures();
363			if (features.carbons()) {
364				this.mServerInfoCarbons.setText(R.string.server_info_available);
365			} else {
366				this.mServerInfoCarbons
367						.setText(R.string.server_info_unavailable);
368			}
369			if (features.sm()) {
370				this.mServerInfoSm.setText(R.string.server_info_available);
371			} else {
372				this.mServerInfoSm.setText(R.string.server_info_unavailable);
373			}
374			if (features.pubsub()) {
375				this.mServerInfoPep.setText(R.string.server_info_available);
376			} else {
377				this.mServerInfoPep.setText(R.string.server_info_unavailable);
378			}
379			final String fingerprint = this.mAccount
380					.getOtrFingerprint(xmppConnectionService);
381			if (fingerprint != null) {
382				this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
383				this.mOtrFingerprint.setText(fingerprint);
384				this.mOtrFingerprintToClipboardButton
385						.setVisibility(View.VISIBLE);
386				this.mOtrFingerprintToClipboardButton
387						.setOnClickListener(new View.OnClickListener() {
388
389							@Override
390							public void onClick(View v) {
391
392								if (copyTextToClipboard(fingerprint,R.string.otr_fingerprint)) {
393									Toast.makeText(
394											EditAccountActivity.this,
395											R.string.toast_message_otr_fingerprint,
396											Toast.LENGTH_SHORT).show();
397								}
398							}
399						});
400			} else {
401				this.mOtrFingerprintBox.setVisibility(View.GONE);
402			}
403		} else {
404			if (this.mAccount.errorStatus()) {
405				this.mAccountJid.setError(getString(this.mAccount
406						.getReadableStatusId()));
407				this.mAccountJid.requestFocus();
408			}
409			this.mStats.setVisibility(View.GONE);
410		}
411	}
412}