diff --git a/src/org/thialfihar/android/apg/ApgService.java b/src/org/thialfihar/android/apg/ApgService.java index 9cdc5099d..bb5b6463a 100644 --- a/src/org/thialfihar/android/apg/ApgService.java +++ b/src/org/thialfihar/android/apg/ApgService.java @@ -4,6 +4,10 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import android.content.Intent; import android.os.IBinder; @@ -20,7 +24,10 @@ public class ApgService extends Service { private final IApgService.Stub mBinder = new IApgService.Stub() { - public String encrypt_with_passphrase(String msg, String passphrase) { + public String encrypt_with_passphrase(List args) { + String msg = args.remove(0); + String passphrase = args.remove(0); + Preferences mPreferences = Preferences.getPreferences(getBaseContext(), true); InputStream inStream = new ByteArrayInputStream(msg.getBytes()); InputData in = new InputData(inStream, 9999); @@ -29,17 +36,16 @@ public class ApgService extends Service { Apg.initialize(getApplicationContext()); try { - Apg.encrypt( - getApplicationContext(), - in, - out, + Apg.encrypt(getApplicationContext(), // context + in, // input stream + out, // output stream true, // armored enc_keys, // encryption keys 0, // signature key null, // signature passphrase null, // progress - mPreferences.getDefaultEncryptionAlgorithm(), - mPreferences.getDefaultHashAlgorithm(), + mPreferences.getDefaultEncryptionAlgorithm(), // encryption + mPreferences.getDefaultHashAlgorithm(), // hash Id.choice.compression.none, // compression false, // mPreferences.getForceV3Signatures(), passphrase // passPhrase @@ -53,12 +59,13 @@ public class ApgService extends Service { return out.toString(); } - public String decrypt_with_passphrase(String encrypted_msg, - String passphrase) { - InputStream inStream = new ByteArrayInputStream(encrypted_msg - .getBytes()); + public String decrypt_with_passphrase(List args) { + String encrypted_msg = args.remove(0); + String passphrase = args.remove(0); + + InputStream inStream = new ByteArrayInputStream(encrypted_msg.getBytes()); InputData in = new InputData(inStream, 9999); // XXX what size in - // second parameter? + // second parameter? OutputStream out = new ByteArrayOutputStream(); try { Apg.decrypt(getApplicationContext(), in, out, passphrase, null, // progress diff --git a/src/org/thialfihar/android/apg/IApgService.aidl b/src/org/thialfihar/android/apg/IApgService.aidl index 6097d4ce4..65d2653d5 100644 --- a/src/org/thialfihar/android/apg/IApgService.aidl +++ b/src/org/thialfihar/android/apg/IApgService.aidl @@ -1,6 +1,6 @@ package org.thialfihar.android.apg; interface IApgService { - String encrypt_with_passphrase(String msg, String passphrase); - String decrypt_with_passphrase(String encrypted_msg, String passphrase); + String encrypt_with_passphrase(in List params); + String decrypt_with_passphrase(in List params); } \ No newline at end of file diff --git a/src/org/thialfihar/android/apg/utils/ApgCon.java b/src/org/thialfihar/android/apg/utils/ApgCon.java new file mode 100644 index 000000000..f6ace207a --- /dev/null +++ b/src/org/thialfihar/android/apg/utils/ApgCon.java @@ -0,0 +1,144 @@ +package org.thialfihar.android.apg.utils; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import android.content.Context; +import android.content.ComponentName; +import android.content.ServiceConnection; +import android.content.Intent; +import android.os.IBinder; +import android.util.Log; + +import org.thialfihar.android.apg.IApgService; + +/** + * This class can be used by other projects to simplify connecting to the + * APG-Service. Kind of wrapper of for AIDL. + * + * It is not used in this project. + */ +public class ApgCon { + + private String TAG = "ApgCon"; + + private final Context mContext; + + /** Remote service for decrypting and encrypting data */ + private IApgService apgService = null; + + /** Set apgService accordingly to connection status */ + private ServiceConnection apgConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName className, IBinder service) { + Log.d(TAG, "IApgService bound to apgService"); + apgService = IApgService.Stub.asInterface(service); + } + + public void onServiceDisconnected(ComponentName className) { + Log.d(TAG, "IApgService disconnected"); + apgService = null; + } + }; + + /** Possible fields which are returned to the application */ + public static enum retKey { + ERROR, // error enum, see below + ERROR_DESC, // human readable description + RESULT, // if everything went fine, result + } + + public static enum error { + GENERIC, // no special type + CANNOT_BIND_TO_APG, // connection to apg service not possible + CALL_MISSING, // function to call not provided + CALL_NOT_KNOWN, // apg service does not know what to do + } + + public ApgCon(Context ctx) { + Log.d(TAG, "EncryptionService created"); + mContext = ctx; + } + + /** try to connect to the apg service */ + private boolean connect() { + Log.d(TAG, "trying to bind the apgService to context"); + + if (apgService != null) { + Log.d(TAG, "allready connected"); + return true; + } + + try { + mContext.bindService(new Intent(IApgService.class.getName()), apgConnection, Context.BIND_AUTO_CREATE); + } catch (Exception e) { + Log.d(TAG, "could not bind APG service"); + return false; + } + + int wait_count = 0; + while (apgService == null && wait_count++ < 15) { + Log.d(TAG, "sleeping 1 second to wait for apg"); + android.os.SystemClock.sleep(1000); + } + ; + + if (wait_count >= 15) { + Log.d(TAG, "slept waiting for nothing!"); + return false; + } + + return true; + } + + private boolean initialize() { + if (apgService == null) { + if (!connect()) { + Log.d(TAG, "connection to apg service failed"); + return false; + } + } + return true; + } + + public boolean call(String function, Map return_map, String... function_params) { + return_map.put(retKey.ERROR, null); + + if (!initialize()) { + return_map.put(retKey.ERROR, error.CANNOT_BIND_TO_APG); + return false; + } + + if (function == null || function.length() == 0) { + return_map.put(retKey.ERROR, error.CALL_MISSING); + return false; + } + + try { + List params_list = Arrays.asList(function_params); + return_map.put(retKey.RESULT, IApgService.class.getMethod(function, List.class).invoke(apgService, params_list)); + } catch (NoSuchMethodException e) { + Log.d(TAG, e.getMessage()); + return_map.put(retKey.ERROR, error.CALL_NOT_KNOWN); + return_map.put(retKey.ERROR_DESC, e.getMessage()); + return false; + } catch (Exception e) { + Log.d(TAG, e.getMessage()); + return_map.put(retKey.ERROR, error.GENERIC); + return_map.put(retKey.ERROR_DESC, e.getMessage()); + return false; + } + + return true; + + } + + private void disconnect() { + Log.d(TAG, "disconnecting apgService"); + if (apgService != null) { + mContext.unbindService(apgConnection); + apgService = null; + } + } + +}