Home | History | Annotate | Download | only in keyboard
      1 /*
      2  * Copyright (C) 2015 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;
     18 
     19 import com.android.inputmethod.annotations.UsedForTesting;
     20 
     21 import java.util.ArrayList;
     22 import java.util.List;
     23 
     24 import javax.annotation.Nonnull;
     25 
     26 /**
     27  * KeyboardLayout maintains the keyboard layout information.
     28  */
     29 public class KeyboardLayout {
     30 
     31     private final int[] mKeyCodes;
     32 
     33     private final int[] mKeyXCoordinates;
     34     private final int[] mKeyYCoordinates;
     35 
     36     private final int[] mKeyWidths;
     37     private final int[] mKeyHeights;
     38 
     39     public final int mMostCommonKeyWidth;
     40     public final int mMostCommonKeyHeight;
     41 
     42     public final int mKeyboardWidth;
     43     public final int mKeyboardHeight;
     44 
     45     public KeyboardLayout(ArrayList<Key> layoutKeys, int mostCommonKeyWidth,
     46             int mostCommonKeyHeight, int keyboardWidth, int keyboardHeight) {
     47         mMostCommonKeyWidth = mostCommonKeyWidth;
     48         mMostCommonKeyHeight = mostCommonKeyHeight;
     49         mKeyboardWidth = keyboardWidth;
     50         mKeyboardHeight = keyboardHeight;
     51 
     52         mKeyCodes = new int[layoutKeys.size()];
     53         mKeyXCoordinates = new int[layoutKeys.size()];
     54         mKeyYCoordinates = new int[layoutKeys.size()];
     55         mKeyWidths = new int[layoutKeys.size()];
     56         mKeyHeights = new int[layoutKeys.size()];
     57 
     58         for (int i = 0; i < layoutKeys.size(); i++) {
     59             Key key = layoutKeys.get(i);
     60             mKeyCodes[i] = Character.toLowerCase(key.getCode());
     61             mKeyXCoordinates[i] = key.getX();
     62             mKeyYCoordinates[i] = key.getY();
     63             mKeyWidths[i] = key.getWidth();
     64             mKeyHeights[i] = key.getHeight();
     65         }
     66     }
     67 
     68     @UsedForTesting
     69     public int[] getKeyCodes() {
     70         return mKeyCodes;
     71     }
     72 
     73     /**
     74      * The x-coordinate for the top-left corner of the keys.
     75      *
     76      */
     77     public int[] getKeyXCoordinates() {
     78         return mKeyXCoordinates;
     79     }
     80 
     81     /**
     82      * The y-coordinate for the top-left corner of the keys.
     83      */
     84     public int[] getKeyYCoordinates() {
     85         return mKeyYCoordinates;
     86     }
     87 
     88     /**
     89      * The widths of the keys which are smaller than the true hit-area due to the gaps
     90      * between keys. The mostCommonKey(Width/Height) represents the true key width/height
     91      * including the gaps.
     92      */
     93     public int[] getKeyWidths() {
     94         return mKeyWidths;
     95     }
     96 
     97     /**
     98      * The heights of the keys which are smaller than the true hit-area due to the gaps
     99      * between keys. The mostCommonKey(Width/Height) represents the true key width/height
    100      * including the gaps.
    101      */
    102     public int[] getKeyHeights() {
    103         return mKeyHeights;
    104     }
    105 
    106     /**
    107      * Factory method to create {@link KeyboardLayout} objects.
    108      */
    109     public static KeyboardLayout newKeyboardLayout(@Nonnull final List<Key> sortedKeys,
    110             int mostCommonKeyWidth, int mostCommonKeyHeight,
    111             int occupiedWidth, int occupiedHeight) {
    112         final ArrayList<Key> layoutKeys = new ArrayList<Key>();
    113         for (final Key key : sortedKeys) {
    114             if (!ProximityInfo.needsProximityInfo(key)) {
    115                 continue;
    116             }
    117             if (key.getCode() != ',') {
    118                 layoutKeys.add(key);
    119             }
    120         }
    121         return new KeyboardLayout(layoutKeys, mostCommonKeyWidth,
    122                 mostCommonKeyHeight, occupiedWidth, occupiedHeight);
    123     }
    124 }
    125