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.Bundle;
     22 import android.util.Log;
     23 import android.view.textservice.SpellCheckerSession;
     24 import android.view.textservice.SuggestionsInfo;
     25 import android.view.textservice.TextInfo;
     26 import android.view.textservice.TextServicesManager;
     27 import android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener;
     28 import android.widget.TextView;
     29 import java.lang.StringBuilder;
     30 
     31 public class HelloSpellCheckerActivity extends Activity implements SpellCheckerSessionListener {
     32     private static final String TAG = HelloSpellCheckerActivity.class.getSimpleName();
     33     private TextView mMainView;
     34     private SpellCheckerSession mScs;
     35     /** Called when the activity is first created. */
     36     @Override
     37     public void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         setContentView(R.layout.main);
     40         mMainView = (TextView)findViewById(R.id.main);
     41     }
     42 
     43     @Override
     44     public void onResume() {
     45         super.onResume();
     46         final TextServicesManager tsm = (TextServicesManager) getSystemService(
     47                 Context.TEXT_SERVICES_MANAGER_SERVICE);
     48         mScs = tsm.newSpellCheckerSession(null, null, this, true);
     49         if (mScs != null) {
     50             // Instantiate TextInfo for each query
     51             // TextInfo can be passed a sequence number and a cookie number to identify the result
     52             mScs.getSuggestions(new TextInfo("tgis"), 3);
     53             mScs.getSuggestions(new TextInfo("hllo"), 3);
     54             mScs.getSuggestions(new TextInfo("helloworld"), 3);
     55         } else {
     56             Log.e(TAG, "Couldn't obtain the spell checker service.");
     57         }
     58     }
     59 
     60     @Override
     61     public void onPause() {
     62         super.onPause();
     63         if (mScs != null) {
     64             mScs.close();
     65         }
     66     }
     67 
     68     @Override
     69     public void onGetSuggestions(final SuggestionsInfo[] arg0) {
     70         final StringBuilder sb = new StringBuilder();
     71         for (int i = 0; i < arg0.length; ++i) {
     72             // Returned suggestions are contained in SuggestionsInfo
     73             final int len = arg0[i].getSuggestionsCount();
     74             sb.append('\n');
     75             for (int j = 0; j < len; ++j) {
     76                 sb.append("," + arg0[i].getSuggestionAt(j));
     77             }
     78             sb.append(" (" + len + ")");
     79         }
     80         runOnUiThread(new Runnable() {
     81             @Override
     82             public void run() {
     83                 mMainView.append(sb.toString());
     84             }
     85         });
     86     }
     87 }
     88