From c5cad6e4b026fdd35866c45d2c8f0601cd388356 Mon Sep 17 00:00:00 2001 From: drdev Date: Sat, 23 Aug 2014 16:46:41 +0000 Subject: [PATCH] Support running installer after exiting --- forge-gui-android/src/forge/app/Main.java | 12 +++++- forge-gui-mobile/src/forge/Forge.java | 32 ++++++++++------ .../src/forge/assets/AssetsDownloader.java | 38 +++++++++++++------ .../src/forge/error/BugReportDialog.java | 3 +- 4 files changed, 58 insertions(+), 27 deletions(-) diff --git a/forge-gui-android/src/forge/app/Main.java b/forge-gui-android/src/forge/app/Main.java index 2da6d49f108..d04900aa4aa 100644 --- a/forge-gui-android/src/forge/app/Main.java +++ b/forge-gui-android/src/forge/app/Main.java @@ -3,9 +3,11 @@ package forge.app; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; +import android.content.Intent; import android.content.pm.ActivityInfo; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; @@ -15,6 +17,7 @@ import com.badlogic.gdx.backends.android.AndroidApplication; import forge.Forge; import forge.interfaces.INetworkConnection; +import forge.util.Callback; import forge.util.FileUtil; public class Main extends AndroidApplication { @@ -42,9 +45,14 @@ public class Main extends AndroidApplication { } initialize(Forge.getApp(new AndroidClipboard(), new AndroidNetworkConnection(), - assetsDir, new Runnable() { + assetsDir, new Callback() { @Override - public void run() { + public void run(String runOnExit) { + if (runOnExit != null) { + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(runOnExit)); + startActivity(intent); + } + //ensure process doesn't stick around after exiting android.os.Process.killProcess(android.os.Process.myPid()); } diff --git a/forge-gui-mobile/src/forge/Forge.java b/forge-gui-mobile/src/forge/Forge.java index 1c2443f6b63..6c2ecb24e17 100644 --- a/forge-gui-mobile/src/forge/Forge.java +++ b/forge-gui-mobile/src/forge/Forge.java @@ -42,17 +42,19 @@ public class Forge implements ApplicationListener { private static final ApplicationListener app = new Forge(); private static Clipboard clipboard; private static INetworkConnection networkConnection; - private static Runnable onExit; + private static Callback onExit; private static int screenWidth; private static int screenHeight; private static Graphics graphics; private static FScreen currentScreen; private static SplashScreen splashScreen; private static KeyInputAdapter keyInputAdapter; + private static String runAfterExit; + private static boolean exited; private static final SoundSystem soundSystem = new SoundSystem(); private static final Stack screens = new Stack(); - public static ApplicationListener getApp(Clipboard clipboard0, INetworkConnection networkConnection0, String assetDir0, Runnable onExit0) { + public static ApplicationListener getApp(Clipboard clipboard0, INetworkConnection networkConnection0, String assetDir0, Callback onExit0) { if (GuiBase.getInterface() == null) { clipboard = clipboard0; networkConnection = networkConnection0; @@ -88,10 +90,8 @@ public class Forge implements ApplicationListener { @Override public void run() { //see if app or assets need updating - if (!AssetsDownloader.checkForUpdates(splashScreen)) { - Gdx.app.exit(); //exit if user chose to exit or couldn't download required assets - return; - } + AssetsDownloader.checkForUpdates(splashScreen); + if (exited) { return; } //don't continue if user chose to exit or couldn't download required assets FModel.initialize(splashScreen.getProgressBar()); @@ -145,7 +145,7 @@ public class Forge implements ApplicationListener { public static void back() { if (screens.size() < 2) { - exit(); //prompt to exit if attempting to go back from home screen + exit(false, null); //prompt to exit if attempting to go back from home screen return; } currentScreen.onClose(new Callback() { @@ -159,15 +159,25 @@ public class Forge implements ApplicationListener { }); } - public static void exit() { - FOptionPane.showConfirmDialog("Are you sure you wish to exit Forge?", "Exit Forge", "Exit", "Cancel", new Callback() { + public static void exit(boolean silent, final String runAfterExit0) { + if (exited) { return; } //don't allow exiting multiple times + + Callback callback = new Callback() { @Override public void run(Boolean result) { if (result) { + exited = true; + runAfterExit = runAfterExit0; Gdx.app.exit(); } } - }); + }; + if (silent) { + callback.run(true); + } + else { + FOptionPane.showConfirmDialog("Are you sure you wish to exit Forge?", "Exit Forge", "Exit", "Cancel", callback); + } } public static void openScreen(final FScreen screen0) { @@ -283,7 +293,7 @@ public class Forge implements ApplicationListener { soundSystem.dispose(); if (onExit != null) { - onExit.run(); + onExit.run(runAfterExit); } } diff --git a/forge-gui-mobile/src/forge/assets/AssetsDownloader.java b/forge-gui-mobile/src/forge/assets/AssetsDownloader.java index 75410646165..8c7c68cf18e 100644 --- a/forge-gui-mobile/src/forge/assets/AssetsDownloader.java +++ b/forge-gui-mobile/src/forge/assets/AssetsDownloader.java @@ -28,11 +28,11 @@ import forge.util.FileUtil; import forge.util.gui.SOptionPane; public class AssetsDownloader { - public static final boolean SHARE_DESKTOP_ASSETS = true; //change to false to test downloading separate assets for desktop version + public static final boolean SHARE_DESKTOP_ASSETS = false; //change to false to test downloading separate assets for desktop version //if not sharing desktop assets, check whether assets are up to date - public static boolean checkForUpdates(final SplashScreen splashScreen) { - if (Gdx.app.getType() == ApplicationType.Desktop && SHARE_DESKTOP_ASSETS) { return true; } + public static void checkForUpdates(final SplashScreen splashScreen) { + if (Gdx.app.getType() == ApplicationType.Desktop && SHARE_DESKTOP_ASSETS) { return; } splashScreen.getProgressBar().setDescription("Checking for updates..."); @@ -49,11 +49,18 @@ public class AssetsDownloader { "You are currently on an older version (" + Forge.CURRENT_VERSION + ").\n\n" + "Would you like to update to the new version now?"; if (!Forge.getNetworkConnection().isConnectedToWifi()) { - message += " If so, you may want to connect to wifi first. The installer download is 6.5MB."; + message += " If so, you may want to connect to wifi first. The download is around 6.5MB."; } if (SOptionPane.showConfirmDialog(message, "New Version Available", "Update Now", "Update Later")) { - - return false; + String apkFile = downloadFile("update", "forge-android-" + version + "-signed-aligned.apk", + "http://cardforge.org/android/releases/forge/forge-gui-android/" + version + "/", + ForgeConstants.ASSETS_DIR, splashScreen.getProgressBar()); + if (apkFile != null) { + Forge.exit(true, apkFile); + return; + } + SOptionPane.showMessageDialog("Could not download update. " + + "Press OK to proceed without update.", "Update Failed"); } } } @@ -70,11 +77,12 @@ public class AssetsDownloader { } catch (IOException e) { e.printStackTrace(); - return false; //can't continue if this fails + Forge.exit(true, null); //can't continue if this fails + return; } } else if (Forge.CURRENT_VERSION.equals(FileUtil.readFileToString(versionFile)) && FSkin.getSkinDir() != null) { - return true; //if version matches what had been previously saved and FSkin isn't requesting assets download, no need to download assets + return; //if version matches what had been previously saved and FSkin isn't requesting assets download, no need to download assets } splashScreen.prepareForDialogs(); //ensure colors set up for showing message dialogs @@ -90,7 +98,10 @@ public class AssetsDownloader { message += "You cannot start the app since you haven't previously downloaded these files."; } SOptionPane.showMessageDialog(message, "No Internet Connection"); - return canIgnoreDownload; //exit if can't ignore download + if (!canIgnoreDownload) { + Forge.exit(true, null); //exit if can't ignore download + } + return; } //prompt user whether they wish to download the updated resource files @@ -115,9 +126,13 @@ public class AssetsDownloader { switch (SOptionPane.showOptionDialog(message, "Download Resource Files?", null, options)) { case 1: - return canIgnoreDownload; //return true or false based on whether second option is Ignore vs. Exit + if (!canIgnoreDownload) { + Forge.exit(true, null); //exit if can't ignore download + } + return; case 2: - return false; //return false to indicate to exit application + Forge.exit(true, null); + return; } downloadAssets(splashScreen.getProgressBar()); @@ -136,7 +151,6 @@ public class AssetsDownloader { //save version string to file once assets finish downloading //so they don't need to be re-downloaded until you upgrade again FileUtil.writeFile(versionFile, Forge.CURRENT_VERSION); - return true; } private static String downloadFile(final String desc, final String filename, final String sourceFolder, final String destFolder, final FProgressBar progressBar) { diff --git a/forge-gui-mobile/src/forge/error/BugReportDialog.java b/forge-gui-mobile/src/forge/error/BugReportDialog.java index a4b0840fe4a..cb82450174a 100644 --- a/forge-gui-mobile/src/forge/error/BugReportDialog.java +++ b/forge-gui-mobile/src/forge/error/BugReportDialog.java @@ -1,6 +1,5 @@ package forge.error; -import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment; import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds; @@ -63,7 +62,7 @@ public class BugReportDialog extends FScreen { //use screen rather than dialog s btnExit.setCommand(new FEventHandler() { @Override public void handleEvent(FEvent e) { - Gdx.app.exit(); + Forge.exit(true, null); } }); }