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