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