get Default Keyboard Height - Android User Interface

Android examples for User Interface:Input Method

Description

get Default Keyboard Height

Demo Code

/*//w  ww .  ja  v  a2  s .  c  o  m
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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;
import java.util.regex.PatternSyntaxException;

public class Main{
    private static final String TAG = ResourceUtils.class.getSimpleName();
    public static final float UNDEFINED_RATIO = -1.0f;
    private static final HashMap<String, String> sDeviceOverrideValueMap = CollectionUtils
            .newHashMap();
    private static final HashMap<String, String> sBuildKeyValues;
    private static final String sBuildKeyValuesDebugString;
    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 String getDeviceOverrideValue(final Resources res,
            final int overrideResId) {
        final int orientation = res.getConfiguration().orientation;
        final String key = overrideResId + "-" + orientation;
        if (sDeviceOverrideValueMap.containsKey(key)) {
            return sDeviceOverrideValueMap.get(key);
        }

        final String[] overrideArray = res.getStringArray(overrideResId);
        final String overrideValue = findConstantForKeyValuePairs(
                sBuildKeyValues, overrideArray);
        // The overrideValue might be an empty string.
        if (overrideValue != null) {
            Log.i(TAG,
                    "Find override value:" + " resource="
                            + res.getResourceEntryName(overrideResId)
                            + " build=" + sBuildKeyValuesDebugString
                            + " override=" + overrideValue);
            sDeviceOverrideValueMap.put(key, overrideValue);
            return overrideValue;
        }

        String defaultValue = null;
        try {
            defaultValue = findDefaultConstant(overrideArray);
            // The defaultValue might be an empty string.
            if (defaultValue == null) {
                Log.w(TAG,
                        "Couldn't find override value nor default value:"
                                + " resource="
                                + res.getResourceEntryName(overrideResId)
                                + " build=" + sBuildKeyValuesDebugString);
            } else {
                Log.i(TAG,
                        "Found default value:" + " resource="
                                + res.getResourceEntryName(overrideResId)
                                + " build=" + sBuildKeyValuesDebugString
                                + " default=" + defaultValue);
            }
        } catch (final DeviceOverridePatternSyntaxError e) {
            Log.w(TAG, "Syntax error, ignored", e);
        }
        sDeviceOverrideValueMap.put(key, defaultValue);
        return defaultValue;
    }
    public static float getFraction(final TypedArray a, final int index,
            final float defValue) {
        final TypedValue value = a.peekValue(index);
        if (value == null || !isFractionValue(value)) {
            return defValue;
        }
        return a.getFraction(index, 1, 1, defValue);
    }
    public static float getFraction(final TypedArray a, final int index) {
        return getFraction(a, index, UNDEFINED_RATIO);
    }
    /**
     * Find the condition that fulfills specified key value pairs from an array of
     * "condition,constant", and return the corresponding string constant. A condition is
     * "pattern1[:pattern2...] (or an empty string for the default). A pattern is
     * "key=regexp_value" string. The condition matches only if all patterns of the condition
     * are true for the specified key value pairs.
     *
     * For example, "condition,constant" has the following format.
     * (See {@link ResourceUtilsTests#testFindConstantForKeyValuePairsRegexp()})
     *  - HARDWARE=mako,constantForNexus4
     *  - MODEL=Nexus 4:MANUFACTURER=LGE,constantForNexus4
     *  - ,defaultConstant
     *
     * @param keyValuePairs attributes to be used to look for a matched condition.
     * @param conditionConstantArray an array of "condition,constant" elements to be searched.
     * @return the constant part of the matched "condition,constant" element. Returns null if no
     * condition matches.
     */
    @UsedForTesting
    static String findConstantForKeyValuePairs(
            final HashMap<String, String> keyValuePairs,
            final String[] conditionConstantArray) {
        if (conditionConstantArray == null || keyValuePairs == null) {
            return null;
        }
        String foundValue = null;
        for (final String conditionConstant : conditionConstantArray) {
            final int posComma = conditionConstant.indexOf(',');
            if (posComma < 0) {
                Log.w(TAG, "Array element has no comma: "
                        + conditionConstant);
                continue;
            }
            final String condition = conditionConstant.substring(0,
                    posComma);
            if (condition.isEmpty()) {
                // Default condition. The default condition should be searched by
                // {@link #findConstantForDefault(String[])}.
                continue;
            }
            try {
                if (fulfillsCondition(keyValuePairs, condition)) {
                    // Take first match
                    if (foundValue == null) {
                        foundValue = conditionConstant
                                .substring(posComma + 1);
                    }
                    // And continue walking through all conditions.
                }
            } catch (final DeviceOverridePatternSyntaxError e) {
                Log.w(TAG, "Syntax error, ignored", e);
            }
        }
        return foundValue;
    }
    @UsedForTesting
    static String findDefaultConstant(final String[] conditionConstantArray)
            throws DeviceOverridePatternSyntaxError {
        if (conditionConstantArray == null) {
            return null;
        }
        for (final String condition : conditionConstantArray) {
            final int posComma = condition.indexOf(',');
            if (posComma < 0) {
                throw new DeviceOverridePatternSyntaxError(
                        "Array element has no comma", condition);
            }
            if (posComma == 0) { // condition is empty.
                return condition.substring(posComma + 1);
            }
        }
        return null;
    }
    public static boolean isFractionValue(final TypedValue v) {
        return v.type == TypedValue.TYPE_FRACTION;
    }
    private static boolean fulfillsCondition(
            final HashMap<String, String> keyValuePairs,
            final String condition) throws DeviceOverridePatternSyntaxError {
        final String[] patterns = condition.split(":");
        // Check all patterns in a condition are true
        boolean matchedAll = true;
        for (final String pattern : patterns) {
            final int posEqual = pattern.indexOf('=');
            if (posEqual < 0) {
                throw new DeviceOverridePatternSyntaxError(
                        "Pattern has no '='", condition);
            }
            final String key = pattern.substring(0, posEqual);
            final String value = keyValuePairs.get(key);
            if (value == null) {
                throw new DeviceOverridePatternSyntaxError("Unknown key",
                        condition);
            }
            final String patternRegexpValue = pattern
                    .substring(posEqual + 1);
            try {
                if (!value.matches(patternRegexpValue)) {
                    matchedAll = false;
                    // And continue walking through all patterns.
                }
            } catch (final PatternSyntaxException e) {
                throw new DeviceOverridePatternSyntaxError("Syntax error",
                        condition, e);
            }
        }
        return matchedAll;
    }
}

Related Tutorials