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