mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-08-13 15:53:53 -04:00
code simplifications
This commit is contained in:
parent
5f39cb3ec0
commit
e0111c2ec4
@ -44,6 +44,7 @@ import org.openintents.openpgp.util.OpenPgpServiceConnection;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -140,15 +141,17 @@ public class OpenPgpProviderActivity extends Activity {
|
|||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class MyCallback implements OpenPgpApi.IOpenPgpCallback {
|
||||||
|
boolean returnToCiphertextField;
|
||||||
|
ByteArrayOutputStream os;
|
||||||
|
int requestCode;
|
||||||
|
|
||||||
public void sign(Bundle params) {
|
private MyCallback(boolean returnToCiphertextField, ByteArrayOutputStream os, int requestCode) {
|
||||||
params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true);
|
this.returnToCiphertextField = returnToCiphertextField;
|
||||||
|
this.os = os;
|
||||||
|
this.requestCode = requestCode;
|
||||||
|
}
|
||||||
|
|
||||||
InputStream is = getInputstream(false);
|
|
||||||
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
|
|
||||||
api.sign(params, is, os, new OpenPgpApi.IOpenPgpCallback() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onReturn(Bundle result) {
|
public void onReturn(Bundle result) {
|
||||||
switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
|
switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
|
||||||
@ -157,7 +160,11 @@ public class OpenPgpProviderActivity extends Activity {
|
|||||||
Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
|
Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
|
||||||
+ " str=" + os.toString("UTF-8"));
|
+ " str=" + os.toString("UTF-8"));
|
||||||
|
|
||||||
|
if (returnToCiphertextField) {
|
||||||
mCiphertext.setText(os.toString("UTF-8"));
|
mCiphertext.setText(os.toString("UTF-8"));
|
||||||
|
} else {
|
||||||
|
mMessage.setText(os.toString("UTF-8"));
|
||||||
|
}
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
|
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
|
||||||
}
|
}
|
||||||
@ -167,15 +174,30 @@ public class OpenPgpProviderActivity extends Activity {
|
|||||||
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
|
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
|
||||||
try {
|
try {
|
||||||
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
|
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
|
||||||
REQUEST_CODE_SIGN, null, 0, 0, 0);
|
requestCode, null, 0, 0, 0);
|
||||||
} catch (IntentSender.SendIntentException e) {
|
} catch (IntentSender.SendIntentException e) {
|
||||||
Log.e(Constants.TAG, "SendIntentException", e);
|
Log.e(Constants.TAG, "SendIntentException", e);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case OpenPgpConstants.RESULT_CODE_ERROR: {
|
||||||
|
OpenPgpError error = result.getParcelable(OpenPgpConstants.RESULT_ERRORS);
|
||||||
|
handleError(error);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void sign(Bundle params) {
|
||||||
|
params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true);
|
||||||
|
|
||||||
|
InputStream is = getInputstream(false);
|
||||||
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
|
||||||
|
api.sign(params, is, os, new MyCallback(true, os, REQUEST_CODE_SIGN));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void encrypt(Bundle params) {
|
public void encrypt(Bundle params) {
|
||||||
@ -186,34 +208,7 @@ public class OpenPgpProviderActivity extends Activity {
|
|||||||
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
|
||||||
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
|
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
|
||||||
api.encrypt(params, is, os, new OpenPgpApi.IOpenPgpCallback() {
|
api.encrypt(params, is, os, new MyCallback(true, os, REQUEST_CODE_ENCRYPT));
|
||||||
@Override
|
|
||||||
public void onReturn(Bundle result) {
|
|
||||||
switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
|
|
||||||
case OpenPgpConstants.RESULT_CODE_SUCCESS: {
|
|
||||||
try {
|
|
||||||
Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
|
|
||||||
+ " str=" + os.toString("UTF-8"));
|
|
||||||
|
|
||||||
mCiphertext.setText(os.toString("UTF-8"));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED: {
|
|
||||||
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
|
|
||||||
try {
|
|
||||||
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
|
|
||||||
REQUEST_CODE_ENCRYPT, null, 0, 0, 0);
|
|
||||||
} catch (IntentSender.SendIntentException e) {
|
|
||||||
Log.e(Constants.TAG, "SendIntentException", e);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void signAndEncrypt(Bundle params) {
|
public void signAndEncrypt(Bundle params) {
|
||||||
@ -224,34 +219,7 @@ public class OpenPgpProviderActivity extends Activity {
|
|||||||
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
|
||||||
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
|
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
|
||||||
api.signAndEncrypt(params, is, os, new OpenPgpApi.IOpenPgpCallback() {
|
api.signAndEncrypt(params, is, os, new MyCallback(true, os, REQUEST_CODE_SIGN_AND_ENCRYPT));
|
||||||
@Override
|
|
||||||
public void onReturn(Bundle result) {
|
|
||||||
switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
|
|
||||||
case OpenPgpConstants.RESULT_CODE_SUCCESS: {
|
|
||||||
try {
|
|
||||||
Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
|
|
||||||
+ " str=" + os.toString("UTF-8"));
|
|
||||||
|
|
||||||
mCiphertext.setText(os.toString("UTF-8"));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED: {
|
|
||||||
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
|
|
||||||
try {
|
|
||||||
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
|
|
||||||
REQUEST_CODE_SIGN_AND_ENCRYPT, null, 0, 0, 0);
|
|
||||||
} catch (IntentSender.SendIntentException e) {
|
|
||||||
Log.e(Constants.TAG, "SendIntentException", e);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void decryptAndVerify(Bundle params) {
|
public void decryptAndVerify(Bundle params) {
|
||||||
@ -261,34 +229,7 @@ public class OpenPgpProviderActivity extends Activity {
|
|||||||
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
|
||||||
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
|
OpenPgpApi api = new OpenPgpApi(mCryptoServiceConnection.getService());
|
||||||
api.decryptAndVerify(params, is, os, new OpenPgpApi.IOpenPgpCallback() {
|
api.decryptAndVerify(params, is, os, new MyCallback(true, os, REQUEST_CODE_DECRYPT_AND_VERIFY));
|
||||||
@Override
|
|
||||||
public void onReturn(Bundle result) {
|
|
||||||
switch (result.getInt(OpenPgpConstants.RESULT_CODE)) {
|
|
||||||
case OpenPgpConstants.RESULT_CODE_SUCCESS: {
|
|
||||||
try {
|
|
||||||
Log.d(OpenPgpConstants.TAG, "result: " + os.toByteArray().length
|
|
||||||
+ " str=" + os.toString("UTF-8"));
|
|
||||||
|
|
||||||
mMessage.setText(os.toString("UTF-8"));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
Log.e(Constants.TAG, "UnsupportedEncodingException", e);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case OpenPgpConstants.RESULT_CODE_USER_INTERACTION_REQUIRED: {
|
|
||||||
PendingIntent pi = result.getParcelable(OpenPgpConstants.RESULT_INTENT);
|
|
||||||
try {
|
|
||||||
OpenPgpProviderActivity.this.startIntentSenderForResult(pi.getIntentSender(),
|
|
||||||
REQUEST_CODE_DECRYPT_AND_VERIFY, null, 0, 0, 0);
|
|
||||||
} catch (IntentSender.SendIntentException e) {
|
|
||||||
Log.e(Constants.TAG, "SendIntentException", e);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -181,6 +181,7 @@ public class RemoteServiceActivity extends ActionBarActivity {
|
|||||||
ArrayList<String> dublicateUserIds = intent
|
ArrayList<String> dublicateUserIds = intent
|
||||||
.getStringArrayListExtra(EXTRA_DUBLICATE_USER_IDS);
|
.getStringArrayListExtra(EXTRA_DUBLICATE_USER_IDS);
|
||||||
|
|
||||||
|
// TODO: do this with spannable instead of HTML to prevent parsing failures with weird user ids
|
||||||
String text = new String();
|
String text = new String();
|
||||||
text += "<b>" + getString(R.string.api_select_pub_keys_text) + "</b>";
|
text += "<b>" + getString(R.string.api_select_pub_keys_text) + "</b>";
|
||||||
text += "<br/><br/>";
|
text += "<br/><br/>";
|
||||||
@ -209,7 +210,7 @@ public class RemoteServiceActivity extends ActionBarActivity {
|
|||||||
new View.OnClickListener() {
|
new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
// sdd key ids to params Bundle for new request
|
// add key ids to params Bundle for new request
|
||||||
Bundle params = extras.getBundle(OpenPgpConstants.PI_RESULT_PARAMS);
|
Bundle params = extras.getBundle(OpenPgpConstants.PI_RESULT_PARAMS);
|
||||||
params.putLongArray(OpenPgpConstants.PARAMS_KEY_IDS,
|
params.putLongArray(OpenPgpConstants.PARAMS_KEY_IDS,
|
||||||
mSelectFragment.getSelectedMasterKeyIds());
|
mSelectFragment.getSelectedMasterKeyIds());
|
||||||
@ -297,12 +298,12 @@ public class RemoteServiceActivity extends ActionBarActivity {
|
|||||||
// return given params again, for calling the service method again
|
// return given params again, for calling the service method again
|
||||||
Intent finishIntent = new Intent();
|
Intent finishIntent = new Intent();
|
||||||
finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params);
|
finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params);
|
||||||
RemoteServiceActivity.this.setResult(RESULT_OK, finishIntent);
|
setResult(RESULT_OK, finishIntent);
|
||||||
} else {
|
} else {
|
||||||
RemoteServiceActivity.this.setResult(RESULT_CANCELED);
|
setResult(RESULT_CANCELED);
|
||||||
}
|
}
|
||||||
|
|
||||||
RemoteServiceActivity.this.finish();
|
finish();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -318,7 +319,7 @@ public class RemoteServiceActivity extends ActionBarActivity {
|
|||||||
Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!");
|
Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!");
|
||||||
// return given params again, for calling the service method again
|
// return given params again, for calling the service method again
|
||||||
Intent finishIntent = new Intent();
|
Intent finishIntent = new Intent();
|
||||||
finishIntent.putExtras(params);
|
finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params);
|
||||||
setResult(RESULT_OK, finishIntent);
|
setResult(RESULT_OK, finishIntent);
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user