am 03288ef4: Refactor MoreKeysKeyboard.Builder a bit

* commit '03288ef47fd93758b5665e19fe9b892ece6e586f':
  Refactor MoreKeysKeyboard.Builder a bit
main
Tadashi G. Takaoka 2014-06-19 01:13:04 +00:00 committed by Android Git Automerger
commit 335028c039
2 changed files with 41 additions and 45 deletions

View File

@ -47,6 +47,7 @@ import com.android.inputmethod.keyboard.internal.KeyDrawParams;
import com.android.inputmethod.keyboard.internal.KeyPreviewChoreographer; import com.android.inputmethod.keyboard.internal.KeyPreviewChoreographer;
import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams; import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams;
import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper; import com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper;
import com.android.inputmethod.keyboard.internal.MoreKeySpec;
import com.android.inputmethod.keyboard.internal.NonDistinctMultitouchHelper; import com.android.inputmethod.keyboard.internal.NonDistinctMultitouchHelper;
import com.android.inputmethod.keyboard.internal.SlidingKeyInputDrawingPreview; import com.android.inputmethod.keyboard.internal.SlidingKeyInputDrawingPreview;
import com.android.inputmethod.keyboard.internal.TimerHandler; import com.android.inputmethod.keyboard.internal.TimerHandler;
@ -427,15 +428,6 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
windowContentView.addView(mDrawingPreviewPlacerView); windowContentView.addView(mDrawingPreviewPlacerView);
} }
/**
* Returns the enabled state of the key feedback preview
* @return whether or not the key feedback preview is enabled
* @see #setKeyPreviewPopupEnabled(boolean, int)
*/
public boolean isKeyPreviewPopupEnabled() {
return mKeyPreviewDrawParams.isPopupEnabled();
}
// Implements {@link DrawingHandler.Callbacks} method. // Implements {@link DrawingHandler.Callbacks} method.
@Override @Override
public void dismissAllKeyPreviews() { public void dismissAllKeyPreviews() {
@ -553,13 +545,25 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
} }
private MoreKeysPanel onCreateMoreKeysPanel(final Key key, final Context context) { private MoreKeysPanel onCreateMoreKeysPanel(final Key key, final Context context) {
if (key.getMoreKeys() == null) { final MoreKeySpec[] moreKeys = key.getMoreKeys();
if (moreKeys == null) {
return null; return null;
} }
Keyboard moreKeysKeyboard = mMoreKeysKeyboardCache.get(key); Keyboard moreKeysKeyboard = mMoreKeysKeyboardCache.get(key);
if (moreKeysKeyboard == null) { if (moreKeysKeyboard == null) {
moreKeysKeyboard = new MoreKeysKeyboard.Builder( // {@link KeyPreviewDrawParams#mPreviewVisibleWidth} should have been set at
context, key, this, mKeyPreviewDrawParams).build(); // {@link KeyPreviewChoreographer#placeKeyPreview(Key,TextView,KeyboardIconsSet,KeyDrawParams,int,int[]},
// though there may be some chances that the value is zero. <code>width == 0</code>
// will cause zero-division error at
// {@link MoreKeysKeyboardParams#setParameters(int,int,int,int,int,int,boolean,int)}.
final boolean singleMoreKeyWithPreview = mKeyPreviewDrawParams.isPopupEnabled()
&& !key.noKeyPreview() && moreKeys.length == 1
&& mKeyPreviewDrawParams.getVisibleWidth() > 0;
final MoreKeysKeyboard.Builder builder = new MoreKeysKeyboard.Builder(
context, key, getKeyboard(), singleMoreKeyWithPreview,
mKeyPreviewDrawParams.getVisibleWidth(),
mKeyPreviewDrawParams.getVisibleHeight(), newLabelPaint(key));
moreKeysKeyboard = builder.build();
mMoreKeysKeyboardCache.put(key, moreKeysKeyboard); mMoreKeysKeyboardCache.put(key, moreKeysKeyboard);
} }
@ -615,7 +619,8 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
final int[] lastCoords = CoordinateUtils.newInstance(); final int[] lastCoords = CoordinateUtils.newInstance();
tracker.getLastCoordinates(lastCoords); tracker.getLastCoordinates(lastCoords);
final boolean keyPreviewEnabled = isKeyPreviewPopupEnabled() && !key.noKeyPreview(); final boolean keyPreviewEnabled = mKeyPreviewDrawParams.isPopupEnabled()
&& !key.noKeyPreview();
// The more keys keyboard is usually horizontally aligned with the center of the parent key. // The more keys keyboard is usually horizontally aligned with the center of the parent key.
// If showMoreKeysKeyboardAtTouchedPoint is true and the key preview is disabled, the more // If showMoreKeysKeyboardAtTouchedPoint is true and the key preview is disabled, the more
// keys keyboard is placed at the touch point of the parent key. // keys keyboard is placed at the touch point of the parent key.

View File

@ -21,7 +21,6 @@ import android.graphics.Paint;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.keyboard.internal.KeyboardParams; import com.android.inputmethod.keyboard.internal.KeyboardParams;
@ -260,32 +259,25 @@ public final class MoreKeysKeyboard extends Keyboard {
/** /**
* The builder of MoreKeysKeyboard. * The builder of MoreKeysKeyboard.
* @param context the context of {@link MoreKeysKeyboardView}. * @param context the context of {@link MoreKeysKeyboardView}.
* @param parentKey the {@link Key} that invokes more keys keyboard. * @param key the {@link Key} that invokes more keys keyboard.
* @param parentKeyboardView the {@link KeyboardView} that contains the parentKey. * @param keyboard the {@link Keyboard} that contains the parentKey.
* @param singleMoreKeyWithPreview true if the <code>key</code> has only one more key
* and key popup preview is enabled.
* @param keyPreviewDrawParams the parameter to place key preview. * @param keyPreviewDrawParams the parameter to place key preview.
* @param paintToMeasure the {@link Paint} object to measure a more key width
*/ */
public Builder(final Context context, final Key parentKey, public Builder(final Context context, final Key key, final Keyboard keyboard,
final MainKeyboardView parentKeyboardView, final boolean singleMoreKeyWithPreview, final int keyPreviewVisibleWidth,
final KeyPreviewDrawParams keyPreviewDrawParams) { final int keyPreviewVisibleHeight, final Paint paintToMeasure) {
super(context, new MoreKeysKeyboardParams()); super(context, new MoreKeysKeyboardParams());
final Keyboard parentKeyboard = parentKeyboardView.getKeyboard(); load(keyboard.mMoreKeysTemplate, keyboard.mId);
load(parentKeyboard.mMoreKeysTemplate, parentKeyboard.mId);
// TODO: More keys keyboard's vertical gap is currently calculated heuristically. // TODO: More keys keyboard's vertical gap is currently calculated heuristically.
// Should revise the algorithm. // Should revise the algorithm.
mParams.mVerticalGap = parentKeyboard.mVerticalGap / 2; mParams.mVerticalGap = keyboard.mVerticalGap / 2;
mParentKey = parentKey; mParentKey = key;
final MoreKeySpec[] moreKeys = parentKey.getMoreKeys(); final int keyWidth, rowHeight;
final int width, height;
// {@link KeyPreviewDrawParams#mPreviewVisibleWidth} should have been set at
// {@link MainKeyboardView#showKeyPreview(PointerTracker}, though there may be
// some chances that the value is zero. <code>width == 0</code> will cause
// zero-division error at
// {@link MoreKeysKeyboardParams#setParameters(int,int,int,int,int,int,boolean,int)}.
final boolean singleMoreKeyWithPreview = parentKeyboardView.isKeyPreviewPopupEnabled()
&& !parentKey.noKeyPreview() && moreKeys.length == 1
&& keyPreviewDrawParams.getVisibleWidth() > 0;
if (singleMoreKeyWithPreview) { if (singleMoreKeyWithPreview) {
// Use pre-computed width and height if this more keys keyboard has only one key to // Use pre-computed width and height if this more keys keyboard has only one key to
// mitigate visual flicker between key preview and more keys keyboard. // mitigate visual flicker between key preview and more keys keyboard.
@ -294,29 +286,28 @@ public final class MoreKeysKeyboard extends Keyboard {
// left/right/top paddings. The bottom paddings of both backgrounds don't need to // left/right/top paddings. The bottom paddings of both backgrounds don't need to
// be considered because the vertical positions of both backgrounds were already // be considered because the vertical positions of both backgrounds were already
// adjusted with their bottom paddings deducted. // adjusted with their bottom paddings deducted.
width = keyPreviewDrawParams.getVisibleWidth(); keyWidth = keyPreviewVisibleWidth;
height = keyPreviewDrawParams.getVisibleHeight() + mParams.mVerticalGap; rowHeight = keyPreviewVisibleHeight + mParams.mVerticalGap;
} else { } else {
final float padding = context.getResources().getDimension( final float padding = context.getResources().getDimension(
R.dimen.config_more_keys_keyboard_key_horizontal_padding) R.dimen.config_more_keys_keyboard_key_horizontal_padding)
+ (parentKey.hasLabelsInMoreKeys() + (key.hasLabelsInMoreKeys()
? mParams.mDefaultKeyWidth * LABEL_PADDING_RATIO : 0.0f); ? mParams.mDefaultKeyWidth * LABEL_PADDING_RATIO : 0.0f);
width = getMaxKeyWidth(parentKey, mParams.mDefaultKeyWidth, padding, keyWidth = getMaxKeyWidth(key, mParams.mDefaultKeyWidth, padding, paintToMeasure);
parentKeyboardView.newLabelPaint(parentKey)); rowHeight = keyboard.mMostCommonKeyHeight;
height = parentKeyboard.mMostCommonKeyHeight;
} }
final int dividerWidth; final int dividerWidth;
if (parentKey.needsDividersInMoreKeys()) { if (key.needsDividersInMoreKeys()) {
mDivider = mResources.getDrawable(R.drawable.more_keys_divider); mDivider = mResources.getDrawable(R.drawable.more_keys_divider);
dividerWidth = (int)(width * DIVIDER_RATIO); dividerWidth = (int)(keyWidth * DIVIDER_RATIO);
} else { } else {
mDivider = null; mDivider = null;
dividerWidth = 0; dividerWidth = 0;
} }
mParams.setParameters(moreKeys.length, parentKey.getMoreKeysColumn(), final MoreKeySpec[] moreKeys = key.getMoreKeys();
width, height, parentKey.getX() + parentKey.getWidth() / 2, mParams.setParameters(moreKeys.length, key.getMoreKeysColumn(), keyWidth, rowHeight,
parentKeyboard.mId.mWidth, parentKey.isFixedColumnOrderMoreKeys(), key.getX() + key.getWidth() / 2, keyboard.mId.mWidth,
dividerWidth); key.isFixedColumnOrderMoreKeys(), dividerWidth);
} }
private static int getMaxKeyWidth(final Key parentKey, final int minKeyWidth, private static int getMaxKeyWidth(final Key parentKey, final int minKeyWidth,