/ime/latinime/src/com/googlecode/eyesfree/inputmethod/latin/tutorial/TutorialModule.java

http://eyes-free.googlecode.com/ · Java · 165 lines · 69 code · 23 blank · 73 comment · 5 complexity · ddee2a555386603590a2ef511315e37c MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  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. package com.googlecode.eyesfree.inputmethod.latin.tutorial;
  17. import android.content.Context;
  18. import android.view.LayoutInflater;
  19. import android.widget.FrameLayout;
  20. import android.widget.TextView;
  21. import android.widget.ViewAnimator;
  22. import com.googlecode.eyesfree.inputmethod.latin.R;
  23. import java.util.TreeSet;
  24. /**
  25. * Abstract class that represents a single module within a tutorial.
  26. *
  27. * @author alanv@google.com (Alan Viverette)
  28. */
  29. public abstract class TutorialModule extends FrameLayout implements TutorialReader.ReaderListener {
  30. private final TutorialController mController;
  31. private final TreeSet<Integer> mFlags;
  32. private final ViewAnimator mInstructions;
  33. /** Whether this module is currently focused. */
  34. private boolean mIsVisible;
  35. /**
  36. * Constructs a new tutorial module for the given context and controller
  37. * with the specified layout.
  38. *
  39. * @param context The parent context.
  40. * @param controller The parent tutorial controller.
  41. * @param layoutResId The layout to use for this module.
  42. */
  43. public TutorialModule(Context context, TutorialController controller, int layoutResId) {
  44. super(context);
  45. LayoutInflater.from(context).inflate(layoutResId, this, true);
  46. mController = controller;
  47. mFlags = new TreeSet<Integer>();
  48. mInstructions = (ViewAnimator) findViewById(R.id.tutorial_instructions);
  49. }
  50. /**
  51. * Returns the controller for this tutorial.
  52. *
  53. * @return Tthe controller for this tutorial.
  54. */
  55. protected TutorialController getController() {
  56. return mController;
  57. }
  58. /**
  59. * Called when this tutorial gains focus.
  60. */
  61. public void onShown() {
  62. mIsVisible = true;
  63. // Reset tutorial.
  64. mFlags.clear();
  65. mInstructions.removeAllViews();
  66. }
  67. /**
  68. * Called when this tutorial loses focus.
  69. */
  70. public void onHidden() {
  71. mIsVisible = false;
  72. }
  73. /**
  74. * Returns {@code true} if this tutorial is currently focused.
  75. *
  76. * @return {@code true} if this tutorial is currently focused.
  77. */
  78. public boolean isVisible() {
  79. return mIsVisible;
  80. }
  81. /**
  82. * Formats an instruction string and adds it to the speaking queue.
  83. *
  84. * @param resId The resource id of the instruction string.
  85. * @param formatArgs Optional formatting arguments.
  86. * @see String#format(String, Object...)
  87. */
  88. protected void addInstruction(final int resId, Object... formatArgs) {
  89. if (!mIsVisible || mInstructions == null) {
  90. return;
  91. }
  92. // Construct a new TextView with the instruction
  93. final String text = getContext().getString(resId, formatArgs);
  94. final int index = mInstructions.getChildCount();
  95. final TextView view = new TextView(getContext());
  96. view.setText(text);
  97. view.setTextAppearance(getContext(), android.R.style.TextAppearance_Small_Inverse);
  98. mInstructions.addView(view, index);
  99. mInstructions.setDisplayedChild(index);
  100. mController.speak(text, resId);
  101. }
  102. @Override
  103. public void onUtteranceCompleted(final int resId) {
  104. mController.runOnUiThread(new Runnable() {
  105. @Override
  106. public void run() {
  107. onInstructionRead(resId);
  108. }
  109. });
  110. }
  111. /**
  112. * Optional override. Receives spoken instruction completion events.
  113. *
  114. * @param resId The resource if of the spoken instruction.
  115. * @see #addInstruction(int, Object...)
  116. */
  117. public void onInstructionRead(int resId) {
  118. // Placeholder, do nothing.
  119. }
  120. /**
  121. * Returns {@code true} if the flag with the specified id has been set.
  122. *
  123. * @param flagId The id of the flag to check for.
  124. * @return {@code true} if the flag with the specified id has been set.
  125. */
  126. protected boolean hasFlag(int flagId) {
  127. return mFlags.contains(flagId);
  128. }
  129. /**
  130. * Sets or removes the flag with the specified id.
  131. *
  132. * @param flagId The id of the flag to modify.
  133. * @param value {@code true} to set the flag, {@code false} to remove it.
  134. */
  135. protected void setFlag(int flagId, boolean value) {
  136. if (value) {
  137. mFlags.add(flagId);
  138. } else {
  139. mFlags.remove(flagId);
  140. }
  141. }
  142. }