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
|
<item
|
||||||
android:id="@+id/menu_add"
|
android:id="@+id/menu_add"
|
||||||
android:icon="@drawable/ic_menu_add"
|
android:icon="@drawable/ic_menu_add"
|
||||||
android:orderInCategory="0"
|
android:orderInCategory="2"
|
||||||
android:showAsAction="always"
|
android:showAsAction="always"
|
||||||
android:title="@string/Add"/>
|
android:title="@string/Add"/>
|
||||||
|
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
<item
|
<item
|
||||||
android:id="@+id/menu_load"
|
android:id="@+id/menu_load"
|
||||||
android:icon="@drawable/ic_menu_archive"
|
android:icon="@drawable/ic_menu_archive"
|
||||||
android:orderInCategory="0"
|
android:orderInCategory="1"
|
||||||
android:showAsAction="always"
|
android:showAsAction="ifRoom"
|
||||||
android:title="@string/load"/>
|
android:title="@string/load"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/menu_save"
|
android:id="@+id/menu_save"
|
||||||
@ -22,12 +22,13 @@
|
|||||||
<item
|
<item
|
||||||
android:id="@+id/preference_menu_option"
|
android:id="@+id/preference_menu_option"
|
||||||
android:icon="@drawable/ic_menu_preferences"
|
android:icon="@drawable/ic_menu_preferences"
|
||||||
|
android:orderInCategory="10"
|
||||||
android:title="@string/Preferences"/>
|
android:title="@string/Preferences"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/menu_about"
|
android:id="@+id/menu_about"
|
||||||
|
android:icon="@drawable/ic_menu_info_details"
|
||||||
|
android:orderInCategory="10"
|
||||||
android:showAsAction="never"
|
android:showAsAction="never"
|
||||||
android:icon="@drawable/ic_menu_info_details"
|
|
||||||
|
|
||||||
android:title="@string/About"/>
|
android:title="@string/About"/>
|
||||||
|
|
||||||
</menu>
|
</menu>
|
@ -4,7 +4,7 @@
|
|||||||
<item
|
<item
|
||||||
android:id="@+id/menu_add"
|
android:id="@+id/menu_add"
|
||||||
android:icon="@drawable/ic_menu_add"
|
android:icon="@drawable/ic_menu_add"
|
||||||
android:orderInCategory="0"
|
android:orderInCategory="2"
|
||||||
android:showAsAction="always"
|
android:showAsAction="always"
|
||||||
android:title="@string/Add"/>
|
android:title="@string/Add"/>
|
||||||
|
|
||||||
|
@ -20,6 +20,8 @@ public class CurrentRocket {
|
|||||||
|
|
||||||
private RocketChangedEventHandler handler;
|
private RocketChangedEventHandler handler;
|
||||||
|
|
||||||
|
private boolean isModified = false;
|
||||||
|
|
||||||
public void setHandler( RocketChangedEventHandler handler ) {
|
public void setHandler( RocketChangedEventHandler handler ) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
@ -32,6 +34,7 @@ public class CurrentRocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void notifySimsChanged() {
|
public void notifySimsChanged() {
|
||||||
|
isModified = true;
|
||||||
if ( handler != null ) {
|
if ( handler != null ) {
|
||||||
handler.simsChangedMessage();
|
handler.simsChangedMessage();
|
||||||
}
|
}
|
||||||
@ -52,6 +55,7 @@ public class CurrentRocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String addNewMotorConfig() {
|
public String addNewMotorConfig() {
|
||||||
|
isModified = true;
|
||||||
String configId = rocketDocument.getRocket().newMotorConfigurationID();
|
String configId = rocketDocument.getRocket().newMotorConfigurationID();
|
||||||
if ( handler != null ) {
|
if ( handler != null ) {
|
||||||
handler.configsChangedMessage();
|
handler.configsChangedMessage();
|
||||||
@ -63,6 +67,7 @@ public class CurrentRocket {
|
|||||||
*/
|
*/
|
||||||
public void setRocketDocument(OpenRocketDocument rocketDocument) {
|
public void setRocketDocument(OpenRocketDocument rocketDocument) {
|
||||||
this.rocketDocument = rocketDocument;
|
this.rocketDocument = rocketDocument;
|
||||||
|
isModified = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WarningSet getWarnings() {
|
public WarningSet getWarnings() {
|
||||||
@ -81,14 +86,17 @@ public class CurrentRocket {
|
|||||||
this.fileUri = fileUri;
|
this.fileUri = fileUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isModified() {
|
||||||
|
return this.isModified;
|
||||||
|
}
|
||||||
|
|
||||||
public void saveOpenRocketDocument() throws IOException {
|
public void saveOpenRocketDocument() throws IOException {
|
||||||
OpenRocketSaver saver = new OpenRocketSaver();
|
OpenRocketSaver saver = new OpenRocketSaver();
|
||||||
StorageOptions options = new StorageOptions();
|
StorageOptions options = new StorageOptions();
|
||||||
options.setCompressionEnabled(true);
|
options.setCompressionEnabled(true);
|
||||||
options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);
|
options.setSimulationTimeSkip(StorageOptions.SIMULATION_DATA_ALL);
|
||||||
saver.save(new File(fileUri.getPath()),rocketDocument,options);
|
saver.save(new File(fileUri.getPath()),rocketDocument,options);
|
||||||
|
isModified = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -103,10 +103,12 @@ public class OpenRocketLoaderFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(OpenRocketLoaderResult result) {
|
protected void onPostExecute(OpenRocketLoaderResult result) {
|
||||||
super.onPostExecute(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);
|
Fragment progress = getActivity().getSupportFragmentManager().findFragmentByTag(PROGRESS_DIALOG_TAG);
|
||||||
if ( progress != null ) {
|
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 ) {
|
if ( listener != null ) {
|
||||||
listener.onOpenRocketFileLoaded(result);
|
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;
|
package net.sf.openrocket.android.rocket;
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import net.sf.openrocket.R;
|
import net.sf.openrocket.R;
|
||||||
import net.sf.openrocket.android.ActivityHelpers;
|
import net.sf.openrocket.android.ActivityHelpers;
|
||||||
import net.sf.openrocket.android.CurrentRocketHolder;
|
import net.sf.openrocket.android.CurrentRocketHolder;
|
||||||
@ -37,8 +35,6 @@ implements Simulations.OnSimulationSelectedListener
|
|||||||
private final static int TABSIZE = 4;
|
private final static int TABSIZE = 4;
|
||||||
|
|
||||||
private OpenRocketViewerPagerAdapter viewPagerAdapter;
|
private OpenRocketViewerPagerAdapter viewPagerAdapter;
|
||||||
|
|
||||||
private MenuItem saveAction;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -80,7 +76,14 @@ implements Simulations.OnSimulationSelectedListener
|
|||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
MenuInflater inflater = getSupportMenuInflater();
|
MenuInflater inflater = getSupportMenuInflater();
|
||||||
inflater.inflate(R.menu.rocket_viewer_option_menu, menu);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,13 +97,9 @@ implements Simulations.OnSimulationSelectedListener
|
|||||||
return true;
|
return true;
|
||||||
case R.id.menu_save:
|
case R.id.menu_save:
|
||||||
// FIXME - Probably want to open a dialog here.
|
// FIXME - Probably want to open a dialog here.
|
||||||
try {
|
getSupportFragmentManager().beginTransaction()
|
||||||
CurrentRocketHolder.getCurrentRocket().saveOpenRocketDocument();
|
.add( OpenRocketSaverFragment.newInstance(), "saver")
|
||||||
saveAction.setVisible(false);
|
.commitAllowingStateLoss();
|
||||||
invalidateOptionsMenu();
|
|
||||||
} catch ( IOException iex ) {
|
|
||||||
AndroidLogWrapper.d(OpenRocketViewer.class, iex.getMessage());
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
case android.R.id.home:
|
case android.R.id.home:
|
||||||
ActivityHelpers.goHome(this);
|
ActivityHelpers.goHome(this);
|
||||||
@ -154,13 +153,10 @@ implements Simulations.OnSimulationSelectedListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class RocketChangedEventHandler extends net.sf.openrocket.android.RocketChangedEventHandler {
|
private class RocketChangedEventHandler extends net.sf.openrocket.android.RocketChangedEventHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doSimsChanged() {
|
protected void doSimsChanged() {
|
||||||
if (saveAction != null ) {
|
invalidateOptionsMenu();
|
||||||
saveAction.setVisible(true);
|
|
||||||
invalidateOptionsMenu();
|
|
||||||
}
|
|
||||||
Simulations sims = (Simulations) viewPagerAdapter.getFragmentAtPos(SIMS_POS);
|
Simulations sims = (Simulations) viewPagerAdapter.getFragmentAtPos(SIMS_POS);
|
||||||
if ( sims != null ) {
|
if ( sims != null ) {
|
||||||
sims.refreshSimulationList();
|
sims.refreshSimulationList();
|
||||||
@ -169,10 +165,7 @@ implements Simulations.OnSimulationSelectedListener
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doMotorConfigsChanged() {
|
protected void doMotorConfigsChanged() {
|
||||||
if (saveAction != null ) {
|
invalidateOptionsMenu();
|
||||||
saveAction.setVisible(true);
|
|
||||||
invalidateOptionsMenu();
|
|
||||||
}
|
|
||||||
Configurations configs = (Configurations) viewPagerAdapter.getFragmentAtPos(CONFIGS_POS);
|
Configurations configs = (Configurations) viewPagerAdapter.getFragmentAtPos(CONFIGS_POS);
|
||||||
if ( configs != null ) {
|
if ( configs != null ) {
|
||||||
configs.refreshConfigsList();
|
configs.refreshConfigsList();
|
||||||
|
@ -20,19 +20,6 @@ public class ProgressDialogFragment extends DialogFragment {
|
|||||||
return fragment;
|
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
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user