MoreKeysKeyboardView.java revision ac78633be28e8990fc3b3a8de192c80966e746e3
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 17package com.android.inputmethod.keyboard; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.util.AttributeSet; 22import android.view.Gravity; 23import android.view.View; 24import android.widget.PopupWindow; 25 26import com.android.inputmethod.keyboard.PointerTracker.DrawingProxy; 27import com.android.inputmethod.keyboard.PointerTracker.TimerProxy; 28import com.android.inputmethod.latin.Constants; 29import com.android.inputmethod.latin.InputPointers; 30import com.android.inputmethod.latin.R; 31 32/** 33 * A view that renders a virtual {@link MoreKeysKeyboard}. It handles rendering of keys and 34 * detecting key presses and touch movements. 35 */ 36public class MoreKeysKeyboardView extends KeyboardView implements MoreKeysPanel { 37 private final int[] mCoordinates = new int[2]; 38 39 private final KeyDetector mKeyDetector; 40 41 private Controller mController; 42 private KeyboardActionListener mListener; 43 private int mOriginX; 44 private int mOriginY; 45 46 private static final TimerProxy EMPTY_TIMER_PROXY = new TimerProxy.Adapter(); 47 48 private final KeyboardActionListener mMoreKeysKeyboardListener = 49 new KeyboardActionListener.Adapter() { 50 @Override 51 public void onCodeInput(int primaryCode, int x, int y) { 52 // Because a more keys keyboard doesn't need proximity characters correction, we don't 53 // send touch event coordinates. 54 mListener.onCodeInput( 55 primaryCode, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); 56 } 57 58 @Override 59 public void onTextInput(CharSequence text) { 60 mListener.onTextInput(text); 61 } 62 63 @Override 64 public void onStartBatchInput() { 65 mListener.onStartBatchInput(); 66 } 67 68 @Override 69 public void onUpdateBatchInput(InputPointers batchPointers) { 70 mListener.onUpdateBatchInput(batchPointers); 71 } 72 73 @Override 74 public void onEndBatchInput(InputPointers batchPointers) { 75 mListener.onEndBatchInput(batchPointers); 76 } 77 78 @Override 79 public void onCancelInput() { 80 mListener.onCancelInput(); 81 } 82 83 @Override 84 public void onPressKey(int primaryCode) { 85 mListener.onPressKey(primaryCode); 86 } 87 88 @Override 89 public void onReleaseKey(int primaryCode, boolean withSliding) { 90 mListener.onReleaseKey(primaryCode, withSliding); 91 } 92 }; 93 94 public MoreKeysKeyboardView(Context context, AttributeSet attrs) { 95 this(context, attrs, R.attr.moreKeysKeyboardViewStyle); 96 } 97 98 public MoreKeysKeyboardView(Context context, AttributeSet attrs, int defStyle) { 99 super(context, attrs, defStyle); 100 101 final Resources res = context.getResources(); 102 mKeyDetector = new MoreKeysDetector( 103 res.getDimension(R.dimen.more_keys_keyboard_slide_allowance)); 104 setKeyPreviewPopupEnabled(false, 0); 105 } 106 107 @Override 108 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 109 final Keyboard keyboard = getKeyboard(); 110 if (keyboard != null) { 111 final int width = keyboard.mOccupiedWidth + getPaddingLeft() + getPaddingRight(); 112 final int height = keyboard.mOccupiedHeight + getPaddingTop() + getPaddingBottom(); 113 setMeasuredDimension(width, height); 114 } else { 115 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 116 } 117 } 118 119 @Override 120 public void setKeyboard(Keyboard keyboard) { 121 super.setKeyboard(keyboard); 122 mKeyDetector.setKeyboard(keyboard, -getPaddingLeft(), 123 -getPaddingTop() + mVerticalCorrection); 124 } 125 126 @Override 127 public KeyDetector getKeyDetector() { 128 return mKeyDetector; 129 } 130 131 @Override 132 public KeyboardActionListener getKeyboardActionListener() { 133 return mMoreKeysKeyboardListener; 134 } 135 136 @Override 137 public DrawingProxy getDrawingProxy() { 138 return this; 139 } 140 141 @Override 142 public TimerProxy getTimerProxy() { 143 return EMPTY_TIMER_PROXY; 144 } 145 146 @Override 147 public void setKeyPreviewPopupEnabled(boolean previewEnabled, int delay) { 148 // More keys keyboard needs no pop-up key preview displayed, so we pass always false with a 149 // delay of 0. The delay does not matter actually since the popup is not shown anyway. 150 super.setKeyPreviewPopupEnabled(false, 0); 151 } 152 153 @Override 154 public void showMoreKeysPanel(View parentView, Controller controller, int pointX, int pointY, 155 PopupWindow window, KeyboardActionListener listener) { 156 mController = controller; 157 mListener = listener; 158 final View container = (View)getParent(); 159 final MoreKeysKeyboard pane = (MoreKeysKeyboard)getKeyboard(); 160 final int defaultCoordX = pane.getDefaultCoordX(); 161 // The coordinates of panel's left-top corner in parentView's coordinate system. 162 final int x = pointX - defaultCoordX - container.getPaddingLeft(); 163 final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom(); 164 165 window.setContentView(container); 166 window.setWidth(container.getMeasuredWidth()); 167 window.setHeight(container.getMeasuredHeight()); 168 parentView.getLocationInWindow(mCoordinates); 169 window.showAtLocation(parentView, Gravity.NO_GRAVITY, 170 x + mCoordinates[0], y + mCoordinates[1]); 171 172 mOriginX = x + container.getPaddingLeft(); 173 mOriginY = y + container.getPaddingTop(); 174 } 175 176 private boolean mIsDismissing; 177 178 @Override 179 public boolean dismissMoreKeysPanel() { 180 if (mIsDismissing || mController == null) return false; 181 mIsDismissing = true; 182 final boolean dismissed = mController.dismissMoreKeysPanel(); 183 mIsDismissing = false; 184 return dismissed; 185 } 186 187 @Override 188 public int translateX(int x) { 189 return x - mOriginX; 190 } 191 192 @Override 193 public int translateY(int y) { 194 return y - mOriginY; 195 } 196} 197