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 private boolean mIsInModalMode; 44 45 public MoreSuggestionsView(final Context context, final AttributeSet attrs) { 46 this(context, attrs, R.attr.moreKeysKeyboardViewStyle); 47 } 48 49 public MoreSuggestionsView(final Context context, final AttributeSet attrs, 50 final int defStyle) { 51 super(context, attrs, defStyle); 52 } 53 54 // TODO: Remove redundant override method. 55 @Override 56 public void setKeyboard(final Keyboard keyboard) { 57 super.setKeyboard(keyboard); 58 mIsInModalMode = false; 59 // With accessibility mode off, {@link #mAccessibilityDelegate} is set to null at the 60 // above {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. 61 // With accessibility mode on, {@link #mAccessibilityDelegate} is set to a 62 // {@link MoreKeysKeyboardAccessibilityDelegate} object at the above 63 // {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. 64 if (mAccessibilityDelegate != null) { 65 mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions); 66 mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions); 67 } 68 } 69 70 @Override 71 protected int getDefaultCoordX() { 72 final MoreSuggestions pane = (MoreSuggestions)getKeyboard(); 73 return pane.mOccupiedWidth / 2; 74 } 75 76 public void updateKeyboardGeometry(final int keyHeight) { 77 updateKeyDrawParams(keyHeight); 78 } 79 80 public void setModalMode() { 81 mIsInModalMode = true; 82 // Set vertical correction to zero (Reset more keys keyboard sliding allowance 83 // {@link R#dimen.config_more_keys_keyboard_slide_allowance}). 84 mKeyDetector.setKeyboard(getKeyboard(), -getPaddingLeft(), -getPaddingTop()); 85 } 86 87 public boolean isInModalMode() { 88 return mIsInModalMode; 89 } 90 91 @Override 92 protected void onKeyInput(final Key key, final int x, final int y) { 93 if (!(key instanceof MoreSuggestionKey)) { 94 Log.e(TAG, "Expected key is MoreSuggestionKey, but found " 95 + key.getClass().getName()); 96 return; 97 } 98 final Keyboard keyboard = getKeyboard(); 99 if (!(keyboard instanceof MoreSuggestions)) { 100 Log.e(TAG, "Expected keyboard is MoreSuggestions, but found " 101 + keyboard.getClass().getName()); 102 return; 103 } 104 final SuggestedWords suggestedWords = ((MoreSuggestions)keyboard).mSuggestedWords; 105 final int index = ((MoreSuggestionKey)key).mSuggestedWordIndex; 106 if (index < 0 || index >= suggestedWords.size()) { 107 Log.e(TAG, "Selected suggestion has an illegal index: " + index); 108 return; 109 } 110 if (!(mListener instanceof MoreSuggestionsListener)) { 111 Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found " 112 + mListener.getClass().getName()); 113 return; 114 } 115 ((MoreSuggestionsListener)mListener).onSuggestionSelected(suggestedWords.getInfo(index)); 116 } 117 } 118