Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2013 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 
     17 package com.android.inputmethod.keyboard.internal;
     18 
     19 public final class TypingTimeRecorder {
     20     private final int mStaticTimeThresholdAfterFastTyping; // msec
     21     private final int mSuppressKeyPreviewAfterBatchInputDuration;
     22     private long mLastTypingTime;
     23     private long mLastLetterTypingTime;
     24     private long mLastBatchInputTime;
     25 
     26     public TypingTimeRecorder(final int staticTimeThresholdAfterFastTyping,
     27             final int suppressKeyPreviewAfterBatchInputDuration) {
     28         mStaticTimeThresholdAfterFastTyping = staticTimeThresholdAfterFastTyping;
     29         mSuppressKeyPreviewAfterBatchInputDuration = suppressKeyPreviewAfterBatchInputDuration;
     30     }
     31 
     32     public boolean isInFastTyping(final long eventTime) {
     33         final long elapsedTimeSinceLastLetterTyping = eventTime - mLastLetterTypingTime;
     34         return elapsedTimeSinceLastLetterTyping < mStaticTimeThresholdAfterFastTyping;
     35     }
     36 
     37     private boolean wasLastInputTyping() {
     38         return mLastTypingTime >= mLastBatchInputTime;
     39     }
     40 
     41     public void onCodeInput(final int code, final long eventTime) {
     42         // Record the letter typing time when
     43         // 1. Letter keys are typed successively without any batch input in between.
     44         // 2. A letter key is typed within the threshold time since the last any key typing.
     45         // 3. A non-letter key is typed within the threshold time since the last letter key typing.
     46         if (Character.isLetter(code)) {
     47             if (wasLastInputTyping()
     48                     || eventTime - mLastTypingTime < mStaticTimeThresholdAfterFastTyping) {
     49                 mLastLetterTypingTime = eventTime;
     50             }
     51         } else {
     52             if (eventTime - mLastLetterTypingTime < mStaticTimeThresholdAfterFastTyping) {
     53                 // This non-letter typing should be treated as a part of fast typing.
     54                 mLastLetterTypingTime = eventTime;
     55             }
     56         }
     57         mLastTypingTime = eventTime;
     58     }
     59 
     60     public void onEndBatchInput(final long eventTime) {
     61         mLastBatchInputTime = eventTime;
     62     }
     63 
     64     public long getLastLetterTypingTime() {
     65         return mLastLetterTypingTime;
     66     }
     67 
     68     public boolean needsToSuppressKeyPreviewPopup(final long eventTime) {
     69         return !wasLastInputTyping()
     70                 && eventTime - mLastBatchInputTime < mSuppressKeyPreviewAfterBatchInputDuration;
     71     }
     72 }
     73