-
Notifications
You must be signed in to change notification settings - Fork 55
/
XAPKExpansionSupport.java
87 lines (74 loc) · 3.03 KB
/
XAPKExpansionSupport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.flyingsoftgames.xapkreader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Vector;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.os.Environment;
public class XAPKExpansionSupport {
// The shared path to all app expansion files
private final static String EXP_PATH = "/Android/obb/";
static String[] getAPKExpansionFiles (Context ctx, int mainVersion, int patchVersion) {
String packageName = ctx.getPackageName ();
Vector<String> ret = new Vector<String> ();
// Exit out if there's no way to get to the shared storage directory.
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) return ret.toArray (new String[0]);
// Build the full path to the app's expansion files.
File expPath = ctx.getObbDir();
String expPathString = expPath.getAbsolutePath();
// Check that the expansion file path exists.
if (!expPath.exists()) return ret.toArray (new String[0]);
// Get a list of files in this directory.
ArrayList<String> fileList = listFiles (expPathString);
// Check if either the main or path files exist.
int[] versionNumber = {mainVersion, patchVersion};
String[] versionCheck = {"main" , "patch"};
for (int i = 0; i < versionNumber.length; i++) {
String strPath = "";
int currentVersionNumber = versionNumber[i];
if (currentVersionNumber > 0) {
strPath = expPath + File.separator + versionCheck[i] + "." + currentVersionNumber + "." + packageName + ".obb";
} else if (currentVersionNumber == 0) {
// Find if there is a "patch" or "main" file in the files list.
for (String file : fileList) {
if (file.indexOf(expPath + File.separator + versionCheck[i]) == 0) {strPath = file; break;}
}
}
if (new File(strPath).isFile()) ret.add(strPath);
}
String[] retArray = new String[ret.size()];
ret.toArray(retArray);
return retArray;
}
// Get all the files in a directory.
static public ArrayList<String> listFiles (String directoryName) {
File directory = new File (directoryName);
ArrayList<String> files = new ArrayList<String>();
File[] fileList = directory.listFiles();
if(fileList == null) {
return files;
}
for (File file : fileList) {
if (file.isFile()) {files.add (file.toString()); continue;}
}
return files;
}
static public XAPKZipResourceFile getResourceZipFile (String[] expansionFiles) throws IOException {
XAPKZipResourceFile apkExpansionFile = null;
for (String expansionFilePath : expansionFiles) {
if (apkExpansionFile == null) {
apkExpansionFile = new XAPKZipResourceFile (expansionFilePath);
} else {
apkExpansionFile.addPatchFile (expansionFilePath);
}
}
return apkExpansionFile;
}
static public XAPKZipResourceFile getAPKExpansionZipFile (Context ctx, int mainVersion, int patchVersion) throws IOException {
String[] expansionFiles = getAPKExpansionFiles (ctx, mainVersion, patchVersion);
return getResourceZipFile (expansionFiles);
}
}