Rewrite the ThrustCurve search and download process to be orientation friendly.

This commit is contained in:
Kevin Ruland 2012-02-09 04:04:29 +00:00
parent 21fb67f8d2
commit 81cb5abcb1
7 changed files with 235 additions and 226 deletions

View File

@ -18,18 +18,16 @@ import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
public class OpenRocketLoaderActivity extends FragmentActivity
implements TCQueryAction.OnComplete, OpenRocketLoaderFragment.OnOpenRocketFileLoaded
implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnOpenRocketFileLoaded
{
private final static String MISSING_MOTOR_DIAG_FRAGMENT_TAG = "missingmotordialog";
private TCMissingMotorDownloadAction missingMotorDownloadAction;
private final static String MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG = "missingmotortask";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
missingMotorDownloadAction = new TCMissingMotorDownloadAction(this);
if ( savedInstanceState == null || savedInstanceState.getBoolean("isLoading", false) == false ) {
Intent i = getIntent();
Uri file = i.getData();
@ -44,15 +42,6 @@ implements TCQueryAction.OnComplete, OpenRocketLoaderFragment.OnOpenRocketFileLo
outState.putBoolean("isLoading", true);
}
@Override
protected void onDestroy() {
if ( missingMotorDownloadAction != null ) {
missingMotorDownloadAction.dismiss();
}
super.onDestroy();
}
private void loadOrkFile( Uri file ) {
AndroidLogWrapper.d(OpenRocketLoaderActivity.class,"Use ork file: " + file);
String path = file.getPath();
@ -93,7 +82,7 @@ implements TCQueryAction.OnComplete, OpenRocketLoaderFragment.OnOpenRocketFileLo
* Called when the TCMissingMotorDownload process finishes.
*/
@Override
public void onComplete() {
public void onTCQueryComplete(String message) {
Rocket rocket = ((Application)OpenRocketLoaderActivity.this.getApplication()).getRocketDocument().getRocket();
WarningSet warnings = ((Application)OpenRocketLoaderActivity.this.getApplication()).getWarnings();
@ -120,8 +109,8 @@ implements TCQueryAction.OnComplete, OpenRocketLoaderFragment.OnOpenRocketFileLo
Rocket rocket = ((Application)OpenRocketLoaderActivity.this.getApplication()).getRocketDocument().getRocket();
Set<ThrustCurveMotorPlaceholder> missingMotors = MissingMotorHelpers.findMissingMotors(rocket);
missingMotorDownloadAction.setMissingMotors(missingMotors);
missingMotorDownloadAction.start();
TCMissingMotorDownloadAction motorfrag = TCMissingMotorDownloadAction.newInstance( missingMotors );
getSupportFragmentManager().beginTransaction().add( motorfrag, MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG).commit();
}

View File

@ -2,29 +2,25 @@ package net.sf.openrocket.android.thrustcurve;
import java.util.Set;
import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;
import net.sf.openrocket.android.util.AndroidLogWrapper;
import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;
import android.app.Activity;
public class TCMissingMotorDownloadAction extends TCQueryAction {
public static TCMissingMotorDownloadAction newInstance( Set<ThrustCurveMotorPlaceholder> missingMotors ) {
TCMissingMotorDownloadAction frag = new TCMissingMotorDownloadAction();
frag.task = frag.new Downloader(missingMotors);
return frag;
}
private class Downloader extends TCQueryAction.TCQueryTask {
private Set<ThrustCurveMotorPlaceholder> missingMotors;
public TCMissingMotorDownloadAction(Activity parent) {
super(parent);
}
public void setMissingMotors( Set<ThrustCurveMotorPlaceholder> missingMotors ) {
private Downloader( Set<ThrustCurveMotorPlaceholder> missingMotors ) {
this.missingMotors = missingMotors;
}
protected Runnable getTask() {
return new Downloader();
}
private class Downloader implements Runnable {
private void downloadMissingMotor( ThrustCurveMotorPlaceholder motor ) {
try {
SearchRequest request = new SearchRequest();
@ -55,55 +51,32 @@ public class TCMissingMotorDownloadAction extends TCQueryAction {
continue;
}
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
m.setThrustCurveMotor( b.getThrustCurveMotor() );
writeMotor( mi, b);
// Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.
m.setImpulseClass(mi.getImpulse_class());
if ( "a".equalsIgnoreCase(mi.getImpulse_class())) {
if( mi.getCommon_name().startsWith("1/2A") ) {
m.setImpulseClass("1/2A");
} else if (mi.getCommon_name().startsWith("1/4A") ) {
m.setImpulseClass("1/4A");
} else if (mi.getCommon_name().startsWith("Micro") ) {
m.setImpulseClass("1/8A");
}
}
// Convert Case Info.
if ( mi.getCase_info() == null
|| "single use".equalsIgnoreCase(mi.getCase_info())
|| "single-use".equalsIgnoreCase(mi.getCase_info())) {
m.setCaseInfo(mi.getType()+ " " + mi.getDiameter() + "x" + mi.getLength());
} else {
m.setCaseInfo(mi.getCase_info());
}
AndroidLogWrapper.d(TCQueryAction.class,"adding motor " + m.toString());
// Write motor.
mDbHelper.getMotorDao().insertOrUpdateMotor(m);
}
}
catch( Exception ex){
AndroidLogWrapper.d(TCQueryAction.class,ex.toString());
handler.post( new Error(ex.toString()) );
handler.post( new UpdateMessage("Failed") );
}
}
@Override
public void run() {
protected String doInBackground(Void... arg0) {
for ( ThrustCurveMotorPlaceholder motor : missingMotors ) {
AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "Motor: {}", motor);
downloadMissingMotor(motor);
}
handler.post( new Dismiss() );
dismiss();
return null;
}
}
}
}

View File

@ -1,11 +1,18 @@
package net.sf.openrocket.android.thrustcurve;
import net.sf.openrocket.android.db.DbAdapter;
import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;
import net.sf.openrocket.android.util.AndroidLogWrapper;
import net.sf.openrocket.android.util.ProgressDialogFragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* TCQueryAction is a class which provides all the functionality required
@ -25,62 +32,111 @@ import android.os.Handler;
* When the parent Activity is dismissed, it must call TCQueryAction.dismiss() to free resources.
*
*/
public abstract class TCQueryAction {
public abstract class TCQueryAction extends Fragment {
public interface OnComplete {
public void onComplete();
private final static String PROGRESS_DIALOG_TAG = "progress_dialog";
public interface OnTCQueryCompleteListener {
public void onTCQueryComplete(String message);
}
protected DbAdapter mDbHelper;
private ProgressDialog progress;
private Thread downloadThread;
protected AsyncTask<Void,Void,String> task;
protected Handler handler;
private final Activity parent;
private OnComplete onCompleteListener;
private OnTCQueryCompleteListener onCompleteListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
handler = new Handler();
if ( savedInstanceState == null ) {
// this means we are starting for the first time.
task.execute((Void)null);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return null;
}
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
Activity parent = getActivity();
if ( parent instanceof OnTCQueryCompleteListener ) {
onCompleteListener = (OnTCQueryCompleteListener) parent;
}
}
/**
* Create a new TCQueryAction.
* The return value is a message string which may be displayed by the caller.
*
* If the parent implements TCQueryAction.OnComplete, it will be used as the
* onCompleteListener and notified when the process is finished.
*
* @param parent
*/
public TCQueryAction( Activity parent ) {
this.parent = parent;
protected abstract class TCQueryTask extends AsyncTask<Void,Void,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
DialogFragment newFragment = ProgressDialogFragment.newInstance("", "");
newFragment.show(getFragmentManager(), PROGRESS_DIALOG_TAG);
}
mDbHelper = new DbAdapter(this.parent);
@Override
protected void onPostExecute(String obj) {
super.onPostExecute(obj);
AndroidLogWrapper.d(TCQueryAction.class,"Finished loading " + TCQueryAction.this);
dismiss();
if (onCompleteListener != null ) {
onCompleteListener.onTCQueryComplete(obj);
}
}
}
protected void writeMotor( TCMotor mi, MotorBurnFile b) throws Exception {
DbAdapter mDbHelper = new DbAdapter(getActivity());
mDbHelper.open();
try {
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
if (parent instanceof OnComplete ) {
this.onCompleteListener = (OnComplete) parent;
m.setThrustCurveMotor( b.getThrustCurveMotor() );
// Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.
m.setImpulseClass(mi.getImpulse_class());
if ( "a".equalsIgnoreCase(mi.getImpulse_class())) {
if( mi.getCommon_name().startsWith("1/2A") ) {
m.setImpulseClass("1/2A");
} else if (mi.getCommon_name().startsWith("1/4A") ) {
m.setImpulseClass("1/4A");
} else if (mi.getCommon_name().startsWith("Micro") ) {
m.setImpulseClass("1/8A");
}
}
public void setOnCompleteListener(OnComplete onCompleteListener) {
this.onCompleteListener = onCompleteListener;
// Convert Case Info.
if ( mi.getCase_info() == null
|| "single use".equalsIgnoreCase(mi.getCase_info())
|| "single-use".equalsIgnoreCase(mi.getCase_info())) {
m.setCaseInfo(mi.getType()+ " " + mi.getDiameter() + "x" + mi.getLength());
} else {
m.setCaseInfo(mi.getCase_info());
}
protected abstract Runnable getTask();
public void start() {
handler = new Handler();
progress = ProgressDialog.show(parent, null, "");
downloadThread = new Thread( getTask() );
downloadThread.start();
}
public void dismiss() {
// TODO - need to kill the thread.
AndroidLogWrapper.d(TCQueryAction.class,"adding motor " + m.toString());
// Write motor.
mDbHelper.getMotorDao().insertOrUpdateMotor(m);
} finally {
mDbHelper.close();
}
}
if ( progress != null && progress.isShowing() ) {
protected void dismiss() {
AndroidLogWrapper.d(TCQueryAction.class,"dismiss the progress");
ProgressDialogFragment progress = (ProgressDialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
if ( progress != null ) {
progress.dismiss();
}
}
@ -92,41 +148,10 @@ public abstract class TCQueryAction {
}
@Override
public void run() {
ProgressDialogFragment progress = (ProgressDialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
if ( progress != null )
progress.setMessage(newMessage);
}
}
protected class Dismiss implements Runnable {
@Override
public void run() {
progress.dismiss();
if (onCompleteListener != null ) {
onCompleteListener.onComplete();
}
// TCQueryActivity.this.finish();
}
}
protected class Error implements Runnable {
private String newMessage;
Error( String message ) {
this.newMessage = message;
}
@Override
public void run() {
progress.dismiss();
final AlertDialog dialog = new AlertDialog.Builder(parent).create();
dialog.setMessage(newMessage);
dialog.setButton(DialogInterface.BUTTON_NEUTRAL,"Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
dialog.dismiss();
}
});
dialog.show();
}
}
}

View File

@ -2,26 +2,24 @@ package net.sf.openrocket.android.thrustcurve;
import net.sf.openrocket.R;
import net.sf.openrocket.android.util.AndroidLogWrapper;
import android.app.Activity;
import net.sf.openrocket.android.util.ErrorDialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class TCQueryActivity extends Activity
implements TCQueryAction.OnComplete
public class TCQueryActivity extends FragmentActivity
implements TCQueryAction.OnTCQueryCompleteListener
{
private TCSearchAction queryAction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tcqueryform);
queryAction = new TCSearchAction(this);
final Spinner manufacturerField = (Spinner) findViewById(R.id.TCMotorSearchFormManufacturerField);
final Spinner impulseField = (Spinner) findViewById(R.id.TCMotorSearchFormImpulseField);
final Spinner diameterField = (Spinner) findViewById(R.id.TCMotorSearchFormDiameterField);
@ -54,29 +52,20 @@ implements TCQueryAction.OnComplete
}
r.setCommon_name(commonName);
queryAction.setRequest(r);
queryAction.start();
TCSearchAction motorfrag = TCSearchAction.newInstance( r );
getSupportFragmentManager().beginTransaction().add( motorfrag, "dloader").commit();
}
}
);
});
}
@Override
public void onComplete() {
public void onTCQueryComplete(String message) {
if ( message != null) {
ErrorDialogFragment error = ErrorDialogFragment.newInstance(message);
error.show(getSupportFragmentManager(), "ErrorDialog");
} else {
finish();
}
/*
* TODO - ??
@Override
public Object onRetainNonConfigurationInstance() {
return downloadThread;
}
*/
@Override
protected void onDestroy() {
queryAction.dismiss();
super.onDestroy();
}
}

View File

@ -1,32 +1,28 @@
package net.sf.openrocket.android.thrustcurve;
import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;
import net.sf.openrocket.android.util.AndroidLogWrapper;
import android.app.Activity;
public class TCSearchAction extends TCQueryAction {
private SearchRequest request;
public TCSearchAction(Activity parent) {
super(parent);
public static TCSearchAction newInstance( SearchRequest searchRequest ) {
TCSearchAction frag = new TCSearchAction();
frag.task = frag.new Downloader(searchRequest);
return frag;
}
public void setRequest( SearchRequest request ) {
this.request = request;
}
private class Downloader extends TCQueryAction.TCQueryTask {
protected Runnable getTask() {
return new Downloader();
}
private SearchRequest searchRequest;
private class Downloader implements Runnable {
private Downloader( SearchRequest searchRequest ) {
this.searchRequest = searchRequest;
}
@Override
public void run() {
protected String doInBackground(Void... params) {
try {
handler.post( new UpdateMessage("Quering Thrustcurve"));
SearchResponse res = new ThrustCurveAPI().doSearch(request);
SearchResponse res = new ThrustCurveAPI().doSearch(searchRequest);
int total = res.getResults().size();
int count = 1;
@ -48,50 +44,24 @@ public class TCSearchAction extends TCQueryAction {
continue;
}
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
m.setThrustCurveMotor( b.getThrustCurveMotor() );
// Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.
m.setImpulseClass(mi.getImpulse_class());
if ( "a".equalsIgnoreCase(mi.getImpulse_class())) {
if( mi.getCommon_name().startsWith("1/2A") ) {
m.setImpulseClass("1/2A");
} else if (mi.getCommon_name().startsWith("1/4A") ) {
m.setImpulseClass("1/4A");
} else if (mi.getCommon_name().startsWith("Micro") ) {
m.setImpulseClass("1/8A");
}
}
// Convert Case Info.
if ( mi.getCase_info() == null
|| "single use".equalsIgnoreCase(mi.getCase_info())
|| "single-use".equalsIgnoreCase(mi.getCase_info())) {
m.setCaseInfo(mi.getType()+ " " + mi.getDiameter() + "x" + mi.getLength());
} else {
m.setCaseInfo(mi.getCase_info());
}
AndroidLogWrapper.d(TCQueryAction.class,"adding motor " + m.toString());
// Write motor.
mDbHelper.getMotorDao().insertOrUpdateMotor(m);
writeMotor( mi, b);
}
if ( total < res.getMatches() ) {
handler.post( new Error( total + " motors downloaded, " + res.getMatches() + " matched. Try restricting the query more.") );
dismiss();
return "" + total + " motors downloaded, " + res.getMatches() + " matched. Try restricting the query more.";
} else {
handler.post( new Dismiss());
dismiss();
return null;
}
}
catch( Exception ex){
AndroidLogWrapper.d(TCQueryAction.class,ex.toString());
handler.post( new Error(ex.toString()) );
AndroidLogWrapper.d(TCSearchAction.class,ex.toString());
return ex.toString();
}
}
}

View File

@ -0,0 +1,46 @@
package net.sf.openrocket.android.util;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class ErrorDialogFragment extends DialogFragment {
public static ErrorDialogFragment newInstance( String message ) {
ErrorDialogFragment dialog = new ErrorDialogFragment();
Bundle b = new Bundle();
b.putString("message",message);
dialog.setArguments(b);
return dialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String message = getArguments().getString("message");
final AlertDialog dialog = new AlertDialog.Builder(getActivity()).create();
dialog.setOwnerActivity(getActivity());
dialog.setMessage(message);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setButton(DialogInterface.BUTTON_NEUTRAL,"Dismiss", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
dialog.dismiss();
}
});
return dialog;
}
}

View File

@ -6,6 +6,8 @@ import android.support.v4.app.DialogFragment;
public class ProgressDialogFragment extends DialogFragment {
ProgressDialog progressDialog;
public static ProgressDialogFragment newInstance(String title, String message) {
ProgressDialogFragment fragment = new ProgressDialogFragment();
Bundle args = new Bundle();
@ -16,20 +18,35 @@ public class ProgressDialogFragment extends DialogFragment {
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String message = getArguments().getString("message");
String title = null;
String message = null;
Bundle args = getArguments();
if ( args != null ) {
title = getArguments().getString("title");
message = getArguments().getString("message");
}
ProgressDialog progressDialog = new ProgressDialog(getActivity());
AndroidLogWrapper.d(ProgressDialogFragment.class, "onCreateDialog");
progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle(title);
progressDialog.setMessage(message);
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
return progressDialog;
}
public void setMessage( String message ) {
progressDialog.setMessage(message);
}
}