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 17:32:13 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2014-08-31 17:32:13 +0200
commit7da783228499ea9d5b0a98201e8ba5b17e30bb10 (patch)
tree610e3d5ef33d717eaa56d091ebe7d92c5b237bbb /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ProgressDialogFragment.java
parent38c6cf045c6e451b3a150bf9b659056c2252d27c (diff)
downloadopen-keychain-7da783228499ea9d5b0a98201e8ba5b17e30bb10.tar.gz
open-keychain-7da783228499ea9d5b0a98201e8ba5b17e30bb10.tar.bz2
open-keychain-7da783228499ea9d5b0a98201e8ba5b17e30bb10.zip
Add cancelable mechanism and support in key import
Closes #323
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.java96
1 files changed, 42 insertions, 54 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 0324dd3b9..49a8ac49f 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
@@ -21,32 +21,26 @@ 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.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
import org.sufficientlysecure.keychain.R;
+import org.sufficientlysecure.keychain.service.KeychainIntentService;
public class ProgressDialogFragment extends DialogFragment {
private static final String ARG_MESSAGE = "message";
private static final String ARG_STYLE = "style";
private static final String ARG_CANCELABLE = "cancelable";
- private OnCancelListener mOnCancelListener;
-
- /**
- * Creates new instance of this fragment
- *
- * @param message
- * @param style
- * @param cancelable
- * @return
- */
- public static ProgressDialogFragment newInstance(String message, int style, boolean cancelable,
- OnCancelListener onCancelListener) {
+ /** Creates new instance of this fragment */
+ public static ProgressDialogFragment newInstance(String message, int style, boolean cancelable) {
ProgressDialogFragment frag = new ProgressDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_MESSAGE, message);
@@ -54,28 +48,16 @@ public class ProgressDialogFragment extends DialogFragment {
args.putBoolean(ARG_CANCELABLE, cancelable);
frag.setArguments(args);
- frag.mOnCancelListener = onCancelListener;
return frag;
}
- /**
- * Updates progress of dialog
- *
- * @param messageId
- * @param progress
- * @param max
- */
+ /** Updates progress of dialog */
public void setProgress(int messageId, int progress, int max) {
setProgress(getString(messageId), progress, max);
}
- /**
- * Updates progress of dialog
- *
- * @param progress
- * @param max
- */
+ /** Updates progress of dialog */
public void setProgress(int progress, int max) {
ProgressDialog dialog = (ProgressDialog) getDialog();
@@ -83,13 +65,7 @@ public class ProgressDialogFragment extends DialogFragment {
dialog.setMax(max);
}
- /**
- * Updates progress of dialog
- *
- * @param message
- * @param progress
- * @param max
- */
+ /** Updates progress of dialog */
public void setProgress(String message, int progress, int max) {
ProgressDialog dialog = (ProgressDialog) getDialog();
@@ -98,15 +74,6 @@ 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
*/
@@ -121,41 +88,62 @@ public class ProgressDialogFragment extends DialogFragment {
ProgressDialog dialog = new ProgressDialog(context);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+ // We never use the builtin cancel method
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
String message = getArguments().getString(ARG_MESSAGE);
int style = getArguments().getInt(ARG_STYLE);
- boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE);
+ final boolean cancelable = getArguments().getBoolean(ARG_CANCELABLE);
dialog.setMessage(message);
dialog.setProgressStyle(style);
+ // If this is supposed to be cancelable, add our (custom) cancel mechanic
if (cancelable) {
+ // Just show the button, take care of the onClickListener afterwards (in onStart)
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.getString(R.string.progress_cancel), (DialogInterface.OnClickListener) null);
}
- // Disable the back button
+ // Disable the back button regardless
OnKeyListener keyListener = new OnKeyListener() {
-
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
+ if (cancelable) {
+ ((ProgressDialog) dialog).getButton(
+ DialogInterface.BUTTON_NEGATIVE).performClick();
+ }
+ // return true, indicating we handled this
return true;
}
return false;
}
-
};
dialog.setOnKeyListener(keyListener);
return dialog;
}
+
+ @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);
+ negative.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ // 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.
+ Intent intent = new Intent(getActivity(), KeychainIntentService.class);
+ intent.setAction(KeychainIntentService.ACTION_CANCEL);
+ getActivity().startService(intent);
+ }
+ });
+
+ }
+
}