From bc493cc00746e0ba6d17b77c122cbf33ed16e8e5 Mon Sep 17 00:00:00 2001
From: Jari Vetoniemi <jari.vetoniemi@indooratlas.com>
Date: Thu, 17 Feb 2022 04:35:24 +0900
Subject: launcher: better activity handling and cleanup

---
 src/pw/cloudef/rpg/Launcher.java | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

(limited to 'src')

diff --git a/src/pw/cloudef/rpg/Launcher.java b/src/pw/cloudef/rpg/Launcher.java
index 626ac14..ab2e9b2 100644
--- a/src/pw/cloudef/rpg/Launcher.java
+++ b/src/pw/cloudef/rpg/Launcher.java
@@ -74,6 +74,14 @@ public class Launcher extends Activity {
       return null;
    }
 
+   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+      super.onActivityResult(requestCode, resultCode, data);
+      try { Os.remove(zipPath()); } catch (Exception e) {}
+      try { Os.remove(baseDir() + "/game.zip"); } catch (Exception e) {}
+      try { Os.remove(baseDir() + "/rtp"); } catch (Exception e) {}
+      finishAndRemoveTask();
+   }
+
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       try {
@@ -82,6 +90,7 @@ public class Launcher extends Activity {
          OutputStream output = new FileOutputStream(file);
          byte[] buf = new byte[8 * 1024];
          for (int read = 0; (read = input.read(buf)) != -1;) output.write(buf, 0, read);
+         output.close(); input.close();
          Class activity = getActivityForZip(file);
          if (activity == null) throw new Exception("Can't handle this game");
          new File(baseDir()).mkdirs();
@@ -92,13 +101,14 @@ public class Launcher extends Activity {
          Os.symlink(rtpDir(), baseDir() + "/rtp");
          Intent intent = new Intent(this, activity);
          intent.putExtra("id", gameId());
-         startActivity(intent);
+         startActivityForResult(intent, 1);
          finish();
       } catch (Exception e) {
          Log.d("pw.cloudef.rpg", e.toString());
          try { Os.remove(zipPath()); } catch (Exception e2) {}
          try { Os.remove(baseDir() + "/game.zip"); } catch (Exception e2) {}
+         try { Os.remove(baseDir() + "/rtp"); } catch (Exception e2) {}
+         finishAndRemoveTask();
       }
-      finishAndRemoveTask();
    }
 }
-- 
cgit v1.2.3-70-g09d2