1/*
2 * Copyright (c) 2009-2010 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32package com.jme3.asset;
33
34import com.jme3.asset.plugins.AndroidLocator;
35import com.jme3.asset.plugins.ClasspathLocator;
36import com.jme3.audio.plugins.AndroidAudioLoader;
37import com.jme3.texture.Texture;
38import com.jme3.texture.plugins.AndroidImageLoader;
39import java.net.URL;
40import java.util.logging.Level;
41import java.util.logging.Logger;
42
43/**
44 * <code>AndroidAssetManager</code> is an implementation of DesktopAssetManager for Android
45 *
46 * @author larynx
47 */
48public class AndroidAssetManager extends DesktopAssetManager {
49
50    private static final Logger logger = Logger.getLogger(AndroidAssetManager.class.getName());
51
52    public AndroidAssetManager() {
53        this(null);
54    }
55
56    @Deprecated
57    public AndroidAssetManager(boolean loadDefaults) {
58        //this(Thread.currentThread().getContextClassLoader().getResource("com/jme3/asset/Android.cfg"));
59        this(null);
60    }
61
62    private void registerLoaderSafe(Class<? extends AssetLoader> loaderClass, String ... extensions) {
63        try {
64            registerLoader(loaderClass, extensions);
65        } catch (Exception e){
66            logger.log(Level.WARNING, "Failed to load AssetLoader", e);
67        }
68    }
69
70    /**
71     * AndroidAssetManager constructor
72     * If URL == null then a default list of locators and loaders for android is set
73     * @param configFile
74     */
75    public AndroidAssetManager(URL configFile) {
76        System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver");
77
78        // Set Default Android config
79        registerLocator("", AndroidLocator.class);
80        registerLocator("", ClasspathLocator.class);
81
82        registerLoader(AndroidImageLoader.class, "jpg", "bmp", "gif", "png", "jpeg");
83        registerLoader(AndroidAudioLoader.class, "ogg", "mp3", "wav");
84        registerLoader(com.jme3.material.plugins.J3MLoader.class, "j3m");
85        registerLoader(com.jme3.material.plugins.J3MLoader.class, "j3md");
86        registerLoader(com.jme3.shader.plugins.GLSLLoader.class, "vert", "frag", "glsl", "glsllib");
87        registerLoader(com.jme3.export.binary.BinaryImporter.class, "j3o");
88        registerLoader(com.jme3.font.plugins.BitmapFontLoader.class, "fnt");
89
90        // Less common loaders (especially on Android)
91        registerLoaderSafe(com.jme3.texture.plugins.DDSLoader.class, "dds");
92        registerLoaderSafe(com.jme3.texture.plugins.PFMLoader.class, "pfm");
93        registerLoaderSafe(com.jme3.texture.plugins.HDRLoader.class, "hdr");
94        registerLoaderSafe(com.jme3.texture.plugins.TGALoader.class, "tga");
95        registerLoaderSafe(com.jme3.scene.plugins.OBJLoader.class, "obj");
96        registerLoaderSafe(com.jme3.scene.plugins.MTLLoader.class, "mtl");
97        registerLoaderSafe(com.jme3.scene.plugins.ogre.MeshLoader.class, "mesh.xml");
98        registerLoaderSafe(com.jme3.scene.plugins.ogre.SkeletonLoader.class, "skeleton.xml");
99        registerLoaderSafe(com.jme3.scene.plugins.ogre.MaterialLoader.class, "material");
100        registerLoaderSafe(com.jme3.scene.plugins.ogre.SceneLoader.class, "scene");
101
102
103        logger.info("AndroidAssetManager created.");
104    }
105
106    /**
107     * Loads a texture.
108     *
109     * @return
110     */
111    @Override
112    public Texture loadTexture(TextureKey key) {
113        Texture tex = (Texture) loadAsset(key);
114
115        // XXX: This will improve performance on some really
116        // low end GPUs (e.g. ones with OpenGL ES 1 support only)
117        // but otherwise won't help on the higher ones.
118        // Strongly consider removing this.
119        tex.setMagFilter(Texture.MagFilter.Nearest);
120        tex.setAnisotropicFilter(0);
121        if (tex.getMinFilter().usesMipMapLevels()) {
122            tex.setMinFilter(Texture.MinFilter.NearestNearestMipMap);
123        } else {
124            tex.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
125        }
126        return tex;
127    }
128}
129