diff options
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java')
-rw-r--r-- | OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java | 135 |
1 files changed, 73 insertions, 62 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 3e0520c2a..8e1bb3f60 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -122,15 +122,20 @@ public class ProviderHelper { * Find the master key id related to a given query. The id will either be extracted from the * query, which should work for all specific /key_rings/ queries, or will be queried if it can't. */ - public static long getMasterKeyId(Context context, Uri queryUri) throws NotFoundException { + public static long extractOrGetMasterKeyId(Context context, Uri queryUri) + throws NotFoundException { // try extracting from the uri first -// String firstSegment = queryUri.getPathSegments().get(1); -// if(!firstSegment.equals("find")) try { -// return Long.parseLong(firstSegment); -// } catch(NumberFormatException e) { -// // didn't work? oh well. -// Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying..."); -// } + String firstSegment = queryUri.getPathSegments().get(1); + if(!firstSegment.equals("find")) try { + return Long.parseLong(firstSegment); + } catch(NumberFormatException e) { + // didn't work? oh well. + Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying..."); + } + return getMasterKeyId(context, queryUri); + } + + public static long getMasterKeyId(Context context, Uri queryUri) throws NotFoundException { Object data = getGenericData(context, queryUri, KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER); if(data != null) { return (Long) data; @@ -461,70 +466,49 @@ public class ProviderHelper { return ContentProviderOperation.newInsert(uri).withValues(values).build(); } - public static ArrayList<String> getKeyRingsAsArmoredString(Context context, long[] masterKeyIds) { - ArrayList<String> output = new ArrayList<String>(); - - if (masterKeyIds != null && masterKeyIds.length > 0) { - - Cursor cursor = getCursorWithSelectedKeyringMasterKeyIds(context, masterKeyIds); - - if (cursor != null) { - int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID); - int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA); - if (cursor.moveToFirst()) { - do { - Log.d(Constants.TAG, "masterKeyId: " + cursor.getLong(masterIdCol)); + private static String getKeyRingAsArmoredString(Context context, byte[] data) throws IOException { + Object keyRing = null; + if (data != null) { + keyRing = PgpConversionHelper.BytesToPGPKeyRing(data); + } - // get actual keyring data blob and write it to ByteArrayOutputStream - try { - Object keyRing = null; - byte[] data = cursor.getBlob(dataCol); - if (data != null) { - keyRing = PgpConversionHelper.BytesToPGPKeyRing(data); - } + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ArmoredOutputStream aos = new ArmoredOutputStream(bos); + aos.setHeader("Version", PgpHelper.getFullVersion(context)); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - ArmoredOutputStream aos = new ArmoredOutputStream(bos); - aos.setHeader("Version", PgpHelper.getFullVersion(context)); + if (keyRing instanceof PGPSecretKeyRing) { + aos.write(((PGPSecretKeyRing) keyRing).getEncoded()); + } else if (keyRing instanceof PGPPublicKeyRing) { + aos.write(((PGPPublicKeyRing) keyRing).getEncoded()); + } + aos.close(); - if (keyRing instanceof PGPSecretKeyRing) { - aos.write(((PGPSecretKeyRing) keyRing).getEncoded()); - } else if (keyRing instanceof PGPPublicKeyRing) { - aos.write(((PGPPublicKeyRing) keyRing).getEncoded()); - } - aos.close(); + String armoredKey = bos.toString("UTF-8"); - String armoredKey = bos.toString("UTF-8"); + Log.d(Constants.TAG, "armoredKey:" + armoredKey); - Log.d(Constants.TAG, "armoredKey:" + armoredKey); + return armoredKey; + } - output.add(armoredKey); - } catch (IOException e) { - Log.e(Constants.TAG, "IOException", e); - } - } while (cursor.moveToNext()); - } - } + public static String getKeyRingAsArmoredString(Context context, Uri uri) + throws NotFoundException, IOException { + byte[] data = (byte[]) ProviderHelper.getGenericData( + context, uri, KeyRingData.KEY_RING_DATA, ProviderHelper.FIELD_TYPE_BLOB); + return getKeyRingAsArmoredString(context, data); + } - if (cursor != null) { - cursor.close(); - } + // TODO This method is NOT ACTUALLY USED. Is this preparation for something, or just dead code? + public static ArrayList<String> getKeyRingsAsArmoredString(Context context, long[] masterKeyIds) + throws IOException { + ArrayList<String> output = new ArrayList<String>(); - } else { + if (masterKeyIds == null || masterKeyIds.length == 0) { Log.e(Constants.TAG, "No master keys given!"); - } - - if (output.size() > 0) { return output; - } else { - return null; } - } - - private static Cursor getCursorWithSelectedKeyringMasterKeyIds(Context context, long[] masterKeyIds) { - Cursor cursor = null; - if (masterKeyIds != null && masterKeyIds.length > 0) { + // Build a cursor for the selected masterKeyIds + Cursor cursor = null; { String inMasterKeyList = KeyRingData.MASTER_KEY_ID + " IN ("; for (int i = 0; i < masterKeyIds.length; ++i) { if (i != 0) { @@ -536,10 +520,37 @@ public class ProviderHelper { cursor = context.getContentResolver().query(KeyRingData.buildPublicKeyRingUri(), new String[] { KeyRingData._ID, KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA - }, inMasterKeyList, null, null); + }, inMasterKeyList, null, null); } - return cursor; + if (cursor != null) { + int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID); + int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA); + if (cursor.moveToFirst()) { + do { + Log.d(Constants.TAG, "masterKeyId: " + cursor.getLong(masterIdCol)); + + byte[] data = cursor.getBlob(dataCol); + + // get actual keyring data blob and write it to ByteArrayOutputStream + try { + output.add(getKeyRingAsArmoredString(context, data)); + } catch (IOException e) { + Log.e(Constants.TAG, "IOException", e); + } + } while (cursor.moveToNext()); + } + } + + if (cursor != null) { + cursor.close(); + } + + if (output.size() > 0) { + return output; + } else { + return null; + } } public static ArrayList<String> getRegisteredApiApps(Context context) { |