Skip to content

Commit

Permalink
Merge pull request #10031 from garymathews/TIMOB-25231
Browse files Browse the repository at this point in the history
[TIMOB-25231] Android: List encrypted assets
  • Loading branch information
lokeshchdhry committed May 16, 2018
2 parents dc1e0c8 + 0fab7ef commit fbb7eae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class KrollAssetHelper

public interface AssetCrypt {
String readAsset(String path);
String[] getAssetPaths();
}

public static void setAssetCrypt(AssetCrypt assetCrypt)
Expand Down Expand Up @@ -77,6 +78,14 @@ public static String readAsset(String path)
return null;
}

public static String[] getEncryptedAssetPaths()
{
if (assetCrypt != null) {
return assetCrypt.getAssetPaths();
}
return null;
}

public static String readFile(String path)
{
try {
Expand Down
4 changes: 4 additions & 0 deletions android/templates/build/AssetCryptImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public String readAsset(String path)
return new String(filterDataInRange(assetsBytes, range.offset, range.length));
}

public String[] getAssetPaths() {
return assets.keySet().toArray(new String[assets.size()]);
}

private static byte[] filterDataInRange(byte[] data, int offset, int length)
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.util.KrollAssetHelper;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
Expand All @@ -29,7 +30,7 @@ public class TiResourceFile extends TiBaseFile
{
private static final String TAG = "TiResourceFile";

private final String path;
private String path;
private boolean statsFetched = false;
private boolean exists = false;

Expand Down Expand Up @@ -190,6 +191,9 @@ public long spaceAvailable()

public String toURL()
{
if (!path.isEmpty() && !path.endsWith("/") && isDirectory()) {
path += "/";
}
return TiC.URL_ANDROID_ASSET_RESOURCES + path;
}

Expand Down Expand Up @@ -222,17 +226,39 @@ public List<String> getDirectoryListing()
{
List<String> listing = new ArrayList<String>();
try {
String lpath = TiFileHelper2.joinSegments("Resources", path);
String lpath = TiFileHelper2.getResourcesPath(path);
if (lpath.endsWith("/")) {
lpath = lpath.substring(0, lpath.lastIndexOf("/"));
}

// list application assets
String[] names = TiApplication.getInstance().getAssets().list(lpath);
if (names != null) {
int len = names.length;
for (int i = 0; i < len; i++) {
listing.add(names[i]);
}
}

// list encrypted assets
String[] assets = KrollAssetHelper.getEncryptedAssetPaths();
if (assets != null) {
for (String asset : assets) {
if (asset.startsWith(path)) {
String relativePath = asset.substring(path.length());
int dirIndex = relativePath.lastIndexOf('/');
if (dirIndex != -1) {
String dir = relativePath.substring(0, dirIndex);
if (dir.length() > 0 && !listing.contains(dir)) {
listing.add(dir);
}
} else if (relativePath.length() > 0) {
listing.add(relativePath);
}
}
}
}

} catch (IOException e) {
Log.e(TAG, "Error while getting a directory listing: " + e.getMessage(), e);
}
Expand All @@ -246,33 +272,23 @@ public String toString()

private void fetchStats()
{
InputStream is = null;
try {
is = getInputStream();
if (KrollAssetHelper.assetExists(TiFileHelper2.getResourcesPath(path))) {
this.typeDir = false;
this.typeFile = true;
this.exists = true;
} catch (IOException e) {
this.typeFile = false; // definitely not a file
// getInputStream() will throw a FileNotFoundException if it is a
// directory. We check if there are directory listings. If there is,
// we can assume it is a directory and it exists.

} else {
this.typeFile = false;

if (!getDirectoryListing().isEmpty()) {
this.typeDir = true;
this.exists = true;

// does not exist; neither file or directory
} else {
// doesn't exist so neither file nor directory
this.typeDir = false;
this.exists = false;
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Ignore
}
}
}
statsFetched = true;
}
Expand Down

0 comments on commit fbb7eae

Please sign in to comment.