diff options
author | Thialfihar <thialfihar@gmail.com> | 2010-06-05 00:33:33 +0000 |
---|---|---|
committer | Thialfihar <thialfihar@gmail.com> | 2010-06-05 00:33:33 +0000 |
commit | a85ae5e0092057d1007ba62fada17c9f6a085c92 (patch) | |
tree | 0de13d1eb3bf345684ed83d6154a08fbebb2fd95 /src/org/thialfihar/android/apg/provider | |
parent | ed2cb1e525778d62949958bbf108687c571e4ebe (diff) | |
parent | a571ce7c5222d1f2246acdfada7c95e48a170dd4 (diff) | |
download | open-keychain-a85ae5e0092057d1007ba62fada17c9f6a085c92.tar.gz open-keychain-a85ae5e0092057d1007ba62fada17c9f6a085c92.tar.bz2 open-keychain-a85ae5e0092057d1007ba62fada17c9f6a085c92.zip |
branching trunk out of latest 1.0.x to get a clean start for it
Diffstat (limited to 'src/org/thialfihar/android/apg/provider')
-rw-r--r-- | src/org/thialfihar/android/apg/provider/Accounts.java | 13 | ||||
-rw-r--r-- | src/org/thialfihar/android/apg/provider/DataProvider.java | 572 | ||||
-rw-r--r-- | src/org/thialfihar/android/apg/provider/Database.java | 605 | ||||
-rw-r--r-- | src/org/thialfihar/android/apg/provider/KeyRings.java (renamed from src/org/thialfihar/android/apg/provider/Accounts1.java) | 69 | ||||
-rw-r--r-- | src/org/thialfihar/android/apg/provider/Keys.java | 51 | ||||
-rw-r--r-- | src/org/thialfihar/android/apg/provider/PublicKeys1.java | 42 | ||||
-rw-r--r-- | src/org/thialfihar/android/apg/provider/SecretKeys.java | 22 | ||||
-rw-r--r-- | src/org/thialfihar/android/apg/provider/SecretKeys1.java | 42 | ||||
-rw-r--r-- | src/org/thialfihar/android/apg/provider/UserIds.java (renamed from src/org/thialfihar/android/apg/provider/PublicKeys.java) | 17 |
9 files changed, 909 insertions, 524 deletions
diff --git a/src/org/thialfihar/android/apg/provider/Accounts.java b/src/org/thialfihar/android/apg/provider/Accounts.java index 4fce2b607..8162472ec 100644 --- a/src/org/thialfihar/android/apg/provider/Accounts.java +++ b/src/org/thialfihar/android/apg/provider/Accounts.java @@ -16,7 +16,12 @@ package org.thialfihar.android.apg.provider;
-public class Accounts extends Accounts1 {
- private Accounts() {
- }
-}
\ No newline at end of file +import android.provider.BaseColumns;
+
+public class Accounts implements BaseColumns {
+ public static final String TABLE_NAME = "accounts";
+
+ public static final String _ID_type = "INTEGER PRIMARY KEY";
+ public static final String NAME = "c_name";
+ public static final String NAME_type = "TEXT";
+}
diff --git a/src/org/thialfihar/android/apg/provider/DataProvider.java b/src/org/thialfihar/android/apg/provider/DataProvider.java index fbc1be047..8a3fefdff 100644 --- a/src/org/thialfihar/android/apg/provider/DataProvider.java +++ b/src/org/thialfihar/android/apg/provider/DataProvider.java @@ -18,15 +18,12 @@ package org.thialfihar.android.apg.provider; import java.util.HashMap; +import org.thialfihar.android.apg.Id; + import android.content.ContentProvider; -import android.content.ContentUris; import android.content.ContentValues; -import android.content.Context; import android.content.UriMatcher; import android.database.Cursor; -import android.database.SQLException; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.text.TextUtils; @@ -34,153 +31,190 @@ import android.text.TextUtils; public class DataProvider extends ContentProvider { public static final String AUTHORITY = "org.thialfihar.android.apg.provider"; - private static final String DATABASE_NAME = "apg"; - private static final int DATABASE_VERSION = 1; - - private static final int PUBLIC_KEYS = 101; - private static final int PUBLIC_KEY_ID = 102; - private static final int PUBLIC_KEY_BY_KEY_ID = 103; - - private static final int SECRET_KEYS = 201; - private static final int SECRET_KEY_ID = 202; - private static final int SECRET_KEY_BY_KEY_ID = 203; - - private static final int ACCOUNTS = 301; - private static final int ACCOUNT_ID = 302; + private static final int PUBLIC_KEY_RINGS = 101; + private static final int PUBLIC_KEY_RING_ID = 102; + private static final int PUBLIC_KEY_RING_BY_KEY_ID = 103; + private static final int PUBLIC_KEY_RING_KEYS = 111; + private static final int PUBLIC_KEY_RING_KEY_RANK = 112; + private static final int PUBLIC_KEY_RING_USER_IDS = 121; + private static final int PUBLIC_KEY_RING_USER_ID_RANK = 122; + + private static final int SECRET_KEY_RINGS = 201; + private static final int SECRET_KEY_RING_ID = 202; + private static final int SECRET_KEY_RING_BY_KEY_ID = 203; + private static final int SECRET_KEY_RING_KEYS = 211; + private static final int SECRET_KEY_RING_KEY_RANK = 212; + private static final int SECRET_KEY_RING_USER_IDS = 221; + private static final int SECRET_KEY_RING_USER_ID_RANK = 222; + + private static final String PUBLIC_KEY_RING_CONTENT_DIR_TYPE = + "vnd.android.cursor.dir/vnd.thialfihar.apg.public.key_ring"; + private static final String PUBLIC_KEY_RING_CONTENT_ITEM_TYPE = + "vnd.android.cursor.item/vnd.thialfihar.apg.public.key_ring"; + + private static final String PUBLIC_KEY_CONTENT_DIR_TYPE = + "vnd.android.cursor.dir/vnd.thialfihar.apg.public.key"; + private static final String PUBLIC_KEY_CONTENT_ITEM_TYPE = + "vnd.android.cursor.item/vnd.thialfihar.apg.public.key"; + + private static final String SECRET_KEY_RING_CONTENT_DIR_TYPE = + "vnd.android.cursor.dir/vnd.thialfihar.apg.secret.key_ring"; + private static final String SECRET_KEY_RING_CONTENT_ITEM_TYPE = + "vnd.android.cursor.item/vnd.thialfihar.apg.secret.key_ring"; + + private static final String SECRET_KEY_CONTENT_DIR_TYPE = + "vnd.android.cursor.dir/vnd.thialfihar.apg.secret.key"; + private static final String SECRET_KEY_CONTENT_ITEM_TYPE = + "vnd.android.cursor.item/vnd.thialfihar.apg.secret.key"; + + private static final String USER_ID_CONTENT_DIR_TYPE = + "vnd.android.cursor.dir/vnd.thialfihar.apg.user_id"; + private static final String USER_ID_CONTENT_ITEM_TYPE = + "vnd.android.cursor.item/vnd.thialfihar.apg.user_id"; + + public static final String MASTER_KEY_ID = "master_key_id"; + public static final String KEY_ID = "key_id"; + public static final String USER_ID = "user_id"; private static final UriMatcher mUriMatcher; - private static final HashMap<String, String> mPublicKeysProjectionMap; - private static final HashMap<String, String> mSecretKeysProjectionMap; - private static final HashMap<String, String> mAccountsProjectionMap; - private DatabaseHelper mdbHelper; + private Database mDb; static { mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); - mUriMatcher.addURI(DataProvider.AUTHORITY, "public_keys", PUBLIC_KEYS); - mUriMatcher.addURI(DataProvider.AUTHORITY, "public_keys/#", PUBLIC_KEY_ID); - mUriMatcher.addURI(DataProvider.AUTHORITY, "public_keys/key_id/*", PUBLIC_KEY_BY_KEY_ID); - - mUriMatcher.addURI(DataProvider.AUTHORITY, "secret_keys", SECRET_KEYS); - mUriMatcher.addURI(DataProvider.AUTHORITY, "secret_keys/#", SECRET_KEY_ID); - mUriMatcher.addURI(DataProvider.AUTHORITY, "secret_keys/key_id/*", SECRET_KEY_BY_KEY_ID); - - mUriMatcher.addURI(DataProvider.AUTHORITY, "accounts", ACCOUNTS); - mUriMatcher.addURI(DataProvider.AUTHORITY, "accounts/#", ACCOUNT_ID); - - mPublicKeysProjectionMap = new HashMap<String, String>(); - mPublicKeysProjectionMap.put(PublicKeys._ID, PublicKeys._ID); - mPublicKeysProjectionMap.put(PublicKeys.KEY_ID, PublicKeys.KEY_ID); - mPublicKeysProjectionMap.put(PublicKeys.KEY_DATA, PublicKeys.KEY_DATA); - mPublicKeysProjectionMap.put(PublicKeys.WHO_ID, PublicKeys.WHO_ID); - - mSecretKeysProjectionMap = new HashMap<String, String>(); - mSecretKeysProjectionMap.put(PublicKeys._ID, PublicKeys._ID); - mSecretKeysProjectionMap.put(PublicKeys.KEY_ID, PublicKeys.KEY_ID); - mSecretKeysProjectionMap.put(PublicKeys.KEY_DATA, PublicKeys.KEY_DATA); - mSecretKeysProjectionMap.put(PublicKeys.WHO_ID, PublicKeys.WHO_ID); - - mAccountsProjectionMap = new HashMap<String, String>(); - mAccountsProjectionMap.put(Accounts._ID, Accounts._ID); - mAccountsProjectionMap.put(Accounts.NAME, Accounts.NAME); - } + mUriMatcher.addURI(AUTHORITY, "key_rings/public/key_id/*", PUBLIC_KEY_RING_BY_KEY_ID); - /** - * This class helps open, create, and upgrade the database file. - */ - private static class DatabaseHelper extends SQLiteOpenHelper { + mUriMatcher.addURI(AUTHORITY, "key_rings/public/*/keys", PUBLIC_KEY_RING_KEYS); + mUriMatcher.addURI(AUTHORITY, "key_rings/public/*/keys/#", PUBLIC_KEY_RING_KEY_RANK); - DatabaseHelper(Context context) { - super(context, DATABASE_NAME, null, DATABASE_VERSION); - } + mUriMatcher.addURI(AUTHORITY, "key_rings/public/*/user_ids", PUBLIC_KEY_RING_USER_IDS); + mUriMatcher.addURI(AUTHORITY, "key_rings/public/*/user_ids/#", PUBLIC_KEY_RING_USER_ID_RANK); - @Override - public void onCreate(SQLiteDatabase db) { - db.execSQL("CREATE TABLE " + PublicKeys.TABLE_NAME + " (" + - PublicKeys._ID + " " + PublicKeys._ID_type + "," + - PublicKeys.KEY_ID + " " + PublicKeys.KEY_ID_type + ", " + - PublicKeys.KEY_DATA + " " + PublicKeys.KEY_DATA_type + ", " + - PublicKeys.WHO_ID + " " + PublicKeys.WHO_ID_type + ");"); - - db.execSQL("CREATE TABLE " + SecretKeys.TABLE_NAME + " (" + - SecretKeys._ID + " " + SecretKeys._ID_type + "," + - SecretKeys.KEY_ID + " " + SecretKeys.KEY_ID_type + ", " + - SecretKeys.KEY_DATA + " " + SecretKeys.KEY_DATA_type + ", " + - SecretKeys.WHO_ID + " " + SecretKeys.WHO_ID_type + ");"); - - db.execSQL("CREATE TABLE " + Accounts.TABLE_NAME + " (" + - Accounts._ID + " " + Accounts._ID_type + "," + - Accounts.NAME + " " + Accounts.NAME_type + ");"); - } + mUriMatcher.addURI(AUTHORITY, "key_rings/public", PUBLIC_KEY_RINGS); + mUriMatcher.addURI(AUTHORITY, "key_rings/public/*", PUBLIC_KEY_RING_ID); - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - // TODO: upgrade db if necessary, and do that in a clever way - } + mUriMatcher.addURI(AUTHORITY, "key_rings/secret/key_id/*", SECRET_KEY_RING_BY_KEY_ID); + + mUriMatcher.addURI(AUTHORITY, "key_rings/secret/*/keys", SECRET_KEY_RING_KEYS); + mUriMatcher.addURI(AUTHORITY, "key_rings/secret/*/keys/#", SECRET_KEY_RING_KEY_RANK); + + mUriMatcher.addURI(AUTHORITY, "key_rings/secret/*/user_ids", SECRET_KEY_RING_USER_IDS); + mUriMatcher.addURI(AUTHORITY, "key_rings/secret/*/user_ids/#", SECRET_KEY_RING_USER_ID_RANK); + + mUriMatcher.addURI(AUTHORITY, "key_rings/secret", SECRET_KEY_RINGS); + mUriMatcher.addURI(AUTHORITY, "key_rings/secret/*", SECRET_KEY_RING_ID); } @Override public boolean onCreate() { - mdbHelper = new DatabaseHelper(getContext()); + mDb = new Database(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + // TODO: implement the others, then use them for the lists SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); - - switch (mUriMatcher.match(uri)) { - case PUBLIC_KEYS: { - qb.setTables(PublicKeys.TABLE_NAME); - qb.setProjectionMap(mPublicKeysProjectionMap); + HashMap<String, String> projectionMap = new HashMap<String, String>(); + int match = mUriMatcher.match(uri); + int type; + switch (match) { + case PUBLIC_KEY_RINGS: + case PUBLIC_KEY_RING_ID: + case PUBLIC_KEY_RING_BY_KEY_ID: + case PUBLIC_KEY_RING_KEYS: + case PUBLIC_KEY_RING_KEY_RANK: + case PUBLIC_KEY_RING_USER_IDS: + case PUBLIC_KEY_RING_USER_ID_RANK: + type = Id.database.type_public; break; - } - case PUBLIC_KEY_ID: { - qb.setTables(PublicKeys.TABLE_NAME); - qb.setProjectionMap(mPublicKeysProjectionMap); - qb.appendWhere(PublicKeys._ID + "=" + uri.getPathSegments().get(1)); + case SECRET_KEY_RINGS: + case SECRET_KEY_RING_ID: + case SECRET_KEY_RING_BY_KEY_ID: + case SECRET_KEY_RING_KEYS: + case SECRET_KEY_RING_KEY_RANK: + case SECRET_KEY_RING_USER_IDS: + case SECRET_KEY_RING_USER_ID_RANK: + type = Id.database.type_secret; break; - } - case PUBLIC_KEY_BY_KEY_ID: { - qb.setTables(PublicKeys.TABLE_NAME); - qb.setProjectionMap(mPublicKeysProjectionMap); - qb.appendWhere(PublicKeys.KEY_ID + "=" + uri.getPathSegments().get(2)); - break; + default: { + throw new IllegalArgumentException("Unknown URI " + uri); } + } - case SECRET_KEYS: { - qb.setTables(SecretKeys.TABLE_NAME); - qb.setProjectionMap(mSecretKeysProjectionMap); - break; - } + qb.appendWhere(KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = " + type); + + switch (match) { + case PUBLIC_KEY_RINGS: + case SECRET_KEY_RINGS: { + qb.setTables(KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " + + "(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + + Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + " AND " + + Keys.TABLE_NAME + "." + Keys.IS_MASTER_KEY + " = '1'" + + ") " + + " INNER JOIN " + UserIds.TABLE_NAME + " ON " + + "(" + Keys.TABLE_NAME + "." + Keys._ID + " = " + + UserIds.TABLE_NAME + "." + UserIds.KEY_ID + " AND " + + UserIds.TABLE_NAME + "." + UserIds.RANK + " = '0') "); + + projectionMap.put(MASTER_KEY_ID, + KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID); + projectionMap.put(USER_ID, + UserIds.TABLE_NAME + "." + UserIds.USER_ID); - case SECRET_KEY_ID: { - qb.setTables(SecretKeys.TABLE_NAME); - qb.setProjectionMap(mSecretKeysProjectionMap); - qb.appendWhere(SecretKeys._ID + "=" + uri.getPathSegments().get(1)); break; } - case SECRET_KEY_BY_KEY_ID: { - qb.setTables(SecretKeys.TABLE_NAME); - qb.setProjectionMap(mSecretKeysProjectionMap); - qb.appendWhere(SecretKeys.KEY_ID + "=" + uri.getPathSegments().get(2)); + case PUBLIC_KEY_RING_ID: + case SECRET_KEY_RING_ID: { + qb.setTables(KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " + + "(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + + Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + " AND " + + Keys.TABLE_NAME + "." + Keys.IS_MASTER_KEY + " = '1'" + + ") " + + " INNER JOIN " + UserIds.TABLE_NAME + " ON " + + "(" + Keys.TABLE_NAME + "." + Keys._ID + " = " + + UserIds.TABLE_NAME + "." + UserIds.KEY_ID + " AND " + + UserIds.TABLE_NAME + "." + UserIds.RANK + " = '0') "); + + projectionMap.put(MASTER_KEY_ID, + KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID); + projectionMap.put(USER_ID, + UserIds.TABLE_NAME + "." + UserIds.USER_ID); + + qb.appendWhere(" AND " + + KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID + " = "); + qb.appendWhereEscapeString(uri.getPathSegments().get(2)); break; } - case ACCOUNTS: { - qb.setTables(Accounts.TABLE_NAME); - qb.setProjectionMap(mAccountsProjectionMap); - break; - } + case SECRET_KEY_RING_BY_KEY_ID: + case PUBLIC_KEY_RING_BY_KEY_ID: { + qb.setTables(Keys.TABLE_NAME + " AS tmp INNER JOIN " + + KeyRings.TABLE_NAME + " ON (" + + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + + "tmp." + Keys.KEY_RING_ID + ")" + + " INNER JOIN " + Keys.TABLE_NAME + " ON " + + "(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + + Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + " AND " + + Keys.TABLE_NAME + "." + Keys.IS_MASTER_KEY + " = '1'" + + ") " + + " INNER JOIN " + UserIds.TABLE_NAME + " ON " + + "(" + Keys.TABLE_NAME + "." + Keys._ID + " = " + + UserIds.TABLE_NAME + "." + UserIds.KEY_ID + " AND " + + UserIds.TABLE_NAME + "." + UserIds.RANK + " = '0') "); + + projectionMap.put(MASTER_KEY_ID, + KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID); + projectionMap.put(USER_ID, + UserIds.TABLE_NAME + "." + UserIds.USER_ID); + + qb.appendWhere(" AND tmp." + Keys.KEY_ID + " = "); + qb.appendWhereEscapeString(uri.getPathSegments().get(3)); - case ACCOUNT_ID: { - qb.setTables(Accounts.TABLE_NAME); - qb.setProjectionMap(mAccountsProjectionMap); - qb.appendWhere(Accounts._ID + "=" + uri.getPathSegments().get(1)); break; } @@ -189,20 +223,20 @@ public class DataProvider extends ContentProvider { } } + qb.setProjectionMap(projectionMap); + // If no sort order is specified use the default String orderBy; if (TextUtils.isEmpty(sortOrder)) { - orderBy = PublicKeys.DEFAULT_SORT_ORDER; + orderBy = null; } else { orderBy = sortOrder; } - // Get the database and run the query - SQLiteDatabase db = mdbHelper.getReadableDatabase(); - Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); + //System.out.println(qb.buildQuery(projection, selection, selectionArgs, null, null, sortOrder, null).replace("WHERE", "WHERE\n")); + Cursor c = qb.query(mDb.db(), projection, selection, selectionArgs, null, null, orderBy); - // Tell the cursor what uri to watch, so it knows when its source data - // changes + // Tell the cursor what uri to watch, so it knows when its source data changes c.setNotificationUri(getContext().getContentResolver(), uri); return c; } @@ -210,278 +244,68 @@ public class DataProvider extends ContentProvider { @Override public String getType(Uri uri) { switch (mUriMatcher.match(uri)) { - case PUBLIC_KEYS: { - return PublicKeys.CONTENT_TYPE; - } + case PUBLIC_KEY_RINGS: + return PUBLIC_KEY_RING_CONTENT_DIR_TYPE; - case PUBLIC_KEY_ID: { - return PublicKeys.CONTENT_ITEM_TYPE; - } + case PUBLIC_KEY_RING_ID: + return PUBLIC_KEY_RING_CONTENT_ITEM_TYPE; - case PUBLIC_KEY_BY_KEY_ID: { - return PublicKeys.CONTENT_ITEM_TYPE; - } + case PUBLIC_KEY_RING_BY_KEY_ID: + return PUBLIC_KEY_RING_CONTENT_ITEM_TYPE; - case SECRET_KEYS: { - return SecretKeys.CONTENT_TYPE; - } + case PUBLIC_KEY_RING_KEYS: + return PUBLIC_KEY_CONTENT_DIR_TYPE; - case SECRET_KEY_ID: { - return SecretKeys.CONTENT_ITEM_TYPE; - } + case PUBLIC_KEY_RING_KEY_RANK: + return PUBLIC_KEY_CONTENT_ITEM_TYPE; - case SECRET_KEY_BY_KEY_ID: { - return SecretKeys.CONTENT_ITEM_TYPE; - } + case PUBLIC_KEY_RING_USER_IDS: + return USER_ID_CONTENT_DIR_TYPE; - case ACCOUNTS: { - return Accounts.CONTENT_TYPE; - } + case PUBLIC_KEY_RING_USER_ID_RANK: + return USER_ID_CONTENT_ITEM_TYPE; - case ACCOUNT_ID: { - return Accounts.CONTENT_ITEM_TYPE; - } + case SECRET_KEY_RINGS: + return SECRET_KEY_RING_CONTENT_DIR_TYPE; - default: { - throw new IllegalArgumentException("Unknown URI " + uri); - } - } - } + case SECRET_KEY_RING_ID: + return SECRET_KEY_RING_CONTENT_ITEM_TYPE; - @Override - public Uri insert(Uri uri, ContentValues initialValues) { - switch (mUriMatcher.match(uri)) { - case PUBLIC_KEYS: { - ContentValues values; - if (initialValues != null) { - values = new ContentValues(initialValues); - } else { - values = new ContentValues(); - } - - if (!values.containsKey(PublicKeys.WHO_ID)) { - values.put(PublicKeys.WHO_ID, ""); - } - - SQLiteDatabase db = mdbHelper.getWritableDatabase(); - long rowId = db.insert(PublicKeys.TABLE_NAME, PublicKeys.WHO_ID, values); - if (rowId > 0) { - Uri transferUri = ContentUris.withAppendedId(PublicKeys.CONTENT_URI, rowId); - getContext().getContentResolver().notifyChange(transferUri, null); - return transferUri; - } - - throw new SQLException("Failed to insert row into " + uri); - } + case SECRET_KEY_RING_BY_KEY_ID: + return SECRET_KEY_RING_CONTENT_ITEM_TYPE; - case SECRET_KEYS: { - ContentValues values; - if (initialValues != null) { - values = new ContentValues(initialValues); - } else { - values = new ContentValues(); - } - - if (!values.containsKey(SecretKeys.WHO_ID)) { - values.put(SecretKeys.WHO_ID, ""); - } - - SQLiteDatabase db = mdbHelper.getWritableDatabase(); - long rowId = db.insert(SecretKeys.TABLE_NAME, SecretKeys.WHO_ID, values); - if (rowId > 0) { - Uri transferUri = ContentUris.withAppendedId(SecretKeys.CONTENT_URI, rowId); - getContext().getContentResolver().notifyChange(transferUri, null); - return transferUri; - } - - throw new SQLException("Failed to insert row into " + uri); - } + case SECRET_KEY_RING_KEYS: + return SECRET_KEY_CONTENT_DIR_TYPE; - case ACCOUNTS: { - ContentValues values; - if (initialValues != null) { - values = new ContentValues(initialValues); - } else { - values = new ContentValues(); - } - - SQLiteDatabase db = mdbHelper.getWritableDatabase(); - long rowId = db.insert(Accounts.TABLE_NAME, null, values); - if (rowId > 0) { - Uri transferUri = ContentUris.withAppendedId(Accounts.CONTENT_URI, rowId); - getContext().getContentResolver().notifyChange(transferUri, null); - return transferUri; - } - - throw new SQLException("Failed to insert row into " + uri); - } + case SECRET_KEY_RING_KEY_RANK: + return SECRET_KEY_CONTENT_ITEM_TYPE; - default: { + case SECRET_KEY_RING_USER_IDS: + return USER_ID_CONTENT_DIR_TYPE; + + case SECRET_KEY_RING_USER_ID_RANK: + return USER_ID_CONTENT_ITEM_TYPE; + + default: throw new IllegalArgumentException("Unknown URI " + uri); - } } } @Override - public int delete(Uri uri, String where, String[] whereArgs) { - SQLiteDatabase db = mdbHelper.getWritableDatabase(); - int count; - switch (mUriMatcher.match(uri)) { - case PUBLIC_KEYS: { - count = db.delete(PublicKeys.TABLE_NAME, where, whereArgs); - break; - } - - case PUBLIC_KEY_ID: { - String publicKeyId = uri.getPathSegments().get(1); - count = db.delete(PublicKeys.TABLE_NAME, - PublicKeys._ID + "=" + publicKeyId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - case PUBLIC_KEY_BY_KEY_ID: { - String publicKeyKeyId = uri.getPathSegments().get(2); - count = db.delete(PublicKeys.TABLE_NAME, - PublicKeys.KEY_ID + "=" + publicKeyKeyId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - case SECRET_KEYS: { - count = db.delete(SecretKeys.TABLE_NAME, where, whereArgs); - break; - } - - case SECRET_KEY_ID: { - String secretKeyId = uri.getPathSegments().get(1); - count = db.delete(SecretKeys.TABLE_NAME, - SecretKeys._ID + "=" + secretKeyId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - case SECRET_KEY_BY_KEY_ID: { - String secretKeyKeyId = uri.getPathSegments().get(2); - count = db.delete(SecretKeys.TABLE_NAME, - SecretKeys.KEY_ID + "=" + secretKeyKeyId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - case ACCOUNTS: { - count = db.delete(Accounts.TABLE_NAME, where, whereArgs); - break; - } - - case ACCOUNT_ID: { - String accountId = uri.getPathSegments().get(1); - count = db.delete(Accounts.TABLE_NAME, - Accounts._ID + "=" + accountId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - default: { - throw new IllegalArgumentException("Unknown URI " + uri); - } - } + public Uri insert(Uri uri, ContentValues initialValues) { + // not supported + return null; + } - getContext().getContentResolver().notifyChange(uri, null); - return count; + @Override + public int delete(Uri uri, String where, String[] whereArgs) { + // not supported + return 0; } @Override public int update(Uri uri, ContentValues values, String where, String[] whereArgs) { - SQLiteDatabase db = mdbHelper.getWritableDatabase(); - int count; - switch (mUriMatcher.match(uri)) { - case PUBLIC_KEYS: { - count = db.update(PublicKeys.TABLE_NAME, values, where, whereArgs); - break; - } - - case PUBLIC_KEY_ID: { - String publicKeyId = uri.getPathSegments().get(1); - - count = db.update(PublicKeys.TABLE_NAME, values, - PublicKeys._ID + "=" + publicKeyId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - case PUBLIC_KEY_BY_KEY_ID: { - String publicKeyKeyId = uri.getPathSegments().get(2); - - count = db.update(PublicKeys.TABLE_NAME, values, - PublicKeys.KEY_ID + "=" + publicKeyKeyId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - case SECRET_KEYS: { - count = db.update(SecretKeys.TABLE_NAME, values, where, whereArgs); - break; - } - - case SECRET_KEY_ID: { - String secretKeyId = uri.getPathSegments().get(1); - - count = db.update(SecretKeys.TABLE_NAME, values, - SecretKeys._ID + "=" + secretKeyId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - case SECRET_KEY_BY_KEY_ID: { - String secretKeyKeyId = uri.getPathSegments().get(2); - - count = db.update(SecretKeys.TABLE_NAME, values, - SecretKeys.KEY_ID + "=" + secretKeyKeyId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - case ACCOUNTS: { - count = db.update(Accounts.TABLE_NAME, values, where, whereArgs); - break; - } - - case ACCOUNT_ID: { - String accountId = uri.getPathSegments().get(1); - - count = db.update(Accounts.TABLE_NAME, values, - Accounts._ID + "=" + accountId + - (!TextUtils.isEmpty(where) ? - " AND (" + where + ')' : ""), - whereArgs); - break; - } - - default: { - throw new IllegalArgumentException("Unknown URI " + uri); - } - } - - getContext().getContentResolver().notifyChange(uri, null); - return count; + // not supported + return 0; } } diff --git a/src/org/thialfihar/android/apg/provider/Database.java b/src/org/thialfihar/android/apg/provider/Database.java new file mode 100644 index 000000000..810ebebbf --- /dev/null +++ b/src/org/thialfihar/android/apg/provider/Database.java @@ -0,0 +1,605 @@ +package org.thialfihar.android.apg.provider;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Vector;
+
+import org.bouncycastle2.openpgp.PGPException;
+import org.bouncycastle2.openpgp.PGPPublicKey;
+import org.bouncycastle2.openpgp.PGPPublicKeyRing;
+import org.bouncycastle2.openpgp.PGPSecretKey;
+import org.bouncycastle2.openpgp.PGPSecretKeyRing;
+import org.thialfihar.android.apg.Apg;
+import org.thialfihar.android.apg.Id;
+import org.thialfihar.android.apg.utils.IterableIterator;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.util.Log;
+
+public class Database extends SQLiteOpenHelper {
+ public static class GeneralException extends Exception {
+ static final long serialVersionUID = 0xf812773343L;
+
+ public GeneralException(String message) {
+ super(message);
+ }
+ }
+
+ private static final String DATABASE_NAME = "apg";
+ private static final int DATABASE_VERSION = 2;
+
+ public static final String AUTHORITY = "org.thialfihar.android.apg.database";
+
+ 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);
+ }
+
+ public Database(Context context) {
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ // force upgrade to test things
+ //onUpgrade(getWritableDatabase(), 1, 2);
+ mDb = getWritableDatabase();
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ mDb.close();
+ super.finalize();
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ db.execSQL("CREATE TABLE " + KeyRings.TABLE_NAME + " (" +
+ KeyRings._ID + " " + KeyRings._ID_type + "," +
+ KeyRings.MASTER_KEY_ID + " " + KeyRings.MASTER_KEY_ID_type + ", " +
+ KeyRings.TYPE + " " + KeyRings.TYPE_type + ", " +
+ KeyRings.WHO_ID + " " + KeyRings.WHO_ID_type + ", " +
+ KeyRings.KEY_RING_DATA + " " + KeyRings.KEY_RING_DATA_type + ");");
+
+ db.execSQL("CREATE TABLE " + Keys.TABLE_NAME + " (" +
+ Keys._ID + " " + Keys._ID_type + "," +
+ Keys.KEY_ID + " " + Keys.KEY_ID_type + ", " +
+ Keys.TYPE + " " + Keys.TYPE_type + ", " +
+ Keys.IS_MASTER_KEY + " " + Keys.IS_MASTER_KEY_type + ", " +
+ Keys.ALGORITHM + " " + Keys.ALGORITHM_type + ", " +
+ Keys.KEY_SIZE + " " + Keys.KEY_SIZE_type + ", " +
+ Keys.CAN_SIGN + " " + Keys.CAN_SIGN_type + ", " +
+ Keys.CAN_ENCRYPT + " " + Keys.CAN_ENCRYPT_type + ", " +
+ Keys.IS_REVOKED + " " + Keys.IS_REVOKED_type + ", " +
+ Keys.CREATION + " " + Keys.CREATION_type + ", " +
+ Keys.EXPIRY + " " + Keys.EXPIRY_type + ", " +
+ Keys.KEY_RING_ID + " " + Keys.KEY_RING_ID_type + ", " +
+ Keys.KEY_DATA + " " + Keys.KEY_DATA_type +
+ Keys.RANK + " " + Keys.RANK_type + ");");
+
+ db.execSQL("CREATE TABLE " + UserIds.TABLE_NAME + " (" +
+ UserIds._ID + " " + UserIds._ID_type + "," +
+ UserIds.KEY_ID + " " + UserIds.KEY_ID_type + "," +
+ UserIds.USER_ID + " " + UserIds.USER_ID_type + "," +
+ UserIds.RANK + " " + UserIds.RANK_type + ");");
+
+ db.execSQL("CREATE TABLE " + Accounts.TABLE_NAME + " (" +
+ Accounts._ID + " " + Accounts._ID_type + "," +
+ Accounts.NAME + " " + Accounts.NAME_type + ");");
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ mDb = db;
+ for (int version = oldVersion; version < newVersion; ++version) {
+ switch (version) {
+ case 1: { // upgrade 1 to 2
+ db.execSQL("DROP TABLE IF EXISTS " + KeyRings.TABLE_NAME + ";");
+ db.execSQL("DROP TABLE IF EXISTS " + Keys.TABLE_NAME + ";");
+ db.execSQL("DROP TABLE IF EXISTS " + UserIds.TABLE_NAME + ";");
+
+ db.execSQL("CREATE TABLE " + KeyRings.TABLE_NAME + " (" +
+ KeyRings._ID + " " + KeyRings._ID_type + "," +
+ KeyRings.MASTER_KEY_ID + " " + KeyRings.MASTER_KEY_ID_type + ", " +
+ KeyRings.TYPE + " " + KeyRings.TYPE_type + ", " +
+ KeyRings.WHO_ID + " " + KeyRings.WHO_ID_type + ", " +
+ KeyRings.KEY_RING_DATA + " " + KeyRings.KEY_RING_DATA_type + ");");
+
+ db.execSQL("CREATE TABLE " + Keys.TABLE_NAME + " (" +
+ Keys._ID + " " + Keys._ID_type + "," +
+ Keys.KEY_ID + " " + Keys.KEY_ID_type + ", " +
+ Keys.TYPE + " " + Keys.TYPE_type + ", " +
+ Keys.IS_MASTER_KEY + " " + Keys.IS_MASTER_KEY_type + ", " +
+ Keys.ALGORITHM + " " + Keys.ALGORITHM_type + ", " +
+ Keys.KEY_SIZE + " " + Keys.KEY_SIZE_type + ", " +
+ Keys.CAN_SIGN + " " + Keys.CAN_SIGN_type + ", " +
+ Keys.CAN_ENCRYPT + " " + Keys.CAN_ENCRYPT_type + ", " +
+ Keys.IS_REVOKED + " " + Keys.IS_REVOKED_type + ", " +
+ Keys.CREATION + " " + Keys.CREATION_type + ", " +
+ Keys.EXPIRY + " " + Keys.EXPIRY_type + ", " +
+ Keys.KEY_RING_ID + " " + Keys.KEY_RING_ID_type + ", " +
+ Keys.KEY_DATA + " " + Keys.KEY_DATA_type +
+ Keys.RANK + " " + Keys.RANK_type + ");");
+
+ db.execSQL("CREATE TABLE " + UserIds.TABLE_NAME + " (" +
+ UserIds._ID + " " + UserIds._ID_type + "," +
+ UserIds.KEY_ID + " " + UserIds.KEY_ID_type + "," +
+ UserIds.USER_ID + " " + UserIds.USER_ID_type + "," +
+ UserIds.RANK + " " + UserIds.RANK_type + ");");
+
+ Cursor cursor = db.query("public_keys", new String[] { "c_key_data" },
+ null, null, null, null, null);
+ if (cursor != null && cursor.moveToFirst()) {
+ do {
+ byte[] data = cursor.getBlob(0);
+ try {
+ PGPPublicKeyRing keyRing = new PGPPublicKeyRing(data);
+ saveKeyRing(keyRing);
+ } catch (IOException e) {
+ Log.e("apg.db.upgrade", "key import failed: " + e);
+ } catch (GeneralException e) {
+ Log.e("apg.db.upgrade", "key import failed: " + e);
+ }
+ } while (cursor.moveToNext());
+ }
+
+ if (cursor != null) {
+ cursor.close();
+ }
+
+ cursor = db.query("secret_keys", new String[]{ "c_key_data" },
+ null, null, null, null, null);
+ if (cursor != null && cursor.moveToFirst()) {
+ do {
+ byte[] data = cursor.getBlob(0);
+ try {
+ PGPSecretKeyRing keyRing = new PGPSecretKeyRing(data);
+ saveKeyRing(keyRing);
+ } catch (IOException e) {
+ Log.e("apg.db.upgrade", "key import failed: " + e);
+ } catch (PGPException e) {
+ Log.e("apg.db.upgrade", "key import failed: " + e);
+ } catch (GeneralException e) {
+ Log.e("apg.db.upgrade", "key import failed: " + e);
+ }
+ } while (cursor.moveToNext());
+ }
+
+ if (cursor != null) {
+ cursor.close();
+ }
+
+ db.execSQL("DROP TABLE IF EXISTS public_keys;");
+ db.execSQL("DROP TABLE IF EXISTS secret_keys;");
+
+ break;
+ }
+
+ default: {
+ break;
+ }
+ }
+ }
+ mDb = null;
+ }
+
+ public int saveKeyRing(PGPPublicKeyRing keyRing) throws IOException, GeneralException {
+ mDb.beginTransaction();
+ ContentValues values = new ContentValues();
+ PGPPublicKey masterKey = keyRing.getPublicKey();
+ long masterKeyId = masterKey.getKeyID();
+
+ values.put(KeyRings.MASTER_KEY_ID, masterKeyId);
+ values.put(KeyRings.TYPE, Id.database.type_public);
+ values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded());
+
+ long rowId = insertOrUpdateKeyRing(values);
+ int returnValue = mStatus;
+
+ if (rowId == -1) {
+ throw new GeneralException("saving public key ring " + masterKeyId + " failed");
+ }
+
+ Vector<Integer> seenIds = new Vector<Integer>();
+ int rank = 0;
+ for (PGPPublicKey key : new IterableIterator<PGPPublicKey>(keyRing.getPublicKeys())) {
+ seenIds.add(saveKey(rowId, key, rank));
+ ++rank;
+ }
+
+ String seenIdsStr = "";
+ for (Integer id : seenIds) {
+ if (seenIdsStr.length() > 0) {
+ seenIdsStr += ",";
+ }
+ seenIdsStr += id;
+ }
+ mDb.delete(Keys.TABLE_NAME,
+ Keys.KEY_RING_ID + " = ? AND " +
+ Keys._ID + " NOT IN (" + seenIdsStr + ")",
+ new String[] { "" + rowId });
+
+ mDb.setTransactionSuccessful();
+ mDb.endTransaction();
+ return returnValue;
+ }
+
+ public int saveKeyRing(PGPSecretKeyRing keyRing) throws IOException, GeneralException {
+ mDb.beginTransaction();
+ ContentValues values = new ContentValues();
+ PGPSecretKey masterKey = keyRing.getSecretKey();
+ long masterKeyId = masterKey.getKeyID();
+
+ values.put(KeyRings.MASTER_KEY_ID, masterKeyId);
+ values.put(KeyRings.TYPE, Id.database.type_secret);
+ values.put(KeyRings.KEY_RING_DATA, keyRing.getEncoded());
+
+ long rowId = insertOrUpdateKeyRing(values);
+ int returnValue = mStatus;
+
+ if (rowId == -1) {
+ throw new GeneralException("saving secret key ring " + masterKeyId + " failed");
+ }
+
+ Vector<Integer> seenIds = new Vector<Integer>();
+ int rank = 0;
+ for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(keyRing.getSecretKeys())) {
+ seenIds.add(saveKey(rowId, key, rank));
+ ++rank;
+ }
+
+ String seenIdsStr = "";
+ for (Integer id : seenIds) {
+ if (seenIdsStr.length() > 0) {
+ seenIdsStr += ",";
+ }
+ seenIdsStr += id;
+ }
+ mDb.delete(Keys.TABLE_NAME,
+ Keys.KEY_RING_ID + " = ? AND " +
+ Keys._ID + " NOT IN (" + seenIdsStr + ")",
+ new String[] { "" + rowId });
+
+ mDb.setTransactionSuccessful();
+ mDb.endTransaction();
+ return returnValue;
+ }
+
+ private int saveKey(long keyRingId, PGPPublicKey key, int rank)
+ throws IOException, GeneralException {
+ ContentValues values = new ContentValues();
+
+ values.put(Keys.KEY_ID, key.getKeyID());
+ values.put(Keys.TYPE, Id.database.type_public);
+ 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, Apg.isSigningKey(key));
+ values.put(Keys.CAN_ENCRYPT, Apg.isEncryptionKey(key));
+ values.put(Keys.IS_REVOKED, key.isRevoked());
+ values.put(Keys.CREATION, Apg.getCreationDate(key).getTime() / 1000);
+ Date expiryDate = Apg.getExpiryDate(key);
+ if (expiryDate != null) {
+ values.put(Keys.EXPIRY, expiryDate.getTime() / 1000);
+ }
+ values.put(Keys.KEY_RING_ID, keyRingId);
+ values.put(Keys.KEY_DATA, key.getEncoded());
+ values.put(Keys.RANK, rank);
+
+ long rowId = insertOrUpdateKey(values);
+
+ if (rowId == -1) {
+ throw new GeneralException("saving public key " + key.getKeyID() + " failed");
+ }
+
+ Vector<Integer> seenIds = new Vector<Integer>();
+ int userIdRank = 0;
+ for (String userId : new IterableIterator<String>(key.getUserIDs())) {
+ seenIds.add(saveUserId(rowId, userId, userIdRank));
+ ++userIdRank;
+ }
+
+ String seenIdsStr = "";
+ for (Integer id : seenIds) {
+ if (seenIdsStr.length() > 0) {
+ seenIdsStr += ",";
+ }
+ seenIdsStr += id;
+ }
+ mDb.delete(UserIds.TABLE_NAME,
+ UserIds.KEY_ID + " = ? AND " +
+ UserIds._ID + " NOT IN (" + seenIdsStr + ")",
+ new String[] { "" + rowId });
+
+ return (int)rowId;
+ }
+
+ private int saveKey(long keyRingId, PGPSecretKey key, int rank)
+ throws IOException, GeneralException {
+ ContentValues values = new ContentValues();
+
+ values.put(Keys.KEY_ID, key.getPublicKey().getKeyID());
+ values.put(Keys.TYPE, Id.database.type_secret);
+ 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, Apg.isSigningKey(key));
+ values.put(Keys.CAN_ENCRYPT, Apg.isEncryptionKey(key));
+ values.put(Keys.IS_REVOKED, key.getPublicKey().isRevoked());
+ values.put(Keys.CREATION, Apg.getCreationDate(key).getTime() / 1000);
+ Date expiryDate = Apg.getExpiryDate(key);
+ if (expiryDate != null) {
+ values.put(Keys.EXPIRY, expiryDate.getTime() / 1000);
+ }
+ values.put(Keys.KEY_RING_ID, keyRingId);
+ values.put(Keys.KEY_DATA, key.getEncoded());
+ values.put(Keys.RANK, rank);
+
+ long rowId = insertOrUpdateKey(values);
+
+ if (rowId == -1) {
+ throw new GeneralException("saving secret key " + key.getPublicKey().getKeyID() + " failed");
+ }
+
+ Vector<Integer> seenIds = new Vector<Integer>();
+ int userIdRank = 0;
+ for (String userId : new IterableIterator<String>(key.getUserIDs())) {
+ seenIds.add(saveUserId(rowId, userId, userIdRank));
+ ++userIdRank;
+ }
+
+ String seenIdsStr = "";
+ for (Integer id : seenIds) {
+ if (seenIdsStr.length() > 0) {
+ seenIdsStr += ",";
+ }
+ seenIdsStr += id;
+ }
+ mDb.delete(UserIds.TABLE_NAME,
+ UserIds.KEY_ID + " = ? AND " +
+ UserIds._ID + " NOT IN (" + seenIdsStr + ")",
+ new String[] { "" + rowId });
+
+ return (int)rowId;
+ }
+
+ private int saveUserId(long keyId, String userId, int rank) throws GeneralException {
+ ContentValues values = new ContentValues();
+
+ values.put(UserIds.KEY_ID, keyId);
+ values.put(UserIds.USER_ID, userId);
+ values.put(UserIds.RANK, rank);
+
+ long rowId = insertOrUpdateUserId(values);
+
+ if (rowId == -1) {
+ throw new GeneralException("saving user id " + userId + " failed");
+ }
+
+ return (int)rowId;
+ }
+
+ private long insertOrUpdateKeyRing(ContentValues values) {
+ Cursor c = mDb.query(KeyRings.TABLE_NAME, new String[] { KeyRings._ID },
+ KeyRings.MASTER_KEY_ID + " = ? AND " + KeyRings.TYPE + " = ?",
+ new String[] {
+ values.getAsString(KeyRings.MASTER_KEY_ID),
+ values.getAsString(KeyRings.TYPE),
+ },
+ null, null, null);
+ long rowId = -1;
+ if (c != null && c.moveToFirst()) {
+ rowId = c.getLong(0);
+ mDb.update(KeyRings.TABLE_NAME, values,
+ KeyRings._ID + " = ?", new String[] { "" + rowId });
+ mStatus = Id.return_value.updated;
+ } else {
+ rowId = mDb.insert(KeyRings.TABLE_NAME, KeyRings.WHO_ID, values);
+ mStatus = Id.return_value.ok;
+ }
+
+ if (c != null) {
+ c.close();
+ }
+
+ return rowId;
+ }
+
+ private long insertOrUpdateKey(ContentValues values) {
+ Cursor c = mDb.query(Keys.TABLE_NAME, new String[] { Keys._ID },
+ Keys.KEY_ID + " = ? AND " + Keys.TYPE + " = ?",
+ new String[] {
+ values.getAsString(Keys.KEY_ID),
+ values.getAsString(Keys.TYPE),
+ },
+ null, null, null);
+ long rowId = -1;
+ if (c != null && c.moveToFirst()) {
+ rowId = c.getLong(0);
+ mDb.update(Keys.TABLE_NAME, values,
+ Keys._ID + " = ?", new String[] { "" + rowId });
+ } else {
+ rowId = mDb.insert(Keys.TABLE_NAME, Keys.KEY_DATA, values);
+ }
+
+ if (c != null) {
+ c.close();
+ }
+
+ return rowId;
+ }
+
+ private long insertOrUpdateUserId(ContentValues values) {
+ Cursor c = mDb.query(UserIds.TABLE_NAME, new String[] { UserIds._ID },
+ UserIds.KEY_ID + " = ? AND " + UserIds.USER_ID + " = ?",
+ new String[] {
+ values.getAsString(UserIds.KEY_ID),
+ values.getAsString(UserIds.USER_ID),
+ },
+ null, null, null);
+ long rowId = -1;
+ if (c != null && c.moveToFirst()) {
+ rowId = c.getLong(0);
+ mDb.update(UserIds.TABLE_NAME, values,
+ UserIds._ID + " = ?", new String[] { "" + rowId });
+ } else {
+ rowId = mDb.insert(UserIds.TABLE_NAME, UserIds.USER_ID, values);
+ }
+
+ if (c != null) {
+ c.close();
+ }
+
+ return rowId;
+ }
+
+ public Object getKeyRing(int keyRingId) {
+ Cursor c = mDb.query(KeyRings.TABLE_NAME,
+ new String[] { KeyRings.KEY_RING_DATA, KeyRings.TYPE },
+ KeyRings._ID + " = ?",
+ new String[] {
+ "" + keyRingId,
+ },
+ null, null, null);
+ byte[] data = null;
+ Object keyRing = null;
+ if (c != null && c.moveToFirst()) {
+ data = c.getBlob(0);
+ if (data != null) {
+ try {
+ if (c.getInt(1) == Id.database.type_public) {
+ keyRing = new PGPPublicKeyRing(data);
+ } else {
+ keyRing = new PGPSecretKeyRing(data);
+ }
+ } catch (IOException e) {
+ // can't load it, then
+ } catch (PGPException e) {
+ // can't load it, then
+ }
+ }
+ }
+
+ if (c != null) {
+ c.close();
+ }
+
+ return keyRing;
+ }
+
+ public byte[] getKeyRingDataFromKeyId(int type, long keyId) {
+ Cursor c = mDb.query(Keys.TABLE_NAME + " INNER JOIN " + KeyRings.TABLE_NAME + " ON (" +
+ KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " +
+ Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + ")",
+ new String[] { KeyRings.TABLE_NAME + "." + KeyRings.KEY_RING_DATA },
+ Keys.TABLE_NAME + "." + Keys.KEY_ID + " = ? AND " +
+ KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?",
+ new String[] {
+ "" + keyId,
+ "" + type,
+ },
+ null, null, null);
+
+ byte[] data = null;
+ if (c != null && c.moveToFirst()) {
+ data = c.getBlob(0);
+ }
+
+ if (c != null) {
+ c.close();
+ }
+
+ return data;
+ }
+
+ public byte[] getKeyDataFromKeyId(int type, long keyId) {
+ Cursor c = mDb.query(Keys.TABLE_NAME, new String[] { Keys.KEY_DATA },
+ Keys.KEY_ID + " = ? AND " + Keys.TYPE + " = ?",
+ new String[] {
+ "" + keyId,
+ "" + type,
+ },
+ null, null, null);
+ byte[] data = null;
+ if (c != null && c.moveToFirst()) {
+ data = c.getBlob(0);
+ }
+
+ if (c != null) {
+ c.close();
+ }
+
+ return data;
+ }
+
+ public void deleteKeyRing(int keyRingId) {
+ mDb.beginTransaction();
+ mDb.delete(KeyRings.TABLE_NAME,
+ KeyRings._ID + " = ?", new String[] { "" + keyRingId });
+
+ Cursor c = mDb.query(Keys.TABLE_NAME, new String[] { Keys._ID },
+ Keys.KEY_RING_ID + " = ?",
+ new String[] {
+ "" + keyRingId,
+ },
+ null, null, null);
+ if (c != null && c.moveToFirst()) {
+ do {
+ int keyId = c.getInt(0);
+ deleteKey(keyId);
+ } while (c.moveToNext());
+ }
+
+ if (c != null) {
+ c.close();
+ }
+
+ mDb.setTransactionSuccessful();
+ mDb.endTransaction();
+ }
+
+ private void deleteKey(int keyId) {
+ mDb.delete(Keys.TABLE_NAME,
+ Keys._ID + " = ?", new String[] { "" + keyId });
+
+ mDb.delete(UserIds.TABLE_NAME,
+ UserIds.KEY_ID + " = ?", new String[] { "" + keyId });
+ }
+
+ public SQLiteDatabase db() {
+ return mDb;
+ }
+}
diff --git a/src/org/thialfihar/android/apg/provider/Accounts1.java b/src/org/thialfihar/android/apg/provider/KeyRings.java index 9009e4598..a4eae6695 100644 --- a/src/org/thialfihar/android/apg/provider/Accounts1.java +++ b/src/org/thialfihar/android/apg/provider/KeyRings.java @@ -1,36 +1,33 @@ -/* - * 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. - */ - -package org.thialfihar.android.apg.provider; - -import android.net.Uri; -import android.provider.BaseColumns; - -class Accounts1 implements BaseColumns { - public static final String TABLE_NAME = "accounts"; - - public static final String _ID_type = "INTEGER PRIMARY KEY"; - public static final String NAME = "c_name"; - public static final String NAME_type = "TEXT"; - - public static final Uri CONTENT_URI = - Uri.parse("content://" + DataProvider.AUTHORITY + "/accounts"); - public static final String CONTENT_TYPE = - "vnd.android.cursor.dir/vnd.thialfihar.apg.account"; - public static final String CONTENT_ITEM_TYPE = - "vnd.android.cursor.item/vnd.thialfihar.apg.account"; - public static final String DEFAULT_SORT_ORDER = _ID + " DESC"; -}
\ No newline at end of file +/*
+ * 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.
+ */
+
+package org.thialfihar.android.apg.provider;
+
+import android.provider.BaseColumns;
+
+public class KeyRings implements BaseColumns {
+ public static final String TABLE_NAME = "key_rings";
+
+ public static final String _ID_type = "INTEGER PRIMARY KEY";
+ public static final String MASTER_KEY_ID = "c_master_key_id";
+ public static final String MASTER_KEY_ID_type = "INT64";
+ public static final String TYPE = "c_type";
+ public static final String TYPE_type = "INTEGER";
+ public static final String WHO_ID = "c_who_id";
+ public static final String WHO_ID_type = "INTEGER";
+ public static final String KEY_RING_DATA = "c_key_ring_data";
+ public static final String KEY_RING_DATA_type = "BLOB";
+}
diff --git a/src/org/thialfihar/android/apg/provider/Keys.java b/src/org/thialfihar/android/apg/provider/Keys.java new file mode 100644 index 000000000..618c5e920 --- /dev/null +++ b/src/org/thialfihar/android/apg/provider/Keys.java @@ -0,0 +1,51 @@ +/* + * 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. + */ + +package org.thialfihar.android.apg.provider; + +import android.provider.BaseColumns; + +public class Keys implements BaseColumns { + public static final String TABLE_NAME = "keys"; + + public static final String _ID_type = "INTEGER PRIMARY KEY"; + public static final String KEY_ID = "c_key_id"; + public static final String KEY_ID_type = "INT64"; + public static final String TYPE = "c_type"; + public static final String TYPE_type = "INTEGER"; + public static final String IS_MASTER_KEY = "c_is_master_key"; + public static final String IS_MASTER_KEY_type = "INTEGER"; + public static final String ALGORITHM = "c_algorithm"; + public static final String ALGORITHM_type = "INTEGER"; + public static final String KEY_SIZE = "c_key_size"; + public static final String KEY_SIZE_type = "INTEGER"; + public static final String CAN_SIGN = "c_can_sign"; + public static final String CAN_SIGN_type = "INTEGER"; + public static final String CAN_ENCRYPT = "c_can_encrypt"; + public static final String CAN_ENCRYPT_type = "INTEGER"; + public static final String IS_REVOKED = "c_is_revoked"; + public static final String IS_REVOKED_type = "INTEGER"; + public static final String CREATION = "c_creation"; + public static final String CREATION_type = "INTEGER"; + public static final String EXPIRY = "c_expiry"; + public static final String EXPIRY_type = "INTEGER"; + public static final String KEY_RING_ID = "c_key_ring_id"; + public static final String KEY_RING_ID_type = "INTEGER"; + public static final String KEY_DATA = "c_key_data"; + public static final String KEY_DATA_type = "BLOB"; + public static final String RANK = "c_key_data"; + public static final String RANK_type = "INTEGER"; +} diff --git a/src/org/thialfihar/android/apg/provider/PublicKeys1.java b/src/org/thialfihar/android/apg/provider/PublicKeys1.java deleted file mode 100644 index d12a67a17..000000000 --- a/src/org/thialfihar/android/apg/provider/PublicKeys1.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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. - */ - -package org.thialfihar.android.apg.provider; - -import android.net.Uri; -import android.provider.BaseColumns; - -class PublicKeys1 implements BaseColumns { - public static final String TABLE_NAME = "public_keys"; - - public static final String _ID_type = "INTEGER PRIMARY KEY"; - public static final String KEY_ID = "c_key_id"; - public static final String KEY_ID_type = "INT64"; - public static final String KEY_DATA = "c_key_data"; - public static final String KEY_DATA_type = "BLOB"; - public static final String WHO_ID = "c_who_id"; - public static final String WHO_ID_type = "INTEGER"; - - public static final Uri CONTENT_URI = - Uri.parse("content://" + DataProvider.AUTHORITY + "/public_keys"); - public static final Uri CONTENT_URI_BY_KEY_ID = - Uri.parse("content://" + DataProvider.AUTHORITY + "/public_keys/key_id"); - public static final String CONTENT_TYPE = - "vnd.android.cursor.dir/vnd.thialfihar.apg.public_key"; - public static final String CONTENT_ITEM_TYPE = - "vnd.android.cursor.item/vnd.thialfihar.apg.public_key"; - public static final String DEFAULT_SORT_ORDER = _ID + " DESC"; -}
\ No newline at end of file diff --git a/src/org/thialfihar/android/apg/provider/SecretKeys.java b/src/org/thialfihar/android/apg/provider/SecretKeys.java deleted file mode 100644 index d31f306ae..000000000 --- a/src/org/thialfihar/android/apg/provider/SecretKeys.java +++ /dev/null @@ -1,22 +0,0 @@ -/*
- * 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.
- */
-
-package org.thialfihar.android.apg.provider;
-
-public class SecretKeys extends SecretKeys1 {
- private SecretKeys() {
- }
-}
\ No newline at end of file diff --git a/src/org/thialfihar/android/apg/provider/SecretKeys1.java b/src/org/thialfihar/android/apg/provider/SecretKeys1.java deleted file mode 100644 index 3ca405f70..000000000 --- a/src/org/thialfihar/android/apg/provider/SecretKeys1.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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. - */ - -package org.thialfihar.android.apg.provider; - -import android.net.Uri; -import android.provider.BaseColumns; - -class SecretKeys1 implements BaseColumns { - public static final String TABLE_NAME = "secret_keys"; - - public static final String _ID_type = "INTEGER PRIMARY KEY"; - public static final String KEY_ID = "c_key_id"; - public static final String KEY_ID_type = "INT64"; - public static final String KEY_DATA = "c_key_data"; - public static final String KEY_DATA_type = "BLOB"; - public static final String WHO_ID = "c_who_id"; - public static final String WHO_ID_type = "INTEGER"; - - public static final Uri CONTENT_URI = - Uri.parse("content://" + DataProvider.AUTHORITY + "/secret_keys"); - public static final Uri CONTENT_URI_BY_KEY_ID = - Uri.parse("content://" + DataProvider.AUTHORITY + "/secret_keys/key_id"); - public static final String CONTENT_TYPE = - "vnd.android.cursor.dir/vnd.thialfihar.apg.secret_key"; - public static final String CONTENT_ITEM_TYPE = - "vnd.android.cursor.item/vnd.thialfihar.apg.secret_key"; - public static final String DEFAULT_SORT_ORDER = _ID + " DESC"; -}
\ No newline at end of file diff --git a/src/org/thialfihar/android/apg/provider/PublicKeys.java b/src/org/thialfihar/android/apg/provider/UserIds.java index f15841fa5..2b1162beb 100644 --- a/src/org/thialfihar/android/apg/provider/PublicKeys.java +++ b/src/org/thialfihar/android/apg/provider/UserIds.java @@ -16,7 +16,16 @@ package org.thialfihar.android.apg.provider;
-public class PublicKeys extends PublicKeys1 {
- private PublicKeys() {
- }
-}
\ No newline at end of file +import android.provider.BaseColumns;
+
+public class UserIds implements BaseColumns {
+ public static final String TABLE_NAME = "user_ids";
+
+ public static final String _ID_type = "INTEGER PRIMARY KEY";
+ public static final String KEY_ID = "c_key_id";
+ public static final String KEY_ID_type = "INTEGER";
+ public static final String USER_ID = "c_user_id";
+ public static final String USER_ID_type = "TEXT";
+ public static final String RANK = "c_rank";
+ public static final String RANK_type = "INTEGER";
+}
|