1 package com.jm.utility;
\r
3 import java.lang.ref.WeakReference;
\r
5 import android.app.Activity;
\r
6 import android.app.ProgressDialog;
\r
7 import android.content.DialogInterface;
\r
8 import android.content.DialogInterface.OnKeyListener;
\r
9 import android.os.AsyncTask;
\r
10 import android.view.KeyEvent;
\r
14 public class AsyncTaskCommand extends AsyncTask<String, Integer, String> {
\r
15 private WeakReference<Activity> activityRef;
\r
16 private AsyncTaskIF command;
\r
17 private ProgressDialog dialog;
\r
18 private boolean isCancel = false;
\r
20 @SuppressWarnings({ "rawtypes", "unchecked" })
\r
21 public AsyncTaskCommand(Activity activity, AsyncTaskIF command){
\r
22 this.activityRef = new WeakReference(activity);
\r
23 this.command = command;
\r
27 protected void onPreExecute(){
\r
28 Debug.d(this, "onPreExecute start");
\r
30 Activity activity = activityRef.get();
\r
31 if (activity == null){
\r
34 dialog = new ProgressDialog(activity);
\r
35 dialog.setIndeterminate(true);
\r
36 dialog.setCancelable(false);
\r
37 dialog.setTitle(activity.getString(R.string.calculating));
\r
38 dialog.setButton(activity.getString(R.string.cancel), new DialogInterface.OnClickListener() {
\r
40 public void onClick(DialogInterface dialog, int which) {
\r
42 AsyncTaskCommand.this.cancel(true);
\r
45 dialog.setOnKeyListener(new OnKeyListener() {
\r
47 public boolean onKey(DialogInterface dialog, int keyCode,
\r
49 switch (event.getKeyCode()) {
\r
50 case KeyEvent.KEYCODE_SEARCH:
\r
57 Debug.d(this, "onPreExecute end");
\r
61 protected String doInBackground(String... arg0) {
\r
62 Debug.d(this, "doInBackground start");
\r
63 command.doExecute();
\r
64 Debug.d(this, "doInBackground end");
\r
70 protected void onCancelled(){
\r
71 Debug.d(this, "onCancelled start");
\r
75 if (command != null){
\r
78 onPostExecute(null);
\r
79 Debug.d(this, "onCancelled end");
\r
83 protected void onPostExecute(String result){
\r
84 Debug.d(this, "onPostExecute start");
\r
87 if (command != null){
\r
88 command.done(isCancel);
\r
91 if (dialog != null){
\r
97 catch (RuntimeException e){
\r
98 Debug.d(this, null, e);
\r
101 Debug.d(this, "onPostExecute end");
\r
104 public void stop(){
\r
105 Debug.d(this, "stop start");
\r
107 if (command != null){
\r
111 // onPostExecute()のdialog.dismiss();でエラーとなるため
\r
114 if (dialog != null){
\r
119 Debug.d(this, "stop end");
\r