• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.jme3.asset.plugins;
2 
3 import com.jme3.asset.*;
4 import com.jme3.system.android.JmeAndroidSystem;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.logging.Logger;
8 
9 public class AndroidLocator implements AssetLocator {
10 
11     private static final Logger logger = Logger.getLogger(AndroidLocator.class.getName());
12 
13     private android.content.res.AssetManager androidManager;
14     private String rootPath = "";
15 
16     private class AndroidAssetInfo extends AssetInfo {
17 
18         private InputStream in;
19         private final String assetPath;
20 
AndroidAssetInfo(com.jme3.asset.AssetManager assetManager, AssetKey<?> key, String assetPath, InputStream in)21         public AndroidAssetInfo(com.jme3.asset.AssetManager assetManager, AssetKey<?> key, String assetPath, InputStream in) {
22             super(assetManager, key);
23             this.assetPath = assetPath;
24             this.in = in;
25         }
26 
27         @Override
openStream()28         public InputStream openStream() {
29             if (in != null){
30                 // Reuse the already existing stream (only once)
31                 InputStream in2 = in;
32                 in = null;
33                 return in2;
34             }else{
35                 // Create a new stream for subsequent invocations.
36                 try {
37                     return androidManager.open(assetPath);
38                 } catch (IOException ex) {
39                     throw new AssetLoadException("Failed to open asset " + assetPath, ex);
40                 }
41             }
42         }
43     }
44 
create(AssetManager assetManager, AssetKey key, String assetPath)45     private AndroidAssetInfo create(AssetManager assetManager, AssetKey key, String assetPath) throws IOException {
46         try {
47             InputStream in = androidManager.open(assetPath);
48             if (in == null){
49                 return null;
50             }else{
51                 return new AndroidAssetInfo(assetManager, key, assetPath, in);
52             }
53         } catch (IOException ex) {
54             // XXX: Prefer to show warning here?
55             // Should only surpress exceptions for "file missing" type errors.
56             return null;
57         }
58     }
59 
AndroidLocator()60     public AndroidLocator() {
61         androidManager = JmeAndroidSystem.getResources().getAssets();
62     }
63 
setRootPath(String rootPath)64     public void setRootPath(String rootPath) {
65         this.rootPath = rootPath;
66     }
67 
68     @SuppressWarnings("rawtypes")
69     @Override
locate(com.jme3.asset.AssetManager manager, AssetKey key)70     public AssetInfo locate(com.jme3.asset.AssetManager manager, AssetKey key) {
71         String assetPath = rootPath + key.getName();
72         // Fix path issues
73         if (assetPath.startsWith("/")) {
74             // Remove leading /
75             assetPath = assetPath.substring(1);
76         }
77         assetPath = assetPath.replace("//", "/");
78         try {
79             return create(manager, key, assetPath);
80         } catch (IOException ex) {
81             // This is different handling than URL locator
82             // since classpath locating would return null at the getResource()
83             // call, otherwise there's a more critical error...
84             throw new AssetLoadException("Failed to open asset " + assetPath, ex);
85         }
86     }
87 }
88