1/*
2 * Copyright (C) 2010 The Android Open Source Project
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.android.inputmethod.keyboard.internal;
18
19import android.util.Log;
20
21public final class AlphabetShiftState {
22    private static final String TAG = AlphabetShiftState.class.getSimpleName();
23    private static final boolean DEBUG = false;
24
25    private static final int UNSHIFTED = 0;
26    private static final int MANUAL_SHIFTED = 1;
27    private static final int MANUAL_SHIFTED_FROM_AUTO = 2;
28    private static final int AUTOMATIC_SHIFTED = 3;
29    private static final int SHIFT_LOCKED = 4;
30    private static final int SHIFT_LOCK_SHIFTED = 5;
31
32    private int mState = UNSHIFTED;
33
34    public void setShifted(boolean newShiftState) {
35        final int oldState = mState;
36        if (newShiftState) {
37            switch (oldState) {
38            case UNSHIFTED:
39                mState = MANUAL_SHIFTED;
40                break;
41            case AUTOMATIC_SHIFTED:
42                mState = MANUAL_SHIFTED_FROM_AUTO;
43                break;
44            case SHIFT_LOCKED:
45                mState = SHIFT_LOCK_SHIFTED;
46                break;
47            }
48        } else {
49            switch (oldState) {
50            case MANUAL_SHIFTED:
51            case MANUAL_SHIFTED_FROM_AUTO:
52            case AUTOMATIC_SHIFTED:
53                mState = UNSHIFTED;
54                break;
55            case SHIFT_LOCK_SHIFTED:
56                mState = SHIFT_LOCKED;
57                break;
58            }
59        }
60        if (DEBUG)
61            Log.d(TAG, "setShifted(" + newShiftState + "): " + toString(oldState) + " > " + this);
62    }
63
64    public void setShiftLocked(boolean newShiftLockState) {
65        final int oldState = mState;
66        if (newShiftLockState) {
67            switch (oldState) {
68            case UNSHIFTED:
69            case MANUAL_SHIFTED:
70            case MANUAL_SHIFTED_FROM_AUTO:
71            case AUTOMATIC_SHIFTED:
72                mState = SHIFT_LOCKED;
73                break;
74            }
75        } else {
76            mState = UNSHIFTED;
77        }
78        if (DEBUG)
79            Log.d(TAG, "setShiftLocked(" + newShiftLockState + "): " + toString(oldState)
80                    + " > " + this);
81    }
82
83    public void setAutomaticShifted() {
84        final int oldState = mState;
85        mState = AUTOMATIC_SHIFTED;
86        if (DEBUG)
87            Log.d(TAG, "setAutomaticShifted: " + toString(oldState) + " > " + this);
88    }
89
90    public boolean isShiftedOrShiftLocked() {
91        return mState != UNSHIFTED;
92    }
93
94    public boolean isShiftLocked() {
95        return mState == SHIFT_LOCKED || mState == SHIFT_LOCK_SHIFTED;
96    }
97
98    public boolean isShiftLockShifted() {
99        return mState == SHIFT_LOCK_SHIFTED;
100    }
101
102    public boolean isAutomaticShifted() {
103        return mState == AUTOMATIC_SHIFTED;
104    }
105
106    public boolean isManualShifted() {
107        return mState == MANUAL_SHIFTED || mState == MANUAL_SHIFTED_FROM_AUTO
108                || mState == SHIFT_LOCK_SHIFTED;
109    }
110
111    public boolean isManualShiftedFromAutomaticShifted() {
112        return mState == MANUAL_SHIFTED_FROM_AUTO;
113    }
114
115    @Override
116    public String toString() {
117        return toString(mState);
118    }
119
120    private static String toString(int state) {
121        switch (state) {
122        case UNSHIFTED: return "UNSHIFTED";
123        case MANUAL_SHIFTED: return "MANUAL_SHIFTED";
124        case MANUAL_SHIFTED_FROM_AUTO: return "MANUAL_SHIFTED_FROM_AUTO";
125        case AUTOMATIC_SHIFTED: return "AUTOMATIC_SHIFTED";
126        case SHIFT_LOCKED: return "SHIFT_LOCKED";
127        case SHIFT_LOCK_SHIFTED: return "SHIFT_LOCK_SHIFTED";
128        default: return "UNKNOWN";
129        }
130    }
131}
132