From 71c353aa875f5237b1dce4e18bd4fe86ce28b58e Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Sat, 13 Nov 2010 00:46:45 -0800
Subject: [PATCH] Refactor Tutorial class

Change-Id: Ib8dbcf8f36966988fb0d4efdc6bdb7e30b776a68
---
 .../android/inputmethod/latin/LatinIME.java   |  34 +++---
 .../android/inputmethod/latin/Tutorial.java   | 112 +++++++++---------
 2 files changed, 75 insertions(+), 71 deletions(-)

diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index cd40dd307a..4614df9884 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -82,7 +82,8 @@ import java.util.Map;
 public class LatinIME extends InputMethodService
         implements BaseKeyboardView.OnKeyboardActionListener,
         VoiceInput.UiListener,
-        SharedPreferences.OnSharedPreferenceChangeListener {
+        SharedPreferences.OnSharedPreferenceChangeListener,
+        Tutorial.TutorialListener {
     private static final String TAG = "LatinIME";
     private static final boolean PERF_DEBUG = false;
     static final boolean DEBUG = false;
@@ -162,7 +163,7 @@ public class LatinIME extends InputMethodService
     private AlertDialog mOptionsDialog;
     private AlertDialog mVoiceWarningDialog;
 
-    /* package */ KeyboardSwitcher mKeyboardSwitcher;
+    private KeyboardSwitcher mKeyboardSwitcher;
 
     private UserDictionary mUserDictionary;
     private UserBigramDictionary mUserBigramDictionary;
@@ -177,7 +178,7 @@ public class LatinIME extends InputMethodService
     private String mSystemLocale;
     private LanguageSwitcher mLanguageSwitcher;
 
-    private StringBuilder mComposing = new StringBuilder();
+    private final StringBuilder mComposing = new StringBuilder();
     private WordComposer mWord = new WordComposer();
     private int mCommittedLength;
     private boolean mPredicting;
@@ -226,21 +227,21 @@ public class LatinIME extends InputMethodService
     private long mLastKeyTime;
 
     // Modifier keys state
-    private ModifierKeyState mShiftKeyState = new ModifierKeyState();
-    private ModifierKeyState mSymbolKeyState = new ModifierKeyState();
+    private final ModifierKeyState mShiftKeyState = new ModifierKeyState();
+    private final ModifierKeyState mSymbolKeyState = new ModifierKeyState();
 
     private Tutorial mTutorial;
 
     private AudioManager mAudioManager;
     // Align sound effect volume on music volume
-    private final float FX_VOLUME = -1.0f;
+    private static final float FX_VOLUME = -1.0f;
     private boolean mSilentMode;
 
     /* package */ String mWordSeparators;
     private String mSentenceSeparators;
     private String mSuggestPuncs;
     private VoiceInput mVoiceInput;
-    private VoiceResults mVoiceResults = new VoiceResults();
+    private final VoiceResults mVoiceResults = new VoiceResults();
     private boolean mConfigurationChanging;
 
     // Keeps track of most recently inserted text (multi-character key) for reverting
@@ -248,10 +249,10 @@ public class LatinIME extends InputMethodService
     private boolean mRefreshKeyboardRequired;
 
     // For each word, a list of potential replacements, usually from voice.
-    private Map<String, List<CharSequence>> mWordToSuggestions =
+    private final Map<String, List<CharSequence>> mWordToSuggestions =
             new HashMap<String, List<CharSequence>>();
 
-    private ArrayList<WordAlternatives> mWordHistory = new ArrayList<WordAlternatives>();
+    private final ArrayList<WordAlternatives> mWordHistory = new ArrayList<WordAlternatives>();
 
     private class VoiceResults {
         List<String> candidates;
@@ -319,8 +320,7 @@ public class LatinIME extends InputMethodService
                 case MSG_START_TUTORIAL:
                     if (mTutorial == null) {
                         if (mKeyboardSwitcher.isInputViewShown()) {
-                            mTutorial = new Tutorial(
-                                    LatinIME.this, mKeyboardSwitcher.getInputView());
+                            mTutorial = new Tutorial(LatinIME.this, mKeyboardSwitcher);
                             mTutorial.start();
                         } else {
                             // Try again soon if the view is not yet showing
@@ -395,7 +395,7 @@ public class LatinIME extends InputMethodService
      * Loads a dictionary or multiple separated dictionary
      * @return returns array of dictionary resource ids
      */
-    /* package */ static int[] getDictionary(Resources res) {
+    public static int[] getDictionary(Resources res) {
         String packageName = LatinIME.class.getPackage().getName();
         XmlResourceParser xrp = res.getXml(R.xml.dictionary);
         ArrayList<Integer> dictionaries = new ArrayList<Integer>();
@@ -2430,20 +2430,22 @@ public class LatinIME extends InputMethodService
         mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_TUTORIAL), 500);
     }
 
-    /* package */ void tutorialDone() {
+    // Tutorial.TutorialListener
+    public void onTutorialDone() {
+        sendDownUpKeyEvents(-1); // Inform the setupwizard that tutorial is in last bubble
         mTutorial = null;
     }
 
-    /* package */ void promoteToUserDictionary(String word, int frequency) {
+    public void promoteToUserDictionary(String word, int frequency) {
         if (mUserDictionary.isValidWord(word)) return;
         mUserDictionary.addWord(word, frequency);
     }
 
-    /* package */ WordComposer getCurrentWord() {
+    public WordComposer getCurrentWord() {
         return mWord;
     }
 
-    /* package */ boolean getPopupOn() {
+    public boolean getPopupOn() {
         return mPopupOn;
     }
 
diff --git a/java/src/com/android/inputmethod/latin/Tutorial.java b/java/src/com/android/inputmethod/latin/Tutorial.java
index d3eaf30c66..f18551494b 100644
--- a/java/src/com/android/inputmethod/latin/Tutorial.java
+++ b/java/src/com/android/inputmethod/latin/Tutorial.java
@@ -32,20 +32,24 @@ import android.widget.PopupWindow;
 import android.widget.TextView;
 
 import java.util.ArrayList;
-import java.util.List;
 
 public class Tutorial implements OnTouchListener {
-    
-    private List<Bubble> mBubbles = new ArrayList<Bubble>();
-    private View mInputView;
-    private LatinIME mIme;
-    private int[] mLocation = new int[2];
-    
+
+    public interface TutorialListener {
+        public void onTutorialDone();
+    }
+
+    private final ArrayList<Bubble> mBubbles = new ArrayList<Bubble>();
+    private final KeyboardSwitcher mKeyboardSwitcher;
+    private final View mInputView;
+    private final TutorialListener mListener;
+    private final int[] mLocation = new int[2];
+
     private static final int MSG_SHOW_BUBBLE = 0;
-    
+
     private int mBubbleIndex;
-    
-    Handler mHandler = new Handler() {
+
+    private final Handler mHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
@@ -57,20 +61,18 @@ public class Tutorial implements OnTouchListener {
         }
     };
 
-    class Bubble {
-        Drawable bubbleBackground;
-        int x;
-        int y;
-        int width;
-        int gravity;
-        CharSequence text;
-        boolean dismissOnTouch;
-        boolean dismissOnClose;
-        PopupWindow window;
-        TextView textView;
-        View inputView;
-        
-        Bubble(Context context, View inputView,
+    private class Bubble {
+        private final Drawable bubbleBackground;
+        private final int x;
+        private final int y;
+        private final int width;
+        private final int gravity;
+        private final CharSequence text;
+        private final PopupWindow window;
+        private final TextView textView;
+        private final View inputView;
+
+        private Bubble(Context context, View inputView,
                 int backgroundResource, int bx, int by, int textResource1, int textResource2) {
             bubbleBackground = context.getResources().getDrawable(backgroundResource);
             x = bx;
@@ -81,8 +83,6 @@ public class Tutorial implements OnTouchListener {
                 .append(context.getResources().getText(textResource1))
                 .append("\n") 
                 .append(context.getResources().getText(textResource2));
-            this.dismissOnTouch = true;
-            this.dismissOnClose = false;
             this.inputView = inputView;
             window = new PopupWindow(context);
             window.setBackgroundDrawable(null);
@@ -124,7 +124,7 @@ public class Tutorial implements OnTouchListener {
             return l.getHeight();
         }
 
-        void show(int offx, int offy) {
+        private void show(int offx, int offy) {
             int textHeight = chooseSize(window, inputView, text, textView);
             offy -= textView.getPaddingTop() + textHeight;
             if (inputView.getVisibility() == View.VISIBLE 
@@ -144,63 +144,66 @@ public class Tutorial implements OnTouchListener {
                 }
             }
         }
-        
-        void hide() {
+
+        private void hide() {
             if (window.isShowing()) {
                 textView.setOnTouchListener(null);
                 window.dismiss();
             }
         }
-        
-        boolean isShowing() {
+
+        private boolean isShowing() {
             return window.isShowing();
         }
     }
-    
-    public Tutorial(LatinIME ime, LatinKeyboardView inputView) {
+
+    public Tutorial(TutorialListener listener, KeyboardSwitcher keyboardSwitcher) {
+        mListener = listener;
+        mKeyboardSwitcher = keyboardSwitcher;
+        LatinKeyboardView inputView = keyboardSwitcher.getInputView();
+        mInputView = inputView;
         Context context = inputView.getContext();
-        mIme = ime;
         int inputWidth = inputView.getWidth();
         final int x = inputWidth / 20; // Half of 1/10th
+        ArrayList<Bubble> bubbles = mBubbles;
         Bubble bWelcome = new Bubble(context, inputView, 
                 R.drawable.dialog_bubble_step02, x, 0, 
                 R.string.tip_to_open_keyboard, R.string.touch_to_continue);
-        mBubbles.add(bWelcome);
+        bubbles.add(bWelcome);
         Bubble bAccents = new Bubble(context, inputView, 
                 R.drawable.dialog_bubble_step02, x, 0, 
                 R.string.tip_to_view_accents, R.string.touch_to_continue);
-        mBubbles.add(bAccents);
+        bubbles.add(bAccents);
         Bubble b123 = new Bubble(context, inputView, 
                 R.drawable.dialog_bubble_step07, x, 0, 
                 R.string.tip_to_open_symbols, R.string.touch_to_continue);
-        mBubbles.add(b123);
+        bubbles.add(b123);
         Bubble bABC = new Bubble(context, inputView, 
                 R.drawable.dialog_bubble_step07, x, 0, 
                 R.string.tip_to_close_symbols, R.string.touch_to_continue);
-        mBubbles.add(bABC);
+        bubbles.add(bABC);
         Bubble bSettings = new Bubble(context, inputView, 
                 R.drawable.dialog_bubble_step07, x, 0, 
                 R.string.tip_to_launch_settings, R.string.touch_to_continue);
-        mBubbles.add(bSettings);
+        bubbles.add(bSettings);
         Bubble bDone = new Bubble(context, inputView, 
                 R.drawable.dialog_bubble_step02, x, 0, 
                 R.string.tip_to_start_typing, R.string.touch_to_finish);
-        mBubbles.add(bDone);
-        mInputView = inputView;
+        bubbles.add(bDone);
     }
-    
-    void start() {
+
+    public void start() {
         mInputView.getLocationInWindow(mLocation);
         mBubbleIndex = -1;
         mInputView.setOnTouchListener(this);
         next();
     }
 
-    boolean next() {
+    private void next() {
         if (mBubbleIndex >= 0) {
             // If the bubble is not yet showing, don't move to the next.
             if (!mBubbles.get(mBubbleIndex).isShowing()) {
-                return true;
+                return;
             }
             // Hide all previous bubbles as well, as they may have had a delayed show
             for (int i = 0; i <= mBubbleIndex; i++) {
@@ -210,26 +213,25 @@ public class Tutorial implements OnTouchListener {
         mBubbleIndex++;
         if (mBubbleIndex >= mBubbles.size()) {
             mInputView.setOnTouchListener(null);
-            mIme.sendDownUpKeyEvents(-1); // Inform the setupwizard that tutorial is in last bubble
-            mIme.tutorialDone();
-            return false;
+            mListener.onTutorialDone();
+            return;
         }
         if (mBubbleIndex == 3 || mBubbleIndex == 4) {
-            mIme.mKeyboardSwitcher.toggleSymbols();
+            mKeyboardSwitcher.toggleSymbols();
         }
         mHandler.sendMessageDelayed(
                 mHandler.obtainMessage(MSG_SHOW_BUBBLE, mBubbles.get(mBubbleIndex)), 500);
-        return true;
+        return;
     }
-    
-    void hide() {
-        for (int i = 0; i < mBubbles.size(); i++) {
-            mBubbles.get(i).hide();
+
+    private void hide() {
+        for (Bubble bubble : mBubbles) {
+            bubble.hide();
         }
         mInputView.setOnTouchListener(null);
     }
 
-    boolean close() {
+    public boolean close() {
         mHandler.removeMessages(MSG_SHOW_BUBBLE);
         hide();
         return true;
-- 
GitLab