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

http://eyes-free.googlecode.com/ · Java · 141 lines · 95 code · 20 blank · 26 comment · 32 complexity · 7889429abda59b600904f75496b0b000 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 com.google.android.marvin.aime.TextNavigation;
  18. import android.content.Context;
  19. import android.os.Bundle;
  20. import android.view.View;
  21. import com.googlecode.eyesfree.inputmethod.latin.LatinIME;
  22. import com.googlecode.eyesfree.inputmethod.latin.R;
  23. /**
  24. * @author alanv@google.com (Alan Viverette)
  25. */
  26. public class LatinTutorialModule4 extends TutorialModule implements View.OnClickListener,
  27. View.OnFocusChangeListener, SelectionEditText.SelectionListener,
  28. LatinIMETutorial.KeyboardBroadcastListener {
  29. private static final int FLAG_STARTED = 0;
  30. private static final int FLAG_CHAR_READY = 1;
  31. private static final int FLAG_CHAR_DONE = 2;
  32. private static final int FLAG_WORD_MODE = 3;
  33. private static final int FLAG_WORD_DONE = 4;
  34. private static final int FLAG_CHAR_MODE = 5;
  35. // Use counts for triggering successful "use" of a feature.
  36. private static final int DPAD_USE_TRIGGER = 3;
  37. private static final int ALT_DPAD_USE_TRIGGER = 2;
  38. /**
  39. * Counter for character-level navigation events in Text Navigation
  40. * tutorial.
  41. */
  42. private int mDpadUseCount = 0;
  43. /** Counter for word-level navigation events in Text Navigation tutorial. */
  44. private int mAltDpadUseCount = 0;
  45. public LatinTutorialModule4(Context context, TutorialController controller) {
  46. super(context, controller, R.layout.tutorial_4_text_navigation);
  47. findViewById(R.id.tutorial_previous).setOnClickListener(this);
  48. findViewById(R.id.tutorial_exit).setOnClickListener(this);
  49. SelectionEditText editText = (SelectionEditText) findViewById(R.id.tutorial_4_edittext);
  50. editText.setOnClickListener(this);
  51. editText.setSelectionListener(this);
  52. editText.setOnFocusChangeListener(this);
  53. }
  54. @Override
  55. public void onShown() {
  56. super.onShown();
  57. if (!hasFlag(FLAG_STARTED)) {
  58. setFlag(FLAG_STARTED, true);
  59. addInstruction(R.string.tutorial_4_message_1);
  60. }
  61. }
  62. @Override
  63. public void onInstructionRead(int resId) {
  64. if (resId == R.string.tutorial_4_message_1) {
  65. setFlag(FLAG_CHAR_READY, true);
  66. addInstruction(R.string.tutorial_4_message_2);
  67. findViewById(R.id.tutorial_4_edittext).requestFocusFromTouch();
  68. findViewById(R.id.tutorial_4_edittext).performClick();
  69. }
  70. }
  71. @Override
  72. public void onClick(View v) {
  73. if (v.getId() == R.id.tutorial_previous) {
  74. getController().previous();
  75. } else if (v.getId() == R.id.tutorial_exit) {
  76. getController().finish();
  77. }
  78. }
  79. @Override
  80. public void onFocusChange(View v, boolean hasFocus) {
  81. if (v.getId() == R.id.tutorial_4_edittext) {
  82. // TODO(alanv): Do focus and selection need to be different
  83. // prompts?
  84. }
  85. }
  86. @Override
  87. public void onSelectionChanged(
  88. SelectionEditText editText, int oldSelStart, int oldSelEnd, int selStart, int selEnd) {
  89. if (editText.getId() == R.id.tutorial_4_edittext) {
  90. int diff = Math.abs((selStart - oldSelStart) + (selEnd - oldSelEnd));
  91. if (diff == 2 && hasFlag(FLAG_CHAR_READY) && !hasFlag(FLAG_CHAR_DONE)) {
  92. mDpadUseCount++;
  93. if (mDpadUseCount >= DPAD_USE_TRIGGER) {
  94. setFlag(FLAG_CHAR_DONE, true);
  95. addInstruction(R.string.tutorial_4_character_ok);
  96. }
  97. } else if (diff > 2 && hasFlag(FLAG_CHAR_DONE) && !hasFlag(FLAG_WORD_DONE)) {
  98. mAltDpadUseCount++;
  99. if (mAltDpadUseCount >= ALT_DPAD_USE_TRIGGER) {
  100. setFlag(FLAG_WORD_DONE, true);
  101. addInstruction(R.string.tutorial_4_word_ok);
  102. }
  103. }
  104. }
  105. }
  106. @Override
  107. public void onKeyboardBroadcast(String action, Bundle extras) {
  108. if (LatinIME.BROADCAST_GRANULARITY_CHANGE.equals(action)) {
  109. int granularity = extras.getInt(LatinIME.EXTRA_GRANULARITY, -1);
  110. if (granularity == TextNavigation.GRANULARITY_WORD && hasFlag(FLAG_CHAR_DONE)
  111. && !hasFlag(FLAG_WORD_MODE)) {
  112. setFlag(FLAG_WORD_MODE, true);
  113. addInstruction(R.string.tutorial_4_word_mode);
  114. } else if (granularity == TextNavigation.GRANULARITY_CHAR && hasFlag(FLAG_WORD_DONE)
  115. && !hasFlag(FLAG_CHAR_MODE)) {
  116. setFlag(FLAG_CHAR_MODE, true);
  117. addInstruction(R.string.tutorial_4_char_mode);
  118. }
  119. }
  120. }
  121. }