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

http://eyes-free.googlecode.com/ · Java · 166 lines · 127 code · 20 blank · 19 comment · 39 complexity · 787a4681ede4a2351756fcc3fac8f2ec 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.os.Bundle;
  19. import android.text.Editable;
  20. import android.text.TextWatcher;
  21. import android.view.View;
  22. import android.widget.TextView;
  23. import com.googlecode.eyesfree.inputmethod.latin.LatinIME;
  24. import com.googlecode.eyesfree.inputmethod.latin.R;
  25. /**
  26. * @author alanv@google.com (Alan Viverette)
  27. */
  28. public class LatinTutorialModule3 extends TutorialModule implements View.OnClickListener,
  29. View.OnFocusChangeListener, TextWatcher, LatinIMETutorial.KeyboardModeListener,
  30. LatinIMETutorial.KeyboardBroadcastListener {
  31. private static final int FLAG_STARTED = 0;
  32. private static final int FLAG_TYPING_MODE = 1;
  33. private static final int FLAG_HELLO_READY = 2;
  34. private static final int FLAG_HELLO_DONE = 3;
  35. private static final int FLAG_LEFT_KEYBOARD = 6;
  36. private static final int FLAG_TYPED_NOTHING = 7;
  37. private static final int FLAG_OUTSIDE = 8;
  38. private static final int FLAG_HIDDEN_MODE = 9;
  39. private static final int FLAG_NAVIGATING_MODE = 10;
  40. private final String mTextHello;
  41. public LatinTutorialModule3(Context context, TutorialController controller) {
  42. super(context, controller, R.layout.tutorial_3_talking_keyboard);
  43. findViewById(R.id.tutorial_previous).setOnClickListener(this);
  44. findViewById(R.id.tutorial_continue).setOnClickListener(this);
  45. TextView editText = (TextView) findViewById(R.id.tutorial_3_edittext);
  46. editText.setOnFocusChangeListener(this);
  47. editText.addTextChangedListener(this);
  48. mTextHello = context.getString(R.string.tutorial_3_text_hello);
  49. }
  50. @Override
  51. public void onShown() {
  52. super.onShown();
  53. if (!hasFlag(FLAG_STARTED)) {
  54. addInstruction(R.string.tutorial_3_message_1);
  55. setFlag(FLAG_STARTED, true);
  56. }
  57. }
  58. @Override
  59. public void onInstructionRead(int resId) {
  60. if (resId == R.string.tutorial_3_message_1) {
  61. findViewById(R.id.tutorial_3_edittext).requestFocusFromTouch();
  62. addInstruction(R.string.tutorial_3_message_2);
  63. } else if (resId == R.string.tutorial_3_message_3) {
  64. addInstruction(R.string.tutorial_3_message_4);
  65. } else if (resId == R.string.tutorial_3_message_4) {
  66. addInstruction(R.string.tutorial_3_message_5);
  67. setFlag(FLAG_HELLO_READY, true);
  68. }
  69. }
  70. @Override
  71. public void onClick(View v) {
  72. if (v.getId() == R.id.tutorial_previous) {
  73. getController().previous();
  74. } else if (v.getId() == R.id.tutorial_continue) {
  75. getController().next();
  76. }
  77. }
  78. @Override
  79. public void onFocusChange(View v, boolean hasFocus) {
  80. if (v.getId() == R.id.tutorial_3_edittext) {
  81. if (hasFlag(FLAG_TYPED_NOTHING) && !hasFlag(FLAG_OUTSIDE) && !hasFocus) {
  82. setFlag(FLAG_OUTSIDE, true);
  83. addInstruction(R.string.tutorial_3_outside);
  84. }
  85. }
  86. }
  87. @Override
  88. public void afterTextChanged(Editable s) {
  89. }
  90. @Override
  91. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  92. }
  93. @Override
  94. public void onTextChanged(CharSequence s, int start, int before, int count) {
  95. if (hasFlag(FLAG_HELLO_READY) && !hasFlag(FLAG_HELLO_DONE)) {
  96. String text = s.toString().toLowerCase();
  97. if (text.contains(mTextHello)) {
  98. setFlag(FLAG_HELLO_DONE, true);
  99. addInstruction(R.string.tutorial_3_hello_ok);
  100. }
  101. }
  102. }
  103. @Override
  104. public void onKeyboardBroadcast(String action, Bundle extras) {
  105. if (LatinIME.BROADCAST_LEFT_KEYBOARD_AREA.equals(action)) {
  106. if (hasFlag(FLAG_HELLO_DONE) && !hasFlag(FLAG_LEFT_KEYBOARD)) {
  107. setFlag(FLAG_LEFT_KEYBOARD, true);
  108. addInstruction(R.string.tutorial_3_read_back);
  109. }
  110. } else if (LatinIME.BROADCAST_UP_OUTSIDE_KEYBOARD.equals(action)) {
  111. if (hasFlag(FLAG_LEFT_KEYBOARD) && !hasFlag(FLAG_TYPED_NOTHING)) {
  112. setFlag(FLAG_TYPED_NOTHING, true);
  113. addInstruction(R.string.tutorial_3_finger_lifted);
  114. }
  115. }
  116. }
  117. @Override
  118. public void onKeyboardModeUpdated(int mode) {
  119. // Do nothing.
  120. }
  121. @Override
  122. public void onKeyboardModeChanged(int mode) {
  123. switch (mode) {
  124. case LatinIME.FORCE_HIDDEN:
  125. if (hasFlag(FLAG_STARTED) && !hasFlag(FLAG_TYPING_MODE)) {
  126. addInstruction(R.string.tutorial_3_wrong_mode);
  127. } else if (hasFlag(FLAG_OUTSIDE) && !hasFlag(FLAG_HIDDEN_MODE)) {
  128. setFlag(FLAG_HIDDEN_MODE, true);
  129. addInstruction(R.string.tutorial_3_mode_hidden);
  130. }
  131. break;
  132. case LatinIME.FORCE_DPAD:
  133. if (hasFlag(FLAG_HIDDEN_MODE) && !hasFlag(FLAG_NAVIGATING_MODE)) {
  134. setFlag(FLAG_NAVIGATING_MODE, true);
  135. addInstruction(R.string.tutorial_3_mode_navigating);
  136. }
  137. break;
  138. case LatinIME.FORCE_CACHED:
  139. if (hasFlag(FLAG_STARTED) && !hasFlag(FLAG_TYPING_MODE)) {
  140. setFlag(FLAG_TYPING_MODE, true);
  141. addInstruction(R.string.tutorial_3_message_3);
  142. }
  143. break;
  144. }
  145. }
  146. }