Skip to content

Commit

Permalink
Fix SDL File Handling
Browse files Browse the repository at this point in the history
While updating the SDL2 recipe to version 2.0.4 in PR kivy#713, the
SDLActivity was not updated accordingly.

SDL 2.0.4 expects the Activity to have a method called
"openAPKExpansionInputStream" while this method was called
"openAPKExtensionInputStream" in older versions.

SDL2 is not able to read files when the method is missing.
  • Loading branch information
Kim Rinnewitz committed Aug 30, 2018
1 parent 0ac1013 commit c2e9ac8
Showing 1 changed file with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ public static void pollInputDevices() {
public void keepActive() {
}

// APK extension files support
// APK expansion files support

/** com.android.vending.expansion.zipfile.ZipResourceFile object or null. */
private Object expansionFile;
Expand All @@ -680,16 +680,43 @@ public void keepActive() {
private Method expansionFileMethod;

/**
* This method is called by SDL using JNI.
* This method was called by SDL using JNI.
* @deprecated because of an incorrect name
*/
@Deprecated
public InputStream openAPKExtensionInputStream(String fileName) throws IOException {
return openAPKExpansionInputStream(fileName);
}

/**
* This method is called by SDL using JNI.
* @return an InputStream on success or null if no expansion file was used.
* @throws IOException on errors. Message is set for the SDL error message.
*/
public InputStream openAPKExpansionInputStream(String fileName) throws IOException {
// Get a ZipResourceFile representing a merger of both the main and patch files
if (expansionFile == null) {
Integer mainVersion = Integer.valueOf(nativeGetHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION"));
Integer patchVersion = Integer.valueOf(nativeGetHint("SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION"));
String mainHint = nativeGetHint("SDL_ANDROID_APK_EXPANSION_MAIN_FILE_VERSION");
if (mainHint == null) {
return null; // no expansion use if no main version was set
}
String patchHint = nativeGetHint("SDL_ANDROID_APK_EXPANSION_PATCH_FILE_VERSION");
if (patchHint == null) {
return null; // no expansion use if no patch version was set
}

Integer mainVersion;
Integer patchVersion;
try {
mainVersion = Integer.valueOf(mainHint);
patchVersion = Integer.valueOf(patchHint);
} catch (NumberFormatException ex) {
ex.printStackTrace();
throw new IOException("No valid file versions set for APK expansion files", ex);
}

try {
// To avoid direct dependency on Google APK extension library that is
// To avoid direct dependency on Google APK expansion library that is
// not a part of Android SDK we access it using reflection
expansionFile = Class.forName("com.android.vending.expansion.zipfile.APKExpansionSupport")
.getMethod("getAPKExpansionZipFile", Context.class, int.class, int.class)
Expand All @@ -701,6 +728,7 @@ public InputStream openAPKExtensionInputStream(String fileName) throws IOExcepti
ex.printStackTrace();
expansionFile = null;
expansionFileMethod = null;
throw new IOException("Could not access APK expansion support library", ex);
}
}

Expand All @@ -709,12 +737,14 @@ public InputStream openAPKExtensionInputStream(String fileName) throws IOExcepti
try {
fileStream = (InputStream)expansionFileMethod.invoke(expansionFile, fileName);
} catch (Exception ex) {
// calling "getInputStream" failed
ex.printStackTrace();
fileStream = null;
throw new IOException("Could not open stream from APK expansion file", ex);
}

if (fileStream == null) {
throw new IOException();
// calling "getInputStream" was successful but null was returned
throw new IOException("Could not find path in APK expansion file");
}

return fileStream;
Expand Down

0 comments on commit c2e9ac8

Please sign in to comment.