integration lib and demo

This commit is contained in:
Dominik 2012-04-11 19:29:27 +02:00
parent e092016518
commit 2f58c3d86b
39 changed files with 1048 additions and 606 deletions

View file

@ -2,8 +2,8 @@
<!-- Copyright (C) 2010 Thialfihar <thi@thialfihar.org> 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. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto"
package="org.apg"
android:installLocation="auto"
android:versionCode="50"
android:versionName="1.1" >
@ -80,7 +80,8 @@
android:name=".ui.SelectPublicKeyListActivity"
android:configChanges="keyboardHidden|orientation|keyboard"
android:label="@string/title_selectRecipients"
android:launchMode="singleTop" >
android:launchMode="singleTop"
android:uiOptions="splitActionBarWhenNarrow" >
<intent-filter>
<action android:name="org.apg.intent.SELECT_PUBLIC_KEYS" />

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="APG" default="help">
<project name="MainActivity" default="help">
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
@ -45,21 +45,19 @@
unless="sdk.dir"
/>
<!-- extension targets. Uncomment the ones where you want to do custom work
in between standard targets -->
<!--
<target name="-pre-build">
</target>
<target name="-pre-compile">
</target>
/* This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir} */
<target name="-post-compile">
</target>
-->
<!--
Import per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean
-->
<import file="custom_rules.xml" optional="true" />
<!-- Import the actual build file.

View file

@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
<!--
Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -14,42 +15,21 @@
limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
android:fillViewport="true"
android:orientation="vertical" >
<include android:id="@+id/layout_filter" layout="@layout/filter_info"/>
<include
android:id="@+id/layout_filter"
layout="@layout/filter_info" />
<ListView
android:id="@+id/list"
android:choiceMode="multipleChoice"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
android:layout_weight="1"
android:choiceMode="multipleChoice" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/ButtonBar">
<Button
android:text="@android:string/ok"
android:id="@+id/btn_ok"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:text="@android:string/cancel"
android:id="@+id/btn_cancel"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

View file

@ -47,7 +47,7 @@
<string name="title_signKey">Sign Key</string>
<string name="title_about">About</string>
<string name="title_help">Help</string>
<!-- section_lowerCase: capitalized words, no punctuation -->
<string name="section_userIds">User IDs</string>
<string name="section_keys">Keys</string>
@ -74,6 +74,7 @@
<string name="btn_doNotSave">Cancel</string>
<string name="btn_delete">Delete</string>
<string name="btn_noDate">None</string>
<string name="btn_okay">Okay</string>
<string name="btn_clearFilter">Clear Filter</string>
<string name="btn_changePassPhrase">Change Pass Phrase</string>
<string name="btn_setPassPhrase">Set Pass Phrase</string>

View file

@ -50,6 +50,7 @@ public final class Id {
public static final int reply = 0x21070016;
public static final int cancel = 0x21070017;
public static final int save = 0x21070018;
public static final int okay = 0x21070019;
}
}

View file

@ -66,10 +66,10 @@ public class EditKeyActivity extends BaseActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(2, Id.menu.option.cancel, 0, R.string.btn_doNotSave)
menu.add(1, Id.menu.option.cancel, 0, R.string.btn_doNotSave)
// .setIcon(R.drawable.ic_menu_search_holo_light)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(2, Id.menu.option.save, 1, R.string.btn_save)
menu.add(1, Id.menu.option.save, 1, R.string.btn_save)
// .setIcon(R.drawable.ic_suggestions_add)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;

View file

@ -78,6 +78,7 @@ public class MainActivity extends BaseActivity {
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
// if (!mPreferences.hasSeenHelp()) {
// showDialog(Id.dialog.help);

View file

@ -22,7 +22,9 @@ import org.apg.Apg;
import org.apg.Id;
import org.apg.R;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import android.app.SearchManager;
import android.content.Intent;
@ -43,6 +45,12 @@ public class SelectPublicKeyListActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
setContentView(R.layout.select_public_key);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
@ -51,20 +59,6 @@ public class SelectPublicKeyListActivity extends BaseActivity {
// needed in Android 1.5, where the XML attribute gets ignored
mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button okButton = (Button) findViewById(R.id.btn_ok);
okButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
okClicked();
}
});
Button cancelButton = (Button) findViewById(R.id.btn_cancel);
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
cancelClicked();
}
});
mFilterLayout = findViewById(R.id.layout_filter);
mFilterInfo = (TextView) mFilterLayout.findViewById(R.id.filterInfo);
mClearFilterButton = (Button) mFilterLayout.findViewById(R.id.btn_clear);
@ -166,6 +160,29 @@ public class SelectPublicKeyListActivity extends BaseActivity {
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, Id.menu.option.search, 0, R.string.menu_search).setIcon(
android.R.drawable.ic_menu_search);
menu.add(1, Id.menu.option.cancel, 0, R.string.btn_doNotSave).setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(1, Id.menu.option.okay, 1, R.string.btn_okay).setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Id.menu.option.okay:
okClicked();
return true;
case Id.menu.option.cancel:
cancelClicked();
return true;
default:
break;
}
return false;
}
}

