inital apk extension support apk
authorstopiccot <alexey.petruchik@gmail.com>
Mon, 07 Apr 2014 21:20:39 +0300
branchapk
changeset 8896d931167c375a
parent 8697 abf45e1abfe3
child 8897 fff9c0b9f1fb
inital apk extension support
android-project/src/org/libsdl/app/SDLActivity.java
include/SDL_hints.h
src/core/android/SDL_android.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Sat Apr 05 16:25:30 2014 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Apr 07 21:20:39 2014 +0300
     1.3 @@ -1,5 +1,7 @@
     1.4  package org.libsdl.app;
     1.5  
     1.6 +import java.io.IOException;
     1.7 +import java.io.InputStream;
     1.8  import java.util.ArrayList;
     1.9  import java.util.Arrays;
    1.10  import java.util.Collections;
    1.11 @@ -20,6 +22,8 @@
    1.12  import android.media.*;
    1.13  import android.hardware.*;
    1.14  
    1.15 +import com.android.vending.expansion.zipfile.APKExpansionSupport;
    1.16 +import com.android.vending.expansion.zipfile.ZipResourceFile;
    1.17  
    1.18  /**
    1.19      SDL Activity
    1.20 @@ -296,6 +300,7 @@
    1.21                                                 int is_accelerometer, int nbuttons, 
    1.22                                                 int naxes, int nhats, int nballs);
    1.23      public static native int nativeRemoveJoystick(int device_id);
    1.24 +    public static native String getHint(String name);
    1.25  
    1.26      public static void flipBuffers() {
    1.27          SDLActivity.nativeFlipBuffers();
    1.28 @@ -495,7 +500,28 @@
    1.29              mJoystickHandler.pollInputDevices();
    1.30          }
    1.31      }
    1.32 -    
    1.33 +
    1.34 +    // APK extension files support
    1.35 +    private ZipResourceFile expansionFile = null;
    1.36 +
    1.37 +    public InputStream openAPKExtensionInputStream(String fileName) throws IOException {
    1.38 +        // Get a ZipResourceFile representing a merger of both the main and patch files
    1.39 +        if (expansionFile == null) {
    1.40 +            Integer mainVersion = Integer.parseInt(getHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION"));
    1.41 +            Integer patchVersion = Integer.parseInt(getHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION"));
    1.42 +
    1.43 +            expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this, mainVersion, patchVersion);
    1.44 +        }
    1.45 +
    1.46 +        // Get an input stream for a known file inside the expansion file ZIPs
    1.47 +        InputStream fileStream = expansionFile.getInputStream(fileName);
    1.48 +
    1.49 +        if (fileStream == null) {
    1.50 +            throw new IOException();
    1.51 +        }
    1.52 +
    1.53 +        return fileStream;
    1.54 +    }
    1.55  }
    1.56  
    1.57  /**
     2.1 --- a/include/SDL_hints.h	Sat Apr 05 16:25:30 2014 -0700
     2.2 +++ b/include/SDL_hints.h	Mon Apr 07 21:20:39 2014 +0300
     2.3 @@ -435,6 +435,16 @@
     2.4   */
     2.5  #define SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES    "SDL_VIDEO_MAC_FULLSCREEN_SPACES"
     2.6  
     2.7 +/**
     2.8 + * \brief Android APK expansion main file version. Should be a string number like "1", "2" etc.
     2.9 + */
    2.10 +#define SDL_HINT_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION "SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION"
    2.11 + 
    2.12 +/**
    2.13 + * \brief Android APK expansion patch file version. Should be a string number like "1", "2" etc.
    2.14 + */
    2.15 +#define SDL_HINT_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION "SDL_ANDROID_APK_EXPANSION_MAIN_PATCH_VERSION"
    2.16 +
    2.17  
    2.18  /**
    2.19   *  \brief  An enumeration of hint priorities
     3.1 --- a/src/core/android/SDL_android.c	Sat Apr 05 16:25:30 2014 -0700
     3.2 +++ b/src/core/android/SDL_android.c	Mon Apr 07 21:20:39 2014 +0300
     3.3 @@ -385,7 +385,15 @@
     3.4      (*env)->ReleaseStringUTFChars(env, text, utftext);
     3.5  }
     3.6  
     3.7 +jstring Java_org_libsdl_app_SDLActivity_getHint(JNIEnv* env, jclass cls, jstring name) {
     3.8 +    const char *utfname = (*env)->GetStringUTFChars(env, name, NULL);
     3.9 +    const char *hint = SDL_GetHint(utfname);
    3.10  
    3.11 +    jstring result = (*env)->NewStringUTF(env, hint);
    3.12 +    (*env)->ReleaseStringUTFChars(env, name, utfname);
    3.13 +
    3.14 +    return result;
    3.15 +}
    3.16  
    3.17  /*******************************************************************************
    3.18               Functions called by SDL into Java
    3.19 @@ -758,7 +766,14 @@
    3.20                  "open", "(Ljava/lang/String;I)Ljava/io/InputStream;");
    3.21          inputStream = (*mEnv)->CallObjectMethod(mEnv, assetManager, mid, fileNameJString, 1 /* ACCESS_RANDOM */);
    3.22          if (Android_JNI_ExceptionOccurred(false)) {
    3.23 -            goto failure;
    3.24 +            // Try fallback to APK Extension files
    3.25 +            mid = (*mEnv)->GetMethodID(mEnv, (*mEnv)->GetObjectClass(mEnv, context),
    3.26 +                "openAPKExtensionInputStream", "(Ljava/lang/String;)Ljava/io/InputStream;");
    3.27 +            inputStream = (*mEnv)->CallObjectMethod(mEnv, context, mid, fileNameJString);
    3.28 +
    3.29 +            if (Android_JNI_ExceptionOccurred(false)) {
    3.30 +                goto failure;
    3.31 +            }
    3.32          }
    3.33  
    3.34          ctx->hidden.androidio.inputStreamRef = (*mEnv)->NewGlobalRef(mEnv, inputStream);