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 static Object getGenericData(Context context, Uri uri, String column) {
return getGenericData(context, uri, new String[] { column }).get(column);
// If we ever switch to api level 11, we can ditch this whole mess!
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);
HashMap<String, Object> result = new HashMap<String, Object>(proj.length);
if (cursor != null && cursor.moveToFirst()) {
int pos = 0;
for(String p : proj) {
switch(cursor.getType(pos)) {
case Cursor.FIELD_TYPE_NULL: result.put(p, cursor.isNull(pos)); break;
case Cursor.FIELD_TYPE_INTEGER: result.put(p, cursor.getLong(pos)); break;
case Cursor.FIELD_TYPE_FLOAT: result.put(p, cursor.getFloat(pos)); break;
case Cursor.FIELD_TYPE_STRING: result.put(p, cursor.getString(pos)); break;
case Cursor.FIELD_TYPE_BLOB: result.put(p, cursor.getBlob(pos)); break;
switch(types[pos]) {
case FIELD_TYPE_NULL: result.put(p, cursor.isNull(pos)); break;
case FIELD_TYPE_INTEGER: result.put(p, cursor.getLong(pos)); break;
case FIELD_TYPE_FLOAT: result.put(p, cursor.getFloat(pos)); break;
case FIELD_TYPE_STRING: result.put(p, cursor.getString(pos)); break;
case FIELD_TYPE_BLOB: result.put(p, cursor.getBlob(pos)); break;
}
pos += 1;
}
@ -89,11 +97,11 @@ public class ProviderHelper {
return result;
}
public static Object getUnifiedData(Context context, long masterKeyId, String column) {
return getUnifiedData(context, masterKeyId, new String[] { column }).get(column);
public static Object getUnifiedData(Context context, long masterKeyId, String column, int type) {
return getUnifiedData(context, masterKeyId, new String[] { column }, new int[] { type }).get(column);
}
public static HashMap<String,Object> getUnifiedData(Context context, long masterKeyId, String[] proj) {
return getGenericData(context, KeyRings.buildUnifiedKeyRingUri(Long.toString(masterKeyId)), proj);
public static HashMap<String,Object> getUnifiedData(Context context, long masterKeyId, String[] proj, int[] types) {
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
@ -108,8 +116,8 @@ public class ProviderHelper {
// didn't work? oh well.
Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying...");
}
Object data = getGenericData(context, queryUri, KeyRings.MASTER_KEY_ID);
if(data instanceof Long)
Object data = getGenericData(context, queryUri, KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER);
if(data != null)
return (Long) data;
// TODO better error handling?
return 0L;

View File

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

View File

@ -158,8 +158,10 @@ public class ViewKeyActivity extends ActionBarActivity {
}
private void updateFromKeyserver(Uri dataUri) {
byte[] fingerprintBlob = ProviderHelper.getFingerprint(this, dataUri);
String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob);
byte[] blob = (byte[]) ProviderHelper.getGenericData(
this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
KeychainContract.Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
String fingerprint = PgpKeyHelper.convertFingerprintToHex(blob);
Intent queryIntent = new Intent(this, ImportKeysActivity.class);
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) {
String content;
if (fingerprintOnly) {
Object blob = ProviderHelper.getGenericData(
byte[] data = (byte[]) ProviderHelper.getGenericData(
this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
KeychainContract.Keys.FINGERPRINT);
if(blob instanceof byte[]) {
String fingerprint = PgpKeyHelper.convertFingerprintToHex((byte[]) blob);
KeychainContract.Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
if(data != null) {
String fingerprint = PgpKeyHelper.convertFingerprintToHex(data);
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
} else {
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[]{
KeyRings.USER_ID,
KeyRings.HAS_SECRET
});
}, new int[] { ProviderHelper.FIELD_TYPE_STRING, ProviderHelper.FIELD_TYPE_INTEGER });
String userId = (String) data.get(KeyRings.USER_ID);
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.R;
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.util.QrCodeUtils;
@ -90,15 +90,15 @@ public class ShareQrCodeDialogFragment extends DialogFragment {
if (mFingerprintOnly) {
alert.setPositiveButton(R.string.btn_okay, null);
Object blob = ProviderHelper.getGenericData(
getActivity(), KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
KeychainContract.Keys.FINGERPRINT);
if(!(blob instanceof byte[])) {
byte[] blob = (byte[]) ProviderHelper.getGenericData(
getActivity(), KeyRings.buildUnifiedKeyRingUri(dataUri),
KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
if(blob == null) {
// TODO error handling?!
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);
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
setQrCode(content);