View file

@ -20,6 +20,7 @@ import org.apg.Apg;
import org.apg.Id;
import org.apg.R;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import android.app.SearchManager;
@ -46,10 +47,15 @@ public class SelectSecretKeyListActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
setContentView(R.layout.select_secret_key);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
mList = (ListView) findViewById(R.id.list);
mList.setOnItemClickListener(new OnItemClickListener() {

View file

@ -1,40 +0,0 @@
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}

View file

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send Intent" />
</LinearLayout>

View file

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Intent Demos" >
<Preference
android:key="intent_demo_1"
android:title="Intent Demo 1" />
<Preference
android:key="intent_demo_2"
android:title="Intent Demo 2" />
</PreferenceCategory>
<PreferenceCategory android:title="AIDL Demos" >
<Preference
android:key="aidl_demo"
android:title="AIDL Demo 1" />
</PreferenceCategory>
</PreferenceScreen>

View file

@ -1,44 +0,0 @@
/*
* 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.apg.api_demo;
import org.apg.api_demo.R;
import android.app.Activity;
import android.os.Bundle;
public class IntentDemo1Activity extends Activity {
Activity mActivity;
/**
* Instantiate View for this Activity
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_demo_1);
// org.apg.intent.SELECT_PUBLIC_KEYS
// get back:
// long[] selectedKeyIds data.putExtra(Apg.EXTRA_SELECTION, selectedKeyIds);
// String[] userIds data.putExtra(Apg.EXTRA_USER_IDS, userIds.toArray(userIdArray));
mActivity = this;
}
}

View file

@ -1,233 +0,0 @@
/*
* Copyright (C) 2010-2011 K-9 Mail Contributors
* 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.apg.api_demo.util;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.widget.Toast;
public class IntentHelper {
/**
* Select the signature key.
*
* @param activity
* @param pgpData
* @return success or failure
*/
public boolean selectSecretKey(Activity activity) {
android.content.Intent intent = new android.content.Intent(
Constants.Intent.SELECT_SECRET_KEY);
intent.putExtra(Constants.EXTRA_INTENT_VERSION, Constants.INTENT_VERSION);
try {
activity.startActivityForResult(intent, Constants.SELECT_SECRET_KEY);
return true;
} catch (ActivityNotFoundException e) {
activityNotFound(activity);
return false;
}
}
/**
* Start the encrypt activity.
*
* @param activity
* @param data
* @param pgpData
* @return success or failure
*/
public boolean encrypt(Activity activity, String data, long[] encryptionKeyIds,
long signatureKeyId) {
android.content.Intent intent = new android.content.Intent(
Constants.Intent.ENCRYPT_AND_RETURN);
intent.putExtra(Constants.EXTRA_INTENT_VERSION, Constants.INTENT_VERSION);
intent.setType("text/plain");
intent.putExtra(Constants.EXTRA_TEXT, data);
intent.putExtra(Constants.EXTRA_ENCRYPTION_KEY_IDS, encryptionKeyIds);
intent.putExtra(Constants.EXTRA_SIGNATURE_KEY_ID, signatureKeyId);
try {
activity.startActivityForResult(intent, Constants.ENCRYPT_MESSAGE);
return true;
} catch (ActivityNotFoundException e) {
activityNotFound(activity);
return false;
}
}
/**
* Start the decrypt activity.
*
* @param activity
* @param data
* @param pgpData
* @return success or failure
*/
public boolean decrypt(Activity activity, String data) {
android.content.Intent intent = new android.content.Intent(
Constants.Intent.DECRYPT_AND_RETURN);
intent.putExtra(Constants.EXTRA_INTENT_VERSION, Constants.INTENT_VERSION);
intent.setType("text/plain");
if (data == null) {
return false;
}
try {
intent.putExtra(Constants.EXTRA_TEXT, data);
activity.startActivityForResult(intent, Constants.DECRYPT_MESSAGE);
return true;
} catch (ActivityNotFoundException e) {
activityNotFound(activity);
return false;
}
}
private void activityNotFound(Activity activity) {
Toast.makeText(activity, "APG Activity not found! Is APG installed correctly?",
Toast.LENGTH_LONG).show();
}
/**
* Handle the activity results that concern us.
*
* @param activity
* @param requestCode
* @param resultCode
* @param data
* @return handled or not
*/
public boolean onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case Constants.SELECT_SECRET_KEY:
// if (resultCode != Activity.RESULT_OK || data == null) {
// break;
// }
// pgpData.setSignatureKeyId(data.getLongExtra(Apg.EXTRA_KEY_ID, 0));
// pgpData.setSignatureUserId(data.getStringExtra(Apg.EXTRA_USER_ID));
// ((MessageCompose) activity).updateEncryptLayout();
break;
case Constants.SELECT_PUBLIC_KEYS:
// if (resultCode != Activity.RESULT_OK || data == null) {
// pgpData.setEncryptionKeys(null);
// ((MessageCompose) activity).onEncryptionKeySelectionDone();
// break;
// }
// pgpData.setEncryptionKeys(data.getLongArrayExtra(Apg.EXTRA_SELECTION));
// ((MessageCompose) activity).onEncryptionKeySelectionDone();
break;
case Constants.ENCRYPT_MESSAGE:
// if (resultCode != Activity.RESULT_OK || data == null) {
// pgpData.setEncryptionKeys(null);
// ((MessageCompose) activity).onEncryptDone();
// break;
// }
// pgpData.setEncryptedData(data.getStringExtra(Apg.EXTRA_ENCRYPTED_MESSAGE));
// // this was a stupid bug in an earlier version, just gonna leave this in for an APG
// // version or two
// if (pgpData.getEncryptedData() == null) {
// pgpData.setEncryptedData(data.getStringExtra(Apg.EXTRA_DECRYPTED_MESSAGE));
// }
// if (pgpData.getEncryptedData() != null) {
// ((MessageCompose) activity).onEncryptDone();
// }
break;
case Constants.DECRYPT_MESSAGE:
if (resultCode != Activity.RESULT_OK || data == null) {
break;
}
// pgpData.setSignatureUserId(data.getStringExtra(Apg.EXTRA_SIGNATURE_USER_ID));
// pgpData.setSignatureKeyId(data.getLongExtra(Apg.EXTRA_SIGNATURE_KEY_ID, 0));
// pgpData.setSignatureSuccess(data.getBooleanExtra(Apg.EXTRA_SIGNATURE_SUCCESS,
// false));
// pgpData.setSignatureUnknown(data.getBooleanExtra(Apg.EXTRA_SIGNATURE_UNKNOWN,
// false));
//
// pgpData.setDecryptedData(data.getStringExtra(Apg.EXTRA_DECRYPTED_MESSAGE));
// ((MessageView) activity).onDecryptDone(pgpData);
break;
default:
return false;
}
return true;
}
//
// /**
// * Select encryption keys.
// *
// * @param activity
// * @param emails
// * The emails that should be used for preselection.
// * @param pgpData
// * @return success or failure
// */
// public boolean selectEncryptionKeys(Activity activity, String emails) {
// android.content.Intent intent = new android.content.Intent(Apg.Intent.SELECT_PUBLIC_KEYS);
// intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION);
// long[] initialKeyIds = null;
// if (!pgpData.hasEncryptionKeys()) {
// List<Long> keyIds = new ArrayList<Long>();
// if (pgpData.hasSignatureKey()) {
// keyIds.add(pgpData.getSignatureKeyId());
// }
//
// try {
// Uri contentUri = Uri.withAppendedPath(Apg.CONTENT_URI_PUBLIC_KEY_RING_BY_EMAILS,
// emails);
// Cursor c = activity.getContentResolver().query(contentUri,
// new String[] { "master_key_id" }, null, null, null);
// if (c != null) {
// while (c.moveToNext()) {
// keyIds.add(c.getLong(0));
// }
// }
//
// if (c != null) {
// c.close();
// }
// } catch (SecurityException e) {
// Toast.makeText(activity,
// activity.getResources().getString(R.string.insufficient_apg_permissions),
// Toast.LENGTH_LONG).show();
// }
// if (!keyIds.isEmpty()) {
// initialKeyIds = new long[keyIds.size()];
// for (int i = 0, size = keyIds.size(); i < size; ++i) {
// initialKeyIds[i] = keyIds.get(i);
// }
// }
// } else {
// initialKeyIds = pgpData.getEncryptionKeys();
// }
// intent.putExtra(Apg.EXTRA_SELECTION, initialKeyIds);
// try {
// activity.startActivityForResult(intent, Apg.SELECT_PUBLIC_KEYS);
// return true;
// } catch (ActivityNotFoundException e) {
// Toast.makeText(activity, R.string.error_activity_not_found, Toast.LENGTH_SHORT).show();
// return false;
// }
// }
}

