Refactor TCQueryAction so it can be used from the motor browser or when resolving missing motors.

This commit is contained in:
Kevin Ruland 2012-02-05 02:00:00 +00:00
parent 9def115330
commit ae34a951a6
4 changed files with 224 additions and 98 deletions

View File

@ -0,0 +1,109 @@
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 {
private Set<ThrustCurveMotorPlaceholder> missingMotors;
public TCMissingMotorDownloadAction(Activity parent) {
super(parent);
}
public void setMissingMotors( 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();
request.setManufacturer(motor.getManufacturer());
request.setDesignation(motor.getDesignation());
handler.post( new UpdateMessage("Looking for " + motor.getManufacturer() + " " + motor.getDesignation()));
SearchResponse res = new ThrustCurveAPI().doSearch(request);
int total = res.getResults().size();
int count = 1;
for( TCMotor mi : res.getResults() ) {
StringBuilder message = new StringBuilder();
message.append("Downloading details ");
if ( total > 1 ) {
message.append(count);
message.append(" of " );
message.append(total);
message.append("\n");
}
message.append(mi.getManufacturer());
message.append(" ");
message.append(mi.getCommon_name());
handler.post(new UpdateMessage(message.toString()));
count++;
if ( mi.getData_files() == null || mi.getData_files().intValue() == 0 ) {
continue;
}
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
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);
}
}
catch( Exception ex){
AndroidLogWrapper.d(TCQueryAction.class,ex.toString());
handler.post( new Error(ex.toString()) );
}
}
@Override
public void run() {
for ( ThrustCurveMotorPlaceholder motor : missingMotors ) {
AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "Motor: {}", motor);
downloadMissingMotor(motor);
}
handler.post( new Dismiss() );
}
}
}

View File

@ -1,8 +1,6 @@
package net.sf.openrocket.android.thrustcurve; package net.sf.openrocket.android.thrustcurve;
import net.sf.openrocket.android.db.DbAdapter; import net.sf.openrocket.android.db.DbAdapter;
import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;
import net.sf.openrocket.android.util.AndroidLogWrapper;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.ProgressDialog; import android.app.ProgressDialog;
@ -27,17 +25,17 @@ import android.os.Handler;
* When the parent Activity is dismissed, it must call TCQueryAction.dismiss() to free resources. * When the parent Activity is dismissed, it must call TCQueryAction.dismiss() to free resources.
* *
*/ */
public class TCQueryAction { public abstract class TCQueryAction {
public interface OnComplete { public interface OnComplete {
public void onComplete(); public void onComplete();
} }
private DbAdapter mDbHelper; protected DbAdapter mDbHelper;
private ProgressDialog progress; private ProgressDialog progress;
private Thread downloadThread; private Thread downloadThread;
private Handler handler; protected Handler handler;
private final Activity parent; private final Activity parent;
private OnComplete onCompleteListener; private OnComplete onCompleteListener;
@ -65,17 +63,18 @@ public class TCQueryAction {
this.onCompleteListener = onCompleteListener; this.onCompleteListener = onCompleteListener;
} }
public void start( SearchRequest request) { protected abstract Runnable getTask();
Downloader d = new Downloader(request);
public void start() {
handler = new Handler(); handler = new Handler();
progress = ProgressDialog.show(parent, null, ""); progress = ProgressDialog.show(parent, null, "");
downloadThread = new Thread( d ); downloadThread = new Thread( getTask() );
downloadThread.start(); downloadThread.start();
} }
public void dismiss() { public void dismiss() {
// TODO - need to kill the thread. // TODO - need to kill the thread.
@ -86,7 +85,7 @@ public class TCQueryAction {
} }
} }
private class UpdateMessage implements Runnable { protected class UpdateMessage implements Runnable {
private String newMessage; private String newMessage;
UpdateMessage( String message ) { UpdateMessage( String message ) {
this.newMessage = message; this.newMessage = message;
@ -97,7 +96,7 @@ public class TCQueryAction {
} }
} }
private class Dismiss implements Runnable { protected class Dismiss implements Runnable {
@Override @Override
public void run() { public void run() {
progress.dismiss(); progress.dismiss();
@ -108,7 +107,7 @@ public class TCQueryAction {
} }
} }
private class Error implements Runnable { protected class Error implements Runnable {
private String newMessage; private String newMessage;
Error( String message ) { Error( String message ) {
this.newMessage = message; this.newMessage = message;
@ -130,85 +129,4 @@ public class TCQueryAction {
} }
} }
private class Downloader implements Runnable {
SearchRequest request;
Downloader( SearchRequest request ) {
this.request = request;
}
@Override
public void run() {
try {
handler.post( new UpdateMessage("Quering Thrustcurve"));
SearchResponse res = new ThrustCurveAPI().doSearch(request);
int total = res.getResults().size();
int count = 1;
for( TCMotor mi : res.getResults() ) {
StringBuilder message = new StringBuilder();
message.append("Downloading details ");
if ( total > 1 ) {
message.append(count);
message.append(" of " );
message.append(total);
message.append("\n");
}
message.append(mi.getManufacturer());
message.append(" ");
message.append(mi.getCommon_name());
handler.post(new UpdateMessage(message.toString()));
count++;
if ( mi.getData_files() == null || mi.getData_files().intValue() == 0 ) {
continue;
}
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
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);
}
if ( total < res.getMatches() ) {
handler.post( new Error( total + " motors downloaded, " + res.getMatches() + " matched. Try restricting the query more.") );
} else {
handler.post( new Dismiss());
}
}
catch( Exception ex){
AndroidLogWrapper.d(TCQueryAction.class,ex.toString());
handler.post( new Error(ex.toString()) );
}
}
}
} }

