#226: Async key gen task is stopped if progress is canceled

This commit is contained in:
Jessica Yuen 2014-03-02 20:55:13 -05:00
parent 91a2ecb15c
commit e4e3c555e9
5 changed files with 40 additions and 17 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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();
}
}
});
}

View File

@ -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,