1 package net.sf.openrocket.android.rocket;
\r
4 import java.util.Set;
\r
6 import net.sf.openrocket.R;
\r
7 import net.sf.openrocket.aerodynamics.WarningSet;
\r
8 import net.sf.openrocket.android.CurrentRocketHolder;
\r
9 import net.sf.openrocket.android.filebrowser.SimpleFileBrowser;
\r
10 import net.sf.openrocket.android.thrustcurve.TCMissingMotorDownloadAction;
\r
11 import net.sf.openrocket.android.thrustcurve.TCQueryAction;
\r
12 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
13 import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;
\r
14 import net.sf.openrocket.rocketcomponent.Rocket;
\r
15 import android.app.AlertDialog;
\r
16 import android.app.Dialog;
\r
17 import android.content.ActivityNotFoundException;
\r
18 import android.content.Intent;
\r
19 import android.content.SharedPreferences;
\r
20 import android.content.res.Resources;
\r
21 import android.net.Uri;
\r
22 import android.os.Bundle;
\r
23 import android.preference.PreferenceManager;
\r
24 import android.support.v4.app.DialogFragment;
\r
26 import com.actionbarsherlock.app.SherlockFragmentActivity;
\r
28 public class OpenRocketLoaderActivity extends SherlockFragmentActivity
\r
29 implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnOpenRocketFileLoaded
\r
32 private static final int PICK_ORK_FILE_RESULT = 1;
\r
34 private final static String MISSING_MOTOR_DIAG_FRAGMENT_TAG = "missingmotordialog";
\r
35 private final static String MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG = "missingmotortask";
\r
38 * Set to true when we have started to load a file. Is saved in InstanceState.
\r
40 private boolean isLoading = false;
\r
42 * Set to the Uri of the file we are supposed to load. Is saved in InstanceState.
\r
44 private Uri fileToLoad = null;
\r
47 protected void onPostCreate(Bundle savedInstanceState) {
\r
48 super.onPostCreate(savedInstanceState);
\r
49 Intent i = getIntent();
\r
50 if (Intent.ACTION_VIEW.equals(i.getAction()) && i.getData() != null ) {
\r
51 Uri file = i.getData();
\r
59 protected void onSaveInstanceState(Bundle outState) {
\r
60 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onSaveInstanceState");
\r
61 outState.putBoolean("isLoading", isLoading);
\r
62 if ( fileToLoad != null ) {
\r
63 outState.putParcelable("fileToLoad", fileToLoad);
\r
65 super.onSaveInstanceState(outState);
\r
69 protected void onRestoreInstanceState(Bundle savedInstanceState) {
\r
70 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onRestoreInstanceState");
\r
71 isLoading = savedInstanceState.getBoolean("isLoading",false);
\r
72 if ( savedInstanceState.containsKey("fileToLoad") ) {
\r
73 fileToLoad = savedInstanceState.getParcelable("fileToLoad");
\r
75 super.onRestoreInstanceState(savedInstanceState);
\r
79 protected void onResume() {
\r
80 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onResume");
\r
82 // Start loading a file if we have a file and are not already loading one.
\r
83 if ( fileToLoad != null && !isLoading ) {
\r
89 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
\r
92 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
\r
93 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onActivityResult");
\r
94 switch ( requestCode ) {
\r
95 case PICK_ORK_FILE_RESULT:
\r
96 if(resultCode==RESULT_OK){
\r
97 Uri file = data.getData();
\r
99 // It would be nice to just start loading the file - but that doesn't work correctly.
\r
100 // I'm uncertain if it is a bug in Android 14/15 or a bug in the v4 support library.
\r
101 // essentially what happens is, when the FileBrowserActivity is brought up,
\r
102 // this activity goes through the saveInstanceState calls to push it to the background.
\r
103 // When the FileBrowserActivity returns the result, this.onActivityResult is called
\r
104 // prior to any of the other lifecycle methods (onRestoreInstanceState as documented, but onStart is
\r
105 // a bug. Since onStart hasn't been called, this activity is not able to create fragments - which
\r
106 // are used to indicate progress etc.
\r
107 // Instead of calling loadOrkFile() here, we push the file Uri into a member variable,
\r
108 // then check the member variable in onResume to actuall kick off the work.
\r
112 super.onActivityResult(requestCode, resultCode, data);
\r
116 protected void pickOrkFiles( ) {
\r
117 Resources resources = this.getResources();
\r
118 String key = resources.getString(R.string.PreferenceUseInternalFileBrowserOption);
\r
119 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
\r
121 boolean useinternalbrowser = pref.getBoolean(key, true);
\r
123 if ( useinternalbrowser ) {
\r
124 Intent intent = new Intent(OpenRocketLoaderActivity.this, SimpleFileBrowser.class);
\r
125 startActivityForResult(intent,PICK_ORK_FILE_RESULT);
\r
128 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
\r
129 intent.setType("file/*");
\r
130 startActivityForResult(intent,PICK_ORK_FILE_RESULT);
\r
131 } catch ( ActivityNotFoundException ex ) {
\r
132 // No activity for ACTION_GET_CONTENT use internal file browser
\r
133 // update the preference value.
\r
134 pref.edit().putBoolean(key, false).commit();
\r
135 // fire our browser
\r
136 Intent intent = new Intent(OpenRocketLoaderActivity.this, SimpleFileBrowser.class);
\r
137 startActivityForResult(intent,PICK_ORK_FILE_RESULT);
\r
142 private void loadOrkFile( ) {
\r
143 // a little protection.
\r
144 if ( fileToLoad == null ) {
\r
148 CurrentRocketHolder.getCurrentRocket().setFileUri( fileToLoad );
\r
149 AndroidLogWrapper.d(OpenRocketLoaderActivity.class,"Use ork file: " + fileToLoad);
\r
150 String path = fileToLoad.getPath();
\r
151 File orkFile = new File(path);
\r
153 // Also need commitAllowingState loss because of a bug in v4 dialog show.
\r
154 getSupportFragmentManager().beginTransaction()
\r
155 .add( OpenRocketLoaderFragment.newInstance(orkFile), "loader")
\r
156 .commitAllowingStateLoss();
\r
161 * Called by the OpenRocketLoaderTask when it completes.
\r
162 * is default visibility so it can be called from this package.
\r
166 public void onOpenRocketFileLoaded(OpenRocketLoaderResult result) {
\r
168 if ( result.loadingError != null ) {
\r
170 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
\r
171 dialogBuilder.setTitle( R.string.loadingErrorMessage );
\r
172 dialogBuilder.setMessage( result.loadingError.getLocalizedMessage());
\r
173 dialogBuilder.setCancelable(true);
\r
174 Dialog d = dialogBuilder.create();
\r
175 d.setCanceledOnTouchOutside(true);
\r
179 CurrentRocketHolder.getCurrentRocket().setRocketDocument( result.rocket );
\r
180 CurrentRocketHolder.getCurrentRocket().setWarnings( result.warnings );
\r
182 updateMissingMotors();
\r
186 private void updateMissingMotors() {
\r
187 Rocket rocket = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getRocket();
\r
188 Set<ThrustCurveMotorPlaceholder> missingMotors = MissingMotorHelpers.findMissingMotors(rocket);
\r
190 if ( missingMotors.size() > 0 ) {
\r
191 DialogFragment missingMotorDialog = MissingMotorDialogFragment.newInstance( missingMotors );
\r
192 getSupportFragmentManager().beginTransaction().add(missingMotorDialog, MISSING_MOTOR_DIAG_FRAGMENT_TAG).commit();
\r
196 displayWarningDialog();
\r
200 * Called when the TCMissingMotorDownload process finishes.
\r
203 public void onTCQueryComplete(String message) {
\r
205 Rocket rocket = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getRocket();
\r
206 WarningSet warnings = CurrentRocketHolder.getCurrentRocket().getWarnings();
\r
207 // Need to update the motor references.
\r
208 MissingMotorHelpers.updateMissingMotors(rocket, warnings);
\r
210 displayWarningDialog();
\r
213 private void displayWarningDialog() {
\r
214 WarningSet warnings = CurrentRocketHolder.getCurrentRocket().getWarnings();
\r
215 if (warnings == null || warnings.isEmpty()) {
\r
217 DialogFragment newFragment = WarningDialogFragment.newInstance();
\r
218 newFragment.show(getSupportFragmentManager(), "dialog");
\r
225 public void doFixMissingMotors() {
\r
226 Rocket rocket = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getRocket();
\r
227 Set<ThrustCurveMotorPlaceholder> missingMotors = MissingMotorHelpers.findMissingMotors(rocket);
\r
229 TCMissingMotorDownloadAction motorfrag = TCMissingMotorDownloadAction.newInstance( missingMotors );
\r
230 getSupportFragmentManager().beginTransaction().add( motorfrag, MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG).commit();
\r
234 public void doNotFixMissingMotors() {
\r
235 displayWarningDialog();
\r
238 public void moveOnToViewer() {
\r
239 Intent i = new Intent(this,OpenRocketViewer.class);
\r