PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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