1 package net.sf.openrocket.android.thrustcurve;
\r
3 import java.util.Set;
\r
4 import java.util.regex.Matcher;
\r
5 import java.util.regex.Pattern;
\r
7 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
8 import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;
\r
10 public class TCMissingMotorDownloadAction extends TCQueryAction {
\r
12 private final static String DESIGNATION_REGEX_STRING = "(Micro Maxx|Micro Maxx II|1/4A|1/2A|[A-O][0-9]*)";
\r
14 public static TCMissingMotorDownloadAction newInstance( Set<ThrustCurveMotorPlaceholder> missingMotors ) {
\r
15 TCMissingMotorDownloadAction frag = new TCMissingMotorDownloadAction();
\r
16 frag.task = frag.new Downloader(missingMotors);
\r
20 private class Downloader extends TCQueryAction.TCQueryTask {
\r
22 private Set<ThrustCurveMotorPlaceholder> missingMotors;
\r
23 private Pattern designation_pattern = null;
\r
25 private Downloader( Set<ThrustCurveMotorPlaceholder> missingMotors ) {
\r
26 this.missingMotors = missingMotors;
\r
28 designation_pattern = Pattern.compile(DESIGNATION_REGEX_STRING);
\r
29 } catch ( Exception ex ) {
\r
30 AndroidLogWrapper.e(TCMissingMotorDownloadAction.class, "Exception in pattern compile {}", ex);
\r
34 private void downloadMissingMotor( ThrustCurveMotorPlaceholder motor ) {
\r
37 SearchRequest request = new SearchRequest();
\r
38 request.setManufacturer(motor.getManufacturer());
\r
39 String designation = motor.getDesignation();
\r
40 if ( designation_pattern != null ) {
\r
41 Matcher m = designation_pattern.matcher(designation);
\r
43 designation = m.group();
\r
46 AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "using designation {}", designation);
\r
47 request.setCommon_name(designation);
\r
49 handler.post( new UpdateMessage("Looking for " + motor.getManufacturer() + " " + motor.getDesignation()));
\r
51 SearchResponse res = new ThrustCurveAPI().doSearch(request);
\r
53 int total = res.getResults().size();
\r
55 for( TCMotor mi : res.getResults() ) {
\r
56 StringBuilder message = new StringBuilder();
\r
57 message.append("Downloading details ");
\r
59 message.append(count);
\r
60 message.append(" of " );
\r
61 message.append(total);
\r
62 message.append("\n");
\r
64 message.append(mi.getManufacturer());
\r
65 message.append(" ");
\r
66 message.append(mi.getCommon_name());
\r
67 handler.post(new UpdateMessage(message.toString()));
\r
69 if ( mi.getData_files() == null || mi.getData_files().intValue() == 0 ) {
\r
73 AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
\r
75 MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
\r
81 catch( Exception ex){
\r
82 AndroidLogWrapper.d(TCQueryAction.class,ex.toString());
\r
83 handler.post( new UpdateMessage("Failed") );
\r
90 protected String doInBackground(Void... arg0) {
\r
92 for ( ThrustCurveMotorPlaceholder motor : missingMotors ) {
\r
93 AndroidLogWrapper.d(TCMissingMotorDownloadAction.class, "Motor: {}", motor);
\r
94 downloadMissingMotor(motor);
\r