Work on errors and documentation - once more

This commit is contained in:
Markus Doits 2011-01-20 20:00:28 +00:00
parent 392969629c
commit c9f6f56827
3 changed files with 77 additions and 69 deletions

View File

@ -38,7 +38,11 @@ public class ApgService extends Service {
APG_FAILURE, APG_FAILURE,
NO_MATCHING_SECRET_KEY, NO_MATCHING_SECRET_KEY,
PRIVATE_KEY_PASSPHRASE_WRONG, PRIVATE_KEY_PASSPHRASE_WRONG,
PRIVATE_KEY_PASSPHRASE_MISSING PRIVATE_KEY_PASSPHRASE_MISSING;
public int shifted_ordinal() {
return ordinal() + 100;
}
} }
/** all arguments that can be passed by calling application */ /** all arguments that can be passed by calling application */
@ -355,7 +359,7 @@ public class ApgService extends Service {
/* return if errors happened */ /* return if errors happened */
if (pReturn.getStringArrayList(ret.ERRORS.name()).size() != 0) { if (pReturn.getStringArrayList(ret.ERRORS.name()).size() != 0) {
pReturn.putInt(ret.ERROR.name(), error.ARGUMENTS_MISSING.ordinal()); pReturn.putInt(ret.ERROR.name(), error.ARGUMENTS_MISSING.shifted_ordinal());
return false; return false;
} }
Log.v(TAG, "error return"); Log.v(TAG, "error return");
@ -401,13 +405,13 @@ public class ApgService extends Service {
String _msg = e.getMessage(); String _msg = e.getMessage();
if (_msg.equals(getBaseContext().getString(R.string.error_noSignaturePassPhrase))) { 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.getStringArrayList(ret.ERRORS.name()).add("Cannot encrypt (" + arg.PRIVATE_KEY_PASSPHRASE.name() + " missing): " + _msg);
pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_MISSING.ordinal()); pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_MISSING.shifted_ordinal());
} else if (_msg.equals(getBaseContext().getString(R.string.error_couldNotExtractPrivateKey))) { } 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.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.ordinal()); pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_WRONG.shifted_ordinal());
} else { } else {
pReturn.getStringArrayList(ret.ERRORS.name()).add("Internal failure (" + e.getClass() + ") in APG when encrypting: " + e.getMessage()); pReturn.getStringArrayList(ret.ERRORS.name()).add("Internal failure (" + e.getClass() + ") in APG when encrypting: " + e.getMessage());
pReturn.putInt(ret.ERROR.name(), error.APG_FAILURE.ordinal()); pReturn.putInt(ret.ERROR.name(), error.APG_FAILURE.shifted_ordinal());
} }
return false; return false;
} }
@ -455,13 +459,13 @@ public class ApgService extends Service {
String _msg = e.getMessage(); String _msg = e.getMessage();
if (_msg.equals(getBaseContext().getString(R.string.error_noSecretKeyFound))) { if (_msg.equals(getBaseContext().getString(R.string.error_noSecretKeyFound))) {
pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot decrypt: " + _msg); pReturn.getStringArrayList(ret.ERRORS.name()).add("Cannot decrypt: " + _msg);
pReturn.putInt(ret.ERROR.name(), error.NO_MATCHING_SECRET_KEY.ordinal()); pReturn.putInt(ret.ERROR.name(), error.NO_MATCHING_SECRET_KEY.shifted_ordinal());
} else if (_msg.equals(getBaseContext().getString(R.string.error_wrongPassPhrase))) { } 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.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.ordinal()); pReturn.putInt(ret.ERROR.name(), error.PRIVATE_KEY_PASSPHRASE_WRONG.shifted_ordinal());
} else { } else {
pReturn.getStringArrayList(ret.ERRORS.name()).add("Internal failure (" + e.getClass() + ") in APG when decrypting: " + _msg); pReturn.getStringArrayList(ret.ERRORS.name()).add("Internal failure (" + e.getClass() + ") in APG when decrypting: " + _msg);
pReturn.putInt(ret.ERROR.name(), error.APG_FAILURE.ordinal()); pReturn.putInt(ret.ERROR.name(), error.APG_FAILURE.shifted_ordinal());
} }
return false; return false;
} }

View File

