conversations-classic/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java

452 lines
15 KiB
Java
Raw Normal View History

package eu.siacs.conversations.ui;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
2014-10-05 10:05:27 +00:00
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.utils.Validator;
import eu.siacs.conversations.xmpp.XmppConnection.Features;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.pep.Avatar;
public class EditAccountActivity extends XmppActivity {
private AutoCompleteTextView mAccountJid;
private EditText mPassword;
private EditText mPasswordConfirm;
private CheckBox mRegisterNew;
private Button mCancelButton;
private Button mSaveButton;
private LinearLayout mStats;
private TextView mServerInfoSm;
private TextView mServerInfoCarbons;
private TextView mServerInfoPep;
private TextView mSessionEst;
private TextView mOtrFingerprint;
2014-11-04 16:38:41 +00:00
private ImageView mAvatar;
private RelativeLayout mOtrFingerprintBox;
private ImageButton mOtrFingerprintToClipboardButton;
private Jid jidToEdit;
private Account mAccount;
private boolean mFetchingAvatar = false;
private OnClickListener mSaveButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
2014-10-05 10:05:27 +00:00
if (mAccount != null
&& mAccount.getStatus() == Account.STATUS_DISABLED) {
mAccount.setOption(Account.OPTION_DISABLED, false);
xmppConnectionService.updateAccount(mAccount);
return;
}
if (!Validator.isValidJid(mAccountJid.getText().toString())) {
mAccountJid.setError(getString(R.string.invalid_jid));
mAccountJid.requestFocus();
return;
}
boolean registerNewAccount = mRegisterNew.isChecked();
final Jid jid;
try {
jid = Jid.fromString(mAccountJid.getText().toString());
} catch (final InvalidJidException e) {
// TODO: Handle this error?
return;
}
String password = mPassword.getText().toString();
String passwordConfirm = mPasswordConfirm.getText().toString();
if (registerNewAccount) {
if (!password.equals(passwordConfirm)) {
mPasswordConfirm
.setError(getString(R.string.passwords_do_not_match));
mPasswordConfirm.requestFocus();
return;
}
}
if (mAccount != null) {
mAccount.setPassword(password);
try {
mAccount.setUsername(jid.hasLocalpart() ? jid.getLocalpart() : "");
mAccount.setServer(jid.getDomainpart());
} catch (final InvalidJidException ignored) {
}
mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
xmppConnectionService.updateAccount(mAccount);
} else {
try {
if (xmppConnectionService.findAccountByJid(Jid.fromString(mAccountJid.getText().toString())) != null) {
mAccountJid
.setError(getString(R.string.account_already_exists));
mAccountJid.requestFocus();
return;
}
} catch (InvalidJidException e) {
return;
}
mAccount = new Account(jid.toBareJid(), password);
mAccount.setOption(Account.OPTION_USETLS, true);
mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
xmppConnectionService.createAccount(mAccount);
}
if (jidToEdit != null) {
finish();
} else {
updateSaveButton();
updateAccountInformation();
}
}
};
private OnClickListener mCancelButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
};
private OnAccountUpdate mOnAccountUpdateListener = new OnAccountUpdate() {
@Override
public void onAccountUpdate() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mAccount != null
&& mAccount.getStatus() != Account.STATUS_ONLINE
&& mFetchingAvatar) {
startActivity(new Intent(getApplicationContext(),
ManageAccountActivity.class));
finish();
} else if (jidToEdit == null && mAccount != null
&& mAccount.getStatus() == Account.STATUS_ONLINE) {
if (!mFetchingAvatar) {
mFetchingAvatar = true;
xmppConnectionService.checkForAvatar(mAccount,
mAvatarFetchCallback);
}
} else {
updateSaveButton();
}
if (mAccount != null) {
updateAccountInformation();
}
}
});
}
};
private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
@Override
public void userInputRequried(PendingIntent pi, Avatar avatar) {
finishInitialSetup(avatar);
}
@Override
public void success(Avatar avatar) {
finishInitialSetup(avatar);
}
@Override
public void error(int errorCode, Avatar avatar) {
finishInitialSetup(avatar);
}
};
private TextWatcher mTextWatcher = new TextWatcher() {
2014-10-05 10:05:27 +00:00
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
2014-10-05 10:05:27 +00:00
updateSaveButton();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
2014-10-05 10:05:27 +00:00
}
@Override
public void afterTextChanged(Editable s) {
}
};
2014-11-04 16:38:41 +00:00
private OnClickListener mAvatarClickListener = new OnClickListener() {
@Override
public void onClick(View view) {
if (mAccount!=null) {
Intent intent = new Intent(getApplicationContext(),
PublishProfilePictureActivity.class);
intent.putExtra("account", mAccount.getJid().toBareJid().toString());
2014-11-04 16:38:41 +00:00
startActivity(intent);
}
}
};
protected void finishInitialSetup(final Avatar avatar) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent intent;
if (avatar != null) {
intent = new Intent(getApplicationContext(),
StartConversationActivity.class);
} else {
intent = new Intent(getApplicationContext(),
PublishProfilePictureActivity.class);
intent.putExtra("account", mAccount.getJid().toBareJid().toString());
intent.putExtra("setup", true);
}
startActivity(intent);
finish();
}
});
}
protected void updateSaveButton() {
if (mAccount != null
&& mAccount.getStatus() == Account.STATUS_CONNECTING) {
this.mSaveButton.setEnabled(false);
this.mSaveButton.setTextColor(getSecondaryTextColor());
this.mSaveButton.setText(R.string.account_status_connecting);
2014-10-05 10:05:27 +00:00
} else if (mAccount != null
&& mAccount.getStatus() == Account.STATUS_DISABLED) {
this.mSaveButton.setEnabled(true);
this.mSaveButton.setTextColor(getPrimaryTextColor());
this.mSaveButton.setText(R.string.enable);
} else {
this.mSaveButton.setEnabled(true);
this.mSaveButton.setTextColor(getPrimaryTextColor());
if (jidToEdit != null) {
2014-08-31 14:28:21 +00:00
if (mAccount != null
&& mAccount.getStatus() == Account.STATUS_ONLINE) {
2014-08-29 08:24:25 +00:00
this.mSaveButton.setText(R.string.save);
2014-10-05 10:05:27 +00:00
if (!accountInfoEdited()) {
this.mSaveButton.setEnabled(false);
this.mSaveButton.setTextColor(getSecondaryTextColor());
}
2014-08-29 08:24:25 +00:00
} else {
this.mSaveButton.setText(R.string.connect);
}
} else {
this.mSaveButton.setText(R.string.next);
}
}
}
2014-10-05 10:05:27 +00:00
protected boolean accountInfoEdited() {
return (!this.mAccount.getJid().toBareJid().equals(
2014-10-05 10:05:27 +00:00
this.mAccountJid.getText().toString()))
|| (!this.mAccount.getPassword().equals(
this.mPassword.getText().toString()));
2014-10-05 10:05:27 +00:00
}
@Override
protected String getShareableUri() {
if (mAccount!=null) {
return "xmpp:"+mAccount.getJid().toBareJid();
} else {
return "";
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_account);
this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
2014-10-05 10:05:27 +00:00
this.mAccountJid.addTextChangedListener(this.mTextWatcher);
this.mPassword = (EditText) findViewById(R.id.account_password);
2014-10-05 10:05:27 +00:00
this.mPassword.addTextChangedListener(this.mTextWatcher);
this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
2014-11-04 16:38:41 +00:00
this.mAvatar = (ImageView) findViewById(R.id.avater);
this.mAvatar.setOnClickListener(this.mAvatarClickListener);
this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
this.mStats = (LinearLayout) findViewById(R.id.stats);
this.mSessionEst = (TextView) findViewById(R.id.session_est);
this.mServerInfoCarbons = (TextView) findViewById(R.id.server_info_carbons);
this.mServerInfoSm = (TextView) findViewById(R.id.server_info_sm);
this.mServerInfoPep = (TextView) findViewById(R.id.server_info_pep);
this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
this.mOtrFingerprintBox = (RelativeLayout) findViewById(R.id.otr_fingerprint_box);
this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
this.mSaveButton = (Button) findViewById(R.id.save_button);
this.mCancelButton = (Button) findViewById(R.id.cancel_button);
this.mSaveButton.setOnClickListener(this.mSaveButtonClickListener);
this.mCancelButton.setOnClickListener(this.mCancelButtonClickListener);
this.mRegisterNew
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
mPasswordConfirm.setVisibility(View.VISIBLE);
} else {
mPasswordConfirm.setVisibility(View.GONE);
}
updateSaveButton();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.editaccount, menu);
MenuItem showQrCode = menu.findItem(R.id.action_show_qr_code);
if (mAccount == null) {
showQrCode.setVisible(false);
}
return true;
}
@Override
protected void onStart() {
super.onStart();
if (getIntent() != null) {
try {
this.jidToEdit = Jid.fromString(getIntent().getStringExtra("jid"));
2014-11-08 03:48:17 +00:00
} catch (final InvalidJidException | NullPointerException ignored) {
this.jidToEdit = null;
}
if (this.jidToEdit != null) {
this.mRegisterNew.setVisibility(View.GONE);
2014-11-04 16:38:41 +00:00
getActionBar().setTitle(getString(R.string.account_details));
} else {
2014-11-04 16:38:41 +00:00
this.mAvatar.setVisibility(View.GONE);
getActionBar().setTitle(R.string.action_add_account);
}
}
}
2014-08-21 05:38:44 +00:00
@Override
protected void onStop() {
if (xmppConnectionServiceBound) {
xmppConnectionService.removeOnAccountListChangedListener();
}
super.onStop();
}
@Override
protected void onBackendConnected() {
KnownHostsAdapter mKnownHostsAdapter = new KnownHostsAdapter(this,
android.R.layout.simple_list_item_1,
xmppConnectionService.getKnownHosts());
this.xmppConnectionService
.setOnAccountListChangedListener(this.mOnAccountUpdateListener);
if (this.jidToEdit != null) {
this.mAccount = xmppConnectionService.findAccountByJid(jidToEdit);
updateAccountInformation();
} else if (this.xmppConnectionService.getAccounts().size() == 0) {
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
this.mCancelButton.setEnabled(false);
this.mCancelButton.setTextColor(getSecondaryTextColor());
}
this.mAccountJid.setAdapter(mKnownHostsAdapter);
updateSaveButton();
}
private void updateAccountInformation() {
this.mAccountJid.setText(this.mAccount.getJid().toBareJid().toString());
this.mPassword.setText(this.mAccount.getPassword());
if (this.jidToEdit != null) {
this.mAvatar.setVisibility(View.VISIBLE);
this.mAvatar.setImageBitmap(avatarService().get(this.mAccount, getPixel(72)));
}
if (this.mAccount.isOptionSet(Account.OPTION_REGISTER)) {
this.mRegisterNew.setVisibility(View.VISIBLE);
this.mRegisterNew.setChecked(true);
this.mPasswordConfirm.setText(this.mAccount.getPassword());
} else {
this.mRegisterNew.setVisibility(View.GONE);
this.mRegisterNew.setChecked(false);
}
if (this.mAccount.getStatus() == Account.STATUS_ONLINE
&& !this.mFetchingAvatar) {
this.mStats.setVisibility(View.VISIBLE);
this.mSessionEst.setText(UIHelper.readableTimeDifference(
getApplicationContext(), this.mAccount.getXmppConnection()
.getLastSessionEstablished()));
Features features = this.mAccount.getXmppConnection().getFeatures();
if (features.carbons()) {
this.mServerInfoCarbons.setText(R.string.server_info_available);
} else {
this.mServerInfoCarbons
.setText(R.string.server_info_unavailable);
}
if (features.sm()) {
this.mServerInfoSm.setText(R.string.server_info_available);
} else {
this.mServerInfoSm.setText(R.string.server_info_unavailable);
}
if (features.pubsub()) {
this.mServerInfoPep.setText(R.string.server_info_available);
} else {
this.mServerInfoPep.setText(R.string.server_info_unavailable);
}
final String fingerprint = this.mAccount
.getOtrFingerprint(xmppConnectionService);
if (fingerprint != null) {
this.mOtrFingerprintBox.setVisibility(View.VISIBLE);
this.mOtrFingerprint.setText(fingerprint);
2014-10-05 10:05:27 +00:00
this.mOtrFingerprintToClipboardButton
.setVisibility(View.VISIBLE);
this.mOtrFingerprintToClipboardButton
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (copyTextToClipboard(fingerprint, R.string.otr_fingerprint)) {
Toast.makeText(
EditAccountActivity.this,
R.string.toast_message_otr_fingerprint,
Toast.LENGTH_SHORT).show();
}
}
});
} else {
this.mOtrFingerprintBox.setVisibility(View.GONE);
}
} else {
if (this.mAccount.errorStatus()) {
this.mAccountJid.setError(getString(this.mAccount
.getReadableStatusId()));
this.mAccountJid.requestFocus();
}
this.mStats.setVisibility(View.GONE);
}
}
}