View File

@ -13,14 +13,14 @@ public class TCQueryActivity extends Activity
implements TCQueryAction.OnComplete implements TCQueryAction.OnComplete
{ {
private TCQueryAction queryAction; private TCSearchAction queryAction;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.tcqueryform); setContentView(R.layout.tcqueryform);
queryAction = new TCQueryAction(this); queryAction = new TCSearchAction(this);
final Spinner manufacturerField = (Spinner) findViewById(R.id.TCMotorSearchFormManufacturerField); final Spinner manufacturerField = (Spinner) findViewById(R.id.TCMotorSearchFormManufacturerField);
final Spinner impulseField = (Spinner) findViewById(R.id.TCMotorSearchFormImpulseField); final Spinner impulseField = (Spinner) findViewById(R.id.TCMotorSearchFormImpulseField);
@ -54,7 +54,8 @@ implements TCQueryAction.OnComplete
} }
r.setCommon_name(commonName); r.setCommon_name(commonName);
queryAction.start(r); queryAction.setRequest(r);
queryAction.start();
} }
} }
); );

View File

@ -0,0 +1,98 @@
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 void setRequest( SearchRequest request ) {
this.request = request;
}
protected Runnable getTask() {
return new Downloader();
}
private class Downloader implements Runnable {
@Override
public void run() {
try {
handler.post( new UpdateMessage("Quering Thrustcurve"));
SearchResponse res = new ThrustCurveAPI().doSearch(request);
int total = res.getResults().size();
int count = 1;
for( TCMotor mi : res.getResults() ) {
StringBuilder message = new StringBuilder();
message.append("Downloading details ");
if ( total > 1 ) {
message.append(count);
message.append(" of " );
message.append(total);
message.append("\n");
}
message.append(mi.getManufacturer());
message.append(" ");
message.append(mi.getCommon_name());
handler.post(new UpdateMessage(message.toString()));
count++;
if ( mi.getData_files() == null || mi.getData_files().intValue() == 0 ) {
continue;
}
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
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);
}
if ( total < res.getMatches() ) {
handler.post( new Error( total + " motors downloaded, " + res.getMatches() + " matched. Try restricting the query more.") );
} else {
handler.post( new Dismiss());
}
}
catch( Exception ex){
AndroidLogWrapper.d(TCQueryAction.class,ex.toString());
handler.post( new Error(ex.toString()) );
}
}
}
}