aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2015-06-06 23:17:42 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2015-06-06 23:17:42 +0200
commit074b6633b015aba84f8f60a05878a93d4b8ec9b2 (patch)
treeb7d11a8df2bc0ff93b245fbc67d7c7ba6bf9dd18 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base
parent7cfc0d80d0b4318ce1ae027536d70c2cda4c8605 (diff)
downloadopen-keychain-074b6633b015aba84f8f60a05878a93d4b8ec9b2.tar.gz
open-keychain-074b6633b015aba84f8f60a05878a93d4b8ec9b2.tar.bz2
open-keychain-074b6633b015aba84f8f60a05878a93d4b8ec9b2.zip
eventbus: initial attempt, replace messenger hack with eventbus communication
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java56
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java139
2 files changed, 160 insertions, 35 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java
index d0b6f502f..e8e8c5363 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CachingCryptoOperationFragment.java
@@ -1,26 +1,27 @@
package org.sufficientlysecure.keychain.ui.base;
+import android.app.ProgressDialog;
+import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Parcelable;
+import org.sufficientlysecure.keychain.R;
+import org.sufficientlysecure.keychain.operations.results.OperationResult;
+import org.sufficientlysecure.keychain.service.KeychainNewService;
import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
-public abstract class CachingCryptoOperationFragment <T extends Parcelable> extends CryptoOperationFragment {
+public abstract class CachingCryptoOperationFragment <T extends Parcelable, S extends OperationResult>
+ extends CryptoOperationFragment<T, S> {
public static final String ARG_CACHED_ACTIONS = "cached_actions";
private T mCachedActionsParcel;
@Override
- protected void cryptoOperation(CryptoInputParcel cryptoInput) {
- cryptoOperation(cryptoInput, mCachedActionsParcel);
- }
-
- @Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
@@ -37,21 +38,44 @@ public abstract class CachingCryptoOperationFragment <T extends Parcelable> exte
}
@Override
- public boolean handlePendingMessage(Message message) {
- // see if it's an InputPendingResult, and if so don't care
- if (super.handlePendingMessage(message)) {
- return true;
- }
+ protected void onCryptoOperationResult(S result) {
+ super.onCryptoOperationResult(result);
+ mCachedActionsParcel = null;
+ }
+
+ protected abstract T createOperationInput();
- // if it's a non-input-pending OKAY message, always clear the cached actions parcel
- if (message.arg1 == ServiceProgressHandler.MessageStatus.OKAY.ordinal()) {
- mCachedActionsParcel = null;
+ protected void cryptoOperation(CryptoInputParcel cryptoInput) {
+
+ if (mCachedActionsParcel == null) {
+
+ mCachedActionsParcel = createOperationInput();
+ // this is null if invalid, just return in that case
+ if (mCachedActionsParcel == null) {
+ // Notify was created by createCryptoInput.
+ return;
+ }
}
- return false;
+ // Send all information needed to service to edit key in other thread
+ Intent intent = new Intent(getActivity(), KeychainNewService.class);
+
+ intent.putExtra(KeychainNewService.EXTRA_OPERATION_INPUT, mCachedActionsParcel);
+ intent.putExtra(KeychainNewService.EXTRA_CRYPTO_INPUT, cryptoInput);
+
+ showProgressFragment(
+ getString(R.string.progress_start),
+ ProgressDialog.STYLE_HORIZONTAL,
+ false);
+
+ // start service with intent
+ getActivity().startService(intent);
+
}
- protected abstract void cryptoOperation(CryptoInputParcel cryptoInput, T cachedActionsParcel);
+ protected T getCachedActionsParcel() {
+ return mCachedActionsParcel;
+ }
protected void cacheActionsParcel(T cachedActionsParcel) {
mCachedActionsParcel = cachedActionsParcel;
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java
index 7fc5eb1f4..407904369 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java
@@ -19,28 +19,46 @@
package org.sufficientlysecure.keychain.ui.base;
import android.app.Activity;
+import android.app.ProgressDialog;
import android.content.Intent;
-import android.os.Bundle;
-import android.os.Message;
+import android.os.Parcelable;
import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import de.greenrobot.event.EventBus;
+import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.operations.results.InputPendingResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult;
-import org.sufficientlysecure.keychain.service.ServiceProgressHandler;
+import org.sufficientlysecure.keychain.service.KeychainNewService;
+import org.sufficientlysecure.keychain.service.ProgressEvent;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.ui.NfcOperationActivity;
import org.sufficientlysecure.keychain.ui.PassphraseDialogActivity;
+import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment;
/**
* All fragments executing crypto operations need to extend this class.
*/
-public abstract class CryptoOperationFragment extends Fragment {
+public abstract class CryptoOperationFragment <T extends Parcelable, S extends OperationResult>
+ extends Fragment {
public static final int REQUEST_CODE_PASSPHRASE = 0x00008001;
public static final int REQUEST_CODE_NFC = 0x00008002;
+ @Override
+ public void onStart() {
+ super.onStart();
+ EventBus.getDefault().register(this);
+ }
+
+ @Override
+ public void onStop() {
+ EventBus.getDefault().unregister(this);
+ super.onStop();
+ }
+
private void initiateInputActivity(RequiredInputParcel requiredInput) {
switch (requiredInput.mType) {
@@ -99,35 +117,118 @@ public abstract class CryptoOperationFragment extends Fragment {
}
}
- public boolean handlePendingMessage(Message message) {
+ protected void dismissProgress() {
- if (message.arg1 == ServiceProgressHandler.MessageStatus.OKAY.ordinal()) {
- Bundle data = message.getData();
+ ProgressDialogFragment progressDialogFragment =
+ (ProgressDialogFragment) getFragmentManager().findFragmentByTag("progressDialog");
- OperationResult result = data.getParcelable(OperationResult.EXTRA_RESULT);
- if (result == null || !(result instanceof InputPendingResult)) {
- return false;
- }
+ if (progressDialogFragment == null) {
+ return;
+ }
+
+ progressDialogFragment.dismissAllowingStateLoss();
+
+ }
+
+ public void showProgressFragment(String progressDialogMessage,
+ int progressDialogStyle,
+ boolean cancelable) {
+
+ if (getFragmentManager().findFragmentByTag("progressDialog") != null) {
+ return;
+ }
+
+ ProgressDialogFragment progressDialogFragment = ProgressDialogFragment.newInstance(
+ progressDialogMessage, progressDialogStyle, cancelable);
+
+ FragmentManager manager = getFragmentManager();
+ progressDialogFragment.show(manager, "progressDialog");
+
+ }
+
+ protected abstract T createOperationInput();
+
+ protected void cryptoOperation(CryptoInputParcel cryptoInput) {
+
+ T operationInput = createOperationInput();
+ if (operationInput == null) {
+ return;
+ }
+
+ // Send all information needed to service to edit key in other thread
+ Intent intent = new Intent(getActivity(), KeychainNewService.class);
+
+ intent.putExtra(KeychainNewService.EXTRA_OPERATION_INPUT, operationInput);
+ intent.putExtra(KeychainNewService.EXTRA_CRYPTO_INPUT, cryptoInput);
+
+ showProgressFragment(
+ getString(R.string.progress_start),
+ ProgressDialog.STYLE_HORIZONTAL,
+ false);
+
+ // start service with intent
+ getActivity().startService(intent);
+ }
+
+ protected void cryptoOperation() {
+ cryptoOperation(new CryptoInputParcel());
+ }
+
+ protected void onCryptoOperationResult(S result) {
+ if (result.success()) {
+ onCryptoOperationSuccess(result);
+ } else {
+ onCryptoOperationError(result);
+ }
+ }
+
+ abstract protected void onCryptoOperationSuccess(S result);
+
+ protected void onCryptoOperationError(S result) {
+ result.createNotify(getActivity()).show(this);
+ }
+
+ protected void onCryptoOperationCancelled() {
+ dismissProgress();
+ }
+
+ @SuppressWarnings("unused") // it's an EventBus method
+ public void onEventMainThread(OperationResult result) {
+
+ if (result instanceof InputPendingResult) {
InputPendingResult pendingResult = (InputPendingResult) result;
if (pendingResult.isPending()) {
RequiredInputParcel requiredInput = pendingResult.getRequiredInputParcel();
initiateInputActivity(requiredInput);
- return true;
+ return;
}
}
- return false;
- }
+ dismissProgress();
+
+ try {
+ // noinspection unchecked, because type erasure :(
+ onCryptoOperationResult((S) result);
+ } catch (ClassCastException e) {
+ throw new AssertionError("bad return class ("
+ + result.getClass().getSimpleName() + "), this is a programming error!");
+ }
- protected void cryptoOperation() {
- cryptoOperation(new CryptoInputParcel());
}
- protected abstract void cryptoOperation(CryptoInputParcel cryptoInput);
+ @SuppressWarnings("unused") // it's an EventBus method
+ public void onEventMainThread(ProgressEvent event) {
- protected void onCryptoOperationCancelled() {
- // Nothing to do here, in most cases
+ ProgressDialogFragment progressDialogFragment =
+ (ProgressDialogFragment) getFragmentManager().findFragmentByTag("progressDialog");
+
+ if (progressDialogFragment == null) {
+ return;
+ }
+
+ progressDialogFragment.setProgress(event.mMessage, event.mProgress, event.mMax);
}
+
}