PageRenderTime 73ms CodeModel.GetById 39ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

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