Get rid of more places where URIs are used for loading

This commit is contained in:
Vincent Breitmoser
2018-06-25 17:38:26 +02:00
parent 1425f34321
commit 4416ddf11c
15 changed files with 86 additions and 243 deletions

View File

@@ -29,7 +29,6 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
@@ -41,14 +40,12 @@ import android.widget.ViewAnimator;
import org.openintents.openpgp.OpenPgpDecryptionResult;
import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.util.OpenPgpUtils;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.keyimport.HkpKeyserverAddress;
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.KeyRepository;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
@@ -193,18 +190,14 @@ public abstract class DecryptFragment extends Fragment implements LoaderManager.
}
private void showKey(long keyId) {
try {
KeyRepository keyRepository = KeyRepository.create(requireContext());
long masterKeyId = keyRepository.getCachedPublicKeyRing(
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(keyId)
).getMasterKeyId();
Intent viewKeyIntent = ViewKeyActivity.getViewKeyActivityIntent(requireActivity(), masterKeyId);
startActivity(viewKeyIntent);
} catch (PgpKeyNotFoundException e) {
KeyRepository keyRepository = KeyRepository.create(requireContext());
Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(keyId);
if (masterKeyId == null) {
Notify.create(getActivity(), R.string.error_key_not_found, Style.ERROR).show();
return;
}
Intent viewKeyIntent = ViewKeyActivity.getViewKeyActivityIntent(requireActivity(), masterKeyId);
startActivity(viewKeyIntent);
}
protected void loadVerifyResult(DecryptVerifyResult decryptVerifyResult) {

View File

@@ -36,7 +36,6 @@ import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing;
import org.sufficientlysecure.keychain.provider.KeyRepository;
import org.sufficientlysecure.keychain.provider.KeyRepository.NotFoundException;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter.KeyItem;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.util.Notify;
@@ -139,8 +138,7 @@ public class EncryptModeAsymmetricFragment extends EncryptModeFragment {
private void preselectKeys(Long signatureKeyId, long[] encryptionKeyIds) {
if (signatureKeyId != null) {
try {
CachedPublicKeyRing keyring = mKeyRepository.getCachedPublicKeyRing(
KeyRings.buildUnifiedKeyRingUri(signatureKeyId));
CachedPublicKeyRing keyring = mKeyRepository.getCachedPublicKeyRing(signatureKeyId);
if (keyring.hasAnySecret()) {
mSignKeySpinner.setPreSelectedKeyId(signatureKeyId);
}

View File

@@ -285,10 +285,9 @@ public class PassphraseDialogActivity extends FragmentActivity {
} else {
long subKeyId = subKeyIds[0];
KeyRepository helper =
KeyRepository.create(getContext());
CachedPublicKeyRing cachedPublicKeyRing = helper.getCachedPublicKeyRing(
KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(subKeyId));
KeyRepository keyRepository = KeyRepository.create(getContext());
Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(subKeyId);
CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing(masterKeyId);
// yes the inner try/catch block is necessary, otherwise the final variable
// above can't be statically verified to have been set in all cases because
// the catch clause doesn't return.
@@ -534,11 +533,13 @@ public class PassphraseDialogActivity extends FragmentActivity {
CanonicalizedSecretKey canonicalizedSecretKey = null;
for (long subKeyId : mRequiredInput.getSubKeyIds()) {
CanonicalizedSecretKeyRing secretKeyRing =
KeyRepository.create(getContext()).getCanonicalizedSecretKeyRing(
KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(subKeyId));
CanonicalizedSecretKey secretKeyToUnlock =
secretKeyRing.getSecretKey(subKeyId);
KeyRepository keyRepository = KeyRepository.create(getContext());
Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(subKeyId);
if (masterKeyId == null) {
continue;
}
CanonicalizedSecretKeyRing secretKeyRing = keyRepository.getCanonicalizedSecretKeyRing(masterKeyId);
CanonicalizedSecretKey secretKeyToUnlock = secretKeyRing.getSecretKey(subKeyId);
// this is the operation may take a very long time (100ms to several seconds!)
boolean unlockSucceeded = secretKeyToUnlock.unlock(passphrase);

View File

@@ -199,12 +199,11 @@ public class SecurityTokenOperationActivity extends BaseSecurityTokenActivity {
throw new IOException(getString(R.string.error_wrong_security_token));
}
KeyRepository keyRepository =
KeyRepository.create(this);
KeyRepository keyRepository = KeyRepository.create(this);
CanonicalizedPublicKeyRing publicKeyRing;
try {
publicKeyRing = keyRepository.getCanonicalizedPublicKeyRing(
KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(mRequiredInput.getMasterKeyId()));
Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(mRequiredInput.getMasterKeyId());
publicKeyRing = keyRepository.getCanonicalizedPublicKeyRing(masterKeyId);
} catch (KeyRepository.NotFoundException e) {
throw new IOException("Couldn't find subkey for key to token operation.");
}
@@ -263,9 +262,8 @@ public class SecurityTokenOperationActivity extends BaseSecurityTokenActivity {
KeyRepository.create(this);
CanonicalizedSecretKeyRing secretKeyRing;
try {
secretKeyRing = keyRepository.getCanonicalizedSecretKeyRing(
KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(mRequiredInput.getMasterKeyId())
);
Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(mRequiredInput.getMasterKeyId());
secretKeyRing = keyRepository.getCanonicalizedSecretKeyRing(masterKeyId);
} catch (KeyRepository.NotFoundException e) {
throw new IOException("Couldn't find subkey for key to token operation.");
}

View File

@@ -115,11 +115,13 @@ public abstract class PublicKeyRetrievalLoader extends AsyncTaskLoader<KeyRetrie
log.add(LogType.MSG_RET_LOCAL_SEARCH, 1, KeyFormattingUtils.convertKeyIdToHex(keyId));
try {
CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing(
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(keyId)
);
Long masterKeyId = keyRepository.getMasterKeyIdBySubkeyId(keyId);
if (masterKeyId == null) {
log.add(LogType.MSG_RET_LOCAL_NOT_FOUND, 2);
continue;
}
CachedPublicKeyRing cachedPublicKeyRing = keyRepository.getCachedPublicKeyRing(masterKeyId);
long masterKeyId = cachedPublicKeyRing.getMasterKeyId();
// TODO check fingerprint
// if (!Arrays.equals(fingerprints, cachedPublicKeyRing.getFingerprint())) {
// log.add(LogType.MSG_RET_LOCAL_FP_MISMATCH, 1);
@@ -147,7 +149,7 @@ public abstract class PublicKeyRetrievalLoader extends AsyncTaskLoader<KeyRetrie
throw new IllegalStateException("Unhandled SecretKeyType!");
}
}
} catch (PgpKeyNotFoundException | NotFoundException e) {
} catch (NotFoundException e) {
log.add(LogType.MSG_RET_LOCAL_NOT_FOUND, 2);
}
}