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.utils.JniUtils;
20
21import java.util.Locale;
22
23public final class DicTraverseSession {
24    static {
25        JniUtils.loadNativeLibrary();
26    }
27
28    private static native long setDicTraverseSessionNative(String locale, long dictSize);
29    private static native void initDicTraverseSessionNative(long nativeDicTraverseSession,
30            long dictionary, int[] previousWord, int previousWordLength);
31    private static native void releaseDicTraverseSessionNative(long nativeDicTraverseSession);
32
33    private long mNativeDicTraverseSession;
34
35    public DicTraverseSession(Locale locale, long dictionary, long dictSize) {
36        mNativeDicTraverseSession = createNativeDicTraverseSession(
37                locale != null ? locale.toString() : "", dictSize);
38        initSession(dictionary);
39    }
40
41    public long getSession() {
42        return mNativeDicTraverseSession;
43    }
44
45    public void initSession(long dictionary) {
46        initSession(dictionary, null, 0);
47    }
48
49    public void initSession(long dictionary, int[] previousWord, int previousWordLength) {
50        initDicTraverseSessionNative(
51                mNativeDicTraverseSession, dictionary, previousWord, previousWordLength);
52    }
53
54    private final long createNativeDicTraverseSession(String locale, long dictSize) {
55        return setDicTraverseSessionNative(locale, dictSize);
56    }
57
58    private void closeInternal() {
59        if (mNativeDicTraverseSession != 0) {
60            releaseDicTraverseSessionNative(mNativeDicTraverseSession);
61            mNativeDicTraverseSession = 0;
62        }
63    }
64
65    public void close() {
66        closeInternal();
67    }
68
69    @Override
70    protected void finalize() throws Throwable {
71        try {
72            closeInternal();
73        } finally {
74            super.finalize();
75        }
76    }
77}
78