1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.keyboard.internal;
18
19import com.android.inputmethod.latin.LatinImeLogger;
20
21public final class TouchPositionCorrection {
22    private static final int TOUCH_POSITION_CORRECTION_RECORD_SIZE = 3;
23
24    private boolean mEnabled;
25    private float[] mXs;
26    private float[] mYs;
27    private float[] mRadii;
28
29    public void load(final String[] data) {
30        final int dataLength = data.length;
31        if (dataLength % TOUCH_POSITION_CORRECTION_RECORD_SIZE != 0) {
32            if (LatinImeLogger.sDBG) {
33                throw new RuntimeException(
34                        "the size of touch position correction data is invalid");
35            }
36            return;
37        }
38
39        final int length = dataLength / TOUCH_POSITION_CORRECTION_RECORD_SIZE;
40        mXs = new float[length];
41        mYs = new float[length];
42        mRadii = new float[length];
43        try {
44            for (int i = 0; i < dataLength; ++i) {
45                final int type = i % TOUCH_POSITION_CORRECTION_RECORD_SIZE;
46                final int index = i / TOUCH_POSITION_CORRECTION_RECORD_SIZE;
47                final float value = Float.parseFloat(data[i]);
48                if (type == 0) {
49                    mXs[index] = value;
50                } else if (type == 1) {
51                    mYs[index] = value;
52                } else {
53                    mRadii[index] = value;
54                }
55            }
56            mEnabled = dataLength > 0;
57        } catch (NumberFormatException e) {
58            if (LatinImeLogger.sDBG) {
59                throw new RuntimeException(
60                        "the number format for touch position correction data is invalid");
61            }
62            mEnabled = false;
63            mXs = null;
64            mYs = null;
65            mRadii = null;
66        }
67    }
68
69    // For test only
70    public void setEnabled(final boolean enabled) {
71        mEnabled = enabled;
72    }
73
74    public boolean isValid() {
75        return mEnabled;
76    }
77
78    public int getRows() {
79        return mRadii.length;
80    }
81
82    public float getX(final int row) {
83        return 0.0f;
84        // Touch position correction data for X coordinate is obsolete.
85        // return mXs[row];
86    }
87
88    public float getY(final int row) {
89        return mYs[row];
90    }
91
92    public float getRadius(final int row) {
93        return mRadii[row];
94    }
95}
96