View file

@ -1,181 +0,0 @@
/*
* Copyright (C) 2010-2011 K-9 Mail Contributors
* 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.apg.api_demo.util;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.widget.Toast;
public class ProviderHelper {
/**
* Get secret key ids based on a given email.
*
* @param context
* @param email
* The email in question.
* @return key ids
*/
public long[] getSecretKeyIdsFromEmail(Context context, String email) {
long ids[] = null;
try {
Uri contentUri = Uri.withAppendedPath(Constants.CONTENT_URI_SECRET_KEY_RING_BY_EMAILS,
email);
Cursor c = context.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null && c.getCount() > 0) {
ids = new long[c.getCount()];
while (c.moveToNext()) {
ids[c.getPosition()] = c.getLong(0);
}
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissionsInfo(context);
}
return ids;
}
/**
* Get public key ids based on a given email.
*
* @param context
* @param email
* The email in question.
* @return key ids
*/
public long[] getPublicKeyIdsFromEmail(Context context, String email) {
long ids[] = null;
try {
Uri contentUri = Uri.withAppendedPath(Constants.CONTENT_URI_PUBLIC_KEY_RING_BY_EMAILS,
email);
Cursor c = context.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null && c.getCount() > 0) {
ids = new long[c.getCount()];
while (c.moveToNext()) {
ids[c.getPosition()] = c.getLong(0);
}
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissionsInfo(context);
}
return ids;
}
/**
* Find out if a given email has a secret key.
*
* @param context
* @param email
* The email in question.
* @return true if there is a secret key for this email.
*/
public boolean hasSecretKeyForEmail(Context context, String email) {
try {
Uri contentUri = Uri.withAppendedPath(Constants.CONTENT_URI_SECRET_KEY_RING_BY_EMAILS,
email);
Cursor c = context.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null && c.getCount() > 0) {
c.close();
return true;
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissionsInfo(context);
}
return false;
}
/**
* Find out if a given email has a public key.
*
* @param context
* @param email
* The email in question.
* @return true if there is a public key for this email.
*/
public boolean hasPublicKeyForEmail(Context context, String email) {
try {
Uri contentUri = Uri.withAppendedPath(Constants.CONTENT_URI_PUBLIC_KEY_RING_BY_EMAILS,
email);
Cursor c = context.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null && c.getCount() > 0) {
c.close();
return true;
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissionsInfo(context);
}
return false;
}
/**
* Get the user id based on the key id.
*
* @param context
* @param keyId
* @return user id
*/
public String getUserId(Context context, long keyId) {
String userId = null;
try {
Uri contentUri = ContentUris.withAppendedId(
Constants.CONTENT_URI_SECRET_KEY_RING_BY_KEY_ID, keyId);
Cursor c = context.getContentResolver().query(contentUri, new String[] { "user_id" },
null, null, null);
if (c != null && c.moveToFirst()) {
userId = c.getString(0);
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissionsInfo(context);
}
if (userId == null) {
userId = "unknown";
}
return userId;
}
private void insufficientPermissionsInfo(Context context) {
Toast.makeText(context, "Permission to access APG Provider is missing!", Toast.LENGTH_LONG)
.show();
}
}

