diff options
author | Vincent Breitmoser <valodim@mugenguild.com> | 2015-06-10 17:12:22 +0200 |
---|---|---|
committer | Vincent Breitmoser <valodim@mugenguild.com> | 2015-06-10 17:12:22 +0200 |
commit | 1697e22d1954d90289bac06abe23778c371337da (patch) | |
tree | 39820a156392e15be5fb7d8d33cf6a23aa1c6625 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service | |
parent | d43671b2ed42dba8c0ab8c43a5d171bf842e2782 (diff) | |
parent | 8da88f33bc3991653f91ccfd589cded23a9653b1 (diff) | |
download | open-keychain-1697e22d1954d90289bac06abe23778c371337da.tar.gz open-keychain-1697e22d1954d90289bac06abe23778c371337da.tar.bz2 open-keychain-1697e22d1954d90289bac06abe23778c371337da.zip |
Merge branch 'v/eventbus' into v/multi-decrypt
Conflicts:
OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/base/CryptoOperationFragment.java
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service')
4 files changed, 108 insertions, 99 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainNewService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainNewService.java index 5ef475029..9e33a1421 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainNewService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainNewService.java @@ -25,9 +25,11 @@ import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; +import android.os.Message; +import android.os.Messenger; import android.os.Parcelable; +import android.os.RemoteException; -import de.greenrobot.event.EventBus; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.operations.BaseOperation; import org.sufficientlysecure.keychain.operations.CertifyOperation; @@ -40,6 +42,7 @@ import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.SignEncryptParcel; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.CertifyActionsParcel.CertifyAction; +import org.sufficientlysecure.keychain.service.ServiceProgressHandler.MessageStatus; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.util.Log; @@ -49,13 +52,18 @@ import org.sufficientlysecure.keychain.util.Log; */ public class KeychainNewService extends Service implements Progressable { - /* extras that can be given by intent */ + // messenger for communication (hack) + public static final String EXTRA_MESSENGER = "messenger"; + + // extras for operation public static final String EXTRA_OPERATION_INPUT = "op_input"; public static final String EXTRA_CRYPTO_INPUT = "crypto_input"; // this attribute can possibly merged with the one above? not sure... private AtomicBoolean mActionCanceled = new AtomicBoolean(false); + ThreadLocal<Messenger> mMessenger = new ThreadLocal<>(); + @Override public IBinder onBind(Intent intent) { return null; @@ -66,56 +74,82 @@ public class KeychainNewService extends Service implements Progressable { */ @Override public int onStartCommand(final Intent intent, int flags, int startId) { - EventBus bus = EventBus.getDefault(); - if (!bus.isRegistered(this)) { - bus.register(this); - } - Bundle extras = intent.getExtras(); - if (extras != null) { - bus.post(extras); - } else { - Log.e(Constants.TAG, "Extras bundle is null!"); - } + Runnable actionRunnable = new Runnable() { + @Override + public void run() { + // We have not been cancelled! (yet) + mActionCanceled.set(false); + + Bundle extras = intent.getExtras(); + + // Set messenger for communication (for this particular thread) + mMessenger.set(extras.<Messenger>getParcelable(EXTRA_MESSENGER)); + + // Input + Parcelable inputParcel = extras.getParcelable(EXTRA_OPERATION_INPUT); + CryptoInputParcel cryptoInput = extras.getParcelable(EXTRA_CRYPTO_INPUT); + + // Operation + BaseOperation op; + + // just for brevity + KeychainNewService outerThis = KeychainNewService.this; + if (inputParcel instanceof SignEncryptParcel) { + op = new SignEncryptOperation(outerThis, new ProviderHelper(outerThis), outerThis, mActionCanceled); + } else if (inputParcel instanceof PgpDecryptVerifyInputParcel) { + op = new PgpDecryptVerify(outerThis, new ProviderHelper(outerThis), outerThis); + } else if (inputParcel instanceof SaveKeyringParcel) { + op = new EditKeyOperation(outerThis, new ProviderHelper(outerThis), outerThis, mActionCanceled); + } else if (inputParcel instanceof CertifyAction) { + op = new CertifyOperation(outerThis, new ProviderHelper(outerThis), outerThis, mActionCanceled); + } else { + return; + } + + @SuppressWarnings("unchecked") // this is unchecked, we make sure it's the correct op above! + OperationResult result = op.execute(inputParcel, cryptoInput); + + sendMessageToHandler(MessageStatus.OKAY, result); + + } + }; + + Thread actionThread = new Thread(actionRunnable); + actionThread.start(); return START_NOT_STICKY; } - @Override - public void onDestroy() { - super.onDestroy(); - EventBus.getDefault().unregister(this); - } + private void sendMessageToHandler(MessageStatus status, Integer arg2, Bundle data) { - public void onEventAsync(Bundle bundle) { - - // Input - Parcelable inputParcel = bundle.getParcelable(EXTRA_OPERATION_INPUT); - CryptoInputParcel cryptoInput = bundle.getParcelable(EXTRA_CRYPTO_INPUT); - - // Operation - BaseOperation op; - - if (inputParcel instanceof SignEncryptParcel) { - op = new SignEncryptOperation(this, new ProviderHelper(this), this, mActionCanceled); - } else if (inputParcel instanceof PgpDecryptVerifyInputParcel) { - op = new PgpDecryptVerify(this, new ProviderHelper(this), this); - } else if (inputParcel instanceof SaveKeyringParcel) { - op = new EditKeyOperation(this, new ProviderHelper(this), this, mActionCanceled); - } else if (inputParcel instanceof CertifyAction) { - op = new CertifyOperation(this, new ProviderHelper(this), this, mActionCanceled); - } else { - return; + Message msg = Message.obtain(); + assert msg != null; + msg.arg1 = status.ordinal(); + if (arg2 != null) { + msg.arg2 = arg2; + } + if (data != null) { + msg.setData(data); } - @SuppressWarnings("unchecked") // this is unchecked, we make sure it's the correct op above! - OperationResult result = op.execute(inputParcel, cryptoInput); - - // Result - EventBus.getDefault().post(result); + try { + mMessenger.get().send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } - stopSelf(); + private void sendMessageToHandler(MessageStatus status, OperationResult data) { + Bundle bundle = new Bundle(); + bundle.putParcelable(OperationResult.EXTRA_RESULT, data); + sendMessageToHandler(status, null, bundle); + } + private void sendMessageToHandler(MessageStatus status) { + sendMessageToHandler(status, null, null); } /** @@ -126,9 +160,14 @@ public class KeychainNewService extends Service implements Progressable { Log.d(Constants.TAG, "Send message by setProgress with progress=" + progress + ", max=" + max); - ProgressEvent event = new ProgressEvent(message, progress, max); - EventBus.getDefault().post(event); + Bundle data = new Bundle(); + if (message != null) { + data.putString(ServiceProgressHandler.DATA_MESSAGE, message); + } + data.putInt(ServiceProgressHandler.DATA_PROGRESS, progress); + data.putInt(ServiceProgressHandler.DATA_PROGRESS_MAX, max); + sendMessageToHandler(MessageStatus.UPDATE_PROGRESS, null, data); } @Override @@ -143,8 +182,7 @@ public class KeychainNewService extends Service implements Progressable { @Override public void setPreventCancel() { - // sendMessageToHandler(MessageStatus.PREVENT_CANCEL); + sendMessageToHandler(MessageStatus.PREVENT_CANCEL); } - } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainService.java index e4bfd7dee..ba877c2a2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainService.java @@ -30,7 +30,6 @@ import android.os.RemoteException; import com.textuality.keybase.lib.Proof; import com.textuality.keybase.lib.prover.Prover; -import de.greenrobot.event.EventBus; import org.json.JSONObject; import org.spongycastle.openpgp.PGPUtil; import org.sufficientlysecure.keychain.Constants; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ProgressEvent.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ProgressEvent.java deleted file mode 100644 index d441eccd0..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ProgressEvent.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.sufficientlysecure.keychain.service; - - -public class ProgressEvent { - - public final String mMessage; - public final int mProgress; - public final int mMax; - - public ProgressEvent(String message, int progress, int max) { - mMessage = message; - mProgress = progress; - mMax = max; - } - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ServiceProgressHandler.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ServiceProgressHandler.java index a0bb250d4..76aa1a618 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ServiceProgressHandler.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/ServiceProgressHandler.java @@ -17,8 +17,7 @@ package org.sufficientlysecure.keychain.service; -import android.app.Activity; -import android.content.Intent; +import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -27,7 +26,6 @@ import android.support.v4.app.FragmentManager; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.operations.results.CertifyResult; import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.util.Log; @@ -65,68 +63,58 @@ public class ServiceProgressHandler extends Handler { public static final String KEYBASE_PRESENCE_URL = "keybase_presence_url"; public static final String KEYBASE_PRESENCE_LABEL = "keybase_presence_label"; - Activity mActivity; - ProgressDialogFragment mProgressDialogFragment; + FragmentActivity mActivity; - public ServiceProgressHandler(Activity activity) { - this.mActivity = activity; + public ServiceProgressHandler(FragmentActivity activity) { + mActivity = activity; } - public ServiceProgressHandler(Activity activity, ProgressDialogFragment progressDialogFragment) { - this.mActivity = activity; - this.mProgressDialogFragment = progressDialogFragment; + public void showProgressDialog() { + showProgressDialog("", ProgressDialog.STYLE_SPINNER, false); } - public ServiceProgressHandler(Activity activity, String progressDialogMessage, int progressDialogStyle) { - this(activity, progressDialogMessage, progressDialogStyle, false); - } + public void showProgressDialog( + String progressDialogMessage, int progressDialogStyle, boolean cancelable) { - public ServiceProgressHandler(Activity activity, - String progressDialogMessage, - int progressDialogStyle, - boolean cancelable) { - this.mActivity = activity; - this.mProgressDialogFragment = ProgressDialogFragment.newInstance( + final ProgressDialogFragment frag = ProgressDialogFragment.newInstance( progressDialogMessage, progressDialogStyle, cancelable); - } - - public void showProgressDialog(FragmentActivity activity) { - if (mProgressDialogFragment == null) { - return; - } // TODO: This is a hack!, see // http://stackoverflow.com/questions/10114324/show-dialogfragment-from-onactivityresult - final FragmentManager manager = activity.getSupportFragmentManager(); + final FragmentManager manager = mActivity.getSupportFragmentManager(); Handler handler = new Handler(); handler.post(new Runnable() { public void run() { - mProgressDialogFragment.show(manager, "progressDialog"); + frag.show(manager, "progressDialog"); } }); + } @Override public void handleMessage(Message message) { Bundle data = message.getData(); - if (mProgressDialogFragment == null) { - // Log.e(Constants.TAG, - // "Progress has not been updated because mProgressDialogFragment was null!"); + ProgressDialogFragment progressDialogFragment = + (ProgressDialogFragment) mActivity.getSupportFragmentManager() + .findFragmentByTag("progressDialog"); + + if (progressDialogFragment == null) { + Log.e(Constants.TAG, "Progress has not been updated because mProgressDialogFragment was null!"); return; } MessageStatus status = MessageStatus.fromInt(message.arg1); switch (status) { case OKAY: - mProgressDialogFragment.dismissAllowingStateLoss(); + progressDialogFragment.dismissAllowingStateLoss(); break; case EXCEPTION: - mProgressDialogFragment.dismissAllowingStateLoss(); + progressDialogFragment.dismissAllowingStateLoss(); // show error from service if (data.containsKey(DATA_ERROR)) { @@ -142,13 +130,13 @@ public class ServiceProgressHandler extends Handler { // update progress from service if (data.containsKey(DATA_MESSAGE)) { - mProgressDialogFragment.setProgress(data.getString(DATA_MESSAGE), + progressDialogFragment.setProgress(data.getString(DATA_MESSAGE), data.getInt(DATA_PROGRESS), data.getInt(DATA_PROGRESS_MAX)); } else if (data.containsKey(DATA_MESSAGE_ID)) { - mProgressDialogFragment.setProgress(data.getInt(DATA_MESSAGE_ID), + progressDialogFragment.setProgress(data.getInt(DATA_MESSAGE_ID), data.getInt(DATA_PROGRESS), data.getInt(DATA_PROGRESS_MAX)); } else { - mProgressDialogFragment.setProgress(data.getInt(DATA_PROGRESS), + progressDialogFragment.setProgress(data.getInt(DATA_PROGRESS), data.getInt(DATA_PROGRESS_MAX)); } } @@ -156,7 +144,7 @@ public class ServiceProgressHandler extends Handler { break; case PREVENT_CANCEL: - mProgressDialogFragment.setPreventCancel(true); + progressDialogFragment.setPreventCancel(true); break; default: |