From d686c55a0a86ef845795fc03a8a5de44b5fe73cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 17 Sep 2014 13:45:16 +0200 Subject: Work on new result handling (WIP) --- .../keychain/ui/EncryptTextActivity.java | 137 +++++++++++++-------- .../keychain/ui/EncryptTextFragment.java | 9 +- .../keychain/ui/PassphraseDialogActivity.java | 8 +- .../ui/dialog/CustomAlertDialogBuilder.java | 8 +- 4 files changed, 102 insertions(+), 60 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextActivity.java index 4bf1cad3c..56a2ef233 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextActivity.java @@ -22,7 +22,6 @@ import android.app.ProgressDialog; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; import android.os.Message; import android.os.Messenger; import android.support.v4.app.Fragment; @@ -35,15 +34,11 @@ import org.sufficientlysecure.keychain.api.OpenKeychainIntents; import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.helper.ShareHelper; +import org.sufficientlysecure.keychain.nfc.NfcActivity; import org.sufficientlysecure.keychain.pgp.KeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; -import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; -import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; -import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.service.results.SignEncryptResult; -import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Notify; @@ -200,22 +195,36 @@ public class EncryptTextActivity extends DrawerActivity implements EncryptActivi SignEncryptResult result = message.getData().getParcelable(SignEncryptResult.EXTRA_RESULT); - // TODO if (result.isPending()) - - if (!result.success()) { - result.createNotify(EncryptTextActivity.this).show(); - return; - } - - if (mShareAfterEncrypt) { - // Share encrypted message/file - startActivity(sendWithChooserExcludingEncrypt(message)); + if (result.isPending()) { + Log.d(Constants.TAG, "result.getResult() " + result.getResult()); + if ((result.getResult() & SignEncryptResult.RESULT_PENDING_PASSPHRASE) == + SignEncryptResult.RESULT_PENDING_PASSPHRASE) { + Log.d(Constants.TAG, "passp"); + startPassphraseDialog(result.getKeyIdPassphraseNeeded()); + } else if ((result.getResult() & SignEncryptResult.RESULT_PENDING_NFC) == + SignEncryptResult.RESULT_PENDING_NFC) { + Log.d(Constants.TAG, "nfc"); + + // use after nfc sign +//// data.putExtra(OpenPgpApi.EXTRA_NFC_SIG_CREATION_TIMESTAMP, result.getNfcTimestamp().getTime()); + startNfcSign("123456", result.getNfcHash(), result.getNfcAlgo()); + } else { + throw new RuntimeException("Unhandled pending result!"); + } + + } else if (result.success()) { + if (mShareAfterEncrypt) { + // Share encrypted message/file + startActivity(sendWithChooserExcludingEncrypt(message)); + } else { + // Copy to clipboard + copyToClipboard(message); + result.createNotify(EncryptTextActivity.this).show(); + // Notify.showNotify(EncryptTextActivity.this, + // R.string.encrypt_sign_clipboard_successful, Notify.Style.INFO); + } } else { - // Copy to clipboard - copyToClipboard(message); result.createNotify(EncryptTextActivity.this).show(); - // Notify.showNotify(EncryptTextActivity.this, - // R.string.encrypt_sign_clipboard_successful, Notify.Style.INFO); } } } @@ -231,6 +240,36 @@ public class EncryptTextActivity extends DrawerActivity implements EncryptActivi startService(intent); } + private void startNfcSign(String pin, byte[] hashToSign, int hashAlgo) { + Intent data = new Intent(); + + // build PendingIntent for Yubikey NFC operations + Intent intent = new Intent(this, NfcActivity.class); + intent.setAction(NfcActivity.ACTION_SIGN_HASH); + // pass params through to activity that it can be returned again later to repeat pgp operation + intent.putExtra(NfcActivity.EXTRA_DATA, data); + intent.putExtra(NfcActivity.EXTRA_PIN, pin); + + intent.putExtra(NfcActivity.EXTRA_NFC_HASH_TO_SIGN, hashToSign); + intent.putExtra(NfcActivity.EXTRA_NFC_HASH_ALGO, hashAlgo); + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); + + startActivityForResult(intent, 0); + } + + private void startPassphraseDialog(long subkeyId) { + Intent data = new Intent(); + + // build PendingIntent for Yubikey NFC operations + Intent intent = new Intent(this, PassphraseDialogActivity.class); + // pass params through to activity that it can be returned again later to repeat pgp operation + intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, subkeyId); + +// intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); + + startActivityForResult(intent, 0); + } + private Bundle createEncryptBundle() { // fill values for this action Bundle data = new Bundle(); @@ -326,35 +365,35 @@ public class EncryptTextActivity extends DrawerActivity implements EncryptActivi return false; } - try { - // TODO This should really not be decided here. We do need the info for the passphrase - // TODO dialog fragment though, so that's just the way it is for now. - if (mSigningKeyId != 0) { - CachedPublicKeyRing signingRing = - new ProviderHelper(this).getCachedPublicKeyRing(mSigningKeyId); - long sigSubKeyId = signingRing.getSignId(); - // Make sure the passphrase is cached, then start over. - if (PassphraseCacheService.getCachedPassphrase(this, sigSubKeyId) == null) { - PassphraseDialogFragment.show(this, sigSubKeyId, - new Handler() { - @Override - public void handleMessage(Message message) { - if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { - // restart - startEncrypt(); - } - } - } - ); - - return false; - } - } - } catch (PgpGeneralException e) { - Log.e(Constants.TAG, "Key not found!", e); - } catch (PassphraseCacheService.KeyNotFoundException e) { - Log.e(Constants.TAG, "Key not found!", e); - } +// try { +// // TODO This should really not be decided here. We do need the info for the passphrase +// // TODO dialog fragment though, so that's just the way it is for now. +// if (mSigningKeyId != 0) { +// CachedPublicKeyRing signingRing = +// new ProviderHelper(this).getCachedPublicKeyRing(mSigningKeyId); +// long sigSubKeyId = signingRing.getSignId(); +// // Make sure the passphrase is cached, then start over. +// if (PassphraseCacheService.getCachedPassphrase(this, sigSubKeyId) == null) { +// PassphraseDialogFragment.show(this, sigSubKeyId, +// new Handler() { +// @Override +// public void handleMessage(Message message) { +// if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { +// // restart +// startEncrypt(); +// } +// } +// } +// ); +// +// return false; +// } +// } +// } catch (PgpGeneralException e) { +// Log.e(Constants.TAG, "Key not found!", e); +// } catch (PassphraseCacheService.KeyNotFoundException e) { +// Log.e(Constants.TAG, "Key not found!", e); +// } } return true; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextFragment.java index fa9036565..b13cb7837 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptTextFragment.java @@ -32,13 +32,12 @@ import org.sufficientlysecure.keychain.R; public class EncryptTextFragment extends Fragment { public static final String ARG_TEXT = "text"; - private TextView mMessage = null; + private TextView mText; private View mEncryptShare; private View mEncryptClipboard; private EncryptActivityInterface mEncryptInterface; - @Override public void onAttach(Activity activity) { super.onAttach(activity); @@ -56,8 +55,8 @@ public class EncryptTextFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.encrypt_text_fragment, container, false); - mMessage = (TextView) view.findViewById(R.id.message); - mMessage.addTextChangedListener(new TextWatcher() { + mText = (TextView) view.findViewById(R.id.encrypt_text_text); + mText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -98,7 +97,7 @@ public class EncryptTextFragment extends Fragment { String text = mEncryptInterface.getMessage(); if (text != null) { - mMessage.setText(text); + mText.setText(text); } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java index fa9444862..705eb51ce 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PassphraseDialogActivity.java @@ -59,7 +59,7 @@ import org.sufficientlysecure.keychain.util.Log; public class PassphraseDialogActivity extends FragmentActivity { public static final String MESSAGE_DATA_PASSPHRASE = "passphrase"; - public static final String EXTRA_SECRET_KEY_ID = "secret_key_id"; + public static final String EXTRA_SUBKEY_ID = "secret_key_id"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -76,7 +76,7 @@ public class PassphraseDialogActivity extends FragmentActivity { // this activity itself has no content view (see manifest) - long keyId = getIntent().getLongExtra(EXTRA_SECRET_KEY_ID, 0); + long keyId = getIntent().getLongExtra(EXTRA_SUBKEY_ID, 0); show(this, keyId); } @@ -92,7 +92,7 @@ public class PassphraseDialogActivity extends FragmentActivity { // do NOT check if the key even needs a passphrase. that's not our job here. PassphraseDialogFragment frag = new PassphraseDialogFragment(); Bundle args = new Bundle(); - args.putLong(EXTRA_SECRET_KEY_ID, keyId); + args.putLong(EXTRA_SUBKEY_ID, keyId); frag.setArguments(args); @@ -116,7 +116,7 @@ public class PassphraseDialogActivity extends FragmentActivity { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Activity activity = getActivity(); - mSubKeyId = getArguments().getLong(EXTRA_SECRET_KEY_ID); + mSubKeyId = getArguments().getLong(EXTRA_SUBKEY_ID); CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder(activity); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/CustomAlertDialogBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/CustomAlertDialogBuilder.java index 4b40b7ef1..b33bb2763 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/CustomAlertDialogBuilder.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/CustomAlertDialogBuilder.java @@ -2,12 +2,14 @@ package org.sufficientlysecure.keychain.ui.dialog; import android.app.Activity; import android.app.AlertDialog; +import android.view.ContextThemeWrapper; import android.view.View; import android.widget.TextView; import org.sufficientlysecure.keychain.R; -/** This class extends AlertDiaog.Builder, styling the header using emphasis color. +/** + * This class extends AlertDiaog.Builder, styling the header using emphasis color. * Note that this class is a huge hack, because dialog boxes aren't easily stylable. * Also, the dialog NEEDS to be called with show() directly, not create(), otherwise * the order of internal operations will lead to a crash! @@ -15,7 +17,9 @@ import org.sufficientlysecure.keychain.R; public class CustomAlertDialogBuilder extends AlertDialog.Builder { public CustomAlertDialogBuilder(Activity activity) { - super(activity); + // if the progress dialog is displayed from the application class, design is missing + // hack to get holo design (which is not automatically applied due to activity's Theme.NoDisplay + super(new ContextThemeWrapper(activity, R.style.Theme_AppCompat_Light)); } @Override -- cgit v1.2.3