X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=android%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fandroid%2Frocket%2FOpenRocketLoaderActivity.java;h=9a3a84310d9890cfc508b68abf28f6ef9044e4a2;hb=121456e8dbb3d43058bdc5aa74d59a4e08d06e81;hp=30c313448be5bbb573a7ad905f3d4cd68d98ded3;hpb=7789023faa0e3a92472dbf9f285c38563fe03ed8;p=debian%2Fopenrocket diff --git a/android/src/net/sf/openrocket/android/rocket/OpenRocketLoaderActivity.java b/android/src/net/sf/openrocket/android/rocket/OpenRocketLoaderActivity.java index 30c31344..9a3a8431 100644 --- a/android/src/net/sf/openrocket/android/rocket/OpenRocketLoaderActivity.java +++ b/android/src/net/sf/openrocket/android/rocket/OpenRocketLoaderActivity.java @@ -5,74 +5,161 @@ import java.util.Set; import net.sf.openrocket.R; import net.sf.openrocket.aerodynamics.WarningSet; -import net.sf.openrocket.android.Application; +import net.sf.openrocket.android.CurrentRocketHolder; +import net.sf.openrocket.android.filebrowser.SimpleFileBrowser; import net.sf.openrocket.android.thrustcurve.TCMissingMotorDownloadAction; import net.sf.openrocket.android.thrustcurve.TCQueryAction; import net.sf.openrocket.android.util.AndroidLogWrapper; import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder; -import android.app.ProgressDialog; +import net.sf.openrocket.rocketcomponent.Rocket; +import android.content.ActivityNotFoundException; import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.app.DialogFragment; -import android.support.v4.app.FragmentActivity; -public class OpenRocketLoaderActivity extends FragmentActivity -implements TCQueryAction.OnComplete +import com.actionbarsherlock.app.SherlockFragmentActivity; + +public class OpenRocketLoaderActivity extends SherlockFragmentActivity +implements TCQueryAction.OnTCQueryCompleteListener, OpenRocketLoaderFragment.OnOpenRocketFileLoaded { - private OpenRocketLoaderResult result; + private static final int PICK_ORK_FILE_RESULT = 1; + + private final static String MISSING_MOTOR_DIAG_FRAGMENT_TAG = "missingmotordialog"; + private final static String MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG = "missingmotortask"; + + /* + * Set to true when we have started to load a file. Is saved in InstanceState. + */ + private boolean isLoading = false; + /* + * Set to the Uri of the file we are supposed to load. Is saved in InstanceState. + */ + private Uri fileToLoad = null; - private Set missingMotors; - private OpenRocketLoaderTask task; - private ProgressDialog progress; - private DialogFragment missingMotorDialog; - private TCMissingMotorDownloadAction missingMotorDownloadAction; + protected boolean isLoading() { + AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading " + this.hashCode()); + AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading = " + isLoading); + return isLoading; + } @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main); - missingMotorDownloadAction = new TCMissingMotorDownloadAction(this); - if ( savedInstanceState == null || savedInstanceState.getBoolean("isLoading", false) == false ) { - Intent i = getIntent(); + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + Intent i = getIntent(); + if (Intent.ACTION_VIEW.equals(i.getAction()) && i.getData() != null ) { Uri file = i.getData(); - loadOrkFile(file); + fileToLoad = file; + loadOrkFile(); } else { - progress = ProgressDialog.show(this, "Loading file", ""); } } @Override protected void onSaveInstanceState(Bundle outState) { + AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onSaveInstanceState " + this.hashCode()); + AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading = " + isLoading); + outState.putBoolean("isLoading", isLoading); + if ( fileToLoad != null ) { + outState.putParcelable("fileToLoad", fileToLoad); + } super.onSaveInstanceState(outState); - outState.putBoolean("isLoading", true); } @Override - protected void onDestroy() { - if ( progress != null ) { - if ( progress.isShowing() ) { - progress.dismiss(); - } - progress = null; + protected void onRestoreInstanceState(Bundle savedInstanceState) { + AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onRestoreInstanceState"); + isLoading = savedInstanceState.getBoolean("isLoading",false); + AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "isLoading = " + isLoading); + if ( savedInstanceState.containsKey("fileToLoad") ) { + fileToLoad = savedInstanceState.getParcelable("fileToLoad"); } - if ( missingMotorDownloadAction != null ) { - missingMotorDownloadAction.dismiss(); + super.onRestoreInstanceState(savedInstanceState); + } + + @Override + protected void onResume() { + AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onResume"); + super.onResume(); + // Start loading a file if we have a file and are not already loading one. + if ( fileToLoad != null && !isLoading ) { + loadOrkFile(); + } + } + + /* (non-Javadoc) + * @see android.app.Activity#onActivityResult(int, int, android.content.Intent) + */ + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + AndroidLogWrapper.d(OpenRocketLoaderActivity.class, "onActivityResult"); + switch ( requestCode ) { + case PICK_ORK_FILE_RESULT: + if(resultCode==RESULT_OK){ + Uri file = data.getData(); + fileToLoad = file; + // It would be nice to just start loading the file - but that doesn't work correctly. + // I'm uncertain if it is a bug in Android 14/15 or a bug in the v4 support library. + // essentially what happens is, when the FileBrowserActivity is brought up, + // this activity goes through the saveInstanceState calls to push it to the background. + // When the FileBrowserActivity returns the result, this.onActivityResult is called + // prior to any of the other lifecycle methods (onRestoreInstanceState as documented, but onStart is + // a bug. Since onStart hasn't been called, this activity is not able to create fragments - which + // are used to indicate progress etc. + // Instead of calling loadOrkFile() here, we push the file Uri into a member variable, + // then check the member variable in onResume to actuall kick off the work. + } + break; + default: + super.onActivityResult(requestCode, resultCode, data); } + } + + protected void pickOrkFiles( ) { + Resources resources = this.getResources(); + String key = resources.getString(R.string.PreferenceUseInternalFileBrowserOption); + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); + + boolean useinternalbrowser = pref.getBoolean(key, true); - super.onDestroy(); + if ( useinternalbrowser ) { + Intent intent = new Intent(OpenRocketLoaderActivity.this, SimpleFileBrowser.class); + startActivityForResult(intent,PICK_ORK_FILE_RESULT); + } else { + try { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("file/*"); + startActivityForResult(intent,PICK_ORK_FILE_RESULT); + } catch ( ActivityNotFoundException ex ) { + // No activity for ACTION_GET_CONTENT use internal file browser + // update the preference value. + pref.edit().putBoolean(key, false).commit(); + // fire our browser + Intent intent = new Intent(OpenRocketLoaderActivity.this, SimpleFileBrowser.class); + startActivityForResult(intent,PICK_ORK_FILE_RESULT); + } + } } - private void loadOrkFile( Uri file ) { - AndroidLogWrapper.d(OpenRocketLoaderActivity.class,"Use ork file: " + file); - String path = file.getPath(); + private void loadOrkFile( ) { + // a little protection. + if ( fileToLoad == null ) { + return; + } + isLoading = true; + CurrentRocketHolder.getCurrentRocket().setFileUri( fileToLoad ); + AndroidLogWrapper.d(OpenRocketLoaderActivity.class,"Use ork file: " + fileToLoad); + String path = fileToLoad.getPath(); File orkFile = new File(path); - progress = ProgressDialog.show(this, "Loading file", ""); - task = new OpenRocketLoaderTask(this); - - task.execute(orkFile); + // Also need commitAllowingState loss because of a bug in v4 dialog show. + getSupportFragmentManager().beginTransaction() + .add( OpenRocketLoaderFragment.newInstance(orkFile), "loader") + .commitAllowingStateLoss(); } @@ -82,23 +169,27 @@ implements TCQueryAction.OnComplete * * @param result */ - void finishedLoading(OpenRocketLoaderResult result) { - if ( progress != null && progress.isShowing() ) { - progress.dismiss(); - } - this.result = result; - ((Application)OpenRocketLoaderActivity.this.getApplication()).setRocketDocument( result.rocket ); + public void onOpenRocketFileLoaded(OpenRocketLoaderResult result) { + if ( result.loadingError != null ) { - updateMissingMotors(); + ErrorLoadingFileDialogFragment errorDialog = ErrorLoadingFileDialogFragment.newInstance(R.string.loadingErrorMessage, result.loadingError.getLocalizedMessage()); + errorDialog.show(getSupportFragmentManager(),"errorDialog"); + + } else { + CurrentRocketHolder.getCurrentRocket().setRocketDocument( result.rocket ); + CurrentRocketHolder.getCurrentRocket().setWarnings( result.warnings ); + updateMissingMotors(); + } } private void updateMissingMotors() { - missingMotors = MissingMotorHelpers.findMissingMotors(result.rocket.getRocket()); + Rocket rocket = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getRocket(); + Set missingMotors = MissingMotorHelpers.findMissingMotors(rocket); if ( missingMotors.size() > 0 ) { - missingMotorDialog = MissingMotorDialogFragment.newInstance( missingMotors ); - missingMotorDialog.show(getSupportFragmentManager(), "missing motors"); + MissingMotorDialogFragment missingMotorDialog = MissingMotorDialogFragment.newInstance( missingMotors ); + missingMotorDialog.show(getSupportFragmentManager(), MISSING_MOTOR_DIAG_FRAGMENT_TAG); return; } @@ -109,19 +200,20 @@ implements TCQueryAction.OnComplete * Called when the TCMissingMotorDownload process finishes. */ @Override - public void onComplete() { + public void onTCQueryComplete(String message) { + Rocket rocket = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getRocket(); + WarningSet warnings = CurrentRocketHolder.getCurrentRocket().getWarnings(); // Need to update the motor references. - MissingMotorHelpers.updateMissingMotors(result.rocket.getRocket(), result.warnings); + MissingMotorHelpers.updateMissingMotors(rocket, warnings); displayWarningDialog(); } private void displayWarningDialog() { - WarningSet warnings = result.warnings; + WarningSet warnings = CurrentRocketHolder.getCurrentRocket().getWarnings(); if (warnings == null || warnings.isEmpty()) { } else { - // TODO - Build a warning listing dialog DialogFragment newFragment = WarningDialogFragment.newInstance(); newFragment.show(getSupportFragmentManager(), "dialog"); return; @@ -131,20 +223,25 @@ implements TCQueryAction.OnComplete } public void doFixMissingMotors() { + Rocket rocket = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getRocket(); + Set missingMotors = MissingMotorHelpers.findMissingMotors(rocket); - missingMotorDialog.dismiss(); - - missingMotorDownloadAction.setMissingMotors(missingMotors); - missingMotorDownloadAction.start(); + TCMissingMotorDownloadAction motorfrag = TCMissingMotorDownloadAction.newInstance( missingMotors ); + getSupportFragmentManager().beginTransaction().add( motorfrag, MISSING_MOTOR_DOWNLOAD_FRAGMENT_TAG).commit(); } public void doNotFixMissingMotors() { - missingMotorDialog.dismiss(); displayWarningDialog(); } - private void moveOnToViewer() { + public void doDismissErrorDialog() { + isLoading = false; + fileToLoad = null; + } + + public void moveOnToViewer() { + isLoading = false; Intent i = new Intent(this,OpenRocketViewer.class); startActivity(i); finish();