Home | History | Annotate | Download | only in suggestions
      1 /*
      2  * Copyright (C) 2011 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.latin.suggestions;
     18 
     19 import android.content.Context;
     20 import android.util.AttributeSet;
     21 import android.util.Log;
     22 
     23 import com.android.inputmethod.keyboard.Keyboard;
     24 import com.android.inputmethod.keyboard.MoreKeysKeyboardView;
     25 import com.android.inputmethod.latin.R;
     26 import com.android.inputmethod.latin.SuggestedWords;
     27 import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionsListener;
     28 
     29 /**
     30  * A view that renders a virtual {@link MoreSuggestions}. It handles rendering of keys and detecting
     31  * key presses and touch movements.
     32  */
     33 public final class MoreSuggestionsView extends MoreKeysKeyboardView {
     34     private static final String TAG = MoreSuggestionsView.class.getSimpleName();
     35 
     36     public MoreSuggestionsView(final Context context, final AttributeSet attrs) {
     37         this(context, attrs, R.attr.moreSuggestionsViewStyle);
     38     }
     39 
     40     public MoreSuggestionsView(final Context context, final AttributeSet attrs,
     41             final int defStyle) {
     42         super(context, attrs, defStyle);
     43     }
     44 
     45     @Override
     46     protected int getDefaultCoordX() {
     47         final MoreSuggestions pane = (MoreSuggestions)getKeyboard();
     48         return pane.mOccupiedWidth / 2;
     49     }
     50 
     51     public void updateKeyboardGeometry(final int keyHeight) {
     52         updateKeyDrawParams(keyHeight);
     53     }
     54 
     55     public void adjustVerticalCorrectionForModalMode() {
     56         // Set vertical correction to zero (Reset more keys keyboard sliding allowance
     57         // {@link R#dimen.more_keys_keyboard_slide_allowance}).
     58         mKeyDetector.setKeyboard(getKeyboard(), -getPaddingLeft(), -getPaddingTop());
     59     }
     60 
     61     @Override
     62     public void onCodeInput(final int code, final int x, final int y) {
     63         final Keyboard keyboard = getKeyboard();
     64         if (!(keyboard instanceof MoreSuggestions)) {
     65             Log.e(TAG, "Expected keyboard is MoreSuggestions, but found "
     66                     + keyboard.getClass().getName());
     67             return;
     68         }
     69         final SuggestedWords suggestedWords = ((MoreSuggestions)keyboard).mSuggestedWords;
     70         final int index = code - MoreSuggestions.SUGGESTION_CODE_BASE;
     71         if (index < 0 || index >= suggestedWords.size()) {
     72             Log.e(TAG, "Selected suggestion has an illegal index: " + index);
     73             return;
     74         }
     75         if (!(mListener instanceof MoreSuggestionsListener)) {
     76             Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found "
     77                     + mListener.getClass().getName());
     78             return;
     79         }
     80         ((MoreSuggestionsListener)mListener).onSuggestionSelected(
     81                 index, suggestedWords.getInfo(index));
     82     }
     83 }
     84