Browse Source

Now, we have a filetype association!!!! :)

Jairo Luiz 12 years ago
parent
commit
8069dc02c6

+ 13 - 0
AndroidManifest.xml

@@ -24,6 +24,19 @@
                 <category android:name="tv.ouya.intent.category.GAME"/>
             </intent-filter>
         </activity>
+        <activity
+            android:name="GameLauncher"
+            android:configChanges="orientation|screenSize"
+            android:screenOrientation="landscape" >
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <data android:scheme="file" />
+                <data android:mimeType="*/*" />
+                <data android:pathPattern=".*\\.love" />
+                <data android:host="*" />
+            </intent-filter>
+        </activity>
     </application>
 
     <!-- Android 2.3.3 -->

+ 24 - 0
jni/love/src/common/android.cpp

@@ -52,6 +52,30 @@ double getScreenScale()
   return result;
 }
 
+const char* getSelectedGameFile()
+{
+  static const char *path;
+
+  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;
+}
+
 } // android
 } // love
 

+ 5 - 0
jni/love/src/common/android.h

@@ -36,6 +36,11 @@ namespace android
  **/
 double getScreenScale();
 
+/**
+ * Gets the selected love file in the device filesystem.
+ **/
+const char* getSelectedGameFile();
+
 } // android
 } // love
 

+ 16 - 7
jni/love/src/modules/filesystem/physfs/Filesystem.cpp

@@ -35,6 +35,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <errno.h>
+#include "common/android.h"
 #endif
 
 namespace
@@ -65,18 +66,18 @@ namespace
 		delete[] game_love_data;
 	}
 
-	bool androidMountAssetGame () {
-		SDL_Log ("Trying to mount assets/game.love");
-    SDL_RWops *asset_game_file = SDL_RWFromFile("game.love", "rb");
+	bool androidMountGame (const char *filename) {
+		SDL_Log ("Trying to mount %s", filename);
+    SDL_RWops *asset_game_file = SDL_RWFromFile(filename, "rb");
 		if (!asset_game_file) {
-			SDL_Log ("Could not find assets/game.love");
+			SDL_Log ("Could not find %s", filename);
 			return false;
 		}
 
 		Sint64 file_size = asset_game_file->size(asset_game_file);
 
 		if (file_size == 0) {
-			SDL_Log ("Could not find valid game.love in assets. File has zero size.", (int) file_size);
+			SDL_Log ("Could not find valid %s. File has zero size.", filename);
 			return false;
 		}
 
@@ -106,6 +107,14 @@ namespace
 		return result;
 	}
 
+	bool androidMountAssetGame () {
+		return androidMountGame ("game.love");
+	}
+
+	bool androidMountSelectedGame () {
+		return androidMountGame (love::android::getSelectedGameFile());
+	}
+
 	bool androidDirectoryExists(const char* path) {
 		SDL_Log ("Checking directory exists for %s", path);
 		struct stat s;
@@ -289,7 +298,7 @@ bool Filesystem::setSource(const char *source)
 		SDL_Log ("Error creating storage directories!");
 	}
 
-	if (!androidMountAssetGame()) {
+	if (!androidMountSelectedGame() && !androidMountAssetGame()) {
 		SDL_RWops *sdcard_main = SDL_RWFromFile("/sdcard/lovegame/main.lua", "rb");
 
 		if (sdcard_main) {
@@ -306,7 +315,7 @@ bool Filesystem::setSource(const char *source)
 			// sucessfully, therefore simply fail.
 			return false;
 		}
-	} 
+	}
 #else
 	// Add the directory.
 	if (!PHYSFS_addToSearchPath(new_search_path.c_str(), 1)) {

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

@@ -0,0 +1,24 @@
+package org.love2d.android;
+
+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) {
+            gamePath = game.getPath();
+            Log.d("GameLauncher", "Selected the file: " + gamePath);
+        }
+        super.onCreate(bundle);
+    }
+
+    public static String getGamePath() {
+        return gamePath;
+    }
+}