View file

@ -4,8 +4,11 @@
android:versionCode="1"
android:versionName="1.0" >
<!-- permission to access provider and service, intents are public! -->
<uses-permission android:name="org.apg.permission.READ_KEY_DETAILS" />
<uses-sdk
android:minSdkVersion="4"
android:minSdkVersion="7"
android:targetSdkVersion="14" />
<application
@ -21,7 +24,7 @@
</intent-filter>
</activity>
<activity
android:name=".IntentDemo1Activity"
android:name=".IntentDemoActivity"
android:label="Intent Demo 1" />
</application>

View file

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="BaseActivity" default="help">
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems. -->
<property file="local.properties" />
<!-- The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="ant.properties" />
<!-- The project.properties file is created and updated by the 'android'
tool, as well as ADT.
This contains project specific properties such as project target, and library
dependencies. Lower level build properties are stored in ant.properties
(or in .classpath for Eclipse projects).
This file is an integral part of the build system for your
application and should be checked into Version Control Systems. -->
<loadproperties srcFile="project.properties" />
<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
unless="sdk.dir"
/>
<!--
Import per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean
-->
<import file="custom_rules.xml" optional="true" />
<!-- Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>

View file

@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

View file

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View file

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

View file

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

View file

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/intent_demo_select_secret_key"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="intentDemoSelectSecretKeyOnClick"
android:text="Select secret key" />
<Button
android:id="@+id/intent_demo_select_encryption_key"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="intentDemoSelectEncryptionKeysOnClick"
android:text="Select encryption key(s)" />
<EditText
android:id="@+id/intent_demo_message"
android:layout_width="match_parent"
android:layout_height="150dip"
android:text="message"
android:textAppearance="@android:style/TextAppearance.Small" />
<EditText
android:id="@+id/intent_demo_ciphertext"
android:layout_width="match_parent"
android:layout_height="150dip"
android:text="ciphertext"
android:textAppearance="@android:style/TextAppearance.Small" />
<Button
android:id="@+id/intent_demo_encrypt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="intentDemoEncryptOnClick"
android:text="Encrypt" />
<Button
android:id="@+id/intent_demo_decrypt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="intentDemoDecryptOnClick"
android:text="Decrypt" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="APG Data:" />
<TextView
android:id="@+id/intent_demo_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minLines="10" />
</LinearLayout>
</ScrollView>

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Intent" >
<Preference
android:key="intent_demo"
android:title="Intent Demo" />
</PreferenceCategory>
<PreferenceCategory android:title="AIDL" >
<Preference
android:key="aidl_demo"
android:title="AIDL Demo" />
</PreferenceCategory>
</PreferenceScreen>

