From be49597882a8170ed6244c0f20c375cd525a883d Mon Sep 17 00:00:00 2001 From: Dominik Date: Tue, 11 Sep 2012 15:27:32 +0200 Subject: [PATCH] fixes, refactoring --- org_apg/res/layout/passphrase.xml | 54 +++++----------- org_apg/res/layout/passphrase_repeat.xml | 61 +++++++++++++++++++ org_apg/res/values/strings.xml | 2 +- .../android/apg/ApgApplication.java | 3 +- .../android/apg/deprecated/BaseActivity.java | 18 +++--- .../apg/deprecated/PausableThread.java | 49 --------------- .../android/apg/helper/OtherHelper.java | 23 +++++++ .../apg/helper/PGPConversionHelper.java | 12 ++-- .../android/apg/helper/PGPHelper.java | 12 ++-- .../android/apg/helper/PGPMain.java | 60 ++++++++++++++++-- .../android/apg/service/ApgService.java | 52 +++------------- .../android/apg/ui/DecryptActivity.java | 14 +---- .../android/apg/ui/EditKeyActivity.java | 12 +--- .../android/apg/ui/EncryptActivity.java | 15 +---- .../android/apg/ui/KeyListActivity.java | 20 ++++-- .../android/apg/ui/MainActivity.java | 12 ++-- .../android/apg/ui/SecretKeyListActivity.java | 2 +- .../ui/dialog/DeleteKeyDialogFragment.java | 2 +- .../ui/dialog/PassphraseDialogFragment.java | 22 +++---- .../dialog/SetPassphraseDialogFragment.java | 6 +- 20 files changed, 231 insertions(+), 220 deletions(-) create mode 100644 org_apg/res/layout/passphrase_repeat.xml delete mode 100644 org_apg/src/org/thialfihar/android/apg/deprecated/PausableThread.java diff --git a/org_apg/res/layout/passphrase.xml b/org_apg/res/layout/passphrase.xml index 73a085588..d4a498532 100644 --- a/org_apg/res/layout/passphrase.xml +++ b/org_apg/res/layout/passphrase.xml @@ -15,47 +15,25 @@ limitations under the License. --> - + android:paddingRight="16dp" > - + - + - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/org_apg/res/layout/passphrase_repeat.xml b/org_apg/res/layout/passphrase_repeat.xml new file mode 100644 index 000000000..73a085588 --- /dev/null +++ b/org_apg/res/layout/passphrase_repeat.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/org_apg/res/values/strings.xml b/org_apg/res/values/strings.xml index 46cb781f7..5783386e9 100644 --- a/org_apg/res/values/strings.xml +++ b/org_apg/res/values/strings.xml @@ -193,7 +193,7 @@ The passphrases didn\'t match. Empty passphrases are not allowed. Symmetric encryption. - %s + Enter passphrase for \'%s\' Are you sure you want to delete\n%s? Successfully deleted. Select a file first. diff --git a/org_apg/src/org/thialfihar/android/apg/ApgApplication.java b/org_apg/src/org/thialfihar/android/apg/ApgApplication.java index 23e685b9c..de3c1e745 100644 --- a/org_apg/src/org/thialfihar/android/apg/ApgApplication.java +++ b/org_apg/src/org/thialfihar/android/apg/ApgApplication.java @@ -29,6 +29,7 @@ import android.os.Environment; public class ApgApplication extends Application { static { + // Define Java Security Provider to be Bouncy Castle Security.addProvider(new BouncyCastleProvider()); } @@ -42,7 +43,7 @@ public class ApgApplication extends Application { // TODO: Do it better than this! // this initializes the database to be used in PGPMain PGPMain.initialize(this); - + // Create APG directory on sdcard if not existing if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File dir = new File(Constants.path.APP_DIR); diff --git a/org_apg/src/org/thialfihar/android/apg/deprecated/BaseActivity.java b/org_apg/src/org/thialfihar/android/apg/deprecated/BaseActivity.java index c2d6f96fa..2d1cfcf6c 100644 --- a/org_apg/src/org/thialfihar/android/apg/deprecated/BaseActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/deprecated/BaseActivity.java @@ -45,7 +45,7 @@ public class BaseActivity extends SherlockFragmentActivity implements Runnable, ProgressDialogUpdater, AskForPassphrase.PassPhraseCallbackInterface { private ProgressDialog mProgressDialog = null; - private PausableThread mRunningThread = null; + // private PausableThread mRunningThread = null; private Thread mDeletingThread = null; private long mSecretKeyId = 0; @@ -373,14 +373,14 @@ public class BaseActivity extends SherlockFragmentActivity implements Runnable, // mHandler.sendMessage(msg); // } - public PausableThread getRunningThread() { - return mRunningThread; - } - - public void startThread() { - mRunningThread = new PausableThread(this); - mRunningThread.start(); - } + // public PausableThread getRunningThread() { + // return mRunningThread; + // } + // + // public void startThread() { + // mRunningThread = new PausableThread(this); + // mRunningThread.start(); + // } public void run() { diff --git a/org_apg/src/org/thialfihar/android/apg/deprecated/PausableThread.java b/org_apg/src/org/thialfihar/android/apg/deprecated/PausableThread.java deleted file mode 100644 index a4541c2ae..000000000 --- a/org_apg/src/org/thialfihar/android/apg/deprecated/PausableThread.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.thialfihar.android.apg.deprecated; - -public class PausableThread extends Thread { - private boolean mPaused = false; - - public PausableThread(Runnable runnable) { - super(runnable); - } - - public void pause() { - synchronized (this) { - mPaused = true; - while (mPaused) { - try { - wait(); - } catch (InterruptedException e) { - // ignore - } - } - } - } - - public void unpause() { - synchronized (this) { - mPaused = false; - notify(); - } - } - - public boolean isPaused() { - synchronized (this) { - return mPaused; - } - } -} diff --git a/org_apg/src/org/thialfihar/android/apg/helper/OtherHelper.java b/org_apg/src/org/thialfihar/android/apg/helper/OtherHelper.java index 516d7fccb..0d2f060dd 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/OtherHelper.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/OtherHelper.java @@ -27,6 +27,9 @@ import java.util.Set; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.util.Log; +import com.actionbarsherlock.app.ActionBar; +import com.actionbarsherlock.app.SherlockFragmentActivity; + import android.content.Context; import android.os.Bundle; @@ -108,4 +111,24 @@ public class OtherHelper { } } } + + /** + * Set actionbar without home button if called from another app + * + * @param activity + */ + public static void setActionBarBackButton(SherlockFragmentActivity activity) { + // set actionbar without home button if called from another app + final ActionBar actionBar = activity.getSupportActionBar(); + Log.d(Constants.TAG, "calling package (only set when using startActivityForResult)=" + + activity.getCallingPackage()); + if (activity.getCallingPackage() != null + && activity.getCallingPackage().equals(Constants.PACKAGE_NAME)) { + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setHomeButtonEnabled(true); + } else { + actionBar.setDisplayHomeAsUpEnabled(false); + actionBar.setHomeButtonEnabled(false); + } + } } diff --git a/org_apg/src/org/thialfihar/android/apg/helper/PGPConversionHelper.java b/org_apg/src/org/thialfihar/android/apg/helper/PGPConversionHelper.java index 72294e8ce..c9a7b33a0 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/PGPConversionHelper.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/PGPConversionHelper.java @@ -43,9 +43,7 @@ public class PGPConversionHelper { try { key.encode(os); } catch (IOException e) { - Log.e(Constants.TAG, - "Error while converting PGPSecretKey to byte[]: " + e.getMessage()); - e.printStackTrace(); + Log.e(Constants.TAG, "Error while converting PGPSecretKey to byte[]!", e); } } @@ -68,7 +66,7 @@ public class PGPConversionHelper { Log.e(Constants.TAG, "No keys given!"); } } catch (IOException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "Error while converting to PGPSecretKeyRing!", e); } return keyRing; @@ -88,7 +86,7 @@ public class PGPConversionHelper { Log.e(Constants.TAG, "No keys given!"); } } catch (IOException e) { - e.printStackTrace(); + Log.e(Constants.TAG, "Error while converting to PGPPublicKeyRing!", e); } return keyRing; @@ -135,7 +133,7 @@ public class PGPConversionHelper { try { return key.getEncoded(); } catch (IOException e) { - Log.e(Constants.TAG, "Encoding failed: ", e); + Log.e(Constants.TAG, "Encoding failed", e); return null; } @@ -151,7 +149,7 @@ public class PGPConversionHelper { try { return keyRing.getEncoded(); } catch (IOException e) { - Log.e(Constants.TAG, "Encoding failed: ", e); + Log.e(Constants.TAG, "Encoding failed", e); return null; } diff --git a/org_apg/src/org/thialfihar/android/apg/helper/PGPHelper.java b/org_apg/src/org/thialfihar/android/apg/helper/PGPHelper.java index 6d5a07f56..65b782c96 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/PGPHelper.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/PGPHelper.java @@ -233,16 +233,16 @@ public class PGPHelper { public static String getMainUserIdSafe(Context context, PGPPublicKey key) { String userId = getMainUserId(key); - if (userId == null) { - userId = context.getResources().getString(R.string.unknownUserId); + if (userId == null || userId.equals("")) { + userId = context.getString(R.string.unknownUserId); } return userId; } public static String getMainUserIdSafe(Context context, PGPSecretKey key) { String userId = getMainUserId(key); - if (userId == null) { - userId = context.getResources().getString(R.string.unknownUserId); + if (userId == null || userId.equals("")) { + userId = context.getString(R.string.unknownUserId); } return userId; } @@ -384,7 +384,7 @@ public class PGPHelper { } - public static String getPubkeyAsArmoredString(long keyId) { + public static String getPubkeyAsArmoredString(Context context, long keyId) { PGPPublicKey key = PGPMain.getPublicKey(keyId); // if it is no public key get it from your own keys... if (key == null) { @@ -398,6 +398,8 @@ public class PGPHelper { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ArmoredOutputStream aos = new ArmoredOutputStream(bos); + aos.setHeader("Version", PGPMain.getFullVersion(context)); + String armouredKey = null; try { aos.write(key.getEncoded()); diff --git a/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java b/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java index 960baea72..2fd63bda1 100644 --- a/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java +++ b/org_apg/src/org/thialfihar/android/apg/helper/PGPMain.java @@ -51,6 +51,7 @@ import org.spongycastle.openpgp.PGPSignature; import org.spongycastle.openpgp.PGPSignatureGenerator; import org.spongycastle.openpgp.PGPSignatureList; import org.spongycastle.openpgp.PGPSignatureSubpacketGenerator; +import org.spongycastle.openpgp.PGPSignatureSubpacketVector; import org.spongycastle.openpgp.PGPUtil; import org.spongycastle.openpgp.PGPV3SignatureGenerator; import org.spongycastle.openpgp.operator.PBEDataDecryptorFactory; @@ -88,7 +89,6 @@ import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.R; -import android.app.Activity; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; @@ -750,6 +750,7 @@ public class PGPMain { throw new GeneralException(context.getString(R.string.error_externalStorageNotReady)); } ArmoredOutputStream out = new ArmoredOutputStream(outStream); + out.setHeader("Version", getFullVersion(context)); int numKeys = 0; for (int i = 0; i < keyRingIds.size(); ++i) { @@ -771,7 +772,7 @@ public class PGPMain { ++numKeys; } out.close(); - returnData.putInt("exported", numKeys); + returnData.putInt(ApgService.RESULT_EXPORT, numKeys); if (progress != null) progress.setProgress(R.string.progress_done, 100, 100); @@ -1048,22 +1049,26 @@ public class PGPMain { PGPPrivateKey signaturePrivateKey = null; if (signatureKeyId == 0) { + armorOut.close(); throw new GeneralException(context.getString(R.string.error_noSignatureKey)); } signingKeyRing = getSecretKeyRing(signatureKeyId); signingKey = PGPHelper.getSigningKey(signatureKeyId); if (signingKey == null) { + armorOut.close(); throw new GeneralException(context.getString(R.string.error_signatureFailed)); } if (signaturePassPhrase == null) { + armorOut.close(); throw new GeneralException(context.getString(R.string.error_noSignaturePassPhrase)); } PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( BOUNCY_CASTLE_PROVIDER_NAME).build(signaturePassPhrase.toCharArray()); signaturePrivateKey = signingKey.extractPrivateKey(keyDecryptor); if (signaturePrivateKey == null) { + armorOut.close(); throw new GeneralException(context.getString(R.string.error_couldNotExtractPrivateKey)); } if (progress != null) @@ -1147,8 +1152,10 @@ public class PGPMain { throws GeneralException, PGPException, IOException, NoSuchAlgorithmException, SignatureException { - ArmoredOutputStream armorOut = null; OutputStream out = null; + + // Ascii Armor (Base64) + ArmoredOutputStream armorOut = null; if (armored) { armorOut = new ArmoredOutputStream(outStream); armorOut.setHeader("Version", getFullVersion(context)); @@ -1174,6 +1181,7 @@ public class PGPMain { if (signaturePassPhrase == null) { throw new GeneralException(context.getString(R.string.error_noSignaturePassPhrase)); } + PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( BOUNCY_CASTLE_PROVIDER_NAME).build(signaturePassPhrase.toCharArray()); signaturePrivateKey = signingKey.extractPrivateKey(keyDecryptor); @@ -1202,7 +1210,6 @@ public class PGPMain { if (forceV3Signature) { signatureV3Generator = new PGPV3SignatureGenerator(contentSignerBuilder); signatureV3Generator.init(type, signaturePrivateKey); - } else { signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); signatureGenerator.init(type, signaturePrivateKey); @@ -1261,6 +1268,51 @@ public class PGPMain { progress.setProgress(R.string.progress_done, 100, 100); } + public static PGPPublicKeyRing signKey(Context context, long masterKeyId, long pubKeyId) + throws GeneralException, NoSuchAlgorithmException, NoSuchProviderException, + PGPException, SignatureException { + String signaturePassPhrase = PGPMain.getCachedPassPhrase(masterKeyId); + if (signaturePassPhrase == null || signaturePassPhrase.length() <= 0) { + throw new GeneralException("Unable to obtain passphrase"); + } else { + PGPPublicKeyRing pubring = PGPMain.getPublicKeyRing(pubKeyId); + + PGPSecretKey signingKey = PGPHelper.getSigningKey(masterKeyId); + if (signingKey == null) { + throw new GeneralException(context.getString(R.string.error_signatureFailed)); + } + + PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( + BOUNCY_CASTLE_PROVIDER_NAME).build(signaturePassPhrase.toCharArray()); + PGPPrivateKey signaturePrivateKey = signingKey.extractPrivateKey(keyDecryptor); + if (signaturePrivateKey == null) { + throw new GeneralException( + context.getString(R.string.error_couldNotExtractPrivateKey)); + } + + // TODO: SHA256 fixed? + JcaPGPContentSignerBuilder contentSignerBuilder = new JcaPGPContentSignerBuilder( + signingKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256) + .setProvider(BOUNCY_CASTLE_PROVIDER_NAME); + + PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator( + contentSignerBuilder); + + signatureGenerator.init(PGPSignature.DIRECT_KEY, signaturePrivateKey); + + PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); + + PGPSignatureSubpacketVector packetVector = spGen.generate(); + signatureGenerator.setHashedSubpackets(packetVector); + + PGPPublicKey signedKey = PGPPublicKey.addCertification(pubring.getPublicKey(pubKeyId), + signatureGenerator.generate()); + pubring = PGPPublicKeyRing.insertPublicKey(pubring, signedKey); + + return pubring; + } + } + public static long getDecryptionKeyId(Context context, InputStream inputStream) throws GeneralException, NoAsymmetricEncryptionException, IOException { InputStream in = PGPUtil.getDecoderStream(inputStream); diff --git a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java index 2121a500a..43987ccbc 100644 --- a/org_apg/src/org/thialfihar/android/apg/service/ApgService.java +++ b/org_apg/src/org/thialfihar/android/apg/service/ApgService.java @@ -28,18 +28,10 @@ import java.io.OutputStream; import java.util.ArrayList; import java.util.Vector; -import org.spongycastle.jce.provider.BouncyCastleProvider; import org.spongycastle.openpgp.PGPKeyRing; -import org.spongycastle.openpgp.PGPPrivateKey; -import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKeyRing; import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKeyRing; -import org.spongycastle.openpgp.PGPSignature; -import org.spongycastle.openpgp.PGPSignatureGenerator; -import org.spongycastle.openpgp.PGPSignatureSubpacketGenerator; -import org.spongycastle.openpgp.PGPSignatureSubpacketVector; -import org.spongycastle.openpgp.PGPUtil; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.R; @@ -198,6 +190,9 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { public static final String RESULT_IMPORT_UPDATED = "updated"; public static final String RESULT_IMPORT_BAD = "bad"; + // export + public static final String RESULT_EXPORT = "exported"; + // query public static final String RESULT_QUERY_KEY_KEY_DATA = "queryKeyKeyData"; public static final String RESULT_QUERY_KEY_SEARCH_RESULT = "queryKeySearchResult"; @@ -740,12 +735,10 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { try { /* Input */ - int keyRingId = data.getInt(UPLOAD_KEY_KEYRING_ID); String keyServer = data.getString(UPLOAD_KEY_SERVER); /* Operation */ - HkpKeyServer server = new HkpKeyServer(keyServer); PGPKeyRing keyring = PGPMain.getKeyRing(keyRingId); @@ -770,7 +763,6 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { try { /* Input */ - int queryType = data.getInt(QUERY_KEY_TYPE); String keyServer = data.getString(QUERY_KEY_SERVER); @@ -778,7 +770,6 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { long keyId = data.getLong(QUERY_KEY_ID); /* Operation */ - Bundle resultData = new Bundle(); HkpKeyServer server = new HkpKeyServer(keyServer); @@ -803,43 +794,16 @@ public class ApgService extends IntentService implements ProgressDialogUpdater { try { /* Input */ - long masterKeyId = data.getLong(SIGN_KEY_MASTER_KEY_ID); long pubKeyId = data.getLong(SIGN_KEY_PUB_KEY_ID); /* Operation */ + PGPPublicKeyRing signedPubKeyRing = PGPMain.signKey(this, masterKeyId, pubKeyId); - String passphrase = PGPMain.getCachedPassPhrase(masterKeyId); - if (passphrase == null || passphrase.length() <= 0) { - sendErrorToHandler(new GeneralException("Unable to obtain passphrase")); - } else { - PGPPublicKeyRing pubring = PGPMain.getPublicKeyRing(pubKeyId); - - /* - * sign the incoming key - */ - PGPSecretKey secretKey = PGPMain.getSecretKey(masterKeyId); - PGPPrivateKey signingKey = secretKey.extractPrivateKey( - passphrase.toCharArray(), BouncyCastleProvider.PROVIDER_NAME); - PGPSignatureGenerator sGen = new PGPSignatureGenerator(secretKey.getPublicKey() - .getAlgorithm(), PGPUtil.SHA256, BouncyCastleProvider.PROVIDER_NAME); - sGen.initSign(PGPSignature.DIRECT_KEY, signingKey); - - PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); - - PGPSignatureSubpacketVector packetVector = spGen.generate(); - sGen.setHashedSubpackets(packetVector); - - PGPPublicKey signedKey = PGPPublicKey.addCertification( - pubring.getPublicKey(pubKeyId), sGen.generate()); - pubring = PGPPublicKeyRing.insertPublicKey(pubring, signedKey); - - // store the signed key in our local cache - int retval = PGPMain.storeKeyRingInCache(pubring); - if (retval != Id.return_value.ok && retval != Id.return_value.updated) { - sendErrorToHandler(new GeneralException( - "Failed to store signed key in local cache")); - } + // store the signed key in our local cache + int retval = PGPMain.storeKeyRingInCache(signedPubKeyRing); + if (retval != Id.return_value.ok && retval != Id.return_value.updated) { + throw new GeneralException("Failed to store signed key in local cache"); } sendMessageToHandler(ApgServiceHandler.MESSAGE_OKAY); diff --git a/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java index 52bad8a2a..40f2b3f43 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/DecryptActivity.java @@ -20,6 +20,7 @@ import org.spongycastle.openpgp.PGPPublicKeyRing; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.helper.FileHelper; +import org.thialfihar.android.apg.helper.OtherHelper; import org.thialfihar.android.apg.helper.PGPHelper; import org.thialfihar.android.apg.helper.PGPMain; import org.thialfihar.android.apg.service.ApgServiceHandler; @@ -28,11 +29,9 @@ import org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment; import org.thialfihar.android.apg.ui.dialog.FileDialogFragment; import org.thialfihar.android.apg.ui.dialog.LookupUnknownKeyDialogFragment; import org.thialfihar.android.apg.ui.dialog.PassphraseDialogFragment; -import org.thialfihar.android.apg.ui.dialog.ProgressDialogFragment; import org.thialfihar.android.apg.util.Compatibility; import org.thialfihar.android.apg.R; -import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; @@ -185,16 +184,7 @@ public class DecryptActivity extends SherlockFragmentActivity { setContentView(R.layout.decrypt); // set actionbar without home button if called from another app - final ActionBar actionBar = getSupportActionBar(); - Log.d(Constants.TAG, "calling package (only set when using startActivityForResult)=" - + getCallingPackage()); - if (getCallingPackage() != null && getCallingPackage().equals(Constants.PACKAGE_NAME)) { - actionBar.setDisplayHomeAsUpEnabled(true); - actionBar.setHomeButtonEnabled(true); - } else { - actionBar.setDisplayHomeAsUpEnabled(false); - actionBar.setHomeButtonEnabled(false); - } + OtherHelper.setActionBarBackButton(this); mSource = (ViewFlipper) findViewById(R.id.source); mSourceLabel = (TextView) findViewById(R.id.sourceLabel); diff --git a/org_apg/src/org/thialfihar/android/apg/ui/EditKeyActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/EditKeyActivity.java index 87f2193a3..55ac9518d 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/EditKeyActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/EditKeyActivity.java @@ -21,12 +21,12 @@ import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKeyRing; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; +import org.thialfihar.android.apg.helper.OtherHelper; import org.thialfihar.android.apg.helper.PGPHelper; import org.thialfihar.android.apg.helper.PGPMain; import org.thialfihar.android.apg.helper.PGPConversionHelper; import org.thialfihar.android.apg.service.ApgServiceHandler; import org.thialfihar.android.apg.service.ApgService; -import org.thialfihar.android.apg.ui.dialog.ProgressDialogFragment; import org.thialfihar.android.apg.ui.dialog.SetPassphraseDialogFragment; import org.thialfihar.android.apg.ui.widget.KeyEditor; import org.thialfihar.android.apg.ui.widget.SectionView; @@ -138,14 +138,8 @@ public class EditKeyActivity extends SherlockFragmentActivity { mActionBar = getSupportActionBar(); mActionBar.setDisplayShowTitleEnabled(true); - // set actionbar without home button if called from another app - if (getCallingPackage() != null && getCallingPackage().equals(Constants.PACKAGE_NAME)) { - mActionBar.setDisplayHomeAsUpEnabled(true); - mActionBar.setHomeButtonEnabled(true); - } else { - mActionBar.setDisplayHomeAsUpEnabled(false); - mActionBar.setHomeButtonEnabled(false); - } + // set actionbar without home button if called from another app + OtherHelper.setActionBarBackButton(this); // find views mChangePassPhrase = (Button) findViewById(R.id.edit_key_btn_change_pass_phrase); diff --git a/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java index 7316fc770..4e5fda3db 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/EncryptActivity.java @@ -24,6 +24,7 @@ import org.spongycastle.openpgp.PGPSecretKeyRing; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; import org.thialfihar.android.apg.helper.FileHelper; +import org.thialfihar.android.apg.helper.OtherHelper; import org.thialfihar.android.apg.helper.PGPHelper; import org.thialfihar.android.apg.helper.PGPMain; import org.thialfihar.android.apg.helper.Preferences; @@ -36,7 +37,6 @@ import org.thialfihar.android.apg.util.Choice; import org.thialfihar.android.apg.util.Compatibility; import org.thialfihar.android.apg.R; -import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; @@ -199,16 +199,7 @@ public class EncryptActivity extends SherlockFragmentActivity { setContentView(R.layout.encrypt); // set actionbar without home button if called from another app - final ActionBar actionBar = getSupportActionBar(); - Log.d(Constants.TAG, "calling package (only set when using startActivityForResult)=" - + getCallingPackage()); - if (getCallingPackage() != null && getCallingPackage().equals(Constants.PACKAGE_NAME)) { - actionBar.setDisplayHomeAsUpEnabled(true); - actionBar.setHomeButtonEnabled(true); - } else { - actionBar.setDisplayHomeAsUpEnabled(false); - actionBar.setHomeButtonEnabled(false); - } + OtherHelper.setActionBarBackButton(this); mGenerateSignature = false; @@ -703,7 +694,7 @@ public class EncryptActivity extends SherlockFragmentActivity { // Create a new Messenger for the communication back Messenger messenger = new Messenger(returnHandler); - + try { PassphraseDialogFragment passphraseDialog = PassphraseDialogFragment.newInstance( messenger, mSecretKeyId); diff --git a/org_apg/src/org/thialfihar/android/apg/ui/KeyListActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/KeyListActivity.java index d0438fbe6..f992862a0 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/KeyListActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/KeyListActivity.java @@ -19,6 +19,7 @@ package org.thialfihar.android.apg.ui; import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Id; +import org.thialfihar.android.apg.helper.OtherHelper; import org.thialfihar.android.apg.helper.PGPHelper; import org.thialfihar.android.apg.helper.PGPMain; import org.thialfihar.android.apg.provider.KeyRings; @@ -93,6 +94,9 @@ public class KeyListActivity extends SherlockFragmentActivity { super.onCreate(savedInstanceState); setContentView(R.layout.key_list); + // set actionbar without home button if called from another app + OtherHelper.setActionBarBackButton(this); + setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); mList = (ExpandableListView) findViewById(R.id.list); @@ -152,18 +156,24 @@ public class KeyListActivity extends SherlockFragmentActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { + + case android.R.id.home: + // app icon in Action Bar clicked; go home + Intent intent = new Intent(this, MainActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + return true; + case Id.menu.option.import_keys: { - // showDialog(Id.dialog.import_keys); showImportKeysDialog(); return true; } case Id.menu.option.export_keys: { - // showDialog(Id.dialog.export_keys); showExportKeysDialog(false); return true; } - + case Id.menu.option.search: startSearch("", false, null, false); return true; @@ -401,7 +411,7 @@ public class KeyListActivity extends SherlockFragmentActivity { // get returned data bundle Bundle returnData = message.getData(); - int exported = returnData.getInt("exported"); + int exported = returnData.getInt(ApgService.RESULT_EXPORT); String toastMessage; if (exported == 1) { toastMessage = getString(R.string.keyExported); @@ -724,7 +734,7 @@ public class KeyListActivity extends SherlockFragmentActivity { mFileDialog.setFilename(path); } catch (NullPointerException e) { - Log.e(Constants.TAG, "Nullpointer while retrieving path!"); + Log.e(Constants.TAG, "Nullpointer while retrieving path!", e); } } return; diff --git a/org_apg/src/org/thialfihar/android/apg/ui/MainActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/MainActivity.java index 26387f870..5c9c071c9 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/MainActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/MainActivity.java @@ -32,23 +32,27 @@ import android.view.View; public class MainActivity extends SherlockActivity { public void manageKeysOnClick(View view) { - startActivity(new Intent(this, PublicKeyListActivity.class)); + // used instead of startActivity set actionbar based on callingPackage + startActivityForResult(new Intent(this, PublicKeyListActivity.class), 0); } public void myKeysOnClick(View view) { - startActivity(new Intent(this, SecretKeyListActivity.class)); + // used instead of startActivity set actionbar based on callingPackage + startActivityForResult(new Intent(this, SecretKeyListActivity.class), 0); } public void encryptOnClick(View view) { Intent intent = new Intent(MainActivity.this, EncryptActivity.class); intent.setAction(EncryptActivity.ACTION_ENCRYPT); - startActivityForResult(intent, 0); // used instead of startActivity to get callingPackage + // used instead of startActivity set actionbar based on callingPackage + startActivityForResult(intent, 0); } public void decryptOnClick(View view) { Intent intent = new Intent(MainActivity.this, DecryptActivity.class); intent.setAction(DecryptActivity.ACTION_DECRYPT); - startActivityForResult(intent, 0); // used instead of startActivity to get callingPackage + // used instead of startActivity set actionbar based on callingPackage + startActivityForResult(intent, 0); } public void scanQrcodeOnClick(View view) { diff --git a/org_apg/src/org/thialfihar/android/apg/ui/SecretKeyListActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/SecretKeyListActivity.java index 0ba476768..d7c272cf7 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/SecretKeyListActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/SecretKeyListActivity.java @@ -117,7 +117,7 @@ public class SecretKeyListActivity extends KeyListActivity implements OnChildCli long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()) .getGroupId(mSelectedItem); // String msg = keyId + "," + PGPHelper.getFingerPrint(keyId); - String msg = PGPHelper.getPubkeyAsArmoredString(keyId); + String msg = PGPHelper.getPubkeyAsArmoredString(this, keyId); new IntentIntegrator(this).shareText(msg); } diff --git a/org_apg/src/org/thialfihar/android/apg/ui/dialog/DeleteKeyDialogFragment.java b/org_apg/src/org/thialfihar/android/apg/ui/dialog/DeleteKeyDialogFragment.java index 4cb1cd02f..d8a0cb33b 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/dialog/DeleteKeyDialogFragment.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/dialog/DeleteKeyDialogFragment.java @@ -73,7 +73,7 @@ public class DeleteKeyDialogFragment extends DialogFragment { final int keyType = getArguments().getInt(ARG_KEY_TYPE); // TODO: better way to do this? - String userId = ""; + String userId = activity.getString(R.string.unknownUserId); Object keyRing = PGPMain.getKeyRing(deleteKeyRingId); if (keyRing != null) { if (keyRing instanceof PGPPublicKeyRing) { diff --git a/org_apg/src/org/thialfihar/android/apg/ui/dialog/PassphraseDialogFragment.java b/org_apg/src/org/thialfihar/android/apg/ui/dialog/PassphraseDialogFragment.java index 3e4898a2b..67d4e94c0 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/dialog/PassphraseDialogFragment.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/dialog/PassphraseDialogFragment.java @@ -42,7 +42,6 @@ import org.thialfihar.android.apg.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; -import android.widget.TextView; import android.widget.Toast; public class PassphraseDialogFragment extends DialogFragment { @@ -92,8 +91,7 @@ public class PassphraseDialogFragment extends DialogFragment { private static boolean hasPassphrase(long secretKeyId) { // check if the key has no passphrase try { - PGPSecretKey secretKey = PGPHelper.getMasterKey(PGPMain - .getSecretKeyRing(secretKeyId)); + PGPSecretKey secretKey = PGPHelper.getMasterKey(PGPMain.getSecretKeyRing(secretKeyId)); Log.d(Constants.TAG, "Check if key has no passphrase..."); PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( @@ -132,7 +130,7 @@ public class PassphraseDialogFragment extends DialogFragment { if (secretKeyId == Id.key.symmetric || secretKeyId == Id.key.none) { secretKey = null; - alert.setMessage(getString(R.string.passPhraseForSymmetricEncryption)); + alert.setMessage(R.string.passPhraseForSymmetricEncryption); } else { secretKey = PGPHelper.getMasterKey(PGPMain.getSecretKeyRing(secretKeyId)); if (secretKey == null) { @@ -147,22 +145,17 @@ public class PassphraseDialogFragment extends DialogFragment { return alert.create(); } String userId = PGPHelper.getMainUserIdSafe(activity, secretKey); + + Log.d(Constants.TAG, "User id: '" + userId + "'"); alert.setMessage(getString(R.string.passPhraseFor, userId)); } LayoutInflater inflater = activity.getLayoutInflater(); View view = inflater.inflate(R.layout.passphrase, null); - final EditText input = (EditText) view.findViewById(R.id.passphrase_passphrase); - - final TextView labelNotUsed = (TextView) view - .findViewById(R.id.passphrase_label_passphrase_again); - labelNotUsed.setVisibility(View.GONE); - final EditText inputNotUsed = (EditText) view - .findViewById(R.id.passphrase_passphrase_again); - inputNotUsed.setVisibility(View.GONE); - alert.setView(view); + final EditText input = (EditText) view.findViewById(R.id.passphrase_passphrase); + alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dismiss(); @@ -172,7 +165,8 @@ public class PassphraseDialogFragment extends DialogFragment { if (secretKey != null) { try { PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder() - .setProvider("SC").build(passPhrase.toCharArray()); + .setProvider(PGPMain.BOUNCY_CASTLE_PROVIDER_NAME).build( + passPhrase.toCharArray()); PGPPrivateKey testKey = secretKey.extractPrivateKey(keyDecryptor); if (testKey == null) { Toast.makeText(activity, R.string.error_couldNotExtractPrivateKey, diff --git a/org_apg/src/org/thialfihar/android/apg/ui/dialog/SetPassphraseDialogFragment.java b/org_apg/src/org/thialfihar/android/apg/ui/dialog/SetPassphraseDialogFragment.java index 5bd5342dd..849f3428b 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/dialog/SetPassphraseDialogFragment.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/dialog/SetPassphraseDialogFragment.java @@ -22,7 +22,6 @@ import org.thialfihar.android.apg.R; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; -import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.os.Message; @@ -80,9 +79,8 @@ public class SetPassphraseDialogFragment extends DialogFragment { alert.setTitle(title); alert.setMessage(R.string.enterPassPhraseTwice); - LayoutInflater inflater = (LayoutInflater) activity - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - View view = inflater.inflate(R.layout.passphrase, null); + LayoutInflater inflater = activity.getLayoutInflater(); + View view = inflater.inflate(R.layout.passphrase_repeat, null); final EditText input1 = (EditText) view.findViewById(R.id.passphrase_passphrase); final EditText input2 = (EditText) view.findViewById(R.id.passphrase_passphrase_again);