1 /* 2 * Copyright (C) 2011 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 17 package com.example.android.samplespellcheckerservice; 18 19 import android.service.textservice.SpellCheckerService; 20 import android.util.Log; 21 import android.view.textservice.SuggestionsInfo; 22 import android.view.textservice.TextInfo; 23 24 public class SampleSpellCheckerService extends SpellCheckerService { 25 private static final String TAG = SampleSpellCheckerService.class.getSimpleName(); 26 private static final boolean DBG = true; 27 @Override 28 public Session createSession() { 29 return new AndroidSpellCheckerSession(); 30 } 31 32 private static class AndroidSpellCheckerSession extends Session { 33 private String mLocale; 34 @Override 35 public void onCreate() { 36 mLocale = getLocale(); 37 } 38 39 @Override 40 public SuggestionsInfo onGetSuggestions(TextInfo textInfo, int suggestionsLimit) { 41 if (DBG) { 42 Log.d(TAG, "onGetSuggestions: " + textInfo.getText()); 43 } 44 final String input = textInfo.getText(); 45 final int length = input.length(); 46 // Just a fake logic: 47 // length <= 3 for short words that we assume are in the fake dictionary 48 // length > 20 for too long words that we assume can't be recognized (such as CJK words) 49 final int flags = length <= 3 ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY 50 : length <= 20 ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0; 51 return new SuggestionsInfo(flags, 52 new String[] {"aaa", "bbb", "Candidate for " + input, mLocale}); 53 } 54 } 55 } 56