View file

@ -17,6 +17,7 @@
package org.apg.api_demo;
import org.apg.api_demo.R;
import org.apg.integration.ApgIntentHelper;
import android.app.Activity;
import android.content.Intent;
@ -24,11 +25,13 @@ import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;
public class BaseActivity extends PreferenceActivity {
private Activity mActivity;
private Preference mIntentDemo;
private Preference mAidlDemo;
/**
* Called when the activity is first created.
@ -43,16 +46,26 @@ public class BaseActivity extends PreferenceActivity {
addPreferencesFromResource(R.xml.base_preference);
// find preferences
mIntentDemo = (Preference) findPreference("intent_demo_1");
mIntentDemo = (Preference) findPreference("intent_demo");
mAidlDemo = (Preference) findPreference("aidl_demo");
mIntentDemo.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivity(new Intent(mActivity, IntentDemo1Activity.class));
startActivity(new Intent(mActivity, IntentDemoActivity.class));
return false;
}
});
mAidlDemo.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(mActivity, "Currently not implemented!", Toast.LENGTH_LONG).show();
return false;
}
});
}
}

View file

@ -0,0 +1,95 @@
/*
* 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.apg.api_demo;
import org.apg.api_demo.R;
import org.apg.integration.ApgData;
import org.apg.integration.ApgIntentHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class IntentDemoActivity extends Activity {
Activity mActivity;
ApgIntentHelper mApgIntentHelper;
TextView mMessageTextView;
TextView mCiphertextTextView;
TextView mDataTextView;
ApgData mApgData;
/**
* Instantiate View for this Activity
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_demo);
mActivity = this;
mApgIntentHelper = new ApgIntentHelper(mActivity);
mMessageTextView = (TextView) findViewById(R.id.intent_demo_message);
mCiphertextTextView = (TextView) findViewById(R.id.intent_demo_ciphertext);
mDataTextView = (TextView) findViewById(R.id.intent_demo_data);
mApgData = new ApgData();
}
public void intentDemoSelectSecretKeyOnClick(View view) {
mApgIntentHelper.selectSecretKey();
}
public void intentDemoSelectEncryptionKeysOnClick(View view) {
mApgIntentHelper.selectEncryptionKeys("usera@example.com");
}
public void intentDemoEncryptOnClick(View view) {
mApgIntentHelper.encrypt(mMessageTextView.getText().toString(),
mApgData.getEncryptionKeys(), mApgData.getSignatureKeyId());
}
public void intentDemoDecryptOnClick(View view) {
mApgIntentHelper.decrypt(mCiphertextTextView.getText().toString());
}
@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);
}
private void updateView() {
if (mApgData.getDecryptedData() != null) {
mMessageTextView.setText(mApgData.getDecryptedData());
}
if (mApgData.getEncryptedData() != null) {
mCiphertextTextView.setText(mApgData.getEncryptedData());
}
mDataTextView.setText(mApgData.toString());
}
}

23
org_apg_integration_lib/.gitignore vendored Normal file
View file

@ -0,0 +1,23 @@
#Android generated
bin
gen
obj
libs/armeabi
lint.xml
local.properties
#Eclipse
.project
.classpath
.settings
#IntelliJ IDEA
.idea
*.iml
#Maven
target
release.properties
#Mac
.DS_Store

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.apg.integration"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="14" />
</manifest>

View file

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="org_apg_integration_lib" default="help">
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems. -->
<property file="local.properties" />
<!-- The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="ant.properties" />
<!-- The project.properties file is created and updated by the 'android'
tool, as well as ADT.
This contains project specific properties such as project target, and library
dependencies. Lower level build properties are stored in ant.properties
(or in .classpath for Eclipse projects).
This file is an integral part of the build system for your
application and should be checked into Version Control Systems. -->
<loadproperties srcFile="project.properties" />
<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
unless="sdk.dir"
/>
<!--
Import per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean
-->
<import file="custom_rules.xml" optional="true" />
<!-- Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>

View file

@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

View file

@ -0,0 +1,15 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-15
android.library=true

View file

@ -0,0 +1,107 @@
/*
* Copyright (C) 2010-2011 K-9 Mail Contributors
* 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.apg.integration;
import java.io.Serializable;
import java.util.Arrays;
public class ApgData implements Serializable {
private static final long serialVersionUID = 6314045536270848410L;
protected long mEncryptionKeyIds[] = null;
protected long mSignatureKeyId = 0;
protected String mSignatureUserId = null;
protected boolean mSignatureSuccess = false;
protected boolean mSignatureUnknown = false;
protected String mDecryptedData = null;
protected String mEncryptedData = null;
public void setSignatureKeyId(long keyId) {
mSignatureKeyId = keyId;
}
public long getSignatureKeyId() {
return mSignatureKeyId;
}
public void setEncryptionKeys(long keyIds[]) {
mEncryptionKeyIds = keyIds;
}
public long[] getEncryptionKeys() {
return mEncryptionKeyIds;
}
public boolean hasSignatureKey() {
return mSignatureKeyId != 0;
}
public boolean hasEncryptionKeys() {
return (mEncryptionKeyIds != null) && (mEncryptionKeyIds.length > 0);
}
public String getEncryptedData() {
return mEncryptedData;
}
public void setEncryptedData(String data) {
mEncryptedData = data;
}
public String getDecryptedData() {
return mDecryptedData;
}
public void setDecryptedData(String data) {
mDecryptedData = data;
}
public void setSignatureUserId(String userId) {
mSignatureUserId = userId;
}
public String getSignatureUserId() {
return mSignatureUserId;
}
public boolean getSignatureSuccess() {
return mSignatureSuccess;
}
public void setSignatureSuccess(boolean success) {
mSignatureSuccess = success;
}
public boolean getSignatureUnknown() {
return mSignatureUnknown;
}
public void setSignatureUnknown(boolean unknown) {
mSignatureUnknown = unknown;
}
@Override
public String toString() {
String output = "mEncryptionKeyIds: " + Arrays.toString(mEncryptionKeyIds)
+ "\nmSignatureKeyId: " + mSignatureKeyId + "\nmSignatureUserId: "
+ mSignatureUserId + "\nmSignatureSuccess: " + mSignatureSuccess
+ "\nmSignatureUnknown: " + mSignatureUnknown + "\nmDecryptedData: "
+ mDecryptedData + "\nmEncryptedData: " + mEncryptedData;
return output;
}
}

View file

@ -0,0 +1,397 @@
/*
* Copyright (C) 2010-2011 K-9 Mail Contributors
* 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.apg.integration;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.widget.Toast;
public class ApgIntentHelper {
private Activity activity;
public ApgIntentHelper(Activity activity) {
this.activity = activity;
}
/**
* Select the signature key.
*
* @param activity
* @param pgpData
* @return success or failure
*/
public boolean selectSecretKey() {
Intent intent = new Intent(Constants.Intent.SELECT_SECRET_KEY);
intent.putExtra(Constants.EXTRA_INTENT_VERSION, Constants.INTENT_VERSION);
try {
activity.startActivityForResult(intent, Constants.SELECT_SECRET_KEY);
return true;
} catch (ActivityNotFoundException e) {
activityNotFound();
return false;
}
}
/**
* Start the encrypt activity.
*
* @param activity
* @param data
* @param pgpData
* @return success or failure
*/
public boolean encrypt(String data, long[] encryptionKeyIds, long signatureKeyId) {
Intent intent = new Intent(Constants.Intent.ENCRYPT_AND_RETURN);
intent.putExtra(Constants.EXTRA_INTENT_VERSION, Constants.INTENT_VERSION);
intent.setType("text/plain");
intent.putExtra(Constants.EXTRA_TEXT, data);
intent.putExtra(Constants.EXTRA_ENCRYPTION_KEY_IDS, encryptionKeyIds);
intent.putExtra(Constants.EXTRA_SIGNATURE_KEY_ID, signatureKeyId);
try {
activity.startActivityForResult(intent, Constants.ENCRYPT_MESSAGE);
return true;
} catch (ActivityNotFoundException e) {
activityNotFound();
return false;
}
}
/**
* Start the decrypt activity.
*
* @param activity
* @param data
* @param pgpData
* @return success or failure
*/
public boolean decrypt(String data) {
Intent intent = new Intent(Constants.Intent.DECRYPT_AND_RETURN);
intent.putExtra(Constants.EXTRA_INTENT_VERSION, Constants.INTENT_VERSION);
intent.setType("text/plain");
if (data == null) {
return false;
}
try {
intent.putExtra(Constants.EXTRA_TEXT, data);
activity.startActivityForResult(intent, Constants.DECRYPT_MESSAGE);
return true;
} catch (ActivityNotFoundException e) {
activityNotFound();
return false;
}
}
/**
* Handle the activity results that concern us.
*
* @param requestCode
* @param resultCode
* @param data
* @return handled or not
*/
public boolean onActivityResult(int requestCode, int resultCode, Intent data, ApgData apgData) {
switch (requestCode) {
case Constants.SELECT_SECRET_KEY:
if (resultCode != Activity.RESULT_OK || data == null) {
break;
}
apgData.setSignatureKeyId(data.getLongExtra(Constants.EXTRA_KEY_ID, 0));
apgData.setSignatureUserId(data.getStringExtra(Constants.EXTRA_USER_ID));
break;
case Constants.SELECT_PUBLIC_KEYS:
if (resultCode != Activity.RESULT_OK || data == null) {
apgData.setEncryptionKeys(null);
break;
}
apgData.setEncryptionKeys(data.getLongArrayExtra(Constants.EXTRA_SELECTION));
break;
case Constants.ENCRYPT_MESSAGE:
if (resultCode != Activity.RESULT_OK || data == null) {
apgData.setEncryptionKeys(null);
break;
}
apgData.setEncryptedData(data.getStringExtra(Constants.EXTRA_ENCRYPTED_MESSAGE));
// this was a stupid bug in an earlier version, just gonna leave this in for an APG
// version or two
if (apgData.getEncryptedData() == null) {
apgData.setEncryptedData(data.getStringExtra(Constants.EXTRA_DECRYPTED_MESSAGE));
}
if (apgData.getEncryptedData() != null) {
}
break;
case Constants.DECRYPT_MESSAGE:
if (resultCode != Activity.RESULT_OK || data == null) {
break;
}
apgData.setSignatureUserId(data.getStringExtra(Constants.EXTRA_SIGNATURE_USER_ID));
apgData.setSignatureKeyId(data.getLongExtra(Constants.EXTRA_SIGNATURE_KEY_ID, 0));
apgData.setSignatureSuccess(data.getBooleanExtra(Constants.EXTRA_SIGNATURE_SUCCESS,
false));
apgData.setSignatureUnknown(data.getBooleanExtra(Constants.EXTRA_SIGNATURE_UNKNOWN,
false));
apgData.setDecryptedData(data.getStringExtra(Constants.EXTRA_DECRYPTED_MESSAGE));
break;
default:
return false;
}
return true;
}
/**
* Select encryption keys.
*
* @param emails
* The emails that should be used for preselection.
* @return false when activity could not be found!
*/
public boolean selectEncryptionKeys(String emails) {
return selectEncryptionKeys(emails, null);
}
/**
* Select encryption keys.
*
* @param emails
* The emails that should be used for preselection.
* @param apgData
* ApgData with encryption keys and signature keys preselected
* @return false when activity could not be found!
*/
public boolean selectEncryptionKeys(String emails, ApgData apgData) {
Intent intent = new Intent(Constants.Intent.SELECT_PUBLIC_KEYS);
intent.putExtra(Constants.EXTRA_INTENT_VERSION, Constants.INTENT_VERSION);
long[] initialKeyIds = null;
if (apgData == null || !apgData.hasEncryptionKeys()) {
List<Long> keyIds = new ArrayList<Long>();
if (apgData != null && apgData.hasSignatureKey()) {
keyIds.add(apgData.getSignatureKeyId());
}
try {
Uri contentUri = Uri.withAppendedPath(
Constants.CONTENT_URI_PUBLIC_KEY_RING_BY_EMAILS, emails);
Cursor c = activity.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null) {
while (c.moveToNext()) {
keyIds.add(c.getLong(0));
}
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissions();
}
if (!keyIds.isEmpty()) {
initialKeyIds = new long[keyIds.size()];
for (int i = 0, size = keyIds.size(); i < size; ++i) {
initialKeyIds[i] = keyIds.get(i);
}
}
} else {
initialKeyIds = apgData.getEncryptionKeys();
}
intent.putExtra(Constants.EXTRA_SELECTION, initialKeyIds);
try {
activity.startActivityForResult(intent, Constants.SELECT_PUBLIC_KEYS);
return true;
} catch (ActivityNotFoundException e) {
activityNotFound();
return false;
}
}
/**
* Get secret key ids based on a given email.
*
* @param context
* @param email
* The email in question.
* @return key ids
*/
public long[] getSecretKeyIdsFromEmail(String email) {
long ids[] = null;
try {
Uri contentUri = Uri.withAppendedPath(Constants.CONTENT_URI_SECRET_KEY_RING_BY_EMAILS,
email);
Cursor c = activity.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null && c.getCount() > 0) {
ids = new long[c.getCount()];
while (c.moveToNext()) {
ids[c.getPosition()] = c.getLong(0);
}
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissions();
}
return ids;
}
/**
* Get public key ids based on a given email.
*
* @param context
* @param email
* The email in question.
* @return key ids
*/
public long[] getPublicKeyIdsFromEmail(String email) {
long ids[] = null;
try {
Uri contentUri = Uri.withAppendedPath(Constants.CONTENT_URI_PUBLIC_KEY_RING_BY_EMAILS,
email);
Cursor c = activity.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null && c.getCount() > 0) {
ids = new long[c.getCount()];
while (c.moveToNext()) {
ids[c.getPosition()] = c.getLong(0);
}
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissions();
}
return ids;
}
/**
* Find out if a given email has a secret key.
*
* @param context
* @param email
* The email in question.
* @return true if there is a secret key for this email.
*/
public boolean hasSecretKeyForEmail(String email) {
try {
Uri contentUri = Uri.withAppendedPath(Constants.CONTENT_URI_SECRET_KEY_RING_BY_EMAILS,
email);
Cursor c = activity.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null && c.getCount() > 0) {
c.close();
return true;
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissions();
}
return false;
}
/**
* Find out if a given email has a public key.
*
* @param context
* @param email
* The email in question.
* @return true if there is a public key for this email.
*/
public boolean hasPublicKeyForEmail(String email) {
try {
Uri contentUri = Uri.withAppendedPath(Constants.CONTENT_URI_PUBLIC_KEY_RING_BY_EMAILS,
email);
Cursor c = activity.getContentResolver().query(contentUri,
new String[] { "master_key_id" }, null, null, null);
if (c != null && c.getCount() > 0) {
c.close();
return true;
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissions();
}
return false;
}
/**
* Get the user id based on the key id.
*
* @param context
* @param keyId
* @return user id
*/
public String getUserId(long keyId) {
String userId = null;
try {
Uri contentUri = ContentUris.withAppendedId(
Constants.CONTENT_URI_SECRET_KEY_RING_BY_KEY_ID, keyId);
Cursor c = activity.getContentResolver().query(contentUri, new String[] { "user_id" },
null, null, null);
if (c != null && c.moveToFirst()) {
userId = c.getString(0);
}
if (c != null) {
c.close();
}
} catch (SecurityException e) {
insufficientPermissions();
}
if (userId == null) {
userId = "unknown";
}
return userId;
}
private void activityNotFound() {
Toast.makeText(activity, "APG Activity not found! Is APG installed correctly?",
Toast.LENGTH_LONG).show();
}
private void insufficientPermissions() {
Toast.makeText(activity, "Permission to access APG Provider is missing!", Toast.LENGTH_LONG)
.show();
}
}

View file

@ -15,9 +15,7 @@
* limitations under the License.
*/
package org.apg.api_demo.util;
import java.util.regex.Pattern;
package org.apg.integration;
import android.net.Uri;

View file

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apg.api_demo.util;
package org.apg.integration;
import android.content.Context;
import android.content.pm.PackageInfo;