From 1baae3775e9fe60b39dc30463b0a33bd8d2d14df Mon Sep 17 00:00:00 2001 From: Jessica Yuen Date: Sun, 2 Mar 2014 15:06:43 -0500 Subject: #226: Added cancel option to ProgressDialogFragments. Create Key is now cancelable. --- .../service/KeychainIntentServiceHandler.java | 9 ++++++- .../keychain/ui/EditKeyActivity.java | 4 ++- .../ui/dialog/DeleteFileDialogFragment.java | 2 +- .../keychain/ui/dialog/ProgressDialogFragment.java | 30 ++++++++++++++++++---- .../keychain/ui/widget/SectionView.java | 2 +- 5 files changed, 38 insertions(+), 9 deletions(-) (limited to 'OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java index dfea7eb04..530bbc7ac 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java @@ -21,6 +21,8 @@ import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.R; import android.app.Activity; +import android.content.DialogInterface; +import android.content.DialogInterface.OnCancelListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; @@ -55,9 +57,14 @@ public class KeychainIntentServiceHandler extends Handler { } public KeychainIntentServiceHandler(Activity activity, int progressDialogMessageId, int progressDialogStyle) { + this(activity, progressDialogMessageId, progressDialogStyle, false); + } + + public KeychainIntentServiceHandler(Activity activity, int progressDialogMessageId, + int progressDialogStyle, boolean cancelable) { this.mActivity = activity; this.mProgressDialogFragment = ProgressDialogFragment.newInstance(progressDialogMessageId, - progressDialogStyle); + progressDialogStyle, cancelable); } public void showProgressDialog(FragmentActivity activity) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java index 73426e32d..cb76e37c6 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java @@ -185,7 +185,9 @@ public class EditKeyActivity extends ActionBarActivity { // Message is received after generating is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( - this, R.string.progress_generating, ProgressDialog.STYLE_SPINNER) { + this, R.string.progress_generating, ProgressDialog.STYLE_SPINNER, true) { + + @Override public void handleMessage(Message message) { // handle messages by standard ApgHandler first super.handleMessage(message); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java index a859bb045..8d428edd6 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java @@ -83,7 +83,7 @@ public class DeleteFileDialogFragment extends DialogFragment { intent.putExtra(KeychainIntentService.EXTRA_DATA, data); ProgressDialogFragment deletingDialog = ProgressDialogFragment.newInstance( - R.string.progress_deleting_securely, ProgressDialog.STYLE_HORIZONTAL); + R.string.progress_deleting_securely, ProgressDialog.STYLE_HORIZONTAL, false); // Message is received after deleting is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(activity, deletingDialog) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java index 9b5d233e8..cac257c85 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java @@ -26,21 +26,28 @@ import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.KeyEvent; +import org.sufficientlysecure.keychain.R; + public class ProgressDialogFragment extends DialogFragment { private static final String ARG_MESSAGE_ID = "message_id"; private static final String ARG_STYLE = "style"; + private static final String ARG_CANCELABLE = "cancelable"; /** * Creates new instance of this fragment * - * @param id + * @param messageId + * @param style + * @param cancelable * @return */ - public static ProgressDialogFragment newInstance(int messageId, int style) { + public static ProgressDialogFragment newInstance(int messageId, int style, + boolean cancelable) { ProgressDialogFragment frag = new ProgressDialogFragment(); Bundle args = new Bundle(); args.putInt(ARG_MESSAGE_ID, messageId); args.putInt(ARG_STYLE, style); + args.putBoolean(ARG_CANCELABLE, cancelable); frag.setArguments(args); return frag; @@ -60,7 +67,6 @@ public class ProgressDialogFragment extends DialogFragment { /** * Updates progress of dialog * - * @param messageId * @param progress * @param max */ @@ -74,7 +80,7 @@ public class ProgressDialogFragment extends DialogFragment { /** * Updates progress of dialog * - * @param messageId + * @param message * @param progress * @param max */ @@ -91,7 +97,7 @@ public class ProgressDialogFragment extends DialogFragment { */ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - Activity activity = getActivity(); + final Activity activity = getActivity(); ProgressDialog dialog = new ProgressDialog(activity); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); @@ -100,10 +106,24 @@ public class ProgressDialogFragment extends DialogFragment { int messageId = getArguments().getInt(ARG_MESSAGE_ID); int style = getArguments().getInt(ARG_STYLE); + boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE); dialog.setMessage(getString(messageId)); dialog.setProgressStyle(style); + if (cancelable) { + dialog.setButton(DialogInterface.BUTTON_NEGATIVE, + activity.getString(R.string.progress_cancel), + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + activity.setResult(Activity.RESULT_CANCELED); + activity.finish(); + } + }); + } + // Disable the back button OnKeyListener keyListener = new OnKeyListener() { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java index 57865b032..038b8a613 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java @@ -238,7 +238,7 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor // show progress dialog mGeneratingDialog = ProgressDialogFragment.newInstance(R.string.progress_generating, - ProgressDialog.STYLE_SPINNER); + ProgressDialog.STYLE_SPINNER, true); // Message is received after generating is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(mActivity, -- cgit v1.2.3 From 91a2ecb15ce5acd720c8f23c3a09e60aad2baa1a Mon Sep 17 00:00:00 2001 From: Jessica Yuen Date: Sun, 2 Mar 2014 17:33:21 -0500 Subject: #226: Activity no longer closes if progress is canceled for key creation in EditKeyActivity --- .../keychain/service/KeychainIntentServiceHandler.java | 7 ++++--- .../sufficientlysecure/keychain/ui/EditKeyActivity.java | 2 +- .../keychain/ui/dialog/DeleteFileDialogFragment.java | 2 +- .../keychain/ui/dialog/ProgressDialogFragment.java | 14 ++++++++++---- .../sufficientlysecure/keychain/ui/widget/SectionView.java | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) (limited to 'OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java index 530bbc7ac..5711be596 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java @@ -57,14 +57,15 @@ public class KeychainIntentServiceHandler extends Handler { } public KeychainIntentServiceHandler(Activity activity, int progressDialogMessageId, int progressDialogStyle) { - this(activity, progressDialogMessageId, progressDialogStyle, false); + this(activity, progressDialogMessageId, progressDialogStyle, false, false); } public KeychainIntentServiceHandler(Activity activity, int progressDialogMessageId, - int progressDialogStyle, boolean cancelable) { + int progressDialogStyle, boolean cancelable, + boolean finishActivityOnCancel) { this.mActivity = activity; this.mProgressDialogFragment = ProgressDialogFragment.newInstance(progressDialogMessageId, - progressDialogStyle, cancelable); + progressDialogStyle, cancelable, finishActivityOnCancel); } public void showProgressDialog(FragmentActivity activity) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java index cb76e37c6..09b258fcb 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java @@ -185,7 +185,7 @@ public class EditKeyActivity extends ActionBarActivity { // Message is received after generating is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( - this, R.string.progress_generating, ProgressDialog.STYLE_SPINNER, true) { + this, R.string.progress_generating, ProgressDialog.STYLE_SPINNER, true, true) { @Override public void handleMessage(Message message) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java index 8d428edd6..dc4384886 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java @@ -83,7 +83,7 @@ public class DeleteFileDialogFragment extends DialogFragment { intent.putExtra(KeychainIntentService.EXTRA_DATA, data); ProgressDialogFragment deletingDialog = ProgressDialogFragment.newInstance( - R.string.progress_deleting_securely, ProgressDialog.STYLE_HORIZONTAL, false); + R.string.progress_deleting_securely, ProgressDialog.STYLE_HORIZONTAL, false, false); // Message is received after deleting is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(activity, deletingDialog) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java index cac257c85..0bfd4185e 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java @@ -32,6 +32,7 @@ public class ProgressDialogFragment extends DialogFragment { private static final String ARG_MESSAGE_ID = "message_id"; private static final String ARG_STYLE = "style"; private static final String ARG_CANCELABLE = "cancelable"; + private static final String ARG_FINISH_ON_CANCEL = "finish_activity_on_cancel"; /** * Creates new instance of this fragment @@ -41,13 +42,14 @@ public class ProgressDialogFragment extends DialogFragment { * @param cancelable * @return */ - public static ProgressDialogFragment newInstance(int messageId, int style, - boolean cancelable) { + public static ProgressDialogFragment newInstance(int messageId, int style, boolean cancelable, + boolean finishActivityOnCancel) { ProgressDialogFragment frag = new ProgressDialogFragment(); Bundle args = new Bundle(); args.putInt(ARG_MESSAGE_ID, messageId); args.putInt(ARG_STYLE, style); args.putBoolean(ARG_CANCELABLE, cancelable); + args.putBoolean(ARG_FINISH_ON_CANCEL, finishActivityOnCancel); frag.setArguments(args); return frag; @@ -118,8 +120,12 @@ public class ProgressDialogFragment extends DialogFragment { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); - activity.setResult(Activity.RESULT_CANCELED); - activity.finish(); + + boolean finishActivity = getArguments().getBoolean(ARG_FINISH_ON_CANCEL); + if (finishActivity) { + activity.setResult(Activity.RESULT_CANCELED); + activity.finish(); + } } }); } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java index 038b8a613..89d1e187c 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java @@ -238,7 +238,7 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor // show progress dialog mGeneratingDialog = ProgressDialogFragment.newInstance(R.string.progress_generating, - ProgressDialog.STYLE_SPINNER, true); + ProgressDialog.STYLE_SPINNER, true, false); // Message is received after generating is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(mActivity, -- cgit v1.2.3 From e4e3c555e9ac89cffad594dfc922715309b3b299 Mon Sep 17 00:00:00 2001 From: Jessica Yuen Date: Sun, 2 Mar 2014 20:55:13 -0500 Subject: #226: Async key gen task is stopped if progress is canceled --- .../service/KeychainIntentServiceHandler.java | 6 +++--- .../keychain/ui/EditKeyActivity.java | 16 +++++++++++++-- .../ui/dialog/DeleteFileDialogFragment.java | 2 +- .../keychain/ui/dialog/ProgressDialogFragment.java | 23 +++++++++++++--------- .../keychain/ui/widget/SectionView.java | 10 ++++++++-- 5 files changed, 40 insertions(+), 17 deletions(-) (limited to 'OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java index 5711be596..6eba9cc83 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java @@ -57,15 +57,15 @@ public class KeychainIntentServiceHandler extends Handler { } public KeychainIntentServiceHandler(Activity activity, int progressDialogMessageId, int progressDialogStyle) { - this(activity, progressDialogMessageId, progressDialogStyle, false, false); + this(activity, progressDialogMessageId, progressDialogStyle, false, null); } public KeychainIntentServiceHandler(Activity activity, int progressDialogMessageId, int progressDialogStyle, boolean cancelable, - boolean finishActivityOnCancel) { + OnCancelListener onCancelListener) { this.mActivity = activity; this.mProgressDialogFragment = ProgressDialogFragment.newInstance(progressDialogMessageId, - progressDialogStyle, cancelable, finishActivityOnCancel); + progressDialogStyle, cancelable, onCancelListener); } public void showProgressDialog(FragmentActivity activity) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java index 09b258fcb..0bed6f264 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java @@ -44,8 +44,10 @@ import org.sufficientlysecure.keychain.ui.widget.UserIdEditor; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; +import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -173,7 +175,7 @@ public class EditKeyActivity extends ActionBarActivity { if (generateDefaultKeys) { // Send all information needed to service generate keys in other thread - Intent serviceIntent = new Intent(this, KeychainIntentService.class); + final Intent serviceIntent = new Intent(this, KeychainIntentService.class); serviceIntent.setAction(KeychainIntentService.ACTION_GENERATE_DEFAULT_RSA_KEYS); // fill values for this action @@ -185,7 +187,17 @@ public class EditKeyActivity extends ActionBarActivity { // Message is received after generating is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( - this, R.string.progress_generating, ProgressDialog.STYLE_SPINNER, true, true) { + this, R.string.progress_generating, ProgressDialog.STYLE_SPINNER, true, + + new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + // Stop key generation on cancel + stopService(serviceIntent); + EditKeyActivity.this.setResult(Activity.RESULT_CANCELED); + EditKeyActivity.this.finish(); + } + }) { @Override public void handleMessage(Message message) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java index dc4384886..cd8bc79a9 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/DeleteFileDialogFragment.java @@ -83,7 +83,7 @@ public class DeleteFileDialogFragment extends DialogFragment { intent.putExtra(KeychainIntentService.EXTRA_DATA, data); ProgressDialogFragment deletingDialog = ProgressDialogFragment.newInstance( - R.string.progress_deleting_securely, ProgressDialog.STYLE_HORIZONTAL, false, false); + R.string.progress_deleting_securely, ProgressDialog.STYLE_HORIZONTAL, false, null); // Message is received after deleting is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(activity, deletingDialog) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java index 0bfd4185e..6c62d14e0 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java @@ -21,6 +21,7 @@ import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; +import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnKeyListener; import android.os.Bundle; import android.support.v4.app.DialogFragment; @@ -32,7 +33,8 @@ public class ProgressDialogFragment extends DialogFragment { private static final String ARG_MESSAGE_ID = "message_id"; private static final String ARG_STYLE = "style"; private static final String ARG_CANCELABLE = "cancelable"; - private static final String ARG_FINISH_ON_CANCEL = "finish_activity_on_cancel"; + + private OnCancelListener mOnCancelListener; /** * Creates new instance of this fragment @@ -43,15 +45,16 @@ public class ProgressDialogFragment extends DialogFragment { * @return */ public static ProgressDialogFragment newInstance(int messageId, int style, boolean cancelable, - boolean finishActivityOnCancel) { + OnCancelListener onCancelListener) { ProgressDialogFragment frag = new ProgressDialogFragment(); Bundle args = new Bundle(); args.putInt(ARG_MESSAGE_ID, messageId); args.putInt(ARG_STYLE, style); args.putBoolean(ARG_CANCELABLE, cancelable); - args.putBoolean(ARG_FINISH_ON_CANCEL, finishActivityOnCancel); frag.setArguments(args); + frag.mOnCancelListener = onCancelListener; + return frag; } @@ -94,6 +97,14 @@ public class ProgressDialogFragment extends DialogFragment { dialog.setMax(max); } + @Override + public void onCancel(DialogInterface dialog) { + super.onCancel(dialog); + + if (this.mOnCancelListener != null) + this.mOnCancelListener.onCancel(dialog); + } + /** * Creates dialog */ @@ -120,12 +131,6 @@ public class ProgressDialogFragment extends DialogFragment { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); - - boolean finishActivity = getArguments().getBoolean(ARG_FINISH_ON_CANCEL); - if (finishActivity) { - activity.setResult(Activity.RESULT_CANCELED); - activity.finish(); - } } }); } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java index 89d1e187c..a95d80a4e 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java @@ -18,6 +18,7 @@ package org.sufficientlysecure.keychain.ui.widget; import android.app.ProgressDialog; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Message; @@ -211,7 +212,7 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor private void createKey() { // Send all information needed to service to edit key in other thread - Intent intent = new Intent(mActivity, KeychainIntentService.class); + final Intent intent = new Intent(mActivity, KeychainIntentService.class); intent.setAction(KeychainIntentService.ACTION_GENERATE_KEY); @@ -238,7 +239,12 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor // show progress dialog mGeneratingDialog = ProgressDialogFragment.newInstance(R.string.progress_generating, - ProgressDialog.STYLE_SPINNER, true, false); + ProgressDialog.STYLE_SPINNER, true, new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + mActivity.stopService(intent); + } + }); // Message is received after generating is done in ApgService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(mActivity, -- cgit v1.2.3 From af6713dc78448b7686a546d77c022f4aacd58dfb Mon Sep 17 00:00:00 2001 From: Jessica Yuen Date: Mon, 3 Mar 2014 23:04:55 -0500 Subject: #226: Small fix to prevent message from being sent if IntentService is canceled --- .../keychain/service/KeychainIntentService.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 3904a91d8..302dbea0b 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -203,10 +203,18 @@ public class KeychainIntentService extends IntentService implements ProgressDial Messenger mMessenger; + private boolean mIsCanceled; + public KeychainIntentService() { super("ApgService"); } + @Override + public void onDestroy() { + super.onDestroy(); + this.mIsCanceled = true; + } + /** * The IntentService calls this method from the default worker thread with the intent that * started the service. When this method returns, IntentService stops the service, as @@ -815,6 +823,10 @@ public class KeychainIntentService extends IntentService implements ProgressDial } private void sendErrorToHandler(Exception e) { + // Service was canceled. Do not send error to handler. + if (this.mIsCanceled) + return; + Log.e(Constants.TAG, "ApgService Exception: ", e); e.printStackTrace(); @@ -824,6 +836,10 @@ public class KeychainIntentService extends IntentService implements ProgressDial } private void sendMessageToHandler(Integer arg1, Integer arg2, Bundle data) { + // Service was canceled. Do not send message to handler. + if (this.mIsCanceled) + return; + Message msg = Message.obtain(); msg.arg1 = arg1; if (arg2 != null) { -- cgit v1.2.3