1 package net.sf.openrocket.android.thrustcurve;
\r
3 import net.sf.openrocket.android.db.DbAdapter;
\r
4 import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;
\r
5 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
6 import net.sf.openrocket.android.util.ProgressDialogFragment;
\r
7 import android.app.Activity;
\r
8 import android.os.AsyncTask;
\r
9 import android.os.Bundle;
\r
10 import android.os.Handler;
\r
11 import android.support.v4.app.DialogFragment;
\r
12 import android.support.v4.app.Fragment;
\r
13 import android.view.LayoutInflater;
\r
14 import android.view.View;
\r
15 import android.view.ViewGroup;
\r
18 * TCQueryAction is a class which provides all the functionality required
\r
19 * to download motor data from thrustcurve. It includes UI element for
\r
20 * progress and error reporting dialogs.
\r
22 * To use the class, instantiate a new instance of TCQueryAction passing in the
\r
25 * The Activity should implement TCQueryAction.OnComplete or provide an implementation of
\r
26 * TCQueryAction.OnComplete to be notified when the download process is complete.
\r
28 * A search and download is started with TCQueryActivity.start( SearchRequest ). The TCQueryActivity
\r
29 * produces and updates a progress dialog. When the process is complete, the TCQueryActivity will notify
\r
30 * the registered TCQueryAction.OnComplete handler.
\r
32 * When the parent Activity is dismissed, it must call TCQueryAction.dismiss() to free resources.
\r
35 public abstract class TCQueryAction extends Fragment {
\r
37 private final static String PROGRESS_DIALOG_TAG = "progress_dialog";
\r
39 public interface OnTCQueryCompleteListener {
\r
40 public void onTCQueryComplete(String message);
\r
43 protected AsyncTask<Void,Void,String> task;
\r
44 protected Handler handler;
\r
46 private OnTCQueryCompleteListener onCompleteListener;
\r
49 public void onCreate(Bundle savedInstanceState) {
\r
50 super.onCreate(savedInstanceState);
\r
51 setRetainInstance(true);
\r
52 handler = new Handler();
\r
53 if ( savedInstanceState == null ) {
\r
54 // this means we are starting for the first time.
\r
55 task.execute((Void)null);
\r
61 public View onCreateView(LayoutInflater inflater, ViewGroup container,
\r
62 Bundle savedInstanceState) {
\r
67 public void onActivityCreated(Bundle arg0) {
\r
68 super.onActivityCreated(arg0);
\r
69 Activity parent = getActivity();
\r
70 if ( parent instanceof OnTCQueryCompleteListener ) {
\r
71 onCompleteListener = (OnTCQueryCompleteListener) parent;
\r
76 * The return value is a message string which may be displayed by the caller.
\r
79 protected abstract class TCQueryTask extends AsyncTask<Void,Void,String> {
\r
81 protected void onPreExecute() {
\r
82 super.onPreExecute();
\r
83 DialogFragment newFragment = ProgressDialogFragment.newInstance("", "");
\r
84 newFragment.show(getFragmentManager(), PROGRESS_DIALOG_TAG);
\r
88 protected void onPostExecute(String obj) {
\r
89 super.onPostExecute(obj);
\r
90 AndroidLogWrapper.d(TCQueryAction.class,"Finished loading " + TCQueryAction.this);
\r
92 if (onCompleteListener != null ) {
\r
93 onCompleteListener.onTCQueryComplete(obj);
\r
98 protected void writeMotor( TCMotor mi, MotorBurnFile b) throws Exception {
\r
100 DbAdapter mDbHelper = new DbAdapter(getActivity());
\r
103 ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
\r
105 m.setThrustCurveMotor( b.getThrustCurveMotor() );
\r
107 // Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.
\r
108 m.setImpulseClass(mi.getImpulse_class());
\r
109 if ( "a".equalsIgnoreCase(mi.getImpulse_class())) {
\r
110 if( mi.getCommon_name().startsWith("1/2A") ) {
\r
111 m.setImpulseClass("1/2A");
\r
112 } else if (mi.getCommon_name().startsWith("1/4A") ) {
\r
113 m.setImpulseClass("1/4A");
\r
114 } else if (mi.getCommon_name().startsWith("Micro") ) {
\r
115 m.setImpulseClass("1/8A");
\r
119 // Convert Case Info.
\r
120 if ( mi.getCase_info() == null
\r
121 || "single use".equalsIgnoreCase(mi.getCase_info())
\r
122 || "single-use".equalsIgnoreCase(mi.getCase_info())) {
\r
123 m.setCaseInfo(mi.getType()+ " " + mi.getDiameter() + "x" + mi.getLength());
\r
125 m.setCaseInfo(mi.getCase_info());
\r
128 AndroidLogWrapper.d(TCQueryAction.class,"adding motor " + m.toString());
\r
130 mDbHelper.getMotorDao().insertOrUpdateMotor(m);
\r
136 protected void dismiss() {
\r
137 AndroidLogWrapper.d(TCQueryAction.class,"dismiss the progress");
\r
138 ProgressDialogFragment progress = (ProgressDialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
\r
139 if ( progress != null ) {
\r
140 progress.dismiss();
\r
144 protected class UpdateMessage implements Runnable {
\r
145 private String newMessage;
\r
146 UpdateMessage( String message ) {
\r
147 this.newMessage = message;
\r
150 public void run() {
\r
151 ProgressDialogFragment progress = (ProgressDialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
\r
152 if ( progress != null )
\r
153 progress.setMessage(newMessage);
\r