aboutsummaryrefslogtreecommitdiffstats
path: root/org_apg/src/org/thialfihar/android/apg/provider
diff options
context:
space:
mode:
authorDominik <dominik@dominikschuermann.de>2012-10-30 00:15:08 +0100
committerDominik <dominik@dominikschuermann.de>2012-10-30 00:15:08 +0100
commit691d888e924f63b4d306c48ad5eacd1befd03af5 (patch)
tree02985a1441bdbe1e8d68f1c42e50252838d6c2f5 /org_apg/src/org/thialfihar/android/apg/provider
parent4704e2faf4b5c342a811a91d780c077b05c6237b (diff)
downloadopen-keychain-691d888e924f63b4d306c48ad5eacd1befd03af5.tar.gz
open-keychain-691d888e924f63b4d306c48ad5eacd1befd03af5.tar.bz2
open-keychain-691d888e924f63b4d306c48ad5eacd1befd03af5.zip
KeyListAdapter is working basically, Provider refactored
Diffstat (limited to 'org_apg/src/org/thialfihar/android/apg/provider')
-rw-r--r--org_apg/src/org/thialfihar/android/apg/provider/ApgContract.java111
-rw-r--r--org_apg/src/org/thialfihar/android/apg/provider/ApgProvider.java99
-rw-r--r--org_apg/src/org/thialfihar/android/apg/provider/ProviderHelper.java284
3 files changed, 174 insertions, 320 deletions
diff --git a/org_apg/src/org/thialfihar/android/apg/provider/ApgContract.java b/org_apg/src/org/thialfihar/android/apg/provider/ApgContract.java
index 05a26157a..235d2d8ca 100644
--- a/org_apg/src/org/thialfihar/android/apg/provider/ApgContract.java
+++ b/org_apg/src/org/thialfihar/android/apg/provider/ApgContract.java
@@ -74,113 +74,97 @@ public class ApgContract {
public static final String PATH_USER_IDS = "user_ids";
public static final String PATH_KEYS = "keys";
- public static class PublicKeyRings implements KeyRingsColumns, BaseColumns {
+ public static class KeyRings implements KeyRingsColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
- .appendPath(BASE_KEY_RINGS).appendPath(PATH_PUBLIC).build();
+ .appendPath(BASE_KEY_RINGS).build();
/** Use if multiple items get returned */
- public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.thialfihar.apg.public.key_ring";
+ public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.thialfihar.apg.key_ring";
/** Use if a single item is returned */
- public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.public.key_ring";
+ public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.key_ring";
public static Uri buildPublicKeyRingsUri() {
- return CONTENT_URI.buildUpon().build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC).build();
}
public static Uri buildPublicKeyRingsUri(String keyRingRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC).appendPath(keyRingRowId).build();
}
public static Uri buildPublicKeyRingsByMasterKeyIdUri(String masterKeyId) {
- return CONTENT_URI.buildUpon().appendPath(PATH_BY_MASTER_KEY_ID)
- .appendPath(masterKeyId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC)
+ .appendPath(PATH_BY_MASTER_KEY_ID).appendPath(masterKeyId).build();
}
public static Uri buildPublicKeyRingsByKeyIdUri(String keyId) {
- return CONTENT_URI.buildUpon().appendPath(PATH_BY_KEY_ID).appendPath(keyId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC).appendPath(PATH_BY_KEY_ID)
+ .appendPath(keyId).build();
}
public static Uri buildPublicKeyRingsByEmailsUri(String emails) {
- return CONTENT_URI.buildUpon().appendPath(PATH_BY_EMAILS).appendPath(emails).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC).appendPath(PATH_BY_EMAILS)
+ .appendPath(emails).build();
}
- }
-
- public static class SecretKeyRings implements KeyRingsColumns, BaseColumns {
- public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
- .appendPath(BASE_KEY_RINGS).appendPath(PATH_SECRET).build();
-
- /** Use if multiple items get returned */
- public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.thialfihar.apg.secret.key_ring";
-
- /** Use if a single item is returned */
- public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.secret.key_ring";
public static Uri buildSecretKeyRingsUri() {
- return CONTENT_URI.buildUpon().build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET).build();
}
public static Uri buildSecretKeyRingsUri(String keyRingRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET).appendPath(keyRingRowId).build();
}
public static Uri buildSecretKeyRingsByMasterKeyIdUri(String masterKeyId) {
- return CONTENT_URI.buildUpon().appendPath(PATH_BY_MASTER_KEY_ID)
- .appendPath(masterKeyId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET)
+ .appendPath(PATH_BY_MASTER_KEY_ID).appendPath(masterKeyId).build();
}
public static Uri buildSecretKeyRingsByKeyIdUri(String keyId) {
- return CONTENT_URI.buildUpon().appendPath(PATH_BY_KEY_ID).appendPath(keyId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET).appendPath(PATH_BY_KEY_ID)
+ .appendPath(keyId).build();
}
public static Uri buildSecretKeyRingsByEmailsUri(String emails) {
- return CONTENT_URI.buildUpon().appendPath(PATH_BY_EMAILS).appendPath(emails).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET).appendPath(PATH_BY_EMAILS)
+ .appendPath(emails).build();
}
}
- public static class PublicKeys implements KeysColumns, BaseColumns {
+ public static class Keys implements KeysColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
- .appendPath(BASE_KEY_RINGS).appendPath(PATH_PUBLIC).build();
+ .appendPath(BASE_KEY_RINGS).build();
/** Use if multiple items get returned */
- public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.thialfihar.apg.public.key";
+ public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.thialfihar.apg.key";
/** Use if a single item is returned */
- public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.public.key";
+ public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.key";
public static Uri buildPublicKeysUri(String keyRingRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).appendPath(PATH_KEYS).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC).appendPath(keyRingRowId)
+ .appendPath(PATH_KEYS).build();
}
public static Uri buildPublicKeysUri(String keyRingRowId, String keyRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).appendPath(PATH_KEYS)
- .appendPath(keyRowId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC).appendPath(keyRingRowId)
+ .appendPath(PATH_KEYS).appendPath(keyRowId).build();
}
- }
-
- public static class SecretKeys implements KeysColumns, BaseColumns {
- public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
- .appendPath(BASE_KEY_RINGS).appendPath(PATH_SECRET).build();
-
- /** Use if multiple items get returned */
- public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.thialfihar.apg.secret.key";
-
- /** Use if a single item is returned */
- public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.secret.key";
public static Uri buildSecretKeysUri(String keyRingRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).appendPath(PATH_KEYS).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET).appendPath(keyRingRowId)
+ .appendPath(PATH_KEYS).build();
}
public static Uri buildSecretKeysUri(String keyRingRowId, String keyRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).appendPath(PATH_KEYS)
- .appendPath(keyRowId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET).appendPath(keyRingRowId)
+ .appendPath(PATH_KEYS).appendPath(keyRowId).build();
}
}
- public static class PublicUserIds implements UserIdsColumns, BaseColumns {
+ public static class UserIds implements UserIdsColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
- .appendPath(BASE_KEY_RINGS).appendPath(PATH_PUBLIC).build();
+ .appendPath(BASE_KEY_RINGS).build();
/** Use if multiple items get returned */
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.thialfihar.apg.user_id";
@@ -189,34 +173,23 @@ public class ApgContract {
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.user_id";
public static Uri buildPublicUserIdsUri(String keyRingRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).appendPath(PATH_USER_IDS)
- .build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC).appendPath(keyRingRowId)
+ .appendPath(PATH_USER_IDS).build();
}
public static Uri buildPublicUserIdsUri(String keyRingRowId, String userIdRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).appendPath(PATH_USER_IDS)
- .appendPath(userIdRowId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_PUBLIC).appendPath(keyRingRowId)
+ .appendPath(PATH_USER_IDS).appendPath(userIdRowId).build();
}
- }
-
- public static class SecretUserIds implements UserIdsColumns, BaseColumns {
- public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
- .appendPath(BASE_KEY_RINGS).appendPath(PATH_SECRET).build();
-
- /** Use if multiple items get returned */
- public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.thialfihar.apg.user_id";
-
- /** Use if a single item is returned */
- public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.thialfihar.apg.user_id";
public static Uri buildSecretUserIdsUri(String keyRingRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).appendPath(PATH_USER_IDS)
- .build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET).appendPath(keyRingRowId)
+ .appendPath(PATH_USER_IDS).build();
}
public static Uri buildSecretUserIdsUri(String keyRingRowId, String userIdRowId) {
- return CONTENT_URI.buildUpon().appendPath(keyRingRowId).appendPath(PATH_USER_IDS)
- .appendPath(userIdRowId).build();
+ return CONTENT_URI.buildUpon().appendPath(PATH_SECRET).appendPath(keyRingRowId)
+ .appendPath(PATH_USER_IDS).appendPath(userIdRowId).build();
}
}
diff --git a/org_apg/src/org/thialfihar/android/apg/provider/ApgProvider.java b/org_apg/src/org/thialfihar/android/apg/provider/ApgProvider.java
index bbf5de803..f21cb8877 100644
--- a/org_apg/src/org/thialfihar/android/apg/provider/ApgProvider.java
+++ b/org_apg/src/org/thialfihar/android/apg/provider/ApgProvider.java
@@ -23,15 +23,12 @@ import java.util.Arrays;
import java.util.HashMap;
import org.thialfihar.android.apg.Constants;
+import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
import org.thialfihar.android.apg.provider.ApgContract.KeyRingsColumns;
import org.thialfihar.android.apg.provider.ApgContract.KeyTypes;
import org.thialfihar.android.apg.provider.ApgContract.KeysColumns;
-import org.thialfihar.android.apg.provider.ApgContract.PublicKeyRings;
-import org.thialfihar.android.apg.provider.ApgContract.PublicKeys;
-import org.thialfihar.android.apg.provider.ApgContract.PublicUserIds;
-import org.thialfihar.android.apg.provider.ApgContract.SecretKeyRings;
-import org.thialfihar.android.apg.provider.ApgContract.SecretKeys;
-import org.thialfihar.android.apg.provider.ApgContract.SecretUserIds;
+import org.thialfihar.android.apg.provider.ApgContract.UserIds;
+import org.thialfihar.android.apg.provider.ApgContract.Keys;
import org.thialfihar.android.apg.provider.ApgContract.UserIdsColumns;
import org.thialfihar.android.apg.provider.ApgDatabase.Tables;
import org.thialfihar.android.apg.util.Log;
@@ -189,7 +186,7 @@ public class ApgProvider extends ContentProvider {
* data stream
*
* <pre>
- * data/*
+ * data / _
* </pre>
*/
matcher.addURI(authority, ApgContract.BASE_DATA + "/*", DATA_STREAM);
@@ -214,45 +211,33 @@ public class ApgProvider extends ContentProvider {
switch (match) {
case PUBLIC_KEY_RING:
case PUBLIC_KEY_RING_BY_EMAILS:
- return PublicKeyRings.CONTENT_TYPE;
+ case SECRET_KEY_RING:
+ case SECRET_KEY_RING_BY_EMAILS:
+ return KeyRings.CONTENT_TYPE;
case PUBLIC_KEY_RING_BY_ROW_ID:
case PUBLIC_KEY_RING_BY_MASTER_KEY_ID:
case PUBLIC_KEY_RING_BY_KEY_ID:
- return PublicKeyRings.CONTENT_ITEM_TYPE;
-
- case PUBLIC_KEY_RING_KEY:
- return PublicKeys.CONTENT_TYPE;
-
- case PUBLIC_KEY_RING_KEY_BY_ROW_ID:
- return PublicKeys.CONTENT_ITEM_TYPE;
-
- case PUBLIC_KEY_RING_USER_ID:
- return PublicUserIds.CONTENT_TYPE;
-
- case PUBLIC_KEY_RING_USER_ID_BY_ROW_ID:
- return PublicUserIds.CONTENT_ITEM_TYPE;
-
- case SECRET_KEY_RING:
- case SECRET_KEY_RING_BY_EMAILS:
- return SecretKeyRings.CONTENT_TYPE;
-
case SECRET_KEY_RING_BY_ROW_ID:
case SECRET_KEY_RING_BY_MASTER_KEY_ID:
case SECRET_KEY_RING_BY_KEY_ID:
- return SecretKeyRings.CONTENT_ITEM_TYPE;
+ return KeyRings.CONTENT_ITEM_TYPE;
+ case PUBLIC_KEY_RING_KEY:
case SECRET_KEY_RING_KEY:
- return SecretKeys.CONTENT_TYPE;
+ return Keys.CONTENT_TYPE;
+ case PUBLIC_KEY_RING_KEY_BY_ROW_ID:
case SECRET_KEY_RING_KEY_BY_ROW_ID:
- return SecretKeys.CONTENT_ITEM_TYPE;
+ return Keys.CONTENT_ITEM_TYPE;
+ case PUBLIC_KEY_RING_USER_ID:
case SECRET_KEY_RING_USER_ID:
- return SecretUserIds.CONTENT_TYPE;
+ return UserIds.CONTENT_TYPE;
+ case PUBLIC_KEY_RING_USER_ID_BY_ROW_ID:
case SECRET_KEY_RING_USER_ID_BY_ROW_ID:
- return SecretUserIds.CONTENT_ITEM_TYPE;
+ return UserIds.CONTENT_ITEM_TYPE;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
@@ -314,6 +299,8 @@ public class ApgProvider extends ContentProvider {
projectionMap.put(BaseColumns._ID, Tables.KEY_RINGS + "." + BaseColumns._ID);
projectionMap.put(KeyRingsColumns.MASTER_KEY_ID, Tables.KEY_RINGS + "."
+ KeyRingsColumns.MASTER_KEY_ID);
+ projectionMap.put(KeyRingsColumns.KEY_RING_DATA, Tables.KEY_RINGS + "."
+ + KeyRingsColumns.KEY_RING_DATA);
projectionMap.put(UserIdsColumns.USER_ID, Tables.USER_IDS + "." + UserIdsColumns.USER_ID);
qb.setProjectionMap(projectionMap);
@@ -338,6 +325,7 @@ public class ApgProvider extends ContentProvider {
case PUBLIC_KEY_RING:
case SECRET_KEY_RING:
qb = buildKeyRingQuery(qb, projectionMap, match, sortOrder);
+
if (TextUtils.isEmpty(sortOrder)) {
sortOrder = Tables.USER_IDS + "." + UserIdsColumns.USER_ID + " ASC";
}
@@ -372,30 +360,15 @@ public class ApgProvider extends ContentProvider {
case SECRET_KEY_RING_BY_KEY_ID:
case PUBLIC_KEY_RING_BY_KEY_ID:
- qb.appendWhere(Tables.KEY_RINGS + "." + KeyRingsColumns.TYPE + " = ");
- qb.appendWhereEscapeString(Integer.toString(getKeyType(match)));
-
- qb.setTables(Tables.KEYS + " AS tmp INNER JOIN " + Tables.KEY_RINGS + " ON ("
- + Tables.KEY_RINGS + "." + BaseColumns._ID + " = " + "tmp."
- + KeysColumns.KEY_RING_ROW_ID + ")" + " INNER JOIN " + Tables.KEYS + " ON "
- + "(" + Tables.KEY_RINGS + "." + BaseColumns._ID + " = " + Tables.KEYS + "."
- + KeysColumns.KEY_RING_ROW_ID + " AND " + Tables.KEYS + "."
- + KeysColumns.IS_MASTER_KEY + " = '1'" + ") " + " INNER JOIN "
- + Tables.USER_IDS + " ON " + "(" + Tables.KEY_RINGS + "." + BaseColumns._ID
- + " = " + Tables.USER_IDS + "." + UserIdsColumns.KEY_RING_ROW_ID + " AND "
- + Tables.USER_IDS + "." + UserIdsColumns.RANK + " = '0')");
-
- projectionMap.put(BaseColumns._ID, Tables.KEY_RINGS + "." + BaseColumns._ID);
- projectionMap.put(KeyRingsColumns.MASTER_KEY_ID, Tables.KEY_RINGS + "."
- + KeyRingsColumns.MASTER_KEY_ID);
- projectionMap.put(UserIdsColumns.USER_ID, Tables.USER_IDS + "."
- + UserIdsColumns.USER_ID);
-
- qb.setProjectionMap(projectionMap);
+ qb = buildKeyRingQuery(qb, projectionMap, match, sortOrder);
- qb.appendWhere(" AND tmp." + KeysColumns.KEY_ID + " = ");
+ qb.appendWhere(" AND " + Tables.KEYS + KeysColumns.KEY_ID + " = ");
qb.appendWhereEscapeString(uri.getLastPathSegment());
+ if (TextUtils.isEmpty(sortOrder)) {
+ sortOrder = Tables.USER_IDS + "." + UserIdsColumns.USER_ID + " ASC";
+ }
+
break;
case SECRET_KEY_RING_BY_EMAILS:
@@ -414,6 +387,8 @@ public class ApgProvider extends ContentProvider {
projectionMap.put(BaseColumns._ID, Tables.KEY_RINGS + "." + BaseColumns._ID);
projectionMap.put(KeyRingsColumns.MASTER_KEY_ID, Tables.KEY_RINGS + "."
+ KeyRingsColumns.MASTER_KEY_ID);
+ projectionMap.put(KeyRingsColumns.KEY_RING_DATA, Tables.KEY_RINGS + "."
+ + KeyRingsColumns.KEY_RING_DATA);
projectionMap.put(UserIdsColumns.USER_ID, Tables.USER_IDS + "."
+ UserIdsColumns.USER_ID);
@@ -532,41 +507,41 @@ public class ApgProvider extends ContentProvider {
switch (match) {
case PUBLIC_KEY_RING:
- values.put(PublicKeyRings.TYPE, KeyTypes.PUBLIC);
+ values.put(KeyRings.TYPE, KeyTypes.PUBLIC);
rowId = db.insertOrThrow(Tables.KEY_RINGS, null, values);
- rowUri = PublicKeyRings.buildPublicKeyRingsUri(Long.toString(rowId));
+ rowUri = KeyRings.buildPublicKeyRingsUri(Long.toString(rowId));
break;
case PUBLIC_KEY_RING_KEY:
- values.put(PublicKeys.TYPE, KeyTypes.PUBLIC);
+ values.put(Keys.TYPE, KeyTypes.PUBLIC);
rowId = db.insertOrThrow(Tables.KEYS, null, values);
- rowUri = PublicKeys.buildPublicKeysUri(Long.toString(rowId));
+ rowUri = Keys.buildPublicKeysUri(Long.toString(rowId));
break;
case PUBLIC_KEY_RING_USER_ID:
rowId = db.insertOrThrow(Tables.USER_IDS, null, values);
- rowUri = PublicUserIds.buildPublicUserIdsUri(Long.toString(rowId));
+ rowUri = UserIds.buildPublicUserIdsUri(Long.toString(rowId));
break;
case SECRET_KEY_RING:
- values.put(SecretKeyRings.TYPE, KeyTypes.SECRET);
+ values.put(KeyRings.TYPE, KeyTypes.SECRET);
rowId = db.insertOrThrow(Tables.KEY_RINGS, null, values);
- rowUri = SecretKeyRings.buildSecretKeyRingsUri(Long.toString(rowId));
+ rowUri = KeyRings.buildSecretKeyRingsUri(Long.toString(rowId));
break;
case SECRET_KEY_RING_KEY:
- values.put(SecretKeys.TYPE, KeyTypes.SECRET);
+ values.put(Keys.TYPE, KeyTypes.SECRET);
rowId = db.insertOrThrow(Tables.KEYS, null, values);
- rowUri = SecretKeys.buildSecretKeysUri(Long.toString(rowId));
+ rowUri = Keys.buildSecretKeysUri(Long.toString(rowId));
break;
case SECRET_KEY_RING_USER_ID:
rowId = db.insertOrThrow(Tables.USER_IDS, null, values);
- rowUri = SecretUserIds.buildSecretUserIdsUri(Long.toString(rowId));
+ rowUri = UserIds.buildSecretUserIdsUri(Long.toString(rowId));
break;
default:
diff --git a/org_apg/src/org/thialfihar/android/apg/provider/ProviderHelper.java b/org_apg/src/org/thialfihar/android/apg/provider/ProviderHelper.java
index 0bae2b782..287dc5bfc 100644
--- a/org_apg/src/org/thialfihar/android/apg/provider/ProviderHelper.java
+++ b/org_apg/src/org/thialfihar/android/apg/provider/ProviderHelper.java
@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
+import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey;
@@ -12,12 +13,9 @@ import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.helper.PGPConversionHelper;
import org.thialfihar.android.apg.helper.PGPHelper;
-import org.thialfihar.android.apg.provider.ApgContract.PublicKeyRings;
-import org.thialfihar.android.apg.provider.ApgContract.PublicKeys;
-import org.thialfihar.android.apg.provider.ApgContract.PublicUserIds;
-import org.thialfihar.android.apg.provider.ApgContract.SecretKeyRings;
-import org.thialfihar.android.apg.provider.ApgContract.SecretKeys;
-import org.thialfihar.android.apg.provider.ApgContract.SecretUserIds;
+import org.thialfihar.android.apg.provider.ApgContract.KeyRings;
+import org.thialfihar.android.apg.provider.ApgContract.UserIds;
+import org.thialfihar.android.apg.provider.ApgContract.Keys;
import org.thialfihar.android.apg.util.IterableIterator;
import org.thialfihar.android.apg.util.Log;
@@ -32,63 +30,20 @@ import android.os.RemoteException;
public class ProviderHelper {
- //
- // public static HashMap<String, String> sKeyRingsProjection;
- // public static HashMap<String, String> sKeysProjection;
- // public static HashMap<String, String> sUserIdsProjection;
- //
- // private SQLiteDatabase mDb = null;
- // private int mStatus = 0;
- //
- // static {
- // sKeyRingsProjection = new HashMap<String, String>();
- // sKeyRingsProjection.put(KeyRings._ID, KeyRings._ID);
- // sKeyRingsProjection.put(KeyRings.MASTER_KEY_ID, KeyRings.MASTER_KEY_ID);
- // sKeyRingsProjection.put(KeyRings.TYPE, KeyRings.TYPE);
- // sKeyRingsProjection.put(KeyRings.WHO_ID, KeyRings.WHO_ID);
- // sKeyRingsProjection.put(KeyRings.KEY_RING_DATA, KeyRings.KEY_RING_DATA);
- //
- // sKeysProjection = new HashMap<String, String>();
- // sKeysProjection.put(Keys._ID, Keys._ID);
- // sKeysProjection.put(Keys.KEY_ID, Keys.KEY_ID);
- // sKeysProjection.put(Keys.TYPE, Keys.TYPE);
- // sKeysProjection.put(Keys.IS_MASTER_KEY, Keys.IS_MASTER_KEY);
- // sKeysProjection.put(Keys.ALGORITHM, Keys.ALGORITHM);
- // sKeysProjection.put(Keys.KEY_SIZE, Keys.KEY_SIZE);
- // sKeysProjection.put(Keys.CAN_SIGN, Keys.CAN_SIGN);
- // sKeysProjection.put(Keys.CAN_ENCRYPT, Keys.CAN_ENCRYPT);
- // sKeysProjection.put(Keys.IS_REVOKED, Keys.IS_REVOKED);
- // sKeysProjection.put(Keys.CREATION, Keys.CREATION);
- // sKeysProjection.put(Keys.EXPIRY, Keys.EXPIRY);
- // sKeysProjection.put(Keys.KEY_DATA, Keys.KEY_DATA);
- // sKeysProjection.put(Keys.RANK, Keys.RANK);
- //
- // sUserIdsProjection = new HashMap<String, String>();
- // sUserIdsProjection.put(UserIds._ID, UserIds._ID);
- // sUserIdsProjection.put(UserIds.KEY_ID, UserIds.KEY_ID);
- // sUserIdsProjection.put(UserIds.USER_ID, UserIds.USER_ID);
- // sUserIdsProjection.put(UserIds.RANK, UserIds.RANK);
- // }
-
/**
- * Retrieves the actual PGPPublicKeyRing object from the database blob associated with the
- * maserKeyId
+ * Private helper method to get PGPKeyRing from database
*
* @param context
- * @param masterKeyId
+ * @param queryUri
* @return
*/
- public static PGPPublicKeyRing getPGPPublicKeyRingByMasterKeyId(Context context,
- long masterKeyId) {
- Uri queryUri = PublicKeyRings.buildPublicKeyRingsByMasterKeyIdUri(Long
- .toString(masterKeyId));
- Cursor cursor = context.getContentResolver()
- .query(queryUri, new String[] { PublicKeyRings._ID, PublicKeyRings.KEY_RING_DATA },
- null, null, null);
+ private static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) {
+ Cursor cursor = context.getContentResolver().query(queryUri,
+ new String[] { KeyRings._ID, KeyRings.KEY_RING_DATA }, null, null, null);
- PGPPublicKeyRing keyRing = null;
+ PGPKeyRing keyRing = null;
if (cursor != null && cursor.moveToFirst()) {
- int keyRingDataCol = cursor.getColumnIndex(PublicKeyRings.KEY_RING_DATA);
+ int keyRingDataCol = cursor.getColumnIndex(KeyRings.KEY_RING_DATA);
byte[] data = cursor.getBlob(keyRingDataCol);
if (data != null) {
@@ -103,27 +58,23 @@ public class ProviderHelper {
return keyRing;
}
- public static PGPPublicKeyRing getPGPPublicKeyRing(Context context, long rowId) {
- Uri queryUri = PublicKeyRings.buildPublicKeyRingsUri(Long.toString(rowId));
- Cursor cursor = context.getContentResolver()
- .query(queryUri, new String[] { PublicKeyRings._ID, PublicKeyRings.KEY_RING_DATA },
- null, null, null);
-
- PGPPublicKeyRing keyRing = null;
- if (cursor != null && cursor.moveToFirst()) {
- int keyRingDataCol = cursor.getColumnIndex(PublicKeyRings.KEY_RING_DATA);
-
- byte[] data = cursor.getBlob(keyRingDataCol);
- if (data != null) {
- keyRing = PGPConversionHelper.BytesToPGPPublicKeyRing(data);
- }
- }
-
- if (cursor != null) {
- cursor.close();
- }
+ /**
+ * Retrieves the actual PGPPublicKeyRing object from the database blob associated with the
+ * maserKeyId
+ *
+ * @param context
+ * @param masterKeyId
+ * @return
+ */
+ public static PGPPublicKeyRing getPGPPublicKeyRingByMasterKeyId(Context context,
+ long masterKeyId) {
+ Uri queryUri = KeyRings.buildPublicKeyRingsByMasterKeyIdUri(Long.toString(masterKeyId));
+ return (PGPPublicKeyRing) getPGPKeyRing(context, queryUri);
+ }
- return keyRing;
+ public static PGPPublicKeyRing getPGPPublicKeyRing(Context context, long rowId) {
+ Uri queryUri = KeyRings.buildPublicKeyRingsUri(Long.toString(rowId));
+ return (PGPPublicKeyRing) getPGPKeyRing(context, queryUri);
}
/**
@@ -136,50 +87,13 @@ public class ProviderHelper {
*/
public static PGPSecretKeyRing getPGPSecretKeyRingByMasterKeyId(Context context,
long masterKeyId) {
- Uri queryUri = SecretKeyRings.buildSecretKeyRingsByMasterKeyIdUri(Long
- .toString(masterKeyId));
- Cursor cursor = context.getContentResolver()
- .query(queryUri, new String[] { SecretKeyRings._ID, SecretKeyRings.KEY_RING_DATA },
- null, null, null);
-
- PGPSecretKeyRing keyRing = null;
- if (cursor != null && cursor.moveToFirst()) {
- int keyRingDataCol = cursor.getColumnIndex(SecretKeyRings.KEY_RING_DATA);
-
- byte[] data = cursor.getBlob(keyRingDataCol);
- if (data != null) {
- keyRing = PGPConversionHelper.BytesToPGPSecretKeyRing(data);
- }
- }
-
- if (cursor != null) {
- cursor.close();
- }
-
- return keyRing;
+ Uri queryUri = KeyRings.buildSecretKeyRingsByMasterKeyIdUri(Long.toString(masterKeyId));
+ return (PGPSecretKeyRing) getPGPKeyRing(context, queryUri);
}
public static PGPSecretKeyRing getPGPSecretKeyRing(Context context, long rowId) {
- Uri queryUri = SecretKeyRings.buildSecretKeyRingsUri(Long.toString(rowId));
- Cursor cursor = context.getContentResolver()
- .query(queryUri, new String[] { SecretKeyRings._ID, SecretKeyRings.KEY_RING_DATA },
- null, null, null);
-
- PGPSecretKeyRing keyRing = null;
- if (cursor != null && cursor.moveToFirst()) {
- int keyRingDataCol = cursor.getColumnIndex(SecretKeyRings.KEY_RING_DATA);
-
- byte[] data = cursor.getBlob(keyRingDataCol);
- if (data != null) {
- keyRing = PGPConversionHelper.BytesToPGPSecretKeyRing(data);
- }
- }
-
- if (cursor != null) {
- cursor.close();
- }
-
- return keyRing;
+ Uri queryUri = KeyRings.buildSecretKeyRingsUri(Long.toString(rowId));
+ return (PGPSecretKeyRing) getPGPKeyRing(context, queryUri);
}
public static PGPSecretKey getPGPSecretKey(Context context, long keyId) {
@@ -213,8 +127,7 @@ public class ProviderHelper {
long masterKeyId = masterKey.getKeyID();
// delete old version of this keyRing, which also deletes all keys and userIds on cascade
- Uri deleteUri = PublicKeyRings.buildPublicKeyRingsByMasterKeyIdUri(Long
- .toString(masterKeyId));
+ Uri deleteUri = KeyRings.buildPublicKeyRingsByMasterKeyIdUri(Long.toString(masterKeyId));
try {
context.getContentResolver().delete(deleteUri, null, null);
@@ -223,11 +136,11 @@ public class ProviderHelper {
}
ContentValues values = new ContentValues();
- values.put(PublicKeyRings.MASTER_KEY_ID, masterKeyId);
- values.put(PublicKeyRings.KEY_RING_DATA, keyRing.getEncoded());
+ values.put(KeyRings.MASTER_KEY_ID, masterKeyId);
+ values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded());
// insert new version of this keyRing
- Uri uri = PublicKeyRings.buildPublicKeyRingsUri();
+ Uri uri = KeyRings.buildPublicKeyRingsUri();
Uri insertedUri = context.getContentResolver().insert(uri, values);
long keyRingRowId = Long.valueOf(insertedUri.getLastPathSegment());
@@ -269,8 +182,7 @@ public class ProviderHelper {
long masterKeyId = masterKey.getKeyID();
// delete old version of this keyRing, which also deletes all keys and userIds on cascade
- Uri deleteUri = SecretKeyRings.buildSecretKeyRingsByMasterKeyIdUri(Long
- .toString(masterKeyId));
+ Uri deleteUri = KeyRings.buildSecretKeyRingsByMasterKeyIdUri(Long.toString(masterKeyId));
try {
context.getContentResolver().delete(deleteUri, null, null);
@@ -279,12 +191,12 @@ public class ProviderHelper {
}
ContentValues values = new ContentValues();
- values.put(SecretKeyRings.MASTER_KEY_ID, masterKeyId);
- values.put(SecretKeyRings.KEY_RING_DATA, keyRing.getEncoded());
+ values.put(KeyRings.MASTER_KEY_ID, masterKeyId);
+ values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded());
// insert new version of this keyRing
- Uri uri = SecretKeyRings.buildSecretKeyRingsUri();
- Uri insertedUri = context.getContentResolver().insert(uri, values);
+ Uri uri = KeyRings.buildSecretKeyRingsUri();
+ Uri insertedUri = context.getContentResolver().insert(uri, values);
long keyRingRowId = Long.valueOf(insertedUri.getLastPathSegment());
// save all keys and userIds included in keyRing object in database
@@ -324,23 +236,23 @@ public class ProviderHelper {
private static ContentProviderOperation buildPublicKeyOperations(Context context,
long keyRingRowId, PGPPublicKey key, int rank) throws IOException {
ContentValues values = new ContentValues();
- values.put(PublicKeys.KEY_ID, key.getKeyID());
- values.put(PublicKeys.IS_MASTER_KEY, key.isMasterKey());
- values.put(PublicKeys.ALGORITHM, key.getAlgorithm());
- values.put(PublicKeys.KEY_SIZE, key.getBitStrength());
- values.put(PublicKeys.CAN_SIGN, PGPHelper.isSigningKey(key));
- values.put(PublicKeys.CAN_ENCRYPT, PGPHelper.isEncryptionKey(key));
- values.put(PublicKeys.IS_REVOKED, key.isRevoked());
- values.put(PublicKeys.CREATION, PGPHelper.getCreationDate(key).getTime() / 1000);
+ values.put(Keys.KEY_ID, key.getKeyID());
+ values.put(Keys.IS_MASTER_KEY, key.isMasterKey());
+ values.put(Keys.ALGORITHM, key.getAlgorithm());
+ values.put(Keys.KEY_SIZE, key.getBitStrength());
+ values.put(Keys.CAN_SIGN, PGPHelper.isSigningKey(key));
+ values.put(Keys.CAN_ENCRYPT, PGPHelper.isEncryptionKey(key));
+ values.put(Keys.IS_REVOKED, key.isRevoked());
+ values.put(Keys.CREATION, PGPHelper.getCreationDate(key).getTime() / 1000);
Date expiryDate = PGPHelper.getExpiryDate(key);
if (expiryDate != null) {
- values.put(PublicKeys.EXPIRY, expiryDate.getTime() / 1000);
+ values.put(Keys.EXPIRY, expiryDate.getTime() / 1000);
}
- values.put(PublicKeys.KEY_RING_ROW_ID, keyRingRowId);
- values.put(PublicKeys.KEY_DATA, key.getEncoded());
- values.put(PublicKeys.RANK, rank);
+ values.put(Keys.KEY_RING_ROW_ID, keyRingRowId);
+ values.put(Keys.KEY_DATA, key.getEncoded());
+ values.put(Keys.RANK, rank);
- Uri uri = PublicKeys.buildPublicKeysUri(Long.toString(keyRingRowId));
+ Uri uri = Keys.buildPublicKeysUri(Long.toString(keyRingRowId));
return ContentProviderOperation.newInsert(uri).withValues(values).build();
}
@@ -358,11 +270,11 @@ public class ProviderHelper {
private static ContentProviderOperation buildPublicUserIdOperations(Context context,
long keyRingRowId, String userId, int rank) {
ContentValues values = new ContentValues();
- values.put(PublicUserIds.KEY_RING_ROW_ID, keyRingRowId);
- values.put(PublicUserIds.USER_ID, userId);
- values.put(PublicUserIds.RANK, rank);
+ values.put(UserIds.KEY_RING_ROW_ID, keyRingRowId);
+ values.put(UserIds.USER_ID, userId);
+ values.put(UserIds.RANK, rank);
- Uri uri = PublicUserIds.buildPublicUserIdsUri(Long.toString(keyRingRowId));
+ Uri uri = UserIds.buildPublicUserIdsUri(Long.toString(keyRingRowId));
return ContentProviderOperation.newInsert(uri).withValues(values).build();
}
@@ -380,23 +292,23 @@ public class ProviderHelper {
private static ContentProviderOperation buildSecretKeyOperations(Context context,
long keyRingRowId, PGPSecretKey key, int rank) throws IOException {
ContentValues values = new ContentValues();
- values.put(SecretKeys.KEY_ID, key.getKeyID());
- values.put(SecretKeys.IS_MASTER_KEY, key.isMasterKey());
- values.put(SecretKeys.ALGORITHM, key.getPublicKey().getAlgorithm());
- values.put(SecretKeys.KEY_SIZE, key.getPublicKey().getBitStrength());
- values.put(SecretKeys.CAN_SIGN, PGPHelper.isSigningKey(key));
- values.put(SecretKeys.CAN_ENCRYPT, PGPHelper.isEncryptionKey(key));
- values.put(SecretKeys.IS_REVOKED, key.getPublicKey().isRevoked());
- values.put(SecretKeys.CREATION, PGPHelper.getCreationDate(key).getTime() / 1000);
+ values.put(Keys.KEY_ID, key.getKeyID());
+ values.put(Keys.IS_MASTER_KEY, key.isMasterKey());
+ values.put(Keys.ALGORITHM, key.getPublicKey().getAlgorithm());
+ values.put(Keys.KEY_SIZE, key.getPublicKey().getBitStrength());
+ values.put(Keys.CAN_SIGN, PGPHelper.isSigningKey(key));
+ values.put(Keys.CAN_ENCRYPT, PGPHelper.isEncryptionKey(key));
+ values.put(Keys.IS_REVOKED, key.getPublicKey().isRevoked());
+ values.put(Keys.CREATION, PGPHelper.getCreationDate(key).getTime() / 1000);
Date expiryDate = PGPHelper.getExpiryDate(key);
if (expiryDate != null) {
- values.put(SecretKeys.EXPIRY, expiryDate.getTime() / 1000);
+ values.put(Keys.EXPIRY, expiryDate.getTime() / 1000);
}
- values.put(SecretKeys.KEY_RING_ROW_ID, keyRingRowId);
- values.put(SecretKeys.KEY_DATA, key.getEncoded());
- values.put(SecretKeys.RANK, rank);
+ values.put(Keys.KEY_RING_ROW_ID, keyRingRowId);
+ values.put(Keys.KEY_DATA, key.getEncoded());
+ values.put(Keys.RANK, rank);
- Uri uri = SecretKeys.buildSecretKeysUri(Long.toString(keyRingRowId));
+ Uri uri = Keys.buildSecretKeysUri(Long.toString(keyRingRowId));
return ContentProviderOperation.newInsert(uri).withValues(values).build();
}
@@ -414,29 +326,29 @@ public class ProviderHelper {
private static ContentProviderOperation buildSecretUserIdOperations(Context context,
long keyRingRowId, String userId, int rank) {
ContentValues values = new ContentValues();
- values.put(SecretUserIds.KEY_RING_ROW_ID, keyRingRowId);
- values.put(SecretUserIds.USER_ID, userId);
- values.put(SecretUserIds.RANK, rank);
+ values.put(UserIds.KEY_RING_ROW_ID, keyRingRowId);
+ values.put(UserIds.USER_ID, userId);
+ values.put(UserIds.RANK, rank);
- Uri uri = SecretUserIds.buildSecretUserIdsUri(Long.toString(keyRingRowId));
+ Uri uri = UserIds.buildSecretUserIdsUri(Long.toString(keyRingRowId));
return ContentProviderOperation.newInsert(uri).withValues(values).build();
}
/**
- * Retrieves ids of all SecretKeyRings
+ * Private helper method
*
* @param context
+ * @param queryUri
* @return
*/
- public static Vector<Integer> getSecretKeyRingsRowIds(Context context) {
- Uri queryUri = SecretKeyRings.buildSecretKeyRingsUri();
- Cursor cursor = context.getContentResolver().query(queryUri,
- new String[] { SecretKeyRings._ID }, null, null, null);
+ private static Vector<Integer> getKeyRingsRowIds(Context context, Uri queryUri) {
+ Cursor cursor = context.getContentResolver().query(queryUri, new String[] { KeyRings._ID },
+ null, null, null);
Vector<Integer> keyIds = new Vector<Integer>();
if (cursor != null) {
- int idCol = cursor.getColumnIndex(SecretKeyRings._ID);
+ int idCol = cursor.getColumnIndex(KeyRings._ID);
if (cursor.moveToFirst()) {
do {
keyIds.add(cursor.getInt(idCol));
@@ -452,41 +364,35 @@ public class ProviderHelper {
}
/**
+ * Retrieves ids of all SecretKeyRings
+ *
+ * @param context
+ * @return
+ */
+ public static Vector<Integer> getSecretKeyRingsRowIds(Context context) {
+ Uri queryUri = KeyRings.buildSecretKeyRingsUri();
+ return getKeyRingsRowIds(context, queryUri);
+ }
+
+ /**
* Retrieves ids of all PublicKeyRings
*
* @param context
* @return
*/
public static Vector<Integer> getPublicKeyRingsRowIds(Context context) {
- Uri queryUri = PublicKeyRings.buildPublicKeyRingsUri();
- Cursor cursor = context.getContentResolver().query(queryUri,
- new String[] { PublicKeyRings._ID }, null, null, null);
-
- Vector<Integer> keyIds = new Vector<Integer>();
- if (cursor != null) {
- int idCol = cursor.getColumnIndex(PublicKeyRings._ID);
- if (cursor.moveToFirst()) {
- do {
- keyIds.add(cursor.getInt(idCol));
- } while (cursor.moveToNext());
- }
- }
-
- if (cursor != null) {
- cursor.close();
- }
-
- return keyIds;
+ Uri queryUri = KeyRings.buildPublicKeyRingsUri();
+ return getKeyRingsRowIds(context, queryUri);
}
public static void deletePublicKeyRing(Context context, long rowId) {
ContentResolver cr = context.getContentResolver();
- cr.delete(PublicKeyRings.buildPublicKeyRingsUri(Long.toString(rowId)), null, null);
+ cr.delete(KeyRings.buildPublicKeyRingsUri(Long.toString(rowId)), null, null);
}
public static void deleteSecretKeyRing(Context context, long rowId) {
ContentResolver cr = context.getContentResolver();
- cr.delete(SecretKeyRings.buildSecretKeyRingsUri(Long.toString(rowId)), null, null);
+ cr.delete(KeyRings.buildSecretKeyRingsUri(Long.toString(rowId)), null, null);
}
}