- // APK expansion files support
-
- /** com.android.vending.expansion.zipfile.ZipResourceFile object or null. */
- private static Object expansionFile;
-
- /** com.android.vending.expansion.zipfile.ZipResourceFile's getInputStream() or null. */
- private static Method expansionFileMethod;
-
- /**
- * 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 static InputStream openAPKExpansionInputStream(String fileName) throws IOException {
- // Get a ZipResourceFile representing a merger of both the main and patch files
- if (expansionFile == null) {
- 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 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)
- .invoke(null, SDL.getContext(), mainVersion, patchVersion);
-
- expansionFileMethod = expansionFile.getClass()
- .getMethod("getInputStream", String.class);
- } catch (Exception ex) {
- ex.printStackTrace();
- expansionFile = null;
- expansionFileMethod = null;
- throw new IOException("Could not access APK expansion support library", ex);
- }
- }
-
- // Get an input stream for a known file inside the expansion file ZIPs
- InputStream fileStream;
- try {
- fileStream = (InputStream)expansionFileMethod.invoke(expansionFile, fileName);
- } catch (Exception ex) {
- // calling "getInputStream" failed
- ex.printStackTrace();
- throw new IOException("Could not open stream from APK expansion file", ex);
- }
-
- if (fileStream == null) {
- // calling "getInputStream" was successful but null was returned
- throw new IOException("Could not find path in APK expansion file");
- }
-
- return fileStream;
- }
-