From 0e24a74bb82bc0db720297046909145b61399857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 16 Sep 2013 13:08:02 +0200 Subject: Simplify exception handling in service --- .../service/remote/ExtendedApiService.java | 16 +++-- .../keychain/service/remote/OpenPgpService.java | 68 +++++----------------- 2 files changed, 23 insertions(+), 61 deletions(-) (limited to 'OpenPGP-Keychain/src/org/sufficientlysecure/keychain') diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/service/remote/ExtendedApiService.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/service/remote/ExtendedApiService.java index 4530179cb..427e6bb8f 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/service/remote/ExtendedApiService.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/service/remote/ExtendedApiService.java @@ -45,7 +45,7 @@ public class ExtendedApiService extends RemoteService { } private void selfSignedX509CertSafe(String subjAltNameURI, IExtendedApiCallback callback, - AppSettings appSettings) throws RemoteException { + AppSettings appSettings) { // TODO: for pgp keyrings with password CallbackHandler pgpPwdCallbackHandler = new PgpToX509.PredefinedPasswordCallbackHandler(""); @@ -77,7 +77,11 @@ public class ExtendedApiService extends RemoteService { callback.onSuccess(outputBytes); } catch (Exception 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 @@ -104,19 +108,13 @@ public class ExtendedApiService extends RemoteService { final AppSettings settings = getAppSettings(); Runnable r = new Runnable() { - @Override public void run() { - try { - selfSignedX509CertSafe(subjAltNameURI, callback, settings); - } catch (RemoteException e) { - Log.e(Constants.TAG, "OpenPgpService", e); - } + selfSignedX509CertSafe(subjAltNameURI, callback, settings); } }; checkAndEnqueue(r); - } }; diff --git a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java index f3916bdc6..a6fb78f71 100644 --- a/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java +++ b/OpenPGP-Keychain/src/org/sufficientlysecure/keychain/service/remote/OpenPgpService.java @@ -55,23 +55,6 @@ import android.os.RemoteException; 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) throws UserInteractionRequiredException { String passphrase = PassphraseCacheService.getCachedPassphrase(getContext(), keyId); @@ -234,7 +217,7 @@ public class OpenPgpService extends RemoteService { private synchronized void encryptAndSignSafe(byte[] inputBytes, String[] encryptionUserIds, boolean asciiArmor, boolean allowUserInteraction, IOpenPgpCallback callback, - AppSettings appSettings, boolean sign) throws RemoteException { + AppSettings appSettings, boolean sign) { try { // build InputData and write into OutputStream InputStream inputStream = new ByteArrayInputStream(inputBytes); @@ -285,10 +268,8 @@ public class OpenPgpService extends RemoteService { // TODO: asciiArmor?! private void signSafe(byte[] inputBytes, boolean allowUserInteraction, - IOpenPgpCallback callback, AppSettings appSettings) throws RemoteException { + IOpenPgpCallback callback, AppSettings appSettings) { try { - Log.d(Constants.TAG, "current therad id: " + Thread.currentThread().getId()); - // build InputData and write into OutputStream InputStream inputStream = new ByteArrayInputStream(inputBytes); long inputLength = inputBytes.length; @@ -321,7 +302,7 @@ public class OpenPgpService extends RemoteService { } private synchronized void decryptAndVerifySafe(byte[] inputBytes, boolean allowUserInteraction, - IOpenPgpCallback callback, AppSettings appSettings) throws RemoteException { + IOpenPgpCallback callback, AppSettings appSettings) { try { // TODO: this is not really needed // checked if it is text with BEGIN and END tags @@ -466,7 +447,8 @@ public class OpenPgpService extends RemoteService { try { callback.onError(new OpenPgpError(0, message)); } 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, final boolean asciiArmor, final boolean allowUserInteraction, final IOpenPgpCallback callback) throws RemoteException { - final AppSettings settings = getAppSettings(); Runnable r = new Runnable() { - @Override public void run() { - try { - encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor, - allowUserInteraction, callback, settings, false); - } catch (RemoteException e) { - Log.e(Constants.TAG, "OpenPgpService", e); - } + encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor, + allowUserInteraction, callback, settings, false); } }; @@ -499,19 +475,13 @@ public class OpenPgpService extends RemoteService { public void signAndEncrypt(final byte[] inputBytes, final String[] encryptionUserIds, final boolean asciiArmor, final boolean allowUserInteraction, final IOpenPgpCallback callback) throws RemoteException { - final AppSettings settings = getAppSettings(); Runnable r = new Runnable() { - @Override public void run() { - try { - encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor, - allowUserInteraction, callback, settings, true); - } catch (RemoteException e) { - Log.e(Constants.TAG, "OpenPgpService", e); - } + encryptAndSignSafe(inputBytes, encryptionUserIds, asciiArmor, + allowUserInteraction, callback, settings, true); } }; @@ -525,19 +495,13 @@ public class OpenPgpService extends RemoteService { final AppSettings settings = getAppSettings(); Runnable r = new Runnable() { - @Override public void run() { - try { - signSafe(inputBytes, allowUserInteraction, callback, settings); - } catch (RemoteException e) { - Log.e(Constants.TAG, "OpenPgpService", e); - } + signSafe(inputBytes, allowUserInteraction, callback, settings); } }; checkAndEnqueue(r); - } @Override @@ -547,14 +511,9 @@ public class OpenPgpService extends RemoteService { final AppSettings settings = getAppSettings(); Runnable r = new Runnable() { - @Override public void run() { - try { - decryptAndVerifySafe(inputBytes, allowUserInteraction, callback, settings); - } catch (RemoteException e) { - Log.e(Constants.TAG, "OpenPgpService", e); - } + decryptAndVerifySafe(inputBytes, allowUserInteraction, callback, settings); } }; @@ -563,4 +522,9 @@ public class OpenPgpService extends RemoteService { }; + @Override + public IBinder onBind(Intent intent) { + return mBinder; + } + } -- cgit v1.2.3