Search in sources :

Example 1 with BridgeAssetManager

use of android.content.res.BridgeAssetManager in project platform_frameworks_base by android.

the class FontFamily_Delegate method nAddFontFromAsset.

@LayoutlibDelegate
static /*package*/
boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
    FontFamily_Delegate ffd = sManager.getDelegate(nativeFamily);
    if (ffd == null) {
        return false;
    }
    ffd.mValid = true;
    if (mgr == null) {
        return false;
    }
    if (mgr instanceof BridgeAssetManager) {
        InputStream fontStream = null;
        try {
            AssetRepository assetRepository = ((BridgeAssetManager) mgr).getAssetRepository();
            if (assetRepository == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, null);
                return false;
            }
            if (!assetRepository.isSupported()) {
                // Don't log any warnings on unsupported IDEs.
                return false;
            }
            // Check cache
            FontInfo fontInfo = sCache.get(path);
            if (fontInfo != null) {
                // renew the font's lease.
                sCache.put(path, fontInfo);
                ffd.addFont(fontInfo);
                return true;
            }
            fontStream = assetRepository.openAsset(path, AssetManager.ACCESS_STREAMING);
            if (fontStream == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, path);
                return false;
            }
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
            fontInfo = new FontInfo();
            fontInfo.mFont = font;
            fontInfo.mWeight = font.isBold() ? BOLD_FONT_WEIGHT : DEFAULT_FONT_WEIGHT;
            fontInfo.mIsItalic = font.isItalic();
            ffd.addFont(fontInfo);
            return true;
        } catch (IOException e) {
            Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Unable to load font " + path, e, path);
        } catch (FontFormatException e) {
            if (path.endsWith(EXTENSION_OTF)) {
                // otf fonts are not supported on the user's config (JRE version + OS)
                Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "OpenType fonts are not supported yet: " + path, null, path);
            } else {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to load font " + path, e, path);
            }
        } finally {
            if (fontStream != null) {
                try {
                    fontStream.close();
                } catch (IOException ignored) {
                }
            }
        }
        return false;
    }
    // This should never happen. AssetManager is a final class (from user's perspective), and
    // we've replaced every creation of AssetManager with our implementation. We create an
    // exception and log it, but continue with rest of the rendering, without loading this font.
    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "You have found a bug in the rendering library. Please file a bug at b.android.com.", new RuntimeException("Asset Manager is not an instance of BridgeAssetManager"), null);
    return false;
}
Also used : BridgeAssetManager(android.content.res.BridgeAssetManager) InputStream(java.io.InputStream) AssetRepository(com.android.ide.common.rendering.api.AssetRepository) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException) Font(java.awt.Font) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 2 with BridgeAssetManager

use of android.content.res.BridgeAssetManager in project android_frameworks_base by ResurrectionRemix.

the class FontFamily_Delegate method nAddFontFromAsset.

@LayoutlibDelegate
static /*package*/
boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
    FontFamily_Delegate ffd = sManager.getDelegate(nativeFamily);
    if (ffd == null) {
        return false;
    }
    ffd.mValid = true;
    if (mgr == null) {
        return false;
    }
    if (mgr instanceof BridgeAssetManager) {
        InputStream fontStream = null;
        try {
            AssetRepository assetRepository = ((BridgeAssetManager) mgr).getAssetRepository();
            if (assetRepository == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, null);
                return false;
            }
            if (!assetRepository.isSupported()) {
                // Don't log any warnings on unsupported IDEs.
                return false;
            }
            // Check cache
            FontInfo fontInfo = sCache.get(path);
            if (fontInfo != null) {
                // renew the font's lease.
                sCache.put(path, fontInfo);
                ffd.addFont(fontInfo);
                return true;
            }
            fontStream = assetRepository.openAsset(path, AssetManager.ACCESS_STREAMING);
            if (fontStream == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, path);
                return false;
            }
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
            fontInfo = new FontInfo();
            fontInfo.mFont = font;
            fontInfo.mWeight = font.isBold() ? BOLD_FONT_WEIGHT : DEFAULT_FONT_WEIGHT;
            fontInfo.mIsItalic = font.isItalic();
            ffd.addFont(fontInfo);
            return true;
        } catch (IOException e) {
            Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Unable to load font " + path, e, path);
        } catch (FontFormatException e) {
            if (path.endsWith(EXTENSION_OTF)) {
                // otf fonts are not supported on the user's config (JRE version + OS)
                Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "OpenType fonts are not supported yet: " + path, null, path);
            } else {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to load font " + path, e, path);
            }
        } finally {
            if (fontStream != null) {
                try {
                    fontStream.close();
                } catch (IOException ignored) {
                }
            }
        }
        return false;
    }
    // This should never happen. AssetManager is a final class (from user's perspective), and
    // we've replaced every creation of AssetManager with our implementation. We create an
    // exception and log it, but continue with rest of the rendering, without loading this font.
    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "You have found a bug in the rendering library. Please file a bug at b.android.com.", new RuntimeException("Asset Manager is not an instance of BridgeAssetManager"), null);
    return false;
}
Also used : BridgeAssetManager(android.content.res.BridgeAssetManager) InputStream(java.io.InputStream) AssetRepository(com.android.ide.common.rendering.api.AssetRepository) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException) Font(java.awt.Font) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 3 with BridgeAssetManager

use of android.content.res.BridgeAssetManager in project android_frameworks_base by DirtyUnicorns.

the class FontFamily_Delegate method nAddFontFromAsset.

@LayoutlibDelegate
static /*package*/
boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
    FontFamily_Delegate ffd = sManager.getDelegate(nativeFamily);
    if (ffd == null) {
        return false;
    }
    ffd.mValid = true;
    if (mgr == null) {
        return false;
    }
    if (mgr instanceof BridgeAssetManager) {
        InputStream fontStream = null;
        try {
            AssetRepository assetRepository = ((BridgeAssetManager) mgr).getAssetRepository();
            if (assetRepository == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, null);
                return false;
            }
            if (!assetRepository.isSupported()) {
                // Don't log any warnings on unsupported IDEs.
                return false;
            }
            // Check cache
            FontInfo fontInfo = sCache.get(path);
            if (fontInfo != null) {
                // renew the font's lease.
                sCache.put(path, fontInfo);
                ffd.addFont(fontInfo);
                return true;
            }
            fontStream = assetRepository.openAsset(path, AssetManager.ACCESS_STREAMING);
            if (fontStream == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, path);
                return false;
            }
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
            fontInfo = new FontInfo();
            fontInfo.mFont = font;
            fontInfo.mWeight = font.isBold() ? BOLD_FONT_WEIGHT : DEFAULT_FONT_WEIGHT;
            fontInfo.mIsItalic = font.isItalic();
            ffd.addFont(fontInfo);
            return true;
        } catch (IOException e) {
            Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Unable to load font " + path, e, path);
        } catch (FontFormatException e) {
            if (path.endsWith(EXTENSION_OTF)) {
                // otf fonts are not supported on the user's config (JRE version + OS)
                Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "OpenType fonts are not supported yet: " + path, null, path);
            } else {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to load font " + path, e, path);
            }
        } finally {
            if (fontStream != null) {
                try {
                    fontStream.close();
                } catch (IOException ignored) {
                }
            }
        }
        return false;
    }
    // This should never happen. AssetManager is a final class (from user's perspective), and
    // we've replaced every creation of AssetManager with our implementation. We create an
    // exception and log it, but continue with rest of the rendering, without loading this font.
    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "You have found a bug in the rendering library. Please file a bug at b.android.com.", new RuntimeException("Asset Manager is not an instance of BridgeAssetManager"), null);
    return false;
}
Also used : BridgeAssetManager(android.content.res.BridgeAssetManager) InputStream(java.io.InputStream) AssetRepository(com.android.ide.common.rendering.api.AssetRepository) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException) Font(java.awt.Font) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 4 with BridgeAssetManager

use of android.content.res.BridgeAssetManager in project android_frameworks_base by crdroidandroid.

the class FontFamily_Delegate method nAddFontFromAsset.

@LayoutlibDelegate
static /*package*/
boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
    FontFamily_Delegate ffd = sManager.getDelegate(nativeFamily);
    if (ffd == null) {
        return false;
    }
    ffd.mValid = true;
    if (mgr == null) {
        return false;
    }
    if (mgr instanceof BridgeAssetManager) {
        InputStream fontStream = null;
        try {
            AssetRepository assetRepository = ((BridgeAssetManager) mgr).getAssetRepository();
            if (assetRepository == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, null);
                return false;
            }
            if (!assetRepository.isSupported()) {
                // Don't log any warnings on unsupported IDEs.
                return false;
            }
            // Check cache
            FontInfo fontInfo = sCache.get(path);
            if (fontInfo != null) {
                // renew the font's lease.
                sCache.put(path, fontInfo);
                ffd.addFont(fontInfo);
                return true;
            }
            fontStream = assetRepository.openAsset(path, AssetManager.ACCESS_STREAMING);
            if (fontStream == null) {
                Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Asset not found: " + path, path);
                return false;
            }
            Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
            fontInfo = new FontInfo();
            fontInfo.mFont = font;
            fontInfo.mWeight = font.isBold() ? BOLD_FONT_WEIGHT : DEFAULT_FONT_WEIGHT;
            fontInfo.mIsItalic = font.isItalic();
            ffd.addFont(fontInfo);
            return true;
        } catch (IOException e) {
            Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET, "Unable to load font " + path, e, path);
        } catch (FontFormatException e) {
            if (path.endsWith(EXTENSION_OTF)) {
                // otf fonts are not supported on the user's config (JRE version + OS)
                Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "OpenType fonts are not supported yet: " + path, null, path);
            } else {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Unable to load font " + path, e, path);
            }
        } finally {
            if (fontStream != null) {
                try {
                    fontStream.close();
                } catch (IOException ignored) {
                }
            }
        }
        return false;
    }
    // This should never happen. AssetManager is a final class (from user's perspective), and
    // we've replaced every creation of AssetManager with our implementation. We create an
    // exception and log it, but continue with rest of the rendering, without loading this font.
    Bridge.getLog().error(LayoutLog.TAG_BROKEN, "You have found a bug in the rendering library. Please file a bug at b.android.com.", new RuntimeException("Asset Manager is not an instance of BridgeAssetManager"), null);
    return false;
}
Also used : BridgeAssetManager(android.content.res.BridgeAssetManager) InputStream(java.io.InputStream) AssetRepository(com.android.ide.common.rendering.api.AssetRepository) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException) Font(java.awt.Font) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Aggregations

BridgeAssetManager (android.content.res.BridgeAssetManager)4 AssetRepository (com.android.ide.common.rendering.api.AssetRepository)4 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)4 Font (java.awt.Font)4 FontFormatException (java.awt.FontFormatException)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4