/ime/latinime/src/com/googlecode/eyesfree/inputmethod/voice/FieldContext.java

http://eyes-free.googlecode.com/ · Java · 103 lines · 70 code · 14 blank · 19 comment · 9 complexity · 361c300524b7f44860be9ca3634924af MD5 · raw file

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