Home | History | Annotate | Download | only in hellospellchecker
      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.hellospellchecker;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.os.Build;
     22 import android.os.Bundle;
     23 import android.util.Log;
     24 import android.view.textservice.SentenceSuggestionsInfo;
     25 import android.view.textservice.SpellCheckerSession;
     26 import android.view.textservice.SuggestionsInfo;
     27 import android.view.textservice.TextInfo;
     28 import android.view.textservice.TextServicesManager;
     29 import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
     30 import android.widget.TextView;
     31 import java.lang.StringBuilder;
     32 
     33 public class HelloSpellCheckerActivity extends Activity implements SpellCheckerSessionListener {
     34     private static final String TAG = HelloSpellCheckerActivity.class.getSimpleName();
     35     private static final int NOT_A_LENGTH = -1;
     36     private TextView mMainView;
     37     private SpellCheckerSession mScs;
     38 
     39     /** Called when the activity is first created. */
     40     @Override
     41     public void onCreate(Bundle savedInstanceState) {
     42         super.onCreate(savedInstanceState);
     43         setContentView(R.layout.main);
     44         mMainView = (TextView)findViewById(R.id.main);
     45     }
     46 
     47     private boolean isSentenceSpellCheckSupported() {
     48         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
     49     }
     50 
     51     @Override
     52     public void onResume() {
     53         super.onResume();
     54         final TextServicesManager tsm = (TextServicesManager) getSystemService(
     55                 Context.TEXT_SERVICES_MANAGER_SERVICE);
     56         mScs = tsm.newSpellCheckerSession(null, null, this, true);
     57 
     58         if (mScs != null) {
     59             // Instantiate TextInfo for each query
     60             // TextInfo can be passed a sequence number and a cookie number to identify the result
     61             if (isSentenceSpellCheckSupported()) {
     62                 // Note that getSentenceSuggestions works on JB or later.
     63                 Log.d(TAG, "Sentence spellchecking supported.");
     64                 mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo("tgisis")}, 3);
     65                 mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo(
     66                         "I wold like to here form you")}, 3);
     67                 mScs.getSentenceSuggestions(new TextInfo[] {new TextInfo("hell othere")}, 3);
     68             } else {
     69                 // Note that getSuggestions() is a deprecated API.
     70                 // It is recommended for an application running on Jelly Bean or later
     71                 // to call getSentenceSuggestions() only.
     72                 mScs.getSuggestions(new TextInfo("tgis"), 3);
     73                 mScs.getSuggestions(new TextInfo("hllo"), 3);
     74                 mScs.getSuggestions(new TextInfo("helloworld"), 3);
     75             }
     76         } else {
     77             Log.e(TAG, "Couldn't obtain the spell checker service.");
     78         }
     79     }
     80 
     81     @Override
     82     public void onPause() {
     83         super.onPause();
     84         if (mScs != null) {
     85             mScs.close();
     86         }
     87     }
     88 
     89     private void dumpSuggestionsInfoInternal(
     90             final StringBuilder sb, final SuggestionsInfo si, final int length, final int offset) {
     91         // Returned suggestions are contained in SuggestionsInfo
     92         final int len = si.getSuggestionsCount();
     93         sb.append('\n');
     94         for (int j = 0; j < len; ++j) {
     95             if (j != 0) {
     96                 sb.append(", ");
     97             }
     98             sb.append(si.getSuggestionAt(j));
     99         }
    100         sb.append(" (" + len + ")");
    101         if (length != NOT_A_LENGTH) {
    102             sb.append(" length = " + length + ", offset = " + offset);
    103         }
    104     }
    105 
    106     /**
    107      * Callback for {@link SpellCheckerSession#getSuggestions(TextInfo, int)}
    108      * and {@link SpellCheckerSession#getSuggestions(TextInfo[], int, boolean)}
    109      * @param results an array of {@link SuggestionsInfo}s.
    110      * These results are suggestions for {@link TextInfo}s queried by
    111      * {@link SpellCheckerSession#getSuggestions(TextInfo, int)} or
    112      * {@link SpellCheckerSession#getSuggestions(TextInfo[], int, boolean)}
    113      */
    114     @Override
    115     public void onGetSuggestions(final SuggestionsInfo[] arg0) {
    116         Log.d(TAG, "onGetSuggestions");
    117         final StringBuilder sb = new StringBuilder();
    118         for (int i = 0; i < arg0.length; ++i) {
    119             dumpSuggestionsInfoInternal(sb, arg0[i], 0, NOT_A_LENGTH);
    120         }
    121         runOnUiThread(new Runnable() {
    122             @Override
    123             public void run() {
    124                 mMainView.append(sb.toString());
    125             }
    126         });
    127     }
    128 
    129     /**
    130      * Callback for {@link SpellCheckerSession#getSentenceSuggestions(TextInfo[], int)}
    131      * @param results an array of {@link SentenceSuggestionsInfo}s.
    132      * These results are suggestions for {@link TextInfo}s
    133      * queried by {@link SpellCheckerSession#getSentenceSuggestions(TextInfo[], int)}.
    134      */
    135     @Override
    136     public void onGetSentenceSuggestions(final SentenceSuggestionsInfo[] arg0) {
    137         if (!isSentenceSpellCheckSupported()) {
    138             Log.e(TAG, "Sentence spell check is not supported on this platform, "
    139                     + "but accidentially called.");
    140             return;
    141         }
    142         Log.d(TAG, "onGetSentenceSuggestions");
    143         final StringBuilder sb = new StringBuilder();
    144         for (int i = 0; i < arg0.length; ++i) {
    145             final SentenceSuggestionsInfo ssi = arg0[i];
    146             for (int j = 0; j < ssi.getSuggestionsCount(); ++j) {
    147                 dumpSuggestionsInfoInternal(
    148                         sb, ssi.getSuggestionsInfoAt(j), ssi.getOffsetAt(j), ssi.getLengthAt(j));
    149             }
    150         }
    151         runOnUiThread(new Runnable() {
    152             @Override
    153             public void run() {
    154                 mMainView.append(sb.toString());
    155             }
    156         });
    157     }
    158 }
    159