Various fixes which should probably be broken into multiple commits:)
- Changed the way the ProgressDialog is dismissed from using some non-functional reflection code, to using a FragmentTransaction (which works) - Added code which shows the save button only when the CurrentRocket is modified. - Broke the save functionality into a Fragment with background Task so it is more reliable.
This commit is contained in:
parent
bbec3f7dbd
commit
294aa177c4
@ -4,7 +4,7 @@
|
||||
<item
|
||||
android:id="@+id/menu_add"
|
||||
android:icon="@drawable/ic_menu_add"
|
||||
android:orderInCategory="0"
|
||||
android:orderInCategory="2"
|
||||
android:showAsAction="always"
|
||||
android:title="@string/Add"/>
|
||||
|
||||
|
@ -4,8 +4,8 @@
|
||||
<item
|
||||
android:id="@+id/menu_load"
|
||||
android:icon="@drawable/ic_menu_archive"
|
||||
android:orderInCategory="0"
|
||||
android:showAsAction="always"
|
||||
android:orderInCategory="1"
|
||||
android:showAsAction="ifRoom"
|
||||
android:title="@string/load"/>
|
||||
<item
|
||||
android:id="@+id/menu_save"
|
||||
@ -22,12 +22,13 @@
|
||||
<item
|
||||
android:id="@+id/preference_menu_option"
|
||||
android:icon="@drawable/ic_menu_preferences"
|
||||
android:orderInCategory="10"
|
||||
android:title="@string/Preferences"/>
|
||||
<item
|
||||
android:id="@+id/menu_about"
|
||||
android:showAsAction="never"
|
||||
android:icon="@drawable/ic_menu_info_details"
|
||||
|
||||
android:orderInCategory="10"
|
||||
android:showAsAction="never"
|
||||
android:title="@string/About"/>
|
||||
|
||||
</menu>
|
@ -4,7 +4,7 @@
|
||||
<item
|
||||
android:id="@+id/menu_add"
|
||||
android:icon="@drawable/ic_menu_add"
|
||||
android:orderInCategory="0"
|
||||
android:orderInCategory="2"
|
||||
android:showAsAction="always"
|
||||
android:title="@string/Add"/>
|
||||
|
||||
|
@ -20,6 +20,8 @@ public class CurrentRocket {
|
||||
|
||||
private RocketChangedEventHandler handler;
|
||||
|
||||
private boolean isModified = false;
|
||||
|
||||
public void setHandler( RocketChangedEventHandler handler ) {
|
||||
this.handler = handler;
|
||||
}
|
||||
@ -32,6 +34,7 @@ public class CurrentRocket {
|
||||
}
|
||||
|
||||
public void notifySimsChanged() {
|
||||
isModified = true;
|
||||
if ( handler != null ) {
|
||||
handler.simsChangedMessage();
|
||||
}
|
||||
@ -52,6 +55,7 @@ public class CurrentRocket {
|
||||
}
|
||||
|
||||
public String addNewMotorConfig() {
|
||||
isModified = true;
|
||||
String configId = rocketDocument.getRocket().newMotorConfigurationID();
|
||||
if ( handler != null ) {
|
||||
handler.configsChangedMessage();
|
||||
@ -63,6 +67,7 @@ public class CurrentRocket {
|
||||
*/
|
||||
public void setRocketDocument(OpenRocketDocument rocketDocument) {
|
||||
this.rocketDocument = rocketDocument;
|
||||
isModified = false;
|
||||
}
|
||||
|
||||
public WarningSet getWarnings() {
|
||||
@ -81,14 +86,17 @@ public class CurrentRocket {
|
||||
this.fileUri = fileUri;
|
||||
}
|
||||
|
||||
public boolean isModified() {
|
||||
return this.isModified;
|
||||
}
|
||||
|
||||
public void saveOpenRocketDocument() throws IOException {
|
||||
OpenRocketSaver saver = new OpenRocketSaver();
|
||||
StorageOptions options = new StorageOptions();
|
||||
options.setCompressionEnabled(true);
|
||||
options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);
|
||||
saver.save(new File(fileUri.getPath()),rocketDocument,options);
|
||||
|
||||
isModified = false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -103,10 +103,12 @@ public class OpenRocketLoaderFragment extends Fragment {
|
||||
@Override
|
||||
protected void onPostExecute(OpenRocketLoaderResult result) {
|
||||
super.onPostExecute(result);
|
||||
AndroidLogWrapper.d(OpenRocketLoaderActivity.class,"Finished loading " + OpenRocketLoaderTask.this);
|
||||
AndroidLogWrapper.d(OpenRocketLoaderFragment.class,"Finished loading " + OpenRocketLoaderTask.this);
|
||||
Fragment progress = getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
|
||||
if ( progress != null ) {
|
||||
((ProgressDialogFragment)progress).dismissAllowingStateLoss();
|
||||
// Remove the fragment instead of trying to use DialogFragment.dismiss.
|
||||
// If the dialog is now currently shown, dismiss fails.
|
||||
getFragmentManager().beginTransaction().remove(progress).commitAllowingStateLoss();
|
||||
}
|
||||
if ( listener != null ) {
|
||||
listener.onOpenRocketFileLoaded(result);
|
||||
|
@ -0,0 +1,105 @@
|
||||
package net.sf.openrocket.android.rocket;
|
||||
|
||||
import net.sf.openrocket.android.CurrentRocketHolder;
|
||||
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
||||
import net.sf.openrocket.android.util.ProgressDialogFragment;
|
||||
import android.app.Activity;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class OpenRocketSaverFragment extends Fragment {
|
||||
|
||||
private final static String SAVING_MESSAGE = "Savinging file...";
|
||||
|
||||
public static OpenRocketSaverFragment newInstance() {
|
||||
OpenRocketSaverFragment frag = new OpenRocketSaverFragment();
|
||||
Bundle b = new Bundle();
|
||||
frag.setArguments(b);
|
||||
return frag;
|
||||
}
|
||||
|
||||
public interface OnOpenRocketFileSaved {
|
||||
public void onOpenRocketFileSaved( Boolean result );
|
||||
}
|
||||
|
||||
private OpenRocketSaverTask task;
|
||||
private OnOpenRocketFileSaved listener;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setRetainInstance(true);
|
||||
Bundle b = getArguments();
|
||||
if ( task == null ) {
|
||||
// since we retain instance state, task will be non-null if it is already loading.
|
||||
task = new OpenRocketSaverTask();
|
||||
task.execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle arg0) {
|
||||
super.onActivityCreated(arg0);
|
||||
Activity parent = getActivity();
|
||||
if ( parent instanceof OnOpenRocketFileSaved ) {
|
||||
listener = (OnOpenRocketFileSaved) parent;
|
||||
}
|
||||
}
|
||||
|
||||
public class OpenRocketSaverTask extends AsyncTask<Void, Void, Boolean> {
|
||||
|
||||
private final static String PROGRESS_DIALOG_TAG = "progress_dialog";
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
DialogFragment newFragment = ProgressDialogFragment.newInstance("", SAVING_MESSAGE);
|
||||
newFragment.show(getFragmentManager(), PROGRESS_DIALOG_TAG);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see android.os.AsyncTask#doInBackground(Params[])
|
||||
*/
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... arg0) {
|
||||
AndroidLogWrapper.d(OpenRocketSaverTask.class, "doInBackgroud");
|
||||
|
||||
try {
|
||||
CurrentRocketHolder.getCurrentRocket().saveOpenRocketDocument();
|
||||
return true;
|
||||
} catch (Throwable ex) {
|
||||
AndroidLogWrapper.e(OpenRocketSaverTask.class, "doInBackground rocketLaoder.load threw {}", ex);
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
super.onPostExecute(result);
|
||||
AndroidLogWrapper.d(OpenRocketSaverFragment.class,"Finished saving " + OpenRocketSaverTask.this);
|
||||
Fragment progress = getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
|
||||
if ( progress != null ) {
|
||||
// Remove the fragment instead of trying to use DialogFragment.dismiss.
|
||||
// If the dialog is now currently shown, dismiss fails.
|
||||
getFragmentManager().beginTransaction().remove(progress).commitAllowingStateLoss();
|
||||
}
|
||||
if ( listener != null ) {
|
||||
listener.onOpenRocketFileSaved(result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package net.sf.openrocket.android.rocket;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import net.sf.openrocket.R;
|
||||
import net.sf.openrocket.android.ActivityHelpers;
|
||||
import net.sf.openrocket.android.CurrentRocketHolder;
|
||||
@ -38,8 +36,6 @@ implements Simulations.OnSimulationSelectedListener
|
||||
|
||||
private OpenRocketViewerPagerAdapter viewPagerAdapter;
|
||||
|
||||
private MenuItem saveAction;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -80,7 +76,14 @@ implements Simulations.OnSimulationSelectedListener
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getSupportMenuInflater();
|
||||
inflater.inflate(R.menu.rocket_viewer_option_menu, menu);
|
||||
saveAction = menu.findItem(R.id.menu_save);
|
||||
MenuItem saveAction = menu.findItem(R.id.menu_save);
|
||||
if ( CurrentRocketHolder.getCurrentRocket().isModified() ) {
|
||||
saveAction.setVisible(true);
|
||||
saveAction.setShowAsAction( MenuItem.SHOW_AS_ACTION_ALWAYS );
|
||||
} else {
|
||||
saveAction.setVisible(false);
|
||||
saveAction.setShowAsAction( MenuItem.SHOW_AS_ACTION_NEVER );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -94,13 +97,9 @@ implements Simulations.OnSimulationSelectedListener
|
||||
return true;
|
||||
case R.id.menu_save:
|
||||
// FIXME - Probably want to open a dialog here.
|
||||
try {
|
||||
CurrentRocketHolder.getCurrentRocket().saveOpenRocketDocument();
|
||||
saveAction.setVisible(false);
|
||||
invalidateOptionsMenu();
|
||||
} catch ( IOException iex ) {
|
||||
AndroidLogWrapper.d(OpenRocketViewer.class, iex.getMessage());
|
||||
}
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add( OpenRocketSaverFragment.newInstance(), "saver")
|
||||
.commitAllowingStateLoss();
|
||||
return true;
|
||||
case android.R.id.home:
|
||||
ActivityHelpers.goHome(this);
|
||||
@ -157,10 +156,7 @@ implements Simulations.OnSimulationSelectedListener
|
||||
|
||||
@Override
|
||||
protected void doSimsChanged() {
|
||||
if (saveAction != null ) {
|
||||
saveAction.setVisible(true);
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
Simulations sims = (Simulations) viewPagerAdapter.getFragmentAtPos(SIMS_POS);
|
||||
if ( sims != null ) {
|
||||
sims.refreshSimulationList();
|
||||
@ -169,10 +165,7 @@ implements Simulations.OnSimulationSelectedListener
|
||||
|
||||
@Override
|
||||
protected void doMotorConfigsChanged() {
|
||||
if (saveAction != null ) {
|
||||
saveAction.setVisible(true);
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
Configurations configs = (Configurations) viewPagerAdapter.getFragmentAtPos(CONFIGS_POS);
|
||||
if ( configs != null ) {
|
||||
configs.refreshConfigsList();
|
||||
|
@ -20,19 +20,6 @@ public class ProgressDialogFragment extends DialogFragment {
|
||||
return fragment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose the private method to allow dismissing a dialog after saveInstanceState.
|
||||
*/
|
||||
public void dismissAllowingStateLoss() {
|
||||
try {
|
||||
Method dismissInternalMethod = ProgressDialogFragment.class.getMethod("dismissInternal", Boolean.class);
|
||||
dismissInternalMethod.setAccessible(true);
|
||||
dismissInternalMethod.invoke(this, true);
|
||||
} catch (Exception ex ) {
|
||||
AndroidLogWrapper.d(ProgressDialogFragment.class, "Exception calling dismissAllowingStateInteral");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
Loading…
x
Reference in New Issue
Block a user