2015-07-19 16:36:28 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
2015-10-16 21:48:42 +00:00
|
|
|
import android.widget.Toast;
|
2015-07-19 16:36:28 +00:00
|
|
|
|
|
|
|
import org.whispersystems.libaxolotl.IdentityKey;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
2015-10-16 21:48:42 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.AxolotlService;
|
2015-08-01 16:27:52 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
|
2015-07-19 16:36:28 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
2015-07-21 12:18:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
|
2015-07-19 16:36:28 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
|
2015-07-21 12:18:16 +00:00
|
|
|
public class TrustKeysActivity extends XmppActivity implements OnKeyStatusUpdated {
|
2015-07-19 16:36:28 +00:00
|
|
|
private Jid accountJid;
|
|
|
|
private Jid contactJid;
|
2015-10-16 21:48:42 +00:00
|
|
|
|
2015-07-19 16:36:28 +00:00
|
|
|
private Contact contact;
|
2015-10-16 21:48:42 +00:00
|
|
|
private Account mAccount;
|
2015-08-15 16:52:47 +00:00
|
|
|
private TextView keyErrorMessage;
|
|
|
|
private LinearLayout keyErrorMessageCard;
|
2015-07-19 16:36:28 +00:00
|
|
|
private TextView ownKeysTitle;
|
|
|
|
private LinearLayout ownKeys;
|
|
|
|
private LinearLayout ownKeysCard;
|
|
|
|
private TextView foreignKeysTitle;
|
|
|
|
private LinearLayout foreignKeys;
|
|
|
|
private LinearLayout foreignKeysCard;
|
|
|
|
private Button mSaveButton;
|
|
|
|
private Button mCancelButton;
|
|
|
|
|
2016-01-13 11:19:56 +00:00
|
|
|
private AxolotlService.FetchStatus lastFetchReport = AxolotlService.FetchStatus.SUCCESS;
|
|
|
|
|
2015-09-06 13:08:42 +00:00
|
|
|
private final Map<String, Boolean> ownKeysToTrust = new HashMap<>();
|
|
|
|
private final Map<String, Boolean> foreignKeysToTrust = new HashMap<>();
|
2015-07-19 16:36:28 +00:00
|
|
|
|
|
|
|
private final OnClickListener mSaveButtonListener = new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
commitTrusts();
|
2015-10-16 21:48:42 +00:00
|
|
|
finishOk();
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private final OnClickListener mCancelButtonListener = new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void refreshUiReal() {
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
populateView();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String getShareableUri() {
|
|
|
|
if (contact != null) {
|
|
|
|
return contact.getShareableUri();
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_trust_keys);
|
|
|
|
try {
|
2016-01-22 19:46:24 +00:00
|
|
|
this.accountJid = Jid.fromString(getIntent().getExtras().getString(EXTRA_ACCOUNT));
|
2015-07-19 16:36:28 +00:00
|
|
|
} catch (final InvalidJidException ignored) {
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.contactJid = Jid.fromString(getIntent().getExtras().getString("contact"));
|
|
|
|
} catch (final InvalidJidException ignored) {
|
|
|
|
}
|
|
|
|
|
2015-08-15 16:52:47 +00:00
|
|
|
keyErrorMessageCard = (LinearLayout) findViewById(R.id.key_error_message_card);
|
|
|
|
keyErrorMessage = (TextView) findViewById(R.id.key_error_message);
|
2015-07-19 16:36:28 +00:00
|
|
|
ownKeysTitle = (TextView) findViewById(R.id.own_keys_title);
|
|
|
|
ownKeys = (LinearLayout) findViewById(R.id.own_keys_details);
|
|
|
|
ownKeysCard = (LinearLayout) findViewById(R.id.own_keys_card);
|
|
|
|
foreignKeysTitle = (TextView) findViewById(R.id.foreign_keys_title);
|
|
|
|
foreignKeys = (LinearLayout) findViewById(R.id.foreign_keys_details);
|
|
|
|
foreignKeysCard = (LinearLayout) findViewById(R.id.foreign_keys_card);
|
|
|
|
mCancelButton = (Button) findViewById(R.id.cancel_button);
|
|
|
|
mCancelButton.setOnClickListener(mCancelButtonListener);
|
|
|
|
mSaveButton = (Button) findViewById(R.id.save_button);
|
|
|
|
mSaveButton.setOnClickListener(mSaveButtonListener);
|
|
|
|
|
|
|
|
|
|
|
|
if (getActionBar() != null) {
|
|
|
|
getActionBar().setHomeButtonEnabled(true);
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void populateView() {
|
2015-08-08 11:29:21 +00:00
|
|
|
setTitle(getString(R.string.trust_omemo_fingerprints));
|
2015-07-19 16:36:28 +00:00
|
|
|
ownKeys.removeAllViews();
|
|
|
|
foreignKeys.removeAllViews();
|
|
|
|
boolean hasOwnKeys = false;
|
|
|
|
boolean hasForeignKeys = false;
|
2015-09-06 13:08:42 +00:00
|
|
|
for(final String fingerprint : ownKeysToTrust.keySet()) {
|
2015-07-19 16:36:28 +00:00
|
|
|
hasOwnKeys = true;
|
2015-09-06 13:08:42 +00:00
|
|
|
addFingerprintRowWithListeners(ownKeys, contact.getAccount(), fingerprint, false,
|
|
|
|
XmppAxolotlSession.Trust.fromBoolean(ownKeysToTrust.get(fingerprint)), false,
|
2015-07-19 16:36:28 +00:00
|
|
|
new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
2015-09-06 13:08:42 +00:00
|
|
|
ownKeysToTrust.put(fingerprint, isChecked);
|
2015-08-05 20:22:37 +00:00
|
|
|
// own fingerprints have no impact on locked status.
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
},
|
2015-12-23 18:18:53 +00:00
|
|
|
null,
|
2015-07-19 16:36:28 +00:00
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2015-09-06 13:08:42 +00:00
|
|
|
for(final String fingerprint : foreignKeysToTrust.keySet()) {
|
2015-07-19 16:36:28 +00:00
|
|
|
hasForeignKeys = true;
|
2015-09-06 13:08:42 +00:00
|
|
|
addFingerprintRowWithListeners(foreignKeys, contact.getAccount(), fingerprint, false,
|
|
|
|
XmppAxolotlSession.Trust.fromBoolean(foreignKeysToTrust.get(fingerprint)), false,
|
2015-07-19 16:36:28 +00:00
|
|
|
new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
2015-09-06 13:08:42 +00:00
|
|
|
foreignKeysToTrust.put(fingerprint, isChecked);
|
2015-08-05 20:22:37 +00:00
|
|
|
lockOrUnlockAsNeeded();
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
},
|
2015-12-23 18:18:53 +00:00
|
|
|
null,
|
2015-07-19 16:36:28 +00:00
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hasOwnKeys) {
|
|
|
|
ownKeysTitle.setText(accountJid.toString());
|
|
|
|
ownKeysCard.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
if(hasForeignKeys) {
|
|
|
|
foreignKeysTitle.setText(contactJid.toString());
|
|
|
|
foreignKeysCard.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2015-10-16 21:48:42 +00:00
|
|
|
if(hasPendingKeyFetches()) {
|
2015-07-20 12:56:41 +00:00
|
|
|
setFetching();
|
|
|
|
lock();
|
|
|
|
} else {
|
2015-10-16 21:48:42 +00:00
|
|
|
if (!hasForeignKeys && hasNoOtherTrustedKeys()) {
|
2015-08-15 16:52:47 +00:00
|
|
|
keyErrorMessageCard.setVisibility(View.VISIBLE);
|
2016-01-13 11:19:56 +00:00
|
|
|
if (lastFetchReport == AxolotlService.FetchStatus.ERROR) {
|
|
|
|
keyErrorMessage.setText(R.string.error_no_keys_to_trust_server_error);
|
|
|
|
} else {
|
|
|
|
keyErrorMessage.setText(R.string.error_no_keys_to_trust);
|
|
|
|
}
|
2015-08-15 16:52:47 +00:00
|
|
|
ownKeys.removeAllViews(); ownKeysCard.setVisibility(View.GONE);
|
|
|
|
foreignKeys.removeAllViews(); foreignKeysCard.setVisibility(View.GONE);
|
|
|
|
}
|
2015-08-05 20:22:37 +00:00
|
|
|
lockOrUnlockAsNeeded();
|
2015-07-20 12:56:41 +00:00
|
|
|
setDone();
|
|
|
|
}
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 21:48:42 +00:00
|
|
|
private boolean reloadFingerprints() {
|
2015-10-17 12:09:26 +00:00
|
|
|
ownKeysToTrust.clear();
|
|
|
|
foreignKeysToTrust.clear();
|
2015-10-16 21:48:42 +00:00
|
|
|
AxolotlService service = this.mAccount.getAxolotlService();
|
|
|
|
Set<IdentityKey> ownKeysSet = service.getKeysWithTrust(XmppAxolotlSession.Trust.UNDECIDED);
|
|
|
|
Set<IdentityKey> foreignKeysSet = service.getKeysWithTrust(XmppAxolotlSession.Trust.UNDECIDED, contact);
|
2015-10-17 12:09:26 +00:00
|
|
|
if (hasNoOtherTrustedKeys() && ownKeysSet.size() == 0) {
|
2015-10-16 21:48:42 +00:00
|
|
|
foreignKeysSet.addAll(service.getKeysWithTrust(XmppAxolotlSession.Trust.UNTRUSTED, contact));
|
2015-07-20 20:35:07 +00:00
|
|
|
}
|
2015-07-19 16:36:28 +00:00
|
|
|
for(final IdentityKey identityKey : ownKeysSet) {
|
|
|
|
if(!ownKeysToTrust.containsKey(identityKey)) {
|
2015-09-06 13:08:42 +00:00
|
|
|
ownKeysToTrust.put(identityKey.getFingerprint().replaceAll("\\s", ""), false);
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for(final IdentityKey identityKey : foreignKeysSet) {
|
|
|
|
if(!foreignKeysToTrust.containsKey(identityKey)) {
|
2015-09-06 13:08:42 +00:00
|
|
|
foreignKeysToTrust.put(identityKey.getFingerprint().replaceAll("\\s", ""), false);
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-16 21:48:42 +00:00
|
|
|
return ownKeysSet.size() + foreignKeysSet.size() > 0;
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackendConnected() {
|
|
|
|
if ((accountJid != null) && (contactJid != null)) {
|
2015-10-16 21:48:42 +00:00
|
|
|
this.mAccount = xmppConnectionService.findAccountByJid(accountJid);
|
|
|
|
if (this.mAccount == null) {
|
2015-07-19 16:36:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-10-16 21:48:42 +00:00
|
|
|
this.contact = this.mAccount.getRoster().getContact(contactJid);
|
|
|
|
reloadFingerprints();
|
2015-07-19 16:36:28 +00:00
|
|
|
populateView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 21:48:42 +00:00
|
|
|
private boolean hasNoOtherTrustedKeys() {
|
|
|
|
return mAccount == null || mAccount.getAxolotlService().getNumTrustedKeys(contact) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean hasPendingKeyFetches() {
|
|
|
|
return mAccount != null && contact != null && mAccount.getAxolotlService().hasPendingKeyFetches(mAccount,contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-19 16:36:28 +00:00
|
|
|
@Override
|
2015-10-17 12:09:26 +00:00
|
|
|
public void onKeyStatusUpdated(final AxolotlService.FetchStatus report) {
|
|
|
|
if (report != null) {
|
2016-01-13 11:19:56 +00:00
|
|
|
lastFetchReport = report;
|
2015-10-17 12:09:26 +00:00
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
switch (report) {
|
|
|
|
case ERROR:
|
|
|
|
Toast.makeText(TrustKeysActivity.this,R.string.error_fetching_omemo_key,Toast.LENGTH_SHORT).show();
|
|
|
|
break;
|
|
|
|
case SUCCESS_VERIFIED:
|
|
|
|
Toast.makeText(TrustKeysActivity.this,R.string.verified_omemo_key_with_certificate,Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2015-10-16 21:48:42 +00:00
|
|
|
boolean keysToTrust = reloadFingerprints();
|
|
|
|
if (keysToTrust || hasPendingKeyFetches() || hasNoOtherTrustedKeys()) {
|
|
|
|
refreshUi();
|
|
|
|
} else {
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
finishOk();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void finishOk() {
|
|
|
|
Intent data = new Intent();
|
|
|
|
data.putExtra("choice", getIntent().getIntExtra("choice", ConversationActivity.ATTACHMENT_CHOICE_INVALID));
|
|
|
|
setResult(RESULT_OK, data);
|
|
|
|
finish();
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void commitTrusts() {
|
2015-09-06 13:08:42 +00:00
|
|
|
for(final String fingerprint :ownKeysToTrust.keySet()) {
|
2015-07-19 16:36:28 +00:00
|
|
|
contact.getAccount().getAxolotlService().setFingerprintTrust(
|
2015-09-06 13:08:42 +00:00
|
|
|
fingerprint,
|
|
|
|
XmppAxolotlSession.Trust.fromBoolean(ownKeysToTrust.get(fingerprint)));
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
2015-09-06 13:08:42 +00:00
|
|
|
for(final String fingerprint:foreignKeysToTrust.keySet()) {
|
2015-07-19 16:36:28 +00:00
|
|
|
contact.getAccount().getAxolotlService().setFingerprintTrust(
|
2015-09-06 13:08:42 +00:00
|
|
|
fingerprint,
|
|
|
|
XmppAxolotlSession.Trust.fromBoolean(foreignKeysToTrust.get(fingerprint)));
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void unlock() {
|
|
|
|
mSaveButton.setEnabled(true);
|
|
|
|
mSaveButton.setTextColor(getPrimaryTextColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void lock() {
|
|
|
|
mSaveButton.setEnabled(false);
|
|
|
|
mSaveButton.setTextColor(getSecondaryTextColor());
|
|
|
|
}
|
2015-07-20 12:56:41 +00:00
|
|
|
|
2015-08-05 20:22:37 +00:00
|
|
|
private void lockOrUnlockAsNeeded() {
|
2015-10-16 21:48:42 +00:00
|
|
|
if (hasNoOtherTrustedKeys() && !foreignKeysToTrust.values().contains(true)){
|
2015-08-05 20:22:37 +00:00
|
|
|
lock();
|
|
|
|
} else {
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-20 12:56:41 +00:00
|
|
|
private void setDone() {
|
|
|
|
mSaveButton.setText(getString(R.string.done));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setFetching() {
|
|
|
|
mSaveButton.setText(getString(R.string.fetching_keys));
|
|
|
|
}
|
2015-07-19 16:36:28 +00:00
|
|
|
}
|