summaryrefslogtreecommitdiff
path: root/PinyinIME/src/com/android/inputmethod/pinyin/PinyinDecoderService.java
blob: a4a3ac4fc4562d9d33631158b32026615ad026fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.pinyin;

import com.android.inputmethod.pinyin.IPinyinDecoderService;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import android.app.Service;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

/**
 * This class is used to separate the input method kernel in an individual
 * service so that both IME and IME-syncer can use it.
 */
public class PinyinDecoderService extends Service {
    native static boolean nativeImOpenDecoder(byte fn_sys_dict[],
            byte fn_usr_dict[]);

    native static boolean nativeImOpenDecoderFd(FileDescriptor fd,
            long startOffset, long length, byte fn_usr_dict[]);

    native static void nativeImSetMaxLens(int maxSpsLen, int maxHzsLen);

    native static boolean nativeImCloseDecoder();

    native static int nativeImSearch(byte pyBuf[], int pyLen);

    native static int nativeImDelSearch(int pos, boolean is_pos_in_splid,
            boolean clear_fixed_this_step);

    native static void nativeImResetSearch();

    native static int nativeImAddLetter(byte ch);

    native static String nativeImGetPyStr(boolean decoded);

    native static int nativeImGetPyStrLen(boolean decoded);

    native static int[] nativeImGetSplStart();

    native static String nativeImGetChoice(int choiceId);

    native static int nativeImChoose(int choiceId);

    native static int nativeImCancelLastChoice();

    native static int nativeImGetFixedLen();

    native static boolean nativeImCancelInput();

    native static boolean nativeImFlushCache();

    native static int nativeImGetPredictsNum(String fixedStr);

    native static String nativeImGetPredictItem(int predictNo);

    // Sync related
    native static String nativeSyncUserDict(byte[] user_dict, String tomerge);

    native static boolean nativeSyncBegin(byte[] user_dict);

    native static boolean nativeSyncFinish();

    native static String nativeSyncGetLemmas();

    native static int nativeSyncPutLemmas(String tomerge);

    native static int nativeSyncGetLastCount();

    native static int nativeSyncGetTotalCount();

    native static boolean nativeSyncClearLastGot();

    native static int nativeSyncGetCapacity();

    private final static int MAX_PATH_FILE_LENGTH = 100;
    private static boolean inited = false;

    private String mUsr_dict_file;

    static {
        try {
            System.loadLibrary("jni_pinyinime");
        } catch (UnsatisfiedLinkError ule) {
            Log.e("PinyinDecoderService",
                    "WARNING: Could not load jni_pinyinime natives");
        }
    }

    // Get file name of the specified dictionary
    private boolean getUsrDictFileName(byte usr_dict[]) {
        if (null == usr_dict) {
            return false;
        }

        for (int i = 0; i < mUsr_dict_file.length(); i++)
            usr_dict[i] = (byte) mUsr_dict_file.charAt(i);
        usr_dict[mUsr_dict_file.length()] = 0;

        return true;
    }

    private void initPinyinEngine() {
        byte usr_dict[];
        usr_dict = new byte[MAX_PATH_FILE_LENGTH];

        // Here is how we open a built-in dictionary for access through
        // a file descriptor...
        AssetFileDescriptor afd = getResources().openRawResourceFd(
                R.raw.dict_pinyin);
        if (Environment.getInstance().needDebug()) {
            Log
                    .i("foo", "Dict: start=" + afd.getStartOffset()
                            + ", length=" + afd.getLength() + ", fd="
                            + afd.getParcelFileDescriptor());
        }
        if (getUsrDictFileName(usr_dict)) {
            inited = nativeImOpenDecoderFd(afd.getFileDescriptor(), afd
                    .getStartOffset(), afd.getLength(), usr_dict);
        }
        try {
            afd.close();
        } catch (IOException e) {
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mUsr_dict_file = getFileStreamPath("usr_dict.dat").getPath();
        // This is a hack to make sure our "files" directory has been
        // created.
        try {
            openFileOutput("dummy", 0).close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }

        initPinyinEngine();
    }

    @Override
    public void onDestroy() {
        nativeImCloseDecoder();
        inited = false;
        super.onDestroy();
    }

    private final IPinyinDecoderService.Stub mBinder = new IPinyinDecoderService.Stub() {
        public int getInt() {
            return 12345;
        }

        public void setMaxLens(int maxSpsLen, int maxHzsLen) {
            nativeImSetMaxLens(maxSpsLen, maxHzsLen);
        }

        public int imSearch(byte[] pyBuf, int pyLen) {
            return nativeImSearch(pyBuf, pyLen);
        }

        public int imDelSearch(int pos, boolean is_pos_in_splid,
                boolean clear_fixed_this_step) {
            return nativeImDelSearch(pos, is_pos_in_splid,
                    clear_fixed_this_step);
        }

        public void imResetSearch() {
            nativeImResetSearch();
        }

        public int imAddLetter(byte ch) {
            return nativeImAddLetter(ch);
        }

        public String imGetPyStr(boolean decoded) {
            return nativeImGetPyStr(decoded);
        }

        public int imGetPyStrLen(boolean decoded) {
            return nativeImGetPyStrLen(decoded);
        }

        public int[] imGetSplStart() {
            return nativeImGetSplStart();
        }

        public String imGetChoice(int choiceId) {
            return nativeImGetChoice(choiceId);
        }

        public String imGetChoices(int choicesNum) {
            String retStr = null;
            for (int i = 0; i < choicesNum; i++) {
                if (null == retStr)
                    retStr = nativeImGetChoice(i);
                else
                    retStr += " " + nativeImGetChoice(i);
            }
            return retStr;
        }

        public List<String> imGetChoiceList(int choicesStart, int choicesNum,
                int sentFixedLen) {
            Vector<String> choiceList = new Vector<String>();
            for (int i = choicesStart; i < choicesStart + choicesNum; i++) {
                String retStr = nativeImGetChoice(i);
                if (0 == i) retStr = retStr.substring(sentFixedLen);
                choiceList.add(retStr);
            }
            return choiceList;
        }

        public int imChoose(int choiceId) {
            return nativeImChoose(choiceId);
        }

        public int imCancelLastChoice() {
            return nativeImCancelLastChoice();
        }

        public int imGetFixedLen() {
            return nativeImGetFixedLen();
        }

        public boolean imCancelInput() {
            return nativeImCancelInput();
        }

        public void imFlushCache() {
            nativeImFlushCache();
        }

        public int imGetPredictsNum(String fixedStr) {
            return nativeImGetPredictsNum(fixedStr);
        }

        public String imGetPredictItem(int predictNo) {
            return nativeImGetPredictItem(predictNo);
        }

        public List<String> imGetPredictList(int predictsStart, int predictsNum) {
            Vector<String> predictList = new Vector<String>();
            for (int i = predictsStart; i < predictsStart + predictsNum; i++) {
                predictList.add(nativeImGetPredictItem(i));
            }
            return predictList;
        }

        public String syncUserDict(String tomerge) {
            byte usr_dict[];
            usr_dict = new byte[MAX_PATH_FILE_LENGTH];

            if (getUsrDictFileName(usr_dict)) {
                return nativeSyncUserDict(usr_dict, tomerge);
            }
            return null;
        }

        public boolean syncBegin() {
            byte usr_dict[];
            usr_dict = new byte[MAX_PATH_FILE_LENGTH];

            if (getUsrDictFileName(usr_dict)) {
                return nativeSyncBegin(usr_dict);
            }
            return false;
        }

        public void syncFinish() {
            nativeSyncFinish();
        }

        public int syncPutLemmas(String tomerge) {
            return nativeSyncPutLemmas(tomerge);
        }

        public String syncGetLemmas() {
            return nativeSyncGetLemmas();
        }

        public int syncGetLastCount() {
            return nativeSyncGetLastCount();
        }

        public int syncGetTotalCount() {
            return nativeSyncGetTotalCount();
        }

        public void syncClearLastGot() {
            nativeSyncClearLastGot();
        }

        public int imSyncGetCapacity() {
            return nativeSyncGetCapacity();
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}