135ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka/*
235ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka * Copyright (C) 2012 The Android Open Source Project
335ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka *
48aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License");
58aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * you may not use this file except in compliance with the License.
68aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * You may obtain a copy of the License at
735ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka *
88aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka *      http://www.apache.org/licenses/LICENSE-2.0
935ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka *
1035ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka * Unless required by applicable law or agreed to in writing, software
118aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS,
128aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * See the License for the specific language governing permissions and
148aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * limitations under the License.
1535ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka */
1635ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka
1735ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaokapackage com.android.inputmethod.keyboard.internal;
1835ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka
1915f6d4ae34664ea3d92827a2c3003198c0bac70bTadashi G. Takaokaimport com.android.inputmethod.annotations.UsedForTesting;
202dae79b1966a7970c25c8b79beec1c95c13f6c87Tadashi G. Takaokaimport com.android.inputmethod.latin.define.DebugFlags;
2135ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka
2217752016713b92a55e9c2356d07b7ed51c67416bTadashi G. Takaokapublic final class TouchPositionCorrection {
2335ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka    private static final int TOUCH_POSITION_CORRECTION_RECORD_SIZE = 3;
2435ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka
256ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    private boolean mEnabled;
266ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    private float[] mXs;
276ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    private float[] mYs;
286ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    private float[] mRadii;
2935ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka
3035ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka    public void load(final String[] data) {
3135ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        final int dataLength = data.length;
3235ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        if (dataLength % TOUCH_POSITION_CORRECTION_RECORD_SIZE != 0) {
332dae79b1966a7970c25c8b79beec1c95c13f6c87Tadashi G. Takaoka            if (DebugFlags.DEBUG_ENABLED) {
3435ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                throw new RuntimeException(
3535ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                        "the size of touch position correction data is invalid");
3635ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka            }
3735ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka            return;
3835ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        }
3935ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka
4035ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        final int length = dataLength / TOUCH_POSITION_CORRECTION_RECORD_SIZE;
4135ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        mXs = new float[length];
4235ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        mYs = new float[length];
4335ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        mRadii = new float[length];
4435ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        try {
4535ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka            for (int i = 0; i < dataLength; ++i) {
4635ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                final int type = i % TOUCH_POSITION_CORRECTION_RECORD_SIZE;
4735ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                final int index = i / TOUCH_POSITION_CORRECTION_RECORD_SIZE;
4835ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                final float value = Float.parseFloat(data[i]);
4935ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                if (type == 0) {
5035ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                    mXs[index] = value;
5135ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                } else if (type == 1) {
5235ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                    mYs[index] = value;
5335ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                } else {
5435ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                    mRadii[index] = value;
5535ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                }
5635ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka            }
576ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka            mEnabled = dataLength > 0;
5835ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        } catch (NumberFormatException e) {
592dae79b1966a7970c25c8b79beec1c95c13f6c87Tadashi G. Takaoka            if (DebugFlags.DEBUG_ENABLED) {
6035ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                throw new RuntimeException(
6135ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka                        "the number format for touch position correction data is invalid");
6235ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka            }
636ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka            mEnabled = false;
6435ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka            mXs = null;
6535ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka            mYs = null;
6635ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka            mRadii = null;
6735ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        }
6835ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka    }
6935ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka
7015f6d4ae34664ea3d92827a2c3003198c0bac70bTadashi G. Takaoka    @UsedForTesting
7135ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka    public void setEnabled(final boolean enabled) {
7235ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka        mEnabled = enabled;
7335ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka    }
7435ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka
7535ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka    public boolean isValid() {
766ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka        return mEnabled;
776ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    }
786ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka
796ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    public int getRows() {
806ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka        return mRadii.length;
816ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    }
826ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka
83d3a4c5132422b189c8dbb94dbbe84a9b9761b0a8Tadashi G. Takaoka    @SuppressWarnings({ "static-method", "unused" })
846ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    public float getX(final int row) {
8517752016713b92a55e9c2356d07b7ed51c67416bTadashi G. Takaoka        return 0.0f;
8617752016713b92a55e9c2356d07b7ed51c67416bTadashi G. Takaoka        // Touch position correction data for X coordinate is obsolete.
8717752016713b92a55e9c2356d07b7ed51c67416bTadashi G. Takaoka        // return mXs[row];
886ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    }
896ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka
906ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    public float getY(final int row) {
916ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka        return mYs[row];
926ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    }
936ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka
946ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka    public float getRadius(final int row) {
956ca50d99208efdbcad96b3260fe7592bf95a6b00Tadashi G. Takaoka        return mRadii[row];
9635ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka    }
9735ff94547c16c84c5b6fafdae0b4a683be782b97Tadashi G. Takaoka}
98