Selaa lähdekoodia

Merged GameLauncher and GameActivity together

The GameActivity would not run otherwise as the App started via
GameActivity would call in love::android::getSelectedGameFile() the wrong
activity (namely GameLauncher) that was not initialized properly. Hence it
would result in a segfault.
fysx 12 vuotta sitten
vanhempi
sitoutus
60a58f9240

+ 1 - 1
AndroidManifest.xml

@@ -25,7 +25,7 @@
             </intent-filter>
         </activity>
         <activity
-            android:name="GameLauncher"
+            android:name="GameActivity"
             android:configChanges="orientation|screenSize"
             android:screenOrientation="landscape" >
             <intent-filter>

+ 20 - 20
jni/love/src/common/android.cpp

@@ -54,26 +54,26 @@ double getScreenScale()
 
 const char* getSelectedGameFile()
 {
-  static const char *path = NULL;
-
-  if (!path) {
-    JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
-    jclass activity = env->FindClass("org/love2d/android/GameLauncher");
-
-    jmethodID getGamePath = env->GetStaticMethodID(activity, "getGamePath", "()Ljava/lang/String;");
-    jstring gamePath = (jstring) env->CallStaticObjectMethod(activity, getGamePath);
-    const char *utf = env->GetStringUTFChars(gamePath, 0);
-    if (utf)
-    {
-      path = SDL_strdup(utf);
-      env->ReleaseStringUTFChars(gamePath, utf);
-    }
-
-    env->DeleteLocalRef (gamePath);
-    env->DeleteLocalRef (activity);
-  }
-
-  return path;
+	static const char *path = NULL;
+
+	if (!path) {
+		JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv();
+		jclass activity = env->FindClass("org/love2d/android/GameActivity");
+
+		jmethodID getGamePath = env->GetStaticMethodID(activity, "getGamePath", "()Ljava/lang/String;");
+		jstring gamePath = (jstring) env->CallStaticObjectMethod(activity, getGamePath);
+		const char *utf = env->GetStringUTFChars(gamePath, 0);
+		if (utf)
+		{
+			path = SDL_strdup(utf);
+			env->ReleaseStringUTFChars(gamePath, utf);
+		}
+
+		env->DeleteLocalRef (gamePath);
+		env->DeleteLocalRef (activity);
+	}
+
+	return path;
 }
 
 } // android

+ 82 - 5
src/org/love2d/android/GameActivity.java

@@ -1,20 +1,97 @@
 package org.love2d.android;
 
 import org.libsdl.app.SDLActivity;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import android.net.Uri;
 import android.os.Bundle;
