diff options
Diffstat (limited to 'APG-API-Demo/src')
4 files changed, 216 insertions, 25 deletions
diff --git a/APG-API-Demo/src/org/thialfihar/android/apg/demo/AidlDemoActivity.java b/APG-API-Demo/src/org/thialfihar/android/apg/demo/AidlDemoActivity.java index 7c08c0e06..65bd3caf5 100644 --- a/APG-API-Demo/src/org/thialfihar/android/apg/demo/AidlDemoActivity.java +++ b/APG-API-Demo/src/org/thialfihar/android/apg/demo/AidlDemoActivity.java @@ -18,9 +18,10 @@ package org.thialfihar.android.apg.demo; import org.thialfihar.android.apg.integration.ApgData; import org.thialfihar.android.apg.integration.ApgIntentHelper; -import org.thialfihar.android.apg.service.IApgEncryptDecryptHandler; -import org.thialfihar.android.apg.service.IApgHelperHandler; -import org.thialfihar.android.apg.service.IApgService; +import org.thialfihar.android.apg.service.IApgApiService; +import org.thialfihar.android.apg.service.handler.IApgDecryptHandler; +import org.thialfihar.android.apg.service.handler.IApgEncryptHandler; +import org.thialfihar.android.apg.service.handler.IApgGetDecryptionKeyIdHandler; import android.app.Activity; import android.app.AlertDialog; @@ -44,10 +45,10 @@ public class AidlDemoActivity extends Activity { ApgIntentHelper mApgIntentHelper; ApgData mApgData; - private IApgService service = null; + private IApgApiService service = null; private ServiceConnection svcConn = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { - service = IApgService.Stub.asInterface(binder); + service = IApgApiService.Stub.asInterface(binder); } public void onServiceDisconnected(ComponentName className) { @@ -69,7 +70,7 @@ public class AidlDemoActivity extends Activity { mApgIntentHelper = new ApgIntentHelper(mActivity); mApgData = new ApgData(); - bindService(new Intent("org.thialfihar.android.apg.service.IApgService"), svcConn, + bindService(new Intent("org.thialfihar.android.apg.service.IApgApiService"), svcConn, Context.BIND_AUTO_CREATE); } @@ -77,8 +78,8 @@ public class AidlDemoActivity extends Activity { byte[] inputBytes = mMessageTextView.getText().toString().getBytes(); try { - service.encryptAsymmetric(inputBytes, null, true, 0, mApgData.getEncryptionKeys(), 7, - encryptDecryptHandler); + service.encryptAsymmetric(inputBytes, null, true, 0, mApgData.getPublicKeys(), 7, + encryptHandler); } catch (RemoteException e) { exceptionImplementation(-1, e.toString()); } @@ -88,7 +89,7 @@ public class AidlDemoActivity extends Activity { byte[] inputBytes = mCiphertextTextView.getText().toString().getBytes(); try { - service.decryptAndVerifyAsymmetric(inputBytes, null, null, encryptDecryptHandler); + service.decryptAndVerifyAsymmetric(inputBytes, null, null, decryptHandler); } catch (RemoteException e) { exceptionImplementation(-1, e.toString()); } @@ -116,7 +117,7 @@ public class AidlDemoActivity extends Activity { builder.setTitle("Exception!").setMessage(error).setPositiveButton("OK", null).show(); } - private final IApgEncryptDecryptHandler.Stub encryptDecryptHandler = new IApgEncryptDecryptHandler.Stub() { + private final IApgEncryptHandler.Stub encryptHandler = new IApgEncryptHandler.Stub() { @Override public void onException(final int exceptionId, final String message) throws RemoteException { @@ -128,8 +129,7 @@ public class AidlDemoActivity extends Activity { } @Override - public void onSuccessEncrypt(final byte[] outputBytes, String outputUri) - throws RemoteException { + public void onSuccess(final byte[] outputBytes, String outputUri) throws RemoteException { runOnUiThread(new Runnable() { public void run() { mApgData.setEncryptedData(new String(outputBytes)); @@ -138,8 +138,21 @@ public class AidlDemoActivity extends Activity { }); } + }; + + private final IApgDecryptHandler.Stub decryptHandler = new IApgDecryptHandler.Stub() { + + @Override + public void onException(final int exceptionId, final String message) throws RemoteException { + runOnUiThread(new Runnable() { + public void run() { + exceptionImplementation(exceptionId, message); + } + }); + } + @Override - public void onSuccessDecrypt(final byte[] outputBytes, String outputUri, boolean signature, + public void onSuccess(final byte[] outputBytes, String outputUri, boolean signature, long signatureKeyId, String signatureUserId, boolean signatureSuccess, boolean signatureUnknown) throws RemoteException { runOnUiThread(new Runnable() { @@ -153,7 +166,7 @@ public class AidlDemoActivity extends Activity { }; - private final IApgHelperHandler.Stub helperHandler = new IApgHelperHandler.Stub() { + private final IApgGetDecryptionKeyIdHandler.Stub helperHandler = new IApgGetDecryptionKeyIdHandler.Stub() { @Override public void onException(final int exceptionId, final String message) throws RemoteException { @@ -165,7 +178,7 @@ public class AidlDemoActivity extends Activity { } @Override - public void onSuccessGetDecryptionKey(long arg0, boolean arg1) throws RemoteException { + public void onSuccess(long arg0, boolean arg1) throws RemoteException { // TODO Auto-generated method stub } @@ -182,7 +195,7 @@ public class AidlDemoActivity extends Activity { } public void selectEncryptionKeysOnClick(View view) { - mApgIntentHelper.selectEncryptionKeys("user@example.com"); + mApgIntentHelper.selectPublicKeys("user@example.com"); } diff --git a/APG-API-Demo/src/org/thialfihar/android/apg/demo/AidlDemoActivity2.java b/APG-API-Demo/src/org/thialfihar/android/apg/demo/AidlDemoActivity2.java new file mode 100644 index 000000000..51054f58a --- /dev/null +++ b/APG-API-Demo/src/org/thialfihar/android/apg/demo/AidlDemoActivity2.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.thialfihar.android.apg.demo; + +import java.util.ArrayList; +import java.util.List; + +import org.thialfihar.android.apg.integration.ApgData; +import org.thialfihar.android.apg.integration.ApgIntentHelper; +import org.thialfihar.android.apg.service.IApgKeyService; +import org.thialfihar.android.apg.service.handler.IApgGetKeyringsHandler; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.app.AlertDialog; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Base64; +import android.view.View; +import android.widget.TextView; + +public class AidlDemoActivity2 extends Activity { + Activity mActivity; + + TextView mKeyringsTextView; + + ApgIntentHelper mApgIntentHelper; + ApgData mApgData; + + byte[] keysBytes; + ArrayList<String> keysStrings; + + private IApgKeyService service = null; + private ServiceConnection svcConn = new ServiceConnection() { + public void onServiceConnected(ComponentName className, IBinder binder) { + service = IApgKeyService.Stub.asInterface(binder); + } + + public void onServiceDisconnected(ComponentName className) { + service = null; + } + }; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setContentView(R.layout.aidl_demo2); + + mActivity = this; + + mKeyringsTextView = (TextView) findViewById(R.id.aidl_demo_keyrings); + + mApgIntentHelper = new ApgIntentHelper(mActivity); + mApgData = new ApgData(); + + bindService(new Intent("org.thialfihar.android.apg.service.IApgKeyService"), svcConn, + Context.BIND_AUTO_CREATE); + } + + public void getKeyringsStringsOnClick(View view) { + try { + service.getPublicKeyRings(mApgData.getPublicKeys(), true, getKeyringsHandler); + } catch (RemoteException e) { + exceptionImplementation(-1, e.toString()); + } + } + + public void getKeyringsBytesOnClick(View view) { + try { + service.getPublicKeyRings(mApgData.getPublicKeys(), false, getKeyringsHandler); + } catch (RemoteException e) { + exceptionImplementation(-1, e.toString()); + } + } + + @SuppressLint("NewApi") + private void updateView() { + if (keysBytes != null) { + mKeyringsTextView.setText(Base64.encodeToString(keysBytes, Base64.DEFAULT)); + } else if (keysStrings != null) { + mKeyringsTextView.setText(""); + for (String output : keysStrings) { + mKeyringsTextView.append(output); + } + } + } + + @Override + public void onDestroy() { + super.onDestroy(); + + unbindService(svcConn); + } + + private void exceptionImplementation(int exceptionId, String error) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("Exception!").setMessage(error).setPositiveButton("OK", null).show(); + } + + private final IApgGetKeyringsHandler.Stub getKeyringsHandler = new IApgGetKeyringsHandler.Stub() { + + @Override + public void onException(final int exceptionId, final String message) throws RemoteException { + runOnUiThread(new Runnable() { + public void run() { + exceptionImplementation(exceptionId, message); + } + }); + } + + @Override + public void onSuccess(final byte[] outputBytes, final List<String> outputStrings) + throws RemoteException { + runOnUiThread(new Runnable() { + public void run() { + if (outputBytes != null) { + keysBytes = outputBytes; + keysStrings = null; + } else if (outputStrings != null) { + keysBytes = null; + keysStrings = (ArrayList<String>) outputStrings; + } + updateView(); + } + }); + + } + + }; + + public void selectEncryptionKeysOnClick(View view) { + mApgIntentHelper.selectPublicKeys("user@example.com"); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // this updates the mApgData object to the result of the methods + boolean result = mApgIntentHelper.onActivityResult(requestCode, resultCode, data, mApgData); + if (result) { + updateView(); + } + + // continue with other activity results + super.onActivityResult(requestCode, resultCode, data); + } + +} diff --git a/APG-API-Demo/src/org/thialfihar/android/apg/demo/BaseActivity.java b/APG-API-Demo/src/org/thialfihar/android/apg/demo/BaseActivity.java index 66c3a9a0e..5e2108c50 100644 --- a/APG-API-Demo/src/org/thialfihar/android/apg/demo/BaseActivity.java +++ b/APG-API-Demo/src/org/thialfihar/android/apg/demo/BaseActivity.java @@ -29,8 +29,9 @@ public class BaseActivity extends PreferenceActivity { private Activity mActivity; private Preference mIntentDemo; - private Preference mAidlDemo; private Preference mContentProviderDemo; + private Preference mAidlDemo; + private Preference mAidlDemo2; /** * Called when the activity is first created. @@ -46,8 +47,9 @@ public class BaseActivity extends PreferenceActivity { // find preferences mIntentDemo = (Preference) findPreference("intent_demo"); - mAidlDemo = (Preference) findPreference("aidl_demo"); mContentProviderDemo = (Preference) findPreference("content_provider_demo"); + mAidlDemo = (Preference) findPreference("aidl_demo"); + mAidlDemo2 = (Preference) findPreference("aidl_demo2"); mIntentDemo.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override @@ -58,6 +60,15 @@ public class BaseActivity extends PreferenceActivity { } }); + mContentProviderDemo.setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + startActivity(new Intent(mActivity, ContentProviderDemoActivity.class)); + + return false; + } + }); + mAidlDemo.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { @@ -67,14 +78,15 @@ public class BaseActivity extends PreferenceActivity { } }); - mContentProviderDemo.setOnPreferenceClickListener(new OnPreferenceClickListener() { + mAidlDemo2.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { - startActivity(new Intent(mActivity, ContentProviderDemoActivity.class)); + startActivity(new Intent(mActivity, AidlDemoActivity2.class)); return false; } }); + } } diff --git a/APG-API-Demo/src/org/thialfihar/android/apg/demo/IntentDemoActivity.java b/APG-API-Demo/src/org/thialfihar/android/apg/demo/IntentDemoActivity.java index b707e48a8..b789dccb1 100644 --- a/APG-API-Demo/src/org/thialfihar/android/apg/demo/IntentDemoActivity.java +++ b/APG-API-Demo/src/org/thialfihar/android/apg/demo/IntentDemoActivity.java @@ -65,17 +65,17 @@ public class IntentDemoActivity extends Activity { } public void selectEncryptionKeysOnClick(View view) { - mApgIntentHelper.selectEncryptionKeys("user@example.com"); + mApgIntentHelper.selectPublicKeys("user@example.com"); } public void encryptOnClick(View view) { - mApgIntentHelper.encrypt(mMessageTextView.getText().toString(), - mApgData.getEncryptionKeys(), mApgData.getSignatureKeyId(), false); + mApgIntentHelper.encrypt(mMessageTextView.getText().toString(), mApgData.getPublicKeys(), + mApgData.getSecretKeyId(), false); } public void encryptAndReturnOnClick(View view) { - mApgIntentHelper.encrypt(mMessageTextView.getText().toString(), - mApgData.getEncryptionKeys(), mApgData.getSignatureKeyId(), true); + mApgIntentHelper.encrypt(mMessageTextView.getText().toString(), mApgData.getPublicKeys(), + mApgData.getSecretKeyId(), true); } public void decryptOnClick(View view) { |