@ -5,8 +5,13 @@ interface IApgService {
/* All functions fill the return_vals Bundle with the following keys: /* All functions fill the return_vals Bundle with the following keys:
* *
* ArrayList<String> "WARNINGS" = Warnings, if any * ArrayList<String> "WARNINGS" = Warnings, if any
* ArrayList<String> "ERRORS" = Human readable error descriptions, why function call failed * ArrayList<String> "ERRORS" = Human readable error descriptions, if any
* int "ERROR" = Numeric representation of error * 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
* 103: Private key's passphrase wrong
* 104: Private key's passphrase missing
*/ */
/* Encryption function's arguments /* Encryption function's arguments

View File

@ -17,10 +17,20 @@ import android.util.Log;
import org.thialfihar.android.apg.IApgService; import org.thialfihar.android.apg.IApgService;
/** /**
* This class can be used by other projects to simplify connecting to the * A APG-AIDL-Wrapper
* APG-Service. Kind of wrapper of for AIDL.
* *
* <p>
* This class can be used by other projects to simplify connecting to the
* APG-AIDL-Service. Kind of wrapper of for AIDL.
* </p>
*
* <p>
* It is not used in this project. * It is not used in this project.
* </p>
*
* @author Markus Doits <markus.doits@googlemail.com>
* @version 0.9
*
*/ */
public class ApgCon { public class ApgCon {
@ -81,7 +91,6 @@ public class ApgCon {
private final Bundle args = new Bundle(); private final Bundle args = new Bundle();
private final ArrayList<String> error_list = new ArrayList<String>(); private final ArrayList<String> error_list = new ArrayList<String>();
private final ArrayList<String> warning_list = new ArrayList<String>(); private final ArrayList<String> warning_list = new ArrayList<String>();
private error local_error;
/** Remote service for decrypting and encrypting data */ /** Remote service for decrypting and encrypting data */
private IApgService apgService = null; private IApgService apgService = null;
@ -99,21 +108,44 @@ public class ApgCon {
} }
}; };
/**
* Different types of local errors
*
* @author markus
*
*/
public static enum error { public static enum error {
GENERIC, // no special type /**
CANNOT_BIND_TO_APG, // connection to apg service not possible * generic error
CALL_MISSING, // function to call not provided */
CALL_NOT_KNOWN, // apg service does not know what to do GENERIC,
APG_NOT_FOUND, // could not find APG installed /**
APG_AIDL_MISSING, // found APG but without AIDL interface * connection to apg service not possible
*/
CANNOT_BIND_TO_APG,
/**
* function to call not provided
*/
CALL_MISSING,
/**
* apg service does not know what to do
*/
CALL_NOT_KNOWN,
/**
* could not find APG being installed
*/
APG_NOT_FOUND,
/**
* found APG but without AIDL interface
*/
APG_AIDL_MISSING
} }
public static enum ret { private static enum ret {
ERROR, // returned from AIDL ERROR, // returned from AIDL
RESULT, // returned from AIDL RESULT, // returned from AIDL
WARNINGS, // mixed AIDL and LOCAL WARNINGS, // mixed AIDL and LOCAL
ERRORS, // mixed AIDL and LOCAL ERRORS, // mixed AIDL and LOCAL
LOCAL_ERROR, // LOCAL error
} }
/** /**
@ -160,7 +192,7 @@ public class ApgCon {
if (!apg_service_found) { if (!apg_service_found) {
Log.e(TAG, "Could not find APG with AIDL interface, this probably won't work"); 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"); error_list.add("(LOCAL) Could not find APG with AIDL interface, this probably won't work");
local_error = error.APG_AIDL_MISSING; result.putInt(ret.ERROR.name(), error.APG_AIDL_MISSING.ordinal());
} }
} }
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
@ -168,7 +200,7 @@ public class ApgCon {
e.printStackTrace(); e.printStackTrace();
Log.e(TAG, "Could not find APG, is it installed?"); Log.e(TAG, "Could not find APG, is it installed?");
error_list.add("(LOCAL) Could not find APG, is it installed?"); error_list.add("(LOCAL) Could not find APG, is it installed?");
local_error = error.APG_NOT_FOUND; result.putInt(ret.ERROR.name(), error.APG_NOT_FOUND.ordinal());
} }
} }
@ -297,18 +329,15 @@ public class ApgCon {
private boolean call(String function, Bundle pArgs, Bundle pReturn) { private boolean call(String function, Bundle pArgs, Bundle pReturn) {
error_list.clear();
warning_list.clear();
if (!initialize()) { if (!initialize()) {
error_list.add("(LOCAL) Cannot bind to ApgService"); error_list.add("(LOCAL) Cannot bind to ApgService");
local_error = error.CANNOT_BIND_TO_APG; result.putInt(ret.ERROR.name(), error.CANNOT_BIND_TO_APG.ordinal());
return false; return false;
} }
if (function == null || function.length() == 0) { if (function == null || function.length() == 0) {
error_list.add("(LOCAL) Function to call missing"); error_list.add("(LOCAL) Function to call missing");
local_error = error.CALL_MISSING; result.putInt(ret.ERROR.name(), error.CALL_MISSING.ordinal());
return false; return false;
} }
@ -324,7 +353,7 @@ public class ApgCon {
e.printStackTrace(); e.printStackTrace();
Log.e(TAG, "Remote call not known (" + function + "): " + e.getMessage()); Log.e(TAG, "Remote call not known (" + function + "): " + e.getMessage());
error_list.add("(LOCAL) Remote call not known (" + function + "): " + e.getMessage()); error_list.add("(LOCAL) Remote call not known (" + function + "): " + e.getMessage());
local_error = error.CALL_NOT_KNOWN; result.putInt(ret.ERROR.name(), error.CALL_NOT_KNOWN.ordinal());
return false; return false;
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
if (stacktraces) if (stacktraces)
@ -338,7 +367,7 @@ public class ApgCon {
e.printStackTrace(); e.printStackTrace();
Log.e(TAG, "Generic error (" + e.getClass() + "): " + e.getMessage()); Log.e(TAG, "Generic error (" + e.getClass() + "): " + e.getMessage());
error_list.add("(LOCAL) Generic error (" + e.getClass() + "): " + e.getMessage()); error_list.add("(LOCAL) Generic error (" + e.getClass() + "): " + e.getMessage());
local_error = error.GENERIC; result.putInt(ret.ERROR.name(), error.GENERIC.ordinal());
return false; return false;
} }
@ -515,52 +544,23 @@ public class ApgCon {
} }
/** /**
* Returns the type of error happened * Get the numeric representation of the last error
* *
* <p> * <p>
* Currently, two error types are possible: * Values <100 mean the error happened locally, values >=100 mean the error
* <ul> * happened at the remote side (APG). See the IApgService.aidl (or get the
* <li>ret.LOCAL_ERROR: An error that happened on the caller site. This * human readable description with {@link #get_next_error()}) for what
* might be something like connection to AIDL not possible or the funciton * errors >=100 mean.
* call not know by AIDL. This means, the instance is not set up correctly
* or prerequisites to use APG with AIDL are not met.</li>
* <li>ret.ERROR: Connection to APG was successful, and the call started but
* failed. Mostly this is because of wrong or missing parameters for APG.</li>
* </ul>
* </p> * </p>
* *
* @return the type of error that happend: ret.LOCAL_ERROR or ret.ERROR, or * @return the id of the error that happened
* null if none happend
*/ */
public ret get_error_type() { public int get_error() {
if (local_error != null) { if (result.containsKey(ret.ERROR.name()))
return ret.LOCAL_ERROR;
} else if (result.containsKey(ret.ERROR.name())) {
return ret.ERROR;
} else {
return null;
}
}
public error get_local_error() {
return local_error;
}
public void clear_local_error() {
local_error = null;
}
public int get_remote_error() {
if (result.containsKey(ret.ERROR.name())) {
return result.getInt(ret.ERROR.name()); return result.getInt(ret.ERROR.name());
} else { else
return -1; return -1;
} }
}
public void clear_remote_error() {
result.remove(ret.ERROR.name());
}
/** /**
* Iterates through the warnings * Iterates through the warnings
@ -630,7 +630,6 @@ public class ApgCon {
public void clear_errors() { public void clear_errors() {
error_list.clear(); error_list.clear();
result.remove(ret.ERROR.name()); result.remove(ret.ERROR.name());
clear_local_error();
} }
/** /**