1 /* 2 * Copyright (C) 2009 Google Inc. 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 17 package com.android.inputmethod.voice; 18 19 import android.os.Bundle; 20 import android.util.Log; 21 import android.view.inputmethod.EditorInfo; 22 import android.view.inputmethod.ExtractedText; 23 import android.view.inputmethod.ExtractedTextRequest; 24 import android.view.inputmethod.InputConnection; 25 26 /** 27 * Represents information about a given text field, which can be passed 28 * to the speech recognizer as context information. 29 */ 30 public class FieldContext { 31 private static final boolean DBG = false; 32 33 static final String LABEL = "label"; 34 static final String HINT = "hint"; 35 static final String PACKAGE_NAME = "packageName"; 36 static final String FIELD_ID = "fieldId"; 37 static final String FIELD_NAME = "fieldName"; 38 static final String SINGLE_LINE = "singleLine"; 39 static final String INPUT_TYPE = "inputType"; 40 static final String IME_OPTIONS = "imeOptions"; 41 static final String SELECTED_LANGUAGE = "selectedLanguage"; 42 static final String ENABLED_LANGUAGES = "enabledLanguages"; 43 44 Bundle mFieldInfo; 45 46 public FieldContext(InputConnection conn, EditorInfo info, 47 String selectedLanguage, String[] enabledLanguages) { 48 mFieldInfo = new Bundle(); 49 addEditorInfoToBundle(info, mFieldInfo); 50 addInputConnectionToBundle(conn, mFieldInfo); 51 addLanguageInfoToBundle(selectedLanguage, enabledLanguages, mFieldInfo); 52 if (DBG) Log.i("FieldContext", "Bundle = " + mFieldInfo.toString()); 53 } 54 55 private static String safeToString(Object o) { 56 if (o == null) { 57 return ""; 58 } 59 return o.toString(); 60 } 61 62 private static void addEditorInfoToBundle(EditorInfo info, Bundle bundle) { 63 if (info == null) { 64 return; 65 } 66 67 bundle.putString(LABEL, safeToString(info.label)); 68 bundle.putString(HINT, safeToString(info.hintText)); 69 bundle.putString(PACKAGE_NAME, safeToString(info.packageName)); 70 bundle.putInt(FIELD_ID, info.fieldId); 71 bundle.putString(FIELD_NAME, safeToString(info.fieldName)); 72 bundle.putInt(INPUT_TYPE, info.inputType); 73 bundle.putInt(IME_OPTIONS, info.imeOptions); 74 } 75 76 private static void addInputConnectionToBundle( 77 InputConnection conn, Bundle bundle) { 78 if (conn == null) { 79 return; 80 } 81 82 ExtractedText et = conn.getExtractedText(new ExtractedTextRequest(), 0); 83 if (et == null) { 84 return; 85 } 86 bundle.putBoolean(SINGLE_LINE, (et.flags & et.FLAG_SINGLE_LINE) > 0); 87 } 88 89 private static void addLanguageInfoToBundle( 90 String selectedLanguage, String[] enabledLanguages, Bundle bundle) { 91 bundle.putString(SELECTED_LANGUAGE, selectedLanguage); 92 bundle.putStringArray(ENABLED_LANGUAGES, enabledLanguages); 93 } 94 95 public Bundle getBundle() { 96 return mFieldInfo; 97 } 98 99 public String toString() { 100 return mFieldInfo.toString(); 101 } 102 } 103