Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.inputmethod.keyboard.internal;
     18 
     19 import android.util.Log;
     20 
     21 import com.android.inputmethod.keyboard.KeyboardSwitcher;
     22 
     23 public class ModifierKeyState {
     24     protected static final String TAG = "ModifierKeyState";
     25     protected static final boolean DEBUG = KeyboardSwitcher.DEBUG_STATE;
     26 
     27     protected static final int RELEASING = 0;
     28     protected static final int PRESSING = 1;
     29     protected static final int MOMENTARY = 2;
     30 
     31     protected final String mName;
     32     protected int mState = RELEASING;
     33 
     34     public ModifierKeyState(String name) {
     35         mName = name;
     36     }
     37 
     38     public void onPress() {
     39         final int oldState = mState;
     40         mState = PRESSING;
     41         if (DEBUG)
     42             Log.d(TAG, mName + ".onPress: " + toString(oldState) + " > " + this);
     43     }
     44 
     45     public void onRelease() {
     46         final int oldState = mState;
     47         mState = RELEASING;
     48         if (DEBUG)
     49             Log.d(TAG, mName + ".onRelease: " + toString(oldState) + " > " + this);
     50     }
     51 
     52     public void onOtherKeyPressed() {
     53         final int oldState = mState;
     54         if (oldState == PRESSING)
     55             mState = MOMENTARY;
     56         if (DEBUG)
     57             Log.d(TAG, mName + ".onOtherKeyPressed: " + toString(oldState) + " > " + this);
     58     }
     59 
     60     public boolean isPressing() {
     61         return mState == PRESSING;
     62     }
     63 
     64     public boolean isReleasing() {
     65         return mState == RELEASING;
     66     }
     67 
     68     public boolean isMomentary() {
     69         return mState == MOMENTARY;
     70     }
     71 
     72     @Override
     73     public String toString() {
     74         return toString(mState);
     75     }
     76 
     77     protected String toString(int state) {
     78         switch (state) {
     79         case RELEASING: return "RELEASING";
     80         case PRESSING: return "PRESSING";
     81         case MOMENTARY: return "MOMENTARY";
     82         default: return "UNKNOWN";
     83         }
     84     }
     85 }
     86