mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-23 17:22:16 -05:00
Cleanup of code, AIDL-calls renamed!
This commit is contained in:
parent
6502e58c26
commit
24205b8dbc
@ -40,7 +40,7 @@ public class ApgService extends Service {
|
||||
PRIVATE_KEY_PASSPHRASE_WRONG,
|
||||
PRIVATE_KEY_PASSPHRASE_MISSING;
|
||||
|
||||
public int shifted_ordinal() {
|
||||
public int shiftedOrdinal() {
|
||||
return ordinal() + 100;
|
||||
}
|
||||
}
|
||||
@ -90,7 +90,6 @@ public class ApgService extends Service {
|
||||
args = new HashSet<arg>();
|
||||
args.add(arg.KEY_TYPE);
|
||||
FUNCTIONS_REQUIRED_ARGS.put("get_keys", args);
|
||||
|
||||
}
|
||||
|
||||
/** optional arguments for each AIDL function */
|
||||
@ -124,21 +123,7 @@ public class ApgService extends Service {
|
||||
FUNCTIONS_DEFAULTS.put(arg.COMPRESSION, "getDefaultMessageCompression");
|
||||
}
|
||||
|
||||
/** a map the default functions to their return types */
|
||||
private static final HashMap<String, Class<?>> FUNCTIONS_DEFAULTS_TYPES = new HashMap<String, Class<?>>();
|
||||
static {
|
||||
try {
|
||||
FUNCTIONS_DEFAULTS_TYPES.put("getDefaultEncryptionAlgorithm", Preferences.class.getMethod("getDefaultEncryptionAlgorithm").getReturnType());
|
||||
FUNCTIONS_DEFAULTS_TYPES.put("getDefaultHashAlgorithm", Preferences.class.getMethod("getDefaultHashAlgorithm").getReturnType());
|
||||
FUNCTIONS_DEFAULTS_TYPES.put("getDefaultAsciiArmour", Preferences.class.getMethod("getDefaultAsciiArmour").getReturnType());
|
||||
FUNCTIONS_DEFAULTS_TYPES.put("getForceV3Signatures", Preferences.class.getMethod("getForceV3Signatures").getReturnType());
|
||||
FUNCTIONS_DEFAULTS_TYPES.put("getDefaultMessageCompression", Preferences.class.getMethod("getDefaultMessageCompression").getReturnType());
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Function default exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** a map the default function names to their method */
|
||||
/** a map of the default function names to their method */
|
||||
private static final HashMap<String, Method> FUNCTIONS_DEFAULTS_METHODS = new HashMap<String, Method>();
|
||||
static {
|
||||
try {
|
||||
@ -152,44 +137,45 @@ public class ApgService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* maps a fingerprint or user id of a key to as master key in database
|
||||
*
|
||||
* @param search_key
|
||||
* fingerprint or user id to search for
|
||||
* @return master key if found, or 0
|
||||
*/
|
||||
private static long get_master_key(String search_key, Bundle pReturn) {
|
||||
if (search_key == null || search_key.length() != 8) {
|
||||
return 0;
|
||||
}
|
||||
ArrayList<String> tmp = new ArrayList<String>();
|
||||
tmp.add(search_key);
|
||||
long[] _keys = get_master_key(tmp, pReturn);
|
||||
if (_keys.length > 0)
|
||||
return _keys[0];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static Cursor get_key_entries(HashMap<String, Object> params) {
|
||||
private static Cursor getKeyEntries(HashMap<String, Object> pParams) {
|
||||
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
|
||||
qb.setTables(KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " + "(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + Keys.TABLE_NAME
|
||||
+ "." + Keys.KEY_RING_ID + " AND " + Keys.TABLE_NAME + "." + Keys.IS_MASTER_KEY + " = '1'" + ") " + " INNER JOIN " + UserIds.TABLE_NAME
|
||||
+ " ON " + "(" + Keys.TABLE_NAME + "." + Keys._ID + " = " + UserIds.TABLE_NAME + "." + UserIds.KEY_ID + " AND " + UserIds.TABLE_NAME + "."
|
||||
+ UserIds.RANK + " = '0') ");
|
||||
|
||||
String orderBy = params.containsKey("order_by") ? (String) params.get("order_by") : UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC";
|
||||
String orderBy = pParams.containsKey("order_by") ? (String) pParams.get("order_by") : UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC";
|
||||
|
||||
String type_val[] = null;
|
||||
String type_where = null;
|
||||
if (params.containsKey("key_type")) {
|
||||
type_where = KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?";
|
||||
type_val = new String[] {
|
||||
"" + params.get("key_type")
|
||||
String typeVal[] = null;
|
||||
String typeWhere = null;
|
||||
if (pParams.containsKey("key_type")) {
|
||||
typeWhere = KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?";
|
||||
typeVal = new String[] {
|
||||
"" + pParams.get("key_type")
|
||||
};
|
||||
}
|
||||
return qb.query(Apg.getDatabase().db(), (String[]) params.get("columns"), type_where, type_val, null, null, orderBy);
|
||||
return qb.query(Apg.getDatabase().db(), (String[]) pParams.get("columns"), typeWhere, typeVal, null, null, orderBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* maps a fingerprint or user id of a key to a master key in database
|
||||
*
|
||||
* @param search_key
|
||||
* fingerprint or user id to search for
|
||||
* @return master key if found, or 0
|
||||
*/
|
||||
private static long getMasterKey(String pSearchKey, Bundle pReturn) {
|
||||
if (pSearchKey == null || pSearchKey.length() != 8) {
|
||||
return 0;
|
||||
}
|
||||
ArrayList<String> keyList = new ArrayList<String>();
|
||||
keyList.add(pSearchKey);
|
||||
long[] keys = getMasterKey(keyList, pReturn);
|
||||
if (keys.length > 0) {
|
||||
return keys[0];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -200,7 +186,7 @@ public class ApgService extends Service {
|
||||
* database
|
||||
* @return an array of master keys
|
||||
*/
|
||||
private static long[] get_master_key(ArrayList<String> search_keys, Bundle pReturn) {
|
||||
private static long[] getMasterKey(ArrayList<String> pSearchKeys, Bundle pReturn) {
|
||||
|
||||
HashMap<String, Object> qParams = new HashMap<String, Object>();
|
||||
qParams.put("columns", new String[] {
|
||||
@ -209,30 +195,30 @@ public class ApgService extends Service {
|
||||
});
|
||||
qParams.put("key_type", Id.database.type_public);
|
||||
|
||||
Cursor mCursor = get_key_entries(qParams);
|
||||
Cursor mCursor = getKeyEntries(qParams);
|
||||
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "going through installed user keys");
|
||||
ArrayList<Long> _master_keys = new ArrayList<Long>();
|
||||
ArrayList<Long> masterKeys = new ArrayList<Long>();
|
||||
while (mCursor.moveToNext()) {
|
||||
long _cur_mkey = mCursor.getLong(0);
|
||||
String _cur_user = mCursor.getString(1);
|
||||
long curMkey = mCursor.getLong(0);
|
||||
String curUser = mCursor.getString(1);
|
||||
|
||||
String _cur_fprint = Apg.getSmallFingerPrint(_cur_mkey);
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "current user: " + _cur_user + " (" + _cur_fprint + ")");
|
||||
if (search_keys.contains(_cur_fprint) || search_keys.contains(_cur_user)) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "master key found for: " + _cur_fprint);
|
||||
_master_keys.add(_cur_mkey);
|
||||
search_keys.remove(_cur_fprint);
|
||||
String curFprint = Apg.getSmallFingerPrint(curMkey);
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "current user: " + curUser + " (" + curFprint + ")");
|
||||
if (pSearchKeys.contains(curFprint) || pSearchKeys.contains(curUser)) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "master key found for: " + curFprint);
|
||||
masterKeys.add(curMkey);
|
||||
pSearchKeys.remove(curFprint);
|
||||
} else {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Installed key " + _cur_fprint + " is not in the list of public keys to encrypt with");
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Installed key " + curFprint + " is not in the list of public keys to encrypt with");
|
||||
}
|
||||
}
|
||||
mCursor.close();
|
||||
|
||||
long[] _master_longs = new long[_master_keys.size()];
|
||||
long[] masterKeyLongs = new long[masterKeys.size()];
|
||||
int i = 0;
|
||||
for (Long _key : _master_keys) {
|
||||
_master_longs[i++] = _key;
|
||||
for (Long key : masterKeys) {
|
||||
masterKeyLongs[i++] = key;
|
||||
}
|
||||
|
||||
if (i == 0) {
|
||||
@ -240,12 +226,12 @@ public class ApgService extends Service {
|
||||
pReturn.getStringArrayList(ret.WARNINGS.name()).add("Searched for public key(s) but found not one");
|
||||
}
|
||||
|
||||
for (String _key : search_keys) {
|
||||
Log.w(TAG, "Searched for key " + _key + " but cannot find it in APG");
|
||||
pReturn.getStringArrayList(ret.WARNINGS.name()).add("Searched for key " + _key + " but cannot find it in APG");
|
||||
for (String key : pSearchKeys) {
|
||||
Log.w(TAG, "Searched for key " + key + " but cannot find it in APG");
|
||||
pReturn.getStringArrayList(ret.WARNINGS.name()).add("Searched for key " + key + " but cannot find it in APG");
|
||||
}
|
||||
|
||||
return _master_longs;
|
||||
return masterKeyLongs;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -254,27 +240,27 @@ public class ApgService extends Service {
|
||||
* @param args
|
||||
* the bundle to add default parameters to if missing
|
||||
*/
|
||||
private void add_default_arguments(String call, Bundle args) {
|
||||
private void addDefaultArguments(String pCall, Bundle pArgs) {
|
||||
// check whether there are optional elements defined for that call
|
||||
if (FUNCTIONS_OPTIONAL_ARGS.containsKey(call)) {
|
||||
Preferences _mPreferences = Preferences.getPreferences(getBaseContext(), true);
|
||||
if (FUNCTIONS_OPTIONAL_ARGS.containsKey(pCall)) {
|
||||
Preferences preferences = Preferences.getPreferences(getBaseContext(), true);
|
||||
|
||||
Iterator<arg> _iter = FUNCTIONS_DEFAULTS.keySet().iterator();
|
||||
while (_iter.hasNext()) {
|
||||
arg _current_arg = _iter.next();
|
||||
String _current_key = _current_arg.name();
|
||||
if (!args.containsKey(_current_key) && FUNCTIONS_OPTIONAL_ARGS.get(call).contains(_current_arg)) {
|
||||
String _current_function_name = FUNCTIONS_DEFAULTS.get(_current_arg);
|
||||
Iterator<arg> iter = FUNCTIONS_DEFAULTS.keySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
arg currentArg = iter.next();
|
||||
String currentKey = currentArg.name();
|
||||
if (!pArgs.containsKey(currentKey) && FUNCTIONS_OPTIONAL_ARGS.get(pCall).contains(currentArg)) {
|
||||
String currentFunctionName = FUNCTIONS_DEFAULTS.get(currentArg);
|
||||
try {
|
||||
Class<?> _ret_type = FUNCTIONS_DEFAULTS_TYPES.get(_current_function_name);
|
||||
if (_ret_type == String.class) {
|
||||
args.putString(_current_key, (String) FUNCTIONS_DEFAULTS_METHODS.get(_current_function_name).invoke(_mPreferences));
|
||||
} else if (_ret_type == boolean.class) {
|
||||
args.putBoolean(_current_key, (Boolean) FUNCTIONS_DEFAULTS_METHODS.get(_current_function_name).invoke(_mPreferences));
|
||||
} else if (_ret_type == int.class) {
|
||||
args.putInt(_current_key, (Integer) FUNCTIONS_DEFAULTS_METHODS.get(_current_function_name).invoke(_mPreferences));
|
||||
Class<?> returnType = FUNCTIONS_DEFAULTS_METHODS.get(currentFunctionName).getReturnType();
|
||||
if (returnType == String.class) {
|
||||
pArgs.putString(currentKey, (String) FUNCTIONS_DEFAULTS_METHODS.get(currentFunctionName).invoke(preferences));
|
||||
} else if (returnType == boolean.class) {
|
||||
pArgs.putBoolean(currentKey, (Boolean) FUNCTIONS_DEFAULTS_METHODS.get(currentFunctionName).invoke(preferences));
|
||||
} else if (returnType == int.class) {
|
||||
pArgs.putInt(currentKey, (Integer) FUNCTIONS_DEFAULTS_METHODS.get(currentFunctionName).invoke(preferences));
|
||||
} else {
|
||||
Log.e(TAG, "Unknown return type " + _ret_type.toString() + " for default option");
|
||||
Log.e(TAG, "Unknown return type " + returnType.toString() + " for default option");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Exception in add_default_arguments " + e.getMessage());
|
||||
@ -290,7 +276,7 @@ public class ApgService extends Service {
|
||||
* @param pReturn
|
||||
* the Bundle to update
|
||||
*/
|
||||
private void add_default_returns(Bundle pReturn) {
|
||||
private void addDefaultReturns(Bundle pReturn) {
|
||||
ArrayList<String> errors = new ArrayList<String>();
|
||||
ArrayList<String> warnings = new ArrayList<String>();
|
||||
|
||||
@ -308,13 +294,13 @@ public class ApgService extends Service {
|
||||
* @param pReturn
|
||||
* the bundle to write errors to
|
||||
*/
|
||||
private void check_required_args(String function, Bundle pArgs, Bundle pReturn) {
|
||||
if (FUNCTIONS_REQUIRED_ARGS.containsKey(function)) {
|
||||
Iterator<arg> _iter = FUNCTIONS_REQUIRED_ARGS.get(function).iterator();
|
||||
while (_iter.hasNext()) {
|
||||
String _cur_arg = _iter.next().name();
|
||||
if (!pArgs.containsKey(_cur_arg)) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Argument missing: " + _cur_arg);
|
||||
private void checkForRequiredArgs(String pFunction, Bundle pArgs, Bundle pReturn) {
|
||||
if (FUNCTIONS_REQUIRED_ARGS.containsKey(pFunction)) {
|
||||
Iterator<arg> iter = FUNCTIONS_REQUIRED_ARGS.get(pFunction).iterator();
|
||||
while (iter.hasNext()) {
|
||||
String curArg = iter.next().name();
|
||||
if (!pArgs.containsKey(curArg)) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Argument missing: " + curArg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -330,60 +316,60 @@ public class ApgService extends Service {
|
||||
* @param pReturn
|
||||
* the bundle to write warnings to
|
||||
*/
|
||||
private void check_unknown_args(String function, Bundle pArgs, Bundle pReturn) {
|
||||
private void checkForUnknownArgs(String pFunction, Bundle pArgs, Bundle pReturn) {
|
||||
|
||||
HashSet<arg> all_args = new HashSet<arg>();
|
||||
if (FUNCTIONS_REQUIRED_ARGS.containsKey(function)) {
|
||||
all_args.addAll(FUNCTIONS_REQUIRED_ARGS.get(function));
|
||||
HashSet<arg> allArgs = new HashSet<arg>();
|
||||
if (FUNCTIONS_REQUIRED_ARGS.containsKey(pFunction)) {
|
||||
allArgs.addAll(FUNCTIONS_REQUIRED_ARGS.get(pFunction));
|
||||
}
|
||||
if (FUNCTIONS_OPTIONAL_ARGS.containsKey(function)) {
|
||||
all_args.addAll(FUNCTIONS_OPTIONAL_ARGS.get(function));
|
||||
if (FUNCTIONS_OPTIONAL_ARGS.containsKey(pFunction)) {
|
||||
allArgs.addAll(FUNCTIONS_OPTIONAL_ARGS.get(pFunction));
|
||||
}
|
||||
|
||||
ArrayList<String> _unknown_args = new ArrayList<String>();
|
||||
Iterator<String> _iter = pArgs.keySet().iterator();
|
||||
while (_iter.hasNext()) {
|
||||
String _cur_key = _iter.next();
|
||||
ArrayList<String> unknownArgs = new ArrayList<String>();
|
||||
Iterator<String> iter = pArgs.keySet().iterator();
|
||||
while (iter.hasNext()) {
|
||||
String curKey = iter.next();
|
||||
try {
|
||||
arg _cur_arg = arg.valueOf(_cur_key);
|
||||
if (!all_args.contains(_cur_arg)) {
|
||||
pReturn.getStringArrayList(ret.WARNINGS.name()).add("Unknown argument: " + _cur_key);
|
||||
_unknown_args.add(_cur_key);
|
||||
arg curArg = arg.valueOf(curKey);
|
||||
if (!allArgs.contains(curArg)) {
|
||||
pReturn.getStringArrayList(ret.WARNINGS.name()).add("Unknown argument: " + curKey);
|
||||
unknownArgs.add(curKey);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
pReturn.getStringArrayList(ret.WARNINGS.name()).add("Unknown argument: " + _cur_key);
|
||||
_unknown_args.add(_cur_key);
|
||||
pReturn.getStringArrayList(ret.WARNINGS.name()).add("Unknown argument: " + curKey);
|
||||
unknownArgs.add(curKey);
|
||||
}
|
||||
}
|
||||
|
||||
// remove unknown arguments so our bundle has just what we need
|
||||
for (String _arg : _unknown_args) {
|
||||
pArgs.remove(_arg);
|
||||
for (String arg : unknownArgs) {
|
||||
pArgs.remove(arg);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean prepare_args(String call, Bundle pArgs, Bundle pReturn) {
|
||||
private boolean prepareArgs(String pCall, Bundle pArgs, Bundle pReturn) {
|
||||
Apg.initialize(getBaseContext());
|
||||
|
||||
/* add default return values for all functions */
|
||||
add_default_returns(pReturn);
|
||||
addDefaultReturns(pReturn);
|
||||
|
||||
/* add default arguments if missing */
|
||||
add_default_arguments(call, pArgs);
|
||||
addDefaultArguments(pCall, pArgs);
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "add_default_arguments");
|
||||
|
||||
/* check for required arguments */
|
||||
check_required_args(call, pArgs, pReturn);
|
||||
checkForRequiredArgs(pCall, pArgs, pReturn);
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "check_required_args");
|
||||
|
||||
/* check for unknown arguments and add to warning if found */
|
||||
check_unknown_args(call, pArgs, pReturn);
|
||||
checkForUnknownArgs(pCall, pArgs, pReturn);
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "check_unknown_args");
|
||||
|
||||
/* return if errors happened */
|
||||
if (pReturn.getStringArrayList(ret.ERRORS.name()).size() != 0) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Errors after preparing, not executing "+call);
|
||||
pReturn.putInt(ret.ERROR.name(), error.ARGUMENTS_MISSING.shifted_ordinal());
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Errors after preparing, not executing "+pCall);
|
||||
pReturn.putInt(ret.ERROR.name(), error.ARGUMENTS_MISSING.shiftedOrdinal());
|
||||
return false;
|
||||
}
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "error return");
|
||||
@ -393,30 +379,30 @@ public class ApgService extends Service {
|
||||
|
||||
private boolean encrypt(Bundle pArgs, Bundle pReturn) {
|
||||
|
||||
long _pub_master_keys[] = {};
|
||||
long pubMasterKeys[] = {};
|
||||
if (pArgs.containsKey(arg.PUBLIC_KEYS.name())) {
|
||||
ArrayList<String> _list = pArgs.getStringArrayList(arg.PUBLIC_KEYS.name());
|
||||
ArrayList<String> _pub_keys = new ArrayList<String>();
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Long size: " + _list.size());
|
||||
Iterator<String> _iter = _list.iterator();
|
||||
while (_iter.hasNext()) {
|
||||
_pub_keys.add(_iter.next());
|
||||
ArrayList<String> list = pArgs.getStringArrayList(arg.PUBLIC_KEYS.name());
|
||||
ArrayList<String> pubKeys = new ArrayList<String>();
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Long size: " + list.size());
|
||||
Iterator<String> iter = list.iterator();
|
||||
while (iter.hasNext()) {
|
||||
pubKeys.add(iter.next());
|
||||
}
|
||||
_pub_master_keys = get_master_key(_pub_keys, pReturn);
|
||||
pubMasterKeys = getMasterKey(pubKeys, pReturn);
|
||||
}
|
||||
|
||||
InputStream _inStream = new ByteArrayInputStream(pArgs.getString(arg.MESSAGE.name()).getBytes());
|
||||
InputData _in = new InputData(_inStream, 0); // XXX Size second param?
|
||||
InputStream inStream = new ByteArrayInputStream(pArgs.getString(arg.MESSAGE.name()).getBytes());
|
||||
InputData in = new InputData(inStream, 0); // XXX Size second param?
|
||||
|
||||
OutputStream _out = new ByteArrayOutputStream();
|
||||
OutputStream out = new ByteArrayOutputStream();
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "About to encrypt");
|
||||
try {
|
||||
Apg.encrypt(getBaseContext(), // context
|
||||
_in, // input stream
|
||||
_out, // output stream
|
||||
in, // input stream
|
||||
out, // output stream
|
||||
pArgs.getBoolean(arg.ARMORED_OUTPUT.name()), // ARMORED_OUTPUT
|
||||
_pub_master_keys, // encryption keys
|
||||
get_master_key(pArgs.getString(arg.SIGNATURE_KEY.name()), pReturn), // signature key
|
||||
pubMasterKeys, // encryption keys
|
||||
getMasterKey(pArgs.getString(arg.SIGNATURE_KEY.name()), pReturn), // signature key
|
||||
pArgs.getString(arg.PRIVATE_KEY_PASSPHRASE.name()), // signature passphrase
|
||||
null, // progress
|
||||
pArgs.getInt(arg.ENCRYPTION_ALGORYTHM.name()), // encryption
|
||||
@ -427,29 +413,29 @@ public class ApgService extends Service {
|
||||
);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Exception in encrypt");
|
||||
String _msg = e.getMessage();
|
||||
if (_msg.equals(getBaseContext().getString(R.string.error_noSignaturePassPhrase))) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot encrypt (" + arg.PRIVATE_KEY_PASSPHRASE.name() + " missing): " + _msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_MISSING.shifted_ordinal());
|
||||
} else if (_msg.equals(getBaseContext().getString(R.string.error_couldNotExtractPrivateKey))) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot encrypt (" + arg.PRIVATE_KEY_PASSPHRASE.name() + " probably wrong): " + _msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_WRONG.shifted_ordinal());
|
||||
String msg = e.getMessage();
|
||||
if (msg.equals(getBaseContext().getString(R.string.error_noSignaturePassPhrase))) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot encrypt (" + arg.PRIVATE_KEY_PASSPHRASE.name() + " missing): " + msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_MISSING.shiftedOrdinal());
|
||||
} else if (msg.equals(getBaseContext().getString(R.string.error_couldNotExtractPrivateKey))) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot encrypt (" + arg.PRIVATE_KEY_PASSPHRASE.name() + " probably wrong): " + msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_WRONG.shiftedOrdinal());
|
||||
} else {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Internal failure (" + e.getClass() + ") in APG when encrypting: " + e.getMessage());
|
||||
pReturn.putInt(ret.ERROR.name(), error.APG_FAILURE.shifted_ordinal());
|
||||
pReturn.putInt(ret.ERROR.name(), error.APG_FAILURE.shiftedOrdinal());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Encrypted");
|
||||
pReturn.putString(ret.RESULT.name(), _out.toString());
|
||||
pReturn.putString(ret.RESULT.name(), out.toString());
|
||||
return true;
|
||||
}
|
||||
|
||||
private final IApgService.Stub mBinder = new IApgService.Stub() {
|
||||
|
||||
public boolean get_keys(Bundle pArgs, Bundle pReturn) {
|
||||
public boolean getKeys(Bundle pArgs, Bundle pReturn) {
|
||||
|
||||
prepare_args("get_keys", pArgs, pReturn);
|
||||
prepareArgs("get_keys", pArgs, pReturn);
|
||||
|
||||
HashMap<String, Object> qParams = new HashMap<String, Object>();
|
||||
qParams.put("columns", new String[] {
|
||||
@ -459,31 +445,31 @@ public class ApgService extends Service {
|
||||
|
||||
qParams.put("key_type", pArgs.getInt(arg.KEY_TYPE.name()));
|
||||
|
||||
Cursor mCursor = get_key_entries(qParams);
|
||||
ArrayList<String> fprints = new ArrayList<String>();
|
||||
Cursor cursor = getKeyEntries(qParams);
|
||||
ArrayList<String> fPrints = new ArrayList<String>();
|
||||
ArrayList<String> ids = new ArrayList<String>();
|
||||
while (mCursor.moveToNext()) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "adding key "+Apg.getSmallFingerPrint(mCursor.getLong(0)));
|
||||
fprints.add(Apg.getSmallFingerPrint(mCursor.getLong(0)));
|
||||
ids.add(mCursor.getString(1));
|
||||
while (cursor.moveToNext()) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "adding key "+Apg.getSmallFingerPrint(cursor.getLong(0)));
|
||||
fPrints.add(Apg.getSmallFingerPrint(cursor.getLong(0)));
|
||||
ids.add(cursor.getString(1));
|
||||
}
|
||||
mCursor.close();
|
||||
cursor.close();
|
||||
|
||||
pReturn.putStringArrayList(ret.FINGERPRINTS.name(), fprints);
|
||||
pReturn.putStringArrayList(ret.FINGERPRINTS.name(), fPrints);
|
||||
pReturn.putStringArrayList(ret.USER_IDS.name(), ids);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean encrypt_with_public_key(Bundle pArgs, Bundle pReturn) {
|
||||
if (!prepare_args("encrypt_with_public_key", pArgs, pReturn)) {
|
||||
public boolean encryptWithPublicKey(Bundle pArgs, Bundle pReturn) {
|
||||
if (!prepareArgs("encrypt_with_public_key", pArgs, pReturn)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return encrypt(pArgs, pReturn);
|
||||
}
|
||||
|
||||
public boolean encrypt_with_passphrase(Bundle pArgs, Bundle pReturn) {
|
||||
if (!prepare_args("encrypt_with_passphrase", pArgs, pReturn)) {
|
||||
public boolean encryptWithPassphrase(Bundle pArgs, Bundle pReturn) {
|
||||
if (!prepareArgs("encrypt_with_passphrase", pArgs, pReturn)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -492,11 +478,11 @@ public class ApgService extends Service {
|
||||
}
|
||||
|
||||
public boolean decrypt(Bundle pArgs, Bundle pReturn) {
|
||||
if (!prepare_args("decrypt", pArgs, pReturn)) {
|
||||
if (!prepareArgs("decrypt", pArgs, pReturn)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String _passphrase = pArgs.getString(arg.SYMMETRIC_PASSPHRASE.name()) != null ? pArgs.getString(arg.SYMMETRIC_PASSPHRASE.name()) : pArgs
|
||||
String passphrase = pArgs.getString(arg.SYMMETRIC_PASSPHRASE.name()) != null ? pArgs.getString(arg.SYMMETRIC_PASSPHRASE.name()) : pArgs
|
||||
.getString(arg.PRIVATE_KEY_PASSPHRASE.name());
|
||||
|
||||
InputStream inStream = new ByteArrayInputStream(pArgs.getString(arg.MESSAGE.name()).getBytes());
|
||||
@ -504,21 +490,21 @@ public class ApgService extends Service {
|
||||
OutputStream out = new ByteArrayOutputStream();
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "About to decrypt");
|
||||
try {
|
||||
Apg.decrypt(getBaseContext(), in, out, _passphrase, null, // progress
|
||||
Apg.decrypt(getBaseContext(), in, out, passphrase, null, // progress
|
||||
pArgs.getString(arg.SYMMETRIC_PASSPHRASE.name()) != null // symmetric
|
||||
);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Exception in decrypt");
|
||||
String _msg = e.getMessage();
|
||||
if (_msg.equals(getBaseContext().getString(R.string.error_noSecretKeyFound))) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot decrypt: " + _msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.NO_MATCHING_SECRET_KEY.shifted_ordinal());
|
||||
} else if (_msg.equals(getBaseContext().getString(R.string.error_wrongPassPhrase))) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot decrypt (" + arg.PRIVATE_KEY_PASSPHRASE.name() + " wrong/missing): " + _msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_WRONG.shifted_ordinal());
|
||||
String msg = e.getMessage();
|
||||
if (msg.equals(getBaseContext().getString(R.string.error_noSecretKeyFound))) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot decrypt: " + msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.NO_MATCHING_SECRET_KEY.shiftedOrdinal());
|
||||
} else if (msg.equals(getBaseContext().getString(R.string.error_wrongPassPhrase))) {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot decrypt (" + arg.PRIVATE_KEY_PASSPHRASE.name() + " wrong/missing): " + msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_WRONG.shiftedOrdinal());
|
||||
} else {
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Internal failure (" + e.getClass() + ") in APG when decrypting: " + _msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.APG_FAILURE.shifted_ordinal());
|
||||
pReturn.getStringArrayList(ret.ERRORS.name()).add("Internal failure (" + e.getClass() + ") in APG when decrypting: " + msg);
|
||||
pReturn.putInt(ret.ERROR.name(), error.APG_FAILURE.shiftedOrdinal());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -2,11 +2,12 @@ package org.thialfihar.android.apg;
|
||||
|
||||
interface IApgService {
|
||||
|
||||
/* All functions fill the return_vals Bundle with the following keys:
|
||||
/* All functions fill the returnVals Bundle with the following keys:
|
||||
*
|
||||
* ArrayList<String> "WARNINGS" = Warnings, if any
|
||||
* ArrayList<String> "ERRORS" = Human readable error descriptions, if any
|
||||
* int "ERROR" = Numeric representation of error, if any, starting with 100
|
||||
* int "ERROR" = Numeric representation of error, if any
|
||||
* starting with 100:
|
||||
* 100: Required argument missing
|
||||
* 101: Generic failure of APG
|
||||
* 102: No matching private key found
|
||||
@ -14,11 +15,10 @@ interface IApgService {
|
||||
* 104: Private key's passphrase missing
|
||||
*/
|
||||
|
||||
/* *******************************************************
|
||||
* Encrypting and decrypting
|
||||
/* ********************************************************
|
||||
* Encryption
|
||||
* ********************************************************/
|
||||
|
||||
|
||||
/* All encryption function's arguments
|
||||
*
|
||||
* Bundle params' keys:
|
||||
@ -54,27 +54,28 @@ interface IApgService {
|
||||
* (optional)
|
||||
* String "PRIVATE_KEY_PASSPHRASE" = Passphrase for signing key
|
||||
*
|
||||
* Bundle return_vals (in addition to the ERRORS/WARNINGS above):
|
||||
* Bundle returnVals (in addition to the ERRORS/WARNINGS above):
|
||||
* String "RESULT" = Encrypted message
|
||||
*/
|
||||
|
||||
/* Additional argument:
|
||||
/* Additional argument for function below:
|
||||
* (required)
|
||||
* String "SYMMETRIC_PASSPHRASE" = Symmetric passphrase to use
|
||||
*/
|
||||
boolean encrypt_with_passphrase(in Bundle params, out Bundle return_vals);
|
||||
boolean encryptWithPassphrase(in Bundle params, out Bundle returnVals);
|
||||
|
||||
/* Additional argument:
|
||||
* (required)
|
||||
* ArrayList<String> "PUBLIC_KEYS" = Public keys (8char fingerprint "123ABC12" OR
|
||||
* complete id "Alice Meyer <ab@email.com>")
|
||||
*/
|
||||
boolean encrypt_with_public_key(in Bundle params, out Bundle return_vals);
|
||||
boolean encryptWithPublicKey(in Bundle params, out Bundle returnVals);
|
||||
|
||||
/* ********************************************************
|
||||
* Decryption
|
||||
* ********************************************************/
|
||||
|
||||
/* Decrypt something
|
||||
*
|
||||
* Bundle params:
|
||||
/* Bundle params:
|
||||
* (required)
|
||||
* String "MESSAGE" = Message to decrypt
|
||||
*
|
||||
@ -87,11 +88,9 @@ interface IApgService {
|
||||
* Bundle return_vals:
|
||||
* String "RESULT" = Decrypted message
|
||||
*/
|
||||
boolean decrypt(in Bundle params, out Bundle returnVals);
|
||||
|
||||
boolean decrypt(in Bundle params, out Bundle return_vals);
|
||||
|
||||
|
||||
/* *******************************************************
|
||||
/* ********************************************************
|
||||
* Get key information
|
||||
* ********************************************************/
|
||||
|
||||
@ -106,8 +105,9 @@ interface IApgService {
|
||||
* Returns:
|
||||
* StringArrayList "FINGERPRINTS" = Short fingerprints of keys
|
||||
*
|
||||
* StringArrayList "USER_IDS" = User ids of corrosponding fingerprints (order is the same)
|
||||
* StringArrayList "USER_IDS" = User ids of corresponding fingerprints
|
||||
* (order is the same as in FINGERPRINTS)
|
||||
*/
|
||||
boolean get_keys(in Bundle params, out Bundle return_vals);
|
||||
boolean getKeys(in Bundle params, out Bundle returnVals);
|
||||
|
||||
}
|
@ -46,14 +46,19 @@ import org.thialfihar.android.apg.utils.ApgConInterface.OnCallFinishListener;
|
||||
* </p>
|
||||
*
|
||||
* @author Markus Doits <markus.doits@googlemail.com>
|
||||
* @version 0.9.9
|
||||
* @version 1.0rc1
|
||||
*
|
||||
*/
|
||||
public class ApgCon {
|
||||
private static final boolean LOCAL_LOGV = true;
|
||||
private static final boolean LOCAL_LOGD = true;
|
||||
|
||||
private class call_async extends AsyncTask<String, Void, Void> {
|
||||
private final static String TAG = "ApgCon";
|
||||
private final static int API_VERSION = 1; // aidl api-version it expects
|
||||
|
||||
public int secondsToWaitForConnection = 15;
|
||||
|
||||
private class CallAsync extends AsyncTask<String, Void, Void> {
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(String... arg) {
|
||||
@ -64,48 +69,46 @@ public class ApgCon {
|
||||
|
||||
protected void onPostExecute(Void res) {
|
||||
if( LOCAL_LOGD ) Log.d(TAG, "Async execution finished");
|
||||
async_running = false;
|
||||
mAsyncRunning = false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final static String TAG = "ApgCon";
|
||||
private final static int api_version = 1; // aidl api-version it expects
|
||||
|
||||
private final Context mContext;
|
||||
private final error connection_status;
|
||||
private boolean async_running = false;
|
||||
private OnCallFinishListener onCallFinishListener;
|
||||
private final error mConnectionStatus;
|
||||
private boolean mAsyncRunning = false;
|
||||
private OnCallFinishListener mOnCallFinishListener;
|
||||
|
||||
private final Bundle result = new Bundle();
|
||||
private final Bundle args = new Bundle();
|
||||
private final ArrayList<String> error_list = new ArrayList<String>();
|
||||
private final ArrayList<String> warning_list = new ArrayList<String>();
|
||||
private final Bundle mResult = new Bundle();
|
||||
private final Bundle mArgs = new Bundle();
|
||||
private final ArrayList<String> mErrorList = new ArrayList<String>();
|
||||
private final ArrayList<String> mWarningList = new ArrayList<String>();
|
||||
|
||||
/** Remote service for decrypting and encrypting data */
|
||||
private IApgService apgService = null;
|
||||
private IApgService mApgService = null;
|
||||
|
||||
/** Set apgService accordingly to connection status */
|
||||
private ServiceConnection apgConnection = new ServiceConnection() {
|
||||
private ServiceConnection mApgConnection = new ServiceConnection() {
|
||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||
if( LOCAL_LOGD ) Log.d(TAG, "IApgService bound to apgService");
|
||||
apgService = IApgService.Stub.asInterface(service);
|
||||
mApgService = IApgService.Stub.asInterface(service);
|
||||
}
|
||||
|
||||
public void onServiceDisconnected(ComponentName className) {
|
||||
if( LOCAL_LOGD ) Log.d(TAG, "IApgService disconnected");
|
||||
apgService = null;
|
||||
mApgService = null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Different types of local errors
|
||||
*
|
||||
* @author markus
|
||||
*
|
||||
*/
|
||||
public static enum error {
|
||||
/**
|
||||
* no error
|
||||
*/
|
||||
NO_ERROR,
|
||||
/**
|
||||
* generic error
|
||||
@ -131,6 +134,9 @@ public class ApgCon {
|
||||
* found APG but without AIDL interface
|
||||
*/
|
||||
APG_AIDL_MISSING,
|
||||
/**
|
||||
* found APG but with wrong API
|
||||
*/
|
||||
APG_API_MISSMATCH
|
||||
}
|
||||
|
||||
@ -156,77 +162,78 @@ public class ApgCon {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "EncryptionService created");
|
||||
mContext = ctx;
|
||||
|
||||
error tmp_connection_status = null;
|
||||
error tmpError = null;
|
||||
try {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Searching for the right APG version");
|
||||
ServiceInfo apg_services[] = ctx.getPackageManager().getPackageInfo("org.thialfihar.android.apg",
|
||||
ServiceInfo apgServices[] = ctx.getPackageManager().getPackageInfo("org.thialfihar.android.apg",
|
||||
PackageManager.GET_SERVICES | PackageManager.GET_META_DATA).services;
|
||||
if (apg_services == null) {
|
||||
if (apgServices == null) {
|
||||
Log.e(TAG, "Could not fetch services");
|
||||
tmp_connection_status = error.GENERIC;
|
||||
tmpError = error.GENERIC;
|
||||
} else {
|
||||
boolean apg_service_found = false;
|
||||
for (ServiceInfo inf : apg_services) {
|
||||
boolean apgServiceFound = false;
|
||||
for (ServiceInfo inf : apgServices) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Found service of APG: " + inf.name);
|
||||
if (inf.name.equals("org.thialfihar.android.apg.ApgService")) {
|
||||
apg_service_found = true;
|
||||
apgServiceFound = true;
|
||||
if (inf.metaData == null) {
|
||||
Log.w(TAG, "Could not determine ApgService API");
|
||||
Log.w(TAG, "This probably won't work!");
|
||||
warning_list.add("(LOCAL) Could not determine ApgService API");
|
||||
tmp_connection_status = error.APG_API_MISSMATCH;
|
||||
} else if (inf.metaData.getInt("api_version") != api_version) {
|
||||
Log.w(TAG, "Found ApgService API version" + inf.metaData.getInt("api_version") + " but exspected " + api_version);
|
||||
mWarningList.add("(LOCAL) Could not determine ApgService API");
|
||||
tmpError = error.APG_API_MISSMATCH;
|
||||
} else if (inf.metaData.getInt("api_version") != API_VERSION) {
|
||||
Log.w(TAG, "Found ApgService API version" + inf.metaData.getInt("api_version") + " but exspected " + API_VERSION);
|
||||
Log.w(TAG, "This probably won't work!");
|
||||
warning_list.add("(LOCAL) Found ApgService API version" + inf.metaData.getInt("api_version") + " but exspected " + api_version);
|
||||
tmp_connection_status = error.APG_API_MISSMATCH;
|
||||
mWarningList.add("(LOCAL) Found ApgService API version" + inf.metaData.getInt("api_version") + " but exspected " + API_VERSION);
|
||||
tmpError = error.APG_API_MISSMATCH;
|
||||
} else {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Found api_version " + api_version + ", everything should work");
|
||||
tmp_connection_status = error.NO_ERROR;
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "Found api_version " + API_VERSION + ", everything should work");
|
||||
tmpError = error.NO_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!apg_service_found) {
|
||||
if (!apgServiceFound) {
|
||||
Log.e(TAG, "Could not find APG with AIDL interface, this probably won't work");
|
||||
error_list.add("(LOCAL) Could not find APG with AIDL interface, this probably won't work");
|
||||
result.putInt(ret.ERROR.name(), error.APG_AIDL_MISSING.ordinal());
|
||||
tmp_connection_status = error.APG_NOT_FOUND;
|
||||
mErrorList.add("(LOCAL) Could not find APG with AIDL interface, this probably won't work");
|
||||
mResult.putInt(ret.ERROR.name(), error.APG_AIDL_MISSING.ordinal());
|
||||
tmpError = error.APG_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.e(TAG, "Could not find APG, is it installed?", e);
|
||||
error_list.add("(LOCAL) Could not find APG, is it installed?");
|
||||
result.putInt(ret.ERROR.name(), error.APG_NOT_FOUND.ordinal());
|
||||
tmp_connection_status = error.APG_NOT_FOUND;
|
||||
mErrorList.add("(LOCAL) Could not find APG, is it installed?");
|
||||
mResult.putInt(ret.ERROR.name(), error.APG_NOT_FOUND.ordinal());
|
||||
tmpError = error.APG_NOT_FOUND;
|
||||
}
|
||||
|
||||
connection_status = tmp_connection_status;
|
||||
mConnectionStatus = tmpError;
|
||||
|
||||
}
|
||||
|
||||
/** try to connect to the apg service */
|
||||
private boolean connect() {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "trying to bind the apgService to context");
|
||||
|
||||
if (apgService != null) {
|
||||
if (mApgService != null) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "allready connected");
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
mContext.bindService(new Intent(IApgService.class.getName()), apgConnection, Context.BIND_AUTO_CREATE);
|
||||
mContext.bindService(new Intent(IApgService.class.getName()), mApgConnection, Context.BIND_AUTO_CREATE);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "could not bind APG service", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
int wait_count = 0;
|
||||
while (apgService == null && wait_count++ < 15) {
|
||||
int waitCount = 0;
|
||||
while (mApgService == null && waitCount++ < secondsToWaitForConnection) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "sleeping 1 second to wait for apg");
|
||||
android.os.SystemClock.sleep(1000);
|
||||
}
|
||||
|
||||
if (wait_count >= 15) {
|
||||
if (waitCount >= secondsToWaitForConnection) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "slept waiting for nothing!");
|
||||
return false;
|
||||
}
|
||||
@ -250,14 +257,14 @@ public class ApgCon {
|
||||
*/
|
||||
public void disconnect() {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "disconnecting apgService");
|
||||
if (apgService != null) {
|
||||
mContext.unbindService(apgConnection);
|
||||
apgService = null;
|
||||
if (mApgService != null) {
|
||||
mContext.unbindService(mApgConnection);
|
||||
mApgService = null;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean initialize() {
|
||||
if (apgService == null) {
|
||||
if (mApgService == null) {
|
||||
if (!connect()) {
|
||||
if( LOCAL_LOGV ) Log.v(TAG, "connection to apg service failed");
|
||||
return false;
|
||||
@ -270,40 +277,40 @@ public class ApgCon {
|
||||
* Calls a function from APG's AIDL-interface
|
||||
*
|
||||
* <p>
|
||||
* After you have set up everything with {@link #set_arg(String, String)}
|
||||
* After you have set up everything with {@link #setArg(String, String)}
|
||||
* (and variants), you can call a function from the AIDL-interface. This
|
||||
* will
|
||||
* <ul>
|
||||
* <li>start connection to the remote interface (if not already connected)</li>
|
||||
* <li>call the passed function with all set up parameters synchronously</li>
|
||||
* <li>set up everything to retrieve the result and/or warnings/errors</li>
|
||||
* <li>set up everything to retrieve the mResult and/or warnings/errors</li>
|
||||
* <li>call the callback if provided
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Note your thread will be blocked during execution - if you want to call
|
||||
* the function asynchronously, see {@link #call_async(String)}.
|
||||
* the function asynchronously, see {@link #callAsync(String)}.
|
||||
* </p>
|
||||
*
|
||||
* @param function
|
||||
* a remote function to call
|
||||
* @return true, if call successful (= no errors), else false
|
||||
*
|
||||
* @see #call_async(String)
|
||||
* @see #set_arg(String, String)
|
||||
* @see #set_onCallFinishListener(OnCallFinishListener)
|
||||
* @see #callAsync(String)
|
||||
* @see #setArg(String, String)
|
||||
* @see #setOnCallFinishListener(OnCallFinishListener)
|
||||
*/
|
||||
public boolean call(String function) {
|
||||
boolean success = this.call(function, args, result);
|
||||
if (onCallFinishListener != null) {
|
||||
boolean success = this.call(function, mArgs, mResult);
|
||||
if (mOnCallFinishListener != null) {
|
||||
try {
|
||||
if( LOCAL_LOGD ) Log.d(TAG, "About to execute callback");
|
||||
onCallFinishListener.onCallFinish(result);
|
||||
mOnCallFinishListener.onCallFinish(mResult);
|
||||
if( LOCAL_LOGD ) Log.d(TAG, "Callback executed");
|
||||
} catch (Exception e) {
|
||||
Log.w(TAG, "Exception on callback: (" + e.getClass() + ") " + e.getMessage(), e);
|
||||
warning_list.add("(LOCAL) Could not execute callback (" + e.getClass() + "): " + e.getMessage());
|
||||
mWarningList.add("(LOCAL) Could not execute callback (" + e.getClass() + "): " + e.getMessage());
|
||||
}
|
||||
}
|
||||
return success;
|
||||
@ -321,8 +328,8 @@ public class ApgCon {
|
||||
* <p>
|
||||
* To see whether the task is finished, you have to possibilities:
|
||||
* <ul>
|
||||
* <li>In your thread, poll {@link #is_running()}</li>
|
||||
* <li>Supply a callback with {@link #set_onCallFinishListener(OnCallFinishListener)}</li>
|
||||
* <li>In your thread, poll {@link #isRunning()}</li>
|
||||
* <li>Supply a callback with {@link #setOnCallFinishListener(OnCallFinishListener)}</li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
@ -330,47 +337,47 @@ public class ApgCon {
|
||||
* a remote function to call
|
||||
*
|
||||
* @see #call(String)
|
||||
* @see #is_running()
|
||||
* @see #set_onCallFinishListener(OnCallFinishListener)
|
||||
* @see #isRunning()
|
||||
* @see #setOnCallFinishListener(OnCallFinishListener)
|
||||
*/
|
||||
public void call_async(String function) {
|
||||
async_running = true;
|
||||
new call_async().execute(function);
|
||||
public void callAsync(String function) {
|
||||
mAsyncRunning = true;
|
||||
new CallAsync().execute(function);
|
||||
}
|
||||
|
||||
private boolean call(String function, Bundle pArgs, Bundle pReturn) {
|
||||
|
||||
if (!initialize()) {
|
||||
error_list.add("(LOCAL) Cannot bind to ApgService");
|
||||
result.putInt(ret.ERROR.name(), error.CANNOT_BIND_TO_APG.ordinal());
|
||||
mErrorList.add("(LOCAL) Cannot bind to ApgService");
|
||||
mResult.putInt(ret.ERROR.name(), error.CANNOT_BIND_TO_APG.ordinal());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (function == null || function.length() == 0) {
|
||||
error_list.add("(LOCAL) Function to call missing");
|
||||
result.putInt(ret.ERROR.name(), error.CALL_MISSING.ordinal());
|
||||
mErrorList.add("(LOCAL) Function to call missing");
|
||||
mResult.putInt(ret.ERROR.name(), error.CALL_MISSING.ordinal());
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
Boolean success = (Boolean) IApgService.class.getMethod(function, Bundle.class, Bundle.class).invoke(apgService, pArgs, pReturn);
|
||||
error_list.addAll(pReturn.getStringArrayList(ret.ERRORS.name()));
|
||||
warning_list.addAll(pReturn.getStringArrayList(ret.WARNINGS.name()));
|
||||
Boolean success = (Boolean) IApgService.class.getMethod(function, Bundle.class, Bundle.class).invoke(mApgService, pArgs, pReturn);
|
||||
mErrorList.addAll(pReturn.getStringArrayList(ret.ERRORS.name()));
|
||||
mWarningList.addAll(pReturn.getStringArrayList(ret.WARNINGS.name()));
|
||||
return success;
|
||||
} catch (NoSuchMethodException e) {
|
||||
Log.e(TAG, "Remote call not known (" + function + "): " + e.getMessage(), e);
|
||||
error_list.add("(LOCAL) Remote call not known (" + function + "): " + e.getMessage());
|
||||
result.putInt(ret.ERROR.name(), error.CALL_NOT_KNOWN.ordinal());
|
||||
mErrorList.add("(LOCAL) Remote call not known (" + function + "): " + e.getMessage());
|
||||
mResult.putInt(ret.ERROR.name(), error.CALL_NOT_KNOWN.ordinal());
|
||||
return false;
|
||||
} catch (InvocationTargetException e) {
|
||||
Throwable orig = e.getTargetException();
|
||||
Log.w(TAG, "Exception of type '" + orig.getClass() + "' on AIDL call '" + function + "': " + orig.getMessage(), orig);
|
||||
error_list.add("(LOCAL) Exception of type '" + orig.getClass() + "' on AIDL call '" + function + "': " + orig.getMessage());
|
||||
mErrorList.add("(LOCAL) Exception of type '" + orig.getClass() + "' on AIDL call '" + function + "': " + orig.getMessage());
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Generic error (" + e.getClass() + "): " + e.getMessage(), e);
|
||||
error_list.add("(LOCAL) Generic error (" + e.getClass() + "): " + e.getMessage());
|
||||
result.putInt(ret.ERROR.name(), error.GENERIC.ordinal());
|
||||
mErrorList.add("(LOCAL) Generic error (" + e.getClass() + "): " + e.getMessage());
|
||||
mResult.putInt(ret.ERROR.name(), error.GENERIC.ordinal());
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -390,7 +397,7 @@ public class ApgCon {
|
||||
*
|
||||
* <p>
|
||||
* Note, that the parameters are not deleted after a call, so you have to
|
||||
* reset ({@link #clear_args()}) them manually if you want to.
|
||||
* reset ({@link #clearArgs()}) them manually if you want to.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
@ -399,10 +406,10 @@ public class ApgCon {
|
||||
* @param val
|
||||
* the value
|
||||
*
|
||||
* @see #clear_args()
|
||||
* @see #clearArgs()
|
||||
*/
|
||||
public void set_arg(String key, String val) {
|
||||
args.putString(key, val);
|
||||
public void setArg(String key, String val) {
|
||||
mArgs.putString(key, val);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -415,7 +422,7 @@ public class ApgCon {
|
||||
*
|
||||
* <code>
|
||||
* <pre>
|
||||
* set_arg("a key", new String[]{ "entry 1", "entry 2" });
|
||||
* setArg("a key", new String[]{ "entry 1", "entry 2" });
|
||||
* </pre>
|
||||
* </code>
|
||||
*
|
||||
@ -424,14 +431,14 @@ public class ApgCon {
|
||||
* @param vals
|
||||
* the value
|
||||
*
|
||||
* @see #set_arg(String, String)
|
||||
* @see #setArg(String, String)
|
||||
*/
|
||||
public void set_arg(String key, String vals[]) {
|
||||
public void setArg(String key, String vals[]) {
|
||||
ArrayList<String> list = new ArrayList<String>();
|
||||
for (String val : vals) {
|
||||
list.add(val);
|
||||
}
|
||||
args.putStringArrayList(key, list);
|
||||
mArgs.putStringArrayList(key, list);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -442,10 +449,10 @@ public class ApgCon {
|
||||
* @param vals
|
||||
* the value
|
||||
*
|
||||
* @see #set_arg(String, String)
|
||||
* @see #setArg(String, String)
|
||||
*/
|
||||
public void set_arg(String key, boolean val) {
|
||||
args.putBoolean(key, val);
|
||||
public void setArg(String key, boolean val) {
|
||||
mArgs.putBoolean(key, val);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -456,10 +463,10 @@ public class ApgCon {
|
||||
* @param vals
|
||||
* the value
|
||||
*
|
||||
* @see #set_arg(String, String)
|
||||
* @see #setArg(String, String)
|
||||
*/
|
||||
public void set_arg(String key, int val) {
|
||||
args.putInt(key, val);
|
||||
public void setArg(String key, int val) {
|
||||
mArgs.putInt(key, val);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -474,14 +481,14 @@ public class ApgCon {
|
||||
* @param vals
|
||||
* the value
|
||||
*
|
||||
* @see #set_arg(String, String)
|
||||
* @see #setArg(String, String)
|
||||
*/
|
||||
public void set_arg(String key, int vals[]) {
|
||||
public void setArg(String key, int vals[]) {
|
||||
ArrayList<Integer> list = new ArrayList<Integer>();
|
||||
for (int val : vals) {
|
||||
list.add(val);
|
||||
}
|
||||
args.putIntegerArrayList(key, list);
|
||||
mArgs.putIntegerArrayList(key, list);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -489,17 +496,17 @@ public class ApgCon {
|
||||
*
|
||||
* <p>
|
||||
* Anything the has been set up with the various
|
||||
* {@link #set_arg(String, String)} functions, is cleared.
|
||||
* {@link #setArg(String, String)} functions, is cleared.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note, that any warning, error, callback, result, etc. is not cleared with
|
||||
* Note, that any warning, error, callback, mResult, etc. is not cleared with
|
||||
* this.
|
||||
* </p>
|
||||
*
|
||||
* @see #reset()
|
||||
*/
|
||||
public void clear_args() {
|
||||
args.clear();
|
||||
public void clearArgs() {
|
||||
mArgs.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -509,8 +516,8 @@ public class ApgCon {
|
||||
* the object's key you want to return
|
||||
* @return an object at position key, or null if not set
|
||||
*/
|
||||
public Object get_arg(String key) {
|
||||
return args.get(key);
|
||||
public Object getArg(String key) {
|
||||
return mArgs.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -525,12 +532,12 @@ public class ApgCon {
|
||||
* @return a human readable description of a error that happened, or null if
|
||||
* no more errors
|
||||
*
|
||||
* @see #has_next_error()
|
||||
* @see #clear_errors()
|
||||
* @see #hasNextError()
|
||||
* @see #clearErrors()
|
||||
*/
|
||||
public String get_next_error() {
|
||||
if (error_list.size() != 0)
|
||||
return error_list.remove(0);
|
||||
public String getNextError() {
|
||||
if (mErrorList.size() != 0)
|
||||
return mErrorList.remove(0);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
@ -540,10 +547,10 @@ public class ApgCon {
|
||||
*
|
||||
* @return true, if there are unreturned errors, false otherwise
|
||||
*
|
||||
* @see #get_next_error()
|
||||
* @see #getNextError()
|
||||
*/
|
||||
public boolean has_next_error() {
|
||||
return error_list.size() != 0;
|
||||
public boolean hasNextError() {
|
||||
return mErrorList.size() != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -552,15 +559,15 @@ public class ApgCon {
|
||||
* <p>
|
||||
* Values <100 mean the error happened locally, values >=100 mean the error
|
||||
* happened at the remote side (APG). See the IApgService.aidl (or get the
|
||||
* human readable description with {@link #get_next_error()}) for what
|
||||
* human readable description with {@link #getNextError()}) for what
|
||||
* errors >=100 mean.
|
||||
* </p>
|
||||
*
|
||||
* @return the id of the error that happened
|
||||
*/
|
||||
public int get_error() {
|
||||
if (result.containsKey(ret.ERROR.name()))
|
||||
return result.getInt(ret.ERROR.name());
|
||||
public int getError() {
|
||||
if (mResult.containsKey(ret.ERROR.name()))
|
||||
return mResult.getInt(ret.ERROR.name());
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
@ -577,12 +584,12 @@ public class ApgCon {
|
||||
* @return a human readable description of a warning that happened, or null
|
||||
* if no more warnings
|
||||
*
|
||||
* @see #has_next_warning()
|
||||
* @see #clear_warnings()
|
||||
* @see #hasNextWarning()
|
||||
* @see #clearWarnings()
|
||||
*/
|
||||
public String get_next_warning() {
|
||||
if (warning_list.size() != 0)
|
||||
return warning_list.remove(0);
|
||||
public String getNextWarning() {
|
||||
if (mWarningList.size() != 0)
|
||||
return mWarningList.remove(0);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
@ -592,94 +599,94 @@ public class ApgCon {
|
||||
*
|
||||
* @return true, if there are unreturned warnings, false otherwise
|
||||
*
|
||||
* @see #get_next_warning()
|
||||
* @see #getNextWarning()
|
||||
*/
|
||||
public boolean has_next_warning() {
|
||||
return warning_list.size() != 0;
|
||||
public boolean hasNextWarning() {
|
||||
return mWarningList.size() != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the result
|
||||
*
|
||||
* <p>
|
||||
* This gets your result. After doing an encryption or decryption with APG,
|
||||
* This gets your mResult. After doing an encryption or decryption with APG,
|
||||
* you get the output with this function.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note, that when your last remote call is unsuccessful, the result will
|
||||
* Note, that when your last remote call is unsuccessful, the mResult will
|
||||
* still have the same value like the last successful call (or null, if no
|
||||
* call was successful). To ensure you do not work with old call's results,
|
||||
* either be sure to {@link #reset()} (or at least {@link #clear_result()})
|
||||
* call was successful). To ensure you do not work with old call's mResults,
|
||||
* either be sure to {@link #reset()} (or at least {@link #clearResult()})
|
||||
* your instance before each new call or always check that
|
||||
* {@link #has_next_error()} is false.
|
||||
* {@link #hasNextError()} is false.
|
||||
* </p>
|
||||
*
|
||||
* @return the result of the last {@link #call(String)} or
|
||||
* @return the mResult of the last {@link #call(String)} or
|
||||
* {@link #call_asinc(String)}.
|
||||
*
|
||||
* @see #reset()
|
||||
* @see #clear_result()
|
||||
* @see #get_result_bundle()
|
||||
* @see #clearResult()
|
||||
* @see #getResultBundle()
|
||||
*/
|
||||
public String get_result() {
|
||||
return result.getString(ret.RESULT.name());
|
||||
public String getResult() {
|
||||
return mResult.getString(ret.RESULT.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the result bundle
|
||||
* Get the mResult bundle
|
||||
*
|
||||
* <p>
|
||||
* Unlike {@link #get_result()}, which only returns any en-/decrypted
|
||||
* Unlike {@link #getResult()}, which only returns any en-/decrypted
|
||||
* message, this function returns the complete information that was returned
|
||||
* by Apg. This also includes the "RESULT", but additionally the warnings,
|
||||
* errors and any other information.
|
||||
* </p>
|
||||
* <p>
|
||||
* For warnings and errors it is suggested to use the functions that are
|
||||
* provided here, namely {@link #get_error()}, {@link #get_next_error()},
|
||||
* provided here, namely {@link #getError()}, {@link #getNextError()},
|
||||
* {@link #get_next_Warning()} etc.), but if any call returns something non
|
||||
* standard, you have access to the complete result bundle to extract the
|
||||
* standard, you have access to the complete mResult bundle to extract the
|
||||
* information.
|
||||
* </p>
|
||||
*
|
||||
* @return the complete result-bundle of the last call to apg
|
||||
* @return the complete mResult-bundle of the last call to apg
|
||||
*/
|
||||
public Bundle get_result_bundle() {
|
||||
return result;
|
||||
public Bundle getResultBundle() {
|
||||
return mResult;
|
||||
}
|
||||
|
||||
public error get_connection_status() {
|
||||
return connection_status;
|
||||
public error getConnectionStatus() {
|
||||
return mConnectionStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all unfetched errors
|
||||
*
|
||||
* @see #get_next_error()
|
||||
* @see #has_next_error()
|
||||
* @see #getNextError()
|
||||
* @see #hasNextError()
|
||||
*/
|
||||
public void clear_errors() {
|
||||
error_list.clear();
|
||||
result.remove(ret.ERROR.name());
|
||||
public void clearErrors() {
|
||||
mErrorList.clear();
|
||||
mResult.remove(ret.ERROR.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all unfetched warnings
|
||||
*
|
||||
* @see #get_next_warning()
|
||||
* @see #has_next_warning()
|
||||
* @see #getNextWarning()
|
||||
* @see #hasNextWarning()
|
||||
*/
|
||||
public void clear_warnings() {
|
||||
warning_list.clear();
|
||||
public void clearWarnings() {
|
||||
mWarningList.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the last result
|
||||
* Clears the last mResult
|
||||
*
|
||||
* @see #get_result()
|
||||
* @see #getResult()
|
||||
*/
|
||||
public void clear_result() {
|
||||
result.remove(ret.RESULT.name());
|
||||
public void clearResult() {
|
||||
mResult.remove(ret.RESULT.name());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -689,34 +696,34 @@ public class ApgCon {
|
||||
* a object to call back after async execution
|
||||
* @see ApgConInterface
|
||||
*/
|
||||
public void set_onCallFinishListener(OnCallFinishListener lis) {
|
||||
onCallFinishListener = lis;
|
||||
public void setOnCallFinishListener(OnCallFinishListener lis) {
|
||||
mOnCallFinishListener = lis;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears any callback object
|
||||
*
|
||||
* @see #set_onCallFinishListener(OnCallFinishListener)
|
||||
* @see #setOnCallFinishListener(OnCallFinishListener)
|
||||
*/
|
||||
public void clear_onCallFinishListener() {
|
||||
onCallFinishListener = null;
|
||||
public void clearOnCallFinishListener() {
|
||||
mOnCallFinishListener = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks, whether an async execution is running
|
||||
*
|
||||
* <p>
|
||||
* If you started something with {@link #call_async(String)}, this will
|
||||
* If you started something with {@link #callAsync(String)}, this will
|
||||
* return true if the task is still running
|
||||
* </p>
|
||||
*
|
||||
* @return true, if an async task is still running, false otherwise
|
||||
*
|
||||
* @see #call_async(String)
|
||||
* @see #callAsync(String)
|
||||
*
|
||||
*/
|
||||
public boolean is_running() {
|
||||
return async_running;
|
||||
public boolean isRunning() {
|
||||
return mAsyncRunning;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -724,24 +731,24 @@ public class ApgCon {
|
||||
*
|
||||
* <p>
|
||||
* This currently resets everything in this instance. Errors, warnings,
|
||||
* results, callbacks, ... are removed. Any connection to the remote
|
||||
* mResults, callbacks, ... are removed. Any connection to the remote
|
||||
* interface is upheld, though.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Note, that when an async execution ({@link #call_async(String)}) is
|
||||
* running, it's result, warnings etc. will still be evaluated (which might
|
||||
* Note, that when an async execution ({@link #callAsync(String)}) is
|
||||
* running, it's mResult, warnings etc. will still be evaluated (which might
|
||||
* be not what you want). Also mind, that any callback you set is also
|
||||
* reseted, so on finishing the execution any before defined callback will
|
||||
* NOT BE TRIGGERED.
|
||||
* </p>
|
||||
*/
|
||||
public void reset() {
|
||||
clear_errors();
|
||||
clear_warnings();
|
||||
clear_args();
|
||||
clear_onCallFinishListener();
|
||||
result.clear();
|
||||
clearErrors();
|
||||
clearWarnings();
|
||||
clearArgs();
|
||||
clearOnCallFinishListener();
|
||||
mResult.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user