+import android.util.Log;
 import android.util.DisplayMetrics;
 
 public class GameActivity extends SDLActivity {
     private static DisplayMetrics metrics = new DisplayMetrics();
-	
-	@Override
+
+    private static String gamePath = "";
+
+    @Override
     protected void onCreate(Bundle savedInstanceState) {
+        Uri game = this.getIntent().getData();
+        if (game != null) {
+          if (game.getScheme().equals ("file")) {
+            gamePath = game.getPath();
+          } else {
+            copyGameToCache (game);
+          }
+          Log.d("GameActivity", "Selected the file: " + getGamePath());
+        }
+
         super.onCreate(savedInstanceState);
-        
         getWindowManager().getDefaultDisplay().getMetrics(metrics);
     }
-    
-    public static DisplayMetrics getMetrics() {
+
+    public static String getGamePath() {
+			Log.d ("GameActivity", "called getGamePath(), game path = " + gamePath);
+        return gamePath;
+    }
+
+		public static DisplayMetrics getMetrics() {
         return metrics;
     }
+  
+    void copyGameToCache (Uri sourceuri)
+    {
+      String destinationFilename = this.getCacheDir().getPath()+"/downloaded.love";
+      gamePath = destinationFilename;
+
+      BufferedOutputStream bos = null;
+      try {
+        bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
+      } catch (IOException e) {
+        Log.d ("GameActivity", "Could not open destination file:" + e.getMessage());
+      }
+
+      int chunk_read = 0;
+      int bytes_written = 0;
+
+      BufferedInputStream bis = null;
+      if (sourceuri.getScheme().equals("content")) {
+        try {
+          bis = new BufferedInputStream(getContentResolver().openInputStream(sourceuri));
+        } catch (IOException e) {
+          Log.d ("GameActivity", "Could not open game file:" + e.getMessage());
+        }
+      } else {
+        Log.d ("GameActivity", "Unsupported scheme: " + sourceuri.getScheme());
+      }
+
+      if (bis != null) {
+        // actual copying
+        try {
+          byte[] buf = new byte[1024];
+          chunk_read = bis.read(buf);
+          do {
+            bos.write(buf, 0, chunk_read);
+            bytes_written += chunk_read;
+            chunk_read = bis.read(buf);        
+          } while(chunk_read != -1);
+        } catch (IOException e) {
+          Log.d ("GameActivity", "Copying failed:" + e.getMessage());
+        } 
+      }
+
+      // close streams
+      try {
+        if (bis != null) bis.close();
+        if (bos != null) bos.close();
+      } catch (IOException e) {
+        Log.d ("GameActivity", "Copying failed: " + e.getMessage());
+      }
+
+      Log.d("GameActivity", "Copied " + bytes_written + " bytes");
+    }
+
 }

+ 0 - 86
src/org/love2d/android/GameLauncher.java

@@ -1,86 +0,0 @@
-package org.love2d.android;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-import android.net.Uri;
-import android.os.Bundle;
-import android.util.Log;
-
-public class GameLauncher extends GameActivity {
-
-    private static String gamePath;
-
-    @Override
-    protected void onCreate(Bundle bundle) {
-        Uri game = this.getIntent().getData();
-        if (game != null) {
-          if (game.getScheme().equals ("file")) {
-            gamePath = game.getPath();
-          } else {
-            copyGameToCache (game);
-          }
-          Log.d("GameLauncher", "Selected the file: " + getGamePath());
-        }
-        super.onCreate(bundle);
-    }
-
-    public static String getGamePath() {
-        return gamePath;
-    }
-  
-    void copyGameToCache (Uri sourceuri)
-    {
-      String destinationFilename = this.getCacheDir().getPath()+"/downloaded.love";
-      gamePath = destinationFilename;
-
-      BufferedOutputStream bos = null;
-      try {
-        bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
-      } catch (IOException e) {
-        Log.d ("GameLauncher", "Could not open destination file:" + e.getMessage());
-      }
-
-      int chunk_read = 0;
-      int bytes_written = 0;
-
-      BufferedInputStream bis = null;
-      if (sourceuri.getScheme().equals("content")) {
-        try {
-          bis = new BufferedInputStream(getContentResolver().openInputStream(sourceuri));
-        } catch (IOException e) {
-          Log.d ("GameLauncher", "Could not open game file:" + e.getMessage());
-        }
-      } else {
-        Log.d ("GameLauncher", "Unsupported scheme: " + sourceuri.getScheme());
-      }
-
-      if (bis != null) {
-        // actual copying
-        try {
-          byte[] buf = new byte[1024];
-          chunk_read = bis.read(buf);
-          do {
-            bos.write(buf, 0, chunk_read);
-            bytes_written += chunk_read;
-            chunk_read = bis.read(buf);        
-          } while(chunk_read != -1);
-        } catch (IOException e) {
-          Log.d ("GameLauncher", "Copying failed:" + e.getMessage());
-        } 
-      }
-
-      // close streams
-      try {
-        if (bis != null) bis.close();
-        if (bos != null) bos.close();
-      } catch (IOException e) {
-        Log.d ("GameLauncher", "Copying failed: " + e.getMessage());
-      }
-
-      Log.d("GameLauncher", "Copied " + bytes_written + " bytes");
-    }
-
-}