static AndroidFont getFont(Font meFont) { AndroidFont result = fonts.get(meFont); if (result == null) { Typeface family = Typeface.SANS_SERIF; if (meFont.getFace() == Font.FACE_SYSTEM) { family = Typeface.SANS_SERIF; } else if (meFont.getFace() == Font.FACE_MONOSPACE) { family = Typeface.MONOSPACE; } else if (meFont.getFace() == Font.FACE_PROPORTIONAL) { family = Typeface.SANS_SERIF; } int style = 0; if ((meFont.getStyle() & Font.STYLE_PLAIN) != 0) { style |= Typeface.NORMAL; } if ((meFont.getStyle() & Font.STYLE_BOLD) != 0) { style |= Typeface.BOLD; } if ((meFont.getStyle() & Font.STYLE_ITALIC) != 0) { style |= Typeface.ITALIC; } boolean underlined = false; if ((meFont.getStyle() & Font.STYLE_UNDERLINED) != 0) { underlined = true; } int size = 0; float fontSize = 0; if (meFont.getSize() == Font.SIZE_SMALL) { fontSize = JimmActivity.getInstance() .getResources() .getDimensionPixelSize(R.dimen.small_font_size); size = MicroEmulatorActivity.config.FONT_SIZE_SMALL; } else if (meFont.getSize() == Font.SIZE_MEDIUM) { fontSize = JimmActivity.getInstance() .getResources() .getDimensionPixelSize(R.dimen.medium_font_size); size = MicroEmulatorActivity.config.FONT_SIZE_MEDIUM; } else if (meFont.getSize() == Font.SIZE_LARGE) { fontSize = JimmActivity.getInstance() .getResources() .getDimensionPixelSize(R.dimen.large_font_size); size = MicroEmulatorActivity.config.FONT_SIZE_LARGE; } // Once default font size is defined, // compute size relative to scaleDensity // to enable consistent font size ratio // accross any device resolution/density // size *= metrics.scaledDensity; // fontSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, metrics); result = new AndroidFont(Typeface.create(family, style), fontSize, underlined); fonts.put(meFont, result); } return result; }
/** * WARNING: Can only be used in JavaSE environments! Serializes the specified object. * * @param object the object * @param out the stream into which the object should be serialized * @param useObfuscation true when classnames are obfuscated * @throws IOException when serialization data could not be written or when encountering an object * that cannot be serialized */ public static void serialize(Object object, DataOutputStream out, boolean useObfuscation) throws IOException { // #endif out.writeByte(VERSION); boolean isNull = (object == null); out.writeBoolean(isNull); if (!isNull) { if (object instanceof Externalizable) { out.writeByte(TYPE_EXTERNALIZABLE); String className = object.getClass().getName(); // #if polish.JavaSE if (useObfuscation && obfuscationSerializeMap != null) { String obfuscatedClassName = (String) obfuscationSerializeMap.get(className); if (obfuscatedClassName != null) { // System.out.println("Serializer.serialize: translating classname from " + className + // " to " + obfuscatedClassName + " useObfuscationIndicator=" + // useObfuscationIndicator.get() ); className = obfuscatedClassName; } } // #endif // #debug debug // #= System.out.println("serializing " + className + "=" + object); out.writeUTF(className); ((Externalizable) object).write(out); } else if (object instanceof Externalizable[]) { out.writeByte(TYPE_EXTERNALIZABLE_ARRAY); String cn = object.getClass().getName(); cn = cn.substring(cn.lastIndexOf('[') + 2, cn.length() - 1); // #if polish.JavaSE if (useObfuscation && obfuscationSerializeMap != null) { String obfuscatedClassName = (String) obfuscationSerializeMap.get(cn); if (obfuscatedClassName != null) { // System.out.println("Serializer.serialize: translating classname from " + className + // " to " + obfuscatedClassName ); cn = obfuscatedClassName; } } // #endif out.writeUTF(cn); Externalizable[] externalizables = (Externalizable[]) object; out.writeInt(externalizables.length); Hashtable classNames = new Hashtable(); Class lastClass = null; byte lastId = 0; byte idCounter = 0; for (int i = 0; i < externalizables.length; i++) { Externalizable externalizable = externalizables[i]; Class currentClass = externalizable.getClass(); if (currentClass == lastClass) { out.writeByte(lastId); } else { Byte knownId = (Byte) classNames.get(currentClass); if (knownId != null) { out.writeByte(knownId.byteValue()); } else { // this is a class that has not yet been encountered: out.writeByte(0); idCounter++; String className = currentClass.getName(); // #if polish.JavaSE if (useObfuscation && obfuscationSerializeMap != null) { String obfuscatedClassName = (String) obfuscationSerializeMap.get(className); if (obfuscatedClassName != null) { // System.out.println("Serializer.serialize: translating classname from " + // className + " to " + obfuscatedClassName ); className = obfuscatedClassName; } } // #endif // #debug debug // #= System.out.println("serializing " + className + "=" + object); out.writeUTF(className); lastClass = currentClass; lastId = idCounter; classNames.put(currentClass, new Byte(lastId)); } } externalizable.write(out); } } else if (object instanceof Object[]) { out.writeByte(TYPE_OBJECT_ARRAY); Object[] objects = (Object[]) object; out.writeInt(objects.length); for (int i = 0; i < objects.length; i++) { Object obj = objects[i]; serialize(obj, out); } } else if (object instanceof Byte) { out.writeByte(TYPE_BYTE); out.writeByte(((Byte) object).byteValue()); } else if (object instanceof Short) { out.writeByte(TYPE_SHORT); out.writeShort(((Short) object).shortValue()); } else if (object instanceof Integer) { out.writeByte(TYPE_INTEGER); out.writeInt(((Integer) object).intValue()); } else if (object instanceof Long) { out.writeByte(TYPE_LONG); out.writeLong(((Long) object).longValue()); // #if polish.hasFloatingPoint } else if (object instanceof Float) { out.writeByte(TYPE_FLOAT); out.writeFloat(((Float) object).floatValue()); } else if (object instanceof Double) { out.writeByte(TYPE_DOUBLE); out.writeDouble(((Double) object).doubleValue()); // #endif } else if (object instanceof String) { out.writeByte(TYPE_STRING); out.writeUTF((String) object); } else if (object instanceof StringBuffer) { out.writeByte(TYPE_STRING_BUFFER); out.writeUTF(((StringBuffer) object).toString()); } else if (object instanceof Character) { out.writeByte(TYPE_CHARACTER); out.writeChar(((Character) object).charValue()); } else if (object instanceof Boolean) { out.writeByte(TYPE_BOOLEAN); out.writeBoolean(((Boolean) object).booleanValue()); } else if (object instanceof Date) { out.writeByte(TYPE_DATE); out.writeLong(((Date) object).getTime()); } else if (object instanceof Calendar) { out.writeByte(TYPE_CALENDAR); out.writeLong(((Calendar) object).getTime().getTime()); } else if (object instanceof Random) { out.writeByte(TYPE_RANDOM); } else if (object instanceof Hashtable) { out.writeByte(TYPE_HASHTABLE); Hashtable table = (Hashtable) object; out.writeInt(table.size()); Enumeration enumeration = table.keys(); while (enumeration.hasMoreElements()) { Object key = enumeration.nextElement(); serialize(key, out); Object value = table.get(key); serialize(value, out); } } else if (object instanceof Vector) { // also serializes stacks if (object instanceof Stack) { out.writeByte(TYPE_STACK); } else { out.writeByte(TYPE_VECTOR); } Vector vector = (Vector) object; int size = vector.size(); out.writeInt(size); for (int i = 0; i < size; i++) { serialize(vector.elementAt(i), out); } // #if polish.midp2 } else if (object instanceof Image) { out.writeByte(TYPE_IMAGE); // #if polish.JavaSE boolean handled = false; // we are within a Java SE environment. When the J2ME Polish runtime librarby is used, we // can // store the image in PNG format instead of in the much more verbose RGB format: try { // #if false Field bufferedImageField = null; // #else // # Field bufferedImageField = object.getClass().getDeclaredField("bufferedImage"); // #endif bufferedImageField.setAccessible(true); BufferedImage bufferedImage = (BufferedImage) bufferedImageField.get(object); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "png", byteOut); out.writeByte(TYPE_IMAGE_BYTES); byte[] data = byteOut.toByteArray(); out.writeInt(data.length); for (int i = 0; i < data.length; i++) { out.writeByte(data[i]); } handled = true; } catch (Exception e) { e.printStackTrace(); // #debug warn System.out.println( "Warning: Unable to retrieve bufferedImage field of javax.microedition.lcdui.Image - probably the enough-polish-runtime library is not used."); } if (!handled) { // #endif Image image = (Image) object; out.writeByte(TYPE_IMAGE_RGB); int width = image.getWidth(); int height = image.getHeight(); out.writeInt(width); out.writeInt(height); int[] rgb = new int[width * height]; image.getRGB(rgb, 0, width, 0, 0, width, height); for (int i = 0; i < rgb.length; i++) { out.writeInt(rgb[i]); } // #if polish.JavaSE } // #endif // #endif // #if polish.midp } else if (object instanceof Font) { out.writeByte(TYPE_FONT); Font font = (Font) object; out.writeInt(font.getFace()); out.writeInt(font.getStyle()); out.writeInt(font.getSize()); } else if (object instanceof Command) { out.writeByte(TYPE_COMMAND); Command command = (Command) object; out.writeInt(command.getCommandType()); out.writeInt(command.getPriority()); out.writeUTF(command.getLabel()); // #endif } else if (object instanceof byte[]) { out.writeByte(TYPE_BYTE_ARRAY); byte[] numbers = (byte[]) object; out.writeInt(numbers.length); out.write(numbers, 0, numbers.length); } else if (object instanceof short[]) { out.writeByte(TYPE_SHORT_ARRAY); short[] numbers = (short[]) object; out.writeInt(numbers.length); for (int i = 0; i < numbers.length; i++) { short number = numbers[i]; out.writeShort(number); } } else if (object instanceof int[]) { out.writeByte(TYPE_INT_ARRAY); int[] numbers = (int[]) object; out.writeInt(numbers.length); for (int i = 0; i < numbers.length; i++) { int number = numbers[i]; out.writeInt(number); } } else if (object instanceof long[]) { out.writeByte(TYPE_LONG_ARRAY); long[] numbers = (long[]) object; out.writeInt(numbers.length); for (int i = 0; i < numbers.length; i++) { long number = numbers[i]; out.writeLong(number); } // #if polish.hasFloatingPoint } else if (object instanceof float[]) { out.writeByte(TYPE_FLOAT_ARRAY); float[] numbers = (float[]) object; out.writeInt(numbers.length); for (int i = 0; i < numbers.length; i++) { float number = numbers[i]; out.writeFloat(number); } } else if (object instanceof double[]) { out.writeByte(TYPE_DOUBLE_ARRAY); double[] numbers = (double[]) object; out.writeInt(numbers.length); for (int i = 0; i < numbers.length; i++) { double number = numbers[i]; out.writeDouble(number); } // #endif } else if (object instanceof char[]) { out.writeByte(TYPE_CHAR_ARRAY); char[] characters = (char[]) object; out.writeInt(characters.length); for (int i = 0; i < characters.length; i++) { char c = characters[i]; out.writeChar(c); } } else if (object instanceof boolean[]) { out.writeByte(TYPE_BOOLEAN_ARRAY); boolean[] bools = (boolean[]) object; out.writeInt(bools.length); for (int i = 0; i < bools.length; i++) { boolean b = bools[i]; out.writeBoolean(b); } } else if (object instanceof String[]) { out.writeByte(TYPE_STRING_ARRAY); String[] strings = (String[]) object; out.writeInt(strings.length); for (int i = 0; i < strings.length; i++) { String s = strings[i]; out.writeUTF(s); } } else { throw new IOException("Cannot serialize " + object.getClass().getName()); } } }