2014-08-15 17:31:24 +02:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.app.PendingIntent;
|
2014-10-03 15:55:06 +02:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
2014-08-15 17:31:24 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
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.EditText;
|
2014-10-03 15:55:06 +02:00
|
|
|
import android.widget.ImageButton;
|
2014-08-19 15:06:50 +02:00
|
|
|
import android.widget.LinearLayout;
|
2014-08-15 17:31:24 +02:00
|
|
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
2014-08-19 15:06:50 +02:00
|
|
|
import android.widget.TextView;
|
2014-10-03 15:55:06 +02:00
|
|
|
import android.widget.Toast;
|
2014-08-15 17:31:24 +02:00
|
|
|
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;
|
2014-08-19 15:06:50 +02:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2014-08-15 17:31:24 +02:00
|
|
|
import eu.siacs.conversations.utils.Validator;
|
2014-08-23 15:56:30 +02:00
|
|
|
import eu.siacs.conversations.xmpp.XmppConnection.Features;
|
2014-08-15 17:31:24 +02:00
|
|
|
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;
|
|
|
|
|
2014-08-19 15:06:50 +02:00
|
|
|
private LinearLayout mStats;
|
2014-08-23 15:56:30 +02:00
|
|
|
private TextView mServerInfoSm;
|
|
|
|
private TextView mServerInfoCarbons;
|
|
|
|
private TextView mServerInfoPep;
|
2014-08-19 15:06:50 +02:00
|
|
|
private TextView mSessionEst;
|
2014-08-20 11:32:49 +02:00
|
|
|
private TextView mOtrFingerprint;
|
|
|
|
private TextView mOtrFingerprintHeadline;
|
2014-10-03 15:55:06 +02:00
|
|
|
private ImageButton mOtrFingerprintToClipboardButton;
|
2014-08-19 15:06:50 +02:00
|
|
|
|
2014-08-15 17:31:24 +02:00
|
|
|
private String jidToEdit;
|
|
|
|
private Account mAccount;
|
|
|
|
|
|
|
|
private boolean mFetchingAvatar = false;
|
2014-08-19 15:06:50 +02:00
|
|
|
|
2014-08-15 17:31:24 +02:00
|
|
|
private OnClickListener mSaveButtonClickListener = new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2014-08-20 11:32:49 +02:00
|
|
|
if (!Validator.isValidJid(mAccountJid.getText().toString())) {
|
|
|
|
mAccountJid.setError(getString(R.string.invalid_jid));
|
|
|
|
mAccountJid.requestFocus();
|
2014-08-15 17:31:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
boolean registerNewAccount = mRegisterNew.isChecked();
|
|
|
|
String[] jidParts = mAccountJid.getText().toString().split("@");
|
|
|
|
String username = jidParts[0];
|
|
|
|
String server;
|
|
|
|
if (jidParts.length >= 2) {
|
|
|
|
server = jidParts[1];
|
|
|
|
} else {
|
|
|
|
server = "";
|
|
|
|
}
|
|
|
|
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));
|
2014-08-20 11:32:49 +02:00
|
|
|
mPasswordConfirm.requestFocus();
|
2014-08-15 17:31:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mAccount != null) {
|
|
|
|
mAccount.setPassword(password);
|
|
|
|
mAccount.setUsername(username);
|
|
|
|
mAccount.setServer(server);
|
2014-08-23 21:31:27 +02:00
|
|
|
mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
|
2014-08-15 17:31:24 +02:00
|
|
|
xmppConnectionService.updateAccount(mAccount);
|
|
|
|
} else {
|
2014-08-19 15:06:50 +02:00
|
|
|
if (xmppConnectionService.findAccountByJid(mAccountJid
|
|
|
|
.getText().toString()) != null) {
|
|
|
|
mAccountJid
|
|
|
|
.setError(getString(R.string.account_already_exists));
|
2014-08-20 11:32:49 +02:00
|
|
|
mAccountJid.requestFocus();
|
2014-08-15 17:31:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mAccount = new Account(username, server, password);
|
|
|
|
mAccount.setOption(Account.OPTION_USETLS, true);
|
|
|
|
mAccount.setOption(Account.OPTION_USECOMPRESSION, true);
|
2014-08-20 11:32:49 +02:00
|
|
|
mAccount.setOption(Account.OPTION_REGISTER, registerNewAccount);
|
2014-08-15 17:31:24 +02:00
|
|
|
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() {
|
2014-08-23 21:31:27 +02:00
|
|
|
if (mAccount != null
|
|
|
|
&& mAccount.getStatus() != Account.STATUS_ONLINE
|
|
|
|
&& mFetchingAvatar) {
|
|
|
|
startActivity(new Intent(getApplicationContext(),
|
|
|
|
ManageAccountActivity.class));
|
|
|
|
finish();
|
|
|
|
} else if (jidToEdit == null && mAccount != null
|
2014-08-19 15:06:50 +02:00
|
|
|
&& mAccount.getStatus() == Account.STATUS_ONLINE) {
|
2014-08-15 17:31:24 +02:00
|
|
|
if (!mFetchingAvatar) {
|
|
|
|
mFetchingAvatar = true;
|
2014-08-19 15:06:50 +02:00
|
|
|
xmppConnectionService.checkForAvatar(mAccount,
|
|
|
|
mAvatarFetchCallback);
|
2014-08-15 17:31:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updateSaveButton();
|
|
|
|
}
|
2014-08-20 11:32:49 +02:00
|
|
|
if (mAccount != null) {
|
|
|
|
updateAccountInformation();
|
|
|
|
}
|
2014-08-15 17:31:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
|
2014-08-19 15:06:50 +02:00
|
|
|
|
2014-08-15 17:31:24 +02:00
|
|
|
@Override
|
|
|
|
public void userInputRequried(PendingIntent pi, Avatar avatar) {
|
|
|
|
finishInitialSetup(avatar);
|
|
|
|
}
|
2014-08-19 15:06:50 +02:00
|
|
|
|
2014-08-15 17:31:24 +02:00
|
|
|
@Override
|
|
|
|
public void success(Avatar avatar) {
|
|
|
|
finishInitialSetup(avatar);
|
|
|
|
}
|
2014-08-19 15:06:50 +02:00
|
|
|
|
2014-08-15 17:31:24 +02:00
|
|
|
@Override
|
|
|
|
public void error(int errorCode, Avatar avatar) {
|
|
|
|
finishInitialSetup(avatar);
|
|
|
|
}
|
|
|
|
};
|
2014-08-19 15:06:50 +02:00
|
|
|
private KnownHostsAdapter mKnownHostsAdapter;
|
2014-08-15 17:31:24 +02:00
|
|
|
|
2014-08-16 09:44:37 +02:00
|
|
|
protected void finishInitialSetup(final Avatar avatar) {
|
2014-08-15 17:31:24 +02:00
|
|
|
runOnUiThread(new Runnable() {
|
2014-08-19 15:06:50 +02:00
|
|
|
|
2014-08-15 17:31:24 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-16 09:44:37 +02:00
|
|
|
Intent intent;
|
2014-08-19 15:06:50 +02:00
|
|
|
if (avatar != null) {
|
|
|
|
intent = new Intent(getApplicationContext(),
|
|
|
|
StartConversationActivity.class);
|
2014-08-16 09:44:37 +02:00
|
|
|
} else {
|
2014-08-19 15:06:50 +02:00
|
|
|
intent = new Intent(getApplicationContext(),
|
|
|
|
PublishProfilePictureActivity.class);
|
2014-08-16 09:44:37 +02:00
|
|
|
intent.putExtra("account", mAccount.getJid());
|
2014-08-23 20:00:05 +02:00
|
|
|
intent.putExtra("setup", true);
|
2014-08-16 09:44:37 +02:00
|
|
|
}
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
2014-08-15 17:31:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-08-19 15:06:50 +02:00
|
|
|
|
2014-08-15 17:31:24 +02:00
|
|
|
protected boolean inputDataDiffersFromAccount() {
|
|
|
|
if (mAccount == null) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return (!mAccount.getJid().equals(mAccountJid.getText().toString()))
|
|
|
|
|| (!mAccount.getPassword().equals(
|
|
|
|
mPassword.getText().toString()) || mAccount
|
|
|
|
.isOptionSet(Account.OPTION_REGISTER) != mRegisterNew
|
|
|
|
.isChecked());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateSaveButton() {
|
2014-08-16 09:44:37 +02:00
|
|
|
if (mAccount != null
|
2014-08-19 15:06:50 +02:00
|
|
|
&& mAccount.getStatus() == Account.STATUS_CONNECTING) {
|
2014-08-15 17:31:24 +02:00
|
|
|
this.mSaveButton.setEnabled(false);
|
|
|
|
this.mSaveButton.setTextColor(getSecondaryTextColor());
|
|
|
|
this.mSaveButton.setText(R.string.account_status_connecting);
|
2014-08-19 15:06:50 +02:00
|
|
|
} else {
|
2014-08-15 17:31:24 +02:00
|
|
|
this.mSaveButton.setEnabled(true);
|
|
|
|
this.mSaveButton.setTextColor(getPrimaryTextColor());
|
2014-08-19 15:06:50 +02:00
|
|
|
if (jidToEdit != null) {
|
2014-08-31 16:28:21 +02:00
|
|
|
if (mAccount != null
|
|
|
|
&& mAccount.getStatus() == Account.STATUS_ONLINE) {
|
2014-08-29 10:24:25 +02:00
|
|
|
this.mSaveButton.setText(R.string.save);
|
|
|
|
} else {
|
|
|
|
this.mSaveButton.setText(R.string.connect);
|
|
|
|
}
|
2014-08-16 09:44:37 +02:00
|
|
|
} else {
|
|
|
|
this.mSaveButton.setText(R.string.next);
|
|
|
|
}
|
2014-08-15 17:31:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_edit_account);
|
|
|
|
this.mAccountJid = (AutoCompleteTextView) findViewById(R.id.account_jid);
|
|
|
|
this.mPassword = (EditText) findViewById(R.id.account_password);
|
|
|
|
this.mPasswordConfirm = (EditText) findViewById(R.id.account_password_confirm);
|
|
|
|
this.mRegisterNew = (CheckBox) findViewById(R.id.account_register_new);
|
2014-08-19 15:06:50 +02:00
|
|
|
this.mStats = (LinearLayout) findViewById(R.id.stats);
|
|
|
|
this.mSessionEst = (TextView) findViewById(R.id.session_est);
|
2014-08-23 15:56:30 +02:00
|
|
|
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);
|
2014-08-20 11:32:49 +02:00
|
|
|
this.mOtrFingerprint = (TextView) findViewById(R.id.otr_fingerprint);
|
|
|
|
this.mOtrFingerprintHeadline = (TextView) findViewById(R.id.otr_fingerprint_headline);
|
2014-10-03 15:55:06 +02:00
|
|
|
this.mOtrFingerprintToClipboardButton = (ImageButton) findViewById(R.id.action_copy_to_clipboard);
|
2014-08-15 17:31:24 +02:00
|
|
|
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
|
|
|
|
protected void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
if (getIntent() != null) {
|
|
|
|
this.jidToEdit = getIntent().getStringExtra("jid");
|
|
|
|
if (this.jidToEdit != null) {
|
|
|
|
this.mRegisterNew.setVisibility(View.GONE);
|
|
|
|
getActionBar().setTitle(R.string.mgmt_account_edit);
|
|
|
|
} else {
|
|
|
|
getActionBar().setTitle(R.string.action_add_account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-23 21:31:27 +02:00
|
|
|
|
2014-08-21 07:38:44 +02:00
|
|
|
@Override
|
|
|
|
protected void onStop() {
|
|
|
|
if (xmppConnectionServiceBound) {
|
|
|
|
xmppConnectionService.removeOnAccountListChangedListener();
|
|
|
|
}
|
|
|
|
super.onStop();
|
|
|
|
}
|
2014-08-15 17:31:24 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onBackendConnected() {
|
2014-08-19 15:06:50 +02:00
|
|
|
this.mKnownHostsAdapter = new KnownHostsAdapter(this,
|
|
|
|
android.R.layout.simple_list_item_1,
|
|
|
|
xmppConnectionService.getKnownHosts());
|
2014-08-15 17:31:24 +02:00
|
|
|
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);
|
2014-08-23 21:31:27 +02:00
|
|
|
this.mCancelButton.setTextColor(getSecondaryTextColor());
|
2014-08-15 17:31:24 +02:00
|
|
|
}
|
2014-08-19 15:06:50 +02:00
|
|
|
this.mAccountJid.setAdapter(this.mKnownHostsAdapter);
|
2014-08-15 17:31:24 +02:00
|
|
|
updateSaveButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateAccountInformation() {
|
|
|
|
this.mAccountJid.setText(this.mAccount.getJid());
|
|
|
|
this.mPassword.setText(this.mAccount.getPassword());
|
|
|
|
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);
|
|
|
|
}
|
2014-08-23 21:31:27 +02:00
|
|
|
if (this.mAccount.getStatus() == Account.STATUS_ONLINE
|
|
|
|
&& !this.mFetchingAvatar) {
|
2014-08-19 15:06:50 +02:00
|
|
|
this.mStats.setVisibility(View.VISIBLE);
|
|
|
|
this.mSessionEst.setText(UIHelper.readableTimeDifference(
|
|
|
|
getApplicationContext(), this.mAccount.getXmppConnection()
|
|
|
|
.getLastSessionEstablished()));
|
2014-08-23 15:56:30 +02:00
|
|
|
Features features = this.mAccount.getXmppConnection().getFeatures();
|
|
|
|
if (features.carbons()) {
|
|
|
|
this.mServerInfoCarbons.setText(R.string.server_info_available);
|
|
|
|
} else {
|
2014-08-23 21:31:27 +02:00
|
|
|
this.mServerInfoCarbons
|
|
|
|
.setText(R.string.server_info_unavailable);
|
2014-08-23 15:56:30 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2014-10-03 15:55:06 +02:00
|
|
|
final String fingerprint = this.mAccount
|
2014-09-06 18:21:31 +02:00
|
|
|
.getOtrFingerprint(xmppConnectionService);
|
2014-08-23 21:31:27 +02:00
|
|
|
if (fingerprint != null) {
|
2014-08-20 11:32:49 +02:00
|
|
|
this.mOtrFingerprintHeadline.setVisibility(View.VISIBLE);
|
|
|
|
this.mOtrFingerprint.setVisibility(View.VISIBLE);
|
|
|
|
this.mOtrFingerprint.setText(fingerprint);
|
2014-10-03 15:55:06 +02:00
|
|
|
this.mOtrFingerprintToClipboardButton.setVisibility(View.VISIBLE);
|
|
|
|
this.mOtrFingerprintToClipboardButton
|
|
|
|
.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
|
|
|
if (OtrFingerprintToClipBoard(fingerprint)) {
|
|
|
|
Toast.makeText(
|
|
|
|
EditAccountActivity.this,
|
|
|
|
R.string.toast_message_otr_fingerprint,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-08-20 11:32:49 +02:00
|
|
|
} else {
|
2014-10-03 15:55:06 +02:00
|
|
|
this.mOtrFingerprintToClipboardButton.setVisibility(View.GONE);
|
2014-08-20 11:32:49 +02:00
|
|
|
this.mOtrFingerprint.setVisibility(View.GONE);
|
|
|
|
this.mOtrFingerprintHeadline.setVisibility(View.GONE);
|
|
|
|
}
|
2014-08-19 15:06:50 +02:00
|
|
|
} else {
|
|
|
|
if (this.mAccount.errorStatus()) {
|
|
|
|
this.mAccountJid.setError(getString(this.mAccount
|
|
|
|
.getReadableStatusId()));
|
|
|
|
this.mAccountJid.requestFocus();
|
|
|
|
}
|
|
|
|
this.mStats.setVisibility(View.GONE);
|
|
|
|
}
|
2014-08-15 17:31:24 +02:00
|
|
|
}
|
2014-10-03 15:55:06 +02:00
|
|
|
|
|
|
|
private boolean OtrFingerprintToClipBoard(String fingerprint) {
|
|
|
|
ClipboardManager mClipBoardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
|
|
|
String label = getResources().getString(R.string.otr_fingerprint);
|
|
|
|
if (mClipBoardManager != null) {
|
|
|
|
ClipData mClipData = ClipData.newPlainText(label, fingerprint);
|
|
|
|
mClipBoardManager.setPrimaryClip(mClipData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 17:31:24 +02:00
|
|
|
}
|