private request codes, pass params through methods and pending intents, getKeyIds method

This commit is contained in:
Dominik Schürmann
2014-02-15 01:06:03 +01:00
parent 21ba41edae
commit 494a5fa414
8 changed files with 220 additions and 135 deletions

View File

@@ -159,7 +159,7 @@ public class OpenPgpProviderActivity extends Activity {
mCiphertext.setText(os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
break;
}
@@ -170,7 +170,7 @@ public class OpenPgpProviderActivity extends Activity {
REQUEST_CODE_SIGN, null,
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
@@ -198,7 +198,7 @@ public class OpenPgpProviderActivity extends Activity {
mCiphertext.setText(os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
break;
}
@@ -209,7 +209,7 @@ public class OpenPgpProviderActivity extends Activity {
REQUEST_CODE_ENCRYPT, null,
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
@@ -237,7 +237,7 @@ public class OpenPgpProviderActivity extends Activity {
mCiphertext.setText(os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
break;
}
@@ -248,7 +248,7 @@ public class OpenPgpProviderActivity extends Activity {
REQUEST_CODE_SIGN_AND_ENCRYPT, null,
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
@@ -275,7 +275,7 @@ public class OpenPgpProviderActivity extends Activity {
mMessage.setText(os.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
}
break;
}
@@ -286,7 +286,7 @@ public class OpenPgpProviderActivity extends Activity {
REQUEST_CODE_DECRYPT_AND_VERIFY, null,
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
Log.e(Constants.TAG, "SendIntentException", e);
}
break;
}
@@ -297,7 +297,8 @@ public class OpenPgpProviderActivity extends Activity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
Log.d(Constants.TAG, "onActivityResult");
switch (requestCode) {
case REQUEST_CODE_SIGN: {
@@ -305,7 +306,7 @@ public class OpenPgpProviderActivity extends Activity {
// try to sign again after password caching
if (resultCode == RESULT_OK) {
sign(new Bundle());
sign(data.getExtras());
}
break;
}

View File

@@ -64,4 +64,15 @@ interface IOpenPgpService {
*/
Bundle decryptAndVerify(in Bundle params, in ParcelFileDescriptor input, in ParcelFileDescriptor output);
/**
* Retrieves key ids based on given user ids (=emails)
*
* params:
* String[] user_ids
*
* result:
* long[] key_ids
*/
Bundle getKeyIds(in Bundle params);
}

View File

@@ -35,6 +35,7 @@ public class OpenPgpApi {
private static final int OPERATION_ENCRYPT = 1;
private static final int OPERATION_SIGN_ENCRYPT = 2;
private static final int OPERATION_DECRYPT_VERIFY = 3;
private static final int OPERATION_GET_KEY_IDS = 4;
public OpenPgpApi(IOpenPgpService service) {
this.mService = service;
@@ -88,6 +89,10 @@ public class OpenPgpApi {
executeApiAsync(OPERATION_DECRYPT_VERIFY, params, is, os, callback);
}
public Bundle getKeyIds(Bundle params) {
return executeApi(OPERATION_GET_KEY_IDS, params, null, null);
}
public interface IOpenPgpCallback {
void onReturn(final Bundle result);
}
@@ -124,24 +129,6 @@ public class OpenPgpApi {
private Bundle executeApi(int operationId, Bundle params, InputStream is, OutputStream os) {
try {
// send the input and output pfds
ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Copy to service finished");
}
});
ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Service finished writing!");
}
});
params.putInt(OpenPgpConstants.PARAMS_API_VERSION, OpenPgpConstants.API_VERSION);
// default result is error
@@ -150,25 +137,49 @@ public class OpenPgpApi {
result.putParcelable(OpenPgpConstants.RESULT_ERRORS,
new OpenPgpError(OpenPgpError.GENERIC_ERROR, "This should never happen!"));
// blocks until result is ready
switch (operationId) {
case OPERATION_SIGN:
result = mService.sign(params, input, output);
break;
case OPERATION_ENCRYPT:
result = mService.encrypt(params, input, output);
break;
case OPERATION_SIGN_ENCRYPT:
result = mService.signAndEncrypt(params, input, output);
break;
case OPERATION_DECRYPT_VERIFY:
result = mService.decryptAndVerify(params, input, output);
break;
}
// close() is required to halt the TransferThread
output.close();
if (operationId == OPERATION_GET_KEY_IDS) {
result = mService.getKeyIds(params);
return result;
} else {
// send the input and output pfds
ParcelFileDescriptor input = ParcelFileDescriptorUtil.pipeFrom(is,
new ParcelFileDescriptorUtil.IThreadListener() {
return result;
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Copy to service finished");
}
});
ParcelFileDescriptor output = ParcelFileDescriptorUtil.pipeTo(os,
new ParcelFileDescriptorUtil.IThreadListener() {
@Override
public void onThreadFinished(Thread thread) {
Log.d(OpenPgpConstants.TAG, "Service finished writing!");
}
});
// blocks until result is ready
switch (operationId) {
case OPERATION_SIGN:
result = mService.sign(params, input, output);
break;
case OPERATION_ENCRYPT:
result = mService.encrypt(params, input, output);
break;
case OPERATION_SIGN_ENCRYPT:
result = mService.signAndEncrypt(params, input, output);
break;
case OPERATION_DECRYPT_VERIFY:
result = mService.decryptAndVerify(params, input, output);
break;
}
// close() is required to halt the TransferThread
output.close();
return result;
}
} catch (Exception e) {
Log.e(OpenPgpConstants.TAG, "Exception", e);
Bundle result = new Bundle();

View File

@@ -87,12 +87,12 @@ public class ParcelFileDescriptorUtil {
try {
mIn.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e);
}
try {
mOut.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(OpenPgpConstants.TAG, "TransferThread" + getId(), e);
}
}
if (mListener != null) {