?? emotions.java.svn-base
字號:
/******************************************************************************* Jimm - Mobile Messaging - J2ME ICQ clone Copyright (C) 2003-05 Jimm Project This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ******************************************************************************** File: src/jimm/Emotions.java Version: ###VERSION### Date: ###DATE### Author(s): Artyomov Denis *******************************************************************************/package jimm;//#sijapp cond.if modules_SMILES_STD="true" | modules_SMILES_ANI="true" #import java.util.Hashtable;import java.util.Timer;import java.util.TimerTask;import java.util.Vector;import javax.microedition.lcdui.*;import java.io.*;import jimm.comm.Util;import jimm.util.ResourceBundle;import DrawControls.*;public class Emotions implements VirtualListCommands, CommandListener{ private static Emotions _this; private static Vector imagesData = new Vector(); private static Vector timeData = new Vector(); private static Vector findedEmotions = new Vector(); private static boolean used; private static boolean animated; private static int[] selEmotionsIndexes, textCorrIndexes; private static String[] selEmotionsWord; private static String[] selEmotionsSmileNames; private static String[] textCorrWords; private static boolean[] emoFinded; private static String setName; public Emotions() { used = false; _this = this; load(); } static void load() { int iconsSize = -1; animated = false; String lang = Options.getString(Options.OPTION_UI_LANGUAGE).toLowerCase(); if (lang == null || lang.length() == 0) lang = "en"; ImageList imageList = new ImageList(); Vector textCorr = new Vector(); Vector selEmotions = new Vector(); Vector timeingsValues = new Vector(); InputStream stream; DataInputStream dos; Hashtable transl = new Hashtable(); //#sijapp cond.if target="MIDP2" | target="SIEMENS2"# imageList.setScale(Options.getInt(Options.OPTION_IMG_SCALE));//#sijapp cond.end# //#sijapp cond.if modules_DEBUGLOG is "true"# System.gc(); long mem = Runtime.getRuntime().freeMemory(); //#sijapp cond.end# String fileLine = null; try { stream = _this.getClass().getResourceAsStream("/ani_emotions/animate.txt"); if (stream != null) { dos = new DataInputStream(stream); String[] lineItems; String[] nameAndExt; String[] imgAndTime; String fileName; int imgWidth; int index; byte[] timeDataArray; for (;;) { fileLine = readLineFromStream(dos); if (fileLine == null) break; lineItems = Util.explode(fileLine, ' '); if (lineItems.length >= 3) { fileName = lineItems[0]; imgWidth = Integer.parseInt(lineItems[1]); nameAndExt = Util.explode(fileName, '.'); index = Integer.parseInt(nameAndExt[0]); imageList.load("/ani_emotions/"+fileName, imgWidth, -1, -1, Jimm.getPhoneVendor() == Jimm.PHONE_NOKIA); imagesData.setSize(index+1); imagesData.setElementAt(imageList.getImages(), index); timeingsValues.removeAllElements(); for (int i = 2; i < lineItems.length; i++) { imgAndTime = Util.explode(lineItems[i], ','); timeingsValues.addElement(new Byte((byte)(0xFF&Integer.parseInt(imgAndTime[0])))); timeingsValues.addElement(new Byte((byte)(0xFF&Integer.parseInt(imgAndTime[1])))); } timeData.setSize(index+1); if (timeingsValues.size() > 2) { timeDataArray = new byte[timeingsValues.size()]; for (int i = 0; i < timeDataArray.length; i++) timeDataArray[i] = ((Byte)timeingsValues.elementAt(i)).byteValue(); timeData.setElementAt(timeDataArray, index); timeDataArray = null; } } } animated = true; } else animated = false; String translFile = "/smiles."+lang+".txt"; loadTranslation(animated ? "/ani_emotions"+translFile : translFile, transl); // Load file "smiles.txt" stream = _this.getClass().getResourceAsStream(animated ? "/ani_emotions/smiles.txt" : "/smiles.txt"); if (stream == null) return; dos = new DataInputStream(stream); fileLine = readLineFromStream(dos); // Read icon size if (!animated) { iconsSize = Integer.parseInt(fileLine); setName = null; } // Read emotions set name else { String[] emoData = Util.explode(fileLine, '|'); String[] langAndName; if (emoData.length >= 1) { setName = emoData[0]; for (int i = 1; i < emoData.length; i++) { langAndName = Util.explode(emoData[i], ','); if (langAndName.length == 2 && lang.equals(langAndName[0].toLowerCase())) { setName = langAndName[1]; break; } } } } String[] lineItems; Integer currIndex; String smileName; String translation; String word; for (;;) { fileLine = readLineFromStream(dos); if (fileLine == null) break; lineItems = Util.explode(fileLine, ' '); if (lineItems.length >= 3) { if (lineItems[0].charAt(0) == '#') continue; currIndex = Integer.valueOf(lineItems[0]); smileName = lineItems[1]; translation = (String)transl.get(smileName.toLowerCase()); smileName = (translation != null) ? translation : lineItems[1].replace('_', ' '); for (int i = 2; i < lineItems.length; i++) { word = lineItems[i]; if (word.length() == 0) continue; insertTextCorr(textCorr, word.toLowerCase(), currIndex); if (word.indexOf('_') != -1 && word.length() > 3) insertTextCorr(textCorr, word.replace('_', ' ').toLowerCase(), currIndex); if (i == 2) selEmotions.addElement(new Object[] { currIndex, word, smileName }); } } } stream.close(); // Read images if (!animated) { imageList.load("/smiles.png", iconsSize, iconsSize, -1, Jimm.getPhoneVendor() == Jimm.PHONE_NOKIA); for (int i = 0; i < imageList.size(); i++) imagesData.addElement(new Image[] { imageList.elementAt(i) }); timeData.setSize(imagesData.size()); } used = true; } catch (OutOfMemoryError e) { imageList.removeAllElements(); imagesData.removeAllElements(); timeData.removeAllElements(); findedEmotions.removeAllElements(); imageList = null; imagesData = null; timeData = null; findedEmotions = null; selEmotionsIndexes = null; textCorrIndexes = null; selEmotionsWord = null; selEmotionsSmileNames = null; textCorrWords = null; emoFinded = null; System.gc(); Jimm.setLoadError("No memory to load emotions images!"); return; } catch (Exception e) { e.printStackTrace(); Jimm.setLoadError("Error while loading emotions: "+e.toString()); return; } // Write emotions data from vectors to arrays int size = selEmotions.size(); selEmotionsIndexes = new int[size]; selEmotionsWord = new String[size]; selEmotionsSmileNames = new String[size]; Object[] data; for (int i = 0; i < size; i++) { data = (Object[]) selEmotions.elementAt(i); selEmotionsIndexes[i] = ((Integer) data[0]).intValue(); selEmotionsWord[i] = (String) data[1]; selEmotionsSmileNames[i] = (String) data[2]; } size = textCorr.size(); textCorrWords = new String[size]; textCorrIndexes = new int[size]; emoFinded = new boolean[size]; for (int i = 0; i < size; i++) { data = (Object[]) textCorr.elementAt(i); textCorrWords[i] = (String) data[0]; textCorrIndexes[i] = ((Integer) data[1]).intValue(); } data = null; //#sijapp cond.if modules_DEBUGLOG is "true"# selEmotions.removeAllElements(); selEmotions = null; textCorr.removeAllElements(); textCorr = null; dos = null; stream = null; System.gc(); DebugLog.addText("Emotions used: "+(mem - Runtime.getRuntime().freeMemory())); //#sijapp cond.end# } static private void loadTranslation(String file, Hashtable transl) throws IOException { InputStream stream = _this.getClass().getResourceAsStream(file); if (stream == null) return; DataInputStream dos = new DataInputStream(stream); String line; String[] pair; for (;;) { line = readLineFromStream(dos); if (line == null) break; pair = Util.explode(line, '='); if (pair.length < 2) continue; if (pair[1].length() == 0) continue; transl.put(pair[0].toLowerCase(), pair[1]); } stream.close(); } // Add smile text and index to textCorr in decreasing order of text length static void insertTextCorr(Vector textCorr, String word, Integer index) { Object[] data = new Object[] { word, index }; int wordLen = word.length(); int size = textCorr.size(); int insIndex = 0; Object[] cvtData; int cvlDataWordLen; for (; insIndex < size; insIndex++) { cvtData = (Object[]) textCorr.elementAt(insIndex); cvlDataWordLen = ((String) cvtData[0]).length(); if (cvlDataWordLen <= wordLen) { textCorr.insertElementAt(data, insIndex); return; } } cvtData = null; textCorr.addElement(data); } // Reads simple word from stream. Used in Emotions(). // Returns "true" if break was found after word static String readLineFromStream(DataInputStream stream) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); boolean wasRead = false; try { for (;;) { int chr = stream.readByte(); if (chr == -1) break; wasRead = true; if (chr == '\n') break; if (chr != '\r') bytes.write(chr); } } catch (Exception e) {} return wasRead ? Util.byteArrayToString(bytes.toByteArray(), true) : null; } static private void findEmotionInText(String text, String emotion, int index, int startIndex, int recIndex) { if (!emoFinded[recIndex]) return; int findedIndex, len = emotion.length(); findedIndex = text.indexOf(emotion, startIndex); if (findedIndex == -1) { emoFinded[recIndex] = false; return; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -