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.Key; 24 import com.android.inputmethod.keyboard.Keyboard; 25 import com.android.inputmethod.keyboard.KeyboardActionListener; 26 import com.android.inputmethod.keyboard.MoreKeysKeyboardView; 27 import com.android.inputmethod.latin.R; 28 import com.android.inputmethod.latin.SuggestedWords; 29 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 30 import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionKey; 31 32 /** 33 * A view that renders a virtual {@link MoreSuggestions}. It handles rendering of keys and detecting 34 * key presses and touch movements. 35 */ 36 public final class MoreSuggestionsView extends MoreKeysKeyboardView { 37 private static final String TAG = MoreSuggestionsView.class.getSimpleName(); 38 39 public static abstract class MoreSuggestionsListener extends KeyboardActionListener.Adapter { 40 public abstract void onSuggestionSelected(final SuggestedWordInfo info); 41 } 42 43 public MoreSuggestionsView(final Context context, final AttributeSet attrs) { 44 this(context, attrs, R.attr.moreKeysKeyboardViewStyle); 45 } 46 47 public MoreSuggestionsView(final Context context, final AttributeSet attrs, 48 final int defStyle) { 49 super(context, attrs, defStyle); 50 } 51 52 // TODO: Remove redundant override method. 53 @Override 54 public void setKeyboard(final Keyboard keyboard) { 55 super.setKeyboard(keyboard); 56 // With accessibility mode off, {@link #mAccessibilityDelegate} is set to null at the 57 // above {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. 58 // With accessibility mode on, {@link #mAccessibilityDelegate} is set to a 59 // {@link MoreKeysKeyboardAccessibilityDelegate} object at the above 60 // {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. 61 if (mAccessibilityDelegate != null) { 62 mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions); 63 mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions); 64 } 65 } 66 67 @Override 68 protected int getDefaultCoordX() { 69 final MoreSuggestions pane = (MoreSuggestions)getKeyboard(); 70 return pane.mOccupiedWidth / 2; 71 } 72 73 public void updateKeyboardGeometry(final int keyHeight) { 74 updateKeyDrawParams(keyHeight); 75 } 76 77 public void adjustVerticalCorrectionForModalMode() { 78 // Set vertical correction to zero (Reset more keys keyboard sliding allowance 79 // {@link R#dimen.config_more_keys_keyboard_slide_allowance}). 80 mKeyDetector.setKeyboard(getKeyboard(), -getPaddingLeft(), -getPaddingTop()); 81 } 82 83 @Override 84 protected void onKeyInput(final Key key, final int x, final int y) { 85 if (!(key instanceof MoreSuggestionKey)) { 86 Log.e(TAG, "Expected key is MoreSuggestionKey, but found " 87 + key.getClass().getName()); 88 return; 89 } 90 final Keyboard keyboard = getKeyboard(); 91 if (!(keyboard instanceof MoreSuggestions)) { 92 Log.e(TAG, "Expected keyboard is MoreSuggestions, but found " 93 + keyboard.getClass().getName()); 94 return; 95 } 96 final SuggestedWords suggestedWords = ((MoreSuggestions)keyboard).mSuggestedWords; 97 final int index = ((MoreSuggestionKey)key).mSuggestedWordIndex; 98 if (index < 0 || index >= suggestedWords.size()) { 99 Log.e(TAG, "Selected suggestion has an illegal index: " + index); 100 return; 101 } 102 if (!(mListener instanceof MoreSuggestionsListener)) { 103 Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found " 104 + mListener.getClass().getName()); 105 return; 106 } 107 ((MoreSuggestionsListener)mListener).onSuggestionSelected(suggestedWords.getInfo(index)); 108 } 109 } 110