Slight improvements to OpenPgp api connection state handling

This commit is contained in:
Vincent Breitmoser 2018-03-28 14:59:38 +02:00
parent fedf35815f
commit 0c2f9bfb31
3 changed files with 29 additions and 16 deletions

View file

@ -40,7 +40,6 @@ public class ComposeCryptoStatus {
return CryptoStatusDisplayType.UNCONFIGURED;
case UNINITIALIZED:
return CryptoStatusDisplayType.UNINITIALIZED;
case LOST_CONNECTION:
case ERROR:
case UI_REQUIRED:
return CryptoStatusDisplayType.ERROR;

View file

@ -659,7 +659,6 @@ public class RecipientPresenter {
recipientMvpView.launchUserInteractionPendingIntent(pendingIntent, OPENPGP_USER_INTERACTION);
break;
case LOST_CONNECTION:
case UNINITIALIZED:
case ERROR:
openPgpApiManager.refreshConnection();
@ -837,6 +836,9 @@ public class RecipientPresenter {
@Override
public void onOpenPgpProviderError(OpenPgpProviderError error) {
switch (error) {
case ConnectionLost:
openPgpApiManager.refreshConnection();
break;
case VersionIncompatible:
recipientMvpView.showErrorOpenPgpIncompatible();
break;

View file

@ -9,6 +9,7 @@ import android.arch.lifecycle.OnLifecycleEvent;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback;
@ -52,14 +53,14 @@ public class OpenPgpApiManager implements LifecycleObserver {
}
private void setupCryptoProvider() {
boolean providerIsBound = openPgpServiceConnection != null && openPgpServiceConnection.isBound();
if (providerIsBound) {
refreshConnection();
if (TextUtils.isEmpty(openPgpProvider)) {
setOpenPgpProviderState(OpenPgpProviderState.UNCONFIGURED);
return;
}
if (openPgpProvider == null) {
setOpenPgpProviderState(OpenPgpProviderState.UNCONFIGURED);
boolean providerIsBound = openPgpServiceConnection != null && openPgpServiceConnection.isBound();
if (providerIsBound) {
refreshConnection();
return;
}
@ -75,7 +76,7 @@ public class OpenPgpApiManager implements LifecycleObserver {
public void onError(Exception e) {
Timber.e(e, "error connecting to crypto provider!");
setOpenPgpProviderState(OpenPgpProviderState.ERROR);
callback.onOpenPgpProviderError(OpenPgpProviderError.ConnectionFailed);
callbackOpenPgpProviderError(OpenPgpProviderError.ConnectionFailed);
}
});
refreshConnection();
@ -86,18 +87,20 @@ public class OpenPgpApiManager implements LifecycleObserver {
(openPgpServiceConnection == null || !openPgpServiceConnection.isBound());
if (isOkStateButLostConnection) {
userInteractionPendingIntent = null;
setOpenPgpProviderState(OpenPgpProviderState.LOST_CONNECTION);
setOpenPgpProviderState(OpenPgpProviderState.ERROR);
callbackOpenPgpProviderError(OpenPgpProviderError.ConnectionLost);
return;
}
if (openPgpServiceConnection == null) {
userInteractionPendingIntent = null;
setOpenPgpProviderState(OpenPgpProviderState.UNCONFIGURED);
setupCryptoProvider();
return;
}
if (!openPgpServiceConnection.isBound()) {
userInteractionPendingIntent = null;
Timber.d("attempting to bind to openpgp provider: %s (%s)", openPgpProvider, openPgpServiceConnection);
openPgpServiceConnection.bindToService();
return;
}
@ -144,7 +147,7 @@ public class OpenPgpApiManager implements LifecycleObserver {
handleOpenPgpError(error);
} else {
setOpenPgpProviderState(OpenPgpProviderState.ERROR);
callback.onOpenPgpProviderError(OpenPgpProviderError.ConnectionFailed);
callbackOpenPgpProviderError(OpenPgpProviderError.ConnectionFailed);
}
break;
}
@ -153,8 +156,11 @@ public class OpenPgpApiManager implements LifecycleObserver {
private void setOpenPgpProviderState(OpenPgpProviderState state) {
boolean statusChanged = openPgpProviderState != state;
if (statusChanged) {
Timber.d("callback provider status changed from %s to %s", openPgpProviderState, state);
openPgpProviderState = state;
callback.onOpenPgpProviderStatusChanged();
if (callback != null) {
callback.onOpenPgpProviderStatusChanged();
}
}
}
@ -162,14 +168,21 @@ public class OpenPgpApiManager implements LifecycleObserver {
Timber.e("OpenPGP Api error: %s", error);
if (error != null && error.getErrorId() == OpenPgpError.INCOMPATIBLE_API_VERSIONS) {
callback.onOpenPgpProviderError(OpenPgpProviderError.VersionIncompatible);
callbackOpenPgpProviderError(OpenPgpProviderError.VersionIncompatible);
setOpenPgpProviderState(OpenPgpProviderState.UNCONFIGURED);
} else {
callback.onOpenPgpProviderError(OpenPgpProviderError.ConnectionFailed);
callbackOpenPgpProviderError(OpenPgpProviderError.ConnectionFailed);
setOpenPgpProviderState(OpenPgpProviderState.ERROR);
}
}
private void callbackOpenPgpProviderError(OpenPgpProviderError providerError) {
Timber.d("callback provider connection error %s", providerError);
if (callback != null) {
callback.onOpenPgpProviderError(providerError);
}
}
private void disconnect() {
openPgpApi = null;
if (openPgpServiceConnection != null) {
@ -192,14 +205,13 @@ public class OpenPgpApiManager implements LifecycleObserver {
public enum OpenPgpProviderState {
UNCONFIGURED,
UNINITIALIZED,
LOST_CONNECTION,
UI_REQUIRED,
ERROR,
OK
}
public enum OpenPgpProviderError {
ConnectionFailed, VersionIncompatible
ConnectionFailed, ConnectionLost, VersionIncompatible
}
public interface OpenPgpApiManagerCallback {