PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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