Skip to content

Instantly share code, notes, and snippets.

@kiwipxl
Created December 11, 2017 04:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiwipxl/bd8c263e461be0a8de0b79794b2959dd to your computer and use it in GitHub Desktop.
Save kiwipxl/bd8c263e461be0a8de0b79794b2959dd to your computer and use it in GitHub Desktop.
package com.unity3d.plugin.obbExtensions;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import java.io.*;
public class Extensions
{
private Context context;
public Extensions(Context context)
{
this.context = context;
}
public boolean CanReadObb(boolean primaryOnly)
{
// Primary dir check
if (CanReadObbFileFromDir(context.getObbDir())) {
return true;
}
if (primaryOnly) {
return false;
}
// All other shared/external checks
for (File file : context.getObbDirs()) {
if (CanReadObbFileFromDir(file)) {
return true;
}
}
return false;
}
public boolean CanReadObbFileFromDir(File dir)
{
if (dir == null) {
return false;
}
return CanReadObbFile(GetObbPathFromDir(dir));
}
public boolean CanReadObbFile(String path)
{
if (path == null) {
return false;
}
try {
new BufferedReader(new FileReader(path));
return true;
} catch (IOException e) {
return false;
}
}
public String GetObbPathFromDir(File dir)
{
if (dir == null) {
return null;
}
return dir.getAbsolutePath() + File.separator + GetObbFileName();
}
public String GetObbFileName()
{
int versionCode = 0;
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionCode = packageInfo.versionCode;
}catch (PackageManager.NameNotFoundException ex) { }
return "main." + String.valueOf(versionCode) + "." + context.getPackageName() + ".obb";
}
public String GetPrimaryObbPath()
{
return GetObbPathFromDir(context.getObbDir());
}
public String[] GetAllObbPaths()
{
File[] files = context.getObbDirs();
String[] paths = new String[files.length];
for (int n = 0; n < files.length; ++n) {
paths[n] = GetObbPathFromDir(files[n]);
}
return paths;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment