1 /* 2 * Copyright (C) 2012, 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 com.android.inputmethod.latin; 18 19 import com.android.inputmethod.latin.common.NativeSuggestOptions; 20 import com.android.inputmethod.latin.define.DecoderSpecificConstants; 21 import com.android.inputmethod.latin.utils.JniUtils; 22 23 import java.util.Locale; 24 25 public final class DicTraverseSession { 26 static { 27 JniUtils.loadNativeLibrary(); 28 } 29 // Must be equal to MAX_RESULTS in native/jni/src/defines.h 30 private static final int MAX_RESULTS = 18; 31 public final int[] mInputCodePoints = 32 new int[DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH]; 33 public final int[][] mPrevWordCodePointArrays = 34 new int[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM][]; 35 public final boolean[] mIsBeginningOfSentenceArray = 36 new boolean[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; 37 public final int[] mOutputSuggestionCount = new int[1]; 38 public final int[] mOutputCodePoints = 39 new int[DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH * MAX_RESULTS]; 40 public final int[] mSpaceIndices = new int[MAX_RESULTS]; 41 public final int[] mOutputScores = new int[MAX_RESULTS]; 42 public final int[] mOutputTypes = new int[MAX_RESULTS]; 43 // Only one result is ever used 44 public final int[] mOutputAutoCommitFirstWordConfidence = new int[1]; 45 public final float[] mInputOutputWeightOfLangModelVsSpatialModel = new float[1]; 46 47 public final NativeSuggestOptions mNativeSuggestOptions = new NativeSuggestOptions(); 48 49 private static native long setDicTraverseSessionNative(String locale, long dictSize); 50 private static native void initDicTraverseSessionNative(long nativeDicTraverseSession, 51 long dictionary, int[] previousWord, int previousWordLength); 52 private static native void releaseDicTraverseSessionNative(long nativeDicTraverseSession); 53 54 private long mNativeDicTraverseSession; 55 56 public DicTraverseSession(Locale locale, long dictionary, long dictSize) { 57 mNativeDicTraverseSession = createNativeDicTraverseSession( 58 locale != null ? locale.toString() : "", dictSize); 59 initSession(dictionary); 60 } 61 62 public long getSession() { 63 return mNativeDicTraverseSession; 64 } 65 66 public void initSession(long dictionary) { 67 initSession(dictionary, null, 0); 68 } 69 70 public void initSession(long dictionary, int[] previousWord, int previousWordLength) { 71 initDicTraverseSessionNative( 72 mNativeDicTraverseSession, dictionary, previousWord, previousWordLength); 73 } 74 75 private static long createNativeDicTraverseSession(String locale, long dictSize) { 76 return setDicTraverseSessionNative(locale, dictSize); 77 } 78 79 private void closeInternal() { 80 if (mNativeDicTraverseSession != 0) { 81 releaseDicTraverseSessionNative(mNativeDicTraverseSession); 82 mNativeDicTraverseSession = 0; 83 } 84 } 85 86 public void close() { 87 closeInternal(); 88 } 89 90 @Override 91 protected void finalize() throws Throwable { 92 try { 93 closeInternal(); 94 } finally { 95 super.finalize(); 96 } 97 } 98 } 99