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.content.ActivityNotFoundException;
\r
16 import android.content.Intent;
\r
17 import android.content.SharedPreferences;
\r
18 import android.content.res.Resources;
\r
19 import android.net.Uri;
\r
20 import android.os.Bundle;
\r
21 import android.preference.PreferenceManager;
\r
22 import android.support.v4.app.DialogFragment;
\r
24 import com.actionbarsherlock.app.SherlockFragmentActivity;
\r
26 public class OpenRocketLoaderActivity extends SherlockFragmentActivity
\r
27 implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnOpenRocketFileLoaded
\r
30 private static final int PICK_ORK_FILE_RESULT = 1;
\r
32 private final static String MISSING_MOTOR_DIAG_FRAGMENT_TAG = "missingmotordialog";
\r
33 private final static String MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG = "missingmotortask";
\r
36 * Set to true when we have started to load a file. Is saved in InstanceState.
\r
38 private boolean isLoading = false;
\r
40 * Set to the Uri of the file we are supposed to load. Is saved in InstanceState.
\r
42 private Uri fileToLoad = null;
\r
44 protected boolean isLoading() {
\r
45 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading " + this.hashCode());
\r
46 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading = " + isLoading);
\r
51 protected void onPostCreate(Bundle savedInstanceState) {
\r
52 super.onPostCreate(savedInstanceState);
\r
53 Intent i = getIntent();
\r
54 if (Intent.ACTION_VIEW.equals(i.getAction()) && i.getData() != null ) {
\r
55 Uri file = i.getData();
\r
63 protected void onSaveInstanceState(Bundle outState) {
\r
64 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onSaveInstanceState " + this.hashCode());
\r
65 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading = " + isLoading);
\r
66 outState.putBoolean("isLoading", isLoading);
\r
67 if ( fileToLoad != null ) {
\r
68 outState.putParcelable("fileToLoad", fileToLoad);
\r
70 super.onSaveInstanceState(outState);
\r
74 protected void onRestoreInstanceState(Bundle savedInstanceState) {
\r
75 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onRestoreInstanceState");
\r
76 isLoading = savedInstanceState.getBoolean("isLoading",false);
\r
77 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading = " + isLoading);
\r
78 if ( savedInstanceState.containsKey("fileToLoad") ) {
\r
79 fileToLoad = savedInstanceState.getParcelable("fileToLoad");
\r
81 super.onRestoreInstanceState(savedInstanceState);
\r
85 protected void onResume() {
\r
86 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onResume");
\r
88 // Start loading a file if we have a file and are not already loading one.
\r
89 if ( fileToLoad != null && !isLoading ) {
\r
95 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
\r
98 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
\r
99 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onActivityResult");
\r
100 switch ( requestCode ) {
\r
101 case PICK_ORK_FILE_RESULT:
\r
102 if(resultCode==RESULT_OK){
\r
103 Uri file = data.getData();
\r
105 // It would be nice to just start loading the file - but that doesn't work correctly.
\r
106 // I'm uncertain if it is a bug in Android 14/15 or a bug in the v4 support library.
\r
107 // essentially what happens is, when the FileBrowserActivity is brought up,
\r
108 // this activity goes through the saveInstanceState calls to push it to the background.
\r
109 // When the FileBrowserActivity returns the result, this.onActivityResult is called
\r
110 // prior to any of the other lifecycle methods (onRestoreInstanceState as documented, but onStart is
\r
111 // a bug. Since onStart hasn't been called, this activity is not able to create fragments - which
\r
112 // are used to indicate progress etc.
\r
113 // Instead of calling loadOrkFile() here, we push the file Uri into a member variable,
\r
114 // then check the member variable in onResume to actuall kick off the work.
\r
118 super.onActivityResult(requestCode, resultCode, data);
\r
122 protected void pickOrkFiles( ) {
\r
123 Resources resources = this.getResources();
\r
124 String key = resources.getString(R.string.PreferenceUseInternalFileBrowserOption);
\r
125 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
\r
127 boolean useinternalbrowser = pref.getBoolean(key, true);
\r
129 if ( useinternalbrowser ) {
\r
130 Intent intent = new Intent(OpenRocketLoaderActivity.this, SimpleFileBrowser.class);
\r
131 startActivityForResult(intent,PICK_ORK_FILE_RESULT);
\r
134 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
\r
135 intent.setType("file/*");
\r
136 startActivityForResult(intent,PICK_ORK_FILE_RESULT);
\r
137 } catch ( ActivityNotFoundException ex ) {
\r
138 // No activity for ACTION_GET_CONTENT use internal file browser
\r
139 // update the preference value.
\r
140 pref.edit().putBoolean(key, false).commit();
\r
141 // fire our browser
\r
142 Intent intent = new Intent(OpenRocketLoaderActivity.this, SimpleFileBrowser.class);
\r
143 startActivityForResult(intent,PICK_ORK_FILE_RESULT);
\r
148 private void loadOrkFile( ) {
\r
149 // a little protection.
\r
150 if ( fileToLoad == null ) {
\r
154 CurrentRocketHolder.getCurrentRocket().setFileUri( fileToLoad );
\r
155 AndroidLogWrapper.d(OpenRocketLoaderActivity.class,"Use ork file: " + fileToLoad);
\r
156 String path = fileToLoad.getPath();
\r
157 File orkFile = new File(path);
\r
159 // Also need commitAllowingState loss because of a bug in v4 dialog show.
\r
160 getSupportFragmentManager().beginTransaction()
\r
161 .add( OpenRocketLoaderFragment.newInstance(orkFile), "loader")
\r
162 .commitAllowingStateLoss();
\r
167 * Called by the OpenRocketLoaderTask when it completes.
\r
168 * is default visibility so it can be called from this package.
\r
172 public void onOpenRocketFileLoaded(OpenRocketLoaderResult result) {
\r
173 if ( result.loadingError != null ) {
\r
175 ErrorLoadingFileDialogFragment errorDialog = ErrorLoadingFileDialogFragment.newInstance(R.string.loadingErrorMessage, result.loadingError.getLocalizedMessage());
\r
176 errorDialog.show(getSupportFragmentManager(),"errorDialog");
\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 MissingMotorDialogFragment missingMotorDialog = MissingMotorDialogFragment.newInstance( missingMotors );
\r
192 missingMotorDialog.show(getSupportFragmentManager(), MISSING_MOTOR_DIAG_FRAGMENT_TAG);
\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 doDismissErrorDialog() {
\r
243 public void moveOnToViewer() {
\r
245 Intent i = new Intent(this,OpenRocketViewer.class);
\r