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