1/*
2 * Copyright (C) 2014 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 android.hardware.input;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * Encapsulates calibration data for input devices.
24 *
25 * @hide
26 */
27public class TouchCalibration implements Parcelable {
28
29    public static final TouchCalibration IDENTITY = new TouchCalibration();
30
31    public static final Parcelable.Creator<TouchCalibration> CREATOR
32            = new Parcelable.Creator<TouchCalibration>() {
33        public TouchCalibration createFromParcel(Parcel in) {
34            return new TouchCalibration(in);
35        }
36
37        public TouchCalibration[] newArray(int size) {
38            return new TouchCalibration[size];
39        }
40    };
41
42    private final float mXScale, mXYMix, mXOffset;
43    private final float mYXMix, mYScale, mYOffset;
44
45    /**
46     * Create a new TouchCalibration initialized to the identity transformation.
47     */
48    public TouchCalibration() {
49        this(1,0,0,0,1,0);
50    }
51
52    /**
53     * Create a new TouchCalibration from affine transformation paramters.
54     * @param xScale   Influence of input x-axis value on output x-axis value.
55     * @param xyMix    Influence of input y-axis value on output x-axis value.
56     * @param xOffset  Constant offset to be applied to output x-axis value.
57     * @param yXMix    Influence of input x-axis value on output y-axis value.
58     * @param yScale   Influence of input y-axis value on output y-axis value.
59     * @param yOffset  Constant offset to be applied to output y-axis value.
60     */
61    public TouchCalibration(float xScale, float xyMix, float xOffset,
62            float yxMix, float yScale, float yOffset) {
63        mXScale  = xScale;
64        mXYMix   = xyMix;
65        mXOffset = xOffset;
66        mYXMix   = yxMix;
67        mYScale  = yScale;
68        mYOffset = yOffset;
69    }
70
71    public TouchCalibration(Parcel in) {
72        mXScale  = in.readFloat();
73        mXYMix   = in.readFloat();
74        mXOffset = in.readFloat();
75        mYXMix   = in.readFloat();
76        mYScale  = in.readFloat();
77        mYOffset = in.readFloat();
78    }
79
80    @Override
81    public void writeToParcel(Parcel dest, int flags) {
82        dest.writeFloat(mXScale);
83        dest.writeFloat(mXYMix);
84        dest.writeFloat(mXOffset);
85        dest.writeFloat(mYXMix);
86        dest.writeFloat(mYScale);
87        dest.writeFloat(mYOffset);
88    }
89
90    @Override
91    public int describeContents() {
92        return 0;
93    }
94
95    public float[] getAffineTransform() {
96        return new float[] { mXScale, mXYMix, mXOffset, mYXMix, mYScale, mYOffset };
97    }
98
99    @Override
100    public boolean equals(Object obj) {
101        if (obj == this) {
102            return true;
103        } else if (obj instanceof TouchCalibration) {
104            TouchCalibration cal = (TouchCalibration)obj;
105
106            return (cal.mXScale  == mXScale)  &&
107                   (cal.mXYMix   == mXYMix)   &&
108                   (cal.mXOffset == mXOffset) &&
109                   (cal.mYXMix   == mYXMix)   &&
110                   (cal.mYScale  == mYScale)  &&
111                   (cal.mYOffset == mYOffset);
112        } else {
113            return false;
114        }
115    }
116
117    @Override
118    public int hashCode() {
119        return Float.floatToIntBits(mXScale)  ^
120               Float.floatToIntBits(mXYMix)   ^
121               Float.floatToIntBits(mXOffset) ^
122               Float.floatToIntBits(mYXMix)   ^
123               Float.floatToIntBits(mYScale)  ^
124               Float.floatToIntBits(mYOffset);
125    }
126
127    @Override
128    public String toString() {
129        return String.format("[%f, %f, %f, %f, %f, %f]",
130                mXScale, mXYMix, mXOffset, mYXMix, mYScale, mYOffset);
131    }
132}
133