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