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