db-overhaul: workaround api level 11

This commit is contained in:
Vincent Breitmoser 2014-04-04 11:39:34 +02:00
parent 1b38a5b2e3
commit 085ce11d9c
5 changed files with 42 additions and 32 deletions

View File

@ -61,22 +61,30 @@ import java.util.Set;
public class ProviderHelper { public class ProviderHelper {
public static Object getGenericData(Context context, Uri uri, String column) { // If we ever switch to api level 11, we can ditch this whole mess!
return getGenericData(context, uri, new String[] { column }).get(column); public static final int FIELD_TYPE_NULL = 1;
// this is called integer to stay coherent with the constants in Cursor (api level 11)
public static final int FIELD_TYPE_INTEGER = 2;
public static final int FIELD_TYPE_FLOAT = 3;
public static final int FIELD_TYPE_STRING = 4;
public static final int FIELD_TYPE_BLOB = 5;
public static Object getGenericData(Context context, Uri uri, String column, int type) {
return getGenericData(context, uri, new String[] { column }, new int[] { type }).get(column);
} }
public static HashMap<String,Object> getGenericData(Context context, Uri uri, String[] proj) { public static HashMap<String,Object> getGenericData(Context context, Uri uri, String[] proj, int[] types) {
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null); Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
HashMap<String, Object> result = new HashMap<String, Object>(proj.length); HashMap<String, Object> result = new HashMap<String, Object>(proj.length);
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
int pos = 0; int pos = 0;
for(String p : proj) { for(String p : proj) {
switch(cursor.getType(pos)) { switch(types[pos]) {
case Cursor.FIELD_TYPE_NULL: result.put(p, cursor.isNull(pos)); break; case FIELD_TYPE_NULL: result.put(p, cursor.isNull(pos)); break;
case Cursor.FIELD_TYPE_INTEGER: result.put(p, cursor.getLong(pos)); break; case FIELD_TYPE_INTEGER: result.put(p, cursor.getLong(pos)); break;
case Cursor.FIELD_TYPE_FLOAT: result.put(p, cursor.getFloat(pos)); break; case FIELD_TYPE_FLOAT: result.put(p, cursor.getFloat(pos)); break;
case Cursor.FIELD_TYPE_STRING: result.put(p, cursor.getString(pos)); break; case FIELD_TYPE_STRING: result.put(p, cursor.getString(pos)); break;
case Cursor.FIELD_TYPE_BLOB: result.put(p, cursor.getBlob(pos)); break; case FIELD_TYPE_BLOB: result.put(p, cursor.getBlob(pos)); break;
} }
pos += 1; pos += 1;
} }
@ -89,11 +97,11 @@ public class ProviderHelper {
return result; return result;
} }
public static Object getUnifiedData(Context context, long masterKeyId, String column) { public static Object getUnifiedData(Context context, long masterKeyId, String column, int type) {
return getUnifiedData(context, masterKeyId, new String[] { column }).get(column); return getUnifiedData(context, masterKeyId, new String[] { column }, new int[] { type }).get(column);
} }
public static HashMap<String,Object> getUnifiedData(Context context, long masterKeyId, String[] proj) { public static HashMap<String,Object> getUnifiedData(Context context, long masterKeyId, String[] proj, int[] types) {
return getGenericData(context, KeyRings.buildUnifiedKeyRingUri(Long.toString(masterKeyId)), proj); return getGenericData(context, KeyRings.buildUnifiedKeyRingUri(Long.toString(masterKeyId)), proj, types);
} }
/** Find the master key id related to a given query. The id will either be extracted from the /** Find the master key id related to a given query. The id will either be extracted from the
@ -108,8 +116,8 @@ public class ProviderHelper {
// didn't work? oh well. // didn't work? oh well.
Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying..."); Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying...");
} }
Object data = getGenericData(context, queryUri, KeyRings.MASTER_KEY_ID); Object data = getGenericData(context, queryUri, KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER);
if(data instanceof Long) if(data != null)
return (Long) data; return (Long) data;
// TODO better error handling? // TODO better error handling?
return 0L; return 0L;

View File

@ -193,10 +193,10 @@ public class EncryptAsymmetricFragment extends Fragment {
String uid = getResources().getString(R.string.user_id_no_name); String uid = getResources().getString(R.string.user_id_no_name);
String uidExtra = ""; String uidExtra = "";
// See if we can get a user_id from a unified query // See if we can get a user_id from a unified query
Object data = ProviderHelper.getUnifiedData( String user_id = (String) ProviderHelper.getUnifiedData(
getActivity(), mSecretKeyId, KeyRings.USER_ID); getActivity(), mSecretKeyId, KeyRings.USER_ID, ProviderHelper.FIELD_TYPE_STRING);
if(data instanceof String) { if(user_id != null) {
String chunks[] = ((String) data).split(" <", 2); String chunks[] = user_id.split(" <", 2);
uid = chunks[0]; uid = chunks[0];
if (chunks.length > 1) { if (chunks.length > 1) {
uidExtra = "<" + chunks[1]; uidExtra = "<" + chunks[1];

View File

@ -158,8 +158,10 @@ public class ViewKeyActivity extends ActionBarActivity {
} }
private void updateFromKeyserver(Uri dataUri) { private void updateFromKeyserver(Uri dataUri) {
byte[] fingerprintBlob = ProviderHelper.getFingerprint(this, dataUri); byte[] blob = (byte[]) ProviderHelper.getGenericData(
String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob); this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
KeychainContract.Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
String fingerprint = PgpKeyHelper.convertFingerprintToHex(blob);
Intent queryIntent = new Intent(this, ImportKeysActivity.class); Intent queryIntent = new Intent(this, ImportKeysActivity.class);
queryIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN); queryIntent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN);
@ -171,11 +173,11 @@ public class ViewKeyActivity extends ActionBarActivity {
private void shareKey(Uri dataUri, boolean fingerprintOnly) { private void shareKey(Uri dataUri, boolean fingerprintOnly) {
String content; String content;
if (fingerprintOnly) { if (fingerprintOnly) {
Object blob = ProviderHelper.getGenericData( byte[] data = (byte[]) ProviderHelper.getGenericData(
this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri), this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
KeychainContract.Keys.FINGERPRINT); KeychainContract.Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
if(blob instanceof byte[]) { if(data != null) {
String fingerprint = PgpKeyHelper.convertFingerprintToHex((byte[]) blob); String fingerprint = PgpKeyHelper.convertFingerprintToHex(data);
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
} else { } else {
Toast.makeText(getApplicationContext(), "Bad key selected!", Toast.makeText(getApplicationContext(), "Bad key selected!",

View File

@ -106,7 +106,7 @@ public class DeleteKeyDialogFragment extends DialogFragment {
HashMap<String, Object> data = ProviderHelper.getUnifiedData(activity, masterKeyId, new String[]{ HashMap<String, Object> data = ProviderHelper.getUnifiedData(activity, masterKeyId, new String[]{
KeyRings.USER_ID, KeyRings.USER_ID,
KeyRings.HAS_SECRET KeyRings.HAS_SECRET
}); }, new int[] { ProviderHelper.FIELD_TYPE_STRING, ProviderHelper.FIELD_TYPE_INTEGER });
String userId = (String) data.get(KeyRings.USER_ID); String userId = (String) data.get(KeyRings.USER_ID);
boolean hasSecret = ((Long) data.get(KeyRings.HAS_SECRET)) == 1; boolean hasSecret = ((Long) data.get(KeyRings.HAS_SECRET)) == 1;

View File

@ -31,7 +31,7 @@ import android.widget.TextView;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.util.QrCodeUtils; import org.sufficientlysecure.keychain.util.QrCodeUtils;
@ -90,15 +90,15 @@ public class ShareQrCodeDialogFragment extends DialogFragment {
if (mFingerprintOnly) { if (mFingerprintOnly) {
alert.setPositiveButton(R.string.btn_okay, null); alert.setPositiveButton(R.string.btn_okay, null);
Object blob = ProviderHelper.getGenericData( byte[] blob = (byte[]) ProviderHelper.getGenericData(
getActivity(), KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri), getActivity(), KeyRings.buildUnifiedKeyRingUri(dataUri),
KeychainContract.Keys.FINGERPRINT); KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
if(!(blob instanceof byte[])) { if(blob == null) {
// TODO error handling?! // TODO error handling?!
return null; return null;
} }
String fingerprint = PgpKeyHelper.convertFingerprintToHex((byte[]) blob); String fingerprint = PgpKeyHelper.convertFingerprintToHex(blob);
mText.setText(getString(R.string.share_qr_code_dialog_fingerprint_text) + " " + fingerprint); mText.setText(getString(R.string.share_qr_code_dialog_fingerprint_text) + " " + fingerprint);
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
setQrCode(content); setQrCode(content);