Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.badlogic.gdx.backends.android;
     17 
     18 import java.io.File;
     19 import java.io.IOException;
     20 import java.util.Vector;
     21 
     22 import android.content.Context;
     23 import android.os.Environment;
     24 
     25 public class APKExpansionSupport {
     26 	// The shared path to all app expansion files
     27 	private final static String EXP_PATH = "/Android/obb/";
     28 
     29 	static String[] getAPKExpansionFiles(Context ctx, int mainVersion,
     30 			int patchVersion) {
     31 		String packageName = ctx.getPackageName();
     32 		Vector<String> ret = new Vector<String>();
     33 		if (Environment.getExternalStorageState().equals(
     34 				Environment.MEDIA_MOUNTED)) {
     35 			// Build the full path to the app's expansion files
     36 			File root = Environment.getExternalStorageDirectory();
     37 			File expPath = new File(root.toString() + EXP_PATH + packageName);
     38 
     39 			// Check that expansion file path exists
     40 			if (expPath.exists()) {
     41 				if (mainVersion > 0) {
     42 					String strMainPath = expPath + File.separator + "main."
     43 							+ mainVersion + "." + packageName + ".obb";
     44 					File main = new File(strMainPath);
     45 					if (main.isFile()) {
     46 						ret.add(strMainPath);
     47 					}
     48 				}
     49 				if (patchVersion > 0) {
     50 					String strPatchPath = expPath + File.separator + "patch."
     51 							+ mainVersion + "." + packageName + ".obb";
     52 					File main = new File(strPatchPath);
     53 					if (main.isFile()) {
     54 						ret.add(strPatchPath);
     55 					}
     56 				}
     57 			}
     58 		}
     59 		String[] retArray = new String[ret.size()];
     60 		ret.toArray(retArray);
     61 		return retArray;
     62 	}
     63 
     64 	static public ZipResourceFile getResourceZipFile(String[] expansionFiles)
     65 			throws IOException {
     66 		ZipResourceFile apkExpansionFile = null;
     67 		for (String expansionFilePath : expansionFiles) {
     68 			if (null == apkExpansionFile) {
     69 				apkExpansionFile = new ZipResourceFile(expansionFilePath);
     70 			} else {
     71 				apkExpansionFile.addPatchFile(expansionFilePath);
     72 			}
     73 		}
     74 		return apkExpansionFile;
     75 	}
     76 
     77 	static public ZipResourceFile getAPKExpansionZipFile(Context ctx,
     78 			int mainVersion, int patchVersion) throws IOException {
     79 		String[] expansionFiles = getAPKExpansionFiles(ctx, mainVersion,
     80 				patchVersion);
     81 		return getResourceZipFile(expansionFiles);
     82 	}
     83 }
     84