Simplify exception handling in service

This commit is contained in:
Dominik Schürmann 2013-09-16 13:08:02 +02:00
parent 363358d30b
commit 0e24a74bb8
2 changed files with 23 additions and 61 deletions

View File

@ -45,7 +45,7 @@ public class ExtendedApiService extends RemoteService {
} }
private void selfSignedX509CertSafe(String subjAltNameURI, IExtendedApiCallback callback, private void selfSignedX509CertSafe(String subjAltNameURI, IExtendedApiCallback callback,
AppSettings appSettings) throws RemoteException { AppSettings appSettings) {
// TODO: for pgp keyrings with password // TODO: for pgp keyrings with password
CallbackHandler pgpPwdCallbackHandler = new PgpToX509.PredefinedPasswordCallbackHandler(""); CallbackHandler pgpPwdCallbackHandler = new PgpToX509.PredefinedPasswordCallbackHandler("");
@ -77,7 +77,11 @@ public class ExtendedApiService extends RemoteService {
callback.onSuccess(outputBytes); callback.onSuccess(outputBytes);
} catch (Exception e) { } catch (Exception e) {
Log.e(Constants.TAG, "ExtendedApiService", e); Log.e(Constants.TAG, "ExtendedApiService", e);
callback.onError(e.getMessage()); try {
callback.onError(e.getMessage());
} catch (RemoteException e1) {
Log.e(Constants.TAG, "ExtendedApiService", e);
}
} }
// TODO: no private key at the moment! Don't give it to others // TODO: no private key at the moment! Don't give it to others
@ -104,19 +108,13 @@ public class ExtendedApiService extends RemoteService {
final AppSettings settings = getAppSettings(); final AppSettings settings = getAppSettings();
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
try { selfSignedX509CertSafe(subjAltNameURI, callback, settings);
selfSignedX509CertSafe(subjAltNameURI, callback, settings);
} catch (RemoteException e) {
Log.e(Constants.TAG, "OpenPgpService", e);
}
} }
}; };
checkAndEnqueue(r); checkAndEnqueue(r);
} }
}; };

View File

@ -55,23 +55,6 @@ import android.os.RemoteException;
public class OpenPgpService extends RemoteService { public class OpenPgpService extends RemoteService {
@Override
public void onCreate() {
super.onCreate();
Log.d(Constants.TAG, "OpenPgpService, onCreate()");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(Constants.TAG, "OpenPgpService, onDestroy()");
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private String getCachedPassphrase(long keyId, boolean allowUserInteraction) private String getCachedPassphrase(long keyId, boolean allowUserInteraction)
throws UserInteractionRequiredException { throws UserInteractionRequiredException {
String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), keyId); String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), keyId);
@ -234,7 +217,7 @@ public class OpenPgpService extends RemoteService {
private synchronized void encryptAndSignSafe(byte[] inputBytes, String[] encryptionUserIds, private synchronized void encryptAndSignSafe(byte[] inputBytes, String[] encryptionUserIds,
boolean asciiArmor, boolean allowUserInteraction, IOpenPgpCallback callback, boolean asciiArmor, boolean allowUserInteraction, IOpenPgpCallback callback,
AppSettings appSettings, boolean sign) throws RemoteException { AppSettings appSettings, boolean sign) {
try { try {
// build InputData and write into OutputStream // build InputData and write into OutputStream
InputStream inputStream = new ByteArrayInputStream(inputBytes); InputStream inputStream = new ByteArrayInputStream(inputBytes);
@ -285,10 +268,8 @@ public class OpenPgpService extends RemoteService {
// TODO: asciiArmor?! // TODO: asciiArmor?!
private void signSafe(byte[] inputBytes, boolean allowUserInteraction, private void signSafe(byte[] inputBytes, boolean allowUserInteraction,
IOpenPgpCallback callback, AppSettings appSettings) throws RemoteException { IOpenPgpCallback callback, AppSettings appSettings) {
try { try {
Log.d(Constants.TAG, "current therad id: " + Thread.currentThread().getId());
// build InputData and write into OutputStream // build InputData and write into OutputStream
InputStream inputStream = new ByteArrayInputStream(inputBytes); InputStream inputStream = new ByteArrayInputStream(inputBytes);
long inputLength = inputBytes.length; long inputLength = inputBytes.length;
@ -321,7 +302,7 @@ public class OpenPgpService extends RemoteService {
} }
private synchronized void decryptAndVerifySafe(byte[] inputBytes, boolean allowUserInteraction, private synchronized void decryptAndVerifySafe(byte[] inputBytes, boolean allowUserInteraction,
IOpenPgpCallback callback, AppSettings appSettings) throws RemoteException { IOpenPgpCallback callback, AppSettings appSettings) {
try { try {
// TODO: this is not really needed // TODO: this is not really needed
// checked if it is text with BEGIN and END tags // checked if it is text with BEGIN and END tags
@ -466,7 +447,8 @@ public class OpenPgpService extends RemoteService {
try { try {
callback.onError(new OpenPgpError(0, message)); callback.onError(new OpenPgpError(0, message));
} catch (Exception t) { } catch (Exception t) {
Log.e(Constants.TAG, "Error returning exception to client", t); Log.e(Constants.TAG,
"Exception while returning OpenPgpError to client via callback.onError()", t);
} }
} }
@ -476,19 +458,13 @@ public class OpenPgpService extends RemoteService {
public void encrypt(final byte[] inputBytes, final String[] encryptionUserIds, public void encrypt(final byte[] inputBytes, final String[] encryptionUserIds,
final boolean asciiArmor, final boolean allowUserInteraction, final boolean asciiArmor, final boolean allowUserInteraction,
final IOpenPgpCallback callback) throws RemoteException { final IOpenPgpCallback callback) throws RemoteException {
final AppSettings settings = getAppSettings(); final AppSettings settings = getAppSettings();
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
try { encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor,
encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor, allowUserInteraction, callback, settings, false);
allowUserInteraction, callback, settings, false);
} catch (RemoteException e) {
Log.e(Constants.TAG, "OpenPgpService", e);
}
} }
}; };
@ -499,19 +475,13 @@ public class OpenPgpService extends RemoteService {
public void signAndEncrypt(final byte[] inputBytes, final String[] encryptionUserIds, public void signAndEncrypt(final byte[] inputBytes, final String[] encryptionUserIds,
final boolean asciiArmor, final boolean allowUserInteraction, final boolean asciiArmor, final boolean allowUserInteraction,
final IOpenPgpCallback callback) throws RemoteException { final IOpenPgpCallback callback) throws RemoteException {
final AppSettings settings = getAppSettings(); final AppSettings settings = getAppSettings();
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
try { encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor,
encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor, allowUserInteraction, callback, settings, true);
allowUserInteraction, callback, settings, true);
} catch (RemoteException e) {
Log.e(Constants.TAG, "OpenPgpService", e);
}
} }
}; };
@ -525,19 +495,13 @@ public class OpenPgpService extends RemoteService {
final AppSettings settings = getAppSettings(); final AppSettings settings = getAppSettings();
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
try { signSafe(inputBytes, allowUserInteraction, callback, settings);
signSafe(inputBytes, allowUserInteraction, callback, settings);
} catch (RemoteException e) {
Log.e(Constants.TAG, "OpenPgpService", e);
}
} }
}; };
checkAndEnqueue(r); checkAndEnqueue(r);
} }
@Override @Override
@ -547,14 +511,9 @@ public class OpenPgpService extends RemoteService {
final AppSettings settings = getAppSettings(); final AppSettings settings = getAppSettings();
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
try { decryptAndVerifySafe(inputBytes, allowUserInteraction, callback, settings);
decryptAndVerifySafe(inputBytes, allowUserInteraction, callback, settings);
} catch (RemoteException e) {
Log.e(Constants.TAG, "OpenPgpService", e);
}
} }
}; };
@ -563,4 +522,9 @@ public class OpenPgpService extends RemoteService {
}; };
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
} }