Merge "Add ResouceUtils.getDefaultKeyboardHeight"

main
Tadashi G. Takaoka 2013-08-07 10:32:29 +00:00 committed by Android (Google) Code Review
commit 15b80c4ec2
4 changed files with 46 additions and 54 deletions

View File

@ -18,7 +18,6 @@ package com.android.inputmethod.keyboard;
import static com.android.inputmethod.latin.Constants.Subtype.ExtraValue.KEYBOARD_LAYOUT_SET;
import android.content.res.Configuration;
import android.text.InputType;
import android.text.TextUtils;
import android.view.inputmethod.EditorInfo;

View File

@ -23,13 +23,10 @@ import static com.android.inputmethod.latin.Constants.ImeOption.NO_SETTINGS_KEY;
import static com.android.inputmethod.latin.Constants.Subtype.ExtraValue.ASCII_CAPABLE;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.text.InputType;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.util.Xml;
@ -75,7 +72,7 @@ public final class KeyboardLayoutSet {
private static final String KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX = "keyboard_layout_set_";
private static final int SPELLCHECKER_DUMMY_KEYBOARD_WIDTH = 480;
private static final int SPELLCHECKER_DUMMY_KEYBOARD_HEIGHT = 800;
private static final int SPELLCHECKER_DUMMY_KEYBOARD_HEIGHT = 368;
private final Context mContext;
private final Params mParams;
@ -240,38 +237,12 @@ public final class KeyboardLayoutSet {
mPackageName, NO_SETTINGS_KEY, mEditorInfo);
}
public Builder setScreenGeometry(final int widthPixels, final int heightPixels) {
setDefaultKeyboardSize(widthPixels, heightPixels);
public Builder setKeyboardGeometry(final int keyboardWidth, final int keyboardHeight) {
mParams.mKeyboardWidth = keyboardWidth;
mParams.mKeyboardHeight = keyboardHeight;
return this;
}
private void setDefaultKeyboardSize(final int widthPixels, final int heightPixels) {
final String keyboardHeightString = ResourceUtils.getDeviceOverrideValue(
mResources, R.array.keyboard_heights);
final float keyboardHeight;
if (TextUtils.isEmpty(keyboardHeightString)) {
keyboardHeight = mResources.getDimension(R.dimen.keyboardHeight);
} else {
keyboardHeight = Float.parseFloat(keyboardHeightString)
* mResources.getDisplayMetrics().density;
}
final float maxKeyboardHeight = mResources.getFraction(
R.fraction.maxKeyboardHeight, heightPixels, heightPixels);
float minKeyboardHeight = mResources.getFraction(
R.fraction.minKeyboardHeight, heightPixels, heightPixels);
if (minKeyboardHeight < 0.0f) {
// Specified fraction was negative, so it should be calculated against display
// width.
minKeyboardHeight = -mResources.getFraction(
R.fraction.minKeyboardHeight, widthPixels, widthPixels);
}
// Keyboard height will not exceed maxKeyboardHeight and will not be less than
// minKeyboardHeight.
mParams.mKeyboardHeight = (int)Math.max(
Math.min(keyboardHeight, maxKeyboardHeight), minKeyboardHeight);
mParams.mKeyboardWidth = widthPixels;
}
public Builder setSubtype(final InputMethodSubtype subtype) {
final boolean asciiCapable = subtype.containsExtraValueKey(ASCII_CAPABLE);
@SuppressWarnings("deprecation")
@ -450,31 +421,21 @@ public final class KeyboardLayoutSet {
public static KeyboardLayoutSet createKeyboardSetForTest(final Context context,
final InputMethodSubtype subtype, final int orientation,
final boolean testCasesHaveTouchCoordinates) {
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
final int width;
final int height;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
width = Math.max(dm.widthPixels, dm.heightPixels);
height = Math.min(dm.widthPixels, dm.heightPixels);
} else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
width = Math.min(dm.widthPixels, dm.heightPixels);
height = Math.max(dm.widthPixels, dm.heightPixels);
} else {
throw new RuntimeException("Orientation should be ORIENTATION_LANDSCAPE or "
+ "ORIENTATION_PORTRAIT: orientation=" + orientation);
}
return createKeyboardSet(context, subtype, width, height, testCasesHaveTouchCoordinates,
false /* isSpellChecker */);
final Resources res = context.getResources();
final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
final int keyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res);
return createKeyboardSet(context, subtype, keyboardWidth, keyboardHeight,
testCasesHaveTouchCoordinates, false /* isSpellChecker */);
}
private static KeyboardLayoutSet createKeyboardSet(final Context context,
final InputMethodSubtype subtype, final int width, final int height,
final InputMethodSubtype subtype, final int keyboardWidth, final int keyboardHeight,
final boolean testCasesHaveTouchCoordinates, final boolean isSpellChecker) {
final EditorInfo editorInfo = new EditorInfo();
editorInfo.inputType = InputType.TYPE_CLASS_TEXT;
final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
context, editorInfo);
builder.setScreenGeometry(width, height);
builder.setKeyboardGeometry(keyboardWidth, keyboardHeight);
builder.setSubtype(subtype);
builder.setIsSpellChecker(isSpellChecker);
if (!testCasesHaveTouchCoordinates) {

View File

@ -20,7 +20,6 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
@ -39,6 +38,7 @@ import com.android.inputmethod.latin.SubtypeSwitcher;
import com.android.inputmethod.latin.WordComposer;
import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.utils.ResourceUtils;
public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
private static final String TAG = KeyboardSwitcher.class.getSimpleName();
@ -137,8 +137,9 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
mThemeContext, editorInfo);
final Resources res = mThemeContext.getResources();
final DisplayMetrics dm = res.getDisplayMetrics();
builder.setScreenGeometry(dm.widthPixels, dm.heightPixels);
final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
final int keyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res);
builder.setKeyboardGeometry(keyboardWidth, keyboardHeight);
builder.setSubtype(mSubtypeSwitcher.getCurrentSubtype());
builder.setOptions(
settingsValues.isVoiceKeyEnabled(editorInfo),

View File

@ -20,10 +20,12 @@ import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.R;
import java.util.ArrayList;
import java.util.HashMap;
@ -215,6 +217,35 @@ public final class ResourceUtils {
return null;
}
public static int getDefaultKeyboardWidth(final Resources res) {
final DisplayMetrics dm = res.getDisplayMetrics();
return dm.widthPixels;
}
public static int getDefaultKeyboardHeight(final Resources res) {
final DisplayMetrics dm = res.getDisplayMetrics();
final String keyboardHeightString = getDeviceOverrideValue(res, R.array.keyboard_heights);
final float keyboardHeight;
if (TextUtils.isEmpty(keyboardHeightString)) {
keyboardHeight = res.getDimension(R.dimen.keyboardHeight);
} else {
keyboardHeight = Float.parseFloat(keyboardHeightString) * dm.density;
}
final float maxKeyboardHeight = res.getFraction(
R.fraction.maxKeyboardHeight, dm.heightPixels, dm.heightPixels);
float minKeyboardHeight = res.getFraction(
R.fraction.minKeyboardHeight, dm.heightPixels, dm.heightPixels);
if (minKeyboardHeight < 0.0f) {
// Specified fraction was negative, so it should be calculated against display
// width.
minKeyboardHeight = -res.getFraction(
R.fraction.minKeyboardHeight, dm.widthPixels, dm.widthPixels);
}
// Keyboard height will not exceed maxKeyboardHeight and will not be less than
// minKeyboardHeight.
return (int)Math.max(Math.min(keyboardHeight, maxKeyboardHeight), minKeyboardHeight);
}
public static boolean isValidFraction(final float fraction) {
return fraction >= 0.0f;
}