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 net.sf.openrocket.motor.ThrustCurveMotor;
\r
8 import android.app.Activity;
\r
9 import android.os.AsyncTask;
\r
10 import android.os.Bundle;
\r
11 import android.os.Handler;
\r
12 import android.support.v4.app.DialogFragment;
\r
13 import android.support.v4.app.Fragment;
\r
14 import android.view.LayoutInflater;
\r
15 import android.view.View;
\r
16 import android.view.ViewGroup;
\r
19 * TCQueryAction is a class which provides all the functionality required
\r
20 * to download motor data from thrustcurve. It includes UI element for
\r
21 * progress and error reporting dialogs.
\r
23 * To use the class, instantiate a new instance of TCQueryAction passing in the
\r
26 * The Activity should implement TCQueryAction.OnComplete or provide an implementation of
\r
27 * TCQueryAction.OnComplete to be notified when the download process is complete.
\r
29 * A search and download is started with TCQueryActivity.start( SearchRequest ). The TCQueryActivity
\r
30 * produces and updates a progress dialog. When the process is complete, the TCQueryActivity will notify
\r
31 * the registered TCQueryAction.OnComplete handler.
\r
33 * When the parent Activity is dismissed, it must call TCQueryAction.dismiss() to free resources.
\r
36 public abstract class TCQueryAction extends Fragment {
\r
38 private final static String PROGRESS_DIALOG_TAG = "progress_dialog";
\r
40 public interface OnTCQueryCompleteListener {
\r
41 public void onTCQueryComplete(String message);
\r
44 protected AsyncTask<Void,Void,String> task;
\r
45 protected Handler handler;
\r
47 private OnTCQueryCompleteListener onCompleteListener;
\r
50 public void onCreate(Bundle savedInstanceState) {
\r
51 super.onCreate(savedInstanceState);
\r
52 setRetainInstance(true);
\r
53 handler = new Handler();
\r
54 if ( savedInstanceState == null ) {
\r
55 // this means we are starting for the first time.
\r
56 task.execute((Void)null);
\r
62 public View onCreateView(LayoutInflater inflater, ViewGroup container,
\r
63 Bundle savedInstanceState) {
\r
68 public void onActivityCreated(Bundle arg0) {
\r
69 super.onActivityCreated(arg0);
\r
70 Activity parent = getActivity();
\r
71 if ( parent instanceof OnTCQueryCompleteListener ) {
\r
72 onCompleteListener = (OnTCQueryCompleteListener) parent;
\r
77 * The return value is a message string which may be displayed by the caller.
\r
80 protected abstract class TCQueryTask extends AsyncTask<Void,Void,String> {
\r
82 protected void onPreExecute() {
\r
83 super.onPreExecute();
\r
84 DialogFragment newFragment = ProgressDialogFragment.newInstance("", "");
\r
85 newFragment.show(getFragmentManager(), PROGRESS_DIALOG_TAG);
\r
89 protected void onPostExecute(String obj) {
\r
90 super.onPostExecute(obj);
\r
91 AndroidLogWrapper.d(TCQueryAction.class,"Finished loading " + TCQueryAction.this);
\r
93 if (onCompleteListener != null ) {
\r
94 onCompleteListener.onTCQueryComplete(obj);
\r
99 protected void writeMotor( TCMotor mi, ThrustCurveMotor thrustCurveMotor) throws Exception {
\r
101 DbAdapter mDbHelper = new DbAdapter(getActivity());
\r
104 ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor(thrustCurveMotor);
\r
106 // Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.
\r
107 m.setImpulseClass(mi.getImpulse_class());
\r
108 if ( "a".equalsIgnoreCase(mi.getImpulse_class())) {
\r
109 if( mi.getCommon_name().startsWith("1/2A") ) {
\r
110 m.setImpulseClass("1/2A");
\r
111 } else if (mi.getCommon_name().startsWith("1/4A") ) {
\r
112 m.setImpulseClass("1/4A");
\r
113 } else if (mi.getCommon_name().startsWith("Micro") ) {
\r
114 m.setImpulseClass("1/8A");
\r
118 // Convert Case Info.
\r
119 if ( mi.getCase_info() == null
\r
120 || "single use".equalsIgnoreCase(mi.getCase_info())
\r
121 || "single-use".equalsIgnoreCase(mi.getCase_info())) {
\r
122 m.setCaseInfo(mi.getType()+ " " + mi.getDiameter() + "x" + mi.getLength());
\r
124 m.setCaseInfo(mi.getCase_info());
\r
127 AndroidLogWrapper.d(TCQueryAction.class,"adding motor " + m.toString());
\r
129 mDbHelper.getMotorDao().insertOrUpdateMotor(m);
\r
135 protected void dismiss() {
\r
136 AndroidLogWrapper.d(TCQueryAction.class,"dismiss the progress");
\r
137 ProgressDialogFragment progress = (ProgressDialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
\r
138 if ( progress != null ) {
\r
139 getActivity().getSupportFragmentManager().beginTransaction().remove(progress).commit();
\r
143 protected class UpdateMessage implements Runnable {
\r
144 private String newMessage;
\r
145 UpdateMessage( String message ) {
\r
146 this.newMessage = message;
\r
149 public void run() {
\r
150 ProgressDialogFragment progress = (ProgressDialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
\r
151 if ( progress != null )
\r
152 progress.setMessage(newMessage);
\r