certs: close in from both sides :)

This commit is contained in:
Vincent Breitmoser 2014-03-11 00:11:27 +01:00
parent 535f2caf2c
commit 74f8ec0365
2 changed files with 49 additions and 12 deletions

View File

@ -20,6 +20,8 @@ package org.sufficientlysecure.keychain.provider;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Date; import java.util.Date;
import org.spongycastle.bcpg.ArmoredOutputStream; import org.spongycastle.bcpg.ArmoredOutputStream;
@ -59,25 +61,30 @@ public class ProviderHelper {
/** /**
* Private helper method to get PGPKeyRing from database * Private helper method to get PGPKeyRing from database
*/ */
public static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) { public static Map<Long, PGPKeyRing> getPGPKeyRings(Context context, Uri queryUri) {
Cursor cursor = context.getContentResolver().query(queryUri, Cursor cursor = context.getContentResolver().query(queryUri,
new String[]{KeyRings._ID, KeyRings.KEY_RING_DATA}, null, null, null); new String[]{KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.KEY_RING_DATA}, null, null, null);
PGPKeyRing keyRing = null; Map<Long, PGPKeyRing> result = new HashMap<Long, PGPKeyRing>(cursor.getCount());
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) do {
int keyRingDataCol = cursor.getColumnIndex(KeyRings.KEY_RING_DATA); int keyRingDataCol = cursor.getColumnIndex(KeyRings.KEY_RING_DATA);
int masterKeyIdCol = cursor.getColumnIndex(KeyRings.MASTER_KEY_ID);
byte[] data = cursor.getBlob(keyRingDataCol); byte[] data = cursor.getBlob(keyRingDataCol);
if (data != null) { if (data != null) {
keyRing = PgpConversionHelper.BytesToPGPKeyRing(data); result.put(cursor.getLong(masterKeyIdCol), PgpConversionHelper.BytesToPGPKeyRing(data));
} }
}
} while(cursor.moveToNext());
if (cursor != null) { if (cursor != null) {
cursor.close(); cursor.close();
} }
return keyRing; return result;
}
public static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) {
return getPGPKeyRings(context, queryUri).values().iterator().next();
} }
/** /**
@ -321,6 +328,30 @@ public class ProviderHelper {
return ContentProviderOperation.newInsert(uri).withValues(values).build(); return ContentProviderOperation.newInsert(uri).withValues(values).build();
} }
/**
* Build ContentProviderOperation to add PGPPublicKey to database corresponding to a keyRing
*/
private static ContentProviderOperation buildPublicCertOperations(Context context,
long keyRingRowId,
int rank,
long keyId,
PGPSignature cert,
boolean verified)
throws IOException {
ContentValues values = new ContentValues();
values.put(Certs.KEY_RING_ROW_ID, keyRingRowId);
values.put(Certs.RANK, rank);
values.put(Certs.KEY_ID, keyId);
values.put(Certs.KEY_ID_CERTIFIER, cert.getKeyID());
values.put(Certs.CREATION, cert.getCreationTime().getTime() / 1000);
values.put(Certs.VERIFIED, verified);
values.put(Certs.KEY_DATA, cert.getEncoded());
Uri uri = Certs.buildCertsUri(Long.toString(keyRingRowId));
return ContentProviderOperation.newInsert(uri).withValues(values).build();
}
/** /**
* Build ContentProviderOperation to add PublicUserIds to database corresponding to a keyRing * Build ContentProviderOperation to add PublicUserIds to database corresponding to a keyRing
*/ */

View File

@ -280,7 +280,9 @@ public class KeyListFragment extends Fragment implements AdapterView.OnItemClick
} else { } else {
viewIntent = new Intent(getActivity(), ViewKeyActivityJB.class); viewIntent = new Intent(getActivity(), ViewKeyActivityJB.class);
} }
viewIntent.setData(KeychainContract.KeyRings.buildPublicKeyRingsByMasterKeyIdUri(Long.toString(mAdapter.getMasterKeyId(position)))); viewIntent.setData(KeychainContract.KeyRings.buildPublicKeyRingsByMasterKeyIdUri(
Long.toString(mAdapter.getMasterKeyId(position)))
);
startActivity(viewIntent); startActivity(viewIntent);
} }
@ -379,8 +381,8 @@ public class KeyListFragment extends Fragment implements AdapterView.OnItemClick
/** /**
* Bind cursor data to the item list view * Bind cursor data to the item list view
* <p/> * <p/>
* NOTE: CursorAdapter already implements the ViewHolder pattern in its getView() method. Thus * NOTE: CursorAdapter already implements the ViewHolder pattern in its getView() method.
* no ViewHolder is required here. * Thus no ViewHolder is required here.
*/ */
@Override @Override
public void bindView(View view, Context context, Cursor cursor) { public void bindView(View view, Context context, Cursor cursor) {
@ -417,7 +419,11 @@ public class KeyListFragment extends Fragment implements AdapterView.OnItemClick
button.setOnClickListener(new OnClickListener() { button.setOnClickListener(new OnClickListener() {
public void onClick(View view) { public void onClick(View view) {
Intent editIntent = new Intent(getActivity(), EditKeyActivity.class); Intent editIntent = new Intent(getActivity(), EditKeyActivity.class);
editIntent.setData(KeychainContract.KeyRings.buildSecretKeyRingsByMasterKeyIdUri(Long.toString(id))); editIntent.setData(
KeychainContract.KeyRings.buildSecretKeyRingsByMasterKeyIdUri(
Long.toString(id)
)
);
editIntent.setAction(EditKeyActivity.ACTION_EDIT_KEY); editIntent.setAction(EditKeyActivity.ACTION_EDIT_KEY);
startActivityForResult(editIntent, 0); startActivityForResult(editIntent, 0);
} }
@ -504,7 +510,7 @@ public class KeyListFragment extends Fragment implements AdapterView.OnItemClick
String userId = mCursor.getString(KeyListFragment.INDEX_UID); String userId = mCursor.getString(KeyListFragment.INDEX_UID);
String headerText = convertView.getResources().getString(R.string.user_id_no_name); String headerText = convertView.getResources().getString(R.string.user_id_no_name);
if (userId != null && userId.length() > 0) { if (userId != null && userId.length() > 0) {
headerText = "" + mCursor.getString(KeyListFragment.INDEX_UID).subSequence(0, 1).charAt(0); headerText = "" + userId.subSequence(0, 1).charAt(0);
} }
holder.text.setText(headerText); holder.text.setText(headerText);
holder.count.setVisibility(View.GONE); holder.count.setVisibility(View.GONE);