package st.wow.git.pgp; import android.util.Log; import android.content.Context; import android.app.Fragment; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.io.InputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; import android.content.Intent; import android.app.PendingIntent; import android.content.IntentSender; import android.content.IntentSender.SendIntentException; import android.os.Handler; public class PgpFragment extends Fragment { OpenPgpServiceConnection mServiceConnection; Context ctx; Handler handler; @Override public void onAttach(Context ctx) { super.onAttach(ctx); this.ctx = ctx; this.handler = new Handler(ctx.getMainLooper()); Log.d("PgpFragment", "onAttach()"); mServiceConnection = new OpenPgpServiceConnection(ctx, "org.sufficientlysecure.keychain"); mServiceConnection.bindToService(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("PgpFragment", "onActivityResult(" + requestCode + "): " + resultCode); super.onActivityResult(requestCode, resultCode, data); activityResult(requestCode, resultCode); } public String Decrypt(byte []dat, int chint) { if (handler == null) { return ""; } final BlockingQueue q = new LinkedBlockingQueue(); handler.post(new Runnable() { public void run() { try { q.put(_decrypt(dat, chint)); } catch (InterruptedException e) { Log.e("gio", "InterruptedException", e); } } }); try { return q.take(); } catch (InterruptedException e) { Log.e("gio", "InterruptedException", e); } return ""; } private String _decrypt(byte []dat, int chint) { String ret = ""; Intent data = new Intent(); data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); InputStream is = new ByteArrayInputStream(dat); ByteArrayOutputStream os = new ByteArrayOutputStream(); OpenPgpApi api = new OpenPgpApi(this.ctx, mServiceConnection.getService()); Intent result = api.executeApi(data, is, os); switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { case OpenPgpApi.RESULT_CODE_SUCCESS: { try { ret = os.toString("UTF-8"); Log.d(OpenPgpApi.TAG, "output: " + ret); } catch (UnsupportedEncodingException e) { ret = ""; Log.e("gio", "UnsupportedEncodingException", e); } break; } case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); try { startIntentSenderForResult(pi.getIntentSender(), chint, null, 0, 0, 0, null); } catch (IntentSender.SendIntentException e) { Log.e("gio", "SendIntentException", e); } break; } case OpenPgpApi.RESULT_CODE_ERROR: { OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); break; } } return ret; } public String Encrypt(byte[] id, byte[] dat, int chint) { if (handler == null) { return ""; } final BlockingQueue q = new LinkedBlockingQueue(); handler.post(new Runnable() { public void run() { try { q.put(_encrypt(id, dat, chint)); } catch (InterruptedException e) { Log.e("gio", "InterruptedException", e); } } }); try { return q.take(); } catch (InterruptedException e) { Log.e("gio", "InterruptedException", e); } return ""; } private String _encrypt(byte[] id, byte[] dat, int chint) { String ret = ""; Intent data = new Intent(); data.setAction(OpenPgpApi.ACTION_ENCRYPT); data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{new String(id)}); data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); InputStream is = new ByteArrayInputStream(dat); ByteArrayOutputStream os = new ByteArrayOutputStream(); OpenPgpApi api = new OpenPgpApi(this.ctx, mServiceConnection.getService()); Intent result = api.executeApi(data, is, os); switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { case OpenPgpApi.RESULT_CODE_SUCCESS: { try { ret = os.toString("UTF-8"); Log.d(OpenPgpApi.TAG, "output: " + ret); } catch (UnsupportedEncodingException e) { Log.e("gio", "UnsupportedEncodingException", e); } if (result.hasExtra(OpenPgpApi.RESULT_SIGNATURE)) { OpenPgpSignatureResult sigResult = result.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE); } break; } case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); try { IntentSender sender = pi.getIntentSender(); Log.d("PgpConnect", "IntentSender:" + sender.toString()); startIntentSenderForResult(sender, chint, null, 0, 0, 0, null); } catch (IntentSender.SendIntentException e) { Log.e("gio", "SendIntentException", e); } break; } case OpenPgpApi.RESULT_CODE_ERROR: { OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); break; } } return ret; } static private native void activityResult(int requestCode, int resultCode); static private native void stringResult(int requestCode, String result); }