SpellCheckerService.java revision 142d7575b52d03d46246e3b142e22ebc32d45a84
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package android.service.textservice; 18 19import com.android.internal.textservice.ISpellCheckerService; 20import com.android.internal.textservice.ISpellCheckerSession; 21import com.android.internal.textservice.ISpellCheckerSessionListener; 22 23import android.app.Service; 24import android.content.Intent; 25import android.os.IBinder; 26import android.os.RemoteException; 27import android.view.textservice.SuggestionsInfo; 28import android.view.textservice.TextInfo; 29 30import java.lang.ref.WeakReference; 31 32/** 33 * SpellCheckerService provides an abstract base class for a spell checker. 34 * This class combines a service to the system with the spell checker service interface that 35 * spell checker must implement. 36 */ 37public abstract class SpellCheckerService extends Service { 38 private static final String TAG = SpellCheckerService.class.getSimpleName(); 39 public static final String SERVICE_INTERFACE = 40 "android.service.textservice.SpellCheckerService"; 41 42 private final SpellCheckerServiceBinder mBinder = new SpellCheckerServiceBinder(this); 43 44 /** 45 * Get suggestions for specified text in TextInfo. 46 * This function will run on the incoming IPC thread. So, this is not called on the main thread, 47 * but will be called in series on another thread. 48 * @param textInfo the text metadata 49 * @param suggestionsLimit the number of limit of suggestions returned 50 * @param locale the locale for getting suggestions 51 * @return SuggestionInfo which contains suggestions for textInfo 52 */ 53 public abstract SuggestionsInfo getSuggestions( 54 TextInfo textInfo, int suggestionsLimit, String locale); 55 56 /** 57 * A batch process of onGetSuggestions. 58 * This function will run on the incoming IPC thread. So, this is not called on the main thread, 59 * but will be called in series on another thread. 60 * @param textInfos an array of the text metadata 61 * @param locale the locale for getting suggestions 62 * @param suggestionsLimit the number of limit of suggestions returned 63 * @param sequentialWords true if textInfos can be treated as sequential words. 64 * @return an array of SuggestionInfo of onGetSuggestions 65 */ 66 public SuggestionsInfo[] getSuggestionsMultiple( 67 TextInfo[] textInfos, String locale, int suggestionsLimit, boolean sequentialWords) { 68 final int length = textInfos.length; 69 final SuggestionsInfo[] retval = new SuggestionsInfo[length]; 70 for (int i = 0; i < length; ++i) { 71 retval[i] = getSuggestions(textInfos[i], suggestionsLimit, locale); 72 retval[i].setCookieAndSequence(textInfos[i].getCookie(), textInfos[i].getSequence()); 73 } 74 return retval; 75 } 76 77 /** 78 * Request to abort all tasks executed in SpellChecker. 79 * This function will run on the incoming IPC thread. So, this is not called on the main thread, 80 * but will be called in series on another thread. 81 */ 82 public void cancel() {} 83 84 /** 85 * Implement to return the implementation of the internal spell checker 86 * service interface. Subclasses should not override. 87 */ 88 @Override 89 public final IBinder onBind(final Intent intent) { 90 return mBinder; 91 } 92 93 private static class SpellCheckerSessionImpl extends ISpellCheckerSession.Stub { 94 private final WeakReference<SpellCheckerService> mInternalServiceRef; 95 private final String mLocale; 96 private final ISpellCheckerSessionListener mListener; 97 98 public SpellCheckerSessionImpl( 99 SpellCheckerService service, String locale, ISpellCheckerSessionListener listener) { 100 mInternalServiceRef = new WeakReference<SpellCheckerService>(service); 101 mLocale = locale; 102 mListener = listener; 103 } 104 105 @Override 106 public void getSuggestionsMultiple( 107 TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) { 108 final SpellCheckerService service = mInternalServiceRef.get(); 109 if (service == null) return; 110 try { 111 mListener.onGetSuggestions( 112 service.getSuggestionsMultiple(textInfos, mLocale, 113 suggestionsLimit, sequentialWords)); 114 } catch (RemoteException e) { 115 } 116 } 117 118 @Override 119 public void cancel() { 120 final SpellCheckerService service = mInternalServiceRef.get(); 121 if (service == null) return; 122 service.cancel(); 123 } 124 } 125 126 private static class SpellCheckerServiceBinder extends ISpellCheckerService.Stub { 127 private final WeakReference<SpellCheckerService> mInternalServiceRef; 128 129 public SpellCheckerServiceBinder(SpellCheckerService service) { 130 mInternalServiceRef = new WeakReference<SpellCheckerService>(service); 131 } 132 133 @Override 134 public ISpellCheckerSession getISpellCheckerSession( 135 String locale, ISpellCheckerSessionListener listener) { 136 final SpellCheckerService service = mInternalServiceRef.get(); 137 if (service == null) return null; 138 return new SpellCheckerSessionImpl(service, locale, listener); 139 } 140 } 141} 142