Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2008-2009 Google Inc.
      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.latin;
     18 
     19 import android.content.Context;
     20 import android.text.format.DateFormat;
     21 import android.util.Log;
     22 
     23 import android.inputmethodservice.Keyboard.Key;
     24 
     25 import java.io.FileOutputStream;
     26 import java.io.IOException;
     27 import java.util.Calendar;
     28 
     29 public class TextEntryState {
     30 
     31     private static boolean LOGGING = false;
     32 
     33     private static int sBackspaceCount = 0;
     34 
     35     private static int sAutoSuggestCount = 0;
     36 
     37     private static int sAutoSuggestUndoneCount = 0;
     38 
     39     private static int sManualSuggestCount = 0;
     40 
     41     private static int sWordNotInDictionaryCount = 0;
     42 
     43     private static int sSessionCount = 0;
     44 
     45     private static int sTypedChars;
     46 
     47     private static int sActualChars;
     48 
     49     private static final String[] STATES = {
     50         "Unknown",
     51         "Start",
     52         "In word",
     53         "Accepted default",
     54         "Picked suggestion",
     55         "Punc. after word",
     56         "Punc. after accepted",
     57         "Space after accepted",
     58         "Space after picked",
     59         "Undo commit"
     60     };
     61 
     62     public static final int STATE_UNKNOWN = 0;
     63     public static final int STATE_START = 1;
     64     public static final int STATE_IN_WORD = 2;
     65     public static final int STATE_ACCEPTED_DEFAULT = 3;
     66     public static final int STATE_PICKED_SUGGESTION = 4;
     67     public static final int STATE_PUNCTUATION_AFTER_WORD = 5;
     68     public static final int STATE_PUNCTUATION_AFTER_ACCEPTED = 6;
     69     public static final int STATE_SPACE_AFTER_ACCEPTED = 7;
     70     public static final int STATE_SPACE_AFTER_PICKED = 8;
     71     public static final int STATE_UNDO_COMMIT = 9;
     72 
     73     private static int sState = STATE_UNKNOWN;
     74 
     75     private static FileOutputStream sKeyLocationFile;
     76     private static FileOutputStream sUserActionFile;
     77 
     78     public static void newSession(Context context) {
     79         sSessionCount++;
     80         sAutoSuggestCount = 0;
     81         sBackspaceCount = 0;
     82         sAutoSuggestUndoneCount = 0;
     83         sManualSuggestCount = 0;
     84         sWordNotInDictionaryCount = 0;
     85         sTypedChars = 0;
     86         sActualChars = 0;
     87         sState = STATE_START;
     88 
     89         if (LOGGING) {
     90             try {
     91                 sKeyLocationFile = context.openFileOutput("key.txt", Context.MODE_APPEND);
     92                 sUserActionFile = context.openFileOutput("action.txt", Context.MODE_APPEND);
     93             } catch (IOException ioe) {
     94                 Log.e("TextEntryState", "Couldn't open file for output: " + ioe);
     95             }
     96         }
     97     }
     98 
     99     public static void endSession() {
    100         if (sKeyLocationFile == null) {
    101             return;
    102         }
    103         try {
    104             sKeyLocationFile.close();
    105             // Write to log file
    106             // Write timestamp, settings,
    107             String out = DateFormat.format("MM:dd hh:mm:ss", Calendar.getInstance().getTime())
    108                     .toString()
    109                     + " BS: " + sBackspaceCount
    110                     + " auto: " + sAutoSuggestCount
    111                     + " manual: " + sManualSuggestCount
    112                     + " typed: " + sWordNotInDictionaryCount
    113                     + " undone: " + sAutoSuggestUndoneCount
    114                     + " saved: " + ((float) (sActualChars - sTypedChars) / sActualChars)
    115                     + "\n";
    116             sUserActionFile.write(out.getBytes());
    117             sUserActionFile.close();
    118             sKeyLocationFile = null;
    119             sUserActionFile = null;
    120         } catch (IOException ioe) {
    121 
    122         }
    123     }
    124 
    125     public static void acceptedDefault(CharSequence typedWord, CharSequence actualWord) {
    126         if (typedWord == null) return;
    127         if (!typedWord.equals(actualWord)) {
    128             sAutoSuggestCount++;
    129         }
    130         sTypedChars += typedWord.length();
    131         sActualChars += actualWord.length();
    132         sState = STATE_ACCEPTED_DEFAULT;
    133     }
    134 
    135     public static void acceptedTyped(CharSequence typedWord) {
    136         sWordNotInDictionaryCount++;
    137         sState = STATE_PICKED_SUGGESTION;
    138     }
    139 
    140     public static void acceptedSuggestion(CharSequence typedWord, CharSequence actualWord) {
    141         sManualSuggestCount++;
    142         if (typedWord.equals(actualWord)) {
    143             acceptedTyped(typedWord);
    144         }
    145         sState = STATE_PICKED_SUGGESTION;
    146     }
    147 
    148     public static void typedCharacter(char c, boolean isSeparator) {
    149         boolean isSpace = c == ' ';
    150         switch (sState) {
    151             case STATE_IN_WORD:
    152                 if (isSpace || isSeparator) {
    153                     sState = STATE_START;
    154                 } else {
    155                     // State hasn't changed.
    156                 }
    157                 break;
    158             case STATE_ACCEPTED_DEFAULT:
    159             case STATE_SPACE_AFTER_PICKED:
    160                 if (isSpace) {
    161                     sState = STATE_SPACE_AFTER_ACCEPTED;
    162                 } else if (isSeparator) {
    163                     sState = STATE_PUNCTUATION_AFTER_ACCEPTED;
    164                 } else {
    165                     sState = STATE_IN_WORD;
    166                 }
    167                 break;
    168             case STATE_PICKED_SUGGESTION:
    169                 if (isSpace) {
    170                     sState = STATE_SPACE_AFTER_PICKED;
    171                 } else if (isSeparator) {
    172                     // Swap
    173                     sState = STATE_PUNCTUATION_AFTER_ACCEPTED;
    174                 } else {
    175                     sState = STATE_IN_WORD;
    176                 }
    177                 break;
    178             case STATE_START:
    179             case STATE_UNKNOWN:
    180             case STATE_SPACE_AFTER_ACCEPTED:
    181             case STATE_PUNCTUATION_AFTER_ACCEPTED:
    182             case STATE_PUNCTUATION_AFTER_WORD:
    183                 if (!isSpace && !isSeparator) {
    184                     sState = STATE_IN_WORD;
    185                 } else {
    186                     sState = STATE_START;
    187                 }
    188                 break;
    189             case STATE_UNDO_COMMIT:
    190                 if (isSpace || isSeparator) {
    191                     sState = STATE_ACCEPTED_DEFAULT;
    192                 } else {
    193                     sState = STATE_IN_WORD;
    194                 }
    195         }
    196     }
    197 
    198     public static void backspace() {
    199         if (sState == STATE_ACCEPTED_DEFAULT) {
    200             sState = STATE_UNDO_COMMIT;
    201             sAutoSuggestUndoneCount++;
    202         } else if (sState == STATE_UNDO_COMMIT) {
    203             sState = STATE_IN_WORD;
    204         }
    205         sBackspaceCount++;
    206     }
    207 
    208     public static void reset() {
    209         sState = STATE_START;
    210     }
    211 
    212     public static int getState() {
    213         return sState;
    214     }
    215 
    216     public static void keyPressedAt(Key key, int x, int y) {
    217         if (LOGGING && sKeyLocationFile != null && key.codes[0] >= 32) {
    218             String out =
    219                     "KEY: " + (char) key.codes[0]
    220                     + " X: " + x
    221                     + " Y: " + y
    222                     + " MX: " + (key.x + key.width / 2)
    223                     + " MY: " + (key.y + key.height / 2)
    224                     + "\n";
    225             try {
    226                 sKeyLocationFile.write(out.getBytes());
    227             } catch (IOException ioe) {
    228                 // TODO: May run out of space
    229             }
    230         }
    231     }
    232 }
    233 
    234