Refactor restart code so app can support restarting

Add Landscape Mode setting to app
This commit is contained in:
drdev
2015-09-26 17:43:49 +00:00
parent 6fe6df1c50
commit 28316c0fe9
12 changed files with 88 additions and 25 deletions

View File

@@ -7,5 +7,6 @@ public interface IDeviceAdapter {
String getDownloadsDir();
boolean openFile(String filename);
void setLandscapeMode(boolean landscapeMode);
void restart();
void exit();
}

View File

@@ -34,6 +34,7 @@ public class ForgePreferences extends PreferencesStore<ForgePreferences.FPref> {
CONSTRUCTED_P6_DECK_STATE(""),
CONSTRUCTED_P7_DECK_STATE(""),
CONSTRUCTED_P8_DECK_STATE(""),
UI_LANDSCAPE_MODE ("false"),
UI_COMPACT_MAIN_MENU ("false"),
UI_USE_OLD ("false"),
UI_RANDOM_FOIL ("false"),

View File

@@ -0,0 +1,77 @@
package forge.util;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.List;
/**
* Restarts a java app.
* Credit: http://leolewis.website.org/wordpress/2011/07/06/programmatically-restart-a-java-application/
*/
public class RestartUtil {
/**
* Sun property pointing the main class and its arguments.
* Might not be defined on non Hotspot VM implementations.
*/
public static final String SUN_JAVA_COMMAND = "sun.java.command";
/**
* Restart the current Java application.
* @param runBeforeRestart some custom code to be run before restarting
*/
public static boolean prepareForRestart() {
try {
// java binary
final String java = System.getProperty("java.home")
+ File.separator + "bin" + File.separator + "java";
// vm arguments
final List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
final StringBuffer vmArgsOneLine = new StringBuffer();
for (final String arg : vmArguments) {
// if it's the agent argument : we ignore it otherwise the
// address of the old application and the new one will be in conflict
if (!arg.contains("-agentlib")) {
vmArgsOneLine.append(arg);
vmArgsOneLine.append(" ");
}
}
// init the command to execute, add the vm args
final StringBuffer cmd = new StringBuffer("\"" + java + "\" " + vmArgsOneLine);
// program main and program arguments
final String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");
// program main is a jar
if (mainCommand[0].endsWith(".jar")) {
// if it's a jar, add -jar mainJar
cmd.append("-jar " + new File(mainCommand[0]).getPath());
} else {
// else it's a .class, add the classpath and mainClass
cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
}
// finally add program arguments
for (int i = 1; i < mainCommand.length; i++) {
cmd.append(" ");
cmd.append(mainCommand[i]);
}
// execute the command in a shutdown hook, to be sure that all the
// resources have been disposed before restarting the application
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
Runtime.getRuntime().exec(cmd.toString());
} catch (final IOException e) {
//e.printStackTrace();
}
}
});
return true;
}
catch (final Exception ex) {
//ErrorViewer.showError(ex, "Restart \"%s\" exception", "");
return false;
}
}
}