diff --git a/AndroidManifest.xml b/AndroidManifest.xml index da2114f0..f2e70cb1 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -25,7 +25,7 @@ diff --git a/jni/love/src/common/android.cpp b/jni/love/src/common/android.cpp index 42b54c53..88068c9e 100644 --- a/jni/love/src/common/android.cpp +++ b/jni/love/src/common/android.cpp @@ -54,26 +54,26 @@ double getScreenScale() const char* getSelectedGameFile() { - static const char *path = NULL; + static const char *path = NULL; - if (!path) { - JNIEnv *env = (JNIEnv*) SDL_AndroidGetJNIEnv(); - jclass activity = env->FindClass("org/love2d/android/GameLauncher"); + 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); - } + 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); - } + env->DeleteLocalRef (gamePath); + env->DeleteLocalRef (activity); + } - return path; + return path; } } // android diff --git a/src/org/love2d/android/GameActivity.java b/src/org/love2d/android/GameActivity.java index 2f621331..caa1f61c 100644 --- a/src/org/love2d/android/GameActivity.java +++ b/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"); + } + } diff --git a/src/org/love2d/android/GameLauncher.java b/src/org/love2d/android/GameLauncher.java deleted file mode 100644 index d4ace5e5..00000000 --- a/src/org/love2d/android/GameLauncher.java +++ /dev/null @@ -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"); - } - -}