aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2014-08-31 20:05:19 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2014-08-31 20:05:19 +0200
commite9a2f256b9324cc898061d2a2ebde18446ce1dd1 (patch)
tree4d63a5c212f4996dce4eeeac8c45615455f6b0b1 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java
parentd17b478a9ea13d3fd11cc7112f60cd9de43ec8db (diff)
downloadopen-keychain-e9a2f256b9324cc898061d2a2ebde18446ce1dd1.tar.gz
open-keychain-e9a2f256b9324cc898061d2a2ebde18446ce1dd1.tar.bz2
open-keychain-e9a2f256b9324cc898061d2a2ebde18446ce1dd1.zip
add cancel prevention mechanism, improve cancellation for key import
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java45
1 files changed, 41 insertions, 4 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java
index 49a8ac49f..d09be2d51 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java
@@ -23,6 +23,7 @@ import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
+import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.ContextThemeWrapper;
@@ -39,6 +40,8 @@ public class ProgressDialogFragment extends DialogFragment {
private static final String ARG_STYLE = "style";
private static final String ARG_CANCELABLE = "cancelable";
+ boolean mCanCancel = false, mPreventCancel = false, mIsCancelled = false;
+
/** Creates new instance of this fragment */
public static ProgressDialogFragment newInstance(String message, int style, boolean cancelable) {
ProgressDialogFragment frag = new ProgressDialogFragment();
@@ -59,6 +62,10 @@ public class ProgressDialogFragment extends DialogFragment {
/** Updates progress of dialog */
public void setProgress(int progress, int max) {
+ if (mIsCancelled) {
+ return;
+ }
+
ProgressDialog dialog = (ProgressDialog) getDialog();
dialog.setProgress(progress);
@@ -67,6 +74,10 @@ public class ProgressDialogFragment extends DialogFragment {
/** Updates progress of dialog */
public void setProgress(String message, int progress, int max) {
+ if (mIsCancelled) {
+ return;
+ }
+
ProgressDialog dialog = (ProgressDialog) getDialog();
dialog.setMessage(message);
@@ -94,13 +105,13 @@ public class ProgressDialogFragment extends DialogFragment {
String message = getArguments().getString(ARG_MESSAGE);
int style = getArguments().getInt(ARG_STYLE);
- final boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE);
+ mCanCancel = getArguments().getBoolean(ARG_CANCELABLE);
dialog.setMessage(message);
dialog.setProgressStyle(style);
// If this is supposed to be cancelable, add our (custom) cancel mechanic
- if (cancelable) {
+ if (mCanCancel) {
// Just show the button, take care of the onClickListener afterwards (in onStart)
dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
activity.getString(R.string.progress_cancel), (DialogInterface.OnClickListener) null);
@@ -111,7 +122,7 @@ public class ProgressDialogFragment extends DialogFragment {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
- if (cancelable) {
+ if (mCanCancel) {
((ProgressDialog) dialog).getButton(
DialogInterface.BUTTON_NEGATIVE).performClick();
}
@@ -126,15 +137,41 @@ public class ProgressDialogFragment extends DialogFragment {
return dialog;
}
+ public void setPreventCancel(boolean preventCancel) {
+ // Don't care if we can't cancel anymore either way!
+ if (mIsCancelled || ! mCanCancel) {
+ return;
+ }
+
+ mPreventCancel = preventCancel;
+ final Button negative = ((ProgressDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
+ negative.setVisibility(preventCancel ? View.GONE : View.VISIBLE);
+ }
+
@Override
public void onStart() {
super.onStart();
// Override the default behavior so the dialog is NOT dismissed on click
- Button negative = ((ProgressDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
+ final Button negative = ((ProgressDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
negative.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
+ // nvm if we are already cancelled, or weren't able to begin with
+ if (mIsCancelled || ! mCanCancel) {
+ return;
+ }
+
+ // Remember this, and don't allow another click
+ mIsCancelled = true;
+ negative.setClickable(false);
+ negative.setTextColor(Color.GRAY);
+
+ // Set the progress bar accordingly
+ ProgressDialog dialog = (ProgressDialog) getDialog();
+ dialog.setIndeterminate(true);
+ dialog.setMessage(getString(R.string.progress_cancelling));
+
// send a cancel message. note that this message will be handled by
// KeychainIntentService.onStartCommand, which runs in this thread,
// not the service one, and will not queue up a command.