More code cleanup

This commit is contained in:
Dominik Schürmann 2014-04-19 21:56:15 +02:00
parent 0383822585
commit 7017a01bdb
20 changed files with 131 additions and 107 deletions

View File

@ -392,15 +392,29 @@ public class PgpKeyHelper {
return isCertificationKey(key.getPublicKey());
}
public static String getAlgorithmInfo(PGPPublicKey key) {
return getAlgorithmInfo(key.getAlgorithm(), key.getBitStrength());
public static String getAlgorithmInfo(Context context, PGPPublicKey key) {
return getAlgorithmInfo(context, key.getAlgorithm(), key.getBitStrength());
}
public static String getAlgorithmInfo(PGPSecretKey key) {
return getAlgorithmInfo(key.getPublicKey());
public static String getAlgorithmInfo(Context context, PGPSecretKey key) {
return getAlgorithmInfo(context, key.getPublicKey());
}
public static String getAlgorithmInfo(int algorithm, int keySize) {
/**
* TODO: Only used in HkpKeyServer. Get rid of this one!
*/
public static String getAlgorithmInfo(int algorithm) {
return getAlgorithmInfo(null, algorithm, 0);
}
public static String getAlgorithmInfo(Context context, int algorithm) {
return getAlgorithmInfo(context, algorithm, 0);
}
/**
* Based on <a href="http://tools.ietf.org/html/rfc2440#section-9.1">OpenPGP Message Format</a>
*/
public static String getAlgorithmInfo(Context context, int algorithm, int keySize) {
String algorithmStr;
switch (algorithm) {
@ -421,8 +435,19 @@ public class PgpKeyHelper {
break;
}
case PGPPublicKey.ECDSA:
case PGPPublicKey.ECDH: {
algorithmStr = "ECC";
break;
}
default: {
algorithmStr = "Unknown";
if (context != null) {
algorithmStr = context.getResources().getString(R.string.unknown_algorithm);
} else {
// TODO
algorithmStr = "unknown";
}
break;
}
}

View File

@ -71,7 +71,7 @@ public class EncryptActivity extends DrawerActivity implements
private static final int PAGER_CONTENT_MESSAGE = 0;
private static final int PAGER_CONTENT_FILE = 1;
// model useb by message and file fragment
// model used by message and file fragments
private long mEncryptionKeyIds[] = null;
private long mSigningKeyId = Constants.key.none;
private String mPassphrase;

View File

@ -17,7 +17,7 @@
package org.sufficientlysecure.keychain.ui;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
@ -25,6 +25,7 @@ import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
@ -211,7 +212,8 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O
} else {
Log.e(Constants.TAG,
"IMPORT_KEY_FROM_KEYSERVER action needs to contain the 'query', 'key_id', or " +
"'fingerprint' extra!");
"'fingerprint' extra!"
);
return;
}
} else if (ACTION_IMPORT_KEY_FROM_FILE.equals(action)) {
@ -458,9 +460,13 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O
@Override
public void onResume() {
super.onResume();
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
// Check to see if the Activity started due to an Android Beam
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
handleActionNdefDiscovered(getIntent());
} else {
Log.e(Constants.TAG, "Android Beam not supported by Android < 4.1");
}
}
@ -476,7 +482,7 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O
/**
* NFC: Parses the NDEF Message from the intent and prints to the TextView
*/
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
void handleActionNdefDiscovered(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
// only one message sent during the beam

View File

@ -176,7 +176,7 @@ public class ViewCertActivity extends ActionBarActivity
mStatus.setTextColor(getResources().getColor(R.color.black));
}
String algorithmStr = PgpKeyHelper.getAlgorithmInfo(sig.getKeyAlgorithm(), 0);
String algorithmStr = PgpKeyHelper.getAlgorithmInfo(this, sig.getKeyAlgorithm(), 0);
mAlgorithm.setText(algorithmStr);
mRowReason.setVisibility(View.GONE);

View File

@ -50,21 +50,23 @@ public class ViewKeyCertsFragment extends Fragment
implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {
// These are the rows that we will retrieve.
static final String[] PROJECTION = new String[] {
Certs._ID,
Certs.MASTER_KEY_ID,
Certs.VERIFIED,
Certs.TYPE,
Certs.RANK,
Certs.KEY_ID_CERTIFIER,
Certs.USER_ID,
Certs.SIGNER_UID
static final String[] PROJECTION = new String[]{
Certs._ID,
Certs.MASTER_KEY_ID,
Certs.VERIFIED,
Certs.TYPE,
Certs.RANK,
Certs.KEY_ID_CERTIFIER,
Certs.USER_ID,
Certs.SIGNER_UID
};
// sort by our user id,
static final String SORT_ORDER =
Tables.CERTS + "." + Certs.RANK + " ASC, "
+ Certs.VERIFIED + " DESC, " + Certs.TYPE + " DESC, " + Certs.SIGNER_UID + " ASC";
Tables.CERTS + "." + Certs.RANK + " ASC, "
+ Certs.VERIFIED + " DESC, "
+ Certs.TYPE + " DESC, "
+ Certs.SIGNER_UID + " ASC";
public static final String ARG_DATA_URI = "data_uri";
@ -106,10 +108,6 @@ public class ViewKeyCertsFragment extends Fragment
mStickyList.setEmptyView(getActivity().findViewById(R.id.empty));
// TODO this view is made visible if no data is available
// mStickyList.setEmptyView(getActivity().findViewById(R.id.empty));
// Create an empty adapter we will use to display the loaded data.
mAdapter = new CertListAdapter(getActivity(), null);
mStickyList.setAdapter(mAdapter);
@ -138,7 +136,7 @@ public class ViewKeyCertsFragment extends Fragment
*/
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if(view.getTag(R.id.tag_mki) != null) {
if (view.getTag(R.id.tag_mki) != null) {
long masterKeyId = (Long) view.getTag(R.id.tag_mki);
long rank = (Long) view.getTag(R.id.tag_rank);
long certifierId = (Long) view.getTag(R.id.tag_certifierId);
@ -215,17 +213,22 @@ public class ViewKeyCertsFragment extends Fragment
String signerKeyId = PgpKeyHelper.convertKeyIdToHex(cursor.getLong(mIndexSignerKeyId));
String signerUserId = cursor.getString(mIndexSignerUserId);
switch(cursor.getInt(mIndexType)) {
switch (cursor.getInt(mIndexType)) {
case PGPSignature.DEFAULT_CERTIFICATION: // 0x10
wSignStatus.setText(R.string.cert_default); break;
wSignStatus.setText(R.string.cert_default);
break;
case PGPSignature.NO_CERTIFICATION: // 0x11
wSignStatus.setText(R.string.cert_none); break;
wSignStatus.setText(R.string.cert_none);
break;
case PGPSignature.CASUAL_CERTIFICATION: // 0x12
wSignStatus.setText(R.string.cert_casual); break;
wSignStatus.setText(R.string.cert_casual);
break;
case PGPSignature.POSITIVE_CERTIFICATION: // 0x13
wSignStatus.setText(R.string.cert_positive); break;
wSignStatus.setText(R.string.cert_positive);
break;
case PGPSignature.CERTIFICATION_REVOCATION: // 0x30
wSignStatus.setText(R.string.cert_revoke); break;
wSignStatus.setText(R.string.cert_revoke);
break;
}
wSignerUserId.setText(signerUserId);

View File

@ -278,7 +278,10 @@ public class ViewKeyMainFragment extends Fragment implements
}
String algorithmStr = PgpKeyHelper.getAlgorithmInfo(
data.getInt(INDEX_UNIFIED_ALGORITHM), data.getInt(INDEX_UNIFIED_KEY_SIZE));
getActivity(),
data.getInt(INDEX_UNIFIED_ALGORITHM),
data.getInt(INDEX_UNIFIED_KEY_SIZE)
);
mAlgorithm.setText(algorithmStr);
byte[] fingerprintBlob = data.getBlob(INDEX_UNIFIED_FINGERPRINT);

View File

@ -17,7 +17,7 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
@ -61,14 +61,14 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void setData(List<ImportKeysListEntry> data) {
clear();
if (data != null) {
this.mData = data;
// add data to extended ArrayAdapter
if (Build.VERSION.SDK_INT >= 11) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
addAll(data);
} else {
for (ImportKeysListEntry entry : data) {

View File

@ -17,6 +17,7 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;
@ -213,7 +214,7 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
* Constructor based on key object, used for import from NFC, QR Codes, files
*/
@SuppressWarnings("unchecked")
public ImportKeysListEntry(PGPKeyRing pgpKeyRing) {
public ImportKeysListEntry(Context context, PGPKeyRing pgpKeyRing) {
// save actual key object into entry, used to import it later
try {
this.mBytes = pgpKeyRing.getEncoded();
@ -263,32 +264,6 @@ public class ImportKeysListEntry implements Serializable, Parcelable {
this.fingerPrintHex = PgpKeyHelper.convertFingerprintToHex(key.getFingerprint());
this.bitStrength = key.getBitStrength();
final int algorithm = key.getAlgorithm();
this.algorithm = getAlgorithmFromId(algorithm);
}
/**
* Based on <a href="http://tools.ietf.org/html/rfc2440#section-9.1">OpenPGP Message Format</a>
*/
private static final SparseArray<String> ALGORITHM_IDS = new SparseArray<String>() {{
put(-1, "unknown"); // TODO: with resources
put(0, "unencrypted");
put(PGPPublicKey.RSA_GENERAL, "RSA");
put(PGPPublicKey.RSA_ENCRYPT, "RSA");
put(PGPPublicKey.RSA_SIGN, "RSA");
put(PGPPublicKey.ELGAMAL_ENCRYPT, "ElGamal");
put(PGPPublicKey.ELGAMAL_GENERAL, "ElGamal");
put(PGPPublicKey.DSA, "DSA");
put(PGPPublicKey.EC, "ECC");
put(PGPPublicKey.ECDSA, "ECC");
put(PGPPublicKey.ECDH, "ECC");
}};
/**
* Based on <a href="http://tools.ietf.org/html/rfc2440#section-9.1">OpenPGP Message Format</a>
*/
public static String getAlgorithmFromId(int algorithmId) {
return (ALGORITHM_IDS.get(algorithmId) != null ?
ALGORITHM_IDS.get(algorithmId) :
ALGORITHM_IDS.get(-1));
this.algorithm = PgpKeyHelper.getAlgorithmInfo(context, algorithm);
}
}

View File

@ -165,7 +165,7 @@ public class ImportKeysListLoader
}
private void addToData(PGPKeyRing keyring) {
ImportKeysListEntry item = new ImportKeysListEntry(keyring);
ImportKeysListEntry item = new ImportKeysListEntry(getContext(), keyring);
mData.add(item);
}

View File

@ -101,7 +101,7 @@ public class ImportKeysListServerLoader
String fingerprint = query.substring(2);
Log.d(Constants.TAG, "fingerprint: " + fingerprint);
// query must return only one result!
if (searchResult.size() > 0) {
if (searchResult.size() == 1) {
ImportKeysListEntry uniqueEntry = searchResult.get(0);
/*
* set fingerprint explicitly after query

View File

@ -39,7 +39,8 @@ public class KeyValueSpinnerAdapter extends ArrayAdapter<String> {
public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
return e1.getValue().compareTo(e2.getValue());
}
});
}
);
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}

View File

@ -65,8 +65,8 @@ abstract public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter
if (cursor != null) {
mIndexUserId = cursor.getColumnIndexOrThrow(KeyRings.USER_ID);
mIndexMasterKeyId = cursor.getColumnIndexOrThrow(KeyRings.MASTER_KEY_ID);
mIndexExpiry= cursor.getColumnIndexOrThrow(KeyRings.EXPIRY);
mIndexRevoked= cursor.getColumnIndexOrThrow(KeyRings.IS_REVOKED);
mIndexExpiry = cursor.getColumnIndexOrThrow(KeyRings.EXPIRY);
mIndexRevoked = cursor.getColumnIndexOrThrow(KeyRings.IS_REVOKED);
}
}
@ -122,7 +122,7 @@ abstract public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter
h.keyId.setText(PgpKeyHelper.convertKeyIdToHex(masterKeyId));
boolean enabled = true;
if(cursor.getInt(mIndexRevoked) != 0) {
if (cursor.getInt(mIndexRevoked) != 0) {
h.status.setText(R.string.revoked);
enabled = false;
} else if (!cursor.isNull(mIndexExpiry)
@ -134,7 +134,6 @@ abstract public class SelectKeyCursorAdapter extends HighlightQueryCursorAdapter
}
h.status.setTag(enabled);
}
@Override

View File

@ -73,7 +73,7 @@ public class ViewKeyKeysAdapter extends CursorAdapter {
hasAnySecret = true;
break;
}
} while(newCursor.moveToNext());
} while (newCursor.moveToNext());
}
return super.swapCursor(newCursor);
@ -112,8 +112,11 @@ public class ViewKeyKeysAdapter extends CursorAdapter {
ImageView revokedKeyIcon = (ImageView) view.findViewById(R.id.ic_revokedKey);
String keyIdStr = PgpKeyHelper.convertKeyIdToHex(cursor.getLong(mIndexKeyId));
String algorithmStr = PgpKeyHelper.getAlgorithmInfo(cursor.getInt(mIndexAlgorithm),
cursor.getInt(mIndexKeySize));
String algorithmStr = PgpKeyHelper.getAlgorithmInfo(
context,
cursor.getInt(mIndexAlgorithm),
cursor.getInt(mIndexKeySize)
);
keyId.setText(keyIdStr);
// may be set with additional "stripped" later on
@ -173,6 +176,7 @@ public class ViewKeyKeysAdapter extends CursorAdapter {
} else {
keyExpiry.setVisibility(View.GONE);
}
// if key is expired or revoked, strike through text
if (!valid) {
keyId.setText(OtherHelper.strikeOutText(keyId.getText()));

View File

@ -46,8 +46,12 @@ public class ViewKeyUserIdsAdapter extends CursorAdapter implements AdapterView.
private final ArrayList<Boolean> mCheckStates;
public static final String[] USER_IDS_PROJECTION = new String[]{
UserIds._ID, UserIds.USER_ID, UserIds.RANK,
UserIds.VERIFIED, UserIds.IS_PRIMARY, UserIds.IS_REVOKED
UserIds._ID,
UserIds.USER_ID,
UserIds.RANK,
UserIds.VERIFIED,
UserIds.IS_PRIMARY,
UserIds.IS_REVOKED
};
public ViewKeyUserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes) {
@ -102,7 +106,6 @@ public class ViewKeyUserIdsAdapter extends CursorAdapter implements AdapterView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView vRank = (TextView) view.findViewById(R.id.rank);
TextView vUserId = (TextView) view.findViewById(R.id.userId);
TextView vAddress = (TextView) view.findViewById(R.id.address);
@ -137,12 +140,16 @@ public class ViewKeyUserIdsAdapter extends CursorAdapter implements AdapterView.
int verified = cursor.getInt(mVerifiedId);
// TODO introduce own resources for this :)
if (verified == Certs.VERIFIED_SECRET) {
vVerified.setImageResource(R.drawable.key_certify_ok_depth0);
} else if (verified == Certs.VERIFIED_SELF) {
vVerified.setImageResource(R.drawable.key_certify_ok_self);
} else {
vVerified.setImageResource(R.drawable.key_certify_error);
switch (verified) {
case Certs.VERIFIED_SECRET:
vVerified.setImageResource(R.drawable.key_certify_ok_depth0);
break;
case Certs.VERIFIED_SELF:
vVerified.setImageResource(R.drawable.key_certify_ok_self);
break;
default:
vVerified.setImageResource(R.drawable.key_certify_error);
break;
}
}

View File

@ -68,7 +68,6 @@ public class DeleteKeyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final FragmentActivity activity = getActivity();
mMessenger = getArguments().getParcelable(ARG_MESSENGER);

View File

@ -88,13 +88,11 @@ public class ShareNfcDialogFragment extends DialogFragment {
Settings.ACTION_NFCSHARING_SETTINGS);
startActivity(intentSettings);
}
});
}
);
}
}
// no flickering when clicking textview for Android < 4
// aboutTextView.setTextColor(getResources().getColor(android.R.color.black));
return alert.create();
}
}

View File

@ -92,7 +92,7 @@ public class ShareQrCodeDialogFragment extends DialogFragment {
mText = (TextView) view.findViewById(R.id.share_qr_code_dialog_text);
ProviderHelper providerHelper = new ProviderHelper(getActivity());
String content = null;
String content;
try {
if (mFingerprintOnly) {
alert.setPositiveButton(R.string.btn_okay, null);

View File

@ -216,7 +216,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener {
mDeleteButton.setVisibility(View.INVISIBLE);
}
mAlgorithm.setText(PgpKeyHelper.getAlgorithmInfo(key));
mAlgorithm.setText(PgpKeyHelper.getAlgorithmInfo(getContext(), key));
String keyIdStr = PgpKeyHelper.convertKeyIdToHex(key.getKeyID());
mKeyId.setText(keyIdStr);

View File

@ -31,6 +31,7 @@ import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListEntry;
import java.io.ByteArrayOutputStream;
@ -256,7 +257,7 @@ public class HkpKeyServer extends KeyServer {
entry.setBitStrength(Integer.parseInt(matcher.group(3)));
final int algorithmId = Integer.decode(matcher.group(2));
entry.setAlgorithm(ImportKeysListEntry.getAlgorithmFromId(algorithmId));
entry.setAlgorithm(PgpKeyHelper.getAlgorithmInfo(algorithmId));
// group 1 contains the full fingerprint (v4) or the long key id if available
// see http://bit.ly/1d4bxbk and http://bit.ly/1gD1wwr

View File

@ -140,6 +140,8 @@
<string name="no_key">&lt;no key&gt;</string>
<string name="can_encrypt">can encrypt</string>
<string name="can_sign">can sign</string>
<string name="can_certify">can certify</string>
<string name="can_certify_not">cannot certify</string>
<string name="expired">expired</string>
<string name="revoked">revoked</string>
@ -368,6 +370,7 @@
<!-- Help -->
<string name="help_tab_start">Start</string>
<string name="help_tab_faq">FAQ</string>
<string name="help_tab_wot">Web of Trust</string>
<string name="help_tab_nfc_beam">NFC Beam</string>
<string name="help_tab_changelog">Changelog</string>
<string name="help_tab_about">About</string>
@ -468,6 +471,17 @@
<string name="encrypt_content_edit_text_hint">Write message here to encrypt and/or sign…</string>
<string name="decrypt_content_edit_text_hint">Enter ciphertext here to decrypt and/or verify…</string>
<!-- certifications -->
<string name="cert_default">default</string>
<string name="cert_none">none</string>
<string name="cert_casual">casual</string>
<string name="cert_positive">positive</string>
<string name="cert_revoke">revoke</string>
<string name="cert_verify_ok">ok</string>
<string name="cert_verify_failed">failed!</string>
<string name="cert_verify_error">error!</string>
<string name="cert_verify_unavailable">key unavailable</string>
<!-- unsorted -->
<string name="section_signer_id">Signer</string>
<string name="section_cert">Certificate Details</string>
@ -475,26 +489,15 @@
<string name="unknown_uid">&lt;unknown&gt;</string>
<string name="empty_certs">No certificates for this key</string>
<string name="section_uids_to_sign">User IDs to sign</string>
<string name="cert_default">default</string>
<string name="cert_none">none</string>
<string name="cert_casual">casual</string>
<string name="cert_positive">positive</string>
<string name="cert_revoke">revoke</string>
<string name="help_tab_wot">Web of Trust</string>
<string name="cert_verify_ok">ok</string>
<string name="cert_verify_failed">failed!</string>
<string name="cert_verify_error">error!</string>
<string name="cert_verify_unavailable">key unavailable</string>
<string name="label_revocation">Revocation Reason</string>
<string name="label_verify_status">Verification Status</string>
<string name="label_cert_type">Type</string>
<string name="can_certify">can certify</string>
<string name="can_certify_not">cannot certify</string>
<string name="error_key_not_found">Key not found!</string>
<string name="error_key_processing">Error processing key!</string>
<string name="no_subkey">subkey unavailable</string>
<string name="key_stripped">stripped</string>
<string name="secret_cannot_multiple">Secret keys can only be deleted individually!</string>
<string name="title_view_cert">View Certificate Details</string>
<string name="unknown_algorithm">unknown</string>
</resources>