1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.graphics.fonts; 18 19 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 20 21 import android.annotation.NonNull; 22 import android.text.FontConfig; 23 import android.util.Log; 24 25 import java.io.File; 26 import java.io.FileInputStream; 27 import java.io.IOException; 28 import java.nio.ByteBuffer; 29 import java.nio.channels.FileChannel; 30 import java.util.Locale; 31 import java.util.Map; 32 33 /** 34 * Delegate implementing the native methods of android.graphics.fonts.SystemFonts 35 * <p> 36 * Through the layoutlib_create tool, the original native methods of SystemFonts have been 37 * replaced by calls to methods of the same name in this delegate class. 38 * <p> 39 * This class behaves like the original native implementation, but in Java, keeping previously 40 * native data into its own objects and mapping them to int that are sent back and forth between it 41 * and the original SystemFonts class. 42 * 43 */ 44 public class SystemFonts_Delegate { 45 46 private static final String TAG = "SystemFonts_Delegate"; 47 private static String sFontLocation; 48 public static boolean sIsTypefaceInitialized = false; 49 setFontLocation(String fontLocation)50 public static void setFontLocation(String fontLocation) { 51 sFontLocation = fontLocation; 52 } 53 54 @LayoutlibDelegate getSystemFontConfigInternal( String fontsXml, String systemFontDir, String oemXml, String productFontDir, Map<String, File> updatableFontMap, long lastModifiedDate, int configVersion)55 /*package*/ static FontConfig getSystemFontConfigInternal( 56 String fontsXml, 57 String systemFontDir, 58 String oemXml, 59 String productFontDir, 60 Map<String, File> updatableFontMap, 61 long lastModifiedDate, 62 int configVersion) { 63 sIsTypefaceInitialized = true; 64 int lastSeparator = fontsXml.lastIndexOf('/'); 65 String fileName = fontsXml.substring(lastSeparator + 1); 66 return SystemFonts.getSystemFontConfigInternal_Original( 67 sFontLocation + fileName, sFontLocation, null, null, updatableFontMap, 68 lastModifiedDate, configVersion); 69 } 70 71 @LayoutlibDelegate mmap(@onNull String fullPath)72 /*package*/ static ByteBuffer mmap(@NonNull String fullPath) { 73 // Android does memory mapping for font files. But Windows keeps files open 74 // until the byte buffer from the memory mapping is garbage collected. 75 // To avoid that, on Windows, read the file into a byte buffer instead. 76 // See JDK-4715154. 77 String osName = System.getProperty("os.name").toLowerCase(Locale.US); 78 if (osName.startsWith("windows")) { 79 try (FileInputStream file = new FileInputStream(fullPath)) { 80 final FileChannel fileChannel = file.getChannel(); 81 final int size = (int) fileChannel.size(); 82 // Native code requires the ByteBuffer to be direct 83 // (see android/graphics/fonts/Font.cpp) 84 ByteBuffer buffer = ByteBuffer.allocateDirect(size); 85 fileChannel.read(buffer); 86 return buffer; 87 } catch (IOException e) { 88 Log.e(TAG, "Error mapping font file " + fullPath); 89 return null; 90 } 91 } else { 92 return SystemFonts.mmap_Original(fullPath); 93 } 94 } 95 }