aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/anysoftkeyboard/KeyboardUIStateHandler.java
blob: 6c37ba6434ffefc199a41ca466b9e7bafb57ca8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.anysoftkeyboard;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputConnection;
import com.menny.android.anysoftkeyboard.R;

import java.lang.ref.WeakReference;

/**
 * handles all kind of UI thread related operations.
*/
final class KeyboardUIStateHandler extends Handler {
    public static final int MSG_UPDATE_SUGGESTIONS = R.id.keyboard_ui_handler_MSG_UPDATE_SUGGESTIONS;
    public static final int MSG_RESTART_NEW_WORD_SUGGESTIONS = R.id.keyboard_ui_handler_MSG_RESTART_NEW_WORD_SUGGESTIONS;
    public static final int MSG_REMOVE_CLOSE_SUGGESTIONS_HINT = R.id.keyboard_ui_handler_MSG_REMOVE_CLOSE_SUGGESTIONS_HINT;
    public static final int MSG_CLOSE_DICTIONARIES = R.id.keyboard_ui_handler_MSG_CLOSE_DICTIONARIES;

    private static final class CloseTextAnimationListener implements Animation.AnimationListener {
        private View closeText;

        public void setCloseText(View c) {
            closeText = c;
        }

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationRepeat(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            closeText.setVisibility(View.GONE);
            closeText = null;
        }
    }

    private final CloseTextAnimationListener mCloseTextAnimationListener = new CloseTextAnimationListener();
    private final WeakReference<AnySoftKeyboard> mKeyboard;

    public KeyboardUIStateHandler(AnySoftKeyboard keyboard) {
        mKeyboard = new WeakReference<>(keyboard);
    }

    public void removeAllMessages() {
        removeMessages(MSG_UPDATE_SUGGESTIONS);
        removeMessages(MSG_RESTART_NEW_WORD_SUGGESTIONS);
        removeMessages(MSG_REMOVE_CLOSE_SUGGESTIONS_HINT);
        removeMessages(MSG_CLOSE_DICTIONARIES);
    }

    @Override
    public void handleMessage(Message msg) {
        AnySoftKeyboard ask = mKeyboard.get();
        if (ask == null)// delayed posts and such may result in the reference gone
            return;
        final InputConnection ic = ask.getCurrentInputConnection();

        switch (msg.what) {
            case MSG_UPDATE_SUGGESTIONS:
                ask.performUpdateSuggestions();
                break;
            case MSG_RESTART_NEW_WORD_SUGGESTIONS:
                ask.performRestartWordSuggestion(ic);
                break;
            case MSG_REMOVE_CLOSE_SUGGESTIONS_HINT:
                final View closeText = ask.mCandidateCloseText;
                if (closeText != null) {// in API3, this variable is null
                    mCloseTextAnimationListener.setCloseText(closeText);
                    Animation gone = AnimationUtils.loadAnimation(ask.getApplicationContext(), R.anim.close_candidates_hint_out);
                    gone.setAnimationListener(mCloseTextAnimationListener);
                    closeText.startAnimation(gone);
                }
                break;
            case MSG_CLOSE_DICTIONARIES:
                ask.closeDictionaries();
                break;
            default:
                super.handleMessage(msg);
        }
    }
}