create changelog entry
[debian/openrocket] / android / src / net / sf / openrocket / android / rocket / OpenRocketLoaderActivity.java
1 package net.sf.openrocket.android.rocket;\r
2 \r
3 import java.io.File;\r
4 import java.util.Set;\r
5 \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
23 \r
24 import com.actionbarsherlock.app.SherlockFragmentActivity;\r
25 \r
26 public class OpenRocketLoaderActivity extends SherlockFragmentActivity\r
27 implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnOpenRocketFileLoaded\r
28 {\r
29 \r
30         private static final int PICK_ORK_FILE_RESULT = 1;\r
31 \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
34 \r
35         /*\r
36          * Set to true when we have started to load a file.  Is saved in InstanceState.\r
37          */\r
38         private boolean isLoading = false;\r
39         /*\r
40          * Set to the Uri of the file we are supposed to load.  Is saved in InstanceState.\r
41          */\r
42         private Uri fileToLoad = null;\r
43         \r
44         protected boolean isLoading() {\r
45                 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading " + this.hashCode());\r
46                 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading = " + isLoading);\r
47                 return isLoading;\r
48         }\r
49 \r
50         @Override\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
56                         fileToLoad = file;\r
57                         loadOrkFile();\r
58                 } else {\r
59                 }\r
60         }\r
61 \r
62         @Override\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
69                 }\r
70                 super.onSaveInstanceState(outState);\r
71         }\r
72 \r
73         @Override\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
80                 }\r
81                 super.onRestoreInstanceState(savedInstanceState);\r
82         }\r
83 \r
84         @Override\r
85         protected void onResume() {\r
86                 AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onResume");\r
87                 super.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
90                         loadOrkFile();\r
91                 }\r
92         }\r
93 \r
94         /* (non-Javadoc)\r
95          * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)\r
96          */\r
97         @Override\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
104                                 fileToLoad = file;\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
115                         }\r
116                         break;\r
117                 default:\r
118                         super.onActivityResult(requestCode, resultCode, data);\r
119                 }\r
120         }\r
121 \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
126 \r
127                 boolean useinternalbrowser = pref.getBoolean(key, true);\r
128 \r
129                 if ( useinternalbrowser ) {\r
130                         Intent intent = new Intent(OpenRocketLoaderActivity.this, SimpleFileBrowser.class);\r
131                         startActivityForResult(intent,PICK_ORK_FILE_RESULT);\r
132                 } else {\r
133                         try {\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
144                         }\r
145                 }               \r
146         }\r
147 \r
148         private void loadOrkFile( ) {\r
149                 // a little protection.\r
150                 if ( fileToLoad == null ) {\r
151                         return;\r
152                 }\r
153                 isLoading = true;\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
158 \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
163 \r
164         }\r
165 \r
166         /**\r
167          * Called by the OpenRocketLoaderTask when it completes.\r
168          * is default visibility so it can be called from this package.\r
169          * \r
170          * @param result\r
171          */\r
172         public void onOpenRocketFileLoaded(OpenRocketLoaderResult result) {\r
173                 if ( result.loadingError != null ) {\r
174 \r
175                         ErrorLoadingFileDialogFragment errorDialog = ErrorLoadingFileDialogFragment.newInstance(R.string.loadingErrorMessage, result.loadingError.getLocalizedMessage());\r
176                         errorDialog.show(getSupportFragmentManager(),"errorDialog");\r
177 \r
178                 } else {\r
179                         CurrentRocketHolder.getCurrentRocket().setRocketDocument( result.rocket );\r
180                         CurrentRocketHolder.getCurrentRocket().setWarnings( result.warnings );\r
181 \r
182                         updateMissingMotors();\r
183                 }\r
184         }\r
185 \r
186         private void updateMissingMotors() {\r
187                 Rocket rocket = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getRocket();\r
188                 Set<ThrustCurveMotorPlaceholder> missingMotors = MissingMotorHelpers.findMissingMotors(rocket);\r
189 \r
190                 if ( missingMotors.size() > 0 ) {\r
191                         MissingMotorDialogFragment missingMotorDialog = MissingMotorDialogFragment.newInstance( missingMotors );\r
192                         missingMotorDialog.show(getSupportFragmentManager(), MISSING_MOTOR_DIAG_FRAGMENT_TAG);\r
193                         return;\r
194                 }\r
195 \r
196                 displayWarningDialog();\r
197         }\r
198 \r
199         /**\r
200          * Called when the TCMissingMotorDownload process finishes.\r
201          */\r
202         @Override\r
203         public void onTCQueryComplete(String message) {\r
204 \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
209 \r
210                 displayWarningDialog();\r
211         }\r
212 \r
213         private void displayWarningDialog() {\r
214                 WarningSet warnings = CurrentRocketHolder.getCurrentRocket().getWarnings();\r
215                 if (warnings == null || warnings.isEmpty()) {\r
216                 } else {\r
217                         DialogFragment newFragment = WarningDialogFragment.newInstance();\r
218                         newFragment.show(getSupportFragmentManager(), "dialog");\r
219                         return;\r
220                 }\r
221 \r
222                 moveOnToViewer();\r
223         }\r
224 \r
225         public void doFixMissingMotors() {\r
226                 Rocket rocket = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getRocket();\r
227                 Set<ThrustCurveMotorPlaceholder> missingMotors = MissingMotorHelpers.findMissingMotors(rocket);\r
228 \r
229                 TCMissingMotorDownloadAction motorfrag = TCMissingMotorDownloadAction.newInstance( missingMotors );\r
230                 getSupportFragmentManager().beginTransaction().add( motorfrag, MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG).commit();\r
231 \r
232         }\r
233 \r
234         public void doNotFixMissingMotors() {\r
235                 displayWarningDialog();\r
236         }\r
237 \r
238         public void doDismissErrorDialog() {\r
239                 isLoading = false;\r
240                 fileToLoad = null;\r
241         }\r
242         \r
243         public void moveOnToViewer() {\r
244                 isLoading = false;\r
245                 Intent i = new Intent(this,OpenRocketViewer.class);\r
246                 startActivity(i);\r
247                 finish();\r
248         }\